1 /*
   2  * Copyright (c) 2014, 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 TestG1TraceReclaimDeadHumongousObjectsAtYoungGC
  26  * @bug 8058801
  27  * @summary Ensure that the output for a G1TraceReclaimDeadHumongousObjectsAtYoungGC
  28  * includes the expected necessary messages.
  29  * @key gc
  30  * @library /testlibrary
  31  */
  32 
  33 import com.oracle.java.testlibrary.ProcessTools;
  34 import com.oracle.java.testlibrary.OutputAnalyzer;
  35 import java.util.LinkedList;
  36 
  37 public class TestG1TraceReclaimDeadHumongousObjectsAtYoungGC {
  38   public static void main(String[] args) throws Exception {
  39     testGCLogs();
  40     testHumongousObjectGCLogs();
  41   }
  42 
  43   private static void testGCLogs() throws Exception {
  44 
  45     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
  46                                                "-Xms128M",
  47                                                "-Xmx128M",
  48                                                "-Xmn16M",
  49                                                "-XX:G1HeapRegionSize=1M",
  50                                                "-XX:+PrintGC",
  51                                                "-XX:+UnlockExperimentalVMOptions",
  52                                                "-XX:G1LogLevel=finest",
  53                                                "-XX:+G1TraceReclaimDeadHumongousObjectsAtYoungGC",
  54                                                GCTest.class.getName());
  55 
  56     OutputAnalyzer output = new OutputAnalyzer(pb.start());
  57 
  58     // As G1ReclaimDeadHumongousObjectsAtYoungGC is set(default), below logs should be displayed.
  59     // And GCTest doesn't have humongous objects, so values should be zero.
  60     output.shouldContain("[Humongous Reclaim");
  61     output.shouldContain("[Humongous Total: 0]");
  62     output.shouldContain("[Humongous Candidate: 0]");
  63     output.shouldContain("[Humongous Reclaimed: 0]");
  64 
  65     output.shouldHaveExitValue(0);
  66   }
  67 
  68   private static void testHumongousObjectGCLogs() throws Exception {
  69     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
  70                                                "-Xms128M",
  71                                                "-Xmx128M",
  72                                                "-Xmn16M",
  73                                                "-XX:G1HeapRegionSize=1M",
  74                                                "-XX:+PrintGC",
  75                                                "-XX:+UnlockExperimentalVMOptions",
  76                                                "-XX:G1LogLevel=finest",
  77                                                "-XX:+G1TraceReclaimDeadHumongousObjectsAtYoungGC",
  78                                                GCWithHumongousObjectTest.class.getName());
  79 
  80     OutputAnalyzer output = new OutputAnalyzer(pb.start());
  81 
  82     // As G1ReclaimDeadHumongousObjectsAtYoungGC is set(default), below logs should be displayed.
  83     output.shouldContain("[Humongous Reclaim");
  84     output.shouldContain("[Humongous Total");
  85     output.shouldContain("[Humongous Candidate");
  86     output.shouldContain("[Humongous Reclaimed");
  87 
  88     // As G1TraceReclaimDeadHumongousObjectsAtYoungGC is set and GCWithHumongousObjectTest has humongous objects,
  89     // these logs should be displayed.
  90     output.shouldContain("Live humongous");
  91     output.shouldContain("Reclaim humongous region");
  92     output.shouldHaveExitValue(0);
  93   }
  94 
  95   static class GCTest {
  96     private static byte[] garbage;
  97 
  98     public static void main(String [] args) {
  99       System.out.println("Creating garbage");
 100       // create 128MB of garbage. This should result in at least one GC
 101       for (int i = 0; i < 1024; i++) {
 102         garbage = new byte[128 * 1024];
 103       }
 104       System.out.println("Done");
 105     }
 106   }
 107 
 108   static class GCWithHumongousObjectTest {
 109 
 110     public static final int M = 1024*1024;
 111     public static LinkedList<Object> garbageList = new LinkedList<Object>();
 112     // A large object referenced by a static.
 113     static int[] filler = new int[10 * M];
 114 
 115     public static void genGarbage() {
 116       for (int i = 0; i < 32*1024; i++) {
 117         garbageList.add(new int[100]);
 118       }
 119       garbageList.clear();
 120     }
 121 
 122     public static void main(String[] args) {
 123 
 124       int[] large = new int[M];
 125       Object ref = large;
 126 
 127       System.out.println("Creating garbage");
 128       for (int i = 0; i < 100; i++) {
 129         // A large object that will be reclaimed eagerly.
 130         large = new int[6*M];
 131         genGarbage();
 132         // Make sure that the compiler cannot completely remove
 133         // the allocation of the large object until here.
 134         System.out.println(large);
 135       }
 136 
 137       // Keep the reference to the first object alive.
 138       System.out.println(ref);
 139       System.out.println("Done");
 140     }
 141   }
 142 }