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 * 48 * @library /lib / 49 * 50 * @build sun.hotspot.WhiteBox 51 * @run main ClassFileInstaller sun.hotspot.WhiteBox 52 * sun.hotspot.WhiteBox$WhiteBoxPermission 53 * @run main/othervm -Xbootclasspath/a:. 54 * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 55 * -XX:CompileOnly=jdk.jfr.event.compiler.TestCodeSweeperStats::dummyMethod 56 * -XX:+SegmentedCodeCache jdk.jfr.event.compiler.TestCodeSweeperStats 57 * @run main/othervm -Xbootclasspath/a:. 58 * -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI 59 * -XX:CompileOnly=jdk.jfr.event.compiler.TestCodeSweeperStats::dummyMethod 60 * -XX:-SegmentedCodeCache jdk.jfr.event.compiler.TestCodeSweeperStats 61 */ 62 public class TestCodeSweeperStats { 63 private static final String EVENT_NAME = EventNames.CodeSweeperStatistics; 64 private static final int WAIT_TIME = 10_000; 65 private static final String CLASS_METHOD_TO_COMPILE = "dummyMethod"; 66 private static final int METHODS_TO_COMPILE = Integer.getInteger("compile.methods.count", 10); 67 private static final int COMP_LEVEL_SIMPLE = 1; 68 private static final int COMP_LEVEL_FULL_OPTIMIZATION = 4; 69 70 public static void main(String[] args) throws Exception { 71 Recording recording = new Recording(); 72 recording.enable(EVENT_NAME).with("period", "endChunk"); 73 recording.start(); 74 compileAndSweep(); 75 recording.stop(); 76 77 List<RecordedEvent> events = Events.fromRecording(recording); 78 Events.hasEvents(events); 79 for (RecordedEvent event : events) { 80 Events.assertField(event, "sweepCount").atLeast(1); 81 Events.assertField(event, "methodReclaimedCount").equal(METHODS_TO_COMPILE); 82 Events.assertField(event, "totalSweepTime").atLeast(0L); 83 Events.assertField(event, "peakFractionTime").atLeast(0L); 84 Events.assertField(event, "peakSweepTime").atLeast(0L); 85 } 86 } 87 88 private static void compileAndSweep() throws InterruptedException { 89 WhiteBox WB = WhiteBox.getWhiteBox(); 90 for (int i = 0; i < METHODS_TO_COMPILE; i++) { 91 System.out.println("compile " + i); 92 compileMethod(); 93 } 94 95 WB.deoptimizeAll(); 96 System.out.println("All methods deoptimized"); 97 98 // method will be sweeped out of code cache after 5 sweep cycles 99 for (int i = 0; i < 5; i++) { 100 WB.fullGC(); 101 WB.forceNMethodSweep(); 102 103 } 104 // now wait for event(s) to be fired 105 Thread.sleep(WAIT_TIME); 106 } 107 108 public void dummyMethod() { 109 System.out.println("Hello World!"); 110 } 111 112 protected static void compileMethod() { 113 ClassLoader current = TestCodeSweeperStats.class.getClassLoader(); 114 String[] cpaths = System.getProperty("test.classes", ".").split(File.pathSeparator); 115 URL[] urls = new URL[cpaths.length]; 116 try { 117 for (int i = 0; i < cpaths.length; i++) { 118 urls[i] = Paths.get(cpaths[i]).toUri().toURL(); 119 } 120 } catch (MalformedURLException e) { 121 throw new Error(e); 122 } 123 124 String currentClassName = TestCodeSweeperStats.class.getName(); 125 FilterClassLoader cl = new FilterClassLoader(new ParentLastURLClassLoader(urls, current), ClassLoader.getSystemClassLoader(), (name) -> currentClassName.equals(name)); 126 Class<?> loadedClass = null; 127 String className = currentClassName; 128 try { 129 loadedClass = cl.loadClass(className); 130 } catch (ClassNotFoundException ex) { 131 throw new Error("Couldn't load class " + className, ex); 132 } 133 try { 134 Method mtd = loadedClass.getMethod(CLASS_METHOD_TO_COMPILE); 135 WhiteBox WB = WhiteBox.getWhiteBox(); 136 WB.testSetDontInlineMethod(mtd, true); 137 String directive = "[{ match: \"" + TestCodeSweeperStats.class.getName().replace('.', '/') 138 + "." + CLASS_METHOD_TO_COMPILE + "\", " + "BackgroundCompilation: false }]"; 139 WB.addCompilerDirective(directive); 140 if (!WB.enqueueMethodForCompilation(mtd, COMP_LEVEL_FULL_OPTIMIZATION)) { 141 WB.enqueueMethodForCompilation(mtd, COMP_LEVEL_SIMPLE); 142 } 143 Utils.waitForCondition(() -> WB.isMethodCompiled(mtd)); 144 } catch (NoSuchMethodException e) { 145 throw new Error("An exception while trying compile method " + e.getMessage(), e); 146 } 147 } 148 }