1/* 2 * Copyright (c) 2014, 2017, 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 /test/lib 29 * @modules java.base/jdk.internal.misc 30 * java.compiler 31 * java.management 32 * jdk.attach/sun.tools.attach 33 * jdk.internal.jvmstat/sun.jvmstat.monitor 34 * @run main AttachSetGetFlag 35 */ 36 37import java.io.BufferedReader; 38import java.io.InputStreamReader; 39import java.io.InputStream; 40import java.lang.reflect.Field; 41import java.nio.file.Files; 42import java.nio.file.Path; 43import java.nio.file.Paths; 44 45import sun.tools.attach.HotSpotVirtualMachine; 46 47import jdk.test.lib.Asserts; 48import jdk.test.lib.Platform; 49import jdk.test.lib.process.ProcessTools; 50import com.sun.tools.attach.VirtualMachine; 51 52public 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"); 63 64 // Test a uint flag. 65 testGetFlag("ParallelGCThreads", "10"); 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 83 int pid = (int)target.pid(); 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 119 int pid = (int)target.pid(); 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} 179