src/share/vm/jfr/writers/jfrWriterHost.inline.hpp

Tue, 02 Jun 2020 14:29:43 +0800

author
ddong
date
Tue, 02 Jun 2020 14:29:43 +0800
changeset 9941
45c8de52649c
parent 9858
b985cbb00e68
child 9947
db357034b763
permissions
-rw-r--r--

8246310: Clean commented-out code about ModuleEntry andPackageEntry in JFR
Reviewed-by: adinn

apetushkov@9858 1 /*
apetushkov@9858 2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
apetushkov@9858 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
apetushkov@9858 4 *
apetushkov@9858 5 * This code is free software; you can redistribute it and/or modify it
apetushkov@9858 6 * under the terms of the GNU General Public License version 2 only, as
apetushkov@9858 7 * published by the Free Software Foundation.
apetushkov@9858 8 *
apetushkov@9858 9 * This code is distributed in the hope that it will be useful, but WITHOUT
apetushkov@9858 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
apetushkov@9858 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
apetushkov@9858 12 * version 2 for more details (a copy is included in the LICENSE file that
apetushkov@9858 13 * accompanied this code).
apetushkov@9858 14 *
apetushkov@9858 15 * You should have received a copy of the GNU General Public License version
apetushkov@9858 16 * 2 along with this work; if not, write to the Free Software Foundation,
apetushkov@9858 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
apetushkov@9858 18 *
apetushkov@9858 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
apetushkov@9858 20 * or visit www.oracle.com if you need additional information or have any
apetushkov@9858 21 * questions.
apetushkov@9858 22 *
apetushkov@9858 23 */
apetushkov@9858 24
apetushkov@9858 25 #ifndef SHARE_VM_JFR_WRITERS_JFRWRITERHOST_INLINE_HPP
apetushkov@9858 26 #define SHARE_VM_JFR_WRITERS_JFRWRITERHOST_INLINE_HPP
apetushkov@9858 27
apetushkov@9858 28 #include "classfile/javaClasses.hpp"
apetushkov@9858 29 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
apetushkov@9858 30 #include "jfr/recorder/service/jfrOptionSet.hpp"
apetushkov@9858 31 #include "jfr/writers/jfrEncoding.hpp"
apetushkov@9858 32 #include "jfr/writers/jfrWriterHost.hpp"
apetushkov@9858 33 #include "memory/resourceArea.hpp"
apetushkov@9858 34 #include "oops/oop.hpp"
apetushkov@9858 35 #include "oops/symbol.hpp"
apetushkov@9858 36
apetushkov@9858 37 inline bool compressed_integers() {
apetushkov@9858 38 static const bool comp_integers = JfrOptionSet::compressed_integers();
apetushkov@9858 39 return comp_integers;
apetushkov@9858 40 }
apetushkov@9858 41
apetushkov@9858 42 template <typename BE, typename IE, typename WriterPolicyImpl >
apetushkov@9858 43 template <typename T>
apetushkov@9858 44 inline void WriterHost<BE, IE, WriterPolicyImpl>::write_padded(T value) {
apetushkov@9858 45 write_padded(&value, 1);
apetushkov@9858 46 }
apetushkov@9858 47
apetushkov@9858 48 template <typename BE, typename IE, typename WriterPolicyImpl >
apetushkov@9858 49 template <typename T>
apetushkov@9858 50 inline void WriterHost<BE, IE, WriterPolicyImpl>::write_padded(const T* value, size_t len) {
apetushkov@9858 51 assert(value != NULL, "invariant");
apetushkov@9858 52 assert(len > 0, "invariant");
apetushkov@9858 53 u1* const pos = ensure_size(sizeof(T) * len);
apetushkov@9858 54 if (pos) {
apetushkov@9858 55 this->set_current_pos(write_padded(value, len, pos));
apetushkov@9858 56 }
apetushkov@9858 57 }
apetushkov@9858 58
apetushkov@9858 59 template <typename BE, typename IE, typename WriterPolicyImpl >
apetushkov@9858 60 template <typename T>
apetushkov@9858 61 inline u1* WriterHost<BE, IE, WriterPolicyImpl>::write_padded(const T* value, size_t len, u1* pos) {
apetushkov@9858 62 assert(value != NULL, "invariant");
apetushkov@9858 63 assert(len > 0, "invariant");
apetushkov@9858 64 assert(pos != NULL, "invariant");
apetushkov@9858 65 return _compressed_integers ? IE::write_padded(value, len, pos) : BE::write_padded(value, len, pos);
apetushkov@9858 66 }
apetushkov@9858 67
apetushkov@9858 68 template <typename BE, typename IE, typename WriterPolicyImpl >
apetushkov@9858 69 template <typename T>
apetushkov@9858 70 inline void WriterHost<BE, IE, WriterPolicyImpl>::write(const T* value, size_t len) {
apetushkov@9858 71 assert(value != NULL, "invariant");
apetushkov@9858 72 assert(len > 0, "invariant");
apetushkov@9858 73 u1* const pos = ensure_size(sizeof(T) * len);
apetushkov@9858 74 if (pos) {
apetushkov@9858 75 this->set_current_pos(write(value, len, pos));
apetushkov@9858 76 }
apetushkov@9858 77 }
apetushkov@9858 78
apetushkov@9858 79 template <typename BE, typename IE, typename WriterPolicyImpl >
apetushkov@9858 80 template <typename T>
apetushkov@9858 81 inline u1* WriterHost<BE, IE, WriterPolicyImpl>::write(const T* value, size_t len, u1* pos) {
apetushkov@9858 82 assert(value != NULL, "invariant");
apetushkov@9858 83 assert(len > 0, "invariant");
apetushkov@9858 84 assert(pos != NULL, "invariant");
apetushkov@9858 85 return _compressed_integers ? IE::write(value, len, pos) : BE::write(value, len, pos);
apetushkov@9858 86 }
apetushkov@9858 87
apetushkov@9858 88 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 89 void WriterHost<BE, IE, WriterPolicyImpl>::write_utf8(const char* value) {
apetushkov@9858 90 if (NULL == value) {
apetushkov@9858 91 // only write encoding byte indicating NULL string
apetushkov@9858 92 write<u1>(NULL_STRING);
apetushkov@9858 93 return;
apetushkov@9858 94 }
apetushkov@9858 95 write<u1>(UTF8); // designate encoding
apetushkov@9858 96 const jint len = MIN2<jint>(max_jint, (jint)strlen(value));
apetushkov@9858 97 write(len);
apetushkov@9858 98 if (len > 0) {
apetushkov@9858 99 be_write(value, len);
apetushkov@9858 100 }
apetushkov@9858 101 }
apetushkov@9858 102
apetushkov@9858 103 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 104 void WriterHost<BE, IE, WriterPolicyImpl>::write_utf16(const jchar* value, jint len) {
apetushkov@9858 105 assert(value != NULL, "invariant");
apetushkov@9858 106 write((u1)UTF16); // designate encoding
apetushkov@9858 107 write(len);
apetushkov@9858 108 if (len > 0) {
apetushkov@9858 109 write(value, len);
apetushkov@9858 110 }
apetushkov@9858 111 }
apetushkov@9858 112
apetushkov@9858 113 template <typename BE, typename IE, typename WriterPolicyImpl >
apetushkov@9858 114 template <typename T>
apetushkov@9858 115 inline void WriterHost<BE, IE, WriterPolicyImpl>::be_write(T value) {
apetushkov@9858 116 u1* const pos = ensure_size(sizeof(T));
apetushkov@9858 117 if (pos) {
apetushkov@9858 118 this->set_current_pos(BE::be_write(&value, 1, pos));
apetushkov@9858 119 }
apetushkov@9858 120 }
apetushkov@9858 121
apetushkov@9858 122 template <typename BE, typename IE, typename WriterPolicyImpl >
apetushkov@9858 123 template <typename T>
apetushkov@9858 124 inline void WriterHost<BE, IE, WriterPolicyImpl>::be_write(const T* value, size_t len) {
apetushkov@9858 125 assert(value != NULL, "invariant");
apetushkov@9858 126 assert(len > 0, "invariant");
apetushkov@9858 127 u1* const pos = ensure_size(sizeof(T) * len);
apetushkov@9858 128 if (pos) {
apetushkov@9858 129 this->set_current_pos(BE::be_write(value, len, pos));
apetushkov@9858 130 }
apetushkov@9858 131 }
apetushkov@9858 132
apetushkov@9858 133 template <typename BE, typename IE, typename WriterPolicyImpl >
apetushkov@9858 134 template <typename StorageType>
apetushkov@9858 135 inline WriterHost<BE, IE, WriterPolicyImpl>::WriterHost(StorageType* storage, Thread* thread) :
apetushkov@9858 136 WriterPolicyImpl(storage, thread),
apetushkov@9858 137 _compressed_integers(compressed_integers()) {
apetushkov@9858 138 }
apetushkov@9858 139
apetushkov@9858 140 template <typename BE, typename IE, typename WriterPolicyImpl >
apetushkov@9858 141 template <typename StorageType>
apetushkov@9858 142 inline WriterHost<BE, IE, WriterPolicyImpl>::WriterHost(StorageType* storage, size_t size) :
apetushkov@9858 143 WriterPolicyImpl(storage, size),
apetushkov@9858 144 _compressed_integers(compressed_integers()) {
apetushkov@9858 145 }
apetushkov@9858 146
apetushkov@9858 147 template <typename BE, typename IE, typename WriterPolicyImpl >
apetushkov@9858 148 inline WriterHost<BE, IE, WriterPolicyImpl>::WriterHost(Thread* thread) :
apetushkov@9858 149 WriterPolicyImpl(thread),
apetushkov@9858 150 _compressed_integers(compressed_integers()) {
apetushkov@9858 151 }
apetushkov@9858 152
apetushkov@9858 153 // Extra size added as a safety cushion when dimensioning memory.
apetushkov@9858 154 // With varint encoding, the worst case is
apetushkov@9858 155 // associated with writing negative values.
apetushkov@9858 156 // For example, writing a negative s1 (-1)
apetushkov@9858 157 // will encode as 0xff 0x0f (2 bytes).
apetushkov@9858 158 // In this example, the sizeof(T) == 1 and length == 1,
apetushkov@9858 159 // but the implementation will need to dimension
apetushkov@9858 160 // 2 bytes for the encoding.
apetushkov@9858 161 // Hopefully, negative values should be relatively rare.
apetushkov@9858 162 static const size_t size_safety_cushion = 1;
apetushkov@9858 163
apetushkov@9858 164 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 165 inline u1* WriterHost<BE, IE, WriterPolicyImpl>::ensure_size(size_t requested) {
apetushkov@9858 166 if (!this->is_valid()) {
apetushkov@9858 167 // cancelled
apetushkov@9858 168 return NULL;
apetushkov@9858 169 }
apetushkov@9858 170 if (this->available_size() < requested + size_safety_cushion) {
apetushkov@9858 171 if (!this->accommodate(this->used_size(), requested + size_safety_cushion)) {
apetushkov@9858 172 this->cancel();
apetushkov@9858 173 return NULL;
apetushkov@9858 174 }
apetushkov@9858 175 }
apetushkov@9858 176 assert(requested + size_safety_cushion <= this->available_size(), "invariant");
apetushkov@9858 177 return this->current_pos();
apetushkov@9858 178 }
apetushkov@9858 179
apetushkov@9858 180 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 181 template <typename T>
apetushkov@9858 182 inline void WriterHost<BE, IE, WriterPolicyImpl>::write(T value) {
apetushkov@9858 183 write(&value, 1);
apetushkov@9858 184 }
apetushkov@9858 185
apetushkov@9858 186 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 187 inline void WriterHost<BE, IE, WriterPolicyImpl>::write(bool value) {
apetushkov@9858 188 be_write((u1)value);
apetushkov@9858 189 }
apetushkov@9858 190
apetushkov@9858 191 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 192 inline void WriterHost<BE, IE, WriterPolicyImpl>::write(float value) {
apetushkov@9858 193 be_write(*(u4*)&(value));
apetushkov@9858 194 }
apetushkov@9858 195
apetushkov@9858 196 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 197 inline void WriterHost<BE, IE, WriterPolicyImpl>::write(double value) {
apetushkov@9858 198 be_write(*(u8*)&(value));
apetushkov@9858 199 }
apetushkov@9858 200
apetushkov@9858 201 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 202 inline void WriterHost<BE, IE, WriterPolicyImpl>::write(const char* value) {
apetushkov@9858 203 // UTF-8, max_jint len
apetushkov@9858 204 write_utf8(value);
apetushkov@9858 205 }
apetushkov@9858 206
apetushkov@9858 207 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 208 inline void WriterHost<BE, IE, WriterPolicyImpl>::write(char* value) {
apetushkov@9858 209 write(const_cast<const char*>(value));
apetushkov@9858 210 }
apetushkov@9858 211
apetushkov@9858 212 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 213 inline void WriterHost<BE, IE, WriterPolicyImpl>::write(jstring string) {
apetushkov@9858 214 if (string == NULL) {
apetushkov@9858 215 write<u1>(NULL_STRING);
apetushkov@9858 216 return;
apetushkov@9858 217 }
apetushkov@9858 218 const oop string_oop = JNIHandles::resolve_external_guard(string);
apetushkov@9858 219 assert(string_oop != NULL, "invariant");
apetushkov@9858 220 const size_t length = (size_t)java_lang_String::length(string_oop);
apetushkov@9858 221 if (0 == length) {
apetushkov@9858 222 write<u1>(EMPTY_STRING);
apetushkov@9858 223 return;
apetushkov@9858 224 }
apetushkov@9858 225 const bool is_latin1_encoded = false;
apetushkov@9858 226 const typeArrayOop value = java_lang_String::value(string_oop);
apetushkov@9858 227 assert(value != NULL, "invariant");
apetushkov@9858 228 if (is_latin1_encoded) {
apetushkov@9858 229 write<u1>(LATIN1);
apetushkov@9858 230 write<u4>((u4)length);
apetushkov@9858 231 be_write(value->byte_at_addr(0), length);
apetushkov@9858 232 } else {
apetushkov@9858 233 write<u1>(UTF16);
apetushkov@9858 234 write<u4>((u4)length);
apetushkov@9858 235 write(value->char_at_addr(0), length);
apetushkov@9858 236 }
apetushkov@9858 237 }
apetushkov@9858 238
apetushkov@9858 239 template <typename Writer, typename T>
apetushkov@9858 240 inline void tag_write(Writer* w, const T* t) {
apetushkov@9858 241 assert(w != NULL, "invariant");
apetushkov@9858 242 const traceid id = t == NULL ? 0 : JfrTraceId::use(t);
apetushkov@9858 243 w->write(id);
apetushkov@9858 244 }
apetushkov@9858 245
apetushkov@9858 246 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 247 void WriterHost<BE, IE, WriterPolicyImpl>::write(const ClassLoaderData* cld) {
apetushkov@9858 248 tag_write(this, cld);
apetushkov@9858 249 }
apetushkov@9858 250
apetushkov@9858 251 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 252 void WriterHost<BE, IE, WriterPolicyImpl>::write(const Klass* klass) {
apetushkov@9858 253 tag_write(this, klass);
apetushkov@9858 254 }
apetushkov@9858 255
apetushkov@9858 256 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 257 void WriterHost<BE, IE, WriterPolicyImpl>::write(const Method* method) {
apetushkov@9858 258 tag_write(this, method);
apetushkov@9858 259 }
apetushkov@9858 260
apetushkov@9858 261 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 262 void WriterHost<BE, IE, WriterPolicyImpl>::write(const Symbol* symbol) {
apetushkov@9858 263 ResourceMark rm;
apetushkov@9858 264 write_utf8(symbol != NULL ? symbol->as_C_string() : NULL);
apetushkov@9858 265 }
apetushkov@9858 266
apetushkov@9858 267 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 268 void WriterHost<BE, IE, WriterPolicyImpl>::write(const Ticks& time) {
apetushkov@9858 269 write((u8)JfrTime::is_ft_enabled() ? time.ft_value() : time.value());
apetushkov@9858 270 }
apetushkov@9858 271
apetushkov@9858 272 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 273 void WriterHost<BE, IE, WriterPolicyImpl>::write(const Tickspan& time) {
apetushkov@9858 274 write((u8)JfrTime::is_ft_enabled() ? time.ft_value() : time.value());
apetushkov@9858 275 }
apetushkov@9858 276
apetushkov@9858 277 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 278 void WriterHost<BE, IE, WriterPolicyImpl>::write(const JfrTicks& time) {
apetushkov@9858 279 write((u8)time.value());
apetushkov@9858 280 }
apetushkov@9858 281
apetushkov@9858 282 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 283 void WriterHost<BE, IE, WriterPolicyImpl>::write(const JfrTickspan& time) {
apetushkov@9858 284 write((u8)time.value());
apetushkov@9858 285 }
apetushkov@9858 286
apetushkov@9858 287 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 288 void WriterHost<BE, IE, WriterPolicyImpl>::bytes(const void* buf, size_t len) {
apetushkov@9858 289 u1* const pos = this->ensure_size(len);
apetushkov@9858 290 if (pos != NULL) {
apetushkov@9858 291 WriterPolicyImpl::bytes(pos, buf, len); // WriterPolicyImpl responsible for position update
apetushkov@9858 292 }
apetushkov@9858 293 }
apetushkov@9858 294
apetushkov@9858 295 // UTF-8 for use with classfile/bytecodes
apetushkov@9858 296 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 297 inline void WriterHost<BE, IE, WriterPolicyImpl>::write_utf8_u2_len(const char* value) {
apetushkov@9858 298 u2 len = 0;
apetushkov@9858 299 if (value != NULL) {
apetushkov@9858 300 len = MIN2<u2>(max_jushort, (u2)strlen(value));
apetushkov@9858 301 }
apetushkov@9858 302 write(len);
apetushkov@9858 303 if (len > 0) {
apetushkov@9858 304 be_write(value, len);
apetushkov@9858 305 }
apetushkov@9858 306 }
apetushkov@9858 307
apetushkov@9858 308 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 309 inline intptr_t WriterHost<BE, IE, WriterPolicyImpl>::reserve(size_t size) {
apetushkov@9858 310 if (ensure_size(size) != NULL) {
apetushkov@9858 311 intptr_t reserved_offset = this->current_offset();
apetushkov@9858 312 this->set_current_pos(size);
apetushkov@9858 313 return reserved_offset;
apetushkov@9858 314 }
apetushkov@9858 315 this->cancel();
apetushkov@9858 316 return 0;
apetushkov@9858 317 }
apetushkov@9858 318
apetushkov@9858 319 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 320 template <typename T>
apetushkov@9858 321 inline void WriterHost<BE, IE, WriterPolicyImpl>::write_padded_at_offset(T value, intptr_t offset) {
apetushkov@9858 322 if (this->is_valid()) {
apetushkov@9858 323 const intptr_t current = this->current_offset();
apetushkov@9858 324 this->seek(offset);
apetushkov@9858 325 write_padded(value);
apetushkov@9858 326 this->seek(current); // restore
apetushkov@9858 327 }
apetushkov@9858 328 }
apetushkov@9858 329
apetushkov@9858 330 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 331 template <typename T>
apetushkov@9858 332 inline void WriterHost<BE, IE, WriterPolicyImpl>::write_at_offset(T value, intptr_t offset) {
apetushkov@9858 333 if (this->is_valid()) {
apetushkov@9858 334 const intptr_t current = this->current_offset();
apetushkov@9858 335 this->seek(offset);
apetushkov@9858 336 write(value);
apetushkov@9858 337 this->seek(current); // restore
apetushkov@9858 338 }
apetushkov@9858 339 }
apetushkov@9858 340
apetushkov@9858 341 template <typename BE, typename IE, typename WriterPolicyImpl>
apetushkov@9858 342 template <typename T>
apetushkov@9858 343 inline void WriterHost<BE, IE, WriterPolicyImpl>::write_be_at_offset(T value, intptr_t offset) {
apetushkov@9858 344 if (this->is_valid()) {
apetushkov@9858 345 const intptr_t current = this->current_offset();
apetushkov@9858 346 this->seek(offset);
apetushkov@9858 347 be_write(value);
apetushkov@9858 348 this->seek(current); // restore
apetushkov@9858 349 }
apetushkov@9858 350 }
apetushkov@9858 351
apetushkov@9858 352 #endif // SHARE_VM_JFR_WRITERS_JFRWRITERHOST_INLINE_HPP
apetushkov@9858 353

mercurial