File: TestNewSizeFlags.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 (326 lines) | stat: -rw-r--r-- 14,619 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * 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 TestNewSizeFlags
 * @key gc
 * @bug 8025166
 * @summary Verify that young gen size conforms values specified by NewSize, MaxNewSize and Xmn options
 * @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/timeout=240  TestNewSizeFlags
 */

import java.io.IOException;
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 TestNewSizeFlags {

    public static final long M = 1024 * 1024;

    public static void main(String args[]) throws Exception {
        LinkedList<String> options = new LinkedList<>(
                Arrays.asList(Utils.getFilteredTestJavaOpts("(-Xm[nsx][^ ]+)|"
                                + "(-XX:(Max)?((New)|"
                                + "(Heap))((Size)|"
                                + "(Ratio))=[^ ]+)"))
        );

        // Test NewSize and MaxNewSize
        testNewSizeFlags(20 * M, 10 * M, 30 * M, 40 * M, options, false);
        testNewSizeFlags(10 * M, 20 * M, 30 * M, 80 * M, options, false);
        testNewSizeFlags(-1, 20 * M, 30 * M, 40 * M, options, false);
        testNewSizeFlags(10 * M, -1, 30 * M, 40 * M, options, false);
        testNewSizeFlags(20 * M, 20 * M, 30 * M, 40 * M, options, false);
        testNewSizeFlags(20 * M, 30 * M, 40 * M, 50 * M, options, false);
        testNewSizeFlags(30 * M, 100 * M, 150 * M, 200 * M, options, false);
        testNewSizeFlags(20 * M, 30 * M, 128 * M, 128 * M, options, false);

        // Test -Xmn
        testXmnFlags(0, 30 * M, 40 * M, options, true);
        testXmnFlags(20 * M, 30 * M, 40 * M, options, false);
        testXmnFlags(50 * M, 70 * M, 100 * M, options, false);
    }

    /**
     * Verify that NewSize and MaxNewSize flags affect young gen size.
     *
     * @param newSize value of NewSize option, omitted if negative
     * @param maxNewSize value of MaxNewSize option, omitted if negative
     * @param heapSize value of HeapSize option
     * @param maxHeapSize value of MaxHeapSize option
     * @param options additional options for JVM
     * @param failureExpected true if JVM should fail with passed heap size options
     */
    public static void testNewSizeFlags(long newSize, long maxNewSize,
            long heapSize, long maxHeapSize,
            LinkedList<String> options,
            boolean failureExpected) throws Exception {
        long expectedNewSize = newSize;
        long expectedMaxNewSize = (maxNewSize >= 0 ? Math.max(maxNewSize, newSize) : maxNewSize);
        testVMOptions(newSize, maxNewSize,
                heapSize, maxHeapSize,
                expectedNewSize, expectedMaxNewSize,
                options, failureExpected);
    }

    /**
     * Verify that -Xmn flag affect young gen size.
     *
     * @param mnValue value of -Xmn option
     * @param heapSize value of HeapSize option
     * @param maxHeapSize value of MaxHeapSize option
     * @param options additional options for JVM
     * @param failureExpected true if JVM should fail with passed heap size options
     */
    public static void testXmnFlags(long mnValue,
            long heapSize, long maxHeapSize,
            LinkedList<String> options,
            boolean failureExpected) throws Exception {
        LinkedList<String> newOptions = new LinkedList<>(options);
        newOptions.add("-Xmn" + mnValue);
        testVMOptions(-1, -1,
                heapSize, maxHeapSize,
                mnValue, mnValue,
                newOptions, failureExpected);
    }

    /**
     * Verify that NewSize and MaxNewSize flags affect young gen size.
     *
     * @param newSize value of NewSize option, omitted if negative
     * @param maxNewSize value of MaxNewSize option, omitted if negative
     * @param heapSize value of HeapSize option
     * @param maxHeapSize value of MaxHeapSize option
     * @param expectedNewSize expected initial young gen size
     * @param expectedMaxNewSize expected max young gen size
     * @param options additional options for JVM
     * @param failureExpected true if JVM should fail with passed heap size options
     */
    public static void testVMOptions(long newSize, long maxNewSize,
            long heapSize, long maxHeapSize,
            long expectedNewSize, long expectedMaxNewSize,
            LinkedList<String> options, boolean failureExpected) throws Exception {
        OutputAnalyzer analyzer = startVM(options, newSize, maxNewSize, heapSize, maxHeapSize, expectedNewSize, expectedMaxNewSize);

        if (failureExpected) {
            analyzer.shouldHaveExitValue(1);
            analyzer.shouldMatch("(Error occurred during initialization of VM)|"
                    + "(Error: Could not create the Java Virtual Machine.)");
        } else {
            analyzer.shouldHaveExitValue(0);
        }
    }

    private static OutputAnalyzer startVM(LinkedList<String> options,
            long newSize, long maxNewSize,
            long heapSize, long maxHeapSize,
            long expectedNewSize, long expectedMaxNewSize) throws Exception, IOException {
        LinkedList<String> vmOptions = new LinkedList<>(options);
        Collections.addAll(vmOptions,
                "-Xbootclasspath/a:.",
                "-XX:+UnlockDiagnosticVMOptions",
                "-XX:+WhiteBoxAPI",
                (newSize >= 0 ? "-XX:NewSize=" + newSize : ""),
                (maxNewSize >= 0 ? "-XX:MaxNewSize=" + maxNewSize : ""),
                "-Xmx" + maxHeapSize,
                "-Xms" + heapSize,
                "-XX:GCLockerEdenExpansionPercent=0",
                "-XX:-UseLargePages",
                NewSizeVerifier.class.getName(),
                Long.toString(expectedNewSize),
                Long.toString(expectedMaxNewSize),
                Long.toString(heapSize),
                Long.toString(maxHeapSize)
        );
        vmOptions.removeIf(String::isEmpty);
        ProcessBuilder procBuilder = ProcessTools.createJavaProcessBuilder(vmOptions.toArray(new String[vmOptions.size()]));
        OutputAnalyzer analyzer = new OutputAnalyzer(procBuilder.start());
        return analyzer;
    }

    /**
     * NewSizeVerifier checks that initial young gen size is equal to expected
     * regardful to alignment and that young gen size will not be greater than
     * expected max size.
     * In order to verify that young gen size will not be greater then expected
     * max size, NewSizeVerifier do some object allocation to force garbage
     * collection and heap expansion.
     */
    public static class NewSizeVerifier {

        private static final WhiteBox WB = WhiteBox.getWhiteBox();
        private static final GCTypes.YoungGCType YOUNG_GC_TYPE = GCTypes.YoungGCType.getYoungGCType();
        private static final long HEAP_SPACE_ALIGNMENT = WB.getHeapSpaceAlignment();
        private static final long HEAP_ALIGNMENT = WB.getHeapAlignment();
        private static final long PS_VIRTUAL_SPACE_ALIGNMENT =
                (YOUNG_GC_TYPE == GCTypes.YoungGCType.PSNew) ? WB.psVirtualSpaceAlignment() : 0;

        public static final int ARRAY_LENGTH = 100;
        public static final int CHUNK_SIZE = 1024;
        public static final int MAX_ITERATIONS = 10;
        public static byte garbage[][] = new byte[ARRAY_LENGTH][];

        public static void main(String args[]) throws Exception {
            if (args.length != 4) {
                throw new IllegalArgumentException("Expected 4 args: <expectedNewSize> <expectedMaxNewSize> <initialHeapSize> <maxHeapSize>");
            }
            final long newSize = Long.valueOf(args[0]);
            final long maxNewSize = Long.valueOf(args[1]);
            final long initialHeapSize = Long.valueOf(args[2]);
            final long maxHeapSize = Long.valueOf(args[3]);

            // verify initial size
            verifyNewSize(newSize, maxNewSize, initialHeapSize, maxHeapSize);

            // force GC and verify that size is still correct
            AllocationHelper allocator = new AllocationHelper(MAX_ITERATIONS, ARRAY_LENGTH, CHUNK_SIZE, () -> (verifyNewSize(newSize, maxNewSize, initialHeapSize, maxHeapSize)));
            allocator.allocateMemoryAndVerifyNoOOME();
        }

        /**
         * Verify that actual young gen size conforms NewSize and MaxNewSize values.
         */
        public static Void verifyNewSize(long newSize, long maxNewSize,
                long initialHeapSize, long maxHeapSize) {
            long alignedNewSize = alignGenSize(newSize);
            long alignedMaxNewSize = alignGenSize(maxNewSize);
            long alignedXms = alignHeapSize(initialHeapSize);
            long alignedXmx = alignHeapSize(maxHeapSize);

            MemoryUsage youngGenUsage = getYoungGenUsage();
            long initSize = youngGenUsage.getInit();
            long commitedSize = youngGenUsage.getCommitted();
            long maxSize = youngGenUsage.getMax();

            if (newSize != -1) {
                if (initSize < alignedNewSize) {
                    throw new RuntimeException("initial new size < NewSize value: "
                            + initSize + " < " + alignedNewSize);
                }

                if (commitedSize < alignedNewSize) {
                    throw new RuntimeException("actual new size < NewSize value: "
                            + commitedSize + " < " + alignedNewSize);
                }

                // for G1 max new size == committed new size
                if (YOUNG_GC_TYPE != GCTypes.YoungGCType.G1
                        && maxSize < alignedNewSize) {
                    throw new RuntimeException("max new size < NewSize value: "
                            + maxSize + " < " + alignedNewSize);
                }
            }

            if (maxNewSize != -1) {
                if (initSize > alignedMaxNewSize) {
                    throw new RuntimeException("initial new size > MaxNewSize value: "
                            + initSize + " > " + alignedMaxNewSize);
                }

                if (commitedSize > alignedMaxNewSize) {
                    throw new RuntimeException("actual new size > MaxNewSize value: "
                            + commitedSize + " > " + alignedMaxNewSize);
                }

                if (alignedXms != alignedXmx) {
                    if (YOUNG_GC_TYPE != GCTypes.YoungGCType.G1
                            && maxSize != alignedMaxNewSize) {
                        throw new RuntimeException("max new size != MaxNewSize value: "
                                + maxSize + " != " + alignedMaxNewSize);
                    }
                } else {
                    if (YOUNG_GC_TYPE != GCTypes.YoungGCType.G1
                            && maxSize != alignedNewSize) {
                        throw new RuntimeException("max new size != NewSize for case InitialHeapSize == MaxHeapSize value: "
                                + maxSize + " != " + alignedNewSize + " HeapSize == " + alignedXms);
                    }
                }
            }
            return null;
        }

        /**
         *  Get young gen memory usage.
         *
         *  For G1 it is EdenUsage + SurvivorUsage,
         *  for other GCs it is EdenUsage + 2 * SurvivorUsage.
         *  For G1 max value is just LONG_MAX.
         *  For all GCs used value is 0.
         */
        private static MemoryUsage getYoungGenUsage() {
            MemoryUsage edenUsage = HeapRegionUsageTool.getEdenUsage();
            MemoryUsage survivorUsage = HeapRegionUsageTool.getSurvivorUsage();
            long edenUsageInit = edenUsage.getInit();
            long edenUsageCommited = edenUsage.getCommitted();
            long survivorUsageInit = survivorUsage.getInit();
            long survivorUsageCommited = survivorUsage.getCommitted();

            if (YOUNG_GC_TYPE == GCTypes.YoungGCType.G1) {
                return new MemoryUsage(edenUsageInit + survivorUsageInit, 0,
                        edenUsageCommited + survivorUsageCommited, Long.MAX_VALUE);
            } else {
                return new MemoryUsage(edenUsageInit + survivorUsageInit * 2, 0,
                        edenUsageCommited + survivorUsageCommited * 2,
                        edenUsage.getMax() + survivorUsage.getMax() * 2);
            }
        }

        /**
         * Align generation size regardful to used young GC.
         */
        public static long alignGenSize(long value) {
            switch (YOUNG_GC_TYPE) {
                case DefNew:
                case ParNew:
                    return HeapRegionUsageTool.alignDown(value, HEAP_SPACE_ALIGNMENT);
                case PSNew:
                    return HeapRegionUsageTool.alignUp(HeapRegionUsageTool.alignDown(value,
                            HEAP_SPACE_ALIGNMENT),
                            PS_VIRTUAL_SPACE_ALIGNMENT);
                case G1:
                    return HeapRegionUsageTool.alignUp(value, WB.g1RegionSize());
                default:
                    throw new RuntimeException("Unexpected young GC type");
            }
        }

        /**
         * Align heap size.
         */
        public static long alignHeapSize(long value){
            return HeapRegionUsageTool.alignUp(value,HEAP_ALIGNMENT);
        }
    }
}