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  */
43 public class TestSyncOnValueBasedClassEvent {
44     static final String EVENT_NAME = EventNames.SyncOnValueBasedClass;
45     static String[] classesWanted = {"java/lang/Character", "java/lang/Boolean", "java/lang/Byte", "java/lang/Short",
46                                      "java/lang/Integer", "java/lang/Long", "java/lang/Float", "java/lang/Double",
47                                      "java/time/Duration", "java/util/OptionalInt", "java/lang/Runtime$Version"};
48     static List<Object> testObjects = new ArrayList<Object>();
49     static Integer counter = 0;
50 
51     private static void initTestObjects() {
52         testObjects.add(Character.valueOf('H'));
53         testObjects.add(Boolean.valueOf(true));
54         testObjects.add(Byte.valueOf((byte)0x40));
55         testObjects.add(Short.valueOf((short)0x4000));
56         testObjects.add(Integer.valueOf(0x40000000));
57         testObjects.add(Long.valueOf(0x4000000000000000L));
58         testObjects.add(Float.valueOf(1.20f));
59         testObjects.add(Double.valueOf(1.2345));
60         testObjects.add(Duration.ofMillis(5));
61         testObjects.add(OptionalInt.of(10));
62         testObjects.add(Runtime.version());
63     }
64 
65     public static void main(String[] args) throws Throwable {
66         initTestObjects();
67         Recording recording = new Recording();
68         recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(0));
69         recording.start();
70         for (Object obj : testObjects) {
71             synchronized (obj) {
72                 counter++;
73             }
74         }
75         recording.stop();
76 
77         List<String> classesFound = new ArrayList<String>();
78         List<RecordedEvent> events = Events.fromRecording(recording);
79         Events.hasEvents(events);
80         for (RecordedEvent event : events) {
81             String className = Events.assertField(event, "valueBasedClass.name").notEmpty().getValue();
82             RecordedThread jt = event.getThread();
83             if (Thread.currentThread().getName().equals(jt.getJavaName())) {
84                 classesFound.add(className);
85             }
86         }
87         for (String classWanted : classesWanted) {
88             if (!classesFound.contains(classWanted)) {
89                 throw new AssertionError("No matching event SyncOnValueBasedClass with \"valueBasedClass=" + classWanted + "\" and current thread as caller");
90             }
91         }
92         if (classesFound.size() != classesWanted.length) {
93             throw new AssertionError("Invalid number of SyncOnValueBasedClass events for current thread");
94         }
95     }
96 }