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