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