TestHumongousAllocInitialMark.java revision 5776:de6a9e811145
1285242Sachim/*
2285242Sachim * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3285242Sachim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4285242Sachim *
5285242Sachim * This code is free software; you can redistribute it and/or modify it
6285242Sachim * under the terms of the GNU General Public License version 2 only, as
7285242Sachim * published by the Free Software Foundation.
8285242Sachim *
9285242Sachim * This code is distributed in the hope that it will be useful, but WITHOUT
10285242Sachim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11285242Sachim * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12285242Sachim * version 2 for more details (a copy is included in the LICENSE file that
13285242Sachim * accompanied this code).
14285242Sachim *
15285242Sachim * You should have received a copy of the GNU General Public License version
16285242Sachim * 2 along with this work; if not, write to the Free Software Foundation,
17285242Sachim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18285242Sachim *
19285242Sachim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20285242Sachim * or visit www.oracle.com if you need additional information or have any
21285242Sachim * questions.
22285242Sachim */
23285242Sachim
24285242Sachim/*
25285242Sachim * @test TestHumongousAllocInitialMark
26285242Sachim * @bug 7168848
27285242Sachim * @summary G1: humongous object allocations should initiate marking cycles when necessary
28285242Sachim * @library /testlibrary
29285242Sachim */
30285242Sachim
31285242Sachimimport com.oracle.java.testlibrary.*;
32285242Sachim
33285242Sachimpublic class TestHumongousAllocInitialMark {
34285242Sachim    private static final int heapSize                       = 200; // MB
35285242Sachim    private static final int heapRegionSize                 = 1;   // MB
36285242Sachim    private static final int initiatingHeapOccupancyPercent = 50;  // %
37285242Sachim
38285242Sachim    public static void main(String[] args) throws Exception {
39285242Sachim        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
40285242Sachim            "-XX:+UseG1GC",
41285242Sachim            "-Xms" + heapSize + "m",
42285242Sachim            "-Xmx" + heapSize + "m",
43285242Sachim            "-XX:G1HeapRegionSize=" + heapRegionSize + "m",
44285242Sachim            "-XX:InitiatingHeapOccupancyPercent=" + initiatingHeapOccupancyPercent,
45285242Sachim            "-XX:+PrintGC",
46285242Sachim            HumongousObjectAllocator.class.getName());
47285242Sachim
48285242Sachim        OutputAnalyzer output = new OutputAnalyzer(pb.start());
49285242Sachim        output.shouldContain("GC pause (G1 Humongous Allocation) (young) (initial-mark)");
50285242Sachim        output.shouldNotContain("Full GC");
51285242Sachim        output.shouldHaveExitValue(0);
52285242Sachim    }
53285242Sachim
54285242Sachim    static class HumongousObjectAllocator {
55285242Sachim        private static byte[] dummy;
56285242Sachim
57285242Sachim        public static void main(String [] args) throws Exception {
58285242Sachim            // Make object size 75% of region size
59285242Sachim            final int humongousObjectSize =
60285242Sachim                (int)(heapRegionSize * 1024 * 1024 * 0.75);
61285242Sachim
62285242Sachim            // Number of objects to allocate to go above IHOP
63285242Sachim            final int humongousObjectAllocations =
64285242Sachim                (int)((heapSize * initiatingHeapOccupancyPercent / 100.0) / heapRegionSize) + 1;
65285242Sachim
66285242Sachim            // Allocate
67285242Sachim            for (int i = 1; i <= humongousObjectAllocations; i++) {
68285242Sachim                System.out.println("Allocating humongous object " + i + "/" + humongousObjectAllocations +
69285242Sachim                                   " of size " + humongousObjectSize + " bytes");
70285242Sachim                dummy = new byte[humongousObjectSize];
71285242Sachim            }
72285242Sachim        }
73285242Sachim    }
74285242Sachim}
75285242Sachim
76285242Sachim