src/share/vm/services/classLoadingService.cpp

Sat, 01 Sep 2012 13:25:18 -0400

author
coleenp
date
Sat, 01 Sep 2012 13:25:18 -0400
changeset 4037
da91efe96a93
parent 3202
436b4a3231bf
child 4165
fb19af007ffc
permissions
-rw-r--r--

6964458: Reimplement class meta-data storage to use native memory
Summary: Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
Contributed-by: jmasa <jon.masamitsu@oracle.com>, stefank <stefan.karlsson@oracle.com>, mgerdin <mikael.gerdin@oracle.com>, never <tom.rodriguez@oracle.com>

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

mercurial