1 /*
2  * Copyright (c) 2016, 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 package jdk.jfr.event.runtime;
26 
27 import static jdk.test.lib.Asserts.assertEquals;
28 
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 
33 import jdk.jfr.Recording;
34 import jdk.jfr.consumer.RecordedEvent;
35 import jdk.jfr.consumer.RecordedObject;
36 import jdk.test.lib.jfr.EventNames;
37 import jdk.test.lib.jfr.Events;
38 
39 /**
40  * @test
41  * @summary Tests the JFR events related to modules
42  * @key jfr
43  * @requires vm.hasJFR
44  * @requires !vm.graal.enabled
45  * @library /test/lib
46  * @run main/othervm --limit-modules java.base,jdk.jfr jdk.jfr.event.runtime.TestModuleEvents
47  */
48 public final class TestModuleEvents {
49 
50     private static final String MODULE_EXPORT_EVENT_NAME = EventNames.ModuleExport;
51     private static final String MODULE_REQUIRE_EVENT_NAME = EventNames.ModuleRequire;
52     private static final String UNNAMED = "<unnamed>";
53 
main(String[] args)54     public static void main(String[] args) throws Throwable {
55         verifyRequiredModules();
56         verifyExportedModules();
57     }
58 
verifyRequiredModules()59     private static void verifyRequiredModules() throws Throwable {
60         Recording recording = new Recording();
61         recording.enable(MODULE_REQUIRE_EVENT_NAME);
62 
63         recording.start();
64         recording.stop();
65 
66         List<RecordedEvent> events = Events.fromRecording(recording);
67         assertDependency(events, "jdk.jfr", "java.base"); // jdk.jfr requires java.base (by default)
68         assertDependency(events, "java.base", "jdk.jfr"); // java.base requires jdk.jfr for JDK events, i.e. FileRead
69 
70         recording.close();
71     }
72 
assertDependency(List<RecordedEvent> events, String source, String required)73     private static void assertDependency(List<RecordedEvent> events, String source, String required) throws Exception {
74         for (RecordedEvent e : events) {
75             String sourceModule = e.getValue("source.name");
76             if (source.equals(sourceModule)) {
77                 RecordedObject module = e.getValue("requiredModule");
78                 if (module != null) {
79                     if (required.equals(module.getValue("name"))) {
80                         return;
81                     }
82                 }
83             }
84         }
85         throw new Exception("Could not find module dependency between " + source + " and requires modeule "+ required);
86     }
87 
verifyExportedModules()88     private static void verifyExportedModules() throws Throwable {
89         Recording recording = new Recording();
90         recording.enable(MODULE_EXPORT_EVENT_NAME);
91         recording.start();
92         recording.stop();
93 
94         Map<String, String> edges = new HashMap<>();
95 
96         List<RecordedEvent> events = Events.fromRecording(recording);
97         events.stream().forEach((ev) -> {
98             String exportedPackage = getValue(ev.getValue("exportedPackage"), "name", UNNAMED);
99             String toModule = getValue(ev.getValue("targetModule"), "name", UNNAMED);
100             if (!toModule.equals("jdk.proxy1")) { // ignore jdk.proxy1 module
101                 edges.put(exportedPackage, toModule);
102             }
103         });
104 
105         // We expect
106         // 1) jdk.jfr -> <unnamed> (because we use the package)
107         // 2) java.util -> <unnamed> (because we use the package)
108         // 3) jdk.jfr.events -> java.base (from the jfr design)
109         // 4) jdk.internal -> jdk.jfr (from the jfr design)
110         // Where 'a -> b' means "package 'a' exported to module 'b'"
111         assertEquals(edges.get("jdk/jfr"), UNNAMED);
112         assertEquals(edges.get("java/util"), UNNAMED);
113         assertEquals(edges.get("jdk/jfr/events"), "java.base");
114         assertEquals(edges.get("jdk/internal"), "jdk.jfr");
115 
116         recording.close();
117     }
118 
119     // Helper function to get field from a RecordedObject
getValue(RecordedObject ro, String field, String defVal)120     private static String getValue(RecordedObject ro, String field, String defVal) {
121         if (ro != null && ro.getValue(field) != null) {
122             return ro.getValue(field);
123         } else {
124             return defVal;
125         }
126     }
127 }
128