|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
25 | 25 |
|
26 | 26 | package jdk.jfr.event.sampling;
|
27 | 27 |
|
28 |
| -import java.io.File; |
29 |
| -import java.nio.file.Paths; |
30 | 28 | import java.time.Duration;
|
| 29 | +import java.util.List; |
| 30 | +import java.util.concurrent.Semaphore; |
| 31 | +import java.util.concurrent.atomic.AtomicInteger; |
31 | 32 |
|
32 |
| -import jdk.jfr.FlightRecorder; |
33 | 33 | import jdk.jfr.Recording;
|
34 |
| -import jdk.jfr.consumer.RecordedEvent; |
35 |
| -import jdk.jfr.consumer.RecordingFile; |
| 34 | +import jdk.jfr.consumer.RecordedFrame; |
| 35 | +import jdk.jfr.consumer.RecordingStream; |
| 36 | +import jdk.jfr.internal.JVM; |
36 | 37 | import jdk.test.lib.jfr.EventNames;
|
37 |
| -import jdk.test.lib.process.OutputAnalyzer; |
38 |
| -import jdk.test.lib.process.ProcessTools; |
39 | 38 |
|
40 | 39 | /*
|
41 | 40 | * @test
|
42 | 41 | * @key jfr
|
43 | 42 | * @requires vm.hasJFR
|
44 | 43 | * @library /test/lib
|
45 |
| - * @modules java.base/jdk.internal.misc |
46 |
| - * @run main/native jdk.jfr.event.sampling.TestNative |
| 44 | + * @modules jdk.jfr/jdk.jfr.internal |
| 45 | + * @run main jdk.jfr.event.sampling.TestNative |
47 | 46 | */
|
48 | 47 | public class TestNative {
|
49 | 48 |
|
50 |
| - public final static String EVENT_SETTINGS_FILE = System.getProperty("test.src", ".") + File.separator + "sampling.jfc"; |
51 |
| - public final static String JFR_DUMP = "samples.jfr"; |
52 |
| - public final static String EXCEPTION = "No native samples found"; |
53 |
| - public final static String NATIVE_EVENT = EventNames.NativeMethodSample; |
54 |
| - public static Recording recording; |
| 49 | + final static String NATIVE_EVENT = EventNames.NativeMethodSample; |
55 | 50 |
|
56 |
| - public static native void longTime(); |
| 51 | + static volatile boolean alive = true; |
57 | 52 |
|
58 | 53 | public static void main(String[] args) throws Exception {
|
59 |
| - String lib = System.getProperty("test.nativepath"); |
60 |
| - ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "-Djava.library.path=" + lib, "jdk.jfr.event.sampling.TestNative$Test"); |
| 54 | + try (RecordingStream rs = new RecordingStream()) { |
| 55 | + rs.enable(NATIVE_EVENT).withPeriod(Duration.ofMillis(1)); |
| 56 | + rs.onEvent(NATIVE_EVENT, e -> { |
| 57 | + alive = false; |
| 58 | + rs.close(); |
| 59 | + }); |
| 60 | + Thread t = new Thread(TestNative::nativeMethod); |
| 61 | + t.setDaemon(true); |
| 62 | + t.start(); |
| 63 | + rs.start(); |
| 64 | + } |
61 | 65 |
|
62 |
| - OutputAnalyzer output = ProcessTools.executeProcess(pb); |
63 |
| - output.shouldHaveExitValue(0); |
64 |
| - output.stdoutShouldNotContain("No native samples found"); |
65 | 66 | }
|
66 | 67 |
|
67 |
| - static class Test { |
68 |
| - public static void main(String[] args) throws Exception { |
69 |
| - System.loadLibrary("TestNative"); |
70 |
| - FlightRecorder.getFlightRecorder(); |
71 |
| - recording = new Recording(); |
72 |
| - recording.setToDisk(true); |
73 |
| - recording.setDestination(Paths.get(JFR_DUMP)); |
74 |
| - recording.enable(NATIVE_EVENT).withPeriod(Duration.ofMillis(10)); |
75 |
| - recording.start(); |
76 |
| - |
77 |
| - longTime(); |
78 |
| - |
79 |
| - recording.stop(); |
80 |
| - recording.close(); |
81 |
| - |
82 |
| - try (RecordingFile rf = new RecordingFile(Paths.get(JFR_DUMP))) { |
83 |
| - while (rf.hasMoreEvents()) { |
84 |
| - RecordedEvent re = rf.readEvent(); |
85 |
| - if (re.getEventType().getName().equals(NATIVE_EVENT)) { |
86 |
| - return; |
87 |
| - } |
88 |
| - } |
89 |
| - } |
90 |
| - |
91 |
| - throw new Exception("No native samples found"); |
| 68 | + public static void nativeMethod() { |
| 69 | + while (alive) { |
| 70 | + JVM.getJVM().getPid(); |
92 | 71 | }
|
93 | 72 | }
|
94 | 73 | }
|
0 commit comments