1 /* 2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/systemDictionary.hpp" 27 #include "memory/allocation.hpp" 28 #include "memory/universe.hpp" 29 #include "oops/oop.inline.hpp" 30 #include "runtime/mutexLocker.hpp" 31 #include "services/classLoadingService.hpp" 32 #include "services/memoryService.hpp" 33 #include "utilities/dtrace.hpp" 34 35 #ifdef DTRACE_ENABLED 36 37 // Only bother with this argument setup if dtrace is available 38 39 HS_DTRACE_PROBE_DECL4(hotspot, class__loaded, char*, int, oop, bool); 40 HS_DTRACE_PROBE_DECL4(hotspot, class__unloaded, char*, int, oop, bool); 41 42 #define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \ 43 { \ 44 char* data = NULL; \ 45 int len = 0; \ 46 symbolOop name = (clss)->name(); \ 47 if (name != NULL) { \ 48 data = (char*)name->bytes(); \ 49 len = name->utf8_length(); \ 50 } \ 51 HS_DTRACE_PROBE4(hotspot, class__##type, \ 52 data, len, (clss)->class_loader(), (shared)); \ 53 } 54 55 #else // ndef DTRACE_ENABLED 56 57 #define DTRACE_CLASSLOAD_PROBE(type, clss, shared) 58 59 #endif 60 61 // counters for classes loaded from class files 62 PerfCounter* ClassLoadingService::_classes_loaded_count = NULL; 63 PerfCounter* ClassLoadingService::_classes_unloaded_count = NULL; 64 PerfCounter* ClassLoadingService::_classbytes_loaded = NULL; 65 PerfCounter* ClassLoadingService::_classbytes_unloaded = NULL; 66 67 // counters for classes loaded from shared archive 68 PerfCounter* ClassLoadingService::_shared_classes_loaded_count = NULL; 69 PerfCounter* ClassLoadingService::_shared_classes_unloaded_count = NULL; 70 PerfCounter* ClassLoadingService::_shared_classbytes_loaded = NULL; 71 PerfCounter* ClassLoadingService::_shared_classbytes_unloaded = NULL; 72 PerfVariable* ClassLoadingService::_class_methods_size = NULL; 73 74 void ClassLoadingService::init() { 75 EXCEPTION_MARK; 76 77 // These counters are for java.lang.management API support. 78 // They are created even if -XX:-UsePerfData is set and in 79 // that case, they will be allocated on C heap. 80 _classes_loaded_count = 81 PerfDataManager::create_counter(JAVA_CLS, "loadedClasses", 82 PerfData::U_Events, CHECK); 83 84 _classes_unloaded_count = 85 PerfDataManager::create_counter(JAVA_CLS, "unloadedClasses", 86 PerfData::U_Events, CHECK); 87 88 _shared_classes_loaded_count = 89 PerfDataManager::create_counter(JAVA_CLS, "sharedLoadedClasses", 90 PerfData::U_Events, CHECK); 91 92 _shared_classes_unloaded_count = 93 PerfDataManager::create_counter(JAVA_CLS, "sharedUnloadedClasses", 94 PerfData::U_Events, CHECK); 95 96 if (UsePerfData) { 97 _classbytes_loaded = 98 PerfDataManager::create_counter(SUN_CLS, "loadedBytes", 99 PerfData::U_Bytes, CHECK); 100 101 _classbytes_unloaded = 102 PerfDataManager::create_counter(SUN_CLS, "unloadedBytes", 103 PerfData::U_Bytes, CHECK); 104 _shared_classbytes_loaded = 105 PerfDataManager::create_counter(SUN_CLS, "sharedLoadedBytes", 106 PerfData::U_Bytes, CHECK); 107 108 _shared_classbytes_unloaded = 109 PerfDataManager::create_counter(SUN_CLS, "sharedUnloadedBytes", 110 PerfData::U_Bytes, CHECK); 111 _class_methods_size = 112 PerfDataManager::create_variable(SUN_CLS, "methodBytes", 113 PerfData::U_Bytes, CHECK); 114 } 115 } 116 117 void ClassLoadingService::notify_class_unloaded(instanceKlass* k) { 118 DTRACE_CLASSLOAD_PROBE(unloaded, k, false); 119 // Classes that can be unloaded must be non-shared 120 _classes_unloaded_count->inc(); 121 122 if (UsePerfData) { 123 // add the class size 124 size_t size = compute_class_size(k); 125 _classbytes_unloaded->inc(size); 126 127 // Compute method size & subtract from running total. 128 // We are called during phase 1 of mark sweep, so it's 129 // still ok to iterate through methodOops here. 130 objArrayOop methods = k->methods(); 131 for (int i = 0; i < methods->length(); i++) { 132 _class_methods_size->inc(-methods->obj_at(i)->size()); 133 } 134 } 135 136 if (TraceClassUnloading) { 137 ResourceMark rm; 138 tty->print_cr("[Unloading class %s]", k->external_name()); 139 } 140 } 141 142 void ClassLoadingService::notify_class_loaded(instanceKlass* k, bool shared_class) { 143 DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class); 144 PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count 145 : _classes_loaded_count); 146 // increment the count 147 classes_counter->inc(); 148 149 if (UsePerfData) { 150 PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded 151 : _classbytes_loaded); 152 // add the class size 153 size_t size = compute_class_size(k); 154 classbytes_counter->inc(size); 155 } 156 } 157 158 size_t ClassLoadingService::compute_class_size(instanceKlass* k) { 159 // lifted from ClassStatistics.do_class(klassOop k) 160 161 size_t class_size = 0; 162 163 class_size += k->as_klassOop()->size(); 164 165 if (k->oop_is_instance()) { 166 class_size += k->methods()->size(); 167 class_size += k->constants()->size(); 168 class_size += k->local_interfaces()->size(); 169 class_size += k->transitive_interfaces()->size(); 170 // We do not have to count implementors, since we only store one! 171 class_size += k->fields()->size(); 172 } 173 return class_size * oopSize; 174 } 175 176 177 bool ClassLoadingService::set_verbose(bool verbose) { 178 MutexLocker m(Management_lock); 179 180 // verbose will be set to the previous value 181 bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassLoading", &verbose, MANAGEMENT); 182 assert(succeed, "Setting TraceClassLoading flag fails"); 183 reset_trace_class_unloading(); 184 185 return verbose; 186 } 187 188 // Caller to this function must own Management_lock 189 void ClassLoadingService::reset_trace_class_unloading() { 190 assert(Management_lock->owned_by_self(), "Must own the Management_lock"); 191 bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose(); 192 bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassUnloading", &value, MANAGEMENT); 193 assert(succeed, "Setting TraceClassUnLoading flag fails"); 194 } 195 196 GrowableArray<KlassHandle>* LoadedClassesEnumerator::_loaded_classes = NULL; 197 Thread* LoadedClassesEnumerator::_current_thread = NULL; 198 199 LoadedClassesEnumerator::LoadedClassesEnumerator(Thread* cur_thread) { 200 assert(cur_thread == Thread::current(), "Check current thread"); 201 202 int init_size = ClassLoadingService::loaded_class_count(); 203 _klass_handle_array = new GrowableArray<KlassHandle>(init_size); 204 205 // For consistency of the loaded classes, grab the SystemDictionary lock 206 MutexLocker sd_mutex(SystemDictionary_lock); 207 208 // Set _loaded_classes and _current_thread and begin enumerating all classes. 209 // Only one thread will do the enumeration at a time. 210 // These static variables are needed and they are used by the static method 211 // add_loaded_class called from classes_do(). 212 _loaded_classes = _klass_handle_array; 213 _current_thread = cur_thread; 214 215 SystemDictionary::classes_do(&add_loaded_class); 216 217 // FIXME: Exclude array klasses for now 218 // Universe::basic_type_classes_do(&add_loaded_class); 219 }