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