1 /*
   2  * Copyright (c) 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  */
  24 
  25 /**
  26  * @test
  27  * @bug 8148175
  28  * @library /testlibrary /test/lib
  29  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  30  * @run main/othervm/timeout=300 -XX:+UnlockDiagnosticVMOptions
  31  *      -XX:+WhiteBoxAPI -Xbatch -Xbootclasspath/a:.
  32  *      -Xmx300m PreserveFPRegistersTest
  33  */
  34 
  35 import sun.hotspot.WhiteBox;
  36 
  37 public class PreserveFPRegistersTest {
  38 
  39     public static void main(String... args) throws InterruptedException {
  40         new PreserveFPRegistersTest().go();
  41     }
  42 
  43     private static WhiteBox wb = WhiteBox.getWhiteBox();
  44 
  45     public final Object[][] storage;
  46 
  47     /**
  48      * Number of objects per region.
  49      */
  50     public final int K = 10;
  51 
  52     /**
  53      * Length of object array: sizeOf(Object[N]) ~= regionSize / K .
  54      */
  55     public final int N;
  56 
  57     /**
  58      * How many regions involved into testing.
  59      */
  60     public final int regionCount;
  61 
  62     PreserveFPRegistersTest() {
  63         long regionSize = wb.g1RegionSize();
  64         Runtime rt = Runtime.getRuntime();
  65         long used = rt.totalMemory() - rt.freeMemory();
  66         long totalFree = rt.maxMemory() - used;
  67         regionCount = (int) ( (totalFree / regionSize) * 0.9);
  68         int refSize = wb.getHeapOopSize();
  69         N = (int) ((regionSize / K ) / refSize) - 5;
  70 
  71         System.out.println("%% Memory");
  72         System.out.println("%%   used          :        " + used / 1024 + "M");
  73         System.out.println("%%   available     :        " + totalFree / 1024 + "M");
  74         System.out.println("%%   G1 Region Size:        " + regionSize / 1024 + "M");
  75         System.out.println("%%   region count  :        " + regionCount);
  76 
  77         System.out.println("%% Objects storage");
  78         System.out.println("%%   N (array length)      : " + N);
  79         System.out.println("%%   K (objects in regions): " + K);
  80         System.out.println("%%   Reference size        : " + refSize);
  81 
  82         try {
  83             storage = new Object[regionCount * K][];
  84             for (int i = 0; i < storage.length; i++) {
  85                 storage[i] = new Object[N];
  86             }
  87         } catch(OutOfMemoryError e) {
  88             throw new AssertionError("Test Failed with unexpected OutOfMemoryError exception");
  89         }
  90     }
  91 
  92     public void go() throws InterruptedException {
  93         final float FINAL = getValue();
  94 
  95         for (int to = 0; to < regionCount; to++) {
  96             Object celebrity = storage[to * K];
  97             for (int from = 0; from < regionCount; from++) {
  98                 for (int rn = 0; rn != 100; rn++) {
  99                     storage[getY(to, from, rn)][getX(to, from, rn)] = celebrity;
 100                 }
 101                 if (FINAL != getValue()) {
 102                     throw new AssertionError("Final value has changed: " + FINAL + " != " + getValue());
 103                 }
 104             }
 105         }
 106 
 107         System.out.println("TEST PASSED");
 108     }
 109 
 110     public float getValue() {
 111         return 6;
 112     }
 113 
 114     private int getX(int to, int from, int rn) {
 115         return (rn*regionCount + to) % N;
 116     }
 117 
 118     private int getY(int to, int from, int rn) {
 119         return ((rn*regionCount + to) / N + from * K) % (regionCount*K) ;
 120     }
 121 }