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 "oops/objArrayKlass.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->has_class_mirror_holder()) {
  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   bool is_hidden = false;
  76   if(cld->has_class_mirror_holder()) {
  77     // If cld has a class holder then it must be either hidden or unsafe anonymous.
  78     // Either way, count it as a hidden class.
  79     cls->_hidden_classes_count += csc._num_classes;
  80   } else {
  81     cls->_classes_count = csc._num_classes;
  82   }
  83   _total_classes += csc._num_classes;
  84 
  85   ClassLoaderMetaspace* ms = cld->metaspace_or_null();
  86   if (ms != NULL) {
  87     if(cld->has_class_mirror_holder()) {
  88       cls->_hidden_chunk_sz += ms->allocated_chunks_bytes();
  89       cls->_hidden_block_sz += ms->allocated_blocks_bytes();
  90     } else {
  91       cls->_chunk_sz = ms->allocated_chunks_bytes();
  92       cls->_block_sz = ms->allocated_blocks_bytes();
  93     }
  94     _total_chunk_sz += ms->allocated_chunks_bytes();
  95     _total_block_sz += ms->allocated_blocks_bytes();
  96   }
  97 }
  98 
  99 
 100 // Handles the difference in pointer width on 32 and 64 bit platforms
 101 #ifdef _LP64
 102   #define SPACE "%8s"
 103 #else
 104   #define SPACE "%s"
 105 #endif
 106 
 107 
 108 bool ClassLoaderStatsClosure::do_entry(oop const& key, ClassLoaderStats* const& cls) {
 109   Klass* class_loader_klass = (cls->_class_loader == NULL ? NULL : cls->_class_loader->klass());
 110   Klass* parent_klass = (cls->_parent == NULL ? NULL : cls->_parent->klass());
 111 
 112   _out->print(INTPTR_FORMAT "  " INTPTR_FORMAT "  " INTPTR_FORMAT "  " UINTX_FORMAT_W(6) "  " SIZE_FORMAT_W(8) "  " SIZE_FORMAT_W(8) "  ",
 113       p2i(class_loader_klass), p2i(parent_klass), p2i(cls->_cld),
 114       cls->_classes_count,
 115       cls->_chunk_sz, cls->_block_sz);
 116   if (class_loader_klass != NULL) {
 117     _out->print("%s", class_loader_klass->external_name());
 118   } else {
 119     _out->print("<boot class loader>");
 120   }
 121   _out->cr();
 122   if (cls->_hidden_classes_count > 0) {
 123     _out->print_cr(SPACE SPACE SPACE "                                    " UINTX_FORMAT_W(6) "  " SIZE_FORMAT_W(8) "  " SIZE_FORMAT_W(8) "   + hidden classes",
 124         "", "", "",
 125         cls->_hidden_classes_count,
 126         cls->_hidden_chunk_sz, cls->_hidden_block_sz);
 127   }
 128   return true;
 129 }
 130 
 131 
 132 void ClassLoaderStatsClosure::print() {
 133   _out->print_cr("ClassLoader" SPACE " Parent" SPACE "      CLD*" SPACE "       Classes   ChunkSz   BlockSz  Type", "", "", "");
 134   _stats->iterate(this);
 135   _out->print("Total = " UINTX_FORMAT_W(-6), _total_loaders);
 136   _out->print(SPACE SPACE SPACE "                      ", "", "", "");
 137   _out->print_cr(UINTX_FORMAT_W(6) "  " SIZE_FORMAT_W(8) "  " SIZE_FORMAT_W(8) "  ",
 138       _total_classes,
 139       _total_chunk_sz,
 140       _total_block_sz);
 141   _out->print_cr("ChunkSz: Total size of all allocated metaspace chunks");
 142   _out->print_cr("BlockSz: Total size of all allocated metaspace blocks (each chunk has several blocks)");
 143 }
 144 
 145 
 146 void ClassLoaderStatsClosure::addEmptyParents(oop cl) {
 147   while (cl != NULL && java_lang_ClassLoader::loader_data_acquire(cl) == NULL) {
 148     // This classloader has not loaded any classes
 149     ClassLoaderStats** cls_ptr = _stats->get(cl);
 150     if (cls_ptr == NULL) {
 151       // It does not exist in our table - add it
 152       ClassLoaderStats* cls = new ClassLoaderStats();
 153       cls->_class_loader = cl;
 154       cls->_parent = java_lang_ClassLoader::parent(cl);
 155       _stats->put(cl, cls);
 156       _total_loaders++;
 157     }
 158 
 159     cl = java_lang_ClassLoader::parent(cl);
 160   }
 161 }
 162 
 163 
 164 void ClassLoaderStatsVMOperation::doit() {
 165   ClassLoaderStatsClosure clsc (_out);
 166   ClassLoaderDataGraph::loaded_cld_do(&clsc);
 167   clsc.print();
 168 }
 169 
 170 
 171 void ClassLoaderStatsDCmd::execute(DCmdSource source, TRAPS) {
 172   ClassLoaderStatsVMOperation op(output());
 173   VMThread::execute(&op);
 174 }