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