1 /*
   2  * Copyright (c) 2014, 2015, 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.lang.reflect.Method;
  26 import java.util.EnumSet;
  27 
  28 import sun.hotspot.WhiteBox;
  29 import sun.hotspot.code.BlobType;
  30 
  31 import jdk.test.lib.Asserts;
  32 import jdk.test.lib.InfiniteLoop;
  33 import compiler.whitebox.CompilerWhiteBoxTest;
  34 
  35 /*
  36  * @test
  37  * @bug 8059624 8064669
  38  * @library /testlibrary /test/lib /
  39  * @modules java.management
  40  * @build ForceNMethodSweepTest
  41  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  42  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  43  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  44  *                   -XX:-TieredCompilation -XX:+WhiteBoxAPI
  45  *                   -XX:CompileCommand=compileonly,compiler.whitebox.SimpleTestCase$Helper::*
  46  *                   -XX:-BackgroundCompilation ForceNMethodSweepTest
  47  * @summary testing of WB::forceNMethodSweep
  48  */
  49 public class ForceNMethodSweepTest extends CompilerWhiteBoxTest {
  50     public static void main(String[] args) throws Exception {
  51         CompilerWhiteBoxTest.main(ForceNMethodSweepTest::new, args);
  52     }
  53     private final EnumSet<BlobType> blobTypes;
  54     private ForceNMethodSweepTest(TestCase testCase) {
  55         super(testCase);
  56         // to prevent inlining of #method
  57         WHITE_BOX.testSetDontInlineMethod(method, true);
  58         blobTypes = BlobType.getAvailable();
  59     }
  60 
  61     @Override
  62     protected void test() throws Exception {
  63         checkNotCompiled();
  64         guaranteedSweep();
  65         int usage = getTotalUsage();
  66 
  67         compile();
  68         checkCompiled();
  69         int afterCompilation = getTotalUsage();
  70         Asserts.assertGT(afterCompilation, usage,
  71                 "compilation should increase usage");
  72 
  73         guaranteedSweep();
  74         int afterSweep = getTotalUsage();
  75         Asserts.assertLTE(afterSweep, afterCompilation,
  76                 "sweep shouldn't increase usage");
  77 
  78         deoptimize();
  79         guaranteedSweep();
  80         int afterDeoptAndSweep = getTotalUsage();
  81         Asserts.assertLT(afterDeoptAndSweep, afterSweep,
  82                 "sweep after deoptimization should decrease usage");
  83      }
  84 
  85     private int getTotalUsage() {
  86         int usage = 0;
  87         for (BlobType type : blobTypes) {
  88            usage += type.getMemoryPool().getUsage().getUsed();
  89         }
  90         return usage;
  91     }
  92     private void guaranteedSweep() {
  93         // not entrant -> ++stack_traversal_mark -> zombie -> flushed
  94         for (int i = 0; i < 5; ++i) {
  95             WHITE_BOX.fullGC();
  96             WHITE_BOX.forceNMethodSweep();
  97         }
  98     }
  99 }