PreserveFPRegistersTest.java 4.1 KB
Newer Older
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
/*
 * Copyright (c) 2016, 2018, 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
 * @bug 8148175
28 29 30 31
 * @requires vm.gc=="G1" | vm.gc=="null"
 * @library /testlibrary /testlibrary/whitebox
 * @run main/bootclasspath/othervm -Xbatch -XX:+UnlockDiagnosticVMOptions
 *      -XX:+WhiteBoxAPI -Xmx300m -XX:+UseG1GC PreserveFPRegistersTest
32
 */
33 34 35

import sun.hotspot.WhiteBox;

36 37 38 39 40 41
public class PreserveFPRegistersTest {

    public static void main(String... args) throws InterruptedException {
        new PreserveFPRegistersTest().go();
    }

42 43
    private static WhiteBox wb = WhiteBox.getWhiteBox();

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    public final Object[][] storage;

    /**
     * Number of objects per region.
     */
    public final int K = 10;

    /**
     * Length of object array: sizeOf(Object[N]) ~= regionSize / K .
     */
    public final int N;

    /**
     * How many regions involved into testing.
     */
    public final int regionCount;

    PreserveFPRegistersTest() {
62
        long regionSize = wb.g1RegionSize();
63 64 65 66
        Runtime rt = Runtime.getRuntime();
        long used = rt.totalMemory() - rt.freeMemory();
        long totalFree = rt.maxMemory() - used;
        regionCount = (int) ( (totalFree / regionSize) * 0.9);
67
        int refSize = wb.getHeapOopSize();
68
        N = (int) ((regionSize / K ) / refSize) - 5;
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

        System.out.println("%% Memory");
        System.out.println("%%   used          :        " + used / 1024 + "M");
        System.out.println("%%   available     :        " + totalFree / 1024 + "M");
        System.out.println("%%   G1 Region Size:        " + regionSize / 1024 + "M");
        System.out.println("%%   region count  :        " + regionCount);

        System.out.println("%% Objects storage");
        System.out.println("%%   N (array length)      : " + N);
        System.out.println("%%   K (objects in regions): " + K);
        System.out.println("%%   Reference size        : " + refSize);

        try {
            storage = new Object[regionCount * K][];
            for (int i = 0; i < storage.length; i++) {
                storage[i] = new Object[N];
            }
        } catch(OutOfMemoryError e) {
            throw new AssertionError("Test Failed with unexpected OutOfMemoryError exception");
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
        }
    }

    public void go() throws InterruptedException {
        final float FINAL = getValue();

        for (int to = 0; to < regionCount; to++) {
            Object celebrity = storage[to * K];
            for (int from = 0; from < regionCount; from++) {
                for (int rn = 0; rn != 100; rn++) {
                    storage[getY(to, from, rn)][getX(to, from, rn)] = celebrity;
                }
                if (FINAL != getValue()) {
                    throw new AssertionError("Final value has changed: " + FINAL + " != " + getValue());
                }
            }
        }

        System.out.println("TEST PASSED");
    }

    public float getValue() {
        return 6;
    }

    private int getX(int to, int from, int rn) {
        return (rn*regionCount + to) % N;
    }

    private int getY(int to, int from, int rn) {
        return ((rn*regionCount + to) / N + from * K) % (regionCount*K) ;
    }
}