Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/ProcessBuilder/checkHandles/CheckHandles.java
41152 views
1
/*
2
* Copyright (c) 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
import java.io.BufferedReader;
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.io.InputStreamReader;
28
import java.lang.ProcessHandle;
29
30
import jdk.test.lib.util.FileUtils;
31
32
/*
33
* @test
34
* @bug 8239893
35
* @summary Verify that handles for processes that terminate do not accumulate
36
* @requires ((os.family == "windows") & (vm.compMode != "Xcomp"))
37
* @library /test/lib
38
* @run main/othervm/native -Xint CheckHandles
39
*/
40
public class CheckHandles {
41
42
public static void main(String[] args) throws Exception {
43
System.out.println("mypid: " + ProcessHandle.current().pid());
44
45
// Warmup the process launch mechanism and vm to stabilize the number of handles in use
46
int MAX_WARMUP = 20;
47
long prevCount = FileUtils.getProcessHandleCount();
48
for (int i = 0; i < MAX_WARMUP; i++) {
49
oneProcess();
50
System.gc(); // an opportunity to close unreferenced handles
51
sleep(10);
52
53
long count = FileUtils.getProcessHandleCount();
54
if (count < 0)
55
throw new AssertionError("getProcessHandleCount failed");
56
System.out.println("warmup handle delta: " + (count - prevCount));
57
prevCount = count;
58
}
59
System.out.println("Warmup done");
60
System.out.println();
61
62
prevCount = FileUtils.getProcessHandleCount();
63
long startHandles = prevCount;
64
long maxHandles = startHandles;
65
int MAX_SPAWN = 50;
66
for (int i = 0; i < MAX_SPAWN; i++) {
67
oneProcess();
68
System.gc(); // an opportunity to close unreferenced handles
69
sleep(10);
70
71
long count = FileUtils.getProcessHandleCount();
72
if (count < 0)
73
throw new AssertionError("getProcessHandleCount failed");
74
System.out.println("handle delta: " + (count - prevCount));
75
prevCount = count;
76
maxHandles = Math.max(maxHandles, count);
77
}
78
79
System.out.println("Processes started: " + MAX_SPAWN);
80
System.out.println("startHandles: " + startHandles);
81
System.out.println("maxHandles: " + maxHandles);
82
83
final float ERROR_PERCENT = 10.0f; // allowable extra handles
84
final long ERROR_THRESHOLD = startHandles + Math.round(startHandles * ERROR_PERCENT / 100.0f);
85
if (maxHandles >= ERROR_THRESHOLD) {
86
throw new AssertionError("Handle use increased by more than " + ERROR_PERCENT + " percent.");
87
}
88
}
89
90
/**
91
* Start a single process and consume its output.
92
*/
93
private static void oneProcess() {
94
try {
95
96
Process testProcess = new ProcessBuilder("cmd", "/c", "dir").start();
97
98
Thread outputConsumer = new Thread(() -> consumeStream(testProcess.getInputStream()));
99
outputConsumer.setDaemon(true);
100
outputConsumer.start();
101
Thread errorConsumer = new Thread(() -> consumeStream(testProcess.getErrorStream()));
102
errorConsumer.setDaemon(true);
103
errorConsumer.start();
104
105
testProcess.waitFor();
106
outputConsumer.join();
107
errorConsumer.join();
108
} catch (IOException | InterruptedException e) {
109
e.printStackTrace();
110
throw new RuntimeException("Exception", e);
111
}
112
}
113
114
private static void consumeStream(InputStream inputStream) {
115
BufferedReader reader = null;
116
try {
117
int lines = 0;
118
reader = new BufferedReader(new InputStreamReader(inputStream));
119
while (reader.readLine() != null) {
120
lines++;
121
}
122
} catch (IOException e) {
123
e.printStackTrace();
124
} finally {
125
if (reader != null) {
126
try {
127
reader.close();
128
} catch (IOException e) {
129
e.printStackTrace();
130
}
131
}
132
}
133
}
134
135
private static void sleep(long millis) {
136
try {
137
Thread.sleep(millis);
138
} catch (InterruptedException ie) {
139
// ignore
140
}
141
}
142
}
143
144