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 jdk.test.lib.Platform;
  26 import sun.hotspot.WhiteBox;
  27 
  28 /**
  29  * An simple application a part of PLAB Resize test to be executed in the VM under tests.
  30  * The application allocates objects in 3 iterations:
  31  * 1. Objects of fixed size
  32  * 2. Objects of decreasing size
  33  * 3. Objects of increasing size
  34  * The application doesn't have any assumptions about expected behavior.
  35  * It's supposed to be executed a test which suppose to setup test parameters (object sizes, number of allocations, etc)
  36  * and VM flags including flags turning GC logging on. The test will then check the produced log.
  37  */
  38 final public class AppPLABResize {
  39 
  40     // Memory to be promoted by PLAB for one thread.
  41     private static final long MEM_ALLOC_WORDS = 32768;
  42     // Defined by properties.
  43     private static final int ITERATIONS = Integer.getInteger("iterations");
  44     private static final long CHUNK = Long.getLong("chunk.size");
  45     private static final int THREADS = Integer.getInteger("threads");
  46 
  47     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  48 
  49     /**
  50      * Main method for AppPLABResizing. Application expect for next properties:
  51      * iterations, chunk.size and threads.
  52      *
  53      * @param args
  54      */
  55     public static void main(String[] args) {
  56 
  57         if (ITERATIONS == 0 || CHUNK == 0 || THREADS == 0) {
  58             throw new IllegalArgumentException("Properties should be set");
  59         }
  60 
  61         long wordSize = Platform.is32bit() ? 4l : 8l;
  62         // PLAB size is shared between threads.
  63         long initialMemorySize = wordSize * THREADS * MEM_ALLOC_WORDS;
  64 
  65         // Expect changing memory to half during all iterations.
  66         long memChangeStep = initialMemorySize / 2 / ITERATIONS;
  67 
  68         WHITE_BOX.fullGC();
  69 
  70         // Warm the PLAB. Fill memory ITERATIONS times without changing memory size.
  71         iterateAllocation(initialMemorySize, 0);
  72         // Fill memory ITERATIONS times.
  73         // Initial size is initialMemorySize and step is -memChangeStep
  74         iterateAllocation(initialMemorySize, -memChangeStep);
  75         // Fill memory ITERATIONS times.
  76         // Initial size is memoryAfterChanging, step is memChangeStep.
  77         // Memory size at start should be greater then last size on previous step.
  78         // Last size on previous step is initialMemorySize - memChangeStep*(ITERATIONS - 1)
  79         long memoryAfterChanging = initialMemorySize - memChangeStep * (ITERATIONS - 2);
  80         iterateAllocation(memoryAfterChanging, memChangeStep);
  81     }
  82 
  83     private static void iterateAllocation(long memoryToFill, long change) {
  84         int items;
  85         if (change > 0) {
  86             items = (int) ((memoryToFill + change * ITERATIONS) / CHUNK) + 1;
  87         } else {
  88             items = (int) (memoryToFill / CHUNK) + 1;
  89         }
  90 
  91         long currentMemToFill = memoryToFill;
  92         for (int iteration = 0; iteration < ITERATIONS; ++iteration) {
  93 
  94             Storage storage = new Storage(items);
  95             allocateYoung(storage, currentMemToFill);
  96             // Promote all objects to survivor
  97             WHITE_BOX.youngGC();
  98             storage.clear();
  99             currentMemToFill += change;
 100         }
 101     }
 102 
 103     private static void allocateYoung(Storage storage, long memoryToFill) {
 104         long allocated = 0;
 105         while (allocated < memoryToFill) {
 106             storage.store(new byte[(int) CHUNK]);
 107             allocated += CHUNK;
 108         }
 109     }
 110 }