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