author | jiangli |
Fri, 02 Nov 2018 19:30:31 -0400 | |
changeset 52397 | 1322829d1501 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
25959 | 1 |
/* |
43467 | 2 |
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. |
25959 | 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 |
|
40631
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
36851
diff
changeset
|
28 |
* @library /test/lib |
36851 | 29 |
* @modules java.base/jdk.internal.misc |
29678
dd2f3932c21e
8075586: Add @modules as needed to the open hotspot tests
ykantser
parents:
25959
diff
changeset
|
30 |
* java.compiler |
dd2f3932c21e
8075586: Add @modules as needed to the open hotspot tests
ykantser
parents:
25959
diff
changeset
|
31 |
* java.management |
dd2f3932c21e
8075586: Add @modules as needed to the open hotspot tests
ykantser
parents:
25959
diff
changeset
|
32 |
* jdk.attach/sun.tools.attach |
43467 | 33 |
* jdk.internal.jvmstat/sun.jvmstat.monitor |
36209
8db2a78cbc29
8151156: [TESTBUG] Integrate trivial Hotspot test changes from Jake before Jigsaw M3
ctornqvi
parents:
31330
diff
changeset
|
34 |
* @run main AttachSetGetFlag |
25959 | 35 |
*/ |
36 |
||
37 |
import java.io.BufferedReader; |
|
38 |
import java.io.InputStreamReader; |
|
39 |
import java.io.InputStream; |
|
40 |
import java.lang.reflect.Field; |
|
41 |
import java.nio.file.Files; |
|
42 |
import java.nio.file.Path; |
|
43 |
import java.nio.file.Paths; |
|
44 |
||
45 |
import sun.tools.attach.HotSpotVirtualMachine; |
|
46 |
||
30604
b8d532cb6420
8067013: Rename the com.oracle.java.testlibary package
ykantser
parents:
29678
diff
changeset
|
47 |
import jdk.test.lib.Asserts; |
b8d532cb6420
8067013: Rename the com.oracle.java.testlibary package
ykantser
parents:
29678
diff
changeset
|
48 |
import jdk.test.lib.Platform; |
40631
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
36851
diff
changeset
|
49 |
import jdk.test.lib.process.ProcessTools; |
25959 | 50 |
import com.sun.tools.attach.VirtualMachine; |
51 |
||
52 |
public class AttachSetGetFlag { |
|
53 |
||
54 |
public static void main(String... args) throws Exception { |
|
55 |
// Test a manageable uintx flag. |
|
56 |
testGetFlag("MaxHeapFreeRatio", "60"); |
|
57 |
testSetFlag("MaxHeapFreeRatio", "50", "60"); |
|
58 |
||
59 |
// Test a non-manageable size_t flag. |
|
60 |
// Since it is not manageable, we can't test the setFlag functionality. |
|
61 |
testGetFlag("ArrayAllocatorMallocLimit", "128"); |
|
62 |
// testSetFlag("ArrayAllocatorMallocLimit", "64", "128"); |
|
31330
77061bb01b18
8081382: Make flags ParallelGCThreads and ConcGCThreads of type uint
david
parents:
30604
diff
changeset
|
63 |
|
77061bb01b18
8081382: Make flags ParallelGCThreads and ConcGCThreads of type uint
david
parents:
30604
diff
changeset
|
64 |
// Test a uint flag. |
77061bb01b18
8081382: Make flags ParallelGCThreads and ConcGCThreads of type uint
david
parents:
30604
diff
changeset
|
65 |
testGetFlag("ParallelGCThreads", "10"); |
25959 | 66 |
} |
67 |
||
68 |
public static ProcessBuilder runTarget(String flagName, String flagValue) throws Exception { |
|
69 |
return ProcessTools.createJavaProcessBuilder( |
|
70 |
"-XX:+UnlockExperimentalVMOptions", |
|
71 |
"-XX:" + flagName + "=" + flagValue, |
|
72 |
"AttachSetGetFlag$Target"); |
|
73 |
} |
|
74 |
||
75 |
public static void testGetFlag(String flagName, String flagValue) throws Exception { |
|
76 |
ProcessBuilder pb = runTarget(flagName, flagValue); |
|
77 |
||
78 |
Process target = pb.start(); |
|
79 |
||
80 |
try { |
|
81 |
waitForReady(target); |
|
82 |
||
44633
d2f5e8921045
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
43467
diff
changeset
|
83 |
int pid = (int)target.pid(); |
25959 | 84 |
|
85 |
HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString()); |
|
86 |
||
87 |
// Test Get |
|
88 |
BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader( |
|
89 |
vm.printFlag(flagName))); |
|
90 |
||
91 |
boolean foundExpectedLine = false; |
|
92 |
||
93 |
String line = null; |
|
94 |
while((line = remoteDataReader.readLine()) != null) { |
|
95 |
System.out.println("printFlag: " + line); |
|
96 |
if (line.equals("-XX:" + flagName + "=" + flagValue)) { |
|
97 |
foundExpectedLine = true; |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'"); |
|
102 |
||
103 |
vm.detach(); |
|
104 |
} |
|
105 |
finally { |
|
106 |
target.destroy(); |
|
107 |
target.waitFor(); |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
public static void testSetFlag(String flagName, String initialFlagValue, String flagValue) throws Exception { |
|
112 |
ProcessBuilder pb = runTarget(flagName, initialFlagValue); |
|
113 |
||
114 |
Process target = pb.start(); |
|
115 |
||
116 |
try { |
|
117 |
waitForReady(target); |
|
118 |
||
44633
d2f5e8921045
8178347: Process and ProcessHandle getPid method name inconsistency
rriggs
parents:
43467
diff
changeset
|
119 |
int pid = (int)target.pid(); |
25959 | 120 |
|
121 |
HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString()); |
|
122 |
||
123 |
// First set the value. |
|
124 |
BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader( |
|
125 |
vm.setFlag(flagName, flagValue))); |
|
126 |
||
127 |
String line; |
|
128 |
while((line = remoteDataReader.readLine()) != null) { |
|
129 |
System.out.println("setFlag: " + line); |
|
130 |
// Just empty the stream. |
|
131 |
} |
|
132 |
remoteDataReader.close(); |
|
133 |
||
134 |
// Then read and make sure we get back the set value. |
|
135 |
remoteDataReader = new BufferedReader(new InputStreamReader(vm.printFlag(flagName))); |
|
136 |
||
137 |
boolean foundExpectedLine = false; |
|
138 |
line = null; |
|
139 |
while((line = remoteDataReader.readLine()) != null) { |
|
140 |
System.out.println("getFlag: " + line); |
|
141 |
if (line.equals("-XX:" + flagName + "=" + flagValue)) { |
|
142 |
foundExpectedLine = true; |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'"); |
|
147 |
||
148 |
vm.detach(); |
|
149 |
||
150 |
} finally { |
|
151 |
target.destroy(); |
|
152 |
target.waitFor(); |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
private static void waitForReady(Process target) throws Exception { |
|
157 |
InputStream os = target.getInputStream(); |
|
158 |
try (BufferedReader reader = new BufferedReader(new InputStreamReader(os))) { |
|
159 |
String line; |
|
160 |
while ((line = reader.readLine()) != null) { |
|
161 |
if ("Ready".equals(line)) { |
|
162 |
return; |
|
163 |
} |
|
164 |
} |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
||
169 |
public static class Target { |
|
170 |
public static void main(String [] args) throws Exception { |
|
171 |
System.out.println("Ready"); |
|
172 |
System.out.flush(); |
|
173 |
while (true) { |
|
174 |
Thread.sleep(1000); |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
178 |
} |