classLoadingService.cpp revision 1147
325N/A/*
325N/A * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
325N/A * CA 95054 USA or visit www.sun.com if you need additional information or
325N/A * have any questions.
325N/A *
325N/A */
325N/A
325N/A# include "incls/_precompiled.incl"
325N/A# include "incls/_classLoadingService.cpp.incl"
325N/A
325N/A#ifdef DTRACE_ENABLED
325N/A
325N/A// Only bother with this argument setup if dtrace is available
325N/A
325N/AHS_DTRACE_PROBE_DECL4(hotspot, class__loaded, char*, int, oop, bool);
325N/AHS_DTRACE_PROBE_DECL4(hotspot, class__unloaded, char*, int, oop, bool);
325N/A
325N/A#define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \
325N/A { \
325N/A char* data = NULL; \
325N/A int len = 0; \
325N/A symbolOop name = (clss)->name(); \
325N/A if (name != NULL) { \
325N/A data = (char*)name->bytes(); \
325N/A len = name->utf8_length(); \
325N/A } \
325N/A HS_DTRACE_PROBE4(hotspot, class__##type, \
325N/A data, len, (clss)->class_loader(), (shared)); \
325N/A }
325N/A
325N/A#else // ndef DTRACE_ENABLED
325N/A
325N/A#define DTRACE_CLASSLOAD_PROBE(type, clss, shared)
325N/A
325N/A#endif
325N/A
325N/A// counters for classes loaded from class files
325N/APerfCounter* ClassLoadingService::_classes_loaded_count = NULL;
325N/APerfCounter* ClassLoadingService::_classes_unloaded_count = NULL;
325N/APerfCounter* ClassLoadingService::_classbytes_loaded = NULL;
325N/APerfCounter* ClassLoadingService::_classbytes_unloaded = NULL;
325N/A
325N/A// counters for classes loaded from shared archive
325N/APerfCounter* ClassLoadingService::_shared_classes_loaded_count = NULL;
325N/APerfCounter* ClassLoadingService::_shared_classes_unloaded_count = NULL;
325N/APerfCounter* ClassLoadingService::_shared_classbytes_loaded = NULL;
325N/APerfCounter* ClassLoadingService::_shared_classbytes_unloaded = NULL;
325N/APerfVariable* ClassLoadingService::_class_methods_size = NULL;
325N/A
325N/Avoid ClassLoadingService::init() {
325N/A EXCEPTION_MARK;
325N/A
325N/A // These counters are for java.lang.management API support.
325N/A // They are created even if -XX:-UsePerfData is set and in
325N/A // that case, they will be allocated on C heap.
325N/A _classes_loaded_count =
325N/A PerfDataManager::create_counter(JAVA_CLS, "loadedClasses",
325N/A PerfData::U_Events, CHECK);
325N/A
325N/A _classes_unloaded_count =
325N/A PerfDataManager::create_counter(JAVA_CLS, "unloadedClasses",
325N/A PerfData::U_Events, CHECK);
325N/A
325N/A _shared_classes_loaded_count =
325N/A PerfDataManager::create_counter(JAVA_CLS, "sharedLoadedClasses",
325N/A PerfData::U_Events, CHECK);
325N/A
325N/A _shared_classes_unloaded_count =
325N/A PerfDataManager::create_counter(JAVA_CLS, "sharedUnloadedClasses",
325N/A PerfData::U_Events, CHECK);
325N/A
325N/A if (UsePerfData) {
325N/A _classbytes_loaded =
325N/A PerfDataManager::create_counter(SUN_CLS, "loadedBytes",
325N/A PerfData::U_Bytes, CHECK);
325N/A
325N/A _classbytes_unloaded =
325N/A PerfDataManager::create_counter(SUN_CLS, "unloadedBytes",
325N/A PerfData::U_Bytes, CHECK);
325N/A _shared_classbytes_loaded =
325N/A PerfDataManager::create_counter(SUN_CLS, "sharedLoadedBytes",
325N/A PerfData::U_Bytes, CHECK);
325N/A
325N/A _shared_classbytes_unloaded =
325N/A PerfDataManager::create_counter(SUN_CLS, "sharedUnloadedBytes",
325N/A PerfData::U_Bytes, CHECK);
325N/A _class_methods_size =
325N/A PerfDataManager::create_variable(SUN_CLS, "methodBytes",
325N/A PerfData::U_Bytes, CHECK);
325N/A }
325N/A}
325N/A
325N/Avoid ClassLoadingService::notify_class_unloaded(instanceKlass* k) {
325N/A DTRACE_CLASSLOAD_PROBE(unloaded, k, false);
325N/A // Classes that can be unloaded must be non-shared
325N/A _classes_unloaded_count->inc();
325N/A
325N/A if (UsePerfData) {
325N/A // add the class size
325N/A size_t size = compute_class_size(k);
325N/A _classbytes_unloaded->inc(size);
325N/A
325N/A // Compute method size & subtract from running total.
325N/A // We are called during phase 1 of mark sweep, so it's
325N/A // still ok to iterate through methodOops here.
325N/A objArrayOop methods = k->methods();
325N/A for (int i = 0; i < methods->length(); i++) {
325N/A _class_methods_size->inc(-methods->obj_at(i)->size());
325N/A }
325N/A }
325N/A
325N/A if (TraceClassUnloading) {
325N/A ResourceMark rm;
325N/A tty->print_cr("[Unloading class %s]", k->external_name());
325N/A }
325N/A}
325N/A
325N/Avoid ClassLoadingService::notify_class_loaded(instanceKlass* k, bool shared_class) {
325N/A DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class);
325N/A PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count
325N/A : _classes_loaded_count);
325N/A // increment the count
325N/A classes_counter->inc();
325N/A
325N/A if (UsePerfData) {
325N/A PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded
325N/A : _classbytes_loaded);
325N/A // add the class size
325N/A size_t size = compute_class_size(k);
325N/A classbytes_counter->inc(size);
325N/A }
325N/A}
325N/A
325N/Asize_t ClassLoadingService::compute_class_size(instanceKlass* k) {
325N/A // lifted from ClassStatistics.do_class(klassOop k)
325N/A
325N/A size_t class_size = 0;
325N/A
325N/A class_size += k->as_klassOop()->size();
325N/A
325N/A if (k->oop_is_instance()) {
325N/A class_size += k->methods()->size();
325N/A class_size += k->constants()->size();
325N/A class_size += k->local_interfaces()->size();
325N/A class_size += k->transitive_interfaces()->size();
325N/A // We do not have to count implementors, since we only store one!
325N/A class_size += k->fields()->size();
325N/A }
325N/A return class_size * oopSize;
325N/A}
325N/A
325N/A
325N/Abool ClassLoadingService::set_verbose(bool verbose) {
325N/A MutexLocker m(Management_lock);
325N/A
325N/A // verbose will be set to the previous value
325N/A bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassLoading", &verbose, MANAGEMENT);
325N/A assert(succeed, "Setting TraceClassLoading flag fails");
325N/A reset_trace_class_unloading();
325N/A
325N/A return verbose;
325N/A}
325N/A
325N/A// Caller to this function must own Management_lock
325N/Avoid ClassLoadingService::reset_trace_class_unloading() {
325N/A assert(Management_lock->owned_by_self(), "Must own the Management_lock");
325N/A bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose();
325N/A bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassUnloading", &value, MANAGEMENT);
325N/A assert(succeed, "Setting TraceClassUnLoading flag fails");
325N/A}
325N/A
325N/AGrowableArray<KlassHandle>* LoadedClassesEnumerator::_loaded_classes = NULL;
325N/AThread* LoadedClassesEnumerator::_current_thread = NULL;
325N/A
325N/ALoadedClassesEnumerator::LoadedClassesEnumerator(Thread* cur_thread) {
325N/A assert(cur_thread == Thread::current(), "Check current thread");
325N/A
325N/A int init_size = ClassLoadingService::loaded_class_count();
325N/A _klass_handle_array = new GrowableArray<KlassHandle>(init_size);
325N/A
325N/A // For consistency of the loaded classes, grab the SystemDictionary lock
325N/A MutexLocker sd_mutex(SystemDictionary_lock);
325N/A
325N/A // Set _loaded_classes and _current_thread and begin enumerating all classes.
325N/A // Only one thread will do the enumeration at a time.
325N/A // These static variables are needed and they are used by the static method
325N/A // add_loaded_class called from classes_do().
325N/A _loaded_classes = _klass_handle_array;
325N/A _current_thread = cur_thread;
325N/A
325N/A SystemDictionary::classes_do(&add_loaded_class);
325N/A
325N/A // FIXME: Exclude array klasses for now
325N/A // Universe::basic_type_classes_do(&add_loaded_class);
325N/A}
325N/A