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 PeakUsageTest
  26  * @library /test/lib /
  27  * @modules java.base/jdk.internal.misc
  28  *          java.management
  29  *
  30  * @build sun.hotspot.WhiteBox
  31  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  32  *     sun.hotspot.WhiteBox$WhiteBoxPermission
  33  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  34  *     -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*
  35  *     -XX:+SegmentedCodeCache
  36  *     compiler.codecache.jmx.PeakUsageTest
  37  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  38  *     -XX:+WhiteBoxAPI -XX:CompileCommand=compileonly,null::*
  39  *     -XX:-SegmentedCodeCache
  40  *     compiler.codecache.jmx.PeakUsageTest
  41  * @summary testing of getPeakUsage() and resetPeakUsage for
  42  *     segmented code cache
  43  */
  44 
  45 package compiler.codecache.jmx;
  46 
  47 import sun.hotspot.code.BlobType;
  48 
  49 import java.lang.management.MemoryPoolMXBean;
  50 
  51 
  52 public class PeakUsageTest {
  53 
  54     private final BlobType btype;
  55 
  56     public PeakUsageTest(BlobType btype) {
  57         this.btype = btype;
  58     }
  59 
  60     public static void main(String[] args) {
  61         for (BlobType btype : BlobType.getAvailable()) {
  62             new PeakUsageTest(btype).runTest();
  63         }
  64     }
  65 
  66     protected void runTest() {
  67         MemoryPoolMXBean bean = btype.getMemoryPool();
  68         bean.resetPeakUsage();
  69         long addr = CodeCacheUtils.WB.allocateCodeBlob(
  70                 CodeCacheUtils.ALLOCATION_SIZE, btype.id);
  71 
  72         try {
  73             /*
  74             Always save peakUsage after saving currentUsage. Reversing the order
  75             can lead to inconsistent results (currentUsage > peakUsage) because
  76             of intermediate allocations.
  77             */
  78             long currUsage = bean.getUsage().getUsed();
  79             long peakUsage = bean.getPeakUsage().getUsed();
  80             CodeCacheUtils.assertEQorLTE(btype, currUsage,
  81                     peakUsage,
  82                     "Peak usage does not match usage after allocation for "
  83                     + bean.getName());
  84         } finally {
  85             if (addr != 0) {
  86                 CodeCacheUtils.WB.freeCodeBlob(addr);
  87             }
  88         }
  89         bean.resetPeakUsage();
  90         long currUsage = bean.getUsage().getUsed();
  91         long peakUsage = bean.getPeakUsage().getUsed();
  92         CodeCacheUtils.assertEQorLTE(btype, currUsage,
  93                 peakUsage,
  94                 "Code cache peak usage is not equal to usage after reset for "
  95                 + bean.getName());
  96         long addr2 = CodeCacheUtils.WB.allocateCodeBlob(
  97                 CodeCacheUtils.ALLOCATION_SIZE, btype.id);
  98         try {
  99             currUsage = bean.getUsage().getUsed();
 100             peakUsage = bean.getPeakUsage().getUsed();
 101 
 102             CodeCacheUtils.assertEQorLTE(btype, currUsage,
 103                     peakUsage,
 104                     "Code cache peak usage is not equal to usage after fresh "
 105                     + "allocation for " + bean.getName());
 106         } finally {
 107             if (addr2 != 0) {
 108                 CodeCacheUtils.WB.freeCodeBlob(addr2);
 109             }
 110         }
 111         System.out.printf("INFO: Scenario finished successfully for %s%n",
 112                 bean.getName());
 113     }
 114 }