Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/attach/ConcAttachTest.java
41152 views
1
/*
2
* Copyright (c) 2019, 2020, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8225690
27
* @requires os.family != "windows"
28
* @library /test/lib
29
* @modules jdk.attach/com.sun.tools.attach
30
* @run main ConcAttachTest
31
*/
32
33
import java.io.IOException;
34
import java.util.concurrent.CountDownLatch;
35
import java.util.concurrent.Executors;
36
import java.util.concurrent.ExecutorService;
37
import java.util.concurrent.TimeUnit;
38
39
import com.sun.tools.attach.VirtualMachine;
40
import com.sun.tools.attach.AttachNotSupportedException;
41
42
import jdk.test.lib.Utils;
43
import jdk.test.lib.apps.LingeredApp;
44
import jdk.test.lib.Asserts;
45
import jdk.test.lib.JDKToolLauncher;
46
import jdk.test.lib.process.OutputAnalyzer;
47
48
public class ConcAttachTest implements Runnable {
49
50
private static final int NUM_CONC_REQUESTS = 100;
51
52
private static final int THREAD_POOL_TIMEOUT_IN_SEC = 30;
53
54
private static CountDownLatch latch;
55
56
private static String strPID;
57
58
// Attach to LingeredApp concurrently.
59
public void run() {
60
VirtualMachine vm = null;
61
62
try {
63
latch.countDown();
64
latch.await();
65
} catch (InterruptedException e) {
66
throw new RuntimeException(e);
67
}
68
69
try {
70
vm = VirtualMachine.attach(strPID);
71
} catch (AttachNotSupportedException | IOException e) {
72
throw new RuntimeException(e);
73
} finally {
74
try {
75
vm.detach();
76
} catch (IOException e) {
77
throw new RuntimeException(e);
78
}
79
}
80
}
81
82
private static void checkAttachListenerThread() throws InterruptedException, IOException {
83
JDKToolLauncher jcmd = JDKToolLauncher.createUsingTestJDK("jcmd");
84
jcmd.addVMArgs(Utils.getTestJavaOpts());
85
jcmd.addToolArg(strPID);
86
jcmd.addToolArg("Thread.print");
87
88
ProcessBuilder pb = new ProcessBuilder(jcmd.getCommand());
89
Process jcmdProc = pb.start();
90
91
OutputAnalyzer out = new OutputAnalyzer(jcmdProc);
92
93
jcmdProc.waitFor();
94
95
System.out.println(out.getStdout());
96
System.err.println(out.getStderr());
97
98
long numOfAttachListener = out.asLines()
99
.stream()
100
.filter(l -> l.contains("Attach Listener"))
101
.count();
102
103
Asserts.assertEquals(1L, numOfAttachListener, "AttachListener should exist only 1 thread.");
104
}
105
106
public static void main(String... args) throws Exception {
107
LingeredApp app = null;
108
latch = new CountDownLatch(NUM_CONC_REQUESTS);
109
ExecutorService pool = Executors.newFixedThreadPool(NUM_CONC_REQUESTS);
110
111
try {
112
app = LingeredApp.startApp();
113
strPID = Long.toString(app.getPid());
114
115
for (int i = 0; i < NUM_CONC_REQUESTS; i++) {
116
pool.submit(new ConcAttachTest());
117
}
118
119
pool.shutdown();
120
pool.awaitTermination(THREAD_POOL_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
121
122
checkAttachListenerThread();
123
} finally {
124
LingeredApp.stopApp(app);
125
}
126
}
127
128
}
129
130