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