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 jdk.test.lib.Platform;
  26 import sun.hotspot.WhiteBox;
  27 
  28 /**
  29  * This application is part of PLAB Resize test.
  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 by a test which should set up 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  * Expects the following properties to be set:
  39  * - iterations - amount of iteration per cycle.
  40  * - chunk.size - size of objects to be allocated
  41  * - threads - number of gc threads (-XX:ParallelGCThreads) to calculate PLAB sizes.
  42  */
  43 final public class AppPLABResize {
  44 
  45     // Memory to be promoted by PLAB for one thread.
  46     private static final long MEM_ALLOC_WORDS = 32768;
  47     // Defined by properties.
  48     private static final int ITERATIONS = Integer.getInteger("iterations");
  49     private static final long CHUNK = Long.getLong("chunk.size");
  50     private static final int GC_THREADS = Integer.getInteger("threads");
  51 
  52     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  53 
  54     /**
  55      * Main method for AppPLABResizing. Application expect for next properties:
  56      * iterations, chunk.size and threads.
  57      *
  58      * @param args
  59      */
  60     public static void main(String[] args) {
  61 
  62         if (ITERATIONS == 0 || CHUNK == 0 || GC_THREADS == 0) {
  63             throw new IllegalArgumentException("Properties should be set");
  64         }
  65 
  66         long wordSize = Platform.is32bit() ? 4l : 8l;
  67         // PLAB size is shared between threads.
  68         long initialMemorySize = wordSize * GC_THREADS * MEM_ALLOC_WORDS;
  69 
  70         // Expect changing memory to half during all iterations.
  71         long memChangeStep = initialMemorySize / 2 / ITERATIONS;
  72 
  73         WHITE_BOX.fullGC();
  74 
  75         // Warm the PLAB. Fill memory ITERATIONS times without changing memory size.
  76         iterateAllocation(initialMemorySize, 0);
  77         // Fill memory ITERATIONS times.
  78         // Initial size is initialMemorySize and step is -memChangeStep
  79         iterateAllocation(initialMemorySize, -memChangeStep);
  80         // Fill memory ITERATIONS times.
  81         // Initial size is memoryAfterChanging, step is memChangeStep.
  82         // Memory size at start should be greater then last size on previous step.
  83         // Last size on previous step is initialMemorySize - memChangeStep*(ITERATIONS - 1)
  84         long memoryAfterChanging = initialMemorySize - memChangeStep * (ITERATIONS - 2);
  85         iterateAllocation(memoryAfterChanging, memChangeStep);
  86     }
  87 
  88     private static void iterateAllocation(long memoryToFill, long change) {
  89         int items;
  90         if (change > 0) {
  91             items = (int) ((memoryToFill + change * ITERATIONS) / CHUNK) + 1;
  92         } else {
  93             items = (int) (memoryToFill / CHUNK) + 1;
  94         }
  95 
  96         long currentMemToFill = memoryToFill;
  97         for (int iteration = 0; iteration < ITERATIONS; ++iteration) {
  98 
  99             MemoryConsumer storage = new MemoryConsumer(items, (int) CHUNK);
 100             storage.consume(currentMemToFill);
 101             // Promote all objects to survivor
 102             WHITE_BOX.youngGC();
 103             storage.clear();
 104             currentMemToFill += change;
 105         }
 106     }
 107 
 108 }