File: TestSurvivorRatioFlag.java

package info (click to toggle)
openjdk-11-jre-dcevm 11.0.15%2B1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 221,420 kB
  • sloc: java: 1,363,561; cpp: 967,919; ansic: 69,480; xml: 62,475; sh: 8,106; asm: 3,475; python: 1,667; javascript: 943; makefile: 397; sed: 172; perl: 114
file content (182 lines) | stat: -rw-r--r-- 7,576 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test TestSurvivorRatioFlag
 * @key gc
 * @summary Verify that actual survivor ratio is equal to specified SurvivorRatio value
 * @requires vm.gc != "Z" & vm.gc != "Shenandoah"
 * @library /test/lib
 * @modules java.base/jdk.internal.misc
 *          java.management
 * @build sun.hotspot.WhiteBox
 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 * @run driver TestSurvivorRatioFlag
 */

import java.lang.management.MemoryUsage;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.Utils;
import sun.hotspot.WhiteBox;

public class TestSurvivorRatioFlag {

    public static final long M = 1024 * 1024;
    public static final long HEAP_SIZE = 200 * M;
    public static final long NEW_SIZE = 100 * M;

    public static void main(String args[]) throws Exception {
        LinkedList<String> options = new LinkedList<>(
                Arrays.asList(Utils.getFilteredTestJavaOpts("-XX:[^ ]*SurvivorRatio=[^ ]+"))
        );

        testSurvivorRatio(3, options);
        testSurvivorRatio(6, options);
        testSurvivorRatio(10, options);
        testSurvivorRatio(15, options);
        testSurvivorRatio(20, options);
    }

    /**
     * Verify that actual survivor ratio equal to specified.
     *
     * @param ratio survivor ratio that be verified
     * @param options additional options to JVM
     */
    public static void testSurvivorRatio(int ratio, LinkedList<String> options) throws Exception {

        LinkedList<String> vmOptions = new LinkedList<>(options);

        Collections.addAll(vmOptions,
                "-Xbootclasspath/a:.",
                "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
                "-XX:+UnlockDiagnosticVMOptions",
                "-XX:+WhiteBoxAPI",
                "-XX:GCLockerEdenExpansionPercent=0",
                "-XX:MaxNewSize=" + NEW_SIZE,
                "-XX:NewSize=" + NEW_SIZE,
                "-Xmx" + HEAP_SIZE,
                "-Xms" + HEAP_SIZE,
                "-XX:SurvivorRatio=" + ratio,
                SurvivorRatioVerifier.class.getName(),
                Integer.toString(ratio)
        );

        ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(vmOptions.toArray(new String[vmOptions.size()]));
        OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
        analyzer.shouldHaveExitValue(0);
    }

    /**
     * Class that verifies survivor ratio.
     */
    public static class SurvivorRatioVerifier {

        static WhiteBox wb = WhiteBox.getWhiteBox();

        public static final int MAX_ITERATIONS = 10;
        public static final int ARRAY_LENGTH = 10000;
        public static final int CHUNK_SIZE = 10000;

        public static void main(String args[]) throws Exception {
            if (args.length != 1) {
                throw new IllegalArgumentException("Expected 1 arg: <ratio>");
            }
            final int ratio = Integer.valueOf(args[0]);

            AllocationHelper allocator = new AllocationHelper(MAX_ITERATIONS, ARRAY_LENGTH, CHUNK_SIZE, () -> (verifySurvivorRatio(ratio)));
            allocator.allocateMemoryAndVerify();
        }

        /**
         * Verify that actual survivor ratio is equal to expected.
         * Depending on selected young GC we verify that:
         * - for DefNew and ParNew: eden_size / survivor_size is close to expectedRatio;
         * - for PSNew:             survivor_size equal to young_gen_size / expectedRatio;
         * - for G1:                survivor_regions <= young_list_length / expectedRatio.
         */
        public static Void verifySurvivorRatio(int expectedRatio) {
            GCTypes.YoungGCType type = GCTypes.YoungGCType.getYoungGCType();
            switch (type) {
                case DefNew:
                case ParNew:
                    verifyDefNewSurvivorRatio(expectedRatio);
                    break;
                case PSNew:
                    verifyPSSurvivorRatio(expectedRatio);
                    break;
                case G1:
                    verifyG1SurvivorRatio(expectedRatio);
                    break;
                default:
                    throw new RuntimeException("Unexpected young GC type");
            }
            return null;
        }

        private static void verifyDefNewSurvivorRatio(int expectedRatio) {
            MemoryUsage edenUsage = HeapRegionUsageTool.getEdenUsage();
            MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage();

            int actualRatio = (int) (edenUsage.getCommitted() / survivorUsage.getCommitted());
            if (Math.abs(actualRatio - expectedRatio) > 1) {
                throw new RuntimeException("Expected survivor ratio is: " + expectedRatio
                        + ", but observed ratio is: " + actualRatio);
            }
        }

        private static void verifyPSSurvivorRatio(int expectedRatio) {
            MemoryUsage edenUsage = HeapRegionUsageTool.getEdenUsage();
            MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage();

            long youngGenSize = edenUsage.getMax() + 2 * survivorUsage.getMax();
            // for Paralle GC Min/InitialSurvivorRatio = SurvivorRatio + 2
            long expectedSize = HeapRegionUsageTool.alignDown(youngGenSize / (expectedRatio + 2),
                    wb.psHeapGenerationAlignment());

            if (expectedSize != survivorUsage.getCommitted()) {
                throw new RuntimeException("Expected survivor size is: " + expectedSize
                        + ", but observed size is: " + survivorUsage.getCommitted());
            }
        }

        private static void verifyG1SurvivorRatio(int expectedRatio) {
            MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage();

            int regionSize = wb.g1RegionSize();
            int youngListLength = (int) Math.max(NEW_SIZE / regionSize, 1);
            int expectedSurvivorRegions = (int) Math.ceil(youngListLength / (double) expectedRatio);
            int observedSurvivorRegions = (int) (survivorUsage.getCommitted() / regionSize);

            if (expectedSurvivorRegions < observedSurvivorRegions) {
                throw new RuntimeException("Expected amount of G1 survivor regions is "
                        + expectedSurvivorRegions + ", but observed "
                        + observedSurvivorRegions);
            }
        }
    }
}