1 /*
   2  * Copyright (c) 2014, 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 import static com.oracle.java.testlibrary.Asserts.assertLessThanOrEqual;
  25 import com.oracle.java.testlibrary.OutputAnalyzer;
  26 import com.oracle.java.testlibrary.Platform;
  27 import com.oracle.java.testlibrary.ProcessTools;
  28 import com.oracle.java.testlibrary.Utils;
  29 import java.io.IOException;
  30 import java.lang.management.ManagementFactory;
  31 import java.lang.management.MemoryUsage;
  32 import java.text.DecimalFormat;
  33 import java.text.DecimalFormatSymbols;
  34 import java.util.ArrayList;
  35 import java.util.Arrays;
  36 import java.util.Collections;
  37 import java.util.LinkedList;
  38 import java.util.List;
  39 import sun.misc.Unsafe;
  40 
  41 public class TestShrinkAuxiliaryData {
  42 
  43     private final static String[] initialOpts = new String[]{
  44         "-XX:MinHeapFreeRatio=10",
  45         "-XX:MaxHeapFreeRatio=11",
  46         "-XX:+UseG1GC",
  47         "-XX:G1HeapRegionSize=1m",
  48         "-XX:+PrintGCDetails"
  49     };
  50 
  51     private final int RSetCacheSize;
  52 
  53     protected TestShrinkAuxiliaryData(int RSetCacheSize) {
  54         this.RSetCacheSize = RSetCacheSize;
  55     }
  56 
  57     protected void test() throws Exception {
  58         ArrayList<String> vmOpts = new ArrayList();
  59         Collections.addAll(vmOpts, initialOpts);
  60 
  61         int maxCacheSize = Math.max(0, Math.min(31, getMaxCacheSize()));
  62         if (maxCacheSize < RSetCacheSize) {
  63             System.out.format("Skiping test for %d cache size due max cache size %d",
  64                     RSetCacheSize, maxCacheSize
  65             );
  66             return;
  67         }
  68 
  69         printTestInfo(maxCacheSize);
  70 
  71         vmOpts.add("-XX:G1ConcRSLogCacheSize=" + RSetCacheSize);
  72         vmOpts.addAll(Arrays.asList(Utils.getTestJavaOpts()));
  73 
  74         // for 32 bits ObjectAlignmentInBytes is not a option
  75         if (Platform.is32bit()) {
  76             ArrayList<String> vmOptsWithoutAlign = new ArrayList(vmOpts);
  77             vmOptsWithoutAlign.add(ShrinkAuxiliaryDataTest.class.getName());
  78             performTest(vmOptsWithoutAlign);
  79             return;
  80         }
  81 
  82         for (int alignment = 3; alignment <= 8; alignment++) {
  83             ArrayList<String> vmOptsWithAlign = new ArrayList(vmOpts);
  84             vmOptsWithAlign.add("-XX:ObjectAlignmentInBytes="
  85                     + (int) Math.pow(2, alignment));
  86             vmOptsWithAlign.add(ShrinkAuxiliaryDataTest.class.getName());
  87 
  88             performTest(vmOptsWithAlign);
  89         }
  90     }
  91 
  92     private void performTest(List<String> opts) throws Exception {
  93         ProcessBuilder pb
  94                        = ProcessTools.createJavaProcessBuilder(
  95                 opts.toArray(new String[opts.size()])
  96         );
  97 
  98         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  99         output.shouldHaveExitValue(0);
 100     }
 101 
 102     private void printTestInfo(int maxCacheSize) {
 103 
 104         DecimalFormat grouped = new DecimalFormat("000,000");
 105         DecimalFormatSymbols formatSymbols = grouped.getDecimalFormatSymbols();
 106         formatSymbols.setGroupingSeparator(' ');
 107         grouped.setDecimalFormatSymbols(formatSymbols);
 108 
 109         System.out.format("Test will use %s bytes of memory of %s available%n"
 110                 + "Available memory is %s with %d bytes pointer size - can save %s pointers%n"
 111                 + "Max cache size: 2^%d = %s elements%n",
 112                 grouped.format(ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
 113                 grouped.format(Runtime.getRuntime().freeMemory()),
 114                 grouped.format(Runtime.getRuntime().freeMemory()
 115                         - ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
 116                 Unsafe.ADDRESS_SIZE,
 117                 grouped.format((Runtime.getRuntime().freeMemory()
 118                         - ShrinkAuxiliaryDataTest.getMemoryUsedByTest())
 119                         / Unsafe.ADDRESS_SIZE),
 120                 maxCacheSize,
 121                 grouped.format((int) Math.pow(2, maxCacheSize))
 122         );
 123     }
 124 
 125     /**
 126      * Detects maximum possible size of G1ConcRSLogCacheSize available for
 127      * current process based on maximum available process memory size
 128      *
 129      * @return power of two
 130      */
 131     private static int getMaxCacheSize() {
 132         long availableMemory = Runtime.getRuntime().freeMemory()
 133                 - ShrinkAuxiliaryDataTest.getMemoryUsedByTest() - 1l;
 134         if (availableMemory <= 0) {
 135             return 0;
 136         }
 137         long availablePointersCount = availableMemory / Unsafe.ADDRESS_SIZE;
 138         return (63 - (int) Long.numberOfLeadingZeros(availablePointersCount));
 139     }
 140 
 141     static class ShrinkAuxiliaryDataTest {
 142 
 143         public static void main(String[] args) throws IOException {
 144             int iterateCount = DEFAULT_ITERATION_COUNT;
 145 
 146             if (args.length > 0) {
 147                 try {
 148                     iterateCount = Integer.parseInt(args[0]);
 149                 } catch (NumberFormatException e) {
 150                     //num_iterate remains default
 151                 }
 152             }
 153 
 154             new ShrinkAuxiliaryDataTest().test(iterateCount);
 155         }
 156 
 157         class GarbageObject {
 158 
 159             private final List<byte[]> payload = new ArrayList();
 160             private final List<GarbageObject> ref = new LinkedList();
 161 
 162             public GarbageObject(int size) {
 163                 payload.add(new byte[size]);
 164             }
 165 
 166             public void addRef(GarbageObject g) {
 167                 ref.add(g);
 168             }
 169 
 170             public void mutate() {
 171                 if (!payload.isEmpty() && payload.get(0).length > 0) {
 172                     payload.get(0)[0] = (byte) (Math.random() * Byte.MAX_VALUE);
 173                 }
 174             }
 175         }
 176 
 177         private final List<GarbageObject> garbage = new ArrayList();
 178 
 179         public void test(int num_iterate) throws IOException {
 180 
 181             allocate();
 182             link();
 183             mutate();
 184             deallocate();
 185 
 186             MemoryUsage muBeforeHeap
 187                         = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
 188             MemoryUsage muBeforeNonHeap
 189                         = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
 190 
 191             for (int i = 0; i < num_iterate; i++) {
 192                 allocate();
 193                 link();
 194                 mutate();
 195                 deallocate();
 196             }
 197 
 198             System.gc();
 199             MemoryUsage muAfterHeap
 200                         = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
 201             MemoryUsage muAfterNonHeap
 202                         = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
 203 
 204             assertLessThanOrEqual(muAfterHeap.getCommitted(), muBeforeHeap.getCommitted(),
 205                     String.format("heap decommit failed - after > before: %d > %d",
 206                             muAfterHeap.getCommitted(), muBeforeHeap.getCommitted()
 207                     )
 208             );
 209 
 210             if (muAfterHeap.getCommitted() < muBeforeHeap.getCommitted()) {
 211                 assertLessThanOrEqual(muAfterNonHeap.getCommitted(), muBeforeNonHeap.getCommitted(),
 212                         String.format("non-heap decommit failed - after > before: %d > %d",
 213                                 muAfterNonHeap.getCommitted(), muBeforeNonHeap.getCommitted()
 214                         )
 215                 );
 216             }
 217         }
 218 
 219         private void allocate() {
 220             for (int r = 0; r < REGIONS_TO_ALLOCATE; r++) {
 221                 for (int i = 0; i < NUM_OBJECTS_PER_REGION; i++) {
 222                     GarbageObject g = new GarbageObject(REGION_SIZE
 223                             / NUM_OBJECTS_PER_REGION);
 224                     garbage.add(g);
 225                 }
 226             }
 227         }
 228 
 229         /**
 230          * Iterate through all allocated objects, and link to objects in another
 231          * regions
 232          */
 233         private void link() {
 234             for (int ig = 0; ig < garbage.size(); ig++) {
 235                 int regionNumber = ig / NUM_OBJECTS_PER_REGION;
 236 
 237                 for (int i = 0; i < NUM_LINKS; i++) {
 238                     int regionToLink;
 239                     do {
 240                         regionToLink = (int) (Math.random()
 241                                 * REGIONS_TO_ALLOCATE);
 242                     } while (regionToLink == regionNumber);
 243 
 244                     // get random garbage object from random region
 245                     garbage.get(ig).addRef(garbage.get(regionToLink
 246                             * NUM_OBJECTS_PER_REGION + (int) (Math.random()
 247                             * NUM_OBJECTS_PER_REGION)));
 248                 }
 249             }
 250         }
 251 
 252         private void mutate() {
 253             for (int ig = 0; ig < garbage.size(); ig++) {
 254                 garbage.get(ig).mutate();
 255             }
 256         }
 257 
 258         private void deallocate() {
 259             garbage.clear();
 260             System.gc();
 261         }
 262 
 263         static long getMemoryUsedByTest() {
 264             return REGIONS_TO_ALLOCATE * REGION_SIZE;
 265         }
 266 
 267         private static final int REGION_SIZE = 1024 * 1024;
 268         private static final int DEFAULT_ITERATION_COUNT = 1;   // iterate main scenario
 269         private static final int REGIONS_TO_ALLOCATE = 5;
 270         private static final int NUM_OBJECTS_PER_REGION = 10;
 271         private static final int NUM_LINKS = 20; // how many links create for each object
 272 
 273     }
 274 }