1 /*
   2  * Copyright (c) 2014, 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 import java.lang.management.ManagementFactory;
  25 import java.lang.management.MemoryPoolMXBean;
  26 import java.util.Objects;
  27 import java.util.Optional;
  28 import java.util.regex.Matcher;
  29 import java.util.regex.Pattern;
  30 
  31 import jdk.test.lib.Asserts;
  32 import com.sun.management.ThreadMXBean;
  33 import sun.hotspot.WhiteBox;
  34 import jdk.internal.misc.Unsafe;
  35 
  36 /**
  37  * Main class for tests on {@code SurvivorAlignmentInBytes} option.
  38  *
  39  * Typical usage is to obtain instance using fromArgs method, allocate objects
  40  * and verify that actual memory usage in tested heap space is close to
  41  * expected.
  42  */
  43 public class SurvivorAlignmentTestMain {
  44     enum HeapSpace {
  45         EDEN,
  46         SURVIVOR,
  47         TENURED
  48     }
  49 
  50     public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  51 
  52     public static final long MAX_TENURING_THRESHOLD = Optional.ofNullable(
  53             SurvivorAlignmentTestMain.WHITE_BOX.getIntxVMFlag(
  54                     "MaxTenuringThreshold")).orElse(15L);
  55 
  56     /**
  57      * Regexp used to parse memory size params, like 2G, 34m or 15k.
  58      */
  59     private static final Pattern SIZE_REGEX
  60             = Pattern.compile("(?<size>[0-9]+)(?<multiplier>[GMKgmk])?");
  61 
  62     // Names of different heap spaces.
  63     private static final String DEF_NEW_EDEN = "Eden Space";
  64     private static final String DEF_NEW_SURVIVOR = "Survivor Space";
  65     private static final String PAR_NEW_EDEN = "Par Eden Space";
  66     private static final String PAR_NEW_SURVIVOR = "Par Survivor Space";
  67     private static final String PS_EDEN = "PS Eden Space";
  68     private static final String PS_SURVIVOR = "PS Survivor Space";
  69     private static final String G1_EDEN = "G1 Eden Space";
  70     private static final String G1_SURVIVOR = "G1 Survivor Space";
  71     private static final String SERIAL_TENURED = "Tenured Gen";
  72     private static final String CMS_TENURED = "CMS Old Gen";
  73     private static final String PS_TENURED = "PS Old Gen";
  74     private static final String G1_TENURED_LEGACY = "G1 Old Gen";
  75     private static final String G1_TENURED = "G1 Old Space";
  76 
  77     private static final long G1_HEAP_REGION_SIZE = Optional.ofNullable(
  78             SurvivorAlignmentTestMain.WHITE_BOX.getUintxVMFlag(
  79                     "G1HeapRegionSize")).orElse(-1L);
  80 
  81     /**
  82      * Min size of free chunk in CMS generation.
  83      * An object allocated in CMS generation will at least occupy this amount
  84      * of bytes.
  85      */
  86     private static final long CMS_MIN_FREE_CHUNK_SIZE
  87             = 3L * Unsafe.ADDRESS_SIZE;
  88 
  89     private static final AlignmentHelper EDEN_SPACE_HELPER;
  90     private static final AlignmentHelper SURVIVOR_SPACE_HELPER;
  91     private static final AlignmentHelper TENURED_SPACE_HELPER;
  92     /**
  93      * Amount of memory that should be filled during a test run.
  94      */
  95     private final long memoryToFill;
  96     /**
  97      * The size of an objects that will be allocated during a test run.
  98      */
  99     private final long objectSize;
 100     /**
 101      * Amount of memory that will be actually occupied by an object in eden
 102      * space.
 103      */
 104     private final long actualObjectSize;
 105     /**
 106      * Storage for allocated objects.
 107      */
 108     private final Object[] garbage;
 109     /**
 110      * Heap space whose memory usage is a subject of assertions during the test
 111      * run.
 112      */
 113     private final HeapSpace testedSpace;
 114 
 115     private long[] baselinedThreadMemoryUsage = null;
 116     private long[] threadIds = null;
 117 
 118     /**
 119      * Initialize {@code EDEN_SPACE_HELPER}, {@code SURVIVOR_SPACE_HELPER} and
 120      * {@code TENURED_SPACE_HELPER} to represent heap spaces in use.
 121      *
 122      * Note that regardless to GC object's alignment in survivor space is
 123      * expected to be equal to {@code SurvivorAlignmentInBytes} value and
 124      * alignment in other spaces is expected to be equal to
 125      * {@code ObjectAlignmentInBytes} value.
 126      *
 127      * In CMS generation we can't allocate less then {@code MinFreeChunk} value,
 128      * for other CGs we expect that object of size {@code MIN_OBJECT_SIZE}
 129      * could be allocated as it is (of course, its size could be aligned
 130      * according to alignment value used in a particular space).
 131      *
 132      * For G1 GC MXBeans could report memory usage only with region size
 133      * precision (if an object allocated in some G1 heap region, then all region
 134      * will claimed as used), so for G1's spaces precision is equal to
 135      * {@code G1HeapRegionSize} value.
 136      */
 137     static {
 138         AlignmentHelper edenHelper = null;
 139         AlignmentHelper survivorHelper = null;
 140         AlignmentHelper tenuredHelper = null;
 141         for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
 142             switch (pool.getName()) {
 143                 case SurvivorAlignmentTestMain.DEF_NEW_EDEN:
 144                 case SurvivorAlignmentTestMain.PAR_NEW_EDEN:
 145                 case SurvivorAlignmentTestMain.PS_EDEN:
 146                     Asserts.assertNull(edenHelper,
 147                             "Only one bean for eden space is expected.");
 148                     edenHelper = new AlignmentHelper(
 149                             AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
 150                             AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
 151                             AlignmentHelper.MIN_OBJECT_SIZE, pool);
 152                     break;
 153                 case SurvivorAlignmentTestMain.G1_EDEN:
 154                     Asserts.assertNull(edenHelper,
 155                             "Only one bean for eden space is expected.");
 156                     edenHelper = new AlignmentHelper(
 157                             SurvivorAlignmentTestMain.G1_HEAP_REGION_SIZE,
 158                             AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
 159                             AlignmentHelper.MIN_OBJECT_SIZE, pool);
 160                     break;
 161                 case SurvivorAlignmentTestMain.DEF_NEW_SURVIVOR:
 162                 case SurvivorAlignmentTestMain.PAR_NEW_SURVIVOR:
 163                 case SurvivorAlignmentTestMain.PS_SURVIVOR:
 164                     Asserts.assertNull(survivorHelper,
 165                             "Only one bean for survivor space is expected.");
 166                     survivorHelper = new AlignmentHelper(
 167                             AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
 168                             AlignmentHelper.SURVIVOR_ALIGNMENT_IN_BYTES,
 169                             AlignmentHelper.MIN_OBJECT_SIZE, pool);
 170                     break;
 171                 case SurvivorAlignmentTestMain.G1_SURVIVOR:
 172                     Asserts.assertNull(survivorHelper,
 173                             "Only one bean for survivor space is expected.");
 174                     survivorHelper = new AlignmentHelper(
 175                             SurvivorAlignmentTestMain.G1_HEAP_REGION_SIZE,
 176                             AlignmentHelper.SURVIVOR_ALIGNMENT_IN_BYTES,
 177                             AlignmentHelper.MIN_OBJECT_SIZE, pool);
 178                     break;
 179                 case SurvivorAlignmentTestMain.SERIAL_TENURED:
 180                 case SurvivorAlignmentTestMain.PS_TENURED:
 181                 case SurvivorAlignmentTestMain.G1_TENURED_LEGACY:
 182                 case SurvivorAlignmentTestMain.G1_TENURED:
 183                     Asserts.assertNull(tenuredHelper,
 184                             "Only one bean for tenured space is expected.");
 185                     tenuredHelper = new AlignmentHelper(
 186                             AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
 187                             AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
 188                             AlignmentHelper.MIN_OBJECT_SIZE, pool);
 189                     break;
 190                 case SurvivorAlignmentTestMain.CMS_TENURED:
 191                     Asserts.assertNull(tenuredHelper,
 192                             "Only one bean for tenured space is expected.");
 193                     tenuredHelper = new AlignmentHelper(
 194                             AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
 195                             AlignmentHelper.OBJECT_ALIGNMENT_IN_BYTES,
 196                             SurvivorAlignmentTestMain.CMS_MIN_FREE_CHUNK_SIZE,
 197                             pool);
 198                     break;
 199             }
 200         }
 201         EDEN_SPACE_HELPER = Objects.requireNonNull(edenHelper,
 202                 "AlignmentHelper for eden space should be initialized.");
 203         SURVIVOR_SPACE_HELPER = Objects.requireNonNull(survivorHelper,
 204                 "AlignmentHelper for survivor space should be initialized.");
 205         TENURED_SPACE_HELPER = Objects.requireNonNull(tenuredHelper,
 206                 "AlignmentHelper for tenured space should be initialized.");
 207     }
 208     /**
 209      * Returns an SurvivorAlignmentTestMain instance constructed using CLI
 210      * options.
 211      *
 212      * Following options are expected:
 213      * <ul>
 214      *     <li>memoryToFill</li>
 215      *     <li>objectSize</li>
 216      * </ul>
 217      *
 218      * Both argument may contain multiplier suffix k, m or g.
 219      */
 220     public static SurvivorAlignmentTestMain fromArgs(String[] args) {
 221         Asserts.assertEQ(args.length, 3, "Expected three arguments: "
 222                 + "memory size, object size and tested heap space name.");
 223 
 224         long memoryToFill = parseSize(args[0]);
 225         long objectSize = Math.max(parseSize(args[1]),
 226                 AlignmentHelper.MIN_ARRAY_SIZE);
 227         HeapSpace testedSpace = HeapSpace.valueOf(args[2]);
 228 
 229         return new SurvivorAlignmentTestMain(memoryToFill, objectSize,
 230                 testedSpace);
 231     }
 232 
 233     /**
 234      * Returns a value parsed from a string with format
 235      * &lt;integer&gt;&lt;multiplier&gt;.
 236      */
 237     private static long parseSize(String sizeString) {
 238         Matcher matcher = SIZE_REGEX.matcher(sizeString);
 239         Asserts.assertTrue(matcher.matches(),
 240                 "sizeString should have following format \"[0-9]+([MBK])?\"");
 241         long size = Long.valueOf(matcher.group("size"));
 242 
 243         if (matcher.group("multiplier") != null) {
 244             long K = 1024L;
 245             // fall through multipliers
 246             switch (matcher.group("multiplier").toLowerCase()) {
 247                 case "g":
 248                     size *= K;
 249                 case "m":
 250                     size *= K;
 251                 case "k":
 252                     size *= K;
 253             }
 254         }
 255         return size;
 256     }
 257 
 258     private SurvivorAlignmentTestMain(long memoryToFill, long objectSize,
 259             HeapSpace testedSpace) {
 260         this.objectSize = objectSize;
 261         this.memoryToFill = memoryToFill;
 262         this.testedSpace = testedSpace;
 263 
 264         AlignmentHelper helper = SurvivorAlignmentTestMain.EDEN_SPACE_HELPER;
 265 
 266         this.actualObjectSize = helper.getObjectSizeInThisSpace(
 267                 this.objectSize);
 268         int arrayLength = helper.getObjectsCount(memoryToFill, this.objectSize);
 269         garbage = new Object[arrayLength];
 270     }
 271 
 272     /**
 273      * Allocate byte arrays to fill {@code memoryToFill} memory.
 274      */
 275     public void allocate() {
 276         int byteArrayLength = Math.max((int) (objectSize
 277                 - Unsafe.ARRAY_BYTE_BASE_OFFSET), 0);
 278 
 279         for (int i = 0; i < garbage.length; i++) {
 280             garbage[i] = new byte[byteArrayLength];
 281         }
 282     }
 283 
 284     /**
 285      * Release memory occupied after {@code allocate} call.
 286      */
 287     public void release() {
 288         for (int i = 0; i < garbage.length; i++) {
 289             garbage[i] = null;
 290         }
 291     }
 292 
 293     /**
 294      * Returns expected amount of memory occupied in a {@code heapSpace} by
 295      * objects referenced from {@code garbage} array.
 296      */
 297     public long getExpectedMemoryUsage() {
 298         AlignmentHelper alignmentHelper = getAlignmentHelper(testedSpace);
 299         return alignmentHelper.getExpectedMemoryUsage(objectSize,
 300                 garbage.length);
 301     }
 302 
 303     /**
 304      * Verifies that memory usage in a {@code heapSpace} deviates from
 305      * {@code expectedUsage} for no more than {@code MAX_RELATIVE_DEVIATION}.
 306      */
 307     public void verifyMemoryUsage(long expectedUsage) {
 308         AlignmentHelper alignmentHelper = getAlignmentHelper(testedSpace);
 309 
 310         long actualMemoryUsage = alignmentHelper.getActualMemoryUsage();
 311         boolean otherThreadsAllocatedMemory = areOtherThreadsAllocatedMemory();
 312 
 313         long memoryUsageDiff = Math.abs(actualMemoryUsage - expectedUsage);
 314         long maxAllowedUsageDiff
 315                 = alignmentHelper.getAllowedMemoryUsageDeviation(expectedUsage);
 316 
 317         System.out.println("Verifying memory usage in space: " + testedSpace);
 318         System.out.println("Allocated objects count: " + garbage.length);
 319         System.out.println("Desired object size: " + objectSize);
 320         System.out.println("Actual object size: " + actualObjectSize);
 321         System.out.println("Expected object size in space: "
 322                 + alignmentHelper.getObjectSizeInThisSpace(objectSize));
 323         System.out.println("Expected memory usage: " + expectedUsage);
 324         System.out.println("Actual memory usage: " + actualMemoryUsage);
 325         System.out.println("Memory usage diff: " + memoryUsageDiff);
 326         System.out.println("Max allowed usage diff: " + maxAllowedUsageDiff);
 327 
 328         if (memoryUsageDiff > maxAllowedUsageDiff
 329                 && otherThreadsAllocatedMemory) {
 330             System.out.println("Memory usage diff is incorrect, but it seems "
 331                     + "like someone else allocated objects");
 332             return;
 333         }
 334 
 335         Asserts.assertLTE(memoryUsageDiff, maxAllowedUsageDiff,
 336                 "Actual memory usage should not deviate from expected for " +
 337                         "more then " + maxAllowedUsageDiff);
 338     }
 339 
 340     /**
 341      * Baselines amount of memory allocated by each thread.
 342      */
 343     public void baselineMemoryAllocation() {
 344         ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
 345         threadIds = bean.getAllThreadIds();
 346         baselinedThreadMemoryUsage = bean.getThreadAllocatedBytes(threadIds);
 347     }
 348 
 349     /**
 350      * Checks if threads other then the current thread were allocating objects
 351      * after baselinedThreadMemoryUsage call.
 352      *
 353      * If baselinedThreadMemoryUsage was not called, then this method will return
 354      * {@code false}.
 355      */
 356     public boolean areOtherThreadsAllocatedMemory() {
 357         if (baselinedThreadMemoryUsage == null) {
 358             return false;
 359         }
 360 
 361         ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
 362         long currentMemoryAllocation[]
 363                 = bean.getThreadAllocatedBytes(threadIds);
 364         boolean otherThreadsAllocatedMemory = false;
 365 
 366         System.out.println("Verifying amount of memory allocated by threads:");
 367         for (int i = 0; i < threadIds.length; i++) {
 368             System.out.format("Thread %d%nbaseline allocation: %d"
 369                             + "%ncurrent allocation:%d%n", threadIds[i],
 370                     baselinedThreadMemoryUsage[i], currentMemoryAllocation[i]);
 371             System.out.println(bean.getThreadInfo(threadIds[i]));
 372 
 373             long bytesAllocated = Math.abs(currentMemoryAllocation[i]
 374                     - baselinedThreadMemoryUsage[i]);
 375             if (bytesAllocated > 0
 376                     && threadIds[i] != Thread.currentThread().getId()) {
 377                 otherThreadsAllocatedMemory = true;
 378             }
 379         }
 380 
 381         return otherThreadsAllocatedMemory;
 382     }
 383 
 384     @Override
 385     public String toString() {
 386         StringBuilder builder = new StringBuilder();
 387 
 388         builder.append(String.format("SurvivorAlignmentTestMain info:%n"));
 389         builder.append(String.format("Desired object size: %d%n", objectSize));
 390         builder.append(String.format("Memory to fill: %d%n", memoryToFill));
 391         builder.append(String.format("Objects to be allocated: %d%n",
 392                 garbage.length));
 393 
 394         builder.append(String.format("Alignment helpers to be used: %n"));
 395         for (HeapSpace heapSpace: HeapSpace.values()) {
 396             builder.append(String.format("For space %s:%n%s%n", heapSpace,
 397                     getAlignmentHelper(heapSpace)));
 398         }
 399 
 400         return builder.toString();
 401     }
 402 
 403     /**
 404      * Returns {@code AlignmentHelper} for a space {@code heapSpace}.
 405      */
 406     public static AlignmentHelper getAlignmentHelper(HeapSpace heapSpace) {
 407         switch (heapSpace) {
 408             case EDEN:
 409                 return SurvivorAlignmentTestMain.EDEN_SPACE_HELPER;
 410             case SURVIVOR:
 411                 return SurvivorAlignmentTestMain.SURVIVOR_SPACE_HELPER;
 412             case TENURED:
 413                 return SurvivorAlignmentTestMain.TENURED_SPACE_HELPER;
 414             default:
 415                 throw new Error("Unexpected heap space: " + heapSpace);
 416         }
 417     }
 418 }