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