1 /*
 2  * Copyright (c) 2020, 2023, 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 package jdk.jfr.event.runtime;
25 
26 import java.time.Duration;
27 import java.util.*;
28 
29 import jdk.jfr.Recording;
30 import jdk.jfr.consumer.RecordedEvent;
31 import jdk.jfr.consumer.RecordedThread;
32 import jdk.test.lib.jfr.EventNames;
33 import jdk.test.lib.jfr.Events;
34 
35 /**
36  * @test
37  * @bug 8242263
38  * @requires vm.hasJFR
39  * @key jfr
40  * @library /test/lib
41  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:DiagnoseSyncOnValueBasedClasses=2 jdk.jfr.event.runtime.TestSyncOnValueBasedClassEvent
42  * @run main/othervm --enable-preview -XX:+UnlockDiagnosticVMOptions -XX:DiagnoseSyncOnValueBasedClasses=2 jdk.jfr.event.runtime.TestSyncOnValueBasedClassEvent
43  */
44 public class TestSyncOnValueBasedClassEvent {
45     static final String EVENT_NAME = EventNames.SyncOnValueBasedClass;
46     static String[] classesWanted = {"java/lang/Character", "java/lang/Boolean", "java/lang/Byte", "java/lang/Short",
47                                      "java/lang/Integer", "java/lang/Long", "java/lang/Float", "java/lang/Double",
48                                      "java/lang/Runtime$Version"};
49     static List<Object> testObjects = new ArrayList<Object>();
50     static Integer counter = 0;
51 
52     private static void initTestObjects() {
53         testObjects.add(Character.valueOf('H'));
54         testObjects.add(Boolean.valueOf(true));
55         testObjects.add(Byte.valueOf((byte)0x40));
56         testObjects.add(Short.valueOf((short)0x4000));
57         testObjects.add(Integer.valueOf(0x40000000));
58         testObjects.add(Long.valueOf(0x4000000000000000L));
59         testObjects.add(Float.valueOf(1.20f));
60         testObjects.add(Double.valueOf(1.2345));
61         testObjects.add(Runtime.version());
62     }
63 
64     public static void main(String[] args) throws Throwable {
65         initTestObjects();
66         Recording recording = new Recording();
67         recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(0));
68         recording.start();
69         for (Object obj : testObjects) {
70             synchronized (obj) {
71                 counter++;
72             }
73         }
74         recording.stop();
75 
76         List<String> classesFound = new ArrayList<String>();
77         List<RecordedEvent> events = Events.fromRecording(recording);
78         Events.hasEvents(events);
79         for (RecordedEvent event : events) {
80             String className = Events.assertField(event, "valueBasedClass.name").notEmpty().getValue();
81             RecordedThread jt = event.getThread();
82             if (Thread.currentThread().getName().equals(jt.getJavaName())) {
83                 classesFound.add(className);
84             }
85         }
86         for (String classWanted : classesWanted) {
87             if (!classesFound.contains(classWanted)) {
88                 throw new AssertionError("No matching event SyncOnValueBasedClass with \"valueBasedClass=" + classWanted + "\" and current thread as caller");
89             }
90         }
91         if (classesFound.size() != classesWanted.length) {
92             throw new AssertionError("Invalid number of SyncOnValueBasedClass events for current thread");
93         }
94     }
95 }