1 /*
   2  * Copyright (c) 2015, 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.
   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 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderData.hpp"
  27 #include "classfile/moduleEntry.hpp"
  28 #include "classfile/packageEntry.hpp"
  29 #include "jfr/jfrEvents.hpp"
  30 #include "jfr/periodic/jfrModuleEvent.hpp"
  31 #include "jfr/utilities/jfrTime.hpp"
  32 #include "runtime/mutexLocker.hpp"
  33 
  34 // we want all periodic module events to have the same timestamp
  35 static JfrTicks invocation_time;
  36 
  37 typedef void (*EventFunc)(const void* iterated_address, const ModuleEntry* module);
  38 class ModuleEventCallbackClosure : public ModuleClosure {
  39  protected:
  40   const EventFunc _event_func;
  41   ModuleEventCallbackClosure(EventFunc ef) : _event_func(ef) {}
  42 };
  43 
  44 class ModuleDependencyClosure : public ModuleEventCallbackClosure {
  45  private:
  46   const ModuleEntry* const _module;
  47  public:
  48    ModuleDependencyClosure(const ModuleEntry* module, EventFunc ef) : ModuleEventCallbackClosure(ef), _module(module) {}
  49    void do_module(ModuleEntry* entry);
  50 };
  51 
  52 class ModuleExportClosure : public ModuleEventCallbackClosure {
  53  private:
  54   const PackageEntry* const _package;
  55  public:
  56   ModuleExportClosure(const PackageEntry* pkg, EventFunc ef) : ModuleEventCallbackClosure(ef), _package(pkg) {}
  57   void do_module(ModuleEntry* entry);
  58 };
  59 
  60 static void write_module_dependency_event(const void* from_module, const ModuleEntry* to_module) {
  61   EventModuleRequire event(UNTIMED);
  62   event.set_endtime(invocation_time);
  63   event.set_source((const ModuleEntry* const)from_module);
  64   event.set_requiredModule(to_module);
  65   event.commit();
  66 }
  67 
  68 static void write_module_export_event(const void* package, const ModuleEntry* qualified_export) {
  69   EventModuleExport event(UNTIMED);
  70   event.set_endtime(invocation_time);
  71   event.set_exportedPackage((const PackageEntry*)package);
  72   event.set_targetModule(qualified_export);
  73   event.commit();
  74 }
  75 
  76 void ModuleDependencyClosure::do_module(ModuleEntry* to_module) {
  77   assert_locked_or_safepoint(Module_lock);
  78   assert(to_module != NULL, "invariant");
  79   assert(_module != NULL, "invariant");
  80   assert(_event_func != NULL, "invariant");
  81   _event_func(_module, to_module);
  82 }
  83 
  84 void ModuleExportClosure::do_module(ModuleEntry* qualified_export) {
  85   assert_locked_or_safepoint(Module_lock);
  86   assert(qualified_export != NULL, "invariant");
  87   assert(_package != NULL, "invariant");
  88   assert(_event_func != NULL, "invariant");
  89   _event_func(_package, qualified_export);
  90 }
  91 
  92 static void module_dependency_event_callback(ModuleEntry* module) {
  93   assert_locked_or_safepoint(Module_lock);
  94   assert(module != NULL, "invariant");
  95   if (module->has_reads_list()) {
  96     // create an individual event for each directed edge
  97     ModuleDependencyClosure directed_edges(module, &write_module_dependency_event);
  98     module->module_reads_do(&directed_edges);
  99   }
 100 }
 101 
 102 static void module_export_event_callback(PackageEntry* package) {
 103   assert_locked_or_safepoint(Module_lock);
 104   assert(package != NULL, "invariant");
 105   if (package->is_exported()) {
 106     if (package->has_qual_exports_list()) {
 107       // package is qualifiedly exported to a set of modules,
 108       // create an event for each module in the qualified exported list
 109       ModuleExportClosure qexports(package, &write_module_export_event);
 110       package->package_exports_do(&qexports);
 111       return;
 112     }
 113 
 114     assert(!package->is_qual_exported() || package->is_exported_allUnnamed(), "invariant");
 115     // no qualified exports
 116     // only create a single event with NULL
 117     // for the qualified_exports module
 118     write_module_export_event(package, NULL);
 119   }
 120 }
 121 
 122 void JfrModuleEvent::generate_module_dependency_events() {
 123   invocation_time = JfrTicks::now();
 124   MutexLocker cld_lock(ClassLoaderDataGraph_lock);
 125   MutexLocker module_lock(Module_lock);
 126   ClassLoaderDataGraph::modules_do(&module_dependency_event_callback);
 127 }
 128 
 129 void JfrModuleEvent::generate_module_export_events() {
 130   invocation_time = JfrTicks::now();
 131   MutexLocker cld_lock(ClassLoaderDataGraph_lock);
 132   MutexLocker module_lock(Module_lock);
 133   ClassLoaderDataGraph::packages_do(&module_export_event_callback);
 134 }