Path: blob/master/test/hotspot/jtreg/serviceability/attach/AttachSetGetFlag.java
41149 views
/*1* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 805482326* @summary Tests the setFlag and printFlag attach command27* @library /test/lib28* @modules java.base/jdk.internal.misc29* java.compiler30* java.management31* jdk.attach/sun.tools.attach32* jdk.internal.jvmstat/sun.jvmstat.monitor33* @run main AttachSetGetFlag34*/3536import java.io.BufferedReader;37import java.io.InputStreamReader;38import java.io.InputStream;39import java.lang.reflect.Field;40import java.nio.file.Files;41import java.nio.file.Path;42import java.nio.file.Paths;4344import sun.tools.attach.HotSpotVirtualMachine;4546import jdk.test.lib.Asserts;47import jdk.test.lib.Platform;48import jdk.test.lib.process.ProcessTools;49import com.sun.tools.attach.VirtualMachine;5051public class AttachSetGetFlag {5253public static void main(String... args) throws Exception {54// Test a manageable uintx flag.55testGetFlag("MaxHeapFreeRatio", "60");56testSetFlag("MaxHeapFreeRatio", "50", "60");5758// Test a non-manageable size_t flag.59// Since it is not manageable, we can't test the setFlag functionality.60testGetFlag("ArrayAllocatorMallocLimit", "128");61// testSetFlag("ArrayAllocatorMallocLimit", "64", "128");6263// Test a uint flag.64testGetFlag("ParallelGCThreads", "10");65}6667public static ProcessBuilder runTarget(String flagName, String flagValue) throws Exception {68return ProcessTools.createJavaProcessBuilder(69"-XX:+UnlockExperimentalVMOptions",70"-XX:" + flagName + "=" + flagValue,71"AttachSetGetFlag$Target");72}7374public static void testGetFlag(String flagName, String flagValue) throws Exception {75ProcessBuilder pb = runTarget(flagName, flagValue);7677Process target = pb.start();7879try {80waitForReady(target);8182int pid = (int)target.pid();8384HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());8586// Test Get87BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(88vm.printFlag(flagName)));8990boolean foundExpectedLine = false;9192String line = null;93while((line = remoteDataReader.readLine()) != null) {94System.out.println("printFlag: " + line);95if (line.equals("-XX:" + flagName + "=" + flagValue)) {96foundExpectedLine = true;97}98}99100Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'");101102vm.detach();103}104finally {105target.destroy();106target.waitFor();107}108}109110public static void testSetFlag(String flagName, String initialFlagValue, String flagValue) throws Exception {111ProcessBuilder pb = runTarget(flagName, initialFlagValue);112113Process target = pb.start();114115try {116waitForReady(target);117118int pid = (int)target.pid();119120HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());121122// First set the value.123BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(124vm.setFlag(flagName, flagValue)));125126String line;127while((line = remoteDataReader.readLine()) != null) {128System.out.println("setFlag: " + line);129// Just empty the stream.130}131remoteDataReader.close();132133// Then read and make sure we get back the set value.134remoteDataReader = new BufferedReader(new InputStreamReader(vm.printFlag(flagName)));135136boolean foundExpectedLine = false;137line = null;138while((line = remoteDataReader.readLine()) != null) {139System.out.println("getFlag: " + line);140if (line.equals("-XX:" + flagName + "=" + flagValue)) {141foundExpectedLine = true;142}143}144145Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'");146147vm.detach();148149} finally {150target.destroy();151target.waitFor();152}153}154155private static void waitForReady(Process target) throws Exception {156InputStream os = target.getInputStream();157try (BufferedReader reader = new BufferedReader(new InputStreamReader(os))) {158String line;159while ((line = reader.readLine()) != null) {160if ("Ready".equals(line)) {161return;162}163}164}165}166167168public static class Target {169public static void main(String [] args) throws Exception {170System.out.println("Ready");171System.out.flush();172while (true) {173Thread.sleep(1000);174}175}176}177}178179180