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/classLoaderMetaspace.hpp" 28 #include "memory/metaspace/printCLDMetaspaceInfoClosure.hpp" 29 #include "memory/metaspace/printMetaspaceInfoKlassClosure.hpp" 30 #include "memory/metaspace/metaspaceCommon.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "runtime/safepoint.hpp" 33 #include "utilities/globalDefinitions.hpp" 34 #include "utilities/ostream.hpp" 35 36 37 namespace metaspace { 38 39 PrintCLDMetaspaceInfoClosure::PrintCLDMetaspaceInfoClosure(outputStream* out, size_t scale, bool do_print, 40 bool do_print_classes, bool break_down_by_chunktype) 41 : _out(out), _scale(scale), _do_print(do_print), _do_print_classes(do_print_classes) 42 , _break_down_by_chunktype(break_down_by_chunktype) 43 , _num_loaders(0), _num_loaders_without_metaspace(0), _num_loaders_unloading(0) 44 , _num_classes(0), _num_classes_shared(0) 45 { 46 memset(_num_loaders_by_spacetype, 0, sizeof(_num_loaders_by_spacetype)); 47 memset(_num_classes_by_spacetype, 0, sizeof(_num_classes_by_spacetype)); 48 memset(_num_classes_shared_by_spacetype, 0, sizeof(_num_classes_shared_by_spacetype)); 49 } 50 51 // A closure just to count classes 52 class CountKlassClosure : public KlassClosure { 53 public: 54 55 uintx _num_classes; 56 uintx _num_classes_shared; 57 58 CountKlassClosure() : _num_classes(0), _num_classes_shared(0) {} 59 void do_klass(Klass* k) { 60 _num_classes ++; 61 if (k->is_shared()) { 62 _num_classes_shared ++; 63 } 64 } 65 66 }; // end: PrintKlassInfoClosure 67 68 void PrintCLDMetaspaceInfoClosure::do_cld(ClassLoaderData* cld) { 69 70 assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint"); 71 72 if (cld->is_unloading()) { 73 _num_loaders_unloading ++; 74 return; 75 } 76 77 ClassLoaderMetaspace* msp = cld->metaspace_or_null(); 78 if (msp == NULL) { 79 _num_loaders_without_metaspace ++; 80 return; 81 } 82 83 // Collect statistics for this class loader metaspace 84 ClassLoaderMetaspaceStatistics this_cld_stat; 85 msp->add_to_statistics(&this_cld_stat); 86 87 // And add it to the running totals 88 _stats_total.add(this_cld_stat); 89 _num_loaders ++; 90 _stats_by_spacetype[msp->space_type()].add(this_cld_stat); 91 _num_loaders_by_spacetype[msp->space_type()] ++; 92 93 // Count classes loaded by this CLD. 94 CountKlassClosure ckc; 95 cld->classes_do(&ckc); 96 // accumulate. 97 _num_classes += ckc._num_classes; 98 _num_classes_by_spacetype[msp->space_type()] += ckc._num_classes; 99 _num_classes_shared += ckc._num_classes_shared; 100 _num_classes_shared_by_spacetype[msp->space_type()] += ckc._num_classes_shared; 101 102 // Optionally, print 103 if (_do_print) { 104 105 _out->print(UINTX_FORMAT_W(4) ": ", _num_loaders); 106 107 // Print "CLD for [<loader name>,] instance of <loader class name>" 108 // or "CLD for <anonymous class>, loaded by [<loader name>,] instance of <loader class name>" 109 110 ResourceMark rm; 111 const char* name = NULL; 112 const char* class_name = NULL; 113 114 // Note: this should also work if unloading: 115 Klass* k = cld->class_loader_klass(); 116 if (k != NULL) { 117 class_name = k->external_name(); 118 Symbol* s = cld->name(); 119 if (s != NULL) { 120 name = s->as_C_string(); 121 } 122 } else { 123 name = "<bootstrap>"; 124 } 125 126 // Print 127 _out->print("CLD " PTR_FORMAT, p2i(cld)); 128 if (cld->is_unloading()) { 129 _out->print(" (unloading)"); 130 } 131 _out->print(":"); 132 if (cld->is_unsafe_anonymous()) { 133 _out->print(" <anonymous class>, loaded by"); 134 } 135 if (name != NULL) { 136 _out->print(" \"%s\"", name); 137 } 138 if (class_name != NULL) { 139 _out->print(" instance of %s", class_name); 140 } 141 142 if (_do_print_classes) { 143 // Print a detailed description of all loaded classes. 144 streamIndentor sti(_out, 6); 145 _out->cr_indent(); 146 _out->print("Loaded classes"); 147 if (ckc._num_classes_shared > 0) { 148 _out->print("('s' = shared)"); 149 } 150 _out->print(":"); 151 PrintMetaspaceInfoKlassClosure pkic(_out, true); 152 cld->classes_do(&pkic); 153 _out->cr_indent(); 154 _out->print("-total-: "); 155 print_number_of_classes(_out, ckc._num_classes, ckc._num_classes_shared); 156 } else { 157 // Just print a summary about how many classes have been loaded. 158 _out->print(", "); 159 print_number_of_classes(_out, ckc._num_classes, ckc._num_classes_shared); 160 } 161 162 // Print statistics 163 this_cld_stat.print_on(_out, _scale, _break_down_by_chunktype); 164 _out->cr(); 165 166 } 167 168 } 169 170 } // namespace metaspace 171