author | egahlin |
Mon, 25 Nov 2019 14:06:13 +0100 | |
changeset 59246 | fcad92f425c5 |
parent 59226 | a0f39cc47387 |
permissions | -rw-r--r-- |
59226 | 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 |
package jdk.jfr.api.consumer.streaming; |
|
26 |
||
27 |
import java.io.IOException; |
|
28 |
import java.io.RandomAccessFile; |
|
29 |
import java.nio.file.Files; |
|
59246
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
30 |
import java.nio.file.NoSuchFileException; |
59226 | 31 |
import java.nio.file.Path; |
32 |
import java.nio.file.Paths; |
|
33 |
import java.util.Properties; |
|
34 |
||
35 |
import jdk.internal.misc.Unsafe; |
|
36 |
import jdk.jfr.Event; |
|
37 |
import jdk.test.lib.process.ProcessTools; |
|
38 |
||
39 |
import com.sun.tools.attach.VirtualMachine; |
|
40 |
||
41 |
/** |
|
42 |
* Class that emits a NUMBER_OF_EVENTS and then awaits crash or exit |
|
43 |
* |
|
44 |
* Requires jdk.attach module. |
|
45 |
* |
|
46 |
*/ |
|
59246
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
47 |
public final class TestProcess implements AutoCloseable { |
59226 | 48 |
|
49 |
private static class TestEvent extends Event { |
|
50 |
} |
|
51 |
||
52 |
public final static int NUMBER_OF_EVENTS = 10; |
|
53 |
||
54 |
private final Process process; |
|
55 |
private final Path path; |
|
56 |
||
57 |
public TestProcess(String name) throws IOException { |
|
58 |
this.path = Paths.get("action-" + System.currentTimeMillis()).toAbsolutePath(); |
|
59 |
String[] args = { |
|
60 |
"--add-exports", |
|
61 |
"java.base/jdk.internal.misc=ALL-UNNAMED", |
|
62 |
"-XX:StartFlightRecording:settings=none", |
|
63 |
TestProcess.class.getName(), path.toString() |
|
64 |
}; |
|
65 |
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(false, args); |
|
66 |
process = ProcessTools.startProcess(name, pb); |
|
67 |
} |
|
68 |
||
69 |
public static void main(String... args) throws Exception { |
|
70 |
for (int i = 0; i < NUMBER_OF_EVENTS; i++) { |
|
71 |
TestEvent e = new TestEvent(); |
|
72 |
e.commit(); |
|
73 |
} |
|
74 |
||
75 |
Path path = Paths.get(args[0]); |
|
76 |
while (true) { |
|
77 |
try { |
|
78 |
String action = Files.readString(path); |
|
79 |
if ("crash".equals(action)) { |
|
80 |
System.out.println("About to crash..."); |
|
81 |
Unsafe.getUnsafe().putInt(0L, 0); |
|
82 |
} |
|
83 |
if ("exit".equals(action)) { |
|
84 |
System.out.println("About to exit..."); |
|
85 |
System.exit(0); |
|
86 |
} |
|
87 |
} catch (Exception ioe) { |
|
88 |
// Ignore |
|
89 |
} |
|
90 |
takeNap(); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
public Path getRepository() { |
|
95 |
while (true) { |
|
96 |
try { |
|
97 |
VirtualMachine vm = VirtualMachine.attach(String.valueOf(process.pid())); |
|
98 |
Properties p = vm.getSystemProperties(); |
|
99 |
vm.detach(); |
|
100 |
String repo = (String) p.get("jdk.jfr.repository"); |
|
101 |
if (repo != null) { |
|
102 |
return Paths.get(repo); |
|
103 |
} |
|
104 |
} catch (Exception e) { |
|
105 |
System.out.println("Attach failed: " + e.getMessage()); |
|
106 |
System.out.println("Retrying..."); |
|
107 |
} |
|
108 |
takeNap(); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
private static void takeNap() { |
|
113 |
try { |
|
114 |
Thread.sleep(10); |
|
115 |
} catch (InterruptedException ie) { |
|
116 |
// ignore |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
public void crash() { |
|
121 |
try { |
|
122 |
Files.writeString(path, "crash"); |
|
123 |
} catch (IOException ioe) { |
|
124 |
ioe.printStackTrace(); |
|
125 |
} |
|
126 |
} |
|
127 |
||
128 |
public void exit() { |
|
129 |
try { |
|
130 |
Files.writeString(path, "exit"); |
|
131 |
} catch (IOException ioe) { |
|
132 |
ioe.printStackTrace(); |
|
133 |
} |
|
134 |
} |
|
135 |
||
136 |
public long pid() { |
|
137 |
return process.pid(); |
|
138 |
} |
|
59246
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
139 |
|
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
140 |
@Override |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
141 |
public void close() throws Exception { |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
142 |
try { |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
143 |
if (path != null) { |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
144 |
Files.delete(path); |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
145 |
} |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
146 |
} catch(NoSuchFileException nfe) { |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
147 |
// ignore |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
148 |
} |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
149 |
} |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
150 |
|
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
151 |
public void awaitDeath() { |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
152 |
while (process.isAlive()) { |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
153 |
takeNap(); |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
154 |
} |
fcad92f425c5
8234703: JFR TestOutOfProcessMigration.java should clean up files
egahlin
parents:
59226
diff
changeset
|
155 |
} |
59226 | 156 |
} |