1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/services/classLoadingService.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,245 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "classfile/systemDictionary.hpp" 1.30 +#include "memory/allocation.hpp" 1.31 +#include "memory/universe.hpp" 1.32 +#include "oops/oop.inline.hpp" 1.33 +#include "runtime/mutexLocker.hpp" 1.34 +#include "services/classLoadingService.hpp" 1.35 +#include "services/memoryService.hpp" 1.36 +#include "utilities/dtrace.hpp" 1.37 +#include "utilities/macros.hpp" 1.38 + 1.39 +#ifdef DTRACE_ENABLED 1.40 + 1.41 +// Only bother with this argument setup if dtrace is available 1.42 + 1.43 +#ifndef USDT2 1.44 + 1.45 +HS_DTRACE_PROBE_DECL4(hotspot, class__loaded, char*, int, oop, bool); 1.46 +HS_DTRACE_PROBE_DECL4(hotspot, class__unloaded, char*, int, oop, bool); 1.47 + 1.48 +#define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \ 1.49 + { \ 1.50 + char* data = NULL; \ 1.51 + int len = 0; \ 1.52 + Symbol* name = (clss)->name(); \ 1.53 + if (name != NULL) { \ 1.54 + data = (char*)name->bytes(); \ 1.55 + len = name->utf8_length(); \ 1.56 + } \ 1.57 + HS_DTRACE_PROBE4(hotspot, class__##type, \ 1.58 + data, len, SOLARIS_ONLY((void *))(clss)->class_loader(), (shared)); \ 1.59 + } 1.60 + 1.61 +#else /* USDT2 */ 1.62 + 1.63 +#define HOTSPOT_CLASS_unloaded HOTSPOT_CLASS_UNLOADED 1.64 +#define HOTSPOT_CLASS_loaded HOTSPOT_CLASS_LOADED 1.65 +#define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \ 1.66 + { \ 1.67 + char* data = NULL; \ 1.68 + int len = 0; \ 1.69 + Symbol* name = (clss)->name(); \ 1.70 + if (name != NULL) { \ 1.71 + data = (char*)name->bytes(); \ 1.72 + len = name->utf8_length(); \ 1.73 + } \ 1.74 + HOTSPOT_CLASS_##type( /* type = unloaded, loaded */ \ 1.75 + data, len, (clss)->class_loader(), (shared)); \ 1.76 + } 1.77 + 1.78 +#endif /* USDT2 */ 1.79 +#else // ndef DTRACE_ENABLED 1.80 + 1.81 +#define DTRACE_CLASSLOAD_PROBE(type, clss, shared) 1.82 + 1.83 +#endif 1.84 + 1.85 +#if INCLUDE_MANAGEMENT 1.86 +// counters for classes loaded from class files 1.87 +PerfCounter* ClassLoadingService::_classes_loaded_count = NULL; 1.88 +PerfCounter* ClassLoadingService::_classes_unloaded_count = NULL; 1.89 +PerfCounter* ClassLoadingService::_classbytes_loaded = NULL; 1.90 +PerfCounter* ClassLoadingService::_classbytes_unloaded = NULL; 1.91 + 1.92 +// counters for classes loaded from shared archive 1.93 +PerfCounter* ClassLoadingService::_shared_classes_loaded_count = NULL; 1.94 +PerfCounter* ClassLoadingService::_shared_classes_unloaded_count = NULL; 1.95 +PerfCounter* ClassLoadingService::_shared_classbytes_loaded = NULL; 1.96 +PerfCounter* ClassLoadingService::_shared_classbytes_unloaded = NULL; 1.97 +PerfVariable* ClassLoadingService::_class_methods_size = NULL; 1.98 + 1.99 +void ClassLoadingService::init() { 1.100 + EXCEPTION_MARK; 1.101 + 1.102 + // These counters are for java.lang.management API support. 1.103 + // They are created even if -XX:-UsePerfData is set and in 1.104 + // that case, they will be allocated on C heap. 1.105 + _classes_loaded_count = 1.106 + PerfDataManager::create_counter(JAVA_CLS, "loadedClasses", 1.107 + PerfData::U_Events, CHECK); 1.108 + 1.109 + _classes_unloaded_count = 1.110 + PerfDataManager::create_counter(JAVA_CLS, "unloadedClasses", 1.111 + PerfData::U_Events, CHECK); 1.112 + 1.113 + _shared_classes_loaded_count = 1.114 + PerfDataManager::create_counter(JAVA_CLS, "sharedLoadedClasses", 1.115 + PerfData::U_Events, CHECK); 1.116 + 1.117 + _shared_classes_unloaded_count = 1.118 + PerfDataManager::create_counter(JAVA_CLS, "sharedUnloadedClasses", 1.119 + PerfData::U_Events, CHECK); 1.120 + 1.121 + if (UsePerfData) { 1.122 + _classbytes_loaded = 1.123 + PerfDataManager::create_counter(SUN_CLS, "loadedBytes", 1.124 + PerfData::U_Bytes, CHECK); 1.125 + 1.126 + _classbytes_unloaded = 1.127 + PerfDataManager::create_counter(SUN_CLS, "unloadedBytes", 1.128 + PerfData::U_Bytes, CHECK); 1.129 + _shared_classbytes_loaded = 1.130 + PerfDataManager::create_counter(SUN_CLS, "sharedLoadedBytes", 1.131 + PerfData::U_Bytes, CHECK); 1.132 + 1.133 + _shared_classbytes_unloaded = 1.134 + PerfDataManager::create_counter(SUN_CLS, "sharedUnloadedBytes", 1.135 + PerfData::U_Bytes, CHECK); 1.136 + _class_methods_size = 1.137 + PerfDataManager::create_variable(SUN_CLS, "methodBytes", 1.138 + PerfData::U_Bytes, CHECK); 1.139 + } 1.140 +} 1.141 + 1.142 +void ClassLoadingService::notify_class_unloaded(InstanceKlass* k) { 1.143 + DTRACE_CLASSLOAD_PROBE(unloaded, k, false); 1.144 + // Classes that can be unloaded must be non-shared 1.145 + _classes_unloaded_count->inc(); 1.146 + 1.147 + if (UsePerfData) { 1.148 + // add the class size 1.149 + size_t size = compute_class_size(k); 1.150 + _classbytes_unloaded->inc(size); 1.151 + 1.152 + // Compute method size & subtract from running total. 1.153 + // We are called during phase 1 of mark sweep, so it's 1.154 + // still ok to iterate through Method*s here. 1.155 + Array<Method*>* methods = k->methods(); 1.156 + for (int i = 0; i < methods->length(); i++) { 1.157 + _class_methods_size->inc(-methods->at(i)->size()); 1.158 + } 1.159 + } 1.160 + 1.161 + if (TraceClassUnloading) { 1.162 + ResourceMark rm; 1.163 + tty->print_cr("[Unloading class %s " INTPTR_FORMAT "]", k->external_name(), p2i(k)); 1.164 + } 1.165 +} 1.166 + 1.167 +void ClassLoadingService::notify_class_loaded(InstanceKlass* k, bool shared_class) { 1.168 + DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class); 1.169 + PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count 1.170 + : _classes_loaded_count); 1.171 + // increment the count 1.172 + classes_counter->inc(); 1.173 + 1.174 + if (UsePerfData) { 1.175 + PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded 1.176 + : _classbytes_loaded); 1.177 + // add the class size 1.178 + size_t size = compute_class_size(k); 1.179 + classbytes_counter->inc(size); 1.180 + } 1.181 +} 1.182 + 1.183 +size_t ClassLoadingService::compute_class_size(InstanceKlass* k) { 1.184 + // lifted from ClassStatistics.do_class(Klass* k) 1.185 + 1.186 + size_t class_size = 0; 1.187 + 1.188 + class_size += k->size(); 1.189 + 1.190 + if (k->oop_is_instance()) { 1.191 + class_size += k->methods()->size(); 1.192 + // FIXME: Need to count the contents of methods 1.193 + class_size += k->constants()->size(); 1.194 + class_size += k->local_interfaces()->size(); 1.195 + class_size += k->transitive_interfaces()->size(); 1.196 + // We do not have to count implementors, since we only store one! 1.197 + // FIXME: How should these be accounted for, now when they have moved. 1.198 + //class_size += k->fields()->size(); 1.199 + } 1.200 + return class_size * oopSize; 1.201 +} 1.202 + 1.203 + 1.204 +bool ClassLoadingService::set_verbose(bool verbose) { 1.205 + MutexLocker m(Management_lock); 1.206 + 1.207 + // verbose will be set to the previous value 1.208 + bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassLoading", &verbose, Flag::MANAGEMENT); 1.209 + assert(succeed, "Setting TraceClassLoading flag fails"); 1.210 + reset_trace_class_unloading(); 1.211 + 1.212 + return verbose; 1.213 +} 1.214 + 1.215 +// Caller to this function must own Management_lock 1.216 +void ClassLoadingService::reset_trace_class_unloading() { 1.217 + assert(Management_lock->owned_by_self(), "Must own the Management_lock"); 1.218 + bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose(); 1.219 + bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassUnloading", &value, Flag::MANAGEMENT); 1.220 + assert(succeed, "Setting TraceClassUnLoading flag fails"); 1.221 +} 1.222 + 1.223 +GrowableArray<KlassHandle>* LoadedClassesEnumerator::_loaded_classes = NULL; 1.224 +Thread* LoadedClassesEnumerator::_current_thread = NULL; 1.225 + 1.226 +LoadedClassesEnumerator::LoadedClassesEnumerator(Thread* cur_thread) { 1.227 + assert(cur_thread == Thread::current(), "Check current thread"); 1.228 + 1.229 + int init_size = ClassLoadingService::loaded_class_count(); 1.230 + _klass_handle_array = new GrowableArray<KlassHandle>(init_size); 1.231 + 1.232 + // For consistency of the loaded classes, grab the SystemDictionary lock 1.233 + MutexLocker sd_mutex(SystemDictionary_lock); 1.234 + 1.235 + // Set _loaded_classes and _current_thread and begin enumerating all classes. 1.236 + // Only one thread will do the enumeration at a time. 1.237 + // These static variables are needed and they are used by the static method 1.238 + // add_loaded_class called from classes_do(). 1.239 + _loaded_classes = _klass_handle_array; 1.240 + _current_thread = cur_thread; 1.241 + 1.242 + SystemDictionary::classes_do(&add_loaded_class); 1.243 + 1.244 + // FIXME: Exclude array klasses for now 1.245 + // Universe::basic_type_classes_do(&add_loaded_class); 1.246 +} 1.247 + 1.248 +#endif // INCLUDE_MANAGEMENT