Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/services/classLoadingService.cpp
32285 views
/*1* Copyright (c) 2003, 2019, 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 "classfile/systemDictionary.hpp"26#include "memory/allocation.hpp"27#include "memory/universe.hpp"28#include "oops/oop.inline.hpp"29#include "runtime/mutexLocker.hpp"30#include "services/classLoadingService.hpp"31#include "services/memoryService.hpp"32#include "utilities/dtrace.hpp"33#include "utilities/macros.hpp"3435#ifdef DTRACE_ENABLED3637// Only bother with this argument setup if dtrace is available3839#ifndef USDT24041HS_DTRACE_PROBE_DECL4(hotspot, class__loaded, char*, int, oop, bool);42HS_DTRACE_PROBE_DECL4(hotspot, class__unloaded, char*, int, oop, bool);4344#define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \45{ \46char* data = NULL; \47int len = 0; \48Symbol* name = (clss)->name(); \49if (name != NULL) { \50data = (char*)name->bytes(); \51len = name->utf8_length(); \52} \53HS_DTRACE_PROBE4(hotspot, class__##type, \54data, len, (void *)(clss)->class_loader(), (shared)); \55}5657#else /* USDT2 */5859#define HOTSPOT_CLASS_unloaded HOTSPOT_CLASS_UNLOADED60#define HOTSPOT_CLASS_loaded HOTSPOT_CLASS_LOADED61#define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \62{ \63char* data = NULL; \64int len = 0; \65Symbol* name = (clss)->name(); \66if (name != NULL) { \67data = (char*)name->bytes(); \68len = name->utf8_length(); \69} \70HOTSPOT_CLASS_##type( /* type = unloaded, loaded */ \71data, len, (void *)(clss)->class_loader(), (shared)); \72}7374#endif /* USDT2 */75#else // ndef DTRACE_ENABLED7677#define DTRACE_CLASSLOAD_PROBE(type, clss, shared)7879#endif8081#if INCLUDE_MANAGEMENT82// counters for classes loaded from class files83PerfCounter* ClassLoadingService::_classes_loaded_count = NULL;84PerfCounter* ClassLoadingService::_classes_unloaded_count = NULL;85PerfCounter* ClassLoadingService::_classbytes_loaded = NULL;86PerfCounter* ClassLoadingService::_classbytes_unloaded = NULL;8788// counters for classes loaded from shared archive89PerfCounter* ClassLoadingService::_shared_classes_loaded_count = NULL;90PerfCounter* ClassLoadingService::_shared_classes_unloaded_count = NULL;91PerfCounter* ClassLoadingService::_shared_classbytes_loaded = NULL;92PerfCounter* ClassLoadingService::_shared_classbytes_unloaded = NULL;93PerfVariable* ClassLoadingService::_class_methods_size = NULL;9495void ClassLoadingService::init() {96EXCEPTION_MARK;9798// These counters are for java.lang.management API support.99// They are created even if -XX:-UsePerfData is set and in100// that case, they will be allocated on C heap.101_classes_loaded_count =102PerfDataManager::create_counter(JAVA_CLS, "loadedClasses",103PerfData::U_Events, CHECK);104105_classes_unloaded_count =106PerfDataManager::create_counter(JAVA_CLS, "unloadedClasses",107PerfData::U_Events, CHECK);108109_shared_classes_loaded_count =110PerfDataManager::create_counter(JAVA_CLS, "sharedLoadedClasses",111PerfData::U_Events, CHECK);112113_shared_classes_unloaded_count =114PerfDataManager::create_counter(JAVA_CLS, "sharedUnloadedClasses",115PerfData::U_Events, CHECK);116117if (UsePerfData) {118_classbytes_loaded =119PerfDataManager::create_counter(SUN_CLS, "loadedBytes",120PerfData::U_Bytes, CHECK);121122_classbytes_unloaded =123PerfDataManager::create_counter(SUN_CLS, "unloadedBytes",124PerfData::U_Bytes, CHECK);125_shared_classbytes_loaded =126PerfDataManager::create_counter(SUN_CLS, "sharedLoadedBytes",127PerfData::U_Bytes, CHECK);128129_shared_classbytes_unloaded =130PerfDataManager::create_counter(SUN_CLS, "sharedUnloadedBytes",131PerfData::U_Bytes, CHECK);132_class_methods_size =133PerfDataManager::create_variable(SUN_CLS, "methodBytes",134PerfData::U_Bytes, CHECK);135}136}137138void ClassLoadingService::notify_class_unloaded(InstanceKlass* k) {139DTRACE_CLASSLOAD_PROBE(unloaded, k, false);140// Classes that can be unloaded must be non-shared141_classes_unloaded_count->inc();142143if (UsePerfData) {144// add the class size145size_t size = compute_class_size(k);146_classbytes_unloaded->inc(size);147148// Compute method size & subtract from running total.149// We are called during phase 1 of mark sweep, so it's150// still ok to iterate through Method*s here.151Array<Method*>* methods = k->methods();152for (int i = 0; i < methods->length(); i++) {153_class_methods_size->inc(-methods->at(i)->size());154}155}156157if (TraceClassUnloading) {158ResourceMark rm;159tty->print_cr("[Unloading class %s " INTPTR_FORMAT "]", k->external_name(), p2i(k));160}161}162163void ClassLoadingService::notify_class_loaded(InstanceKlass* k, bool shared_class) {164DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class);165PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count166: _classes_loaded_count);167// increment the count168classes_counter->inc();169170if (UsePerfData) {171PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded172: _classbytes_loaded);173// add the class size174size_t size = compute_class_size(k);175classbytes_counter->inc(size);176}177}178179size_t ClassLoadingService::compute_class_size(InstanceKlass* k) {180// lifted from ClassStatistics.do_class(Klass* k)181182size_t class_size = 0;183184class_size += k->size();185186if (k->oop_is_instance()) {187class_size += k->methods()->size();188// FIXME: Need to count the contents of methods189class_size += k->constants()->size();190class_size += k->local_interfaces()->size();191class_size += k->transitive_interfaces()->size();192// We do not have to count implementors, since we only store one!193// FIXME: How should these be accounted for, now when they have moved.194//class_size += k->fields()->size();195}196return class_size * oopSize;197}198199200bool ClassLoadingService::set_verbose(bool verbose) {201MutexLocker m(Management_lock);202203// verbose will be set to the previous value204bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassLoading", &verbose, Flag::MANAGEMENT);205assert(succeed, "Setting TraceClassLoading flag fails");206reset_trace_class_unloading();207208return verbose;209}210211// Caller to this function must own Management_lock212void ClassLoadingService::reset_trace_class_unloading() {213assert(Management_lock->owned_by_self(), "Must own the Management_lock");214bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose();215bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassUnloading", &value, Flag::MANAGEMENT);216assert(succeed, "Setting TraceClassUnLoading flag fails");217}218219GrowableArray<KlassHandle>* LoadedClassesEnumerator::_loaded_classes = NULL;220Thread* LoadedClassesEnumerator::_current_thread = NULL;221222LoadedClassesEnumerator::LoadedClassesEnumerator(Thread* cur_thread) {223assert(cur_thread == Thread::current(), "Check current thread");224225int init_size = ClassLoadingService::loaded_class_count();226_klass_handle_array = new GrowableArray<KlassHandle>(init_size);227228// For consistency of the loaded classes, grab the SystemDictionary lock229MutexLocker sd_mutex(SystemDictionary_lock);230231// Set _loaded_classes and _current_thread and begin enumerating all classes.232// Only one thread will do the enumeration at a time.233// These static variables are needed and they are used by the static method234// add_loaded_class called from classes_do().235_loaded_classes = _klass_handle_array;236_current_thread = cur_thread;237238SystemDictionary::classes_do(&add_loaded_class);239240// FIXME: Exclude array klasses for now241// Universe::basic_type_classes_do(&add_loaded_class);242}243244#endif // INCLUDE_MANAGEMENT245246247