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