author | egahlin |
Fri, 24 May 2019 20:30:12 +0200 | |
branch | JEP-349-branch |
changeset 57373 | 400db63e4937 |
parent 57372 | 50ca040843ea |
child 57376 | 8e8a06a3059c |
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 |
} |
105 |
} |
|
106 |
||
57373 | 107 |
private final EventSetConsumer eventConsumer; |
57361 | 108 |
|
109 |
public EventDirectoryStream(AccessControlContext acc) throws IOException { |
|
57373 | 110 |
eventConsumer = new EventSetConsumer(acc); |
57361 | 111 |
} |
112 |
||
113 |
public void close() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
114 |
eventConsumer.close(); |
57361 | 115 |
} |
116 |
||
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
117 |
public void onFlush(Runnable action) { |
57361 | 118 |
Objects.requireNonNull(action); |
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
119 |
eventConsumer.onFlush(action); |
57361 | 120 |
} |
121 |
||
122 |
void start(long startNanos) { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
123 |
eventConsumer.start(startNanos); |
57361 | 124 |
} |
125 |
||
126 |
@Override |
|
127 |
public void start() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
128 |
start(Instant.now().toEpochMilli() * 1000 * 1000L); |
57361 | 129 |
} |
130 |
||
131 |
@Override |
|
132 |
public void startAsync() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
133 |
startAsync(Instant.now().toEpochMilli() * 1000 * 1000L); |
57361 | 134 |
} |
135 |
||
136 |
void startAsync(long startNanos) { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
137 |
eventConsumer.startAsync(startNanos); |
57361 | 138 |
} |
139 |
||
140 |
@Override |
|
141 |
public void onEvent(Consumer<RecordedEvent> action) { |
|
142 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
143 |
eventConsumer.onEvent(action); |
57361 | 144 |
} |
145 |
||
146 |
@Override |
|
147 |
public void onEvent(String eventName, Consumer<RecordedEvent> action) { |
|
148 |
Objects.requireNonNull(eventName); |
|
149 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
150 |
eventConsumer.onEvent(eventName, action); |
57361 | 151 |
} |
152 |
||
153 |
@Override |
|
154 |
public void onClose(Runnable action) { |
|
155 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
156 |
eventConsumer.addCloseAction(action); |
57361 | 157 |
} |
158 |
||
159 |
@Override |
|
160 |
public boolean remove(Object action) { |
|
161 |
Objects.requireNonNull(action); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
162 |
return eventConsumer.remove(action); |
57361 | 163 |
} |
164 |
||
165 |
@Override |
|
166 |
public void awaitTermination(Duration timeout) { |
|
167 |
Objects.requireNonNull(timeout); |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
168 |
eventConsumer.awaitTermination(timeout); |
57361 | 169 |
} |
170 |
||
171 |
@Override |
|
172 |
public void awaitTermination() { |
|
57372
50ca040843ea
Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
57361
diff
changeset
|
173 |
eventConsumer.awaitTermination(Duration.ofMillis(0)); |
57361 | 174 |
} |
175 |
} |