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