1 /*
2 * Copyright (c) 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 #include "precompiled.hpp"
25 #include "classfile/classLoaderData.inline.hpp"
26 #include "classfile/javaClasses.hpp"
27 #include "memory/metaspace/printCLDMetaspaceInfoClosure.hpp"
28 #include "memory/metaspace/printMetaspaceInfoKlassClosure.hpp"
29 #include "memory/metaspaceShared.hpp"
30 #include "memory/resourceArea.hpp"
31 #include "runtime/safepoint.hpp"
32 #include "utilities/globalDefinitions.hpp"
33 #include "utilities/ostream.hpp"
34
35
36 namespace metaspace {
37
PrintCLDMetaspaceInfoClosure(outputStream * out,size_t scale,bool do_print,bool do_print_classes,bool break_down_by_chunktype)38 PrintCLDMetaspaceInfoClosure::PrintCLDMetaspaceInfoClosure(outputStream* out, size_t scale, bool do_print,
39 bool do_print_classes, bool break_down_by_chunktype)
40 : _out(out), _scale(scale), _do_print(do_print), _do_print_classes(do_print_classes)
41 , _break_down_by_chunktype(break_down_by_chunktype)
42 , _num_loaders(0), _num_loaders_unloading(0), _num_loaders_without_metaspace(0)
43 , _num_classes(0), _num_classes_shared(0)
44 {
45 memset(_num_loaders_by_spacetype, 0, sizeof(_num_loaders_by_spacetype));
46 memset(_num_classes_by_spacetype, 0, sizeof(_num_classes_by_spacetype));
47 memset(_num_classes_shared_by_spacetype, 0, sizeof(_num_classes_shared_by_spacetype));
48 }
49
50 // A closure just to count classes
51 class CountKlassClosure : public KlassClosure {
52 public:
53
54 uintx _num_classes;
55 uintx _num_classes_shared;
56
CountKlassClosure()57 CountKlassClosure() : _num_classes(0), _num_classes_shared(0) {}
do_klass(Klass * k)58 void do_klass(Klass* k) {
59 _num_classes ++;
60 if (k->is_shared()) {
61 _num_classes_shared ++;
62 }
63 }
64
65 }; // end: PrintKlassInfoClosure
66
do_cld(ClassLoaderData * cld)67 void PrintCLDMetaspaceInfoClosure::do_cld(ClassLoaderData* cld) {
68
69 assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
70
71 if (cld->is_unloading()) {
72 _num_loaders_unloading ++;
73 return;
74 }
75
76 ClassLoaderMetaspace* msp = cld->metaspace_or_null();
77 if (msp == NULL) {
78 _num_loaders_without_metaspace ++;
79 return;
80 }
81
82 // Collect statistics for this class loader metaspace
83 ClassLoaderMetaspaceStatistics this_cld_stat;
84 msp->add_to_statistics(&this_cld_stat);
85
86 // And add it to the running totals
87 _stats_total.add(this_cld_stat);
88 _num_loaders ++;
89 _stats_by_spacetype[msp->space_type()].add(this_cld_stat);
90 _num_loaders_by_spacetype[msp->space_type()] ++;
91
92 // Count classes loaded by this CLD.
93 CountKlassClosure ckc;
94 cld->classes_do(&ckc);
95 // accumulate.
96 _num_classes += ckc._num_classes;
97 _num_classes_by_spacetype[msp->space_type()] += ckc._num_classes;
98 _num_classes_shared += ckc._num_classes_shared;
99 _num_classes_shared_by_spacetype[msp->space_type()] += ckc._num_classes_shared;
100
101 // Optionally, print
102 if (_do_print) {
103
104 _out->print(UINTX_FORMAT_W(4) ": ", _num_loaders);
105
106 // Print "CLD for [<loader name>,] instance of <loader class name>"
107 // or "CLD for <anonymous class>, loaded by [<loader name>,] instance of <loader class name>"
108
109 ResourceMark rm;
110 const char* name = NULL;
111 const char* class_name = NULL;
112
113 // Note: this should also work if unloading:
114 Klass* k = cld->class_loader_klass();
115 if (k != NULL) {
116 class_name = k->external_name();
117 Symbol* s = cld->name();
118 if (s != NULL) {
119 name = s->as_C_string();
120 }
121 } else {
122 name = "<bootstrap>";
123 }
124
125 // Print
126 _out->print("CLD " PTR_FORMAT, p2i(cld));
127 if (cld->is_unloading()) {
128 _out->print(" (unloading)");
129 }
130 _out->print(":");
131 if (cld->is_anonymous()) {
132 _out->print(" <anonymous class>, loaded by");
133 }
134 if (name != NULL) {
135 _out->print(" \"%s\"", name);
136 }
137 if (class_name != NULL) {
138 _out->print(" instance of %s", class_name);
139 }
140
141 if (_do_print_classes) {
142 // Print a detailed description of all loaded classes.
143 streamIndentor sti(_out, 6);
144 _out->cr_indent();
145 _out->print("Loaded classes");
146 if (ckc._num_classes_shared > 0) {
147 _out->print("('s' = shared)");
148 }
149 _out->print(":");
150 PrintMetaspaceInfoKlassClosure pkic(_out, true);
151 cld->classes_do(&pkic);
152 _out->cr_indent();
153 _out->print("-total-: ");
154 print_number_of_classes(_out, ckc._num_classes, ckc._num_classes_shared);
155 } else {
156 // Just print a summary about how many classes have been loaded.
157 _out->print(", ");
158 print_number_of_classes(_out, ckc._num_classes, ckc._num_classes_shared);
159 }
160
161 // Print statistics
162 this_cld_stat.print_on(_out, _scale, _break_down_by_chunktype);
163 _out->cr();
164
165 }
166
167 }
168
169 } // namespace metaspace
170
171