1 /*
   2  * Copyright (c) 2014, 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  */
  24 
  25 import java.util.ArrayList;
  26 
  27 import sun.hotspot.code.BlobType;
  28 
  29 /*
  30  * @test RandomAllocationTest
  31  * @library /testlibrary /testlibrary/whitebox
  32  * @build RandomAllocationTest
  33  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  34  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  35  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  36  *                   -XX:CompileCommand=dontinline,Helper$TestCase::method
  37  *                   -XX:+WhiteBoxAPI -XX:-SegmentedCodeCache RandomAllocationTest
  38  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  39  *                   -XX:CompileCommand=dontinline,Helper$TestCase::method
  40  *                   -XX:+WhiteBoxAPI -XX:+SegmentedCodeCache RandomAllocationTest
  41  * @summary stressing code cache by allocating randomly sized "dummy" code blobs
  42  */
  43 public class RandomAllocationTest implements Runnable {
  44     private static final long CODE_CACHE_SIZE
  45             = Helper.WB.getUintxVMFlag("ReservedCodeCacheSize");
  46     private static final int MAX_BLOB_SIZE = (int) (CODE_CACHE_SIZE >> 7);
  47     private static final BlobType[] BLOB_TYPES
  48             = BlobType.getAvailable().toArray(new BlobType[0]);
  49 
  50     public static void main(String[] args) {
  51         new CodeCacheStressRunner(new RandomAllocationTest()).runTest();
  52     }
  53 
  54     private final ArrayList<Long> blobs = new ArrayList<>();
  55     @Override
  56     public void run() {
  57         boolean allocate = blobs.isEmpty() || Helper.R.nextBoolean();
  58         if (allocate) {
  59             int type = Helper.R.nextInt(BLOB_TYPES.length);
  60             long addr = Helper.WB.allocateCodeBlob(
  61                     Helper.R.nextInt(MAX_BLOB_SIZE), BLOB_TYPES[type].id);
  62             if (addr != 0) {
  63                 blobs.add(addr);
  64             }
  65         } else {
  66             int index = Helper.R.nextInt(blobs.size());
  67             Helper.WB.freeCodeBlob(blobs.remove(index));
  68         }
  69     }
  70 
  71 }