annotate src/share/vm/services/management.cpp @ 2453:78542e2b5e35

7036199: Adding a notification to the implementation of GarbageCollectorMXBeans Summary: Add a notification to the GarbageCollectorMXBeans Reviewed-by: acorn, mchung
author fparain
date Thu, 12 May 2011 10:30:11 -0700
parents 02f78cfa4656
children
rev   line source
duke@0 1 /*
phh@1988 2 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "classfile/systemDictionary.hpp"
stefank@1879 27 #include "compiler/compileBroker.hpp"
stefank@1879 28 #include "memory/iterator.hpp"
stefank@1879 29 #include "memory/oopFactory.hpp"
stefank@1879 30 #include "memory/resourceArea.hpp"
stefank@1879 31 #include "oops/klass.hpp"
stefank@1879 32 #include "oops/klassOop.hpp"
stefank@1879 33 #include "oops/objArrayKlass.hpp"
stefank@1879 34 #include "oops/oop.inline.hpp"
stefank@1879 35 #include "runtime/arguments.hpp"
stefank@1879 36 #include "runtime/handles.inline.hpp"
stefank@1879 37 #include "runtime/interfaceSupport.hpp"
stefank@1879 38 #include "runtime/javaCalls.hpp"
stefank@1879 39 #include "runtime/jniHandles.hpp"
stefank@1879 40 #include "runtime/os.hpp"
kamg@2076 41 #include "runtime/serviceThread.hpp"
stefank@1879 42 #include "services/classLoadingService.hpp"
stefank@1879 43 #include "services/heapDumper.hpp"
stefank@1879 44 #include "services/lowMemoryDetector.hpp"
fparain@2453 45 #include "services/gcNotifier.hpp"
stefank@1879 46 #include "services/management.hpp"
stefank@1879 47 #include "services/memoryManager.hpp"
stefank@1879 48 #include "services/memoryPool.hpp"
stefank@1879 49 #include "services/memoryService.hpp"
stefank@1879 50 #include "services/runtimeService.hpp"
stefank@1879 51 #include "services/threadService.hpp"
duke@0 52
duke@0 53 PerfVariable* Management::_begin_vm_creation_time = NULL;
duke@0 54 PerfVariable* Management::_end_vm_creation_time = NULL;
duke@0 55 PerfVariable* Management::_vm_init_done_time = NULL;
duke@0 56
duke@0 57 klassOop Management::_sensor_klass = NULL;
duke@0 58 klassOop Management::_threadInfo_klass = NULL;
duke@0 59 klassOop Management::_memoryUsage_klass = NULL;
duke@0 60 klassOop Management::_memoryPoolMXBean_klass = NULL;
duke@0 61 klassOop Management::_memoryManagerMXBean_klass = NULL;
duke@0 62 klassOop Management::_garbageCollectorMXBean_klass = NULL;
duke@0 63 klassOop Management::_managementFactory_klass = NULL;
fparain@2453 64 klassOop Management::_garbageCollectorImpl_klass = NULL;
fparain@2453 65 klassOop Management::_gcInfo_klass = NULL;
duke@0 66
duke@0 67 jmmOptionalSupport Management::_optional_support = {0};
duke@0 68 TimeStamp Management::_stamp;
duke@0 69
duke@0 70 void management_init() {
duke@0 71 Management::init();
duke@0 72 ThreadService::init();
duke@0 73 RuntimeService::init();
duke@0 74 ClassLoadingService::init();
duke@0 75 }
duke@0 76
duke@0 77 void Management::init() {
duke@0 78 EXCEPTION_MARK;
duke@0 79
duke@0 80 // These counters are for java.lang.management API support.
duke@0 81 // They are created even if -XX:-UsePerfData is set and in
duke@0 82 // that case, they will be allocated on C heap.
duke@0 83
duke@0 84 _begin_vm_creation_time =
duke@0 85 PerfDataManager::create_variable(SUN_RT, "createVmBeginTime",
duke@0 86 PerfData::U_None, CHECK);
duke@0 87
duke@0 88 _end_vm_creation_time =
duke@0 89 PerfDataManager::create_variable(SUN_RT, "createVmEndTime",
duke@0 90 PerfData::U_None, CHECK);
duke@0 91
duke@0 92 _vm_init_done_time =
duke@0 93 PerfDataManager::create_variable(SUN_RT, "vmInitDoneTime",
duke@0 94 PerfData::U_None, CHECK);
duke@0 95
duke@0 96 // Initialize optional support
duke@0 97 _optional_support.isLowMemoryDetectionSupported = 1;
duke@0 98 _optional_support.isCompilationTimeMonitoringSupported = 1;
duke@0 99 _optional_support.isThreadContentionMonitoringSupported = 1;
duke@0 100
duke@0 101 if (os::is_thread_cpu_time_supported()) {
duke@0 102 _optional_support.isCurrentThreadCpuTimeSupported = 1;
duke@0 103 _optional_support.isOtherThreadCpuTimeSupported = 1;
duke@0 104 } else {
duke@0 105 _optional_support.isCurrentThreadCpuTimeSupported = 0;
duke@0 106 _optional_support.isOtherThreadCpuTimeSupported = 0;
duke@0 107 }
phh@1988 108
duke@0 109 _optional_support.isBootClassPathSupported = 1;
duke@0 110 _optional_support.isObjectMonitorUsageSupported = 1;
duke@0 111 #ifndef SERVICES_KERNEL
duke@0 112 // This depends on the heap inspector
duke@0 113 _optional_support.isSynchronizerUsageSupported = 1;
duke@0 114 #endif // SERVICES_KERNEL
phh@1988 115 _optional_support.isThreadAllocatedMemorySupported = 1;
duke@0 116 }
duke@0 117
duke@0 118 void Management::initialize(TRAPS) {
kamg@2076 119 // Start the service thread
kamg@2076 120 ServiceThread::initialize();
duke@0 121
duke@0 122 if (ManagementServer) {
duke@0 123 ResourceMark rm(THREAD);
duke@0 124 HandleMark hm(THREAD);
duke@0 125
duke@0 126 // Load and initialize the sun.management.Agent class
duke@0 127 // invoke startAgent method to start the management server
duke@0 128 Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
coleenp@2062 129 klassOop k = SystemDictionary::resolve_or_fail(vmSymbols::sun_management_Agent(),
duke@0 130 loader,
duke@0 131 Handle(),
duke@0 132 true,
duke@0 133 CHECK);
duke@0 134 instanceKlassHandle ik (THREAD, k);
duke@0 135
duke@0 136 JavaValue result(T_VOID);
duke@0 137 JavaCalls::call_static(&result,
duke@0 138 ik,
coleenp@2062 139 vmSymbols::startAgent_name(),
coleenp@2062 140 vmSymbols::void_method_signature(),
duke@0 141 CHECK);
duke@0 142 }
duke@0 143 }
duke@0 144
duke@0 145 void Management::get_optional_support(jmmOptionalSupport* support) {
duke@0 146 memcpy(support, &_optional_support, sizeof(jmmOptionalSupport));
duke@0 147 }
duke@0 148
coleenp@2062 149 klassOop Management::load_and_initialize_klass(Symbol* sh, TRAPS) {
duke@0 150 klassOop k = SystemDictionary::resolve_or_fail(sh, true, CHECK_NULL);
duke@0 151 instanceKlassHandle ik (THREAD, k);
duke@0 152 if (ik->should_be_initialized()) {
duke@0 153 ik->initialize(CHECK_NULL);
duke@0 154 }
duke@0 155 return ik();
duke@0 156 }
duke@0 157
duke@0 158 void Management::record_vm_startup_time(jlong begin, jlong duration) {
duke@0 159 // if the performance counter is not initialized,
duke@0 160 // then vm initialization failed; simply return.
duke@0 161 if (_begin_vm_creation_time == NULL) return;
duke@0 162
duke@0 163 _begin_vm_creation_time->set_value(begin);
duke@0 164 _end_vm_creation_time->set_value(begin + duration);
duke@0 165 PerfMemory::set_accessible(true);
duke@0 166 }
duke@0 167
duke@0 168 jlong Management::timestamp() {
duke@0 169 TimeStamp t;
duke@0 170 t.update();
duke@0 171 return t.ticks() - _stamp.ticks();
duke@0 172 }
duke@0 173
duke@0 174 void Management::oops_do(OopClosure* f) {
duke@0 175 MemoryService::oops_do(f);
duke@0 176 ThreadService::oops_do(f);
duke@0 177
duke@0 178 f->do_oop((oop*) &_sensor_klass);
duke@0 179 f->do_oop((oop*) &_threadInfo_klass);
duke@0 180 f->do_oop((oop*) &_memoryUsage_klass);
duke@0 181 f->do_oop((oop*) &_memoryPoolMXBean_klass);
duke@0 182 f->do_oop((oop*) &_memoryManagerMXBean_klass);
duke@0 183 f->do_oop((oop*) &_garbageCollectorMXBean_klass);
duke@0 184 f->do_oop((oop*) &_managementFactory_klass);
fparain@2453 185 f->do_oop((oop*) &_garbageCollectorImpl_klass);
fparain@2453 186 f->do_oop((oop*) &_gcInfo_klass);
duke@0 187 }
duke@0 188
duke@0 189 klassOop Management::java_lang_management_ThreadInfo_klass(TRAPS) {
duke@0 190 if (_threadInfo_klass == NULL) {
coleenp@2062 191 _threadInfo_klass = load_and_initialize_klass(vmSymbols::java_lang_management_ThreadInfo(), CHECK_NULL);
duke@0 192 }
duke@0 193 return _threadInfo_klass;
duke@0 194 }
duke@0 195
duke@0 196 klassOop Management::java_lang_management_MemoryUsage_klass(TRAPS) {
duke@0 197 if (_memoryUsage_klass == NULL) {
coleenp@2062 198 _memoryUsage_klass = load_and_initialize_klass(vmSymbols::java_lang_management_MemoryUsage(), CHECK_NULL);
duke@0 199 }
duke@0 200 return _memoryUsage_klass;
duke@0 201 }
duke@0 202
duke@0 203 klassOop Management::java_lang_management_MemoryPoolMXBean_klass(TRAPS) {
duke@0 204 if (_memoryPoolMXBean_klass == NULL) {
coleenp@2062 205 _memoryPoolMXBean_klass = load_and_initialize_klass(vmSymbols::java_lang_management_MemoryPoolMXBean(), CHECK_NULL);
duke@0 206 }
duke@0 207 return _memoryPoolMXBean_klass;
duke@0 208 }
duke@0 209
duke@0 210 klassOop Management::java_lang_management_MemoryManagerMXBean_klass(TRAPS) {
duke@0 211 if (_memoryManagerMXBean_klass == NULL) {
coleenp@2062 212 _memoryManagerMXBean_klass = load_and_initialize_klass(vmSymbols::java_lang_management_MemoryManagerMXBean(), CHECK_NULL);
duke@0 213 }
duke@0 214 return _memoryManagerMXBean_klass;
duke@0 215 }
duke@0 216
duke@0 217 klassOop Management::java_lang_management_GarbageCollectorMXBean_klass(TRAPS) {
duke@0 218 if (_garbageCollectorMXBean_klass == NULL) {
coleenp@2062 219 _garbageCollectorMXBean_klass = load_and_initialize_klass(vmSymbols::java_lang_management_GarbageCollectorMXBean(), CHECK_NULL);
duke@0 220 }
duke@0 221 return _garbageCollectorMXBean_klass;
duke@0 222 }
duke@0 223
duke@0 224 klassOop Management::sun_management_Sensor_klass(TRAPS) {
duke@0 225 if (_sensor_klass == NULL) {
coleenp@2062 226 _sensor_klass = load_and_initialize_klass(vmSymbols::sun_management_Sensor(), CHECK_NULL);
duke@0 227 }
duke@0 228 return _sensor_klass;
duke@0 229 }
duke@0 230
duke@0 231 klassOop Management::sun_management_ManagementFactory_klass(TRAPS) {
duke@0 232 if (_managementFactory_klass == NULL) {
coleenp@2062 233 _managementFactory_klass = load_and_initialize_klass(vmSymbols::sun_management_ManagementFactory(), CHECK_NULL);
duke@0 234 }
duke@0 235 return _managementFactory_klass;
duke@0 236 }
duke@0 237
fparain@2453 238 klassOop Management::sun_management_GarbageCollectorImpl_klass(TRAPS) {
fparain@2453 239 if (_garbageCollectorImpl_klass == NULL) {
fparain@2453 240 _garbageCollectorImpl_klass = load_and_initialize_klass(vmSymbols::sun_management_GarbageCollectorImpl(), CHECK_NULL);
fparain@2453 241 }
fparain@2453 242 return _garbageCollectorImpl_klass;
fparain@2453 243 }
fparain@2453 244
fparain@2453 245 klassOop Management::com_sun_management_GcInfo_klass(TRAPS) {
fparain@2453 246 if (_gcInfo_klass == NULL) {
fparain@2453 247 _gcInfo_klass = load_and_initialize_klass(vmSymbols::com_sun_management_GcInfo(), CHECK_NULL);
fparain@2453 248 }
fparain@2453 249 return _gcInfo_klass;
fparain@2453 250 }
fparain@2453 251
duke@0 252 static void initialize_ThreadInfo_constructor_arguments(JavaCallArguments* args, ThreadSnapshot* snapshot, TRAPS) {
duke@0 253 Handle snapshot_thread(THREAD, snapshot->threadObj());
duke@0 254
duke@0 255 jlong contended_time;
duke@0 256 jlong waited_time;
duke@0 257 if (ThreadService::is_thread_monitoring_contention()) {
duke@0 258 contended_time = Management::ticks_to_ms(snapshot->contended_enter_ticks());
duke@0 259 waited_time = Management::ticks_to_ms(snapshot->monitor_wait_ticks() + snapshot->sleep_ticks());
duke@0 260 } else {
duke@0 261 // set them to -1 if thread contention monitoring is disabled.
duke@0 262 contended_time = max_julong;
duke@0 263 waited_time = max_julong;
duke@0 264 }
duke@0 265
duke@0 266 int thread_status = snapshot->thread_status();
duke@0 267 assert((thread_status & JMM_THREAD_STATE_FLAG_MASK) == 0, "Flags already set in thread_status in Thread object");
duke@0 268 if (snapshot->is_ext_suspended()) {
duke@0 269 thread_status |= JMM_THREAD_STATE_FLAG_SUSPENDED;
duke@0 270 }
duke@0 271 if (snapshot->is_in_native()) {
duke@0 272 thread_status |= JMM_THREAD_STATE_FLAG_NATIVE;
duke@0 273 }
duke@0 274
duke@0 275 ThreadStackTrace* st = snapshot->get_stack_trace();
duke@0 276 Handle stacktrace_h;
duke@0 277 if (st != NULL) {
duke@0 278 stacktrace_h = st->allocate_fill_stack_trace_element_array(CHECK);
duke@0 279 } else {
duke@0 280 stacktrace_h = Handle();
duke@0 281 }
duke@0 282
duke@0 283 args->push_oop(snapshot_thread);
duke@0 284 args->push_int(thread_status);
duke@0 285 args->push_oop(Handle(THREAD, snapshot->blocker_object()));
duke@0 286 args->push_oop(Handle(THREAD, snapshot->blocker_object_owner()));
duke@0 287 args->push_long(snapshot->contended_enter_count());
duke@0 288 args->push_long(contended_time);
duke@0 289 args->push_long(snapshot->monitor_wait_count() + snapshot->sleep_count());
duke@0 290 args->push_long(waited_time);
duke@0 291 args->push_oop(stacktrace_h);
duke@0 292 }
duke@0 293
duke@0 294 // Helper function to construct a ThreadInfo object
duke@0 295 instanceOop Management::create_thread_info_instance(ThreadSnapshot* snapshot, TRAPS) {
duke@0 296 klassOop k = Management::java_lang_management_ThreadInfo_klass(CHECK_NULL);
duke@0 297 instanceKlassHandle ik (THREAD, k);
duke@0 298
duke@0 299 JavaValue result(T_VOID);
duke@0 300 JavaCallArguments args(14);
duke@0 301
duke@0 302 // First allocate a ThreadObj object and
duke@0 303 // push the receiver as the first argument
duke@0 304 Handle element = ik->allocate_instance_handle(CHECK_NULL);
duke@0 305 args.push_oop(element);
duke@0 306
duke@0 307 // initialize the arguments for the ThreadInfo constructor
duke@0 308 initialize_ThreadInfo_constructor_arguments(&args, snapshot, CHECK_NULL);
duke@0 309
duke@0 310 // Call ThreadInfo constructor with no locked monitors and synchronizers
duke@0 311 JavaCalls::call_special(&result,
duke@0 312 ik,
coleenp@2062 313 vmSymbols::object_initializer_name(),
coleenp@2062 314 vmSymbols::java_lang_management_ThreadInfo_constructor_signature(),
duke@0 315 &args,
duke@0 316 CHECK_NULL);
duke@0 317
duke@0 318 return (instanceOop) element();
duke@0 319 }
duke@0 320
duke@0 321 instanceOop Management::create_thread_info_instance(ThreadSnapshot* snapshot,
duke@0 322 objArrayHandle monitors_array,
duke@0 323 typeArrayHandle depths_array,
duke@0 324 objArrayHandle synchronizers_array,
duke@0 325 TRAPS) {
duke@0 326 klassOop k = Management::java_lang_management_ThreadInfo_klass(CHECK_NULL);
duke@0 327 instanceKlassHandle ik (THREAD, k);
duke@0 328
duke@0 329 JavaValue result(T_VOID);
duke@0 330 JavaCallArguments args(17);
duke@0 331
duke@0 332 // First allocate a ThreadObj object and
duke@0 333 // push the receiver as the first argument
duke@0 334 Handle element = ik->allocate_instance_handle(CHECK_NULL);
duke@0 335 args.push_oop(element);
duke@0 336
duke@0 337 // initialize the arguments for the ThreadInfo constructor
duke@0 338 initialize_ThreadInfo_constructor_arguments(&args, snapshot, CHECK_NULL);
duke@0 339
duke@0 340 // push the locked monitors and synchronizers in the arguments
duke@0 341 args.push_oop(monitors_array);
duke@0 342 args.push_oop(depths_array);
duke@0 343 args.push_oop(synchronizers_array);
duke@0 344
duke@0 345 // Call ThreadInfo constructor with locked monitors and synchronizers
duke@0 346 JavaCalls::call_special(&result,
duke@0 347 ik,
coleenp@2062 348 vmSymbols::object_initializer_name(),
coleenp@2062 349 vmSymbols::java_lang_management_ThreadInfo_with_locks_constructor_signature(),
duke@0 350 &args,
duke@0 351 CHECK_NULL);
duke@0 352
duke@0 353 return (instanceOop) element();
duke@0 354 }
duke@0 355
duke@0 356 // Helper functions
duke@0 357 static JavaThread* find_java_thread_from_id(jlong thread_id) {
duke@0 358 assert(Threads_lock->owned_by_self(), "Must hold Threads_lock");
duke@0 359
duke@0 360 JavaThread* java_thread = NULL;
duke@0 361 // Sequential search for now. Need to do better optimization later.
duke@0 362 for (JavaThread* thread = Threads::first(); thread != NULL; thread = thread->next()) {
duke@0 363 oop tobj = thread->threadObj();
duke@0 364 if (!thread->is_exiting() &&
duke@0 365 tobj != NULL &&
duke@0 366 thread_id == java_lang_Thread::thread_id(tobj)) {
duke@0 367 java_thread = thread;
duke@0 368 break;
duke@0 369 }
duke@0 370 }
duke@0 371 return java_thread;
duke@0 372 }
duke@0 373
duke@0 374 static GCMemoryManager* get_gc_memory_manager_from_jobject(jobject mgr, TRAPS) {
duke@0 375 if (mgr == NULL) {
duke@0 376 THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
duke@0 377 }
duke@0 378 oop mgr_obj = JNIHandles::resolve(mgr);
duke@0 379 instanceHandle h(THREAD, (instanceOop) mgr_obj);
duke@0 380
duke@0 381 klassOop k = Management::java_lang_management_GarbageCollectorMXBean_klass(CHECK_NULL);
duke@0 382 if (!h->is_a(k)) {
duke@0 383 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 384 "the object is not an instance of java.lang.management.GarbageCollectorMXBean class",
duke@0 385 NULL);
duke@0 386 }
duke@0 387
duke@0 388 MemoryManager* gc = MemoryService::get_memory_manager(h);
duke@0 389 if (gc == NULL || !gc->is_gc_memory_manager()) {
duke@0 390 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 391 "Invalid GC memory manager",
duke@0 392 NULL);
duke@0 393 }
duke@0 394 return (GCMemoryManager*) gc;
duke@0 395 }
duke@0 396
duke@0 397 static MemoryPool* get_memory_pool_from_jobject(jobject obj, TRAPS) {
duke@0 398 if (obj == NULL) {
duke@0 399 THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
duke@0 400 }
duke@0 401
duke@0 402 oop pool_obj = JNIHandles::resolve(obj);
duke@0 403 assert(pool_obj->is_instance(), "Should be an instanceOop");
duke@0 404 instanceHandle ph(THREAD, (instanceOop) pool_obj);
duke@0 405
duke@0 406 return MemoryService::get_memory_pool(ph);
duke@0 407 }
duke@0 408
duke@0 409 static void validate_thread_id_array(typeArrayHandle ids_ah, TRAPS) {
duke@0 410 int num_threads = ids_ah->length();
duke@0 411
duke@0 412 // Validate input thread IDs
duke@0 413 int i = 0;
duke@0 414 for (i = 0; i < num_threads; i++) {
duke@0 415 jlong tid = ids_ah->long_at(i);
duke@0 416 if (tid <= 0) {
duke@0 417 // throw exception if invalid thread id.
duke@0 418 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 419 "Invalid thread ID entry");
duke@0 420 }
duke@0 421 }
duke@0 422 }
duke@0 423
duke@0 424 static void validate_thread_info_array(objArrayHandle infoArray_h, TRAPS) {
duke@0 425 // check if the element of infoArray is of type ThreadInfo class
duke@0 426 klassOop threadinfo_klass = Management::java_lang_management_ThreadInfo_klass(CHECK);
duke@0 427 klassOop element_klass = objArrayKlass::cast(infoArray_h->klass())->element_klass();
duke@0 428 if (element_klass != threadinfo_klass) {
duke@0 429 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 430 "infoArray element type is not ThreadInfo class");
duke@0 431 }
duke@0 432 }
duke@0 433
duke@0 434
duke@0 435 static MemoryManager* get_memory_manager_from_jobject(jobject obj, TRAPS) {
duke@0 436 if (obj == NULL) {
duke@0 437 THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
duke@0 438 }
duke@0 439
duke@0 440 oop mgr_obj = JNIHandles::resolve(obj);
duke@0 441 assert(mgr_obj->is_instance(), "Should be an instanceOop");
duke@0 442 instanceHandle mh(THREAD, (instanceOop) mgr_obj);
duke@0 443
duke@0 444 return MemoryService::get_memory_manager(mh);
duke@0 445 }
duke@0 446
duke@0 447 // Returns a version string and sets major and minor version if
duke@0 448 // the input parameters are non-null.
duke@0 449 JVM_LEAF(jint, jmm_GetVersion(JNIEnv *env))
duke@0 450 return JMM_VERSION;
duke@0 451 JVM_END
duke@0 452
duke@0 453 // Gets the list of VM monitoring and management optional supports
duke@0 454 // Returns 0 if succeeded; otherwise returns non-zero.
duke@0 455 JVM_LEAF(jint, jmm_GetOptionalSupport(JNIEnv *env, jmmOptionalSupport* support))
duke@0 456 if (support == NULL) {
duke@0 457 return -1;
duke@0 458 }
duke@0 459 Management::get_optional_support(support);
duke@0 460 return 0;
duke@0 461 JVM_END
duke@0 462
duke@0 463 // Returns a java.lang.String object containing the input arguments to the VM.
duke@0 464 JVM_ENTRY(jobject, jmm_GetInputArguments(JNIEnv *env))
duke@0 465 ResourceMark rm(THREAD);
duke@0 466
duke@0 467 if (Arguments::num_jvm_args() == 0 && Arguments::num_jvm_flags() == 0) {
duke@0 468 return NULL;
duke@0 469 }
duke@0 470
duke@0 471 char** vm_flags = Arguments::jvm_flags_array();
duke@0 472 char** vm_args = Arguments::jvm_args_array();
duke@0 473 int num_flags = Arguments::num_jvm_flags();
duke@0 474 int num_args = Arguments::num_jvm_args();
duke@0 475
duke@0 476 size_t length = 1; // null terminator
duke@0 477 int i;
duke@0 478 for (i = 0; i < num_flags; i++) {
duke@0 479 length += strlen(vm_flags[i]);
duke@0 480 }
duke@0 481 for (i = 0; i < num_args; i++) {
duke@0 482 length += strlen(vm_args[i]);
duke@0 483 }
duke@0 484 // add a space between each argument
duke@0 485 length += num_flags + num_args - 1;
duke@0 486
duke@0 487 // Return the list of input arguments passed to the VM
duke@0 488 // and preserve the order that the VM processes.
duke@0 489 char* args = NEW_RESOURCE_ARRAY(char, length);
duke@0 490 args[0] = '\0';
duke@0 491 // concatenate all jvm_flags
duke@0 492 if (num_flags > 0) {
duke@0 493 strcat(args, vm_flags[0]);
duke@0 494 for (i = 1; i < num_flags; i++) {
duke@0 495 strcat(args, " ");
duke@0 496 strcat(args, vm_flags[i]);
duke@0 497 }
duke@0 498 }
duke@0 499
duke@0 500 if (num_args > 0 && num_flags > 0) {
duke@0 501 // append a space if args already contains one or more jvm_flags
duke@0 502 strcat(args, " ");
duke@0 503 }
duke@0 504
duke@0 505 // concatenate all jvm_args
duke@0 506 if (num_args > 0) {
duke@0 507 strcat(args, vm_args[0]);
duke@0 508 for (i = 1; i < num_args; i++) {
duke@0 509 strcat(args, " ");
duke@0 510 strcat(args, vm_args[i]);
duke@0 511 }
duke@0 512 }
duke@0 513
duke@0 514 Handle hargs = java_lang_String::create_from_platform_dependent_str(args, CHECK_NULL);
duke@0 515 return JNIHandles::make_local(env, hargs());
duke@0 516 JVM_END
duke@0 517
duke@0 518 // Returns an array of java.lang.String object containing the input arguments to the VM.
duke@0 519 JVM_ENTRY(jobjectArray, jmm_GetInputArgumentArray(JNIEnv *env))
duke@0 520 ResourceMark rm(THREAD);
duke@0 521
duke@0 522 if (Arguments::num_jvm_args() == 0 && Arguments::num_jvm_flags() == 0) {
duke@0 523 return NULL;
duke@0 524 }
duke@0 525
duke@0 526 char** vm_flags = Arguments::jvm_flags_array();
duke@0 527 char** vm_args = Arguments::jvm_args_array();
duke@0 528 int num_flags = Arguments::num_jvm_flags();
duke@0 529 int num_args = Arguments::num_jvm_args();
duke@0 530
never@1142 531 instanceKlassHandle ik (THREAD, SystemDictionary::String_klass());
duke@0 532 objArrayOop r = oopFactory::new_objArray(ik(), num_args + num_flags, CHECK_NULL);
duke@0 533 objArrayHandle result_h(THREAD, r);
duke@0 534
duke@0 535 int index = 0;
duke@0 536 for (int j = 0; j < num_flags; j++, index++) {
duke@0 537 Handle h = java_lang_String::create_from_platform_dependent_str(vm_flags[j], CHECK_NULL);
duke@0 538 result_h->obj_at_put(index, h());
duke@0 539 }
duke@0 540 for (int i = 0; i < num_args; i++, index++) {
duke@0 541 Handle h = java_lang_String::create_from_platform_dependent_str(vm_args[i], CHECK_NULL);
duke@0 542 result_h->obj_at_put(index, h());
duke@0 543 }
duke@0 544 return (jobjectArray) JNIHandles::make_local(env, result_h());
duke@0 545 JVM_END
duke@0 546
duke@0 547 // Returns an array of java/lang/management/MemoryPoolMXBean object
duke@0 548 // one for each memory pool if obj == null; otherwise returns
duke@0 549 // an array of memory pools for a given memory manager if
duke@0 550 // it is a valid memory manager.
duke@0 551 JVM_ENTRY(jobjectArray, jmm_GetMemoryPools(JNIEnv* env, jobject obj))
duke@0 552 ResourceMark rm(THREAD);
duke@0 553
duke@0 554 int num_memory_pools;
duke@0 555 MemoryManager* mgr = NULL;
duke@0 556 if (obj == NULL) {
duke@0 557 num_memory_pools = MemoryService::num_memory_pools();
duke@0 558 } else {
duke@0 559 mgr = get_memory_manager_from_jobject(obj, CHECK_NULL);
duke@0 560 if (mgr == NULL) {
duke@0 561 return NULL;
duke@0 562 }
duke@0 563 num_memory_pools = mgr->num_memory_pools();
duke@0 564 }
duke@0 565
duke@0 566 // Allocate the resulting MemoryPoolMXBean[] object
duke@0 567 klassOop k = Management::java_lang_management_MemoryPoolMXBean_klass(CHECK_NULL);
duke@0 568 instanceKlassHandle ik (THREAD, k);
duke@0 569 objArrayOop r = oopFactory::new_objArray(ik(), num_memory_pools, CHECK_NULL);
duke@0 570 objArrayHandle poolArray(THREAD, r);
duke@0 571
duke@0 572 if (mgr == NULL) {
duke@0 573 // Get all memory pools
duke@0 574 for (int i = 0; i < num_memory_pools; i++) {
duke@0 575 MemoryPool* pool = MemoryService::get_memory_pool(i);
duke@0 576 instanceOop p = pool->get_memory_pool_instance(CHECK_NULL);
duke@0 577 instanceHandle ph(THREAD, p);
duke@0 578 poolArray->obj_at_put(i, ph());
duke@0 579 }
duke@0 580 } else {
duke@0 581 // Get memory pools managed by a given memory manager
duke@0 582 for (int i = 0; i < num_memory_pools; i++) {
duke@0 583 MemoryPool* pool = mgr->get_memory_pool(i);
duke@0 584 instanceOop p = pool->get_memory_pool_instance(CHECK_NULL);
duke@0 585 instanceHandle ph(THREAD, p);
duke@0 586 poolArray->obj_at_put(i, ph());
duke@0 587 }
duke@0 588 }
duke@0 589 return (jobjectArray) JNIHandles::make_local(env, poolArray());
duke@0 590 JVM_END
duke@0 591
duke@0 592 // Returns an array of java/lang/management/MemoryManagerMXBean object
duke@0 593 // one for each memory manager if obj == null; otherwise returns
duke@0 594 // an array of memory managers for a given memory pool if
duke@0 595 // it is a valid memory pool.
duke@0 596 JVM_ENTRY(jobjectArray, jmm_GetMemoryManagers(JNIEnv* env, jobject obj))
duke@0 597 ResourceMark rm(THREAD);
duke@0 598
duke@0 599 int num_mgrs;
duke@0 600 MemoryPool* pool = NULL;
duke@0 601 if (obj == NULL) {
duke@0 602 num_mgrs = MemoryService::num_memory_managers();
duke@0 603 } else {
duke@0 604 pool = get_memory_pool_from_jobject(obj, CHECK_NULL);
duke@0 605 if (pool == NULL) {
duke@0 606 return NULL;
duke@0 607 }
duke@0 608 num_mgrs = pool->num_memory_managers();
duke@0 609 }
duke@0 610
duke@0 611 // Allocate the resulting MemoryManagerMXBean[] object
duke@0 612 klassOop k = Management::java_lang_management_MemoryManagerMXBean_klass(CHECK_NULL);
duke@0 613 instanceKlassHandle ik (THREAD, k);
duke@0 614 objArrayOop r = oopFactory::new_objArray(ik(), num_mgrs, CHECK_NULL);
duke@0 615 objArrayHandle mgrArray(THREAD, r);
duke@0 616
duke@0 617 if (pool == NULL) {
duke@0 618 // Get all memory managers
duke@0 619 for (int i = 0; i < num_mgrs; i++) {
duke@0 620 MemoryManager* mgr = MemoryService::get_memory_manager(i);
duke@0 621 instanceOop p = mgr->get_memory_manager_instance(CHECK_NULL);
duke@0 622 instanceHandle ph(THREAD, p);
duke@0 623 mgrArray->obj_at_put(i, ph());
duke@0 624 }
duke@0 625 } else {
duke@0 626 // Get memory managers for a given memory pool
duke@0 627 for (int i = 0; i < num_mgrs; i++) {
duke@0 628 MemoryManager* mgr = pool->get_memory_manager(i);
duke@0 629 instanceOop p = mgr->get_memory_manager_instance(CHECK_NULL);
duke@0 630 instanceHandle ph(THREAD, p);
duke@0 631 mgrArray->obj_at_put(i, ph());
duke@0 632 }
duke@0 633 }
duke@0 634 return (jobjectArray) JNIHandles::make_local(env, mgrArray());
duke@0 635 JVM_END
duke@0 636
duke@0 637
duke@0 638 // Returns a java/lang/management/MemoryUsage object containing the memory usage
duke@0 639 // of a given memory pool.
duke@0 640 JVM_ENTRY(jobject, jmm_GetMemoryPoolUsage(JNIEnv* env, jobject obj))
duke@0 641 ResourceMark rm(THREAD);
duke@0 642
duke@0 643 MemoryPool* pool = get_memory_pool_from_jobject(obj, CHECK_NULL);
duke@0 644 if (pool != NULL) {
duke@0 645 MemoryUsage usage = pool->get_memory_usage();
duke@0 646 Handle h = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL);
duke@0 647 return JNIHandles::make_local(env, h());
duke@0 648 } else {
duke@0 649 return NULL;
duke@0 650 }
duke@0 651 JVM_END
duke@0 652
duke@0 653 // Returns a java/lang/management/MemoryUsage object containing the memory usage
duke@0 654 // of a given memory pool.
duke@0 655 JVM_ENTRY(jobject, jmm_GetPeakMemoryPoolUsage(JNIEnv* env, jobject obj))
duke@0 656 ResourceMark rm(THREAD);
duke@0 657
duke@0 658 MemoryPool* pool = get_memory_pool_from_jobject(obj, CHECK_NULL);
duke@0 659 if (pool != NULL) {
duke@0 660 MemoryUsage usage = pool->get_peak_memory_usage();
duke@0 661 Handle h = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL);
duke@0 662 return JNIHandles::make_local(env, h());
duke@0 663 } else {
duke@0 664 return NULL;
duke@0 665 }
duke@0 666 JVM_END
duke@0 667
duke@0 668 // Returns a java/lang/management/MemoryUsage object containing the memory usage
duke@0 669 // of a given memory pool after most recent GC.
duke@0 670 JVM_ENTRY(jobject, jmm_GetPoolCollectionUsage(JNIEnv* env, jobject obj))
duke@0 671 ResourceMark rm(THREAD);
duke@0 672
duke@0 673 MemoryPool* pool = get_memory_pool_from_jobject(obj, CHECK_NULL);
duke@0 674 if (pool != NULL && pool->is_collected_pool()) {
duke@0 675 MemoryUsage usage = pool->get_last_collection_usage();
duke@0 676 Handle h = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL);
duke@0 677 return JNIHandles::make_local(env, h());
duke@0 678 } else {
duke@0 679 return NULL;
duke@0 680 }
duke@0 681 JVM_END
duke@0 682
duke@0 683 // Sets the memory pool sensor for a threshold type
duke@0 684 JVM_ENTRY(void, jmm_SetPoolSensor(JNIEnv* env, jobject obj, jmmThresholdType type, jobject sensorObj))
duke@0 685 if (obj == NULL || sensorObj == NULL) {
duke@0 686 THROW(vmSymbols::java_lang_NullPointerException());
duke@0 687 }
duke@0 688
duke@0 689 klassOop sensor_klass = Management::sun_management_Sensor_klass(CHECK);
duke@0 690 oop s = JNIHandles::resolve(sensorObj);
duke@0 691 assert(s->is_instance(), "Sensor should be an instanceOop");
duke@0 692 instanceHandle sensor_h(THREAD, (instanceOop) s);
duke@0 693 if (!sensor_h->is_a(sensor_klass)) {
duke@0 694 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 695 "Sensor is not an instance of sun.management.Sensor class");
duke@0 696 }
duke@0 697
duke@0 698 MemoryPool* mpool = get_memory_pool_from_jobject(obj, CHECK);
duke@0 699 assert(mpool != NULL, "MemoryPool should exist");
duke@0 700
duke@0 701 switch (type) {
duke@0 702 case JMM_USAGE_THRESHOLD_HIGH:
duke@0 703 case JMM_USAGE_THRESHOLD_LOW:
duke@0 704 // have only one sensor for threshold high and low
duke@0 705 mpool->set_usage_sensor_obj(sensor_h);
duke@0 706 break;
duke@0 707 case JMM_COLLECTION_USAGE_THRESHOLD_HIGH:
duke@0 708 case JMM_COLLECTION_USAGE_THRESHOLD_LOW:
duke@0 709 // have only one sensor for threshold high and low
duke@0 710 mpool->set_gc_usage_sensor_obj(sensor_h);
duke@0 711 break;
duke@0 712 default:
duke@0 713 assert(false, "Unrecognized type");
duke@0 714 }
duke@0 715
duke@0 716 JVM_END
duke@0 717
duke@0 718
duke@0 719 // Sets the threshold of a given memory pool.
duke@0 720 // Returns the previous threshold.
duke@0 721 //
duke@0 722 // Input parameters:
duke@0 723 // pool - the MemoryPoolMXBean object
duke@0 724 // type - threshold type
duke@0 725 // threshold - the new threshold (must not be negative)
duke@0 726 //
duke@0 727 JVM_ENTRY(jlong, jmm_SetPoolThreshold(JNIEnv* env, jobject obj, jmmThresholdType type, jlong threshold))
duke@0 728 if (threshold < 0) {
duke@0 729 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 730 "Invalid threshold value",
duke@0 731 -1);
duke@0 732 }
duke@0 733
swamyv@489 734 if ((size_t)threshold > max_uintx) {
swamyv@489 735 stringStream st;
swamyv@489 736 st.print("Invalid valid threshold value. Threshold value (" UINT64_FORMAT ") > max value of size_t (" SIZE_FORMAT ")", (size_t)threshold, max_uintx);
swamyv@489 737 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), st.as_string(), -1);
duke@0 738 }
duke@0 739
duke@0 740 MemoryPool* pool = get_memory_pool_from_jobject(obj, CHECK_(0L));
duke@0 741 assert(pool != NULL, "MemoryPool should exist");
duke@0 742
duke@0 743 jlong prev = 0;
duke@0 744 switch (type) {
duke@0 745 case JMM_USAGE_THRESHOLD_HIGH:
duke@0 746 if (!pool->usage_threshold()->is_high_threshold_supported()) {
duke@0 747 return -1;
duke@0 748 }
duke@0 749 prev = pool->usage_threshold()->set_high_threshold((size_t) threshold);
duke@0 750 break;
duke@0 751
duke@0 752 case JMM_USAGE_THRESHOLD_LOW:
duke@0 753 if (!pool->usage_threshold()->is_low_threshold_supported()) {
duke@0 754 return -1;
duke@0 755 }
duke@0 756 prev = pool->usage_threshold()->set_low_threshold((size_t) threshold);
duke@0 757 break;
duke@0 758
duke@0 759 case JMM_COLLECTION_USAGE_THRESHOLD_HIGH:
duke@0 760 if (!pool->gc_usage_threshold()->is_high_threshold_supported()) {
duke@0 761 return -1;
duke@0 762 }
duke@0 763 // return and the new threshold is effective for the next GC
duke@0 764 return pool->gc_usage_threshold()->set_high_threshold((size_t) threshold);
duke@0 765
duke@0 766 case JMM_COLLECTION_USAGE_THRESHOLD_LOW:
duke@0 767 if (!pool->gc_usage_threshold()->is_low_threshold_supported()) {
duke@0 768 return -1;
duke@0 769 }
duke@0 770 // return and the new threshold is effective for the next GC
duke@0 771 return pool->gc_usage_threshold()->set_low_threshold((size_t) threshold);
duke@0 772
duke@0 773 default:
duke@0 774 assert(false, "Unrecognized type");
duke@0 775 return -1;
duke@0 776 }
duke@0 777
duke@0 778 // When the threshold is changed, reevaluate if the low memory
duke@0 779 // detection is enabled.
duke@0 780 if (prev != threshold) {
duke@0 781 LowMemoryDetector::recompute_enabled_for_collected_pools();
duke@0 782 LowMemoryDetector::detect_low_memory(pool);
duke@0 783 }
duke@0 784 return prev;
duke@0 785 JVM_END
duke@0 786
phh@1988 787 // Gets an array containing the amount of memory allocated on the Java
phh@1988 788 // heap for a set of threads (in bytes). Each element of the array is
phh@1988 789 // the amount of memory allocated for the thread ID specified in the
phh@1988 790 // corresponding entry in the given array of thread IDs; or -1 if the
phh@1988 791 // thread does not exist or has terminated.
phh@1988 792 JVM_ENTRY(void, jmm_GetThreadAllocatedMemory(JNIEnv *env, jlongArray ids,
phh@1988 793 jlongArray sizeArray))
phh@1988 794 // Check if threads is null
phh@1988 795 if (ids == NULL || sizeArray == NULL) {
phh@1988 796 THROW(vmSymbols::java_lang_NullPointerException());
phh@1988 797 }
phh@1988 798
phh@1988 799 ResourceMark rm(THREAD);
phh@1988 800 typeArrayOop ta = typeArrayOop(JNIHandles::resolve_non_null(ids));
phh@1988 801 typeArrayHandle ids_ah(THREAD, ta);
phh@1988 802
phh@1988 803 typeArrayOop sa = typeArrayOop(JNIHandles::resolve_non_null(sizeArray));
phh@1988 804 typeArrayHandle sizeArray_h(THREAD, sa);
phh@1988 805
phh@1988 806 // validate the thread id array
phh@1988 807 validate_thread_id_array(ids_ah, CHECK);
phh@1988 808
phh@1988 809 // sizeArray must be of the same length as the given array of thread IDs
phh@1988 810 int num_threads = ids_ah->length();
phh@1988 811 if (num_threads != sizeArray_h->length()) {
phh@1988 812 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
phh@1988 813 "The length of the given long array does not match the length of "
phh@1988 814 "the given array of thread IDs");
phh@1988 815 }
phh@1988 816
phh@1988 817 MutexLockerEx ml(Threads_lock);
phh@1988 818 for (int i = 0; i < num_threads; i++) {
phh@1988 819 JavaThread* java_thread = find_java_thread_from_id(ids_ah->long_at(i));
phh@1988 820 if (java_thread != NULL) {
phh@1988 821 sizeArray_h->long_at_put(i, java_thread->cooked_allocated_bytes());
phh@1988 822 }
phh@1988 823 }
phh@1988 824 JVM_END
phh@1988 825
duke@0 826 // Returns a java/lang/management/MemoryUsage object representing
duke@0 827 // the memory usage for the heap or non-heap memory.
duke@0 828 JVM_ENTRY(jobject, jmm_GetMemoryUsage(JNIEnv* env, jboolean heap))
duke@0 829 ResourceMark rm(THREAD);
duke@0 830
duke@0 831 // Calculate the memory usage
duke@0 832 size_t total_init = 0;
duke@0 833 size_t total_used = 0;
duke@0 834 size_t total_committed = 0;
duke@0 835 size_t total_max = 0;
duke@0 836 bool has_undefined_init_size = false;
duke@0 837 bool has_undefined_max_size = false;
duke@0 838
duke@0 839 for (int i = 0; i < MemoryService::num_memory_pools(); i++) {
duke@0 840 MemoryPool* pool = MemoryService::get_memory_pool(i);
duke@0 841 if ((heap && pool->is_heap()) || (!heap && pool->is_non_heap())) {
duke@0 842 MemoryUsage u = pool->get_memory_usage();
duke@0 843 total_used += u.used();
duke@0 844 total_committed += u.committed();
duke@0 845
duke@0 846 // if any one of the memory pool has undefined init_size or max_size,
duke@0 847 // set it to -1
duke@0 848 if (u.init_size() == (size_t)-1) {
duke@0 849 has_undefined_init_size = true;
duke@0 850 }
duke@0 851 if (!has_undefined_init_size) {
duke@0 852 total_init += u.init_size();
duke@0 853 }
duke@0 854
duke@0 855 if (u.max_size() == (size_t)-1) {
duke@0 856 has_undefined_max_size = true;
duke@0 857 }
duke@0 858 if (!has_undefined_max_size) {
duke@0 859 total_max += u.max_size();
duke@0 860 }
duke@0 861 }
duke@0 862 }
duke@0 863
tonyp@1677 864 // In our current implementation, we make sure that all non-heap
tonyp@1677 865 // pools have defined init and max sizes. Heap pools do not matter,
tonyp@1677 866 // as we never use total_init and total_max for them.
tonyp@1677 867 assert(heap || !has_undefined_init_size, "Undefined init size");
tonyp@1677 868 assert(heap || !has_undefined_max_size, "Undefined max size");
duke@0 869
phh@1064 870 MemoryUsage usage((heap ? InitialHeapSize : total_init),
duke@0 871 total_used,
duke@0 872 total_committed,
duke@0 873 (heap ? Universe::heap()->max_capacity() : total_max));
duke@0 874
duke@0 875 Handle obj = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL);
duke@0 876 return JNIHandles::make_local(env, obj());
duke@0 877 JVM_END
duke@0 878
duke@0 879 // Returns the boolean value of a given attribute.
duke@0 880 JVM_LEAF(jboolean, jmm_GetBoolAttribute(JNIEnv *env, jmmBoolAttribute att))
duke@0 881 switch (att) {
duke@0 882 case JMM_VERBOSE_GC:
duke@0 883 return MemoryService::get_verbose();
duke@0 884 case JMM_VERBOSE_CLASS:
duke@0 885 return ClassLoadingService::get_verbose();
duke@0 886 case JMM_THREAD_CONTENTION_MONITORING:
duke@0 887 return ThreadService::is_thread_monitoring_contention();
duke@0 888 case JMM_THREAD_CPU_TIME:
duke@0 889 return ThreadService::is_thread_cpu_time_enabled();
phh@1988 890 case JMM_THREAD_ALLOCATED_MEMORY:
phh@1988 891 return ThreadService::is_thread_allocated_memory_enabled();
duke@0 892 default:
duke@0 893 assert(0, "Unrecognized attribute");
duke@0 894 return false;
duke@0 895 }
duke@0 896 JVM_END
duke@0 897
duke@0 898 // Sets the given boolean attribute and returns the previous value.
duke@0 899 JVM_ENTRY(jboolean, jmm_SetBoolAttribute(JNIEnv *env, jmmBoolAttribute att, jboolean flag))
duke@0 900 switch (att) {
duke@0 901 case JMM_VERBOSE_GC:
duke@0 902 return MemoryService::set_verbose(flag != 0);
duke@0 903 case JMM_VERBOSE_CLASS:
duke@0 904 return ClassLoadingService::set_verbose(flag != 0);
duke@0 905 case JMM_THREAD_CONTENTION_MONITORING:
duke@0 906 return ThreadService::set_thread_monitoring_contention(flag != 0);
duke@0 907 case JMM_THREAD_CPU_TIME:
duke@0 908 return ThreadService::set_thread_cpu_time_enabled(flag != 0);
phh@1988 909 case JMM_THREAD_ALLOCATED_MEMORY:
phh@1988 910 return ThreadService::set_thread_allocated_memory_enabled(flag != 0);
duke@0 911 default:
duke@0 912 assert(0, "Unrecognized attribute");
duke@0 913 return false;
duke@0 914 }
duke@0 915 JVM_END
duke@0 916
duke@0 917
duke@0 918 static jlong get_gc_attribute(GCMemoryManager* mgr, jmmLongAttribute att) {
duke@0 919 switch (att) {
duke@0 920 case JMM_GC_TIME_MS:
duke@0 921 return mgr->gc_time_ms();
duke@0 922
duke@0 923 case JMM_GC_COUNT:
duke@0 924 return mgr->gc_count();
duke@0 925
duke@0 926 case JMM_GC_EXT_ATTRIBUTE_INFO_SIZE:
duke@0 927 // current implementation only has 1 ext attribute
duke@0 928 return 1;
duke@0 929
duke@0 930 default:
duke@0 931 assert(0, "Unrecognized GC attribute");
duke@0 932 return -1;
duke@0 933 }
duke@0 934 }
duke@0 935
duke@0 936 class VmThreadCountClosure: public ThreadClosure {
duke@0 937 private:
duke@0 938 int _count;
duke@0 939 public:
duke@0 940 VmThreadCountClosure() : _count(0) {};
duke@0 941 void do_thread(Thread* thread);
duke@0 942 int count() { return _count; }
duke@0 943 };
duke@0 944
duke@0 945 void VmThreadCountClosure::do_thread(Thread* thread) {
duke@0 946 // exclude externally visible JavaThreads
duke@0 947 if (thread->is_Java_thread() && !thread->is_hidden_from_external_view()) {
duke@0 948 return;
duke@0 949 }
duke@0 950
duke@0 951 _count++;
duke@0 952 }
duke@0 953
duke@0 954 static jint get_vm_thread_count() {
duke@0 955 VmThreadCountClosure vmtcc;
duke@0 956 {
duke@0 957 MutexLockerEx ml(Threads_lock);
duke@0 958 Threads::threads_do(&vmtcc);
duke@0 959 }
duke@0 960
duke@0 961 return vmtcc.count();
duke@0 962 }
duke@0 963
duke@0 964 static jint get_num_flags() {
duke@0 965 // last flag entry is always NULL, so subtract 1
duke@0 966 int nFlags = (int) Flag::numFlags - 1;
duke@0 967 int count = 0;
duke@0 968 for (int i = 0; i < nFlags; i++) {
duke@0 969 Flag* flag = &Flag::flags[i];
ysr@350 970 // Exclude the locked (diagnostic, experimental) flags
duke@0 971 if (flag->is_unlocked() || flag->is_unlocker()) {
duke@0 972 count++;
duke@0 973 }
duke@0 974 }
duke@0 975 return count;
duke@0 976 }
duke@0 977
duke@0 978 static jlong get_long_attribute(jmmLongAttribute att) {
duke@0 979 switch (att) {
duke@0 980 case JMM_CLASS_LOADED_COUNT:
duke@0 981 return ClassLoadingService::loaded_class_count();
duke@0 982
duke@0 983 case JMM_CLASS_UNLOADED_COUNT:
duke@0 984 return ClassLoadingService::unloaded_class_count();
duke@0 985
duke@0 986 case JMM_THREAD_TOTAL_COUNT:
duke@0 987 return ThreadService::get_total_thread_count();
duke@0 988
duke@0 989 case JMM_THREAD_LIVE_COUNT:
duke@0 990 return ThreadService::get_live_thread_count();
duke@0 991
duke@0 992 case JMM_THREAD_PEAK_COUNT:
duke@0 993 return ThreadService::get_peak_thread_count();
duke@0 994
duke@0 995 case JMM_THREAD_DAEMON_COUNT:
duke@0 996 return ThreadService::get_daemon_thread_count();
duke@0 997
duke@0 998 case JMM_JVM_INIT_DONE_TIME_MS:
duke@0 999 return Management::vm_init_done_time();
duke@0 1000
duke@0 1001 case JMM_COMPILE_TOTAL_TIME_MS:
duke@0 1002 return Management::ticks_to_ms(CompileBroker::total_compilation_ticks());
duke@0 1003
duke@0 1004 case JMM_OS_PROCESS_ID:
duke@0 1005 return os::current_process_id();
duke@0 1006
duke@0 1007 // Hotspot-specific counters
duke@0 1008 case JMM_CLASS_LOADED_BYTES:
duke@0 1009 return ClassLoadingService::loaded_class_bytes();
duke@0 1010
duke@0 1011 case JMM_CLASS_UNLOADED_BYTES:
duke@0 1012 return ClassLoadingService::unloaded_class_bytes();
duke@0 1013
duke@0 1014 case JMM_SHARED_CLASS_LOADED_COUNT:
duke@0 1015 return ClassLoadingService::loaded_shared_class_count();
duke@0 1016
duke@0 1017 case JMM_SHARED_CLASS_UNLOADED_COUNT:
duke@0 1018 return ClassLoadingService::unloaded_shared_class_count();
duke@0 1019
duke@0 1020
duke@0 1021 case JMM_SHARED_CLASS_LOADED_BYTES:
duke@0 1022 return ClassLoadingService::loaded_shared_class_bytes();
duke@0 1023
duke@0 1024 case JMM_SHARED_CLASS_UNLOADED_BYTES:
duke@0 1025 return ClassLoadingService::unloaded_shared_class_bytes();
duke@0 1026
duke@0 1027 case JMM_TOTAL_CLASSLOAD_TIME_MS:
duke@0 1028 return ClassLoader::classloader_time_ms();
duke@0 1029
duke@0 1030 case JMM_VM_GLOBAL_COUNT:
duke@0 1031 return get_num_flags();
duke@0 1032
duke@0 1033 case JMM_SAFEPOINT_COUNT:
duke@0 1034 return RuntimeService::safepoint_count();
duke@0 1035
duke@0 1036 case JMM_TOTAL_SAFEPOINTSYNC_TIME_MS:
duke@0 1037 return RuntimeService::safepoint_sync_time_ms();
duke@0 1038
duke@0 1039 case JMM_TOTAL_STOPPED_TIME_MS:
duke@0 1040 return RuntimeService::safepoint_time_ms();
duke@0 1041
duke@0 1042 case JMM_TOTAL_APP_TIME_MS:
duke@0 1043 return RuntimeService::application_time_ms();
duke@0 1044
duke@0 1045 case JMM_VM_THREAD_COUNT:
duke@0 1046 return get_vm_thread_count();
duke@0 1047
duke@0 1048 case JMM_CLASS_INIT_TOTAL_COUNT:
duke@0 1049 return ClassLoader::class_init_count();
duke@0 1050
duke@0 1051 case JMM_CLASS_INIT_TOTAL_TIME_MS:
duke@0 1052 return ClassLoader::class_init_time_ms();
duke@0 1053
duke@0 1054 case JMM_CLASS_VERIFY_TOTAL_TIME_MS:
duke@0 1055 return ClassLoader::class_verify_time_ms();
duke@0 1056
duke@0 1057 case JMM_METHOD_DATA_SIZE_BYTES:
duke@0 1058 return ClassLoadingService::class_method_data_size();
duke@0 1059
duke@0 1060 case JMM_OS_MEM_TOTAL_PHYSICAL_BYTES:
duke@0 1061 return os::physical_memory();
duke@0 1062
duke@0 1063 default:
duke@0 1064 return -1;
duke@0 1065 }
duke@0 1066 }
duke@0 1067
duke@0 1068
duke@0 1069 // Returns the long value of a given attribute.
duke@0 1070 JVM_ENTRY(jlong, jmm_GetLongAttribute(JNIEnv *env, jobject obj, jmmLongAttribute att))
duke@0 1071 if (obj == NULL) {
duke@0 1072 return get_long_attribute(att);
duke@0 1073 } else {
duke@0 1074 GCMemoryManager* mgr = get_gc_memory_manager_from_jobject(obj, CHECK_(0L));
duke@0 1075 if (mgr != NULL) {
duke@0 1076 return get_gc_attribute(mgr, att);
duke@0 1077 }
duke@0 1078 }
duke@0 1079 return -1;
duke@0 1080 JVM_END
duke@0 1081
duke@0 1082 // Gets the value of all attributes specified in the given array
duke@0 1083 // and sets the value in the result array.
duke@0 1084 // Returns the number of attributes found.
duke@0 1085 JVM_ENTRY(jint, jmm_GetLongAttributes(JNIEnv *env,
duke@0 1086 jobject obj,
duke@0 1087 jmmLongAttribute* atts,
duke@0 1088 jint count,
duke@0 1089 jlong* result))
duke@0 1090
duke@0 1091 int num_atts = 0;
duke@0 1092 if (obj == NULL) {
duke@0 1093 for (int i = 0; i < count; i++) {
duke@0 1094 result[i] = get_long_attribute(atts[i]);
duke@0 1095 if (result[i] != -1) {
duke@0 1096 num_atts++;
duke@0 1097 }
duke@0 1098 }
duke@0 1099 } else {
duke@0 1100 GCMemoryManager* mgr = get_gc_memory_manager_from_jobject(obj, CHECK_0);
duke@0 1101 for (int i = 0; i < count; i++) {
duke@0 1102 result[i] = get_gc_attribute(mgr, atts[i]);
duke@0 1103 if (result[i] != -1) {
duke@0 1104 num_atts++;
duke@0 1105 }
duke@0 1106 }
duke@0 1107 }
duke@0 1108 return num_atts;
duke@0 1109 JVM_END
duke@0 1110
duke@0 1111 // Helper function to do thread dump for a specific list of threads
duke@0 1112 static void do_thread_dump(ThreadDumpResult* dump_result,
duke@0 1113 typeArrayHandle ids_ah, // array of thread ID (long[])
duke@0 1114 int num_threads,
duke@0 1115 int max_depth,
duke@0 1116 bool with_locked_monitors,
duke@0 1117 bool with_locked_synchronizers,
duke@0 1118 TRAPS) {
duke@0 1119
duke@0 1120 // First get an array of threadObj handles.
duke@0 1121 // A JavaThread may terminate before we get the stack trace.
duke@0 1122 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
duke@0 1123 {
duke@0 1124 MutexLockerEx ml(Threads_lock);
duke@0 1125 for (int i = 0; i < num_threads; i++) {
duke@0 1126 jlong tid = ids_ah->long_at(i);
duke@0 1127 JavaThread* jt = find_java_thread_from_id(tid);
duke@0 1128 oop thread_obj = (jt != NULL ? jt->threadObj() : (oop)NULL);
duke@0 1129 instanceHandle threadObj_h(THREAD, (instanceOop) thread_obj);
duke@0 1130 thread_handle_array->append(threadObj_h);
duke@0 1131 }
duke@0 1132 }
duke@0 1133
duke@0 1134 // Obtain thread dumps and thread snapshot information
duke@0 1135 VM_ThreadDump op(dump_result,
duke@0 1136 thread_handle_array,
duke@0 1137 num_threads,
duke@0 1138 max_depth, /* stack depth */
duke@0 1139 with_locked_monitors,
duke@0 1140 with_locked_synchronizers);
duke@0 1141 VMThread::execute(&op);
duke@0 1142 }
duke@0 1143
duke@0 1144 // Gets an array of ThreadInfo objects. Each element is the ThreadInfo
duke@0 1145 // for the thread ID specified in the corresponding entry in
duke@0 1146 // the given array of thread IDs; or NULL if the thread does not exist
duke@0 1147 // or has terminated.
duke@0 1148 //
duke@0 1149 // Input parameters:
duke@0 1150 // ids - array of thread IDs
duke@0 1151 // maxDepth - the maximum depth of stack traces to be dumped:
duke@0 1152 // maxDepth == -1 requests to dump entire stack trace.
duke@0 1153 // maxDepth == 0 requests no stack trace.
duke@0 1154 // infoArray - array of ThreadInfo objects
duke@0 1155 //
phh@1988 1156 // QQQ - Why does this method return a value instead of void?
duke@0 1157 JVM_ENTRY(jint, jmm_GetThreadInfo(JNIEnv *env, jlongArray ids, jint maxDepth, jobjectArray infoArray))
duke@0 1158 // Check if threads is null
duke@0 1159 if (ids == NULL || infoArray == NULL) {
duke@0 1160 THROW_(vmSymbols::java_lang_NullPointerException(), -1);
duke@0 1161 }
duke@0 1162
duke@0 1163 if (maxDepth < -1) {
duke@0 1164 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 1165 "Invalid maxDepth", -1);
duke@0 1166 }
duke@0 1167
duke@0 1168 ResourceMark rm(THREAD);
duke@0 1169 typeArrayOop ta = typeArrayOop(JNIHandles::resolve_non_null(ids));
duke@0 1170 typeArrayHandle ids_ah(THREAD, ta);
duke@0 1171
duke@0 1172 oop infoArray_obj = JNIHandles::resolve_non_null(infoArray);
duke@0 1173 objArrayOop oa = objArrayOop(infoArray_obj);
duke@0 1174 objArrayHandle infoArray_h(THREAD, oa);
duke@0 1175
duke@0 1176 // validate the thread id array
duke@0 1177 validate_thread_id_array(ids_ah, CHECK_0);
duke@0 1178
duke@0 1179 // validate the ThreadInfo[] parameters
duke@0 1180 validate_thread_info_array(infoArray_h, CHECK_0);
duke@0 1181
duke@0 1182 // infoArray must be of the same length as the given array of thread IDs
duke@0 1183 int num_threads = ids_ah->length();
duke@0 1184 if (num_threads != infoArray_h->length()) {
duke@0 1185 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 1186 "The length of the given ThreadInfo array does not match the length of the given array of thread IDs", -1);
duke@0 1187 }
duke@0 1188
duke@0 1189 if (JDK_Version::is_gte_jdk16x_version()) {
duke@0 1190 // make sure the AbstractOwnableSynchronizer klass is loaded before taking thread snapshots
duke@0 1191 java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(CHECK_0);
duke@0 1192 }
duke@0 1193
duke@0 1194 // Must use ThreadDumpResult to store the ThreadSnapshot.
duke@0 1195 // GC may occur after the thread snapshots are taken but before
duke@0 1196 // this function returns. The threadObj and other oops kept
duke@0 1197 // in the ThreadSnapshot are marked and adjusted during GC.
duke@0 1198 ThreadDumpResult dump_result(num_threads);
duke@0 1199
duke@0 1200 if (maxDepth == 0) {
duke@0 1201 // no stack trace dumped - do not need to stop the world
duke@0 1202 {
duke@0 1203 MutexLockerEx ml(Threads_lock);
duke@0 1204 for (int i = 0; i < num_threads; i++) {
duke@0 1205 jlong tid = ids_ah->long_at(i);
duke@0 1206 JavaThread* jt = find_java_thread_from_id(tid);
duke@0 1207 ThreadSnapshot* ts;
duke@0 1208 if (jt == NULL) {
duke@0 1209 // if the thread does not exist or now it is terminated,
duke@0 1210 // create dummy snapshot
duke@0 1211 ts = new ThreadSnapshot();
duke@0 1212 } else {
duke@0 1213 ts = new ThreadSnapshot(jt);
duke@0 1214 }
duke@0 1215 dump_result.add_thread_snapshot(ts);
duke@0 1216 }
duke@0 1217 }
duke@0 1218 } else {
duke@0 1219 // obtain thread dump with the specific list of threads with stack trace
duke@0 1220 do_thread_dump(&dump_result,
duke@0 1221 ids_ah,
duke@0 1222 num_threads,
duke@0 1223 maxDepth,
duke@0 1224 false, /* no locked monitor */
duke@0 1225 false, /* no locked synchronizers */
duke@0 1226 CHECK_0);
duke@0 1227 }
duke@0 1228
duke@0 1229 int num_snapshots = dump_result.num_snapshots();
duke@0 1230 assert(num_snapshots == num_threads, "Must match the number of thread snapshots");
duke@0 1231 int index = 0;
duke@0 1232 for (ThreadSnapshot* ts = dump_result.snapshots(); ts != NULL; index++, ts = ts->next()) {
duke@0 1233 // For each thread, create an java/lang/management/ThreadInfo object
duke@0 1234 // and fill with the thread information
duke@0 1235
duke@0 1236 if (ts->threadObj() == NULL) {
duke@0 1237 // if the thread does not exist or now it is terminated, set threadinfo to NULL
duke@0 1238 infoArray_h->obj_at_put(index, NULL);
duke@0 1239 continue;
duke@0 1240 }
duke@0 1241
duke@0 1242 // Create java.lang.management.ThreadInfo object
duke@0 1243 instanceOop info_obj = Management::create_thread_info_instance(ts, CHECK_0);
duke@0 1244 infoArray_h->obj_at_put(index, info_obj);
duke@0 1245 }
duke@0 1246 return 0;
duke@0 1247 JVM_END
duke@0 1248
duke@0 1249 // Dump thread info for the specified threads.
duke@0 1250 // It returns an array of ThreadInfo objects. Each element is the ThreadInfo
duke@0 1251 // for the thread ID specified in the corresponding entry in
duke@0 1252 // the given array of thread IDs; or NULL if the thread does not exist
duke@0 1253 // or has terminated.
duke@0 1254 //
duke@0 1255 // Input parameter:
duke@0 1256 // ids - array of thread IDs; NULL indicates all live threads
duke@0 1257 // locked_monitors - if true, dump locked object monitors
duke@0 1258 // locked_synchronizers - if true, dump locked JSR-166 synchronizers
duke@0 1259 //
duke@0 1260 JVM_ENTRY(jobjectArray, jmm_DumpThreads(JNIEnv *env, jlongArray thread_ids, jboolean locked_monitors, jboolean locked_synchronizers))
duke@0 1261 ResourceMark rm(THREAD);
duke@0 1262
duke@0 1263 if (JDK_Version::is_gte_jdk16x_version()) {
duke@0 1264 // make sure the AbstractOwnableSynchronizer klass is loaded before taking thread snapshots
duke@0 1265 java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(CHECK_NULL);
duke@0 1266 }
duke@0 1267
duke@0 1268 typeArrayOop ta = typeArrayOop(JNIHandles::resolve(thread_ids));
duke@0 1269 int num_threads = (ta != NULL ? ta->length() : 0);
duke@0 1270 typeArrayHandle ids_ah(THREAD, ta);
duke@0 1271
duke@0 1272 ThreadDumpResult dump_result(num_threads); // can safepoint
duke@0 1273
duke@0 1274 if (ids_ah() != NULL) {
duke@0 1275
duke@0 1276 // validate the thread id array
duke@0 1277 validate_thread_id_array(ids_ah, CHECK_NULL);
duke@0 1278
duke@0 1279 // obtain thread dump of a specific list of threads
duke@0 1280 do_thread_dump(&dump_result,
duke@0 1281 ids_ah,
duke@0 1282 num_threads,
duke@0 1283 -1, /* entire stack */
duke@0 1284 (locked_monitors ? true : false), /* with locked monitors */
duke@0 1285 (locked_synchronizers ? true : false), /* with locked synchronizers */
duke@0 1286 CHECK_NULL);
duke@0 1287 } else {
duke@0 1288 // obtain thread dump of all threads
duke@0 1289 VM_ThreadDump op(&dump_result,
duke@0 1290 -1, /* entire stack */
duke@0 1291 (locked_monitors ? true : false), /* with locked monitors */
duke@0 1292 (locked_synchronizers ? true : false) /* with locked synchronizers */);
duke@0 1293 VMThread::execute(&op);
duke@0 1294 }
duke@0 1295
duke@0 1296 int num_snapshots = dump_result.num_snapshots();
duke@0 1297
duke@0 1298 // create the result ThreadInfo[] object
duke@0 1299 klassOop k = Management::java_lang_management_ThreadInfo_klass(CHECK_NULL);
duke@0 1300 instanceKlassHandle ik (THREAD, k);
duke@0 1301 objArrayOop r = oopFactory::new_objArray(ik(), num_snapshots, CHECK_NULL);
duke@0 1302 objArrayHandle result_h(THREAD, r);
duke@0 1303
duke@0 1304 int index = 0;
duke@0 1305 for (ThreadSnapshot* ts = dump_result.snapshots(); ts != NULL; ts = ts->next(), index++) {
duke@0 1306 if (ts->threadObj() == NULL) {
duke@0 1307 // if the thread does not exist or now it is terminated, set threadinfo to NULL
duke@0 1308 result_h->obj_at_put(index, NULL);
duke@0 1309 continue;
duke@0 1310 }
duke@0 1311
duke@0 1312 ThreadStackTrace* stacktrace = ts->get_stack_trace();
duke@0 1313 assert(stacktrace != NULL, "Must have a stack trace dumped");
duke@0 1314
duke@0 1315 // Create Object[] filled with locked monitors
duke@0 1316 // Create int[] filled with the stack depth where a monitor was locked
duke@0 1317 int num_frames = stacktrace->get_stack_depth();
duke@0 1318 int num_locked_monitors = stacktrace->num_jni_locked_monitors();
duke@0 1319
duke@0 1320 // Count the total number of locked monitors
duke@0 1321 for (int i = 0; i < num_frames; i++) {
duke@0 1322 StackFrameInfo* frame = stacktrace->stack_frame_at(i);
duke@0 1323 num_locked_monitors += frame->num_locked_monitors();
duke@0 1324 }
duke@0 1325
duke@0 1326 objArrayHandle monitors_array;
duke@0 1327 typeArrayHandle depths_array;
duke@0 1328 objArrayHandle synchronizers_array;
duke@0 1329
duke@0 1330 if (locked_monitors) {
duke@0 1331 // Constructs Object[] and int[] to contain the object monitor and the stack depth
duke@0 1332 // where the thread locked it
stefank@2139 1333 objArrayOop array = oopFactory::new_objArray(SystemDictionary::Object_klass(), num_locked_monitors, CHECK_NULL);
duke@0 1334 objArrayHandle mh(THREAD, array);
duke@0 1335 monitors_array = mh;
duke@0 1336
duke@0 1337 typeArrayOop tarray = oopFactory::new_typeArray(T_INT, num_locked_monitors, CHECK_NULL);
duke@0 1338 typeArrayHandle dh(THREAD, tarray);
duke@0 1339 depths_array = dh;
duke@0 1340
duke@0 1341 int count = 0;
duke@0 1342 int j = 0;
duke@0 1343 for (int depth = 0; depth < num_frames; depth++) {
duke@0 1344 StackFrameInfo* frame = stacktrace->stack_frame_at(depth);
duke@0 1345 int len = frame->num_locked_monitors();
duke@0 1346 GrowableArray<oop>* locked_monitors = frame->locked_monitors();
duke@0 1347 for (j = 0; j < len; j++) {
duke@0 1348 oop monitor = locked_monitors->at(j);
duke@0 1349 assert(monitor != NULL && monitor->is_instance(), "must be a Java object");
duke@0 1350 monitors_array->obj_at_put(count, monitor);
duke@0 1351 depths_array->int_at_put(count, depth);
duke@0 1352 count++;
duke@0 1353 }
duke@0 1354 }
duke@0 1355
duke@0 1356 GrowableArray<oop>* jni_locked_monitors = stacktrace->jni_locked_monitors();
duke@0 1357 for (j = 0; j < jni_locked_monitors->length(); j++) {
duke@0 1358 oop object = jni_locked_monitors->at(j);
duke@0 1359 assert(object != NULL && object->is_instance(), "must be a Java object");
duke@0 1360 monitors_array->obj_at_put(count, object);
duke@0 1361 // Monitor locked via JNI MonitorEnter call doesn't have stack depth info
duke@0 1362 depths_array->int_at_put(count, -1);
duke@0 1363 count++;
duke@0 1364 }
duke@0 1365 assert(count == num_locked_monitors, "number of locked monitors doesn't match");
duke@0 1366 }
duke@0 1367
duke@0 1368 if (locked_synchronizers) {
duke@0 1369 // Create Object[] filled with locked JSR-166 synchronizers
duke@0 1370 assert(ts->threadObj() != NULL, "Must be a valid JavaThread");
duke@0 1371 ThreadConcurrentLocks* tcl = ts->get_concurrent_locks();
duke@0 1372 GrowableArray<instanceOop>* locks = (tcl != NULL ? tcl->owned_locks() : NULL);
duke@0 1373 int num_locked_synchronizers = (locks != NULL ? locks->length() : 0);
duke@0 1374
stefank@2139 1375 objArrayOop array = oopFactory::new_objArray(SystemDictionary::Object_klass(), num_locked_synchronizers, CHECK_NULL);
duke@0 1376 objArrayHandle sh(THREAD, array);
duke@0 1377 synchronizers_array = sh;
duke@0 1378
duke@0 1379 for (int k = 0; k < num_locked_synchronizers; k++) {
duke@0 1380 synchronizers_array->obj_at_put(k, locks->at(k));
duke@0 1381 }
duke@0 1382 }
duke@0 1383
duke@0 1384 // Create java.lang.management.ThreadInfo object
duke@0 1385 instanceOop info_obj = Management::create_thread_info_instance(ts,
duke@0 1386 monitors_array,
duke@0 1387 depths_array,
duke@0 1388 synchronizers_array,
duke@0 1389 CHECK_NULL);
duke@0 1390 result_h->obj_at_put(index, info_obj);
duke@0 1391 }
duke@0 1392
duke@0 1393 return (jobjectArray) JNIHandles::make_local(env, result_h());
duke@0 1394 JVM_END
duke@0 1395
duke@0 1396 // Returns an array of Class objects.
duke@0 1397 JVM_ENTRY(jobjectArray, jmm_GetLoadedClasses(JNIEnv *env))
duke@0 1398 ResourceMark rm(THREAD);
duke@0 1399
duke@0 1400 LoadedClassesEnumerator lce(THREAD); // Pass current Thread as parameter
duke@0 1401
duke@0 1402 int num_classes = lce.num_loaded_classes();
never@1142 1403 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), num_classes, CHECK_0);
duke@0 1404 objArrayHandle classes_ah(THREAD, r);
duke@0 1405
duke@0 1406 for (int i = 0; i < num_classes; i++) {
duke@0 1407 KlassHandle kh = lce.get_klass(i);
duke@0 1408 oop mirror = Klass::cast(kh())->java_mirror();
duke@0 1409 classes_ah->obj_at_put(i, mirror);
duke@0 1410 }
duke@0 1411
duke@0 1412 return (jobjectArray) JNIHandles::make_local(env, classes_ah());
duke@0 1413 JVM_END
duke@0 1414
duke@0 1415 // Reset statistic. Return true if the requested statistic is reset.
duke@0 1416 // Otherwise, return false.
duke@0 1417 //
duke@0 1418 // Input parameters:
duke@0 1419 // obj - specify which instance the statistic associated with to be reset
duke@0 1420 // For PEAK_POOL_USAGE stat, obj is required to be a memory pool object.
duke@0 1421 // For THREAD_CONTENTION_COUNT and TIME stat, obj is required to be a thread ID.
duke@0 1422 // type - the type of statistic to be reset
duke@0 1423 //
duke@0 1424 JVM_ENTRY(jboolean, jmm_ResetStatistic(JNIEnv *env, jvalue obj, jmmStatisticType type))
duke@0 1425 ResourceMark rm(THREAD);
duke@0 1426
duke@0 1427 switch (type) {
duke@0 1428 case JMM_STAT_PEAK_THREAD_COUNT:
duke@0 1429 ThreadService::reset_peak_thread_count();
duke@0 1430 return true;
duke@0 1431
duke@0 1432 case JMM_STAT_THREAD_CONTENTION_COUNT:
duke@0 1433 case JMM_STAT_THREAD_CONTENTION_TIME: {
duke@0 1434 jlong tid = obj.j;
duke@0 1435 if (tid < 0) {
duke@0 1436 THROW_(vmSymbols::java_lang_IllegalArgumentException(), JNI_FALSE);
duke@0 1437 }
duke@0 1438
duke@0 1439 // Look for the JavaThread of this given tid
duke@0 1440 MutexLockerEx ml(Threads_lock);
duke@0 1441 if (tid == 0) {
duke@0 1442 // reset contention statistics for all threads if tid == 0
duke@0 1443 for (JavaThread* java_thread = Threads::first(); java_thread != NULL; java_thread = java_thread->next()) {
duke@0 1444 if (type == JMM_STAT_THREAD_CONTENTION_COUNT) {
duke@0 1445 ThreadService::reset_contention_count_stat(java_thread);
duke@0 1446 } else {
duke@0 1447 ThreadService::reset_contention_time_stat(java_thread);
duke@0 1448 }
duke@0 1449 }
duke@0 1450 } else {
duke@0 1451 // reset contention statistics for a given thread
duke@0 1452 JavaThread* java_thread = find_java_thread_from_id(tid);
duke@0 1453 if (java_thread == NULL) {
duke@0 1454 return false;
duke@0 1455 }
duke@0 1456
duke@0 1457 if (type == JMM_STAT_THREAD_CONTENTION_COUNT) {
duke@0 1458 ThreadService::reset_contention_count_stat(java_thread);
duke@0 1459 } else {
duke@0 1460 ThreadService::reset_contention_time_stat(java_thread);
duke@0 1461 }
duke@0 1462 }
duke@0 1463 return true;
duke@0 1464 break;
duke@0 1465 }
duke@0 1466 case JMM_STAT_PEAK_POOL_USAGE: {
duke@0 1467 jobject o = obj.l;
duke@0 1468 if (o == NULL) {
duke@0 1469 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
duke@0 1470 }
duke@0 1471
duke@0 1472 oop pool_obj = JNIHandles::resolve(o);
duke@0 1473 assert(pool_obj->is_instance(), "Should be an instanceOop");
duke@0 1474 instanceHandle ph(THREAD, (instanceOop) pool_obj);
duke@0 1475
duke@0 1476 MemoryPool* pool = MemoryService::get_memory_pool(ph);
duke@0 1477 if (pool != NULL) {
duke@0 1478 pool->reset_peak_memory_usage();
duke@0 1479 return true;
duke@0 1480 }
duke@0 1481 break;
duke@0 1482 }
duke@0 1483 case JMM_STAT_GC_STAT: {
duke@0 1484 jobject o = obj.l;
duke@0 1485 if (o == NULL) {
duke@0 1486 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
duke@0 1487 }
duke@0 1488
duke@0 1489 GCMemoryManager* mgr = get_gc_memory_manager_from_jobject(o, CHECK_0);
duke@0 1490 if (mgr != NULL) {
duke@0 1491 mgr->reset_gc_stat();
duke@0 1492 return true;
duke@0 1493 }
duke@0 1494 break;
duke@0 1495 }
duke@0 1496 default:
duke@0 1497 assert(0, "Unknown Statistic Type");
duke@0 1498 }
duke@0 1499 return false;
duke@0 1500 JVM_END
duke@0 1501
duke@0 1502 // Returns the fast estimate of CPU time consumed by
duke@0 1503 // a given thread (in nanoseconds).
duke@0 1504 // If thread_id == 0, return CPU time for the current thread.
duke@0 1505 JVM_ENTRY(jlong, jmm_GetThreadCpuTime(JNIEnv *env, jlong thread_id))
duke@0 1506 if (!os::is_thread_cpu_time_supported()) {
duke@0 1507 return -1;
duke@0 1508 }
duke@0 1509
duke@0 1510 if (thread_id < 0) {
duke@0 1511 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 1512 "Invalid thread ID", -1);
duke@0 1513 }
duke@0 1514
duke@0 1515 JavaThread* java_thread = NULL;
duke@0 1516 if (thread_id == 0) {
duke@0 1517 // current thread
duke@0 1518 return os::current_thread_cpu_time();
duke@0 1519 } else {
duke@0 1520 MutexLockerEx ml(Threads_lock);
duke@0 1521 java_thread = find_java_thread_from_id(thread_id);
duke@0 1522 if (java_thread != NULL) {
duke@0 1523 return os::thread_cpu_time((Thread*) java_thread);
duke@0 1524 }
duke@0 1525 }
duke@0 1526 return -1;
duke@0 1527 JVM_END
duke@0 1528
duke@0 1529 // Returns the CPU time consumed by a given thread (in nanoseconds).
duke@0 1530 // If thread_id == 0, CPU time for the current thread is returned.
duke@0 1531 // If user_sys_cpu_time = true, user level and system CPU time of
duke@0 1532 // a given thread is returned; otherwise, only user level CPU time
duke@0 1533 // is returned.
duke@0 1534 JVM_ENTRY(jlong, jmm_GetThreadCpuTimeWithKind(JNIEnv *env, jlong thread_id, jboolean user_sys_cpu_time))
duke@0 1535 if (!os::is_thread_cpu_time_supported()) {
duke@0 1536 return -1;
duke@0 1537 }
duke@0 1538
duke@0 1539 if (thread_id < 0) {
duke@0 1540 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 1541 "Invalid thread ID", -1);
duke@0 1542 }
duke@0 1543
duke@0 1544 JavaThread* java_thread = NULL;
duke@0 1545 if (thread_id == 0) {
duke@0 1546 // current thread
duke@0 1547 return os::current_thread_cpu_time(user_sys_cpu_time != 0);
duke@0 1548 } else {
duke@0 1549 MutexLockerEx ml(Threads_lock);
duke@0 1550 java_thread = find_java_thread_from_id(thread_id);
duke@0 1551 if (java_thread != NULL) {
duke@0 1552 return os::thread_cpu_time((Thread*) java_thread, user_sys_cpu_time != 0);
duke@0 1553 }
duke@0 1554 }
duke@0 1555 return -1;
duke@0 1556 JVM_END
duke@0 1557
phh@1988 1558 // Gets an array containing the CPU times consumed by a set of threads
phh@1988 1559 // (in nanoseconds). Each element of the array is the CPU time for the
phh@1988 1560 // thread ID specified in the corresponding entry in the given array
phh@1988 1561 // of thread IDs; or -1 if the thread does not exist or has terminated.
phh@1988 1562 // If user_sys_cpu_time = true, the sum of user level and system CPU time
phh@1988 1563 // for the given thread is returned; otherwise, only user level CPU time
phh@1988 1564 // is returned.
phh@1988 1565 JVM_ENTRY(void, jmm_GetThreadCpuTimesWithKind(JNIEnv *env, jlongArray ids,
phh@1988 1566 jlongArray timeArray,
phh@1988 1567 jboolean user_sys_cpu_time))
phh@1988 1568 // Check if threads is null
phh@1988 1569 if (ids == NULL || timeArray == NULL) {
phh@1988 1570 THROW(vmSymbols::java_lang_NullPointerException());
phh@1988 1571 }
phh@1988 1572
phh@1988 1573 ResourceMark rm(THREAD);
phh@1988 1574 typeArrayOop ta = typeArrayOop(JNIHandles::resolve_non_null(ids));
phh@1988 1575 typeArrayHandle ids_ah(THREAD, ta);
phh@1988 1576
phh@1988 1577 typeArrayOop tia = typeArrayOop(JNIHandles::resolve_non_null(timeArray));
phh@1988 1578 typeArrayHandle timeArray_h(THREAD, tia);
phh@1988 1579
phh@1988 1580 // validate the thread id array
phh@1988 1581 validate_thread_id_array(ids_ah, CHECK);
phh@1988 1582
phh@1988 1583 // timeArray must be of the same length as the given array of thread IDs
phh@1988 1584 int num_threads = ids_ah->length();
phh@1988 1585 if (num_threads != timeArray_h->length()) {
phh@1988 1586 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
phh@1988 1587 "The length of the given long array does not match the length of "
phh@1988 1588 "the given array of thread IDs");
phh@1988 1589 }
phh@1988 1590
phh@1988 1591 MutexLockerEx ml(Threads_lock);
phh@1988 1592 for (int i = 0; i < num_threads; i++) {
phh@1988 1593 JavaThread* java_thread = find_java_thread_from_id(ids_ah->long_at(i));
phh@1988 1594 if (java_thread != NULL) {
phh@1988 1595 timeArray_h->long_at_put(i, os::thread_cpu_time((Thread*)java_thread,
phh@1988 1596 user_sys_cpu_time != 0));
phh@1988 1597 }
phh@1988 1598 }
phh@1988 1599 JVM_END
phh@1988 1600
duke@0 1601 // Returns a String array of all VM global flag names
duke@0 1602 JVM_ENTRY(jobjectArray, jmm_GetVMGlobalNames(JNIEnv *env))
duke@0 1603 // last flag entry is always NULL, so subtract 1
duke@0 1604 int nFlags = (int) Flag::numFlags - 1;
duke@0 1605 // allocate a temp array
never@1142 1606 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(),
duke@0 1607 nFlags, CHECK_0);
duke@0 1608 objArrayHandle flags_ah(THREAD, r);
duke@0 1609 int num_entries = 0;
duke@0 1610 for (int i = 0; i < nFlags; i++) {
duke@0 1611 Flag* flag = &Flag::flags[i];
ysr@350 1612 // Exclude the locked (experimental, diagnostic) flags
duke@0 1613 if (flag->is_unlocked() || flag->is_unlocker()) {
duke@0 1614 Handle s = java_lang_String::create_from_str(flag->name, CHECK_0);
duke@0 1615 flags_ah->obj_at_put(num_entries, s());
duke@0 1616 num_entries++;
duke@0 1617 }
duke@0 1618 }
duke@0 1619
duke@0 1620 if (num_entries < nFlags) {
duke@0 1621 // Return array of right length
never@1142 1622 objArrayOop res = oopFactory::new_objArray(SystemDictionary::String_klass(), num_entries, CHECK_0);
duke@0 1623 for(int i = 0; i < num_entries; i++) {
duke@0 1624 res->obj_at_put(i, flags_ah->obj_at(i));
duke@0 1625 }
duke@0 1626 return (jobjectArray)JNIHandles::make_local(env, res);
duke@0 1627 }
duke@0 1628
duke@0 1629 return (jobjectArray)JNIHandles::make_local(env, flags_ah());
duke@0 1630 JVM_END
duke@0 1631
phh@1067 1632 // Utility function used by jmm_GetVMGlobals. Returns false if flag type
phh@1067 1633 // can't be determined, true otherwise. If false is returned, then *global
phh@1067 1634 // will be incomplete and invalid.
phh@1067 1635 bool add_global_entry(JNIEnv* env, Handle name, jmmVMGlobal *global, Flag *flag, TRAPS) {
duke@0 1636 Handle flag_name;
duke@0 1637 if (name() == NULL) {
phh@1067 1638 flag_name = java_lang_String::create_from_str(flag->name, CHECK_false);
duke@0 1639 } else {
duke@0 1640 flag_name = name;
duke@0 1641 }
duke@0 1642 global->name = (jstring)JNIHandles::make_local(env, flag_name());
duke@0 1643
duke@0 1644 if (flag->is_bool()) {
duke@0 1645 global->value.z = flag->get_bool() ? JNI_TRUE : JNI_FALSE;
duke@0 1646 global->type = JMM_VMGLOBAL_TYPE_JBOOLEAN;
duke@0 1647 } else if (flag->is_intx()) {
duke@0 1648 global->value.j = (jlong)flag->get_intx();
duke@0 1649 global->type = JMM_VMGLOBAL_TYPE_JLONG;
duke@0 1650 } else if (flag->is_uintx()) {
duke@0 1651 global->value.j = (jlong)flag->get_uintx();
duke@0 1652 global->type = JMM_VMGLOBAL_TYPE_JLONG;
phh@1067 1653 } else if (flag->is_uint64_t()) {
phh@1067 1654 global->value.j = (jlong)flag->get_uint64_t();
phh@1067 1655 global->type = JMM_VMGLOBAL_TYPE_JLONG;
duke@0 1656 } else if (flag->is_ccstr()) {
phh@1067 1657 Handle str = java_lang_String::create_from_str(flag->get_ccstr(), CHECK_false);
duke@0 1658 global->value.l = (jobject)JNIHandles::make_local(env, str());
duke@0 1659 global->type = JMM_VMGLOBAL_TYPE_JSTRING;
phh@1067 1660 } else {
phh@1067 1661 global->type = JMM_VMGLOBAL_TYPE_UNKNOWN;
phh@1067 1662 return false;
duke@0 1663 }
duke@0 1664
duke@0 1665 global->writeable = flag->is_writeable();
duke@0 1666 global->external = flag->is_external();
duke@0 1667 switch (flag->origin) {
duke@0 1668 case DEFAULT:
duke@0 1669 global->origin = JMM_VMGLOBAL_ORIGIN_DEFAULT;
duke@0 1670 break;
duke@0 1671 case COMMAND_LINE:
duke@0 1672 global->origin = JMM_VMGLOBAL_ORIGIN_COMMAND_LINE;
duke@0 1673 break;
duke@0 1674 case ENVIRON_VAR:
duke@0 1675 global->origin = JMM_VMGLOBAL_ORIGIN_ENVIRON_VAR;
duke@0 1676 break;
duke@0 1677 case CONFIG_FILE:
duke@0 1678 global->origin = JMM_VMGLOBAL_ORIGIN_CONFIG_FILE;
duke@0 1679 break;
duke@0 1680 case MANAGEMENT:
duke@0 1681 global->origin = JMM_VMGLOBAL_ORIGIN_MANAGEMENT;
duke@0 1682 break;
duke@0 1683 case ERGONOMIC:
duke@0 1684 global->origin = JMM_VMGLOBAL_ORIGIN_ERGONOMIC;
duke@0 1685 break;
duke@0 1686 default:
duke@0 1687 global->origin = JMM_VMGLOBAL_ORIGIN_OTHER;
duke@0 1688 }
phh@1067 1689
phh@1067 1690 return true;
duke@0 1691 }
duke@0 1692
duke@0 1693 // Fill globals array of count length with jmmVMGlobal entries
duke@0 1694 // specified by names. If names == NULL, fill globals array
duke@0 1695 // with all Flags. Return value is number of entries
duke@0 1696 // created in globals.
duke@0 1697 // If a Flag with a given name in an array element does not
duke@0 1698 // exist, globals[i].name will be set to NULL.
duke@0 1699 JVM_ENTRY(jint, jmm_GetVMGlobals(JNIEnv *env,
duke@0 1700 jobjectArray names,
duke@0 1701 jmmVMGlobal *globals,
duke@0 1702 jint count))
duke@0 1703
duke@0 1704
duke@0 1705 if (globals == NULL) {
duke@0 1706 THROW_(vmSymbols::java_lang_NullPointerException(), 0);
duke@0 1707 }
duke@0 1708
duke@0 1709 ResourceMark rm(THREAD);
duke@0 1710
duke@0 1711 if (names != NULL) {
duke@0 1712 // return the requested globals
duke@0 1713 objArrayOop ta = objArrayOop(JNIHandles::resolve_non_null(names));
duke@0 1714 objArrayHandle names_ah(THREAD, ta);
duke@0 1715 // Make sure we have a String array
duke@0 1716 klassOop element_klass = objArrayKlass::cast(names_ah->klass())->element_klass();
never@1142 1717 if (element_klass != SystemDictionary::String_klass()) {
duke@0 1718 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 1719 "Array element type is not String class", 0);
duke@0 1720 }
duke@0 1721
duke@0 1722 int names_length = names_ah->length();
duke@0 1723 int num_entries = 0;
duke@0 1724 for (int i = 0; i < names_length && i < count; i++) {
duke@0 1725 oop s = names_ah->obj_at(i);
duke@0 1726 if (s == NULL) {
duke@0 1727 THROW_(vmSymbols::java_lang_NullPointerException(), 0);
duke@0 1728 }
duke@0 1729
duke@0 1730 Handle sh(THREAD, s);
duke@0 1731 char* str = java_lang_String::as_utf8_string(s);
duke@0 1732 Flag* flag = Flag::find_flag(str, strlen(str));
phh@1067 1733 if (flag != NULL &&
phh@1067 1734 add_global_entry(env, sh, &globals[i], flag, THREAD)) {
duke@0 1735 num_entries++;
duke@0 1736 } else {
duke@0 1737 globals[i].name = NULL;
duke@0 1738 }
duke@0 1739 }
duke@0 1740 return num_entries;
duke@0 1741 } else {
duke@0 1742 // return all globals if names == NULL
duke@0 1743
duke@0 1744 // last flag entry is always NULL, so subtract 1
duke@0 1745 int nFlags = (int) Flag::numFlags - 1;
duke@0 1746 Handle null_h;
duke@0 1747 int num_entries = 0;
duke@0 1748 for (int i = 0; i < nFlags && num_entries < count; i++) {
duke@0 1749 Flag* flag = &Flag::flags[i];
ysr@350 1750 // Exclude the locked (diagnostic, experimental) flags
phh@1067 1751 if ((flag->is_unlocked() || flag->is_unlocker()) &&
phh@1067 1752 add_global_entry(env, null_h, &globals[num_entries], flag, THREAD)) {
duke@0 1753 num_entries++;
duke@0 1754 }
duke@0 1755 }
duke@0 1756 return num_entries;
duke@0 1757 }
duke@0 1758 JVM_END
duke@0 1759
duke@0 1760 JVM_ENTRY(void, jmm_SetVMGlobal(JNIEnv *env, jstring flag_name, jvalue new_value))
duke@0 1761 ResourceMark rm(THREAD);
duke@0 1762
duke@0 1763 oop fn = JNIHandles::resolve_external_guard(flag_name);
duke@0 1764 if (fn == NULL) {
duke@0 1765 THROW_MSG(vmSymbols::java_lang_NullPointerException(),
duke@0 1766 "The flag name cannot be null.");
duke@0 1767 }
duke@0 1768 char* name = java_lang_String::as_utf8_string(fn);
duke@0 1769 Flag* flag = Flag::find_flag(name, strlen(name));
duke@0 1770 if (flag == NULL) {
duke@0 1771 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 1772 "Flag does not exist.");
duke@0 1773 }
duke@0 1774 if (!flag->is_writeable()) {
duke@0 1775 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 1776 "This flag is not writeable.");
duke@0 1777 }
duke@0 1778
duke@0 1779 bool succeed;
duke@0 1780 if (flag->is_bool()) {
duke@0 1781 bool bvalue = (new_value.z == JNI_TRUE ? true : false);
duke@0 1782 succeed = CommandLineFlags::boolAtPut(name, &bvalue, MANAGEMENT);
duke@0 1783 } else if (flag->is_intx()) {
phh@1067 1784 intx ivalue = (intx)new_value.j;
duke@0 1785 succeed = CommandLineFlags::intxAtPut(name, &ivalue, MANAGEMENT);
duke@0 1786 } else if (flag->is_uintx()) {
phh@1067 1787 uintx uvalue = (uintx)new_value.j;
duke@0 1788 succeed = CommandLineFlags::uintxAtPut(name, &uvalue, MANAGEMENT);
phh@1067 1789 } else if (flag->is_uint64_t()) {
phh@1067 1790 uint64_t uvalue = (uint64_t)new_value.j;
phh@1067 1791 succeed = CommandLineFlags::uint64_tAtPut(name, &uvalue, MANAGEMENT);
duke@0 1792 } else if (flag->is_ccstr()) {
duke@0 1793 oop str = JNIHandles::resolve_external_guard(new_value.l);
duke@0 1794 if (str == NULL) {
duke@0 1795 THROW(vmSymbols::java_lang_NullPointerException());
duke@0 1796 }
duke@0 1797 ccstr svalue = java_lang_String::as_utf8_string(str);
duke@0 1798 succeed = CommandLineFlags::ccstrAtPut(name, &svalue, MANAGEMENT);
duke@0 1799 }
duke@0 1800 assert(succeed, "Setting flag should succeed");
duke@0 1801 JVM_END
duke@0 1802
duke@0 1803 class ThreadTimesClosure: public ThreadClosure {
duke@0 1804 private:
duke@0 1805 objArrayOop _names;
duke@0 1806 typeArrayOop _times;
duke@0 1807 int _names_len;
duke@0 1808 int _times_len;
duke@0 1809 int _count;
duke@0 1810
duke@0 1811 public:
duke@0 1812 ThreadTimesClosure(objArrayOop names, typeArrayOop times);
duke@0 1813 virtual void do_thread(Thread* thread);
duke@0 1814 int count() { return _count; }
duke@0 1815 };
duke@0 1816
duke@0 1817 ThreadTimesClosure::ThreadTimesClosure(objArrayOop names,
duke@0 1818 typeArrayOop times) {
duke@0 1819 assert(names != NULL, "names was NULL");
duke@0 1820 assert(times != NULL, "times was NULL");
duke@0 1821 _names = names;
duke@0 1822 _names_len = names->length();
duke@0 1823 _times = times;
duke@0 1824 _times_len = times->length();
duke@0 1825 _count = 0;
duke@0 1826 }
duke@0 1827
duke@0 1828 void ThreadTimesClosure::do_thread(Thread* thread) {
duke@0 1829 Handle s;
duke@0 1830 assert(thread != NULL, "thread was NULL");
duke@0 1831
duke@0 1832 // exclude externally visible JavaThreads
duke@0 1833 if (thread->is_Java_thread() && !thread->is_hidden_from_external_view()) {
duke@0 1834 return;
duke@0 1835 }
duke@0 1836
duke@0 1837 if (_count >= _names_len || _count >= _times_len) {
duke@0 1838 // skip if the result array is not big enough
duke@0 1839 return;
duke@0 1840 }
duke@0 1841
duke@0 1842 EXCEPTION_MARK;
duke@0 1843
duke@0 1844 assert(thread->name() != NULL, "All threads should have a name");
duke@0 1845 s = java_lang_String::create_from_str(thread->name(), CHECK);
duke@0 1846 _names->obj_at_put(_count, s());
duke@0 1847
duke@0 1848 _times->long_at_put(_count, os::is_thread_cpu_time_supported() ?
duke@0 1849 os::thread_cpu_time(thread) : -1);
duke@0 1850 _count++;
duke@0 1851 }
duke@0 1852
duke@0 1853 // Fills names with VM internal thread names and times with the corresponding
duke@0 1854 // CPU times. If names or times is NULL, a NullPointerException is thrown.
duke@0 1855 // If the element type of names is not String, an IllegalArgumentException is
duke@0 1856 // thrown.
duke@0 1857 // If an array is not large enough to hold all the entries, only the entries
duke@0 1858 // that fit will be returned. Return value is the number of VM internal
duke@0 1859 // threads entries.
duke@0 1860 JVM_ENTRY(jint, jmm_GetInternalThreadTimes(JNIEnv *env,
duke@0 1861 jobjectArray names,
duke@0 1862 jlongArray times))
duke@0 1863 if (names == NULL || times == NULL) {
duke@0 1864 THROW_(vmSymbols::java_lang_NullPointerException(), 0);
duke@0 1865 }
duke@0 1866 objArrayOop na = objArrayOop(JNIHandles::resolve_non_null(names));
duke@0 1867 objArrayHandle names_ah(THREAD, na);
duke@0 1868
duke@0 1869 // Make sure we have a String array
duke@0 1870 klassOop element_klass = objArrayKlass::cast(names_ah->klass())->element_klass();
never@1142 1871 if (element_klass != SystemDictionary::String_klass()) {
duke@0 1872 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 1873 "Array element type is not String class", 0);
duke@0 1874 }
duke@0 1875
duke@0 1876 typeArrayOop ta = typeArrayOop(JNIHandles::resolve_non_null(times));
duke@0 1877 typeArrayHandle times_ah(THREAD, ta);
duke@0 1878
duke@0 1879 ThreadTimesClosure ttc(names_ah(), times_ah());
duke@0 1880 {
duke@0 1881 MutexLockerEx ml(Threads_lock);
duke@0 1882 Threads::threads_do(&ttc);
duke@0 1883 }
duke@0 1884
duke@0 1885 return ttc.count();
duke@0 1886 JVM_END
duke@0 1887
duke@0 1888 static Handle find_deadlocks(bool object_monitors_only, TRAPS) {
duke@0 1889 ResourceMark rm(THREAD);
duke@0 1890
duke@0 1891 VM_FindDeadlocks op(!object_monitors_only /* also check concurrent locks? */);
duke@0 1892 VMThread::execute(&op);
duke@0 1893
duke@0 1894 DeadlockCycle* deadlocks = op.result();
duke@0 1895 if (deadlocks == NULL) {
duke@0 1896 // no deadlock found and return
duke@0 1897 return Handle();
duke@0 1898 }
duke@0 1899
duke@0 1900 int num_threads = 0;
duke@0 1901 DeadlockCycle* cycle;
duke@0 1902 for (cycle = deadlocks; cycle != NULL; cycle = cycle->next()) {
duke@0 1903 num_threads += cycle->num_threads();
duke@0 1904 }
duke@0 1905
never@1142 1906 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Thread_klass(), num_threads, CHECK_NH);
duke@0 1907 objArrayHandle threads_ah(THREAD, r);
duke@0 1908
duke@0 1909 int index = 0;
duke@0 1910 for (cycle = deadlocks; cycle != NULL; cycle = cycle->next()) {
duke@0 1911 GrowableArray<JavaThread*>* deadlock_threads = cycle->threads();
duke@0 1912 int len = deadlock_threads->length();
duke@0 1913 for (int i = 0; i < len; i++) {
duke@0 1914 threads_ah->obj_at_put(index, deadlock_threads->at(i)->threadObj());
duke@0 1915 index++;
duke@0 1916 }
duke@0 1917 }
duke@0 1918 return threads_ah;
duke@0 1919 }
duke@0 1920
duke@0 1921 // Finds cycles of threads that are deadlocked involved in object monitors
duke@0 1922 // and JSR-166 synchronizers.
duke@0 1923 // Returns an array of Thread objects which are in deadlock, if any.
duke@0 1924 // Otherwise, returns NULL.
duke@0 1925 //
duke@0 1926 // Input parameter:
duke@0 1927 // object_monitors_only - if true, only check object monitors
duke@0 1928 //
duke@0 1929 JVM_ENTRY(jobjectArray, jmm_FindDeadlockedThreads(JNIEnv *env, jboolean object_monitors_only))
duke@0 1930 Handle result = find_deadlocks(object_monitors_only != 0, CHECK_0);
duke@0 1931 return (jobjectArray) JNIHandles::make_local(env, result());
duke@0 1932 JVM_END
duke@0 1933
duke@0 1934 // Finds cycles of threads that are deadlocked on monitor locks
duke@0 1935 // Returns an array of Thread objects which are in deadlock, if any.
duke@0 1936 // Otherwise, returns NULL.
duke@0 1937 JVM_ENTRY(jobjectArray, jmm_FindMonitorDeadlockedThreads(JNIEnv *env))
duke@0 1938 Handle result = find_deadlocks(true, CHECK_0);
duke@0 1939 return (jobjectArray) JNIHandles::make_local(env, result());
duke@0 1940 JVM_END
duke@0 1941
duke@0 1942 // Gets the information about GC extension attributes including
duke@0 1943 // the name of the attribute, its type, and a short description.
duke@0 1944 //
duke@0 1945 // Input parameters:
duke@0 1946 // mgr - GC memory manager
duke@0 1947 // info - caller allocated array of jmmExtAttributeInfo
duke@0 1948 // count - number of elements of the info array
duke@0 1949 //
duke@0 1950 // Returns the number of GC extension attributes filled in the info array; or
duke@0 1951 // -1 if info is not big enough
duke@0 1952 //
duke@0 1953 JVM_ENTRY(jint, jmm_GetGCExtAttributeInfo(JNIEnv *env, jobject mgr, jmmExtAttributeInfo* info, jint count))
duke@0 1954 // All GC memory managers have 1 attribute (number of GC threads)
duke@0 1955 if (count == 0) {
duke@0 1956 return 0;
duke@0 1957 }
duke@0 1958
duke@0 1959 if (info == NULL) {
duke@0 1960 THROW_(vmSymbols::java_lang_NullPointerException(), 0);
duke@0 1961 }
duke@0 1962
duke@0 1963 info[0].name = "GcThreadCount";
duke@0 1964 info[0].type = 'I';
duke@0 1965 info[0].description = "Number of GC threads";
duke@0 1966 return 1;
duke@0 1967 JVM_END
duke@0 1968
duke@0 1969 // verify the given array is an array of java/lang/management/MemoryUsage objects
duke@0 1970 // of a given length and return the objArrayOop
duke@0 1971 static objArrayOop get_memory_usage_objArray(jobjectArray array, int length, TRAPS) {
duke@0 1972 if (array == NULL) {
duke@0 1973 THROW_(vmSymbols::java_lang_NullPointerException(), 0);
duke@0 1974 }
duke@0 1975
duke@0 1976 objArrayOop oa = objArrayOop(JNIHandles::resolve_non_null(array));
duke@0 1977 objArrayHandle array_h(THREAD, oa);
duke@0 1978
duke@0 1979 // array must be of the given length
duke@0 1980 if (length != array_h->length()) {
duke@0 1981 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 1982 "The length of the given MemoryUsage array does not match the number of memory pools.", 0);
duke@0 1983 }
duke@0 1984
duke@0 1985 // check if the element of array is of type MemoryUsage class
duke@0 1986 klassOop usage_klass = Management::java_lang_management_MemoryUsage_klass(CHECK_0);
duke@0 1987 klassOop element_klass = objArrayKlass::cast(array_h->klass())->element_klass();
duke@0 1988 if (element_klass != usage_klass) {
duke@0 1989 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
duke@0 1990 "The element type is not MemoryUsage class", 0);
duke@0 1991 }
duke@0 1992
duke@0 1993 return array_h();
duke@0 1994 }
duke@0 1995
duke@0 1996 // Gets the statistics of the last GC of a given GC memory manager.
duke@0 1997 // Input parameters:
duke@0 1998 // obj - GarbageCollectorMXBean object
duke@0 1999 // gc_stat - caller allocated jmmGCStat where:
duke@0 2000 // a. before_gc_usage - array of MemoryUsage objects
duke@0 2001 // b. after_gc_usage - array of MemoryUsage objects
duke@0 2002 // c. gc_ext_attributes_values_size is set to the
duke@0 2003 // gc_ext_attribute_values array allocated
duke@0 2004 // d. gc_ext_attribute_values is a caller allocated array of jvalue.
duke@0 2005 //
duke@0 2006 // On return,
duke@0 2007 // gc_index == 0 indicates no GC statistics available
duke@0 2008 //
duke@0 2009 // before_gc_usage and after_gc_usage - filled with per memory pool
duke@0 2010 // before and after GC usage in the same order as the memory pools
duke@0 2011 // returned by GetMemoryPools for a given GC memory manager.
duke@0 2012 // num_gc_ext_attributes indicates the number of elements in
duke@0 2013 // the gc_ext_attribute_values array is filled; or
duke@0 2014 // -1 if the gc_ext_attributes_values array is not big enough
duke@0 2015 //
duke@0 2016 JVM_ENTRY(void, jmm_GetLastGCStat(JNIEnv *env, jobject obj, jmmGCStat *gc_stat))
duke@0 2017 ResourceMark rm(THREAD);
duke@0 2018
duke@0 2019 if (gc_stat->gc_ext_attribute_values_size > 0 && gc_stat->gc_ext_attribute_values == NULL) {
duke@0 2020 THROW(vmSymbols::java_lang_NullPointerException());
duke@0 2021 }
duke@0 2022
duke@0 2023 // Get the GCMemoryManager
duke@0 2024 GCMemoryManager* mgr = get_gc_memory_manager_from_jobject(obj, CHECK);
duke@0 2025
duke@0 2026 // Make a copy of the last GC statistics
duke@0 2027 // GC may occur while constructing the last GC information
duke@0 2028 int num_pools = MemoryService::num_memory_pools();
duke@0 2029 GCStatInfo* stat = new GCStatInfo(num_pools);
kevinw@1623 2030 if (mgr->get_last_gc_stat(stat) == 0) {
kevinw@1623 2031 gc_stat->gc_index = 0;
kevinw@1623 2032 return;
kevinw@1623 2033 }
duke@0 2034
duke@0 2035 gc_stat->gc_index = stat->gc_index();
duke@0 2036 gc_stat->start_time = Management::ticks_to_ms(stat->start_time());
duke@0 2037 gc_stat->end_time = Management::ticks_to_ms(stat->end_time());
duke@0 2038
duke@0 2039 // Current implementation does not have GC extension attributes
duke@0 2040 gc_stat->num_gc_ext_attributes = 0;
duke@0 2041
duke@0 2042 // Fill the arrays of MemoryUsage objects with before and after GC
duke@0 2043 // per pool memory usage
duke@0 2044 objArrayOop bu = get_memory_usage_objArray(gc_stat->usage_before_gc,
duke@0 2045 num_pools,
duke@0 2046 CHECK);
duke@0 2047 objArrayHandle usage_before_gc_ah(THREAD, bu);
duke@0 2048
duke@0 2049 objArrayOop au = get_memory_usage_objArray(gc_stat->usage_after_gc,
duke@0 2050 num_pools,
duke@0 2051 CHECK);
duke@0 2052 objArrayHandle usage_after_gc_ah(THREAD, au);
duke@0 2053
duke@0 2054 for (int i = 0; i < num_pools; i++) {
duke@0 2055 Handle before_usage = MemoryService::create_MemoryUsage_obj(stat->before_gc_usage_for_pool(i), CHECK);
duke@0 2056 Handle after_usage;
duke@0 2057
duke@0 2058 MemoryUsage u = stat->after_gc_usage_for_pool(i);
duke@0 2059 if (u.max_size() == 0 && u.used() > 0) {
duke@0 2060 // If max size == 0, this pool is a survivor space.
duke@0 2061 // Set max size = -1 since the pools will be swapped after GC.
duke@0 2062 MemoryUsage usage(u.init_size(), u.used(), u.committed(), (size_t)-1);
duke@0 2063 after_usage = MemoryService::create_MemoryUsage_obj(usage, CHECK);
duke@0 2064 } else {
duke@0 2065 after_usage = MemoryService::create_MemoryUsage_obj(stat->after_gc_usage_for_pool(i), CHECK);
duke@0 2066 }
duke@0 2067 usage_before_gc_ah->obj_at_put(i, before_usage());
duke@0 2068 usage_after_gc_ah->obj_at_put(i, after_usage());
duke@0 2069 }
duke@0 2070
duke@0 2071 if (gc_stat->gc_ext_attribute_values_size > 0) {
duke@0 2072 // Current implementation only has 1 attribute (number of GC threads)
duke@0 2073 // The type is 'I'
duke@0 2074 gc_stat->gc_ext_attribute_values[0].i = mgr->num_gc_threads();
duke@0 2075 }
duke@0 2076 JVM_END
duke@0 2077
fparain@2453 2078 JVM_ENTRY(void, jmm_SetGCNotificationEnabled(JNIEnv *env, jobject obj, jboolean enabled))
fparain@2453 2079 ResourceMark rm(THREAD);
fparain@2453 2080 // Get the GCMemoryManager
fparain@2453 2081 GCMemoryManager* mgr = get_gc_memory_manager_from_jobject(obj, CHECK);
fparain@2453 2082 mgr->set_notification_enabled(enabled?true:false);
fparain@2453 2083 JVM_END
fparain@2453 2084
duke@0 2085 // Dump heap - Returns 0 if succeeds.
duke@0 2086 JVM_ENTRY(jint, jmm_DumpHeap0(JNIEnv *env, jstring outputfile, jboolean live))
duke@0 2087 #ifndef SERVICES_KERNEL
duke@0 2088 ResourceMark rm(THREAD);
duke@0 2089 oop on = JNIHandles::resolve_external_guard(outputfile);
duke@0 2090 if (on == NULL) {
duke@0 2091 THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
duke@0 2092 "Output file name cannot be null.", -1);
duke@0 2093 }
duke@0 2094 char* name = java_lang_String::as_utf8_string(on);
duke@0 2095 if (name == NULL) {
duke@0 2096 THROW_MSG_(vmSymbols::java_lang_NullPointerException(),
duke@0 2097 "Output file name cannot be null.", -1);
duke@0 2098 }
duke@0 2099 HeapDumper dumper(live ? true : false);
duke@0 2100 if (dumper.dump(name) != 0) {
duke@0 2101 const char* errmsg = dumper.error_as_C_string();
duke@0 2102 THROW_MSG_(vmSymbols::java_io_IOException(), errmsg, -1);
duke@0 2103 }
duke@0 2104 return 0;
duke@0 2105 #else // SERVICES_KERNEL
duke@0 2106 return -1;
duke@0 2107 #endif // SERVICES_KERNEL
duke@0 2108 JVM_END
duke@0 2109
duke@0 2110 jlong Management::ticks_to_ms(jlong ticks) {
duke@0 2111 assert(os::elapsed_frequency() > 0, "Must be non-zero");
duke@0 2112 return (jlong)(((double)ticks / (double)os::elapsed_frequency())
duke@0 2113 * (double)1000.0);
duke@0 2114 }
duke@0 2115
duke@0 2116 const struct jmmInterface_1_ jmm_interface = {
duke@0 2117 NULL,
duke@0 2118 NULL,
duke@0 2119 jmm_GetVersion,
duke@0 2120 jmm_GetOptionalSupport,
duke@0 2121 jmm_GetInputArguments,
duke@0 2122 jmm_GetThreadInfo,
duke@0 2123 jmm_GetInputArgumentArray,
duke@0 2124 jmm_GetMemoryPools,
duke@0 2125 jmm_GetMemoryManagers,
duke@0 2126 jmm_GetMemoryPoolUsage,
duke@0 2127 jmm_GetPeakMemoryPoolUsage,
phh@1988 2128 jmm_GetThreadAllocatedMemory,
duke@0 2129 jmm_GetMemoryUsage,
duke@0 2130 jmm_GetLongAttribute,
duke@0 2131 jmm_GetBoolAttribute,
duke@0 2132 jmm_SetBoolAttribute,
duke@0 2133 jmm_GetLongAttributes,
duke@0 2134 jmm_FindMonitorDeadlockedThreads,
duke@0 2135 jmm_GetThreadCpuTime,
duke@0 2136 jmm_GetVMGlobalNames,
duke@0 2137 jmm_GetVMGlobals,
duke@0 2138 jmm_GetInternalThreadTimes,
duke@0 2139 jmm_ResetStatistic,
duke@0 2140 jmm_SetPoolSensor,
duke@0 2141 jmm_SetPoolThreshold,
duke@0 2142 jmm_GetPoolCollectionUsage,
duke@0 2143 jmm_GetGCExtAttributeInfo,
duke@0 2144 jmm_GetLastGCStat,
duke@0 2145 jmm_GetThreadCpuTimeWithKind,
phh@1988 2146 jmm_GetThreadCpuTimesWithKind,
duke@0 2147 jmm_DumpHeap0,
duke@0 2148 jmm_FindDeadlockedThreads,
duke@0 2149 jmm_SetVMGlobal,
duke@0 2150 NULL,
fparain@2453 2151 jmm_DumpThreads,
fparain@2453 2152 jmm_SetGCNotificationEnabled
duke@0 2153 };
duke@0 2154
duke@0 2155 void* Management::get_jmm_interface(int version) {
duke@0 2156 if (version == JMM_VERSION_1_0) {
duke@0 2157 return (void*) &jmm_interface;
duke@0 2158 }
duke@0 2159 return NULL;
duke@0 2160 }