Path: blob/master/src/hotspot/share/services/classLoadingService.cpp
41145 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/allocation.hpp"26#include "memory/resourceArea.hpp"27#include "oops/oop.inline.hpp"28#include "oops/instanceKlass.hpp"29#include "oops/method.hpp"30#include "runtime/mutexLocker.hpp"31#include "services/classLoadingService.hpp"32#include "services/memoryService.hpp"33#include "utilities/dtrace.hpp"34#include "utilities/macros.hpp"35#include "utilities/defaultStream.hpp"36#include "logging/log.hpp"37#include "logging/logConfiguration.hpp"3839#ifdef DTRACE_ENABLED4041// Only bother with this argument setup if dtrace is available4243#define HOTSPOT_CLASS_unloaded HOTSPOT_CLASS_UNLOADED44#define HOTSPOT_CLASS_loaded HOTSPOT_CLASS_LOADED45#define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \46{ \47char* data = NULL; \48int len = 0; \49Symbol* name = (clss)->name(); \50if (name != NULL) { \51data = (char*)name->bytes(); \52len = name->utf8_length(); \53} \54HOTSPOT_CLASS_##type( /* type = unloaded, loaded */ \55data, len, (void*)(clss)->class_loader_data(), (shared)); \56}5758#else // ndef DTRACE_ENABLED5960#define DTRACE_CLASSLOAD_PROBE(type, clss, shared)6162#endif6364#if INCLUDE_MANAGEMENT65// counters for classes loaded from class files66PerfCounter* ClassLoadingService::_classes_loaded_count = NULL;67PerfCounter* ClassLoadingService::_classes_unloaded_count = NULL;68PerfCounter* ClassLoadingService::_classbytes_loaded = NULL;69PerfCounter* ClassLoadingService::_classbytes_unloaded = NULL;7071// counters for classes loaded from shared archive72PerfCounter* ClassLoadingService::_shared_classes_loaded_count = NULL;73PerfCounter* ClassLoadingService::_shared_classes_unloaded_count = NULL;74PerfCounter* ClassLoadingService::_shared_classbytes_loaded = NULL;75PerfCounter* ClassLoadingService::_shared_classbytes_unloaded = NULL;76PerfVariable* ClassLoadingService::_class_methods_size = NULL;7778void ClassLoadingService::init() {79EXCEPTION_MARK;8081// These counters are for java.lang.management API support.82// They are created even if -XX:-UsePerfData is set and in83// that case, they will be allocated on C heap.84_classes_loaded_count =85PerfDataManager::create_counter(JAVA_CLS, "loadedClasses",86PerfData::U_Events, CHECK);8788_classes_unloaded_count =89PerfDataManager::create_counter(JAVA_CLS, "unloadedClasses",90PerfData::U_Events, CHECK);9192_shared_classes_loaded_count =93PerfDataManager::create_counter(JAVA_CLS, "sharedLoadedClasses",94PerfData::U_Events, CHECK);9596_shared_classes_unloaded_count =97PerfDataManager::create_counter(JAVA_CLS, "sharedUnloadedClasses",98PerfData::U_Events, CHECK);99100if (UsePerfData) {101_classbytes_loaded =102PerfDataManager::create_counter(SUN_CLS, "loadedBytes",103PerfData::U_Bytes, CHECK);104105_classbytes_unloaded =106PerfDataManager::create_counter(SUN_CLS, "unloadedBytes",107PerfData::U_Bytes, CHECK);108_shared_classbytes_loaded =109PerfDataManager::create_counter(SUN_CLS, "sharedLoadedBytes",110PerfData::U_Bytes, CHECK);111112_shared_classbytes_unloaded =113PerfDataManager::create_counter(SUN_CLS, "sharedUnloadedBytes",114PerfData::U_Bytes, CHECK);115_class_methods_size =116PerfDataManager::create_variable(SUN_CLS, "methodBytes",117PerfData::U_Bytes, CHECK);118}119}120121void ClassLoadingService::notify_class_unloaded(InstanceKlass* k) {122DTRACE_CLASSLOAD_PROBE(unloaded, k, false);123// Classes that can be unloaded must be non-shared124_classes_unloaded_count->inc();125126if (UsePerfData) {127// add the class size128size_t size = compute_class_size(k);129_classbytes_unloaded->inc(size);130131// Compute method size & subtract from running total.132// We are called during phase 1 of mark sweep, so it's133// still ok to iterate through Method*s here.134Array<Method*>* methods = k->methods();135for (int i = 0; i < methods->length(); i++) {136_class_methods_size->inc(-methods->at(i)->size());137}138}139}140141void ClassLoadingService::notify_class_loaded(InstanceKlass* k, bool shared_class) {142DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class);143PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count144: _classes_loaded_count);145// increment the count146classes_counter->inc();147148if (UsePerfData) {149PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded150: _classbytes_loaded);151// add the class size152size_t size = compute_class_size(k);153classbytes_counter->inc(size);154}155}156157size_t ClassLoadingService::compute_class_size(InstanceKlass* k) {158// lifted from ClassStatistics.do_class(Klass* k)159160size_t class_size = 0;161162class_size += k->size();163164if (k->is_instance_klass()) {165class_size += k->methods()->size();166// FIXME: Need to count the contents of methods167class_size += k->constants()->size();168class_size += k->local_interfaces()->size();169if (k->transitive_interfaces() != NULL) {170class_size += k->transitive_interfaces()->size();171}172// We do not have to count implementors, since we only store one!173// FIXME: How should these be accounted for, now when they have moved.174//class_size += k->fields()->size();175}176return class_size * oopSize;177}178179bool ClassLoadingService::set_verbose(bool verbose) {180MutexLocker m(Management_lock);181// verbose will be set to the previous value182LogLevelType level = verbose ? LogLevel::Info : LogLevel::Off;183LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, load));184reset_trace_class_unloading();185return verbose;186}187188// Caller to this function must own Management_lock189void ClassLoadingService::reset_trace_class_unloading() {190assert(Management_lock->owned_by_self(), "Must own the Management_lock");191bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose();192LogLevelType level = value ? LogLevel::Info : LogLevel::Off;193LogConfiguration::configure_stdout(level, false, LOG_TAGS(class, unload));194}195196#endif // INCLUDE_MANAGEMENT197198199