aoqi@0: /* aoqi@0: * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "memory/universe.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" aoqi@0: #include "services/classLoadingService.hpp" aoqi@0: #include "services/memoryService.hpp" aoqi@0: #include "utilities/dtrace.hpp" aoqi@0: #include "utilities/macros.hpp" aoqi@0: aoqi@0: #ifdef DTRACE_ENABLED aoqi@0: aoqi@0: // Only bother with this argument setup if dtrace is available aoqi@0: aoqi@0: #ifndef USDT2 aoqi@0: aoqi@0: HS_DTRACE_PROBE_DECL4(hotspot, class__loaded, char*, int, oop, bool); aoqi@0: HS_DTRACE_PROBE_DECL4(hotspot, class__unloaded, char*, int, oop, bool); aoqi@0: aoqi@0: #define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \ aoqi@0: { \ aoqi@0: char* data = NULL; \ aoqi@0: int len = 0; \ aoqi@0: Symbol* name = (clss)->name(); \ aoqi@0: if (name != NULL) { \ aoqi@0: data = (char*)name->bytes(); \ aoqi@0: len = name->utf8_length(); \ aoqi@0: } \ aoqi@0: HS_DTRACE_PROBE4(hotspot, class__##type, \ aoqi@0: data, len, SOLARIS_ONLY((void *))(clss)->class_loader(), (shared)); \ aoqi@0: } aoqi@0: aoqi@0: #else /* USDT2 */ aoqi@0: aoqi@0: #define HOTSPOT_CLASS_unloaded HOTSPOT_CLASS_UNLOADED aoqi@0: #define HOTSPOT_CLASS_loaded HOTSPOT_CLASS_LOADED aoqi@0: #define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \ aoqi@0: { \ aoqi@0: char* data = NULL; \ aoqi@0: int len = 0; \ aoqi@0: Symbol* name = (clss)->name(); \ aoqi@0: if (name != NULL) { \ aoqi@0: data = (char*)name->bytes(); \ aoqi@0: len = name->utf8_length(); \ aoqi@0: } \ aoqi@0: HOTSPOT_CLASS_##type( /* type = unloaded, loaded */ \ aoqi@0: data, len, (clss)->class_loader(), (shared)); \ aoqi@0: } aoqi@0: aoqi@0: #endif /* USDT2 */ aoqi@0: #else // ndef DTRACE_ENABLED aoqi@0: aoqi@0: #define DTRACE_CLASSLOAD_PROBE(type, clss, shared) aoqi@0: aoqi@0: #endif aoqi@0: aoqi@0: #if INCLUDE_MANAGEMENT aoqi@0: // counters for classes loaded from class files aoqi@0: PerfCounter* ClassLoadingService::_classes_loaded_count = NULL; aoqi@0: PerfCounter* ClassLoadingService::_classes_unloaded_count = NULL; aoqi@0: PerfCounter* ClassLoadingService::_classbytes_loaded = NULL; aoqi@0: PerfCounter* ClassLoadingService::_classbytes_unloaded = NULL; aoqi@0: aoqi@0: // counters for classes loaded from shared archive aoqi@0: PerfCounter* ClassLoadingService::_shared_classes_loaded_count = NULL; aoqi@0: PerfCounter* ClassLoadingService::_shared_classes_unloaded_count = NULL; aoqi@0: PerfCounter* ClassLoadingService::_shared_classbytes_loaded = NULL; aoqi@0: PerfCounter* ClassLoadingService::_shared_classbytes_unloaded = NULL; aoqi@0: PerfVariable* ClassLoadingService::_class_methods_size = NULL; aoqi@0: aoqi@0: void ClassLoadingService::init() { aoqi@0: EXCEPTION_MARK; aoqi@0: aoqi@0: // These counters are for java.lang.management API support. aoqi@0: // They are created even if -XX:-UsePerfData is set and in aoqi@0: // that case, they will be allocated on C heap. aoqi@0: _classes_loaded_count = aoqi@0: PerfDataManager::create_counter(JAVA_CLS, "loadedClasses", aoqi@0: PerfData::U_Events, CHECK); aoqi@0: aoqi@0: _classes_unloaded_count = aoqi@0: PerfDataManager::create_counter(JAVA_CLS, "unloadedClasses", aoqi@0: PerfData::U_Events, CHECK); aoqi@0: aoqi@0: _shared_classes_loaded_count = aoqi@0: PerfDataManager::create_counter(JAVA_CLS, "sharedLoadedClasses", aoqi@0: PerfData::U_Events, CHECK); aoqi@0: aoqi@0: _shared_classes_unloaded_count = aoqi@0: PerfDataManager::create_counter(JAVA_CLS, "sharedUnloadedClasses", aoqi@0: PerfData::U_Events, CHECK); aoqi@0: aoqi@0: if (UsePerfData) { aoqi@0: _classbytes_loaded = aoqi@0: PerfDataManager::create_counter(SUN_CLS, "loadedBytes", aoqi@0: PerfData::U_Bytes, CHECK); aoqi@0: aoqi@0: _classbytes_unloaded = aoqi@0: PerfDataManager::create_counter(SUN_CLS, "unloadedBytes", aoqi@0: PerfData::U_Bytes, CHECK); aoqi@0: _shared_classbytes_loaded = aoqi@0: PerfDataManager::create_counter(SUN_CLS, "sharedLoadedBytes", aoqi@0: PerfData::U_Bytes, CHECK); aoqi@0: aoqi@0: _shared_classbytes_unloaded = aoqi@0: PerfDataManager::create_counter(SUN_CLS, "sharedUnloadedBytes", aoqi@0: PerfData::U_Bytes, CHECK); aoqi@0: _class_methods_size = aoqi@0: PerfDataManager::create_variable(SUN_CLS, "methodBytes", aoqi@0: PerfData::U_Bytes, CHECK); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ClassLoadingService::notify_class_unloaded(InstanceKlass* k) { aoqi@0: DTRACE_CLASSLOAD_PROBE(unloaded, k, false); aoqi@0: // Classes that can be unloaded must be non-shared aoqi@0: _classes_unloaded_count->inc(); aoqi@0: aoqi@0: if (UsePerfData) { aoqi@0: // add the class size aoqi@0: size_t size = compute_class_size(k); aoqi@0: _classbytes_unloaded->inc(size); aoqi@0: aoqi@0: // Compute method size & subtract from running total. aoqi@0: // We are called during phase 1 of mark sweep, so it's aoqi@0: // still ok to iterate through Method*s here. aoqi@0: Array* methods = k->methods(); aoqi@0: for (int i = 0; i < methods->length(); i++) { aoqi@0: _class_methods_size->inc(-methods->at(i)->size()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (TraceClassUnloading) { aoqi@0: ResourceMark rm; aoqi@0: tty->print_cr("[Unloading class %s " INTPTR_FORMAT "]", k->external_name(), p2i(k)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void ClassLoadingService::notify_class_loaded(InstanceKlass* k, bool shared_class) { aoqi@0: DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class); aoqi@0: PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count aoqi@0: : _classes_loaded_count); aoqi@0: // increment the count aoqi@0: classes_counter->inc(); aoqi@0: aoqi@0: if (UsePerfData) { aoqi@0: PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded aoqi@0: : _classbytes_loaded); aoqi@0: // add the class size aoqi@0: size_t size = compute_class_size(k); aoqi@0: classbytes_counter->inc(size); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: size_t ClassLoadingService::compute_class_size(InstanceKlass* k) { aoqi@0: // lifted from ClassStatistics.do_class(Klass* k) aoqi@0: aoqi@0: size_t class_size = 0; aoqi@0: aoqi@0: class_size += k->size(); aoqi@0: aoqi@0: if (k->oop_is_instance()) { aoqi@0: class_size += k->methods()->size(); aoqi@0: // FIXME: Need to count the contents of methods aoqi@0: class_size += k->constants()->size(); aoqi@0: class_size += k->local_interfaces()->size(); aoqi@0: class_size += k->transitive_interfaces()->size(); aoqi@0: // We do not have to count implementors, since we only store one! aoqi@0: // FIXME: How should these be accounted for, now when they have moved. aoqi@0: //class_size += k->fields()->size(); aoqi@0: } aoqi@0: return class_size * oopSize; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool ClassLoadingService::set_verbose(bool verbose) { aoqi@0: MutexLocker m(Management_lock); aoqi@0: aoqi@0: // verbose will be set to the previous value aoqi@0: bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassLoading", &verbose, Flag::MANAGEMENT); aoqi@0: assert(succeed, "Setting TraceClassLoading flag fails"); aoqi@0: reset_trace_class_unloading(); aoqi@0: aoqi@0: return verbose; aoqi@0: } aoqi@0: aoqi@0: // Caller to this function must own Management_lock aoqi@0: void ClassLoadingService::reset_trace_class_unloading() { aoqi@0: assert(Management_lock->owned_by_self(), "Must own the Management_lock"); aoqi@0: bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose(); aoqi@0: bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassUnloading", &value, Flag::MANAGEMENT); aoqi@0: assert(succeed, "Setting TraceClassUnLoading flag fails"); aoqi@0: } aoqi@0: aoqi@0: GrowableArray* LoadedClassesEnumerator::_loaded_classes = NULL; aoqi@0: Thread* LoadedClassesEnumerator::_current_thread = NULL; aoqi@0: aoqi@0: LoadedClassesEnumerator::LoadedClassesEnumerator(Thread* cur_thread) { aoqi@0: assert(cur_thread == Thread::current(), "Check current thread"); aoqi@0: aoqi@0: int init_size = ClassLoadingService::loaded_class_count(); aoqi@0: _klass_handle_array = new GrowableArray(init_size); aoqi@0: aoqi@0: // For consistency of the loaded classes, grab the SystemDictionary lock aoqi@0: MutexLocker sd_mutex(SystemDictionary_lock); aoqi@0: aoqi@0: // Set _loaded_classes and _current_thread and begin enumerating all classes. aoqi@0: // Only one thread will do the enumeration at a time. aoqi@0: // These static variables are needed and they are used by the static method aoqi@0: // add_loaded_class called from classes_do(). aoqi@0: _loaded_classes = _klass_handle_array; aoqi@0: _current_thread = cur_thread; aoqi@0: aoqi@0: SystemDictionary::classes_do(&add_loaded_class); aoqi@0: aoqi@0: // FIXME: Exclude array klasses for now aoqi@0: // Universe::basic_type_classes_do(&add_loaded_class); aoqi@0: } aoqi@0: aoqi@0: #endif // INCLUDE_MANAGEMENT