1 /* 2 * Copyright (c) 2014, 2015, 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/sun.misc 30 * java.compiler 31 * java.management 32 * jdk.attach/sun.tools.attach 33 * jdk.jvmstat/sun.jvmstat.monitor 34 * @build com.oracle.java.testlibrary.* AttachSetGetFlag 35 * @run driver 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 com.oracle.java.testlibrary.Asserts; 49 import com.oracle.java.testlibrary.Platform; 50 import com.oracle.java.testlibrary.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 66 public static ProcessBuilder runTarget(String flagName, String flagValue) throws Exception { 67 return ProcessTools.createJavaProcessBuilder( 68 "-XX:+UnlockExperimentalVMOptions", 69 "-XX:" + flagName + "=" + flagValue, 70 "AttachSetGetFlag$Target"); 71 } 72 73 public static void testGetFlag(String flagName, String flagValue) throws Exception { 74 ProcessBuilder pb = runTarget(flagName, flagValue); 75 76 Process target = pb.start(); 77 78 try { 79 waitForReady(target); 80 81 int pid = (int)target.getPid(); 82 83 HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString()); 84 85 // Test Get 86 BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader( 87 vm.printFlag(flagName))); 88 89 boolean foundExpectedLine = false; 90 91 String line = null; 92 while((line = remoteDataReader.readLine()) != null) { 93 System.out.println("printFlag: " + line); 94 if (line.equals("-XX:" + flagName + "=" + flagValue)) { 95 foundExpectedLine = true; 96 } 97 } 98 99 Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'"); 100 101 vm.detach(); 102 } 103 finally { 104 target.destroy(); 105 target.waitFor(); 106 } 107 } 108 109 public static void testSetFlag(String flagName, String initialFlagValue, String flagValue) throws Exception { 110 ProcessBuilder pb = runTarget(flagName, initialFlagValue); 111 112 Process target = pb.start(); 113 114 try { 115 waitForReady(target); 116 117 int pid = (int)target.getPid(); 118 119 HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString()); 120 121 // First set the value. 122 BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader( 123 vm.setFlag(flagName, flagValue))); 124 125 String line; 126 while((line = remoteDataReader.readLine()) != null) { 127 System.out.println("setFlag: " + line); 128 // Just empty the stream. 129 } 130 remoteDataReader.close(); 131 132 // Then read and make sure we get back the set value. 133 remoteDataReader = new BufferedReader(new InputStreamReader(vm.printFlag(flagName))); 134 135 boolean foundExpectedLine = false; 136 line = null; 137 while((line = remoteDataReader.readLine()) != null) { 138 System.out.println("getFlag: " + line); 139 if (line.equals("-XX:" + flagName + "=" + flagValue)) { 140 foundExpectedLine = true; 141 } 142 } 143 144 Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'"); 145 146 vm.detach(); 147 148 } finally { 149 target.destroy(); 150 target.waitFor(); 151 } 152 } 153 154 private static void waitForReady(Process target) throws Exception { 155 InputStream os = target.getInputStream(); 156 try (BufferedReader reader = new BufferedReader(new InputStreamReader(os))) { 157 String line; 158 while ((line = reader.readLine()) != null) { 159 if ("Ready".equals(line)) { 160 return; 161 } 162 } 163 } 164 } 165 166 167 public static class Target { 168 public static void main(String [] args) throws Exception { 169 System.out.println("Ready"); 170 System.out.flush(); 171 while (true) { 172 Thread.sleep(1000); 173 } 174 } 175 } 176 }