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