Wed, 03 Jul 2019 20:42:37 +0800
Merge
1 /*
2 * Copyright (c) 2003, 2015, 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 */
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 #include "utilities/macros.hpp"
36 #ifdef DTRACE_ENABLED
38 // Only bother with this argument setup if dtrace is available
40 #ifndef USDT2
42 HS_DTRACE_PROBE_DECL4(hotspot, class__loaded, char*, int, oop, bool);
43 HS_DTRACE_PROBE_DECL4(hotspot, class__unloaded, char*, int, oop, bool);
45 #define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \
46 { \
47 char* data = NULL; \
48 int len = 0; \
49 Symbol* name = (clss)->name(); \
50 if (name != NULL) { \
51 data = (char*)name->bytes(); \
52 len = name->utf8_length(); \
53 } \
54 HS_DTRACE_PROBE4(hotspot, class__##type, \
55 data, len, (void *)(clss)->class_loader(), (shared)); \
56 }
58 #else /* USDT2 */
60 #define HOTSPOT_CLASS_unloaded HOTSPOT_CLASS_UNLOADED
61 #define HOTSPOT_CLASS_loaded HOTSPOT_CLASS_LOADED
62 #define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \
63 { \
64 char* data = NULL; \
65 int len = 0; \
66 Symbol* name = (clss)->name(); \
67 if (name != NULL) { \
68 data = (char*)name->bytes(); \
69 len = name->utf8_length(); \
70 } \
71 HOTSPOT_CLASS_##type( /* type = unloaded, loaded */ \
72 data, len, (clss)->class_loader(), (shared)); \
73 }
75 #endif /* USDT2 */
76 #else // ndef DTRACE_ENABLED
78 #define DTRACE_CLASSLOAD_PROBE(type, clss, shared)
80 #endif
82 #if INCLUDE_MANAGEMENT
83 // counters for classes loaded from class files
84 PerfCounter* ClassLoadingService::_classes_loaded_count = NULL;
85 PerfCounter* ClassLoadingService::_classes_unloaded_count = NULL;
86 PerfCounter* ClassLoadingService::_classbytes_loaded = NULL;
87 PerfCounter* ClassLoadingService::_classbytes_unloaded = NULL;
89 // counters for classes loaded from shared archive
90 PerfCounter* ClassLoadingService::_shared_classes_loaded_count = NULL;
91 PerfCounter* ClassLoadingService::_shared_classes_unloaded_count = NULL;
92 PerfCounter* ClassLoadingService::_shared_classbytes_loaded = NULL;
93 PerfCounter* ClassLoadingService::_shared_classbytes_unloaded = NULL;
94 PerfVariable* ClassLoadingService::_class_methods_size = NULL;
96 void ClassLoadingService::init() {
97 EXCEPTION_MARK;
99 // These counters are for java.lang.management API support.
100 // They are created even if -XX:-UsePerfData is set and in
101 // that case, they will be allocated on C heap.
102 _classes_loaded_count =
103 PerfDataManager::create_counter(JAVA_CLS, "loadedClasses",
104 PerfData::U_Events, CHECK);
106 _classes_unloaded_count =
107 PerfDataManager::create_counter(JAVA_CLS, "unloadedClasses",
108 PerfData::U_Events, CHECK);
110 _shared_classes_loaded_count =
111 PerfDataManager::create_counter(JAVA_CLS, "sharedLoadedClasses",
112 PerfData::U_Events, CHECK);
114 _shared_classes_unloaded_count =
115 PerfDataManager::create_counter(JAVA_CLS, "sharedUnloadedClasses",
116 PerfData::U_Events, CHECK);
118 if (UsePerfData) {
119 _classbytes_loaded =
120 PerfDataManager::create_counter(SUN_CLS, "loadedBytes",
121 PerfData::U_Bytes, CHECK);
123 _classbytes_unloaded =
124 PerfDataManager::create_counter(SUN_CLS, "unloadedBytes",
125 PerfData::U_Bytes, CHECK);
126 _shared_classbytes_loaded =
127 PerfDataManager::create_counter(SUN_CLS, "sharedLoadedBytes",
128 PerfData::U_Bytes, CHECK);
130 _shared_classbytes_unloaded =
131 PerfDataManager::create_counter(SUN_CLS, "sharedUnloadedBytes",
132 PerfData::U_Bytes, CHECK);
133 _class_methods_size =
134 PerfDataManager::create_variable(SUN_CLS, "methodBytes",
135 PerfData::U_Bytes, CHECK);
136 }
137 }
139 void ClassLoadingService::notify_class_unloaded(InstanceKlass* k) {
140 DTRACE_CLASSLOAD_PROBE(unloaded, k, false);
141 // Classes that can be unloaded must be non-shared
142 _classes_unloaded_count->inc();
144 if (UsePerfData) {
145 // add the class size
146 size_t size = compute_class_size(k);
147 _classbytes_unloaded->inc(size);
149 // Compute method size & subtract from running total.
150 // We are called during phase 1 of mark sweep, so it's
151 // still ok to iterate through Method*s here.
152 Array<Method*>* methods = k->methods();
153 for (int i = 0; i < methods->length(); i++) {
154 _class_methods_size->inc(-methods->at(i)->size());
155 }
156 }
158 if (TraceClassUnloading) {
159 ResourceMark rm;
160 tty->print_cr("[Unloading class %s " INTPTR_FORMAT "]", k->external_name(), p2i(k));
161 }
162 }
164 void ClassLoadingService::notify_class_loaded(InstanceKlass* k, bool shared_class) {
165 DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class);
166 PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count
167 : _classes_loaded_count);
168 // increment the count
169 classes_counter->inc();
171 if (UsePerfData) {
172 PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded
173 : _classbytes_loaded);
174 // add the class size
175 size_t size = compute_class_size(k);
176 classbytes_counter->inc(size);
177 }
178 }
180 size_t ClassLoadingService::compute_class_size(InstanceKlass* k) {
181 // lifted from ClassStatistics.do_class(Klass* k)
183 size_t class_size = 0;
185 class_size += k->size();
187 if (k->oop_is_instance()) {
188 class_size += k->methods()->size();
189 // FIXME: Need to count the contents of methods
190 class_size += k->constants()->size();
191 class_size += k->local_interfaces()->size();
192 class_size += k->transitive_interfaces()->size();
193 // We do not have to count implementors, since we only store one!
194 // FIXME: How should these be accounted for, now when they have moved.
195 //class_size += k->fields()->size();
196 }
197 return class_size * oopSize;
198 }
201 bool ClassLoadingService::set_verbose(bool verbose) {
202 MutexLocker m(Management_lock);
204 // verbose will be set to the previous value
205 bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassLoading", &verbose, Flag::MANAGEMENT);
206 assert(succeed, "Setting TraceClassLoading flag fails");
207 reset_trace_class_unloading();
209 return verbose;
210 }
212 // Caller to this function must own Management_lock
213 void ClassLoadingService::reset_trace_class_unloading() {
214 assert(Management_lock->owned_by_self(), "Must own the Management_lock");
215 bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose();
216 bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassUnloading", &value, Flag::MANAGEMENT);
217 assert(succeed, "Setting TraceClassUnLoading flag fails");
218 }
220 GrowableArray<KlassHandle>* LoadedClassesEnumerator::_loaded_classes = NULL;
221 Thread* LoadedClassesEnumerator::_current_thread = NULL;
223 LoadedClassesEnumerator::LoadedClassesEnumerator(Thread* cur_thread) {
224 assert(cur_thread == Thread::current(), "Check current thread");
226 int init_size = ClassLoadingService::loaded_class_count();
227 _klass_handle_array = new GrowableArray<KlassHandle>(init_size);
229 // For consistency of the loaded classes, grab the SystemDictionary lock
230 MutexLocker sd_mutex(SystemDictionary_lock);
232 // Set _loaded_classes and _current_thread and begin enumerating all classes.
233 // Only one thread will do the enumeration at a time.
234 // These static variables are needed and they are used by the static method
235 // add_loaded_class called from classes_do().
236 _loaded_classes = _klass_handle_array;
237 _current_thread = cur_thread;
239 SystemDictionary::classes_do(&add_loaded_class);
241 // FIXME: Exclude array klasses for now
242 // Universe::basic_type_classes_do(&add_loaded_class);
243 }
245 #endif // INCLUDE_MANAGEMENT