Path: blob/master/test/jdk/sun/tools/jmap/BasicJMapTest.java
41149 views
/*1* Copyright (c) 2005, 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*/2223import static jdk.test.lib.Asserts.assertTrue;24import static jdk.test.lib.Asserts.assertFalse;25import static jdk.test.lib.Asserts.fail;2627import java.io.File;28import java.nio.file.Files;29import java.util.Arrays;30import java.util.List;3132import jdk.test.lib.JDKToolLauncher;33import jdk.test.lib.Utils;34import jdk.test.lib.hprof.HprofParser;35import jdk.test.lib.process.OutputAnalyzer;36import jdk.test.lib.process.ProcessTools;3738/*39* @test id=Serial40* @requires vm.gc.Serial41* @summary Unit test for jmap utility (Serial GC)42* @key intermittent43* @library /test/lib44* @build jdk.test.lib.hprof.*45* @build jdk.test.lib.hprof.model.*46* @build jdk.test.lib.hprof.parser.*47* @build jdk.test.lib.hprof.util.*48* @run main/othervm/timeout=240 -XX:+UseSerialGC BasicJMapTest49*/5051/*52* @test id=Parallel53* @requires vm.gc.Parallel54* @summary Unit test for jmap utility (Parallel GC)55* @key intermittent56* @library /test/lib57* @build jdk.test.lib.hprof.*58* @build jdk.test.lib.hprof.model.*59* @build jdk.test.lib.hprof.parser.*60* @build jdk.test.lib.hprof.util.*61* @run main/othervm/timeout=240 -XX:+UseParallelGC BasicJMapTest62*/6364/*65* @test id=G166* @requires vm.gc.G167* @summary Unit test for jmap utility (G1 GC)68* @key intermittent69* @library /test/lib70* @build jdk.test.lib.hprof.*71* @build jdk.test.lib.hprof.model.*72* @build jdk.test.lib.hprof.parser.*73* @build jdk.test.lib.hprof.util.*74* @run main/othervm/timeout=240 -XX:+UseG1GC BasicJMapTest75*/7677/*78* @test id=Shenandoah79* @requires vm.gc.Shenandoah80* @summary Unit test for jmap utility (Shenandoah GC)81* @key intermittent82* @library /test/lib83* @build jdk.test.lib.hprof.*84* @build jdk.test.lib.hprof.model.*85* @build jdk.test.lib.hprof.parser.*86* @build jdk.test.lib.hprof.util.*87* @run main/othervm/timeout=240 -XX:+UseShenandoahGC BasicJMapTest88*/8990/*91* @test id=Z92* @requires vm.gc.Z93* @summary Unit test for jmap utility (Z GC)94* @key intermittent95* @library /test/lib96* @build jdk.test.lib.hprof.*97* @build jdk.test.lib.hprof.model.*98* @build jdk.test.lib.hprof.parser.*99* @build jdk.test.lib.hprof.util.*100* @run main/othervm/timeout=240 -XX:+UseZGC BasicJMapTest101*/102103public class BasicJMapTest {104105private static ProcessBuilder processBuilder = new ProcessBuilder();106107public static void main(String[] args) throws Exception {108testHisto();109testHistoLive();110testHistoAll();111testHistoParallelZero();112testHistoParallel();113testHistoNonParallel();114testHistoToFile();115testHistoLiveToFile();116testHistoAllToFile();117testFinalizerInfo();118testClstats();119testDump();120testDumpLive();121testDumpAll();122testDumpCompressed();123testDumpIllegalCompressedArgs();124}125126private static void testHisto() throws Exception {127OutputAnalyzer output = jmap("-histo:");128output.shouldHaveExitValue(0);129OutputAnalyzer output1 = jmap("-histo");130output1.shouldHaveExitValue(0);131}132133private static void testHistoLive() throws Exception {134OutputAnalyzer output = jmap("-histo:live");135output.shouldHaveExitValue(0);136}137138private static void testHistoAll() throws Exception {139OutputAnalyzer output = jmap("-histo:all");140output.shouldHaveExitValue(0);141}142143private static void testHistoParallelZero() throws Exception {144OutputAnalyzer output = jmap("-histo:parallel=0");145output.shouldHaveExitValue(0);146}147148private static void testHistoParallel() throws Exception {149OutputAnalyzer output = jmap("-histo:parallel=2");150output.shouldHaveExitValue(0);151}152153private static void testHistoNonParallel() throws Exception {154OutputAnalyzer output = jmap("-histo:parallel=1");155output.shouldHaveExitValue(0);156}157158private static void testHistoToFile() throws Exception {159histoToFile(false, false, 1);160}161162private static void testHistoLiveToFile() throws Exception {163histoToFile(true, false, 1);164}165166private static void testHistoAllToFile() throws Exception {167histoToFile(false, true, 1);168}169170private static void testHistoFileParallelZero() throws Exception {171histoToFile(false, false, 0);172}173174private static void testHistoFileParallel() throws Exception {175histoToFile(false, false, 2);176}177178private static void histoToFile(boolean live,179boolean explicitAll,180int parallelThreadNum) throws Exception {181String liveArg = "";182String fileArg = "";183String parArg = "parallel=" + parallelThreadNum;184String allArgs = "-histo:";185186if (live && explicitAll) {187fail("Illegal argument setting for jmap -histo");188}189if (live) {190liveArg = "live,";191}192if (explicitAll) {193liveArg = "all,";194}195196File file = new File("jmap.histo.file" + System.currentTimeMillis() + ".histo");197if (file.exists()) {198file.delete();199}200fileArg = "file=" + file.getName();201202OutputAnalyzer output;203allArgs = allArgs + liveArg + fileArg + ',' + parArg;204output = jmap(allArgs);205output.shouldHaveExitValue(0);206output.shouldContain("Heap inspection file created");207file.delete();208}209210private static void testFinalizerInfo() throws Exception {211OutputAnalyzer output = jmap("-finalizerinfo");212output.shouldHaveExitValue(0);213}214215private static void testClstats() throws Exception {216OutputAnalyzer output = jmap("-clstats");217output.shouldHaveExitValue(0);218}219220private static void testDump() throws Exception {221dump(false, false, false);222}223224private static void testDumpLive() throws Exception {225dump(true, false, false);226}227228private static void testDumpAll() throws Exception {229dump(false, true, false);230}231232private static void testDumpCompressed() throws Exception {233dump(true, false, true);234}235236private static void testDumpIllegalCompressedArgs() throws Exception{237dump(true, false, true, "0", 1, "Compression level out of range");238dump(true, false, true, "100", 1, "Compression level out of range");239dump(true, false, true, "abc", 1, "Invalid compress level");240dump(true, false, true, "", 1, "Fail: no number provided in option:");241}242243private static void dump(boolean live, boolean explicitAll, boolean compressed) throws Exception {244dump(live, explicitAll, compressed, "1", 0, "Heap dump file created");245}246247private static void dump(boolean live,248boolean explicitAll,249boolean compressed,250String compressLevel,251int expExitValue,252String expOutput) throws Exception {253String liveArg = "";254String fileArg = "";255String compressArg = "";256String allArgs = "-dump:";257258if (live && explicitAll) {259fail("Illegal argument setting for jmap -dump");260}261if (live) {262liveArg = "live,";263}264if (explicitAll) {265liveArg = "all,";266}267268String filePath = "jmap.dump" + System.currentTimeMillis() + ".hprof";269if (compressed) {270compressArg = "gz=" + compressLevel;271filePath = filePath + ".gz";272}273274File file = new File(filePath);275if (file.exists()) {276file.delete();277}278fileArg = "file=" + file.getName() + ",";279280OutputAnalyzer output;281allArgs = allArgs + liveArg + "format=b," + fileArg + compressArg;282output = jmap(allArgs);283output.shouldHaveExitValue(expExitValue);284output.shouldContain(expOutput);285if (expExitValue == 0) {286verifyDumpFile(file);287}288file.delete();289}290291private static void verifyDumpFile(File dump) {292assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());293try {294File out = HprofParser.parse(dump);295296assertTrue(out != null && out.exists() && out.isFile(),297"Could not find hprof parser output file");298List<String> lines = Files.readAllLines(out.toPath());299assertTrue(lines.size() > 0, "hprof parser output file is empty");300for (String line : lines) {301assertFalse(line.matches(".*WARNING(?!.*Failed to resolve " +302"object.*constantPoolOop.*).*"));303}304305out.delete();306} catch (Exception e) {307e.printStackTrace();308fail("Could not parse dump file " + dump.getAbsolutePath());309}310}311312private static OutputAnalyzer jmap(String... toolArgs) throws Exception {313JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");314launcher.addVMArgs(Utils.getTestJavaOpts());315if (toolArgs != null) {316for (String toolArg : toolArgs) {317launcher.addToolArg(toolArg);318}319}320launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));321322processBuilder.command(launcher.getCommand());323System.out.println(Arrays.toString(processBuilder.command().toArray()));324OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);325System.out.println(output.getOutput());326327return output;328}329}330331332