1/*
2 * Copyright (c) 2014, 2016, 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
24package compiler.codecache.cli.codeheapsize;
25
26import compiler.codecache.cli.common.CodeCacheCLITestCase;
27import compiler.codecache.cli.common.CodeCacheOptions;
28import jdk.test.lib.process.ExitCode;
29import jdk.test.lib.Utils;
30import jdk.test.lib.cli.CommandLineOptionTest;
31import sun.hotspot.code.BlobType;
32
33import java.util.Random;
34
35/**
36 * Test case runner aimed to verify option's consistency.
37 */
38public class JVMStartupRunner implements CodeCacheCLITestCase.Runner {
39    private static final String INCONSISTENT_CH_SIZES_ERROR
40            = "Invalid code heap sizes.*";
41
42    @Override
43    public void run(CodeCacheCLITestCase.Description testCaseDescription,
44            CodeCacheOptions options) throws Throwable {
45        // Everything should be fine when
46        // sum(all code heap sizes) == reserved CC size
47        CommandLineOptionTest.verifySameJVMStartup(/* expected messages */ null,
48                new String[]{ INCONSISTENT_CH_SIZES_ERROR },
49                "JVM startup should not fail with consistent code heap sizes",
50                "JVM output should not contain warning about inconsistent code "
51                + "heap sizes", ExitCode.OK, options.prepareOptions());
52
53        verifySingleInconsistentValue(options);
54        verifyAllInconsistentValues(options);
55    }
56
57    /**
58     * Verifies that if at least one of three options will have value, such
59     * that sum of all three values will be inconsistent, then JVM startup will
60     * fail.
61     */
62    private static void verifySingleInconsistentValue(CodeCacheOptions options)
63            throws Throwable {
64        verifyHeapSizesSum(options.reserved,
65                scaleCodeHeapSize(options.profiled), options.nonProfiled,
66                options.nonNmethods);
67        verifyHeapSizesSum(options.reserved, options.profiled,
68                scaleCodeHeapSize(options.nonProfiled), options.nonNmethods);
69        verifyHeapSizesSum(options.reserved, options.profiled,
70                options.nonProfiled, scaleCodeHeapSize(options.nonNmethods));
71    }
72
73    /**
74     * Verifies that if all three options will have values such that their sum
75     * is inconsistent with ReservedCodeCacheSize value, then JVM startup will
76     * fail.
77     */
78    private static void verifyAllInconsistentValues(CodeCacheOptions options)
79            throws Throwable {
80        long profiled = options.profiled;
81        long nonProfiled = options.nonProfiled;
82        long nonNMethods = options.nonNmethods;
83
84        while (options.reserved == profiled + nonProfiled + nonNMethods) {
85            profiled = scaleCodeHeapSize(profiled);
86            nonProfiled = scaleCodeHeapSize(nonProfiled);
87            nonNMethods = scaleCodeHeapSize(nonNMethods);
88        }
89
90        verifyHeapSizesSum(options.reserved, profiled, nonProfiled,
91                nonNMethods);
92    }
93
94    private static void verifyHeapSizesSum(long reserved, long profiled,
95            long nonProfiled, long nonNmethods) throws Throwable {
96        // JVM startup expected to fail when
97        // sum(all code heap sizes) != reserved CC size
98        CommandLineOptionTest.verifySameJVMStartup(
99                new String[]{ INCONSISTENT_CH_SIZES_ERROR },
100                /* unexpected messages */ null,
101                "JVM startup should fail with inconsistent code heap size.",
102                "JVM output should contain appropriate error message of code "
103                        + "heap sizes are inconsistent",
104                ExitCode.FAIL,
105                CommandLineOptionTest.prepareBooleanFlag(
106                        CodeCacheOptions.SEGMENTED_CODE_CACHE, true),
107                CommandLineOptionTest.prepareNumericFlag(
108                        BlobType.All.sizeOptionName, reserved),
109                CommandLineOptionTest.prepareNumericFlag(
110                        BlobType.MethodProfiled.sizeOptionName, profiled),
111                CommandLineOptionTest.prepareNumericFlag(
112                        BlobType.MethodNonProfiled.sizeOptionName, nonProfiled),
113                CommandLineOptionTest.prepareNumericFlag(
114                        BlobType.NonNMethod.sizeOptionName, nonNmethods));
115    }
116
117    /**
118     * Returns {@code unscaledSize} value scaled by a random factor from
119     * range (1, 2). If {@code unscaledSize} is not 0, then this
120     * method will return value that won't be equal to {@code unscaledSize}.
121     *
122     * @param unscaledSize The value to be scaled.
123     * @return {@code unscaledSize} value scaled by a factor from range (1, 2).
124     */
125    private static long scaleCodeHeapSize(long unscaledSize) {
126        Random random = Utils.getRandomInstance();
127
128        long scaledSize = unscaledSize;
129        while (scaledSize == unscaledSize && unscaledSize != 0) {
130            float scale = 1.0f + random.nextFloat();
131            scaledSize = (long) Math.ceil(scale * unscaledSize);
132        }
133        return scaledSize;
134    }
135}
136