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_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_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_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 ClassLoaderData* class_loader_data = (cls->_cld == NULL ? ClassLoaderData::the_null_class_loader_data() : cls->_cld); 106 Klass* parent_klass = (cls->_parent == NULL ? NULL : cls->_parent->klass()); 107 108 _out->print(INTPTR_FORMAT " " INTPTR_FORMAT " " INTPTR_FORMAT " " UINTX_FORMAT_W(6) " " SIZE_FORMAT_W(8) " " SIZE_FORMAT_W(8) " ", 109 p2i(class_loader_klass), p2i(parent_klass), p2i(cls->_cld), 110 cls->_classes_count, 111 cls->_chunk_sz, cls->_block_sz); 112 _out->print("%s", class_loader_data->loader_name_and_id()); 113 _out->cr(); 114 if (cls->_anon_classes_count > 0) { 115 _out->print_cr(SPACE SPACE SPACE " " UINTX_FORMAT_W(6) " " SIZE_FORMAT_W(8) " " SIZE_FORMAT_W(8) " + unsafe anonymous classes", 116 "", "", "", 117 cls->_anon_classes_count, 118 cls->_anon_chunk_sz, cls->_anon_block_sz); 119 } 120 return true; 121 } 122 123 124 void ClassLoaderStatsClosure::print() { 125 _out->print_cr("ClassLoader" SPACE " Parent" SPACE " CLD*" SPACE " Classes ChunkSz BlockSz Type", "", "", ""); 126 _stats->iterate(this); 127 _out->print("Total = " UINTX_FORMAT_W(-6), _total_loaders); 128 _out->print(SPACE SPACE SPACE " ", "", "", ""); 129 _out->print_cr(UINTX_FORMAT_W(6) " " SIZE_FORMAT_W(8) " " SIZE_FORMAT_W(8) " ", 130 _total_classes, 131 _total_chunk_sz, 132 _total_block_sz); 133 _out->print_cr("ChunkSz: Total size of all allocated metaspace chunks"); 134 _out->print_cr("BlockSz: Total size of all allocated metaspace blocks (each chunk has several blocks)"); 135 } 136 137 138 void ClassLoaderStatsClosure::addEmptyParents(oop cl) { 139 while (cl != NULL && java_lang_ClassLoader::loader_data(cl) == NULL) { 140 // This classloader has not loaded any classes 141 ClassLoaderStats** cls_ptr = _stats->get(cl); 142 if (cls_ptr == NULL) { 143 // It does not exist in our table - add it 144 ClassLoaderStats* cls = new ClassLoaderStats(); 145 cls->_class_loader = cl; 146 cls->_parent = java_lang_ClassLoader::parent(cl); 147 _stats->put(cl, cls); 148 _total_loaders++; 149 } 150 151 cl = java_lang_ClassLoader::parent(cl); 152 } 153 } 154 155 156 void ClassLoaderStatsVMOperation::doit() { 157 ClassLoaderStatsClosure clsc (_out); 158 ClassLoaderDataGraph::cld_do(&clsc); 159 clsc.print(); 160 } 161 162 163 void ClassLoaderStatsDCmd::execute(DCmdSource source, TRAPS) { 164 ClassLoaderStatsVMOperation op(output()); 165 VMThread::execute(&op); 166 }