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 
  34 /*
  35  * @test
  36  * @bug 8059624 8064669
  37  * @library /testlibrary /../../test/lib
  38  * @modules java.management
  39  * @build ForceNMethodSweepTest
  40  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  41  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  42  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  43  *                   -XX:-TieredCompilation -XX:+WhiteBoxAPI
  44  *                   -XX:CompileCommand=compileonly,SimpleTestCase$Helper::*
  45  *                   -XX:-BackgroundCompilation ForceNMethodSweepTest
  46  * @summary testing of WB::forceNMethodSweep
  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         checkNotCompiled();
  63         guaranteedSweep();
  64         int usage = getTotalUsage();
  65 
  66         compile();
  67         checkCompiled();
  68         int afterCompilation = getTotalUsage();
  69         Asserts.assertGT(afterCompilation, usage,
  70                 "compilation should increase usage");
  71 
  72         guaranteedSweep();
  73         int afterSweep = getTotalUsage();
  74         Asserts.assertLTE(afterSweep, afterCompilation,
  75                 "sweep shouldn't increase usage");
  76 
  77         deoptimize();
  78         guaranteedSweep();
  79         int afterDeoptAndSweep = getTotalUsage();
  80         Asserts.assertLT(afterDeoptAndSweep, afterSweep,
  81                 "sweep after deoptimization should decrease usage");
  82      }
  83 
  84     private int getTotalUsage() {
  85         int usage = 0;
  86         for (BlobType type : blobTypes) {
  87            usage += type.getMemoryPool().getUsage().getUsed();
  88         }
  89         return usage;
  90     }
  91     private void guaranteedSweep() {
  92         // not entrant -> ++stack_traversal_mark -> zombie -> reclamation -> flushed
  93         for (int i = 0; i < 5; ++i) {
  94             WHITE_BOX.fullGC();
  95             WHITE_BOX.forceNMethodSweep();
  96         }
  97     }
  98 }