2
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "memory/allocation.hpp"
27
#include "memory/resourceArea.hpp"
28
#include "oops/oop.inline.hpp"
29
#include "oops/instanceKlass.hpp"
30
#include "oops/method.hpp"
31
#include "runtime/mutexLocker.hpp"
32
#include "services/classLoadingService.hpp"
33
#include "services/memoryService.hpp"
34
#include "utilities/dtrace.hpp"
35
#include "utilities/macros.hpp"
36
#include "utilities/defaultStream.hpp"
37
#include "logging/log.hpp"
38
#include "logging/logConfiguration.hpp"
42
// Only bother with this argument setup if dtrace is available
44
#define HOTSPOT_CLASS_unloaded HOTSPOT_CLASS_UNLOADED
45
#define HOTSPOT_CLASS_loaded HOTSPOT_CLASS_LOADED
46
#define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \
48
char* data = nullptr; \
50
Symbol* name = (clss)->name(); \
51
if (name != nullptr) { \
52
data = (char*)name->bytes(); \
53
len = name->utf8_length(); \
55
HOTSPOT_CLASS_##type( /* type = unloaded, loaded */ \
56
data, len, (void*)(clss)->class_loader_data(), (shared)); \
59
#else // ndef DTRACE_ENABLED
61
#define DTRACE_CLASSLOAD_PROBE(type, clss, shared)
66
// counters for classes loaded from class files
67
PerfCounter* ClassLoadingService::_classes_loaded_count = nullptr;
68
PerfCounter* ClassLoadingService::_classes_unloaded_count = nullptr;
69
PerfCounter* ClassLoadingService::_classbytes_loaded = nullptr;
70
PerfCounter* ClassLoadingService::_classbytes_unloaded = nullptr;
72
// counters for classes loaded from shared archive
73
PerfCounter* ClassLoadingService::_shared_classes_loaded_count = nullptr;
74
PerfCounter* ClassLoadingService::_shared_classes_unloaded_count = nullptr;
75
PerfCounter* ClassLoadingService::_shared_classbytes_loaded = nullptr;
76
PerfCounter* ClassLoadingService::_shared_classbytes_unloaded = nullptr;
77
PerfVariable* ClassLoadingService::_class_methods_size = nullptr;
79
void ClassLoadingService::init() {
82
// These counters are for java.lang.management API support.
83
// They are created even if -XX:-UsePerfData is set and in
84
// that case, they will be allocated on C heap.
85
_classes_loaded_count =
86
PerfDataManager::create_counter(JAVA_CLS, "loadedClasses",
87
PerfData::U_Events, CHECK);
89
_classes_unloaded_count =
90
PerfDataManager::create_counter(JAVA_CLS, "unloadedClasses",
91
PerfData::U_Events, CHECK);
93
_shared_classes_loaded_count =
94
PerfDataManager::create_counter(JAVA_CLS, "sharedLoadedClasses",
95
PerfData::U_Events, CHECK);
97
_shared_classes_unloaded_count =
98
PerfDataManager::create_counter(JAVA_CLS, "sharedUnloadedClasses",
99
PerfData::U_Events, CHECK);
103
PerfDataManager::create_counter(SUN_CLS, "loadedBytes",
104
PerfData::U_Bytes, CHECK);
106
_classbytes_unloaded =
107
PerfDataManager::create_counter(SUN_CLS, "unloadedBytes",
108
PerfData::U_Bytes, CHECK);
109
_shared_classbytes_loaded =
110
PerfDataManager::create_counter(SUN_CLS, "sharedLoadedBytes",
111
PerfData::U_Bytes, CHECK);
113
_shared_classbytes_unloaded =
114
PerfDataManager::create_counter(SUN_CLS, "sharedUnloadedBytes",
115
PerfData::U_Bytes, CHECK);
116
_class_methods_size =
117
PerfDataManager::create_variable(SUN_CLS, "methodBytes",
118
PerfData::U_Bytes, CHECK);
122
bool ClassLoadingService::set_verbose(bool verbose) {
123
MutexLocker m(Management_lock);
124
// verbose will be set to the previous value
125
LogLevelType level = verbose ? LogLevel::Info : LogLevel::Off;
126
LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load));
127
reset_trace_class_unloading();
131
// Caller to this function must own Management_lock
132
void ClassLoadingService::reset_trace_class_unloading() {
133
assert(Management_lock->owned_by_self(), "Must own the Management_lock");
134
bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose();
135
LogLevelType level = value ? LogLevel::Info : LogLevel::Off;
136
LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload));
139
jlong ClassLoadingService::loaded_class_count() {
140
return _classes_loaded_count->get_value() + _shared_classes_loaded_count->get_value();
143
jlong ClassLoadingService::unloaded_class_count() {
144
return _classes_unloaded_count->get_value() + _shared_classes_unloaded_count->get_value();
147
jlong ClassLoadingService::loaded_class_bytes() {
148
return UsePerfData ? _classbytes_loaded->get_value() + _shared_classbytes_loaded->get_value() : -1;
151
jlong ClassLoadingService::unloaded_class_bytes() {
152
return UsePerfData ? _classbytes_unloaded->get_value() + _shared_classbytes_unloaded->get_value() : -1;
155
jlong ClassLoadingService::loaded_shared_class_count() {
156
return _shared_classes_loaded_count->get_value();
159
jlong ClassLoadingService::unloaded_shared_class_count() {
160
return _shared_classes_unloaded_count->get_value();
163
jlong ClassLoadingService::loaded_shared_class_bytes() {
164
return UsePerfData ? _shared_classbytes_loaded->get_value() : -1;
167
jlong ClassLoadingService::unloaded_shared_class_bytes() {
168
return UsePerfData ? _shared_classbytes_unloaded->get_value() : -1;
171
jlong ClassLoadingService::class_method_data_size() {
172
return UsePerfData ? _class_methods_size->get_value() : -1;
175
static size_t compute_class_size(InstanceKlass* k) {
176
// lifted from ClassStatistics.do_class(Klass* k)
177
size_t class_size = k->size();
178
if (k->is_instance_klass()) {
179
class_size += k->methods()->size();
180
// FIXME: Need to count the contents of methods
181
class_size += k->constants()->size();
182
class_size += k->local_interfaces()->size();
183
if (k->transitive_interfaces() != nullptr) {
184
class_size += k->transitive_interfaces()->size();
186
// We do not have to count implementors, since we only store one!
187
// FIXME: How should these be accounted for, now when they have moved.
188
//class_size += k->fields()->size();
190
return class_size * oopSize;
193
void ClassLoadingService::notify_class_loaded(InstanceKlass* k, bool shared_class) {
194
DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class);
195
PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count
196
: _classes_loaded_count);
197
// increment the count
198
classes_counter->inc();
201
PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded
202
: _classbytes_loaded);
203
// add the class size
204
size_t size = compute_class_size(k);
205
classbytes_counter->inc(size);
209
void ClassLoadingService::notify_class_unloaded(InstanceKlass* k) {
210
DTRACE_CLASSLOAD_PROBE(unloaded, k, false);
211
// Classes that can be unloaded must be non-shared
212
_classes_unloaded_count->inc();
215
// add the class size
216
size_t size = compute_class_size(k);
217
_classbytes_unloaded->inc(size);
219
// Compute method size & subtract from running total.
220
// We are called during phase 1 of mark sweep, so it's
221
// still ok to iterate through Method*s here.
222
Array<Method*>* methods = k->methods();
223
for (int i = 0; i < methods->length(); i++) {
224
_class_methods_size->inc(-methods->at(i)->size());
229
#endif // INCLUDE_MANAGEMENT