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