1 /*
   2  * Copyright (c) 2014, 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 
  24 /*
  25  * @test InitialAndMaxUsageTest
  26  * @summary testing of initial and max usage
  27  * @modules java.base/jdk.internal.misc
  28  *          java.management
  29  * @library /testlibrary /test/lib /
  30  *
  31  * @build compiler.codecache.jmx.InitialAndMaxUsageTest
  32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  33  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  34  * @run main/othervm -Xbootclasspath/a:. -XX:-UseCodeCacheFlushing
  35  *     -XX:-MethodFlushing -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  36  *     -XX:CompileCommand=compileonly,null::* -XX:-UseLargePages
  37  *     -XX:+SegmentedCodeCache
  38  *     compiler.codecache.jmx.InitialAndMaxUsageTest
  39  */
  40 
  41 package compiler.codecache.jmx;
  42 
  43 import jdk.test.lib.Asserts;
  44 import sun.hotspot.code.BlobType;
  45 
  46 import java.lang.management.MemoryPoolMXBean;
  47 import java.util.ArrayList;
  48 import java.util.List;
  49 
  50 public class InitialAndMaxUsageTest {
  51 
  52     private static final double CACHE_USAGE_COEF = 0.95d;
  53     private final BlobType btype;
  54     private final boolean lowerBoundIsZero;
  55     private final long maxSize;
  56 
  57     public InitialAndMaxUsageTest(BlobType btype) {
  58         this.btype = btype;
  59         this.maxSize = btype.getSize();
  60         /* Only profiled code cache initial size should be 0, because of
  61          -XX:CompileCommand=compileonly,null::* non-methods might be not empty,
  62          as well as non-profiled methods, because it's used as fallback in
  63          case non-methods is full */
  64         lowerBoundIsZero = btype == BlobType.MethodProfiled;
  65     }
  66 
  67     public static void main(String[] args) {
  68         for (BlobType btype : BlobType.getAvailable()) {
  69             new InitialAndMaxUsageTest(btype).runTest();
  70         }
  71     }
  72 
  73     private void fillWithSize(long size, List<Long> blobs) {
  74         long blob;
  75         while ((blob = CodeCacheUtils.WB.allocateCodeBlob(size, btype.id))
  76                 != 0L) {
  77             blobs.add(blob);
  78         }
  79     }
  80 
  81     protected void runTest() {
  82         long headerSize = CodeCacheUtils.getHeaderSize(btype);
  83         MemoryPoolMXBean bean = btype.getMemoryPool();
  84         long initialUsage = btype.getMemoryPool().getUsage().getUsed();
  85         System.out.printf("INFO: trying to test %s of max size %d and initial"
  86                 + " usage %d%n", bean.getName(), maxSize, initialUsage);
  87         Asserts.assertLT(initialUsage + headerSize + 1L, maxSize,
  88                 "Initial usage is close to total size for " + bean.getName());
  89         if (lowerBoundIsZero) {
  90             Asserts.assertEQ(initialUsage, 0L, "Unexpected initial usage");
  91         }
  92         ArrayList<Long> blobs = new ArrayList<>();
  93         long minAllocationUnit = Math.max(0, CodeCacheUtils.MIN_ALLOCATION - headerSize);
  94         /* now filling code cache with large-sized allocation first, since
  95          lots of small allocations takes too much time, so, just a small
  96          optimization */
  97         try {
  98             for (int coef = 1000000; coef > 0; coef /= 10) {
  99                 fillWithSize(coef * minAllocationUnit, blobs);
 100             }
 101             Asserts.assertGT((double) bean.getUsage().getUsed(),
 102                     CACHE_USAGE_COEF * maxSize, String.format("Unable to fill "
 103                             + "more than %f of %s. Reported usage is %d ",
 104                             CACHE_USAGE_COEF, bean.getName(),
 105                             bean.getUsage().getUsed()));
 106         } finally {
 107             for (long entry : blobs) {
 108                 CodeCacheUtils.WB.freeCodeBlob(entry);
 109             }
 110         }
 111         System.out.printf("INFO: Scenario finished successfully for %s%n",
 112                 bean.getName());
 113     }
 114 
 115 }