Path: blob/master/test/hotspot/jtreg/gc/logging/TestGCId.java
40942 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*/2223package gc.logging;2425/*26* @test TestGCId27* @bug 804360728* @summary Ensure that the GCId is logged29* @library /test/lib30* @modules java.base/jdk.internal.misc31* java.management32* @build sun.hotspot.WhiteBox33* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox34* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI gc.logging.TestGCId35*/3637import jdk.test.lib.process.OutputAnalyzer;38import jdk.test.lib.process.ProcessTools;39import jtreg.SkippedException;40import sun.hotspot.gc.GC;4142public class TestGCId {43public static void main(String[] args) throws Exception {44boolean noneGCSupported = true;4546if (GC.Parallel.isSupported()) {47noneGCSupported = false;48testGCId("UseParallelGC");49}50if (GC.G1.isSupported()) {51noneGCSupported = false;52testGCId("UseG1GC");53}54if (GC.Serial.isSupported()) {55noneGCSupported = false;56testGCId("UseSerialGC");57}58if (GC.Shenandoah.isSupported()) {59noneGCSupported = false;60testGCId("UseShenandoahGC");61}6263if (noneGCSupported) {64throw new SkippedException("Skipping test because none of Parallel/G1/Serial/Shenandoah is supported.");65}66}6768private static void verifyContainsGCIDs(OutputAnalyzer output) {69output.shouldMatch("\\[.*\\]\\[.*\\]\\[.*\\] GC\\(0\\) ");70output.shouldMatch("\\[.*\\]\\[.*\\]\\[.*\\] GC\\(1\\) ");71output.shouldHaveExitValue(0);72}7374private static void testGCId(String gcFlag) throws Exception {75ProcessBuilder pb_default =76ProcessTools.createJavaProcessBuilder("-XX:+UnlockExperimentalVMOptions", "-XX:+" + gcFlag, "-Xlog:gc", "-Xmx10M", GCTest.class.getName());77verifyContainsGCIDs(new OutputAnalyzer(pb_default.start()));78}7980static class GCTest {81private static byte[] garbage;82public static void main(String [] args) {83System.out.println("Creating garbage");84// create 128MB of garbage. This should result in at least one GC85for (int i = 0; i < 1024; i++) {86garbage = new byte[128 * 1024];87}88// do a system gc to get one more gc89System.gc();90System.out.println("Done");91}92}93}949596