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