author | egahlin |
Thu, 30 May 2019 23:04:50 +0200 | |
branch | JEP-349-branch |
changeset 57380 | 6a7e7743b82f |
parent 57376 | 8e8a06a3059c |
child 57385 | 7d9d4f629f6e |
permissions | -rw-r--r-- |
57361 | 1 |
/* |
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
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 java.io.IOException; |
|
29 |
import java.security.AccessControlContext; |
|
30 |
import java.time.Duration; |
|
31 |
import java.time.Instant; |
|
32 |
import java.util.Objects; |
|
33 |
import java.util.function.Consumer; |
|
34 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
35 |
import jdk.jfr.internal.consumer.EventConsumer; |
57361 | 36 |
|
37 |
final class EventDirectoryStream implements EventStream { |
|
38 |
||
57373 | 39 |
private static class EventSetConsumer extends EventConsumer { |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
40 |
private EventSetLocation location; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
41 |
private EventSet eventSet; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
42 |
private int eventSetIndex; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
43 |
private int eventArrayIndex; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
44 |
private RecordedEvent[] currentEventArray = new RecordedEvent[0]; |
57361 | 45 |
|
57373 | 46 |
public EventSetConsumer(AccessControlContext acc) throws IOException { |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
47 |
super(acc); |
57361 | 48 |
} |
49 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
50 |
public void process() throws Exception, IOException { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
51 |
this.location = EventSetLocation.current(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
52 |
this.eventSet = location.acquire(startNanos, null); // use timestamp |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
53 |
// from |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
54 |
if (eventSet == null) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
55 |
return; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
56 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
57 |
while (!isClosed()) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
58 |
processSegment(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
59 |
runFlushActions(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
60 |
do { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
61 |
if (isClosed()) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
62 |
return; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
63 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
64 |
currentEventArray = eventSet.readEvents(eventSetIndex); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
65 |
if (currentEventArray == EventSet.END_OF_SET) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
66 |
eventSet = eventSet.next(eventFilter); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
67 |
if (eventSet == null || isClosed()) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
68 |
return; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
69 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
70 |
eventSetIndex = 0; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
71 |
continue; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
72 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
73 |
if (currentEventArray == null) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
74 |
return; // no more events |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
75 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
76 |
eventSetIndex++; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
77 |
} while (currentEventArray.length == 0); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
78 |
eventArrayIndex = 0; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
79 |
} |
57361 | 80 |
} |
81 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
82 |
private void processSegment() { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
83 |
while (eventArrayIndex < currentEventArray.length) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
84 |
RecordedEvent e = currentEventArray[eventArrayIndex++]; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
85 |
if (e == null) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
86 |
return; |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
87 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
88 |
dispatch(e); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
89 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
90 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
91 |
|
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
92 |
public void close() { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
93 |
setClosed(true); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
94 |
// TODO: Data races here, must fix |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
95 |
synchronized (this) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
96 |
if (eventSet != null) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
97 |
eventSet.release(null); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
98 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
99 |
if (location != null) { |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
100 |
location.release(); |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
101 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
102 |
} |
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
103 |
runCloseActions(); |
57361 | 104 |
} |
57376 | 105 |
|
106 |
public void setReuse(boolean reuse) { |
|
107 |
// ignore hint |
|
108 |
} |
|
57361 | 109 |
} |
110 |
||
57373 | 111 |
private final EventSetConsumer eventConsumer; |
57361 | 112 |
|
113 |
public EventDirectoryStream(AccessControlContext acc) throws IOException { |
|
57373 | 114 |
eventConsumer = new EventSetConsumer(acc); |
57361 | 115 |
} |
116 |
||
117 |
public void close() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
118 |
eventConsumer.close(); |
57361 | 119 |
} |
120 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
121 |
public void onFlush(Runnable action) { |
57361 | 122 |
Objects.requireNonNull(action); |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
123 |
eventConsumer.onFlush(action); |
57361 | 124 |
} |
125 |
||
126 |
void start(long startNanos) { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
127 |
eventConsumer.start(startNanos); |
57361 | 128 |
} |
129 |
||
130 |
@Override |
|
131 |
public void start() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
132 |
start(Instant.now().toEpochMilli() * 1000 * 1000L); |
57361 | 133 |
} |
134 |
||
135 |
@Override |
|
136 |
public void startAsync() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
137 |
startAsync(Instant.now().toEpochMilli() * 1000 * 1000L); |
57361 | 138 |
} |
139 |
||
140 |
void startAsync(long startNanos) { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
141 |
eventConsumer.startAsync(startNanos); |
57361 | 142 |
} |
143 |
||
144 |
@Override |
|
145 |
public void onEvent(Consumer<RecordedEvent> action) { |
|
146 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
147 |
eventConsumer.onEvent(action); |
57361 | 148 |
} |
149 |
||
150 |
@Override |
|
151 |
public void onEvent(String eventName, Consumer<RecordedEvent> action) { |
|
152 |
Objects.requireNonNull(eventName); |
|
153 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
154 |
eventConsumer.onEvent(eventName, action); |
57361 | 155 |
} |
156 |
||
157 |
@Override |
|
158 |
public void onClose(Runnable action) { |
|
159 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
160 |
eventConsumer.addCloseAction(action); |
57361 | 161 |
} |
162 |
||
163 |
@Override |
|
164 |
public boolean remove(Object action) { |
|
165 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
166 |
return eventConsumer.remove(action); |
57361 | 167 |
} |
168 |
||
169 |
@Override |
|
170 |
public void awaitTermination(Duration timeout) { |
|
171 |
Objects.requireNonNull(timeout); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
172 |
eventConsumer.awaitTermination(timeout); |
57361 | 173 |
} |
174 |
||
175 |
@Override |
|
176 |
public void awaitTermination() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
177 |
eventConsumer.awaitTermination(Duration.ofMillis(0)); |
57361 | 178 |
} |
57376 | 179 |
|
180 |
@Override |
|
181 |
public void setReuse(boolean reuse) { |
|
182 |
eventConsumer.setReuse(reuse); |
|
183 |
} |
|
57380
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
184 |
|
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
185 |
@Override |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
186 |
public void setOrdered(boolean ordered) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
187 |
if (ordered == false) { |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
188 |
throw new UnsupportedOperationException("Unordered not implemented yet"); |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
189 |
} |
6a7e7743b82f
setOrdered and setReuse implemented for file stream, incl. unit tests
egahlin
parents:
57376
diff
changeset
|
190 |
} |
57361 | 191 |
} |