1 /* 2 * Copyright (c) 2014, 2020, 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.inline.hpp" 27 #include "classfile/classLoaderDataGraph.hpp" 28 #include "classfile/classLoaderStats.hpp" 29 #include "memory/metaspace.hpp" 30 #include "oops/objArrayKlass.hpp" 31 #include "oops/oop.inline.hpp" 32 #include "utilities/globalDefinitions.hpp" 33 34 35 class ClassStatsClosure : public KlassClosure { 36 public: 37 int _num_classes; 38 39 ClassStatsClosure() : 40 _num_classes(0) { 41 } 42 43 virtual void do_klass(Klass* k) { 44 _num_classes++; 45 } 46 }; 47 48 void ClassLoaderStatsClosure::do_cld(ClassLoaderData* cld) { 49 oop cl = cld->class_loader(); 50 51 // The hashtable key is the ClassLoader oop since we want to account 52 // for "real" classes and anonymous classes together 53 bool added = false; 54 ClassLoaderStats* cls = _stats->put_if_absent(cl, &added); 55 if (added) { 56 cls->_class_loader = cl; 57 _total_loaders++; 58 } 59 assert(cls->_class_loader == cl, "Sanity"); 60 61 if (!cld->has_class_mirror_holder()) { 62 cls->_cld = cld; 63 } 64 65 if (cl != NULL) { 66 cls->_parent = java_lang_ClassLoader::parent(cl); 67 addEmptyParents(cls->_parent); 68 } 69 70 ClassStatsClosure csc; 71 cld->classes_do(&csc); 72 bool is_hidden = false; 73 if(cld->has_class_mirror_holder()) { 74 // If cld has a class holder then it must be either hidden or unsafe anonymous. 75 // Either way, count it as a hidden class. 76 cls->_hidden_classes_count += csc._num_classes; 77 } else { 78 cls->_classes_count = csc._num_classes; 79 } 80 _total_classes += csc._num_classes; 81 82 ClassLoaderMetaspace* ms = cld->metaspace_or_null(); 83 if (ms != NULL) { 84 size_t used_bytes, capacity_bytes; 85 ms->calculate_jfr_stats(&used_bytes, &capacity_bytes); 86 if(cld->has_class_mirror_holder()) { 87 cls->_hidden_chunk_sz += capacity_bytes; 88 cls->_hidden_block_sz += used_bytes; 89 } else { 90 cls->_chunk_sz = capacity_bytes; 91 cls->_block_sz = used_bytes; 92 } 93 _total_chunk_sz += capacity_bytes; 94 _total_block_sz += used_bytes; 95 } 96 } 97 98 99 // Handles the difference in pointer width on 32 and 64 bit platforms 100 #ifdef _LP64 101 #define SPACE "%8s" 102 #else 103 #define SPACE "%s" 104 #endif 105 106 107 bool ClassLoaderStatsClosure::do_entry(oop const& key, ClassLoaderStats const& cls) { 108 Klass* class_loader_klass = (cls._class_loader == NULL ? NULL : cls._class_loader->klass()); 109 Klass* parent_klass = (cls._parent == NULL ? NULL : cls._parent->klass()); 110 111 _out->print(INTPTR_FORMAT " " INTPTR_FORMAT " " INTPTR_FORMAT " " UINTX_FORMAT_W(6) " " SIZE_FORMAT_W(8) " " SIZE_FORMAT_W(8) " ", 112 p2i(class_loader_klass), p2i(parent_klass), p2i(cls._cld), 113 cls._classes_count, 114 cls._chunk_sz, cls._block_sz); 115 if (class_loader_klass != NULL) { 116 _out->print("%s", class_loader_klass->external_name()); 117 } else { 118 _out->print("<boot class loader>"); 119 } 120 _out->cr(); 121 if (cls._hidden_classes_count > 0) { 122 _out->print_cr(SPACE SPACE SPACE " " UINTX_FORMAT_W(6) " " SIZE_FORMAT_W(8) " " SIZE_FORMAT_W(8) " + hidden classes", 123 "", "", "", 124 cls._hidden_classes_count, 125 cls._hidden_chunk_sz, cls._hidden_block_sz); 126 } 127 return true; 128 } 129 130 131 void ClassLoaderStatsClosure::print() { 132 _out->print_cr("ClassLoader" SPACE " Parent" SPACE " CLD*" SPACE " Classes ChunkSz BlockSz Type", "", "", ""); 133 _stats->iterate(this); 134 _out->print("Total = " UINTX_FORMAT_W(-6), _total_loaders); 135 _out->print(SPACE SPACE SPACE " ", "", "", ""); 136 _out->print_cr(UINTX_FORMAT_W(6) " " SIZE_FORMAT_W(8) " " SIZE_FORMAT_W(8) " ", 137 _total_classes, 138 _total_chunk_sz, 139 _total_block_sz); 140 _out->print_cr("ChunkSz: Total size of all allocated metaspace chunks"); 141 _out->print_cr("BlockSz: Total size of all allocated metaspace blocks (each chunk has several blocks)"); 142 } 143 144 145 void ClassLoaderStatsClosure::addEmptyParents(oop cl) { 146 while (cl != NULL && java_lang_ClassLoader::loader_data_acquire(cl) == NULL) { 147 // This classloader has not loaded any classes 148 bool added = false; 149 ClassLoaderStats* cls = _stats->put_if_absent(cl, &added); 150 if (added) { 151 cls->_class_loader = cl; 152 cls->_parent = java_lang_ClassLoader::parent(cl); 153 _total_loaders++; 154 } 155 assert(cls->_class_loader == cl, "Sanity"); 156 157 cl = java_lang_ClassLoader::parent(cl); 158 } 159 } 160 161 162 void ClassLoaderStatsVMOperation::doit() { 163 ClassLoaderStatsClosure clsc (_out); 164 ClassLoaderDataGraph::loaded_cld_do(&clsc); 165 clsc.print(); 166 } 167 168 169 void ClassLoaderStatsDCmd::execute(DCmdSource source, TRAPS) { 170 ClassLoaderStatsVMOperation op(output()); 171 VMThread::execute(&op); 172 }