8203455: jcmd: VM.metaspace: print loader name for anonymous CLDs
authorstuefe
Wed, 30 May 2018 08:52:59 +0200
changeset 50302 fd2fccf3b079
parent 50301 fe42de5250f3
child 50303 7164c3bb55df
child 56625 380796df44a9
8203455: jcmd: VM.metaspace: print loader name for anonymous CLDs Reviewed-by: lfoltan, goetz
src/hotspot/share/classfile/classLoaderData.hpp
src/hotspot/share/memory/metaspace/printCLDMetaspaceInfoClosure.cpp
test/hotspot/jtreg/runtime/Metaspace/PrintMetaspaceDcmd.java
--- a/src/hotspot/share/classfile/classLoaderData.hpp	Tue May 29 19:15:49 2018 -0700
+++ b/src/hotspot/share/classfile/classLoaderData.hpp	Wed May 30 08:52:59 2018 +0200
@@ -400,9 +400,16 @@
   static ClassLoaderData* class_loader_data_or_null(oop loader);
   static ClassLoaderData* anonymous_class_loader_data(Handle loader);
 
-
+  // Returns Klass* of associated class loader, or NULL if associated loader is <bootstrap>.
+  // Also works if unloading.
   Klass* class_loader_klass() const { return _class_loader_klass; }
+
+  // Returns Name of associated class loader.
+  // Returns NULL if associated class loader is <bootstrap> or if no name has been set for
+  //   this loader.
+  // Also works if unloading.
   Symbol* class_loader_name() const { return _class_loader_name; }
+
   JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
 };
 
--- a/src/hotspot/share/memory/metaspace/printCLDMetaspaceInfoClosure.cpp	Tue May 29 19:15:49 2018 -0700
+++ b/src/hotspot/share/memory/metaspace/printCLDMetaspaceInfoClosure.cpp	Wed May 30 08:52:59 2018 +0200
@@ -22,7 +22,8 @@
  *
  */
 #include "precompiled.hpp"
-#include "classfile/classLoaderData.hpp"
+#include "classfile/classLoaderData.inline.hpp"
+#include "classfile/javaClasses.hpp"
 #include "memory/metaspace/printCLDMetaspaceInfoClosure.hpp"
 #include "memory/resourceArea.hpp"
 #include "runtime/safepoint.hpp"
@@ -63,17 +64,44 @@
 
     _out->print(UINTX_FORMAT_W(4) ": ", _num_loaders);
 
-    if (cld->is_anonymous()) {
-      _out->print("ClassLoaderData " PTR_FORMAT " for anonymous class", p2i(cld));
+    // Print "CLD for [<loader name>,] instance of <loader class name>"
+    // or    "CLD for <anonymous class>, loaded by [<loader name>,] instance of <loader class name>"
+
+    ResourceMark rm;
+    const char* name = NULL;
+    const char* class_name = NULL;
+
+    // Note: this should also work if unloading:
+    Klass* k = cld->class_loader_klass();
+    if (k != NULL) {
+      class_name = k->external_name();
+      Symbol* s = cld->class_loader_name();
+      if (s != NULL) {
+        name = s->as_C_string();
+      }
     } else {
-      ResourceMark rm;
-      _out->print("ClassLoaderData " PTR_FORMAT " for %s", p2i(cld), cld->loader_name());
+      name = "<bootstrap>";
     }
 
+    // Print
+    _out->print("CLD " PTR_FORMAT, p2i(cld));
     if (cld->is_unloading()) {
       _out->print(" (unloading)");
     }
+    _out->print(":");
+    if (cld->is_anonymous()) {
+      _out->print(" <anonymous class>, loaded by");
+    }
+    if (name != NULL) {
+      _out->print(" \"%s\"", name);
+    }
+    if (class_name != NULL) {
+      _out->print(" instance of %s", class_name);
+    }
 
+    _out->cr();
+
+    // Print statistics
     this_cld_stat.print_on(_out, _scale, _break_down_by_chunktype);
     _out->cr();
 
--- a/test/hotspot/jtreg/runtime/Metaspace/PrintMetaspaceDcmd.java	Tue May 29 19:15:49 2018 -0700
+++ b/test/hotspot/jtreg/runtime/Metaspace/PrintMetaspaceDcmd.java	Wed May 30 08:52:59 2018 +0200
@@ -73,7 +73,7 @@
         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "show-loaders"});
         output = new OutputAnalyzer(pb.start());
         output.shouldHaveExitValue(0);
-        output.shouldMatch("ClassLoaderData.*for <bootloader>");
+        output.shouldMatch("CLD.*<bootstrap>");
 
         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.metaspace", "by-chunktype"});
         output = new OutputAnalyzer(pb.start());