1 /*
  2  * Copyright (c) 2014, 2026, 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 "classfile/classLoaderData.inline.hpp"
 26 #include "classfile/classLoaderDataGraph.hpp"
 27 #include "classfile/classLoaderStats.hpp"
 28 #include "memory/classLoaderMetaspace.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   // Some klasses should not be reported by ClassStatistics
 36   bool exclude_klass(Klass* k) const {
 37     // Direct instances of ObjArrayKlass represent the Java types that Java code can see.
 38     // RefArrayKlass/FlatArrayKlass describe different implementations of the arrays, filter them out.
 39     if (k->is_objArray_klass() && k->kind() != Klass::KlassKind::ObjArrayKlassKind) {
 40       return true;
 41     }
 42     return false;
 43   }
 44 
 45 public:
 46   int _num_classes;
 47 
 48   ClassStatsClosure() :
 49     _num_classes(0) {
 50   }
 51 
 52   virtual void do_klass(Klass* k) {
 53     if (!exclude_klass(k)) {
 54       _num_classes++;
 55     }
 56   }
 57 
 58 };
 59 
 60 void ClassLoaderStatsClosure::do_cld(ClassLoaderData* cld) {
 61   // Class loaders are not kept alive so this closure must only be
 62   // used during a safepoint.
 63   assert_at_safepoint();
 64   oop cl = cld->class_loader_no_keepalive();
 65 
 66   // The hashtable key is the ClassLoader oop since we want to account
 67   // for "real" classes and hidden classes together
 68   bool added = false;
 69   ClassLoaderStats* cls = _stats->put_if_absent(cl, &added);
 70   if (added) {
 71     cls->_class_loader = cl;
 72     _total_loaders++;
 73   }
 74   assert(cls->_class_loader == cl, "Sanity");
 75 
 76   if (!cld->has_class_mirror_holder()) {
 77     cls->_cld = cld;
 78   }
 79 
 80   if (cl != nullptr) {
 81     cls->_parent = java_lang_ClassLoader::parent_no_keepalive(cl);
 82     addEmptyParents(cls->_parent);
 83   }
 84 
 85   ClassStatsClosure csc;
 86   cld->classes_do(&csc);
 87   bool is_hidden = false;
 88   if(cld->has_class_mirror_holder()) {
 89     // If cld has a class holder then it must be hidden.
 90     // Either way, count it as a hidden class.
 91     cls->_hidden_classes_count += csc._num_classes;
 92   } else {
 93     cls->_classes_count = csc._num_classes;
 94   }
 95   _total_classes += csc._num_classes;
 96 
 97   ClassLoaderMetaspace* ms = cld->metaspace_or_null();
 98   if (ms != nullptr) {
 99     size_t used_words, capacity_words;
100     ms->usage_numbers(&used_words, nullptr, &capacity_words);
101     size_t used_bytes = used_words * BytesPerWord;
102     size_t capacity_bytes = capacity_words * BytesPerWord;
103     if(cld->has_class_mirror_holder()) {
104       cls->_hidden_chunk_sz += capacity_bytes;
105       cls->_hidden_block_sz += used_bytes;
106     } else {
107       cls->_chunk_sz = capacity_bytes;
108       cls->_block_sz = used_bytes;
109     }
110     _total_chunk_sz += capacity_bytes;
111     _total_block_sz += used_bytes;
112   }
113 }
114 
115 // Handles the difference in pointer width on 32 and 64 bit platforms
116 #ifdef _LP64
117   #define SPACE "%8s"
118 #else
119   #define SPACE "%s"
120 #endif
121 
122 
123 bool ClassLoaderStatsClosure::do_entry(oop const& key, ClassLoaderStats const& cls) {
124   Klass* class_loader_klass = (cls._class_loader == nullptr ? nullptr : cls._class_loader->klass());
125   Klass* parent_klass = (cls._parent == nullptr ? nullptr : cls._parent->klass());
126 
127   _out->print(INTPTR_FORMAT "  " INTPTR_FORMAT "  " INTPTR_FORMAT "  %6zu  %8zu  %8zu  ",
128       p2i(class_loader_klass), p2i(parent_klass), p2i(cls._cld),
129       cls._classes_count,
130       cls._chunk_sz, cls._block_sz);
131   if (class_loader_klass != nullptr) {
132     _out->print("%s", class_loader_klass->external_name());
133   } else {
134     _out->print("<boot class loader>");
135   }
136   _out->cr();
137   if (cls._hidden_classes_count > 0) {
138     _out->print_cr(SPACE SPACE SPACE "                                    %6zu  %8zu  %8zu   + hidden classes",
139         "", "", "",
140         cls._hidden_classes_count,
141         cls._hidden_chunk_sz, cls._hidden_block_sz);
142   }
143   return true;
144 }
145 
146 
147 void ClassLoaderStatsClosure::print() {
148   _out->print_cr("ClassLoader" SPACE " Parent" SPACE "      CLD*" SPACE "       Classes   ChunkSz   BlockSz  Type", "", "", "");
149   _stats->iterate(this);
150   _out->print("Total = %-6zu", _total_loaders);
151   _out->print(SPACE SPACE SPACE "                      ", "", "", "");
152   _out->print_cr("%6zu  %8zu  %8zu  ",
153       _total_classes,
154       _total_chunk_sz,
155       _total_block_sz);
156   _out->print_cr("ChunkSz: Total size of all allocated metaspace chunks");
157   _out->print_cr("BlockSz: Total size of all allocated metaspace blocks (each chunk has several blocks)");
158 }
159 
160 
161 void ClassLoaderStatsClosure::addEmptyParents(oop cl) {
162   while (cl != nullptr && java_lang_ClassLoader::loader_data_acquire(cl) == nullptr) {
163     // This classloader has not loaded any classes
164     bool added = false;
165     ClassLoaderStats* cls = _stats->put_if_absent(cl, &added);
166     if (added) {
167       cls->_class_loader = cl;
168       cls->_parent = java_lang_ClassLoader::parent_no_keepalive(cl);
169       _total_loaders++;
170     }
171     assert(cls->_class_loader == cl, "Sanity");
172 
173     cl = java_lang_ClassLoader::parent_no_keepalive(cl);
174   }
175 }
176 
177 
178 void ClassLoaderStatsVMOperation::doit() {
179   ClassLoaderStatsClosure clsc (_out);
180   ClassLoaderDataGraph::loaded_cld_do(&clsc);
181   clsc.print();
182 }
183 
184 
185 void ClassLoaderStatsDCmd::execute(DCmdSource source, TRAPS) {
186   ClassLoaderStatsVMOperation op(output());
187   VMThread::execute(&op);
188 }