Sun, 03 Feb 2013 22:28:08 +0400
8002048: Protocol to discovery of manageable Java processes on a network
Summary: Introduce a protocol to discover manageble Java instances across a network subnet, JDP
Reviewed-by: sla, dfuchs
1 /*
2 * Copyright (c) 2003, 2012, 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"
35 #ifdef DTRACE_ENABLED
37 // Only bother with this argument setup if dtrace is available
39 #ifndef USDT2
41 HS_DTRACE_PROBE_DECL4(hotspot, class__loaded, char*, int, oop, bool);
42 HS_DTRACE_PROBE_DECL4(hotspot, class__unloaded, char*, int, oop, bool);
44 #define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \
45 { \
46 char* data = NULL; \
47 int len = 0; \
48 Symbol* name = (clss)->name(); \
49 if (name != NULL) { \
50 data = (char*)name->bytes(); \
51 len = name->utf8_length(); \
52 } \
53 HS_DTRACE_PROBE4(hotspot, class__##type, \
54 data, len, (clss)->class_loader(), (shared)); \
55 }
57 #else /* USDT2 */
59 #define HOTSPOT_CLASS_unloaded HOTSPOT_CLASS_UNLOADED
60 #define HOTSPOT_CLASS_loaded HOTSPOT_CLASS_LOADED
61 #define DTRACE_CLASSLOAD_PROBE(type, clss, shared) \
62 { \
63 char* data = NULL; \
64 int len = 0; \
65 Symbol* name = (clss)->name(); \
66 if (name != NULL) { \
67 data = (char*)name->bytes(); \
68 len = name->utf8_length(); \
69 } \
70 HOTSPOT_CLASS_##type( /* type = unloaded, loaded */ \
71 data, len, (clss)->class_loader(), (shared)); \
72 }
74 #endif /* USDT2 */
75 #else // ndef DTRACE_ENABLED
77 #define DTRACE_CLASSLOAD_PROBE(type, clss, shared)
79 #endif
81 #if INCLUDE_MANAGEMENT
82 // counters for classes loaded from class files
83 PerfCounter* ClassLoadingService::_classes_loaded_count = NULL;
84 PerfCounter* ClassLoadingService::_classes_unloaded_count = NULL;
85 PerfCounter* ClassLoadingService::_classbytes_loaded = NULL;
86 PerfCounter* ClassLoadingService::_classbytes_unloaded = NULL;
88 // counters for classes loaded from shared archive
89 PerfCounter* ClassLoadingService::_shared_classes_loaded_count = NULL;
90 PerfCounter* ClassLoadingService::_shared_classes_unloaded_count = NULL;
91 PerfCounter* ClassLoadingService::_shared_classbytes_loaded = NULL;
92 PerfCounter* ClassLoadingService::_shared_classbytes_unloaded = NULL;
93 PerfVariable* ClassLoadingService::_class_methods_size = NULL;
95 void ClassLoadingService::init() {
96 EXCEPTION_MARK;
98 // These counters are for java.lang.management API support.
99 // They are created even if -XX:-UsePerfData is set and in
100 // that case, they will be allocated on C heap.
101 _classes_loaded_count =
102 PerfDataManager::create_counter(JAVA_CLS, "loadedClasses",
103 PerfData::U_Events, CHECK);
105 _classes_unloaded_count =
106 PerfDataManager::create_counter(JAVA_CLS, "unloadedClasses",
107 PerfData::U_Events, CHECK);
109 _shared_classes_loaded_count =
110 PerfDataManager::create_counter(JAVA_CLS, "sharedLoadedClasses",
111 PerfData::U_Events, CHECK);
113 _shared_classes_unloaded_count =
114 PerfDataManager::create_counter(JAVA_CLS, "sharedUnloadedClasses",
115 PerfData::U_Events, CHECK);
117 if (UsePerfData) {
118 _classbytes_loaded =
119 PerfDataManager::create_counter(SUN_CLS, "loadedBytes",
120 PerfData::U_Bytes, CHECK);
122 _classbytes_unloaded =
123 PerfDataManager::create_counter(SUN_CLS, "unloadedBytes",
124 PerfData::U_Bytes, CHECK);
125 _shared_classbytes_loaded =
126 PerfDataManager::create_counter(SUN_CLS, "sharedLoadedBytes",
127 PerfData::U_Bytes, CHECK);
129 _shared_classbytes_unloaded =
130 PerfDataManager::create_counter(SUN_CLS, "sharedUnloadedBytes",
131 PerfData::U_Bytes, CHECK);
132 _class_methods_size =
133 PerfDataManager::create_variable(SUN_CLS, "methodBytes",
134 PerfData::U_Bytes, CHECK);
135 }
136 }
138 void ClassLoadingService::notify_class_unloaded(InstanceKlass* k) {
139 DTRACE_CLASSLOAD_PROBE(unloaded, k, false);
140 // Classes that can be unloaded must be non-shared
141 _classes_unloaded_count->inc();
143 if (UsePerfData) {
144 // add the class size
145 size_t size = compute_class_size(k);
146 _classbytes_unloaded->inc(size);
148 // Compute method size & subtract from running total.
149 // We are called during phase 1 of mark sweep, so it's
150 // still ok to iterate through Method*s here.
151 Array<Method*>* methods = k->methods();
152 for (int i = 0; i < methods->length(); i++) {
153 _class_methods_size->inc(-methods->at(i)->size());
154 }
155 }
157 if (TraceClassUnloading) {
158 ResourceMark rm;
159 tty->print_cr("[Unloading class %s " INTPTR_FORMAT "]", k->external_name(), k);
160 }
161 }
163 void ClassLoadingService::notify_class_loaded(InstanceKlass* k, bool shared_class) {
164 DTRACE_CLASSLOAD_PROBE(loaded, k, shared_class);
165 PerfCounter* classes_counter = (shared_class ? _shared_classes_loaded_count
166 : _classes_loaded_count);
167 // increment the count
168 classes_counter->inc();
170 if (UsePerfData) {
171 PerfCounter* classbytes_counter = (shared_class ? _shared_classbytes_loaded
172 : _classbytes_loaded);
173 // add the class size
174 size_t size = compute_class_size(k);
175 classbytes_counter->inc(size);
176 }
177 }
179 size_t ClassLoadingService::compute_class_size(InstanceKlass* k) {
180 // lifted from ClassStatistics.do_class(Klass* k)
182 size_t class_size = 0;
184 class_size += k->size();
186 if (k->oop_is_instance()) {
187 class_size += k->methods()->size();
188 // FIXME: Need to count the contents of methods
189 class_size += k->constants()->size();
190 class_size += k->local_interfaces()->size();
191 class_size += k->transitive_interfaces()->size();
192 // We do not have to count implementors, since we only store one!
193 // FIXME: How should these be accounted for, now when they have moved.
194 //class_size += k->fields()->size();
195 }
196 return class_size * oopSize;
197 }
200 bool ClassLoadingService::set_verbose(bool verbose) {
201 MutexLocker m(Management_lock);
203 // verbose will be set to the previous value
204 bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassLoading", &verbose, MANAGEMENT);
205 assert(succeed, "Setting TraceClassLoading flag fails");
206 reset_trace_class_unloading();
208 return verbose;
209 }
211 // Caller to this function must own Management_lock
212 void ClassLoadingService::reset_trace_class_unloading() {
213 assert(Management_lock->owned_by_self(), "Must own the Management_lock");
214 bool value = MemoryService::get_verbose() || ClassLoadingService::get_verbose();
215 bool succeed = CommandLineFlags::boolAtPut((char*)"TraceClassUnloading", &value, MANAGEMENT);
216 assert(succeed, "Setting TraceClassUnLoading flag fails");
217 }
219 GrowableArray<KlassHandle>* LoadedClassesEnumerator::_loaded_classes = NULL;
220 Thread* LoadedClassesEnumerator::_current_thread = NULL;
222 LoadedClassesEnumerator::LoadedClassesEnumerator(Thread* cur_thread) {
223 assert(cur_thread == Thread::current(), "Check current thread");
225 int init_size = ClassLoadingService::loaded_class_count();
226 _klass_handle_array = new GrowableArray<KlassHandle>(init_size);
228 // For consistency of the loaded classes, grab the SystemDictionary lock
229 MutexLocker sd_mutex(SystemDictionary_lock);
231 // Set _loaded_classes and _current_thread and begin enumerating all classes.
232 // Only one thread will do the enumeration at a time.
233 // These static variables are needed and they are used by the static method
234 // add_loaded_class called from classes_do().
235 _loaded_classes = _klass_handle_array;
236 _current_thread = cur_thread;
238 SystemDictionary::classes_do(&add_loaded_class);
240 // FIXME: Exclude array klasses for now
241 // Universe::basic_type_classes_do(&add_loaded_class);
242 }
244 #endif // INCLUDE_MANAGEMENT