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