Path: blob/master/test/hotspot/jtreg/serviceability/attach/AttachSetGetFlag.java
64476 views
/*1* Copyright (c) 2014, 2021, 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* @requires vm.flagless28* @library /test/lib29* @modules java.base/jdk.internal.misc30* java.compiler31* java.management32* jdk.attach/sun.tools.attach33* jdk.internal.jvmstat/sun.jvmstat.monitor34* @run driver AttachSetGetFlag35*/3637import 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;4445import sun.tools.attach.HotSpotVirtualMachine;4647import jdk.test.lib.Asserts;48import jdk.test.lib.Platform;49import jdk.test.lib.process.ProcessTools;50import com.sun.tools.attach.VirtualMachine;5152public class AttachSetGetFlag {5354public static void main(String... args) throws Exception {55// Test a manageable uintx flag.56testGetFlag("MaxHeapFreeRatio", "60");57testSetFlag("MaxHeapFreeRatio", "50", "60");5859// Test a non-manageable size_t flag.60// Since it is not manageable, we can't test the setFlag functionality.61testGetFlag("ArrayAllocatorMallocLimit", "128");62// testSetFlag("ArrayAllocatorMallocLimit", "64", "128");6364// Test a uint flag.65testGetFlag("ParallelGCThreads", "10");66}6768public static ProcessBuilder runTarget(String flagName, String flagValue) throws Exception {69return ProcessTools.createJavaProcessBuilder(70"-XX:+UnlockExperimentalVMOptions",71"-XX:" + flagName + "=" + flagValue,72"AttachSetGetFlag$Target");73}7475public static void testGetFlag(String flagName, String flagValue) throws Exception {76ProcessBuilder pb = runTarget(flagName, flagValue);7778Process target = pb.start();7980try {81waitForReady(target);8283int pid = (int)target.pid();8485HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());8687// Test Get88BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(89vm.printFlag(flagName)));9091boolean foundExpectedLine = false;9293String line = null;94while((line = remoteDataReader.readLine()) != null) {95System.out.println("printFlag: " + line);96if (line.equals("-XX:" + flagName + "=" + flagValue)) {97foundExpectedLine = true;98}99}100101Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'");102103vm.detach();104}105finally {106target.destroy();107target.waitFor();108}109}110111public static void testSetFlag(String flagName, String initialFlagValue, String flagValue) throws Exception {112ProcessBuilder pb = runTarget(flagName, initialFlagValue);113114Process target = pb.start();115116try {117waitForReady(target);118119int pid = (int)target.pid();120121HotSpotVirtualMachine vm = (HotSpotVirtualMachine)VirtualMachine.attach(((Integer)pid).toString());122123// First set the value.124BufferedReader remoteDataReader = new BufferedReader(new InputStreamReader(125vm.setFlag(flagName, flagValue)));126127String line;128while((line = remoteDataReader.readLine()) != null) {129System.out.println("setFlag: " + line);130// Just empty the stream.131}132remoteDataReader.close();133134// Then read and make sure we get back the set value.135remoteDataReader = new BufferedReader(new InputStreamReader(vm.printFlag(flagName)));136137boolean foundExpectedLine = false;138line = null;139while((line = remoteDataReader.readLine()) != null) {140System.out.println("getFlag: " + line);141if (line.equals("-XX:" + flagName + "=" + flagValue)) {142foundExpectedLine = true;143}144}145146Asserts.assertTrue(foundExpectedLine, "Didn't get the expected output: '-XX:" + flagName + "=" + flagValue + "'");147148vm.detach();149150} finally {151target.destroy();152target.waitFor();153}154}155156private static void waitForReady(Process target) throws Exception {157InputStream os = target.getInputStream();158try (BufferedReader reader = new BufferedReader(new InputStreamReader(os))) {159String line;160while ((line = reader.readLine()) != null) {161if ("Ready".equals(line)) {162return;163}164}165}166}167168169public static class Target {170public static void main(String [] args) throws Exception {171System.out.println("Ready");172System.out.flush();173while (true) {174Thread.sleep(1000);175}176}177}178}179180181