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