author | egahlin |
Thu, 05 Sep 2019 16:46:50 +0200 | |
branch | JEP-349-branch |
changeset 58020 | f082177c5023 |
parent 57985 | be121cbf3284 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
57360 | 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.consumer; |
|
27 |
||
28 |
import static jdk.jfr.internal.EventInstrumentation.FIELD_DURATION; |
|
29 |
||
30 |
import java.io.IOException; |
|
31 |
import java.util.List; |
|
32 |
||
33 |
import jdk.jfr.EventType; |
|
34 |
import jdk.jfr.ValueDescriptor; |
|
57360 | 35 |
import jdk.jfr.internal.consumer.Parser; |
50113 | 36 |
import jdk.jfr.internal.consumer.RecordingInput; |
37 |
||
38 |
/** |
|
39 |
* Parses an event and returns a {@link RecordedEvent}. |
|
40 |
* |
|
41 |
*/ |
|
42 |
final class EventParser extends Parser { |
|
43 |
private final Parser[] parsers; |
|
44 |
private final EventType eventType; |
|
45 |
private final TimeConverter timeConverter; |
|
46 |
private final boolean hasDuration; |
|
47 |
private final List<ValueDescriptor> valueDescriptors; |
|
57360 | 48 |
private final int startIndex; |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
49 |
private final int length; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
50 |
private final RecordedEvent unorderedEvent; |
57463 | 51 |
private final ObjectContext objectContext; |
52 |
||
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
53 |
private RecordedEvent[] cached; |
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
54 |
private int cacheIndex; |
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
55 |
|
57360 | 56 |
private boolean enabled = true; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
57 |
private boolean ordered; |
57922
6598f2825b1f
Rename firstNanos and lastNanos to filterStart and filterEnd
egahlin
parents:
57690
diff
changeset
|
58 |
private long filterStart; |
6598f2825b1f
Rename firstNanos and lastNanos to filterStart and filterEnd
egahlin
parents:
57690
diff
changeset
|
59 |
private long filterEnd = Long.MAX_VALUE; |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
60 |
private long thresholdNanos = -1; |
50113 | 61 |
|
62 |
EventParser(TimeConverter timeConverter, EventType type, Parser[] parsers) { |
|
63 |
this.timeConverter = timeConverter; |
|
64 |
this.parsers = parsers; |
|
65 |
this.eventType = type; |
|
66 |
this.hasDuration = type.getField(FIELD_DURATION) != null; |
|
57360 | 67 |
this.startIndex = hasDuration ? 2 : 1; |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
68 |
this.length = parsers.length - startIndex; |
50113 | 69 |
this.valueDescriptors = type.getFields(); |
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
70 |
this.objectContext = new ObjectContext(type, valueDescriptors, timeConverter); |
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
71 |
this.unorderedEvent = new RecordedEvent(objectContext, new Object[length], 0L, 0L); |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
72 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
73 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
74 |
private RecordedEvent cachedEvent() { |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
75 |
if (ordered) { |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
76 |
if (cacheIndex == cached.length) { |
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
77 |
RecordedEvent[] old = cached; |
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
78 |
cached = new RecordedEvent[cached.length * 2]; |
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
79 |
System.arraycopy(old, 0, cached, 0, old.length); |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
80 |
} |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
81 |
RecordedEvent event = cached[cacheIndex]; |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
82 |
if (event == null) { |
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
83 |
event = new RecordedEvent(objectContext, new Object[length], 0L, 0L); |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
84 |
cached[cacheIndex] = event; |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
85 |
} |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
86 |
cacheIndex++; |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
87 |
return event; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
88 |
} else { |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
89 |
return unorderedEvent; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
90 |
} |
50113 | 91 |
} |
92 |
||
57360 | 93 |
public EventType getEventType() { |
94 |
return eventType; |
|
95 |
} |
|
96 |
||
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
97 |
public void setThresholdNanos(long thresholdNanos) { |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
98 |
this.thresholdNanos = thresholdNanos; |
57360 | 99 |
} |
100 |
||
101 |
public void setEnabled(boolean enabled) { |
|
102 |
this.enabled = enabled; |
|
103 |
} |
|
104 |
||
105 |
public boolean isEnabled() { |
|
57470 | 106 |
return enabled; |
57360 | 107 |
} |
108 |
||
109 |
public RecordedEvent parse(RecordingInput input) throws IOException { |
|
57463 | 110 |
if (!enabled) { |
111 |
return null; |
|
112 |
} |
|
113 |
||
114 |
long startTicks = input.readLong(); |
|
57470 | 115 |
long endTicks = startTicks; |
57463 | 116 |
if (hasDuration) { |
57470 | 117 |
long durationTicks = input.readLong(); |
57463 | 118 |
if (thresholdNanos > 0L) { |
119 |
if (timeConverter.convertTimespan(durationTicks) < thresholdNanos) { |
|
57360 | 120 |
return null; |
121 |
} |
|
122 |
} |
|
57470 | 123 |
endTicks += durationTicks; |
57463 | 124 |
} |
57922
6598f2825b1f
Rename firstNanos and lastNanos to filterStart and filterEnd
egahlin
parents:
57690
diff
changeset
|
125 |
if (filterStart != 0L || filterEnd != Long.MAX_VALUE) { |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
126 |
long eventEnd = timeConverter.convertTimestamp(endTicks); |
57922
6598f2825b1f
Rename firstNanos and lastNanos to filterStart and filterEnd
egahlin
parents:
57690
diff
changeset
|
127 |
if (eventEnd < filterStart) { |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
128 |
return null; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
129 |
} |
57922
6598f2825b1f
Rename firstNanos and lastNanos to filterStart and filterEnd
egahlin
parents:
57690
diff
changeset
|
130 |
if (eventEnd > filterEnd) { |
57463 | 131 |
return null; |
57360 | 132 |
} |
133 |
} |
|
57463 | 134 |
|
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
135 |
if (cached != null) { |
57463 | 136 |
RecordedEvent event = cachedEvent(); |
137 |
event.startTimeTicks = startTicks; |
|
138 |
event.endTimeTicks = endTicks; |
|
139 |
Object[] values = event.objects; |
|
140 |
for (int i = 0; i < values.length; i++) { |
|
141 |
values[i] = parsers[startIndex + i].parse(input); |
|
142 |
} |
|
143 |
return event; |
|
144 |
} |
|
145 |
||
146 |
Object[] values = new Object[length]; |
|
147 |
for (int i = 0; i < values.length; i++) { |
|
148 |
values[i] = parsers[startIndex + i].parse(input); |
|
149 |
} |
|
150 |
return new RecordedEvent(objectContext, values, startTicks, endTicks); |
|
57360 | 151 |
} |
152 |
||
50113 | 153 |
@Override |
57360 | 154 |
public void skip(RecordingInput input) throws IOException { |
155 |
throw new InternalError("Should not call this method. More efficent to read event size and skip ahead"); |
|
50113 | 156 |
} |
57360 | 157 |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
158 |
public void resetCache() { |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
159 |
cacheIndex = 0; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
160 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
161 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
162 |
public boolean hasReuse() { |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
163 |
return cached != null; |
57376 | 164 |
} |
165 |
||
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
166 |
public void setReuse(boolean reuse) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
167 |
if (reuse == hasReuse()) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
168 |
return; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
169 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
170 |
if (reuse) { |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
171 |
cached = new RecordedEvent[2]; |
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
172 |
cacheIndex = 0; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
173 |
} else { |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
174 |
cached = null; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
175 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
176 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
177 |
|
57985 | 178 |
public void setFilterStart(long filterStart) { |
179 |
this.filterStart = filterStart; |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
180 |
} |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
181 |
|
57985 | 182 |
public void setFilterEnd(long filterEnd) { |
183 |
this.filterEnd = filterEnd; |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
184 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
185 |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
186 |
public void setOrdered(boolean ordered) { |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
187 |
if (this.ordered == ordered) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
188 |
return; |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
189 |
} |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
190 |
this.ordered = ordered; |
58020
f082177c5023
Improved handling of Thread.interrupt() + cleanup
egahlin
parents:
57985
diff
changeset
|
191 |
this.cacheIndex = 0; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
192 |
} |
50113 | 193 |
} |