author | egahlin |
Wed, 12 Dec 2018 18:35:26 +0100 | |
changeset 52981 | 4eff16f47ae2 |
parent 51214 | 67736b4846a0 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
2 |
* Copyright (c) 2013, 2018, 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.event.runtime; |
|
27 |
||
28 |
import static jdk.test.lib.Asserts.assertTrue; |
|
29 |
||
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
30 |
import java.nio.file.Files; |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
31 |
import java.nio.file.Paths; |
50113 | 32 |
import java.time.Duration; |
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
33 |
import java.time.Instant; |
50113 | 34 |
import java.util.List; |
35 |
import java.util.concurrent.CountDownLatch; |
|
36 |
import java.util.concurrent.locks.LockSupport; |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
37 |
import java.util.function.Consumer; |
50113 | 38 |
|
39 |
import jdk.jfr.Recording; |
|
40 |
import jdk.jfr.consumer.RecordedEvent; |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
41 |
import jdk.test.lib.Asserts; |
50113 | 42 |
import jdk.test.lib.jfr.EventNames; |
43 |
import jdk.test.lib.jfr.Events; |
|
44 |
import jdk.test.lib.management.ThreadMXBeanTool; |
|
45 |
import jdk.test.lib.thread.TestThread; |
|
46 |
||
51214 | 47 |
/** |
50113 | 48 |
* @test |
49 |
* @key jfr |
|
51214 | 50 |
* @requires vm.hasJFR |
50113 | 51 |
* @library /test/lib |
52 |
* |
|
53 |
* @run main/othervm jdk.jfr.event.runtime.TestThreadParkEvent |
|
54 |
*/ |
|
55 |
||
56 |
public class TestThreadParkEvent { |
|
57 |
private static final String EVENT_NAME = EventNames.ThreadPark; |
|
58 |
private static final long THRESHOLD_MILLIS = 1; |
|
59 |
||
60 |
static class Blocker { |
|
61 |
} |
|
62 |
||
63 |
public static void main(String[] args) throws Throwable { |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
64 |
testParkNoTimeout(); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
65 |
testParkTimeout(); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
66 |
testParkUntil(); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
67 |
} |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
68 |
|
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
69 |
private static void testParkNoTimeout() throws Exception { |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
70 |
RecordedEvent event = testPark(x -> LockSupport.park(x), Thread.State.WAITING); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
71 |
Events.assertMissingValue(event, "timeout"); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
72 |
Events.assertMissingValue(event, "until"); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
73 |
} |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
74 |
|
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
75 |
private static void testParkTimeout() throws Exception { |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
76 |
Duration expected = Duration.ofNanos(1_234_567_890_123L); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
77 |
RecordedEvent event = testPark(x -> LockSupport.parkNanos(x, expected.toNanos()), Thread.State.TIMED_WAITING); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
78 |
Events.assertDuration(event, "timeout", expected); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
79 |
Events.assertMissingValue(event, "until"); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
80 |
} |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
81 |
|
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
82 |
private static void testParkUntil() throws Exception { |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
83 |
long epochMillis = Instant.now().plusSeconds(1000000).toEpochMilli(); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
84 |
RecordedEvent event = testPark(x -> LockSupport.parkUntil(x, epochMillis), Thread.State.TIMED_WAITING); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
85 |
Events.assertMissingValue(event, "timeout"); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
86 |
Events.assertInstant(event, "until", Instant.ofEpochMilli(epochMillis)); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
87 |
} |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
88 |
|
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
89 |
static RecordedEvent testPark(Consumer<Blocker> parkOperation, Thread.State threadState) throws Exception { |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
90 |
|
50113 | 91 |
final CountDownLatch stop = new CountDownLatch(1); |
92 |
final Blocker blocker = new Blocker(); |
|
93 |
TestThread parkThread = new TestThread(new Runnable() { |
|
94 |
public void run() { |
|
95 |
while (stop.getCount() > 0) { |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
96 |
parkOperation.accept(blocker); |
50113 | 97 |
} |
98 |
} |
|
99 |
}); |
|
100 |
||
101 |
Recording recording = new Recording(); |
|
102 |
recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(THRESHOLD_MILLIS)); |
|
103 |
try { |
|
104 |
recording.start(); |
|
105 |
parkThread.start(); |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
106 |
ThreadMXBeanTool.waitUntilBlockingOnObject(parkThread, threadState, blocker); |
50113 | 107 |
// sleep so we know the event is recorded |
108 |
Thread.sleep(2 * THRESHOLD_MILLIS); |
|
109 |
} finally { |
|
110 |
stop.countDown(); |
|
111 |
LockSupport.unpark(parkThread); |
|
112 |
parkThread.join(); |
|
113 |
recording.stop(); |
|
114 |
} |
|
115 |
List<RecordedEvent> events = Events.fromRecording(recording); |
|
116 |
Events.hasEvents(events); |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
117 |
RecordedEvent foundEvent = null; |
50113 | 118 |
for (RecordedEvent event : events) { |
119 |
System.out.println("Event:" + event); |
|
120 |
String klassName = Events.assertField(event, "parkedClass.name").notNull().getValue(); |
|
121 |
if (klassName.equals(blocker.getClass().getName().replace('.', '/'))) { |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
122 |
Asserts.assertNull(foundEvent , "Found more than 1 event"); |
50113 | 123 |
Events.assertField(event, "address").notEqual(0L); |
124 |
Events.assertEventThread(event, parkThread); |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
125 |
foundEvent = event; |
50113 | 126 |
} |
127 |
} |
|
52981
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
128 |
Asserts.assertNotNull(foundEvent, "Correct event not found"); |
4eff16f47ae2
8165675: Trace event for thread park has incorrect unit for timeout
egahlin
parents:
51214
diff
changeset
|
129 |
return foundEvent; |
50113 | 130 |
} |
131 |
||
132 |
} |