25959
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 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 8054823
|
|
27 |
* @summary Tests the setFlag and printFlag attach command
|
|
28 |
* @library /testlibrary
|
|
29 |
* @build com.oracle.java.testlibrary.* AttachSetGetFlag
|
|
30 |
* @run driver AttachSetGetFlag
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.io.BufferedReader;
|
|
34 |
import java.io.InputStreamReader;
|
|
35 |
import java.io.InputStream;
|
|
36 |
import java.lang.reflect.Field;
|
|
37 |
import java.nio.file.Files;
|
|
38 |
import java.nio.file.Path;
|
|
39 |
import java.nio.file.Paths;
|
|
40 |
|
|
41 |
import sun.tools.attach.HotSpotVirtualMachine;
|
|
42 |
|
|
43 |
import com.oracle.java.testlibrary.Asserts;
|
|
44 |
import com.oracle.java.testlibrary.Platform;
|
|
45 |
import com.oracle.java.testlibrary.ProcessTools;
|
|
46 |
import com.sun.tools.attach.VirtualMachine;
|
|
47 |
|
|
48 |
public class AttachSetGetFlag {
|
|
49 |
|
|
50 |
public static void main(String... args) throws Exception {
|
|
51 |
// Test a manageable uintx flag.
|
|
52 |
testGetFlag("MaxHeapFreeRatio", "60");
|
|
53 |
testSetFlag("MaxHeapFreeRatio", "50", "60");
|
|
54 |
|
|
55 |
// Test a non-manageable size_t flag.
|
|
56 |
// Since it is not manageable, we can't test the setFlag functionality.
|
|
57 |
testGetFlag("ArrayAllocatorMallocLimit", "128");
|
|
58 |
// testSetFlag("ArrayAllocatorMallocLimit", "64", "128");
|
|
59 |
}
|
|
60 |
|
|
61 |
public static ProcessBuilder runTarget(String flagName, String flagValue) throws Exception {
|
|
62 |
return ProcessTools.createJavaProcessBuilder(
|
|
63 |
"-XX:+UnlockExperimentalVMOptions",
|
|
64 |
"-XX:" + flagName + "=" + flagValue,
|
|
65 |
"AttachSetGetFlag$Target");
|
|
66 |
}
|
|
67 |
|
|
68 |
public static void testGetFlag(String flagName, String flagValue) throws Exception {
|
|
69 |
ProcessBuilder pb = runTarget(flagName, flagValue);
|
|
70 |
|
|
71 |
Process target = pb.start();
|
|
72 |
|
|
73 |
try {
|
|
74 |
waitForReady(target);
|
|
75 |
|
|
76 |
int pid = (int)target.getPid();
|
|
77 |
|
|
78 |
HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
|
|
79 |
|
|
80 |
// Test Get
|
|
81 |
BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(
|
|
82 |
vm.printFlag(flagName)));
|
|
83 |
|
|
84 |
boolean foundExpectedLine = false;
|
|
85 |
|
|
86 |
String line = null;
|
|
87 |
while((line = remoteDataReader.readLine()) != null) {
|
|
88 |
System.out.println("printFlag: " + line);
|
|
89 |
if (line.equals("-XX:" + flagName + "=" + flagValue)) {
|
|
90 |
foundExpectedLine = true;
|
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'");
|
|
95 |
|
|
96 |
vm.detach();
|
|
97 |
}
|
|
98 |
finally {
|
|
99 |
target.destroy();
|
|
100 |
target.waitFor();
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
public static void testSetFlag(String flagName, String initialFlagValue, String flagValue) throws Exception {
|
|
105 |
ProcessBuilder pb = runTarget(flagName, initialFlagValue);
|
|
106 |
|
|
107 |
Process target = pb.start();
|
|
108 |
|
|
109 |
try {
|
|
110 |
waitForReady(target);
|
|
111 |
|
|
112 |
int pid = (int)target.getPid();
|
|
113 |
|
|
114 |
HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());
|
|
115 |
|
|
116 |
// First set the value.
|
|
117 |
BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(
|
|
118 |
vm.setFlag(flagName, flagValue)));
|
|
119 |
|
|
120 |
String line;
|
|
121 |
while((line = remoteDataReader.readLine()) != null) {
|
|
122 |
System.out.println("setFlag: " + line);
|
|
123 |
// Just empty the stream.
|
|
124 |
}
|
|
125 |
remoteDataReader.close();
|
|
126 |
|
|
127 |
// Then read and make sure we get back the set value.
|
|
128 |
remoteDataReader = new BufferedReader(new InputStreamReader(vm.printFlag(flagName)));
|
|
129 |
|
|
130 |
boolean foundExpectedLine = false;
|
|
131 |
line = null;
|
|
132 |
while((line = remoteDataReader.readLine()) != null) {
|
|
133 |
System.out.println("getFlag: " + line);
|
|
134 |
if (line.equals("-XX:" + flagName + "=" + flagValue)) {
|
|
135 |
foundExpectedLine = true;
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'");
|
|
140 |
|
|
141 |
vm.detach();
|
|
142 |
|
|
143 |
} finally {
|
|
144 |
target.destroy();
|
|
145 |
target.waitFor();
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
private static void waitForReady(Process target) throws Exception {
|
|
150 |
InputStream os = target.getInputStream();
|
|
151 |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(os))) {
|
|
152 |
String line;
|
|
153 |
while ((line = reader.readLine()) != null) {
|
|
154 |
if ("Ready".equals(line)) {
|
|
155 |
return;
|
|
156 |
}
|
|
157 |
}
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
|
|
162 |
public static class Target {
|
|
163 |
public static void main(String [] args) throws Exception {
|
|
164 |
System.out.println("Ready");
|
|
165 |
System.out.flush();
|
|
166 |
while (true) {
|
|
167 |
Thread.sleep(1000);
|
|
168 |
}
|
|
169 |
}
|
|
170 |
}
|
|
171 |
}
|