1 /* 2 * Copyright (c) 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 TestLogging 26 * @summary Check that a mixed GC is reflected in the gc logs 27 * @requires vm.gc=="G1" | vm.gc=="null" 28 * @library /testlibrary /test/lib 29 * @modules java.management 30 * @build sun.hotspot.WhiteBox gc.g1.mixedgc.TestLogging 31 * @run main ClassFileInstaller sun.hotspot.WhiteBox 32 * @run driver gc.g1.mixedgc.TestLogging 33 */ 34 35 package gc.g1.mixedgc; 36 37 import jdk.test.lib.OutputAnalyzer; 38 import jdk.test.lib.ProcessTools; 39 import jdk.test.lib.Asserts; 40 import sun.hotspot.WhiteBox; 41 42 import java.util.ArrayList; 43 import java.util.List; 44 import java.util.Collections; 45 46 /** 47 * Test spawns MixedGCProvoker in a separate VM and expects to find a message 48 * telling that a mixed gc has happened 49 */ 50 public class TestLogging { 51 private static final String[] COMMON_OPTIONS = new String[]{ 52 "-Xbootclasspath/a:.", "-XX:+UseG1GC", 53 "-XX:+UnlockExperimentalVMOptions", 54 "-XX:+UnlockDiagnosticVMOptions", 55 "-XX:+WhiteBoxAPI", 56 "-XX:SurvivorRatio=1", // Survivor-to-eden ratio is 1:1 57 "-Xms10M", "-Xmx10M", 58 "-XX:MaxTenuringThreshold=1", // promote objects after first gc 59 "-XX:InitiatingHeapOccupancyPercent=0", // marking cycle happens 60 // each time 61 "-XX:G1MixedGCCountTarget=4", 62 "-XX:MaxGCPauseMillis=30000", // to have enough time 63 "-XX:G1HeapRegionSize=1m", "-XX:G1HeapWastePercent=0", 64 "-XX:G1MixedGCLiveThresholdPercent=100"}; 65 66 public static final int ALLOCATION_SIZE = 20000; 67 public static final int ALLOCATION_COUNT = 15; 68 69 public static void main(String args[]) throws Exception { 70 // Test turns logging on by giving -Xlog:gc flag 71 test("-Xlog:gc"); 72 // Test turns logging on by giving -Xlog:gc=debug flag 73 test("-Xlog:gc=debug"); 74 } 75 76 private static void test(String vmFlag) throws Exception { 77 System.out.println(String.format("%s: running with %s flag", TestLogging.class.getSimpleName(), vmFlag)); 78 OutputAnalyzer output = spawnMixedGCProvoker(vmFlag); 79 System.out.println(output.getStdout()); 80 output.shouldHaveExitValue(0); 81 output.shouldContain("Pause Mixed (G1 Evacuation Pause)"); 82 } 83 84 /** 85 * Method spawns MixedGCProvoker with addition flags set 86 * 87 * @parameter extraFlags -flags to be added to the common options set 88 */ 89 private static OutputAnalyzer spawnMixedGCProvoker(String... extraFlags) 90 throws Exception { 91 List<String> testOpts = new ArrayList<>(); 92 Collections.addAll(testOpts, COMMON_OPTIONS); 93 Collections.addAll(testOpts, extraFlags); 94 testOpts.add(MixedGCProvoker.class.getName()); 95 System.out.println(testOpts); 96 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(false, 97 testOpts.toArray(new String[testOpts.size()])); 98 return new OutputAnalyzer(pb.start()); 99 } 100 } 101 102 /** 103 * Utility class to guarantee a mixed GC. The class allocates several arrays and 104 * promotes them to the oldgen. After that it tries to provoke mixed GC by 105 * allocating new objects. 106 * 107 * The necessary condition for guaranteed mixed GC is running MixedGCProvoker is 108 * running in VM with the following flags: -XX:MaxTenuringThreshold=1, -Xms10M, 109 * -Xmx10M, -XX:G1MixedGCLiveThresholdPercent=100, -XX:G1HeapWastePercent=0, 110 * -XX:G1HeapRegionSize=1m 111 */ 112 class MixedGCProvoker { 113 private static final WhiteBox WB = WhiteBox.getWhiteBox(); 114 private static final List<byte[]> liveOldObjects = new ArrayList<>(); 115 private static final List<byte[]> newObjects = new ArrayList<>(); 116 117 private static void allocateOldObjects() throws Exception { 118 List<byte[]> deadOldObjects = new ArrayList<>(); 119 // Allocates buffer and promotes it to the old gen. Mix live and dead old 120 // objects 121 for (int i = 0; i < TestLogging.ALLOCATION_COUNT; ++i) { 122 liveOldObjects.add(new byte[TestLogging.ALLOCATION_SIZE * 10]); 123 deadOldObjects.add(new byte[TestLogging.ALLOCATION_SIZE * 10]); 124 } 125 126 // need only 2 promotions to promote objects to the old gen 127 WB.youngGC(); 128 WB.youngGC(); 129 // check it is promoted & keep alive 130 Asserts.assertTrue(WB.isObjectInOldGen(liveOldObjects), 131 "List of the objects is suppose to be in OldGen"); 132 Asserts.assertTrue(WB.isObjectInOldGen(deadOldObjects), 133 "List of the objects is suppose to be in OldGen"); 134 } 135 136 137 /** 138 * Waits until Concurent Mark Cycle finishes 139 * @param wb Whitebox instance 140 * @param sleepTime sleep time 141 */ 142 public static void waitTillCMCFinished(WhiteBox wb, int sleepTime) { 143 while (wb.g1InConcurrentMark()) { 144 if (sleepTime > -1) { 145 try { 146 Thread.sleep(sleepTime); 147 } catch (InterruptedException e) { 148 System.out.println("Got InterruptedException while waiting for ConcMarkCycle to finish"); 149 } 150 } 151 } 152 } 153 154 155 156 public static void main(String args[]) throws Exception { 157 // allocate old objects 158 allocateOldObjects(); 159 waitTillCMCFinished(WB, 0); 160 WB.g1StartConcMarkCycle(); 161 waitTillCMCFinished(WB, 0); 162 163 WB.youngGC(); 164 System.out.println("Allocating new objects to provoke mixed GC"); 165 // allocate more objects to provoke GC 166 for (int i = 0; i < (TestLogging.ALLOCATION_COUNT * 20); i++) { 167 newObjects.add(new byte[TestLogging.ALLOCATION_SIZE]); 168 } 169 // check that liveOldObjects still alive 170 Asserts.assertTrue(WB.isObjectInOldGen(liveOldObjects), 171 "List of the objects is suppose to be in OldGen"); 172 } 173 }