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