1 /*
   2  * Copyright (c) 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 #include "precompiled.hpp"
  25 #include "classfile/classLoaderData.inline.hpp"
  26 #include "classfile/javaClasses.hpp"
  27 #include "memory/metaspace/printCLDMetaspaceInfoClosure.hpp"
  28 #include "memory/metaspace/printMetaspaceInfoKlassClosure.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "runtime/safepoint.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 #include "utilities/ostream.hpp"
  33 
  34 
  35 namespace metaspace {
  36 
  37 PrintCLDMetaspaceInfoClosure::PrintCLDMetaspaceInfoClosure(outputStream* out, size_t scale, bool do_print,
  38     bool do_print_classes, bool break_down_by_chunktype)
  39 : _out(out), _scale(scale), _do_print(do_print), _do_print_classes(do_print_classes)
  40 , _break_down_by_chunktype(break_down_by_chunktype)
  41 , _num_loaders(0), _num_loaders_unloading(0), _num_loaders_without_metaspace(0)
  42 {
  43   memset(_num_loaders_by_spacetype, 0, sizeof(_num_loaders_by_spacetype));
  44 }
  45 
  46 static const char* classes_plural(uintx num) {
  47   return num == 1 ? "" : "es";
  48 }
  49 
  50 void PrintCLDMetaspaceInfoClosure::do_cld(ClassLoaderData* cld) {
  51 
  52   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
  53 
  54   if (cld->is_unloading()) {
  55     _num_loaders_unloading ++;
  56     return;
  57   }
  58 
  59   ClassLoaderMetaspace* msp = cld->metaspace_or_null();
  60   if (msp == NULL) {
  61     _num_loaders_without_metaspace ++;
  62     return;
  63   }
  64 
  65   // Collect statistics for this class loader metaspace
  66   ClassLoaderMetaspaceStatistics this_cld_stat;
  67   msp->add_to_statistics(&this_cld_stat);
  68 
  69   // And add it to the running totals
  70   _stats_total.add(this_cld_stat);
  71   _num_loaders ++;
  72   _stats_by_spacetype[msp->space_type()].add(this_cld_stat);
  73   _num_loaders_by_spacetype[msp->space_type()] ++;
  74 
  75   // Optionally, print.
  76   if (_do_print) {
  77 
  78     _out->print(UINTX_FORMAT_W(4) ": ", _num_loaders);
  79 
  80     // Print "CLD for [<loader name>,] instance of <loader class name>"
  81     // or    "CLD for <anonymous class>, loaded by [<loader name>,] instance of <loader class name>"
  82 
  83     ResourceMark rm;
  84     const char* name = NULL;
  85     const char* class_name = NULL;
  86 
  87     // Note: this should also work if unloading:
  88     Klass* k = cld->class_loader_klass();
  89     if (k != NULL) {
  90       class_name = k->external_name();
  91       Symbol* s = cld->name();
  92       if (s != NULL) {
  93         name = s->as_C_string();
  94       }
  95     } else {
  96       name = "<bootstrap>";
  97     }
  98 
  99     // Print
 100     _out->print("CLD " PTR_FORMAT, p2i(cld));
 101     if (cld->is_unloading()) {
 102       _out->print(" (unloading)");
 103     }
 104     _out->print(":");
 105     if (cld->is_anonymous()) {
 106       _out->print(" <anonymous class>, loaded by");
 107     }
 108     if (name != NULL) {
 109       _out->print(" \"%s\"", name);
 110     }
 111     if (class_name != NULL) {
 112       _out->print(" instance of %s", class_name);
 113     }
 114 
 115     if (_do_print_classes) {
 116       streamIndentor sti(_out, 6);
 117       _out->cr_indent();
 118       _out->print("Loaded classes: ");
 119       PrintMetaspaceInfoKlassClosure pkic(_out, true);
 120       cld->classes_do(&pkic);
 121       _out->cr_indent();
 122       _out->print("-total-: ");
 123       _out->print(UINTX_FORMAT " class%s", pkic._num_classes, classes_plural(pkic._num_classes));
 124       if (pkic._num_instance_classes > 0 || pkic._num_array_classes > 0) {
 125         _out->print(" (");
 126         if (pkic._num_instance_classes > 0) {
 127           _out->print(UINTX_FORMAT " instance class%s", pkic._num_instance_classes,
 128               classes_plural(pkic._num_instance_classes));
 129         }
 130         if (pkic._num_array_classes > 0) {
 131           if (pkic._num_instance_classes > 0) {
 132             _out->print(", ");
 133           }
 134           _out->print(UINTX_FORMAT " array class%s", pkic._num_array_classes,
 135               classes_plural(pkic._num_array_classes));
 136         }
 137         _out->print(").");
 138       }
 139     }
 140 
 141     _out->cr();
 142 
 143 
 144     _out->cr();
 145 
 146     // Print statistics
 147     this_cld_stat.print_on(_out, _scale, _break_down_by_chunktype);
 148     _out->cr();
 149 
 150   }
 151 
 152 }
 153 
 154 } // namespace metaspace
 155