1 /*
   2  * Copyright (c) 2010, 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  * This test stress RememberetSet procerssing in the G1 by creation of references
  25  * between different 1MB blocks.
  26  * Test is specific for G1, for other GCs it should just pass.
  27  */
  28 
  29 
  30 /*
  31  * @test
  32  * @modules java.base/jdk.internal.misc:+open java.base/jdk.internal.vm.annotation:+open java.base/sun.reflect.annotation:+open
  33  * @key stress gc
  34  *
  35  * @summary converted from VM Testbase gc/gctests/RememberedSet.
  36  * VM Testbase keywords: [gc, stress, stressopt, feature_g1, nonconcurrent]
  37  *
  38  * @library /vmTestbase
  39  *          /test/lib
  40  * @run driver jdk.test.lib.FileInstaller . .
  41  * @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.RememberedSet.RememberedSet
  42  */
  43 
  44 package gc.gctests.RememberedSet;
  45 
  46 import java.lang.reflect.Field;
  47 import java.util.ArrayList;
  48 import java.util.List;
  49 import nsk.share.gc.GC;
  50 import nsk.share.gc.MemoryObject;
  51 import nsk.share.gc.ThreadedGCTest;
  52 import nsk.share.test.ExecutionController;
  53 import jdk.internal.misc.Unsafe;
  54 
  55 public class RememberedSet extends ThreadedGCTest {
  56 
  57     static class PointerUtils {
  58         private static Unsafe unsafe;
  59         private static long fieldOffset;
  60         private static PointerUtils instance = new PointerUtils();
  61         private static boolean compressedRef = false;
  62 
  63         static {
  64             try {
  65                 unsafe = Unsafe.getUnsafe();
  66                 fieldOffset = unsafe.objectFieldOffset(PointerUtils.class.getDeclaredField("obj"));
  67                 long fieldOffset0 = unsafe.objectFieldOffset(PointerUtils.class.getDeclaredField("obj0"));
  68                 int oopSize = (int)Math.abs(fieldOffset - fieldOffset0);
  69 
  70                 if (oopSize != unsafe.addressSize()) {
  71                     System.out.println("Compressed oops detected");
  72                     compressedRef = true;
  73                 }
  74             } catch (Exception ex) {
  75                 throw new RuntimeException(ex);
  76             }
  77         }
  78 
  79         private Object obj;
  80         private Object obj0;
  81 
  82         public synchronized static long toAddress(Object o) {
  83             long address;
  84             instance.obj = o;
  85 
  86             if (compressedRef || unsafe.addressSize() == 4) {
  87                 address = unsafe.getInt(instance, fieldOffset);
  88             }
  89             else {
  90                 address = unsafe.getLong(instance, fieldOffset);
  91             }
  92 
  93             return address;
  94         }
  95 
  96     }
  97     private ExecutionController stresser;
  98 
  99     @Override
 100     protected Runnable createRunnable(int i) {
 101         return new Worker();
 102     }
 103 
 104     class Worker implements Runnable {
 105 
 106         static final long BLOCK_SIZE = 1024 * 1024;
 107 
 108 
 109         // this method tries to allocate a new MemoryObject
 110         // which is in another 1MB block
 111         MemoryObject getOutOfTheBlockObject(int size, Object obj) {
 112             long address = PointerUtils.toAddress(obj);
 113             MemoryObject ref = new MemoryObject(size);
 114             int attempt = (int) (BLOCK_SIZE / size);
 115             while (attempt != 0 && Math.abs(address - PointerUtils.toAddress(ref)) < BLOCK_SIZE) {
 116                 ref = new MemoryObject(size);
 117                 attempt--;
 118             }
 119             return ref;
 120         }
 121 
 122         @Override
 123         public void run() {
 124 
 125             int size = (int) Math.sqrt(BLOCK_SIZE);
 126             int refsCount = (int) (runParams.getTestMemory() / BLOCK_SIZE);
 127             int count = (int) (runParams.getTestMemory() / runParams.getNumberOfThreads() / (refsCount * size));
 128             // Each cycle 10% of references and 10% of arrays are reallocated
 129             int step = 10;
 130 
 131             List<List<MemoryObject>> objs = new ArrayList<List<MemoryObject>>(count);
 132             for (int i = 0; i < count; i++) {
 133                 List<MemoryObject> obj = new ArrayList<MemoryObject>();
 134                 objs.add(obj);
 135                 for (int j = 0; j < refsCount; j++) {
 136                     obj.add(getOutOfTheBlockObject(size, obj));
 137                 }
 138             }
 139             if (stresser == null) {
 140                 stresser = getExecutionController();
 141             }
 142             int shift = 0;
 143             while (stresser.continueExecution()) {
 144                 for (int j = shift; j < refsCount; j += step) {
 145                     for (int i = 0; i < count; i ++) {
 146                         // update each 10th reference to allow GC previous one
 147                         List<MemoryObject> obj = objs.get(i);
 148                         obj.set(j, getOutOfTheBlockObject(size, obj));
 149                     }
 150                 }
 151                 for (int i = step - shift; i < count; i += step) {
 152                     // update each 10th array of references
 153                     // to allocate it in the another 1MB block (as new young object)
 154                     List<MemoryObject> obj = new ArrayList<MemoryObject>();
 155                     objs.set(i, obj);
 156                     for (int j = 0; j < refsCount; j++) {
 157                         obj.add(getOutOfTheBlockObject(size, obj));
 158                     }
 159                 }
 160                 // shift is changed from 0 to step - 1
 161                 log.debug("shift = " + shift);
 162                 shift = (shift + 1) % step;
 163             }
 164         }
 165     }
 166 
 167     public static void main(String[] args) {
 168         GC.runTest(new RememberedSet(), args);
 169     }
 170 }