1 /*
   2  * Copyright (c) 2015, 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  * A simple application a part of PLAB no resize test.
  29  * The application fills a part of young get up 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  * Then it prints out the statistics on allocated objections.
  32  * The test running the application is responsible to setup test parameters (object sizes, number of allocations, etc)
  33  * and VM flags including flags turning GC logging on. The test will then check the produced log.
  34  */
  35 final public class AppPLABFixedSize {
  36 
  37     private final static WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  38 
  39     /**
  40      * AppPLABFixedSize for testing fixed size PLAB.
  41      * Expects next properties: chunk.size, allocator.type, reachable
  42      *
  43      * @param args
  44      */
  45     public static void main(String[] args) {
  46         long chunkSize = Long.getLong("chunk.size");
  47         long memToFill = Long.getLong("mem.to.fill");
  48         boolean reachable = Boolean.getBoolean("reachable");
  49         if (chunkSize == 0) {
  50             throw new IllegalArgumentException("Chunk size must be not 0");
  51         }
  52         // Fill requested amount of memory
  53         allocate(reachable, memToFill, chunkSize);
  54         // Promote all allocated objects from Eden to Survivor
  55         WHITE_BOX.youngGC();
  56         // Promote all allocated objects from Survivor to Old
  57         WHITE_BOX.youngGC();
  58     }
  59 
  60     /**
  61      *
  62      * @param reachable - should allocate reachable object
  63      * @param memSize - Memory to fill
  64      * @param chunkSize - requested bytes per objects.
  65      * Actual size of bytes per object will be greater
  66      */
  67     private static void allocate(boolean reachable, long memSize, long chunkSize) {
  68         long realSize = WHITE_BOX.getObjectSize(new byte[(int) chunkSize]);
  69         int items = (int) ((memSize - 1) / (realSize)) + 1;
  70         Storage storage;
  71         if (reachable) {
  72             storage = new Storage(items);
  73         } else {
  74             storage = new Storage(1);
  75         }
  76         // Make all young gen available.
  77         WHITE_BOX.fullGC();
  78         long allocated = 0;
  79         // Expect no GC during allocation.
  80         while (allocated < memSize) {
  81             storage.store(new byte[(int) chunkSize]);
  82             allocated += realSize;
  83         }
  84     }
  85 }