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 package gc.g1.plab.lib;
  24 
  25 import sun.hotspot.WhiteBox;
  26 
  27 /**
  28  * This application is part of PLAB promotion test.
  29  * The application fills a part of young gen with a number of small objects.
  30  * Then it calls young GC twice to promote objects from eden to survivor, and from survivor to old.
  31  * The test which running the application is responsible to set up test parameters
  32  * and VM flags including flags turning GC logging on. The test will then check the produced log.
  33  */
  34 final public class AppPLABPromotion {
  35 
  36     private final static WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  37 
  38     /**
  39      * AppPLABPromotion is used for testing PLAB promotion.
  40      * Expects the following properties to be set:
  41      * - chunk.size - size of one object (byte array)
  42      * - mem.to.fill - amount of memory to be consumed
  43      * - reachable - memory should be consumed by live or dead objects
  44      *
  45      * @param args
  46      */
  47     public static void main(String[] args) {
  48         long chunkSize = Long.getLong("chunk.size");
  49         long memToFill = Long.getLong("mem.to.fill");
  50         boolean reachable = Boolean.getBoolean("reachable");
  51         if (chunkSize == 0) {
  52             throw new IllegalArgumentException("Chunk size must be not 0");
  53         }
  54         if (memToFill <= 0) {
  55             throw new IllegalArgumentException("mem.to.fill property should be above 0");
  56         }
  57         // Fill requested amount of memory
  58         allocate(reachable, memToFill, chunkSize);
  59         // Promote all allocated objects from Eden to Survivor
  60         WHITE_BOX.youngGC();
  61         // Promote all allocated objects from Survivor to Old
  62         WHITE_BOX.youngGC();
  63     }
  64 
  65     /**
  66      *
  67      * @param reachable - should allocate reachable object
  68      * @param memSize - Memory to fill
  69      * @param chunkSize - requested bytes per objects.
  70      * Actual size of bytes per object will be greater
  71      */
  72     private static void allocate(boolean reachable, long memSize, long chunkSize) {
  73         long realSize = WHITE_BOX.getObjectSize(new byte[(int) chunkSize]);
  74         int items = (int) ((memSize - 1) / (realSize)) + 1;
  75         MemoryConsumer storage;
  76         if (reachable) {
  77             storage = new MemoryConsumer(items, (int) chunkSize);
  78         } else {
  79             storage = new MemoryConsumer(1, (int) chunkSize);
  80         }
  81         // Make all young gen available.
  82         WHITE_BOX.fullGC();
  83         storage.consume(items * chunkSize);
  84     }
  85 }