author | egahlin |
Mon, 28 Oct 2019 21:31:23 +0100 | |
branch | JEP-349-branch |
changeset 58828 | 4dc714767d69 |
parent 58806 | a7d850b47b19 |
permissions | -rw-r--r-- |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
1 |
/* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
4 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
10 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
15 |
* accompanied this code). |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
16 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
20 |
* |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
23 |
* questions. |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
24 |
*/ |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
25 |
|
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
26 |
package jdk.jfr.api.consumer.streaming; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
27 |
|
58806 | 28 |
import java.nio.file.Path; |
29 |
import java.nio.file.Paths; |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
30 |
import java.util.concurrent.CountDownLatch; |
58806 | 31 |
import java.util.concurrent.TimeUnit; |
58687 | 32 |
import java.util.concurrent.atomic.AtomicBoolean; |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
33 |
|
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
34 |
import jdk.jfr.Event; |
58806 | 35 |
import jdk.jfr.FlightRecorder; |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
36 |
import jdk.jfr.Name; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
37 |
import jdk.jfr.Recording; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
38 |
import jdk.jfr.consumer.EventStream; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
39 |
import jdk.jfr.consumer.RecordingStream; |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
40 |
|
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
41 |
/** |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
42 |
* @test |
58806 | 43 |
* @summary Verifies that EventStream::openRepository() read from the latest flush |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
44 |
* @key jfr |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
45 |
* @requires vm.hasJFR |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
46 |
* @library /test/lib |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
47 |
* @run main/othervm jdk.jfr.api.consumer.streaming.TestLatestEvent |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
48 |
*/ |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
49 |
public class TestLatestEvent { |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
50 |
|
58687 | 51 |
@Name("NotLatest") |
52 |
static class NotLatestEvent extends Event { |
|
58806 | 53 |
|
54 |
public int id; |
|
58687 | 55 |
} |
56 |
||
57 |
@Name("Latest") |
|
58 |
static class LatestEvent extends Event { |
|
59 |
} |
|
60 |
||
61 |
@Name("MakeChunks") |
|
62 |
static class MakeChunks extends Event { |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
63 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
64 |
|
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
65 |
public static void main(String... args) throws Exception { |
58806 | 66 |
CountDownLatch notLatestEvent = new CountDownLatch(6); |
67 |
CountDownLatch beginChunks = new CountDownLatch(1); |
|
68 |
||
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
69 |
try (RecordingStream r = new RecordingStream()) { |
58806 | 70 |
r.onEvent("MakeChunks", event-> { |
71 |
System.out.println(event); |
|
72 |
beginChunks.countDown(); |
|
73 |
}); |
|
74 |
r.onEvent("NotLatest", event -> { |
|
75 |
System.out.println(event); |
|
76 |
notLatestEvent.countDown(); |
|
77 |
}); |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
78 |
r.startAsync(); |
58687 | 79 |
MakeChunks e = new MakeChunks(); |
80 |
e.commit(); |
|
58806 | 81 |
|
82 |
System.out.println("Waiting for first chunk"); |
|
58687 | 83 |
beginChunks.await(); |
84 |
// Create 5 chunks with events in the repository |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
85 |
for (int i = 0; i < 5; i++) { |
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
86 |
System.out.println("Creating empty chunk"); |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
87 |
try (Recording r1 = new Recording()) { |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
88 |
r1.start(); |
58687 | 89 |
NotLatestEvent notLatest = new NotLatestEvent(); |
58806 | 90 |
notLatest.id = i; |
58687 | 91 |
notLatest.commit(); |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
92 |
r1.stop(); |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
93 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
94 |
} |
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
95 |
System.out.println("All empty chunks created"); |
58806 | 96 |
|
97 |
// Create event in new chunk |
|
58687 | 98 |
NotLatestEvent notLatest = new NotLatestEvent(); |
58806 | 99 |
notLatest.id = 5; |
58687 | 100 |
notLatest.commit(); |
58806 | 101 |
|
102 |
// This latch ensures thatNotLatest has been |
|
103 |
// flushed and a new valid position has been written |
|
104 |
// to the chunk header |
|
105 |
notLatestEvent.await(80, TimeUnit.SECONDS); |
|
106 |
if (notLatestEvent.getCount() != 0) { |
|
107 |
Recording rec = FlightRecorder.getFlightRecorder().takeSnapshot(); |
|
58828
4dc714767d69
TestLatestEvent: Abort if not 6 NotLatest events are found
egahlin
parents:
58806
diff
changeset
|
108 |
Path p = Paths.get("error-not-latest.jfr").toAbsolutePath(); |
58806 | 109 |
rec.dump(p); |
58828
4dc714767d69
TestLatestEvent: Abort if not 6 NotLatest events are found
egahlin
parents:
58806
diff
changeset
|
110 |
System.out.println("Dumping repository as a file for inspection at " + p); |
4dc714767d69
TestLatestEvent: Abort if not 6 NotLatest events are found
egahlin
parents:
58806
diff
changeset
|
111 |
throw new Exception("Timeout 80 s. Expected 6 event, but got " + notLatestEvent.getCount()); |
58806 | 112 |
} |
113 |
||
58687 | 114 |
try (EventStream s = EventStream.openRepository()) { |
58724
3d0a172353fc
Fix race conditation in test and add logging for TestLatestEvent
egahlin
parents:
58687
diff
changeset
|
115 |
System.out.println("EventStream opened"); |
58687 | 116 |
AtomicBoolean foundLatest = new AtomicBoolean(); |
117 |
s.onEvent(event -> { |
|
118 |
String name = event.getEventType().getName(); |
|
119 |
System.out.println("Found event " + name); |
|
120 |
foundLatest.set(name.equals("Latest")); |
|
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
121 |
}); |
58774 | 122 |
s.startAsync(); |
58806 | 123 |
// Must loop here as there is no guarantee |
124 |
// that the parser thread starts before event |
|
125 |
// is flushed |
|
126 |
while (!foundLatest.get()) { |
|
127 |
LatestEvent latest = new LatestEvent(); |
|
128 |
latest.commit(); |
|
129 |
System.out.println("Latest event emitted. Waiting 1 s ..."); |
|
130 |
Thread.sleep(1000); |
|
58687 | 131 |
} |
57690
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
132 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
133 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
134 |
} |
9316d02dd4a5
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents:
diff
changeset
|
135 |
} |