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