50113
|
1 |
/*
|
58863
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
50113
|
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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.jfr.internal;
|
|
27 |
|
|
28 |
import jdk.internal.misc.Unsafe;
|
58863
|
29 |
import jdk.jfr.internal.consumer.StringParser;
|
50113
|
30 |
|
|
31 |
/**
|
|
32 |
* Class must reside in a package with package restriction.
|
|
33 |
*
|
|
34 |
* Users should not have direct access to underlying memory.
|
|
35 |
*
|
|
36 |
*/
|
|
37 |
public final class EventWriter {
|
|
38 |
private static final Unsafe unsafe = Unsafe.getUnsafe();
|
|
39 |
private final static JVM jvm = JVM.getJVM();
|
|
40 |
|
|
41 |
private long startPosition;
|
|
42 |
private long startPositionAddress;
|
|
43 |
private long currentPosition;
|
|
44 |
private long maxPosition;
|
|
45 |
private final long threadID;
|
|
46 |
private PlatformEventType eventType;
|
|
47 |
private int maxEventSize;
|
|
48 |
private boolean started;
|
|
49 |
private boolean valid;
|
|
50 |
private boolean flushOnEnd;
|
|
51 |
// set by the JVM, not private to avoid being optimized out
|
|
52 |
boolean notified;
|
|
53 |
|
|
54 |
public static EventWriter getEventWriter() {
|
|
55 |
EventWriter ew = (EventWriter)JVM.getEventWriter();
|
|
56 |
return ew != null ? ew : JVM.newEventWriter();
|
|
57 |
}
|
|
58 |
|
|
59 |
public void putBoolean(boolean i) {
|
|
60 |
if (isValidForSize(Byte.BYTES)) {
|
|
61 |
currentPosition += Bits.putBoolean(currentPosition, i);
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
public void putByte(byte i) {
|
|
66 |
if (isValidForSize(Byte.BYTES)) {
|
|
67 |
unsafe.putByte(currentPosition, i);
|
|
68 |
++currentPosition;
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
public void putChar(char v) {
|
|
73 |
if (isValidForSize(Character.BYTES + 1)) {
|
|
74 |
putUncheckedLong(v);
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
private void putUncheckedChar(char v) {
|
|
79 |
putUncheckedLong(v);
|
|
80 |
}
|
|
81 |
|
|
82 |
public void putShort(short v) {
|
|
83 |
if (isValidForSize(Short.BYTES + 1)) {
|
|
84 |
putUncheckedLong(v & 0xFFFF);
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
public void putInt(int v) {
|
|
89 |
if (isValidForSize(Integer.BYTES + 1)) {
|
|
90 |
putUncheckedLong(v & 0x00000000ffffffffL);
|
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
private void putUncheckedInt(int v) {
|
|
95 |
putUncheckedLong(v & 0x00000000ffffffffL);
|
|
96 |
}
|
|
97 |
|
|
98 |
public void putFloat(float i) {
|
|
99 |
if (isValidForSize(Float.BYTES)) {
|
|
100 |
currentPosition += Bits.putFloat(currentPosition, i);
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
public void putLong(long v) {
|
|
105 |
if (isValidForSize(Long.BYTES + 1)) {
|
|
106 |
putUncheckedLong(v);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
|
110 |
public void putDouble(double i) {
|
|
111 |
if (isValidForSize(Double.BYTES)) {
|
|
112 |
currentPosition += Bits.putDouble(currentPosition, i);
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
public void putString(String s, StringPool pool) {
|
|
117 |
if (s == null) {
|
58863
|
118 |
putByte(StringParser.Encoding.NULL.byteValue());
|
50113
|
119 |
return;
|
|
120 |
}
|
|
121 |
int length = s.length();
|
|
122 |
if (length == 0) {
|
58863
|
123 |
putByte(StringParser.Encoding.EMPTY_STRING.byteValue());
|
50113
|
124 |
return;
|
|
125 |
}
|
|
126 |
if (length > StringPool.MIN_LIMIT && length < StringPool.MAX_LIMIT) {
|
|
127 |
long l = StringPool.addString(s);
|
|
128 |
if (l > 0) {
|
58863
|
129 |
putByte(StringParser.Encoding.CONSTANT_POOL.byteValue());
|
50113
|
130 |
putLong(l);
|
|
131 |
return;
|
|
132 |
}
|
|
133 |
}
|
|
134 |
putStringValue(s);
|
|
135 |
return;
|
|
136 |
}
|
|
137 |
|
|
138 |
private void putStringValue(String s) {
|
|
139 |
int length = s.length();
|
|
140 |
if (isValidForSize(1 + 5 + 3 * length)) {
|
58863
|
141 |
putUncheckedByte(StringParser.Encoding.CHAR_ARRAY.byteValue()); // 1 byte
|
50113
|
142 |
putUncheckedInt(length); // max 5 bytes
|
|
143 |
for (int i = 0; i < length; i++) {
|
|
144 |
putUncheckedChar(s.charAt(i)); // max 3 bytes
|
|
145 |
}
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
public void putEventThread() {
|
|
150 |
putLong(threadID);
|
|
151 |
}
|
|
152 |
|
|
153 |
public void putThread(Thread athread) {
|
|
154 |
if (athread == null) {
|
|
155 |
putLong(0L);
|
|
156 |
} else {
|
|
157 |
putLong(jvm.getThreadId(athread));
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
public void putClass(Class<?> aClass) {
|
|
162 |
if (aClass == null) {
|
|
163 |
putLong(0L);
|
|
164 |
} else {
|
|
165 |
putLong(JVM.getClassIdNonIntrinsic(aClass));
|
|
166 |
}
|
|
167 |
}
|
|
168 |
|
|
169 |
public void putStackTrace() {
|
|
170 |
if (eventType.getStackTraceEnabled()) {
|
|
171 |
putLong(jvm.getStackTraceId(eventType.getStackTraceOffset()));
|
|
172 |
} else {
|
|
173 |
putLong(0L);
|
|
174 |
}
|
|
175 |
}
|
|
176 |
|
|
177 |
private void reserveEventSizeField() {
|
|
178 |
// move currentPosition Integer.Bytes offset from start position
|
|
179 |
if (isValidForSize(Integer.BYTES)) {
|
|
180 |
currentPosition += Integer.BYTES;
|
|
181 |
}
|
|
182 |
}
|
|
183 |
|
|
184 |
private void reset() {
|
|
185 |
currentPosition = startPosition;
|
|
186 |
if (flushOnEnd) {
|
|
187 |
flushOnEnd = flush();
|
|
188 |
}
|
|
189 |
valid = true;
|
|
190 |
started = false;
|
|
191 |
}
|
|
192 |
|
|
193 |
private boolean isValidForSize(int requestedSize) {
|
|
194 |
if (!valid) {
|
|
195 |
return false;
|
|
196 |
}
|
|
197 |
if (currentPosition + requestedSize > maxPosition) {
|
|
198 |
flushOnEnd = flush(usedSize(), requestedSize);
|
|
199 |
// retry
|
58863
|
200 |
if (!valid) {
|
50113
|
201 |
return false;
|
|
202 |
}
|
|
203 |
}
|
|
204 |
return true;
|
|
205 |
}
|
|
206 |
|
|
207 |
private boolean isNotified() {
|
|
208 |
return notified;
|
|
209 |
}
|
|
210 |
|
|
211 |
private void resetNotified() {
|
|
212 |
notified = false;
|
|
213 |
}
|
|
214 |
|
|
215 |
private int usedSize() {
|
|
216 |
return (int) (currentPosition - startPosition);
|
|
217 |
}
|
|
218 |
|
|
219 |
private boolean flush() {
|
|
220 |
return flush(usedSize(), 0);
|
|
221 |
}
|
|
222 |
|
|
223 |
private boolean flush(int usedSize, int requestedSize) {
|
|
224 |
return JVM.flush(this, usedSize, requestedSize);
|
|
225 |
}
|
|
226 |
|
|
227 |
public boolean beginEvent(PlatformEventType eventType) {
|
|
228 |
if (started) {
|
|
229 |
// recursive write attempt
|
|
230 |
return false;
|
|
231 |
}
|
|
232 |
started = true;
|
|
233 |
this.eventType = eventType;
|
|
234 |
reserveEventSizeField();
|
|
235 |
putLong(eventType.getId());
|
|
236 |
return true;
|
|
237 |
}
|
|
238 |
|
|
239 |
public boolean endEvent() {
|
|
240 |
if (!valid) {
|
|
241 |
reset();
|
|
242 |
return true;
|
|
243 |
}
|
|
244 |
final int eventSize = usedSize();
|
|
245 |
if (eventSize > maxEventSize) {
|
|
246 |
reset();
|
|
247 |
return true;
|
|
248 |
}
|
|
249 |
Bits.putInt(startPosition, makePaddedInt(eventSize));
|
|
250 |
if (isNotified()) {
|
|
251 |
resetNotified();
|
|
252 |
reset();
|
|
253 |
// returning false will trigger restart of the event write attempt
|
|
254 |
return false;
|
|
255 |
}
|
|
256 |
startPosition = currentPosition;
|
|
257 |
unsafe.putAddress(startPositionAddress, startPosition);
|
|
258 |
// the event is now committed
|
|
259 |
if (flushOnEnd) {
|
|
260 |
flushOnEnd = flush();
|
|
261 |
}
|
|
262 |
started = false;
|
|
263 |
return true;
|
|
264 |
}
|
|
265 |
|
|
266 |
private EventWriter(long startPos, long maxPos, long startPosAddress, long threadID, boolean valid) {
|
|
267 |
startPosition = currentPosition = startPos;
|
|
268 |
maxPosition = maxPos;
|
|
269 |
startPositionAddress = startPosAddress;
|
|
270 |
this.threadID = threadID;
|
|
271 |
started = false;
|
|
272 |
flushOnEnd = false;
|
|
273 |
this.valid = valid;
|
|
274 |
notified = false;
|
|
275 |
// event may not exceed size for a padded integer
|
|
276 |
maxEventSize = (1 << 28) -1;
|
|
277 |
}
|
|
278 |
|
|
279 |
private static int makePaddedInt(int v) {
|
|
280 |
// bit 0-6 + pad => bit 24 - 31
|
|
281 |
long b1 = (((v >>> 0) & 0x7F) | 0x80) << 24;
|
|
282 |
|
|
283 |
// bit 7-13 + pad => bit 16 - 23
|
|
284 |
long b2 = (((v >>> 7) & 0x7F) | 0x80) << 16;
|
|
285 |
|
|
286 |
// bit 14-20 + pad => bit 8 - 15
|
|
287 |
long b3 = (((v >>> 14) & 0x7F) | 0x80) << 8;
|
|
288 |
|
|
289 |
// bit 21-28 => bit 0 - 7
|
|
290 |
long b4 = (((v >>> 21) & 0x7F)) << 0;
|
|
291 |
|
|
292 |
return (int) (b1 + b2 + b3 + b4);
|
|
293 |
}
|
|
294 |
|
|
295 |
private void putUncheckedLong(long v) {
|
|
296 |
if ((v & ~0x7FL) == 0L) {
|
|
297 |
putUncheckedByte((byte) v); // 0-6
|
|
298 |
return;
|
|
299 |
}
|
|
300 |
putUncheckedByte((byte) (v | 0x80L)); // 0-6
|
|
301 |
v >>>= 7;
|
|
302 |
if ((v & ~0x7FL) == 0L) {
|
|
303 |
putUncheckedByte((byte) v); // 7-13
|
|
304 |
return;
|
|
305 |
}
|
|
306 |
putUncheckedByte((byte) (v | 0x80L)); // 7-13
|
|
307 |
v >>>= 7;
|
|
308 |
if ((v & ~0x7FL) == 0L) {
|
|
309 |
putUncheckedByte((byte) v); // 14-20
|
|
310 |
return;
|
|
311 |
}
|
|
312 |
putUncheckedByte((byte) (v | 0x80L)); // 14-20
|
|
313 |
v >>>= 7;
|
|
314 |
if ((v & ~0x7FL) == 0L) {
|
|
315 |
putUncheckedByte((byte) v); // 21-27
|
|
316 |
return;
|
|
317 |
}
|
|
318 |
putUncheckedByte((byte) (v | 0x80L)); // 21-27
|
|
319 |
v >>>= 7;
|
|
320 |
if ((v & ~0x7FL) == 0L) {
|
|
321 |
putUncheckedByte((byte) v); // 28-34
|
|
322 |
return;
|
|
323 |
}
|
|
324 |
putUncheckedByte((byte) (v | 0x80L)); // 28-34
|
|
325 |
v >>>= 7;
|
|
326 |
if ((v & ~0x7FL) == 0L) {
|
|
327 |
putUncheckedByte((byte) v); // 35-41
|
|
328 |
return;
|
|
329 |
}
|
|
330 |
putUncheckedByte((byte) (v | 0x80L)); // 35-41
|
|
331 |
v >>>= 7;
|
|
332 |
if ((v & ~0x7FL) == 0L) {
|
|
333 |
putUncheckedByte((byte) v); // 42-48
|
|
334 |
return;
|
|
335 |
}
|
|
336 |
putUncheckedByte((byte) (v | 0x80L)); // 42-48
|
|
337 |
v >>>= 7;
|
|
338 |
|
|
339 |
if ((v & ~0x7FL) == 0L) {
|
|
340 |
putUncheckedByte((byte) v); // 49-55
|
|
341 |
return;
|
|
342 |
}
|
|
343 |
putUncheckedByte((byte) (v | 0x80L)); // 49-55
|
|
344 |
putUncheckedByte((byte) (v >>> 7)); // 56-63, last byte as is.
|
|
345 |
}
|
|
346 |
|
|
347 |
private void putUncheckedByte(byte i) {
|
|
348 |
unsafe.putByte(currentPosition, i);
|
|
349 |
++currentPosition;
|
|
350 |
}
|
|
351 |
}
|