1 /*
   2  * Copyright (c) 2013, 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.event.compiler;
  27 
  28 import java.io.File;
  29 import java.lang.reflect.Method;
  30 import java.net.MalformedURLException;
  31 import java.net.URL;
  32 import java.nio.file.Paths;
  33 import java.util.List;
  34 
  35 import sun.hotspot.WhiteBox;
  36 import jdk.jfr.Recording;
  37 import jdk.jfr.consumer.RecordedEvent;
  38 import jdk.test.lib.classloader.FilterClassLoader;
  39 import jdk.test.lib.classloader.ParentLastURLClassLoader;
  40 import jdk.test.lib.jfr.EventNames;
  41 import jdk.test.lib.jfr.Events;
  42 import jdk.test.lib.Utils;
  43 
  44 /*
  45  * @test TestCodeSweeperStats
  46  * @key jfr
  47  * @library /test/lib
  48  * @requires vm.compMode!="Xint"
  49  * @build sun.hotspot.WhiteBox
  50  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  51  *     sun.hotspot.WhiteBox$WhiteBoxPermission
  52  * @run main/othervm -Xbootclasspath/a:.
  53  *     -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  54  *     -XX:CompileOnly=jdk.jfr.event.compiler.TestCodeSweeperStats::dummyMethod
  55  *     -XX:+SegmentedCodeCache jdk.jfr.event.compiler.TestCodeSweeperStats
  56  * @run main/othervm -Xbootclasspath/a:.
  57  *     -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  58  *     -XX:CompileOnly=jdk.jfr.event.compiler.TestCodeSweeperStats::dummyMethod
  59  *     -XX:-SegmentedCodeCache jdk.jfr.event.compiler.TestCodeSweeperStats
  60  */
  61 public class TestCodeSweeperStats {
  62     private static final String EVENT_NAME = EventNames.CodeSweeperStatistics;
  63     private static final int WAIT_TIME = 10_000;
  64     private static final String CLASS_METHOD_TO_COMPILE = "dummyMethod";
  65     private static final int METHODS_TO_COMPILE = Integer.getInteger("compile.methods.count", 10);
  66     private static final int COMP_LEVEL_SIMPLE = 1;
  67     private static final int COMP_LEVEL_FULL_OPTIMIZATION = 4;
  68 
  69     public static void main(String[] args) throws Exception {
  70         Recording recording = new Recording();
  71         recording.enable(EVENT_NAME).with("period", "endChunk");
  72         recording.start();
  73         compileAndSweep();
  74         recording.stop();
  75 
  76         List<RecordedEvent> events = Events.fromRecording(recording);
  77         Events.hasEvents(events);
  78         for (RecordedEvent event : events) {
  79             Events.assertField(event, "sweepCount").atLeast(1);
  80             Events.assertField(event, "methodReclaimedCount").equal(METHODS_TO_COMPILE);
  81             Events.assertField(event, "totalSweepTime").atLeast(0L);
  82             Events.assertField(event, "peakFractionTime").atLeast(0L);
  83             Events.assertField(event, "peakSweepTime").atLeast(0L);
  84         }
  85     }
  86 
  87     private static void compileAndSweep() throws InterruptedException {
  88         WhiteBox WB = WhiteBox.getWhiteBox();
  89         for (int i = 0; i < METHODS_TO_COMPILE; i++) {
  90             System.out.println("compile " + i);
  91             compileMethod();
  92         }
  93 
  94         WB.deoptimizeAll();
  95         System.out.println("All methods deoptimized");
  96 
  97         // method will be sweeped out of code cache after 5 sweep cycles
  98         for (int i = 0; i < 5; i++) {
  99             WB.fullGC();
 100             WB.forceNMethodSweep();
 101 
 102         }
 103         // now wait for event(s) to be fired
 104         Thread.sleep(WAIT_TIME);
 105     }
 106 
 107     public void dummyMethod() {
 108         System.out.println("Hello World!");
 109     }
 110 
 111     protected static void compileMethod() {
 112         ClassLoader current = TestCodeSweeperStats.class.getClassLoader();
 113         String[] cpaths = System.getProperty("test.classes", ".").split(File.pathSeparator);
 114         URL[] urls = new URL[cpaths.length];
 115         try {
 116             for (int i = 0; i < cpaths.length; i++) {
 117                 urls[i] = Paths.get(cpaths[i]).toUri().toURL();
 118             }
 119         } catch (MalformedURLException e) {
 120             throw new Error(e);
 121         }
 122 
 123         String currentClassName = TestCodeSweeperStats.class.getName();
 124         FilterClassLoader cl = new FilterClassLoader(new ParentLastURLClassLoader(urls, current), ClassLoader.getSystemClassLoader(), (name) -> currentClassName.equals(name));
 125         Class<?> loadedClass = null;
 126         String className = currentClassName;
 127         try {
 128             loadedClass = cl.loadClass(className);
 129         } catch (ClassNotFoundException ex) {
 130             throw new Error("Couldn't load class " + className, ex);
 131         }
 132         try {
 133             Method mtd = loadedClass.getMethod(CLASS_METHOD_TO_COMPILE);
 134             WhiteBox WB = WhiteBox.getWhiteBox();
 135             WB.testSetDontInlineMethod(mtd, true);
 136             String directive = "[{ match: \"" + TestCodeSweeperStats.class.getName().replace('.', '/')
 137                     + "." + CLASS_METHOD_TO_COMPILE + "\", " + "BackgroundCompilation: false }]";
 138             WB.addCompilerDirective(directive);
 139             if (!WB.enqueueMethodForCompilation(mtd, COMP_LEVEL_FULL_OPTIMIZATION)) {
 140                 WB.enqueueMethodForCompilation(mtd, COMP_LEVEL_SIMPLE);
 141             }
 142             Utils.waitForCondition(() -> WB.isMethodCompiled(mtd));
 143         } catch (NoSuchMethodException e) {
 144             throw new Error("An exception while trying compile method " + e.getMessage(), e);
 145         }
 146     }
 147 }