1+ /*
2+ * Copyright (c) 2019, 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 .util .List ;
29+ import java .util .stream .Collectors ;
30+
31+ import jdk .jfr .Recording ;
32+ import jdk .jfr .consumer .RecordedEvent ;
33+ import jdk .test .lib .Asserts ;
34+ import jdk .test .lib .jfr .EventNames ;
35+ import jdk .test .lib .jfr .Events ;
36+
37+ // THIS TEST IS LINE NUMBER SENSITIVE
38+
39+ // Careful if moving this class or method somewhere since verifyDeoptimizationEventFields asserts the linenumber
40+ class Dummy {
41+ static void dummyMethod (boolean b ) {
42+ if (b ) {
43+ return ;
44+ }
45+ }
46+ }
47+
48+ /**
49+ * @test
50+ * @key jfr
51+ * @summary sanity test for Deoptimization event, depends on Compilation event
52+ * @requires vm.hasJFR
53+ * @requires vm.compMode != "Xint"
54+ * @requires vm.flavor == "server" & (vm.opt.TieredStopAtLevel == 4 | vm.opt.TieredStopAtLevel == null)
55+ * @library /test/lib
56+ * @run main/othervm -XX:-BackgroundCompilation jdk.jfr.event.compiler.TestDeoptimization
57+ */
58+ public class TestDeoptimization {
59+ private final static String TYPE_NAME = Dummy .class .getName ().replace ("." , "/" );
60+ private final static String METHOD_NAME = "dummyMethod" ;
61+ private static final String METHOD_DESCRIPTOR = "(Z)V" ;
62+ private static final String COMPILER = "c2" ;
63+
64+ public static void main (String [] args ) throws Throwable {
65+ new TestDeoptimization ().doTest ();
66+ }
67+
68+ public void doTest () throws Throwable {
69+ Recording recording = new Recording ();
70+ recording .enable (EventNames .Deoptimization );
71+ recording .enable (EventNames .Compilation );
72+ recording .start ();
73+
74+ long start = System .currentTimeMillis ();
75+ // compile dummyMethod
76+ for (int i = 0 ; i < 20000 ; i ++) {
77+ Dummy .dummyMethod (false );
78+ }
79+ // provoke deoptimization by executing the uncommon trap in dummyMethod
80+ Dummy .dummyMethod (true );
81+ System .out .println ("Time to load, compile and deoptimize dummyMethod: " + (System .currentTimeMillis () - start ));
82+ recording .stop ();
83+
84+ List <RecordedEvent > events = Events .fromRecording (recording );
85+ Events .hasEvents (events );
86+
87+ // get compile ids for all compilations of dummyMethod
88+ List <Integer > compileIds = events .stream ()
89+ .filter (e -> e .getEventType ().getName ().equals (EventNames .Compilation ))
90+ .filter (TestDeoptimization ::isForDummyMethod )
91+ .map (e -> Events .assertField (e , "compileId" ).<Integer >getValue ())
92+ .collect (Collectors .toList ());
93+ Asserts .assertFalse (compileIds .isEmpty (),
94+ "couldn't find any " + EventNames .Compilation + " for " + METHOD_NAME );
95+
96+ // get all deoptimization events associated with the compile ids
97+ List <RecordedEvent > deoptEventsForCompileIds = events .stream ()
98+ .filter (e -> e .getEventType ().getName ().equals (EventNames .Deoptimization ))
99+ .filter (e -> compileIds .contains (Events .assertField (e , "compileId" ).<Integer >getValue ()))
100+ .collect (Collectors .toList ());
101+ Asserts .assertFalse (deoptEventsForCompileIds .isEmpty (),
102+ "couldn't find any " + EventNames .Deoptimization + " for ids : " + compileIds );
103+
104+ // verify deoptimization event fields
105+ deoptEventsForCompileIds .forEach (this ::verifyDeoptimizationEventFields );
106+ }
107+
108+ static boolean isForDummyMethod (RecordedEvent e ) {
109+ return TYPE_NAME .equals (Events .assertField (e , "method.type.name" ).getValue ())
110+ && METHOD_NAME .equals (Events .assertField (e , "method.name" ).getValue ())
111+ && METHOD_DESCRIPTOR .equals (Events .assertField (e , "method.descriptor" ).getValue ());
112+ }
113+
114+ private void verifyDeoptimizationEventFields (RecordedEvent event ) {
115+ Events .assertEventThread (event );
116+ Events .assertField (event , "compileId" ).atLeast (0 );
117+ Events .assertField (event , "compiler" ).equal (COMPILER );
118+ Events .assertField (event , "lineNumber" ).equal (42 );
119+ Events .assertField (event , "bci" ).equal (1 );
120+ Events .assertField (event , "instruction" ).equal ("ifeq" );
121+ Events .assertField (event , "action" ).notEmpty ().equal ("reinterpret" );
122+ Events .assertField (event , "reason" ).notEmpty ().equal ("unstable_if" );
123+ }
124+ }
0 commit comments