author | egahlin |
Fri, 09 Aug 2019 01:18:18 +0200 | |
branch | JEP-349-branch |
changeset 57690 | 9316d02dd4a5 |
parent 57470 | 025c9b8eaefd |
child 57922 | 6598f2825b1f |
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 |
||
57360 | 53 |
private boolean enabled = true; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
54 |
private RecordedEvent[] eventCache; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
55 |
private int index; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
56 |
private boolean ordered; |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
57 |
private long firstNanos; |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
58 |
private long lastNanos = Long.MAX_VALUE; |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
59 |
private long thresholdNanos = -1; |
50113 | 60 |
|
61 |
EventParser(TimeConverter timeConverter, EventType type, Parser[] parsers) { |
|
62 |
this.timeConverter = timeConverter; |
|
63 |
this.parsers = parsers; |
|
64 |
this.eventType = type; |
|
65 |
this.hasDuration = type.getField(FIELD_DURATION) != null; |
|
57360 | 66 |
this.startIndex = hasDuration ? 2 : 1; |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
67 |
this.length = parsers.length - startIndex; |
50113 | 68 |
this.valueDescriptors = type.getFields(); |
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
69 |
this.objectContext = new ObjectContext(type, valueDescriptors, timeConverter); |
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
70 |
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
|
71 |
} |
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 |
private RecordedEvent cachedEvent() { |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
74 |
if (ordered) { |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
75 |
if (index == eventCache.length) { |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
76 |
RecordedEvent[] cache = eventCache; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
77 |
eventCache = new RecordedEvent[eventCache.length * 2]; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
78 |
System.arraycopy(cache, 0, eventCache, 0, cache.length); |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
79 |
} |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
80 |
RecordedEvent event = eventCache[index]; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
81 |
if (event == null) { |
57460
bcbc53560c77
Reduce allocation rate by minimizing number of fields in RecordedEvent
egahlin
parents:
57452
diff
changeset
|
82 |
event = new RecordedEvent(objectContext, new Object[length], 0L, 0L); |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
83 |
eventCache[index] = event; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
84 |
} |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
85 |
index++; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
86 |
return event; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
87 |
} else { |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
88 |
return unorderedEvent; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
89 |
} |
50113 | 90 |
} |
91 |
||
57360 | 92 |
public EventType getEventType() { |
93 |
return eventType; |
|
94 |
} |
|
95 |
||
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
96 |
public void setThresholdNanos(long thresholdNanos) { |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
97 |
this.thresholdNanos = thresholdNanos; |
57360 | 98 |
} |
99 |
||
100 |
public void setEnabled(boolean enabled) { |
|
101 |
this.enabled = enabled; |
|
102 |
} |
|
103 |
||
104 |
public boolean isEnabled() { |
|
57470 | 105 |
return enabled; |
57360 | 106 |
} |
107 |
||
108 |
public RecordedEvent parse(RecordingInput input) throws IOException { |
|
57463 | 109 |
if (!enabled) { |
110 |
return null; |
|
111 |
} |
|
112 |
||
113 |
long startTicks = input.readLong(); |
|
57470 | 114 |
long endTicks = startTicks; |
57463 | 115 |
if (hasDuration) { |
57470 | 116 |
long durationTicks = input.readLong(); |
57463 | 117 |
if (thresholdNanos > 0L) { |
118 |
if (timeConverter.convertTimespan(durationTicks) < thresholdNanos) { |
|
57360 | 119 |
return null; |
120 |
} |
|
121 |
} |
|
57470 | 122 |
endTicks += durationTicks; |
57463 | 123 |
} |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
124 |
if (firstNanos != 0L || lastNanos != Long.MAX_VALUE) { |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
125 |
long eventEnd = timeConverter.convertTimestamp(endTicks); |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
126 |
if (eventEnd < firstNanos) { |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
127 |
return null; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
128 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
129 |
if (eventEnd > lastNanos) { |
57463 | 130 |
return null; |
57360 | 131 |
} |
132 |
} |
|
57463 | 133 |
|
134 |
if (eventCache != null) { |
|
135 |
RecordedEvent event = cachedEvent(); |
|
136 |
event.startTimeTicks = startTicks; |
|
137 |
event.endTimeTicks = endTicks; |
|
138 |
Object[] values = event.objects; |
|
139 |
for (int i = 0; i < values.length; i++) { |
|
140 |
values[i] = parsers[startIndex + i].parse(input); |
|
141 |
} |
|
142 |
return event; |
|
143 |
} |
|
144 |
||
145 |
Object[] values = new Object[length]; |
|
146 |
for (int i = 0; i < values.length; i++) { |
|
147 |
values[i] = parsers[startIndex + i].parse(input); |
|
148 |
} |
|
149 |
return new RecordedEvent(objectContext, values, startTicks, endTicks); |
|
57360 | 150 |
} |
151 |
||
50113 | 152 |
@Override |
57360 | 153 |
public void skip(RecordingInput input) throws IOException { |
154 |
throw new InternalError("Should not call this method. More efficent to read event size and skip ahead"); |
|
50113 | 155 |
} |
57360 | 156 |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
157 |
public void resetCache() { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
158 |
index = 0; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
159 |
} |
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 |
public boolean hasReuse() { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
162 |
return eventCache != null; |
57376 | 163 |
} |
164 |
||
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
165 |
public void setReuse(boolean reuse) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
166 |
if (reuse == hasReuse()) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
167 |
return; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
168 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
169 |
if (reuse) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
170 |
eventCache = new RecordedEvent[2]; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
171 |
index = 0; |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
172 |
} else { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
173 |
eventCache = null; |
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 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
176 |
|
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
177 |
public void setFirstNanos(long firstNanos) { |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
178 |
this.firstNanos = firstNanos; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
179 |
} |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
180 |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
181 |
public void setLastNanos(long lastNanos) { |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
182 |
this.lastNanos = lastNanos; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
183 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
184 |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
185 |
public void setOrdered(boolean ordered) { |
57385
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
186 |
if (this.ordered == ordered) { |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
187 |
return; |
7d9d4f629f6e
Make setReuse and setOrdered work across chunk boundaries. Improved unit tests
egahlin
parents:
57380
diff
changeset
|
188 |
} |
57452
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
189 |
this.ordered = ordered; |
6fabe73e5d9a
Reduced allocation pressure. Fix getValue for startTime and duration
egahlin
parents:
57385
diff
changeset
|
190 |
this.index = 0; |
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
191 |
} |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
57470
diff
changeset
|
192 |
|
50113 | 193 |
} |