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 import com.oracle.java.testlibrary.Asserts;
  25 import java.lang.management.MemoryPoolMXBean;
  26 import java.util.HashMap;
  27 import java.util.Map;
  28 import sun.hotspot.code.BlobType;
  29 
  30 /*
  31  * @test GetUsageTest
  32  * @library /testlibrary /testlibrary/whitebox
  33  * @build GetUsageTest
  34  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  35  *     sun.hotspot.WhiteBox$WhiteBoxPermission
  36  * @run main/othervm -Xbootclasspath/a:. -XX:CompileCommand=compileonly,null::*
  37  *     -XX:-UseCodeCacheFlushing -XX:-MethodFlushing -XX:+SegmentedCodeCache
  38  *     -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI GetUsageTest
  39  * @summary testing of getUsage() for segmented code cache
  40  */
  41 public class GetUsageTest {
  42 
  43     private final BlobType btype;
  44     private final int allocateSize;
  45 
  46     public GetUsageTest(BlobType btype, int allocSize) {
  47         this.btype = btype;
  48         this.allocateSize = allocSize;
  49     }
  50 
  51     public static void main(String[] args) throws Exception {
  52         for (BlobType btype : BlobType.getAvailable()) {
  53             if (CodeCacheUtils.isCodeHeapPredictable(btype)) {
  54                 for (int allocSize = 10; allocSize < 100000; allocSize *= 10) {
  55                     new GetUsageTest(btype, allocSize).runTest();
  56                 }
  57             }
  58         }
  59     }
  60 
  61     protected final Map<MemoryPoolMXBean, Long> getBeanUsages() {
  62         Map<MemoryPoolMXBean, Long> beanUsages = new HashMap<>();
  63         for (BlobType bt : BlobType.getAvailable()) {
  64             beanUsages.put(bt.getMemoryPool(),
  65                     bt.getMemoryPool().getUsage().getUsed());
  66         }
  67         return beanUsages;
  68     }
  69 
  70     protected void runTest() {
  71         MemoryPoolMXBean[] predictableBeans = BlobType.getAvailable().stream()
  72                 .filter(CodeCacheUtils::isCodeHeapPredictable)
  73                 .map(BlobType::getMemoryPool)
  74                 .toArray(MemoryPoolMXBean[]::new);
  75         CodeCacheUtils.WB.deoptimizeAll();
  76         Map<MemoryPoolMXBean, Long> initial = getBeanUsages();
  77         CodeCacheUtils.WB.allocateCodeBlob(allocateSize, btype.id);
  78         Map<MemoryPoolMXBean, Long> current = getBeanUsages();
  79         long blockCount = Math.floorDiv(allocateSize
  80                 + CodeCacheUtils.getHeaderSize(btype)
  81                 + CodeCacheUtils.SEGMENT_SIZE - 1, CodeCacheUtils.SEGMENT_SIZE);
  82         long usageUpperEstimate = Math.max(blockCount,
  83                 CodeCacheUtils.MIN_BLOCK_LENGTH) * CodeCacheUtils.SEGMENT_SIZE;
  84         for (MemoryPoolMXBean entry : predictableBeans) {
  85             long diff = current.get(entry) - initial.get(entry);
  86             if (entry.equals(btype.getMemoryPool())) {
  87                 Asserts.assertFalse(diff <= 0L || diff > usageUpperEstimate,
  88                         String.format("Pool %s usage increase was reported "
  89                                 + "unexpectedly as increased by %d using "
  90                                 + "allocation size %d", entry.getName(),
  91                                 diff, allocateSize));
  92             } else {
  93                 Asserts.assertEQ(diff, 0L,
  94                         String.format("Pool %s usage changed unexpectedly while"
  95                                 + " trying to increase: %s using allocation "
  96                                 + "size %d", entry.getName(),
  97                                 btype.getMemoryPool().getName(), allocateSize));
  98             }
  99         }
 100         System.out.printf("INFO: Scenario finished successfully for %s%n",
 101                 btype.getMemoryPool().getName());
 102     }
 103 }