src/share/vm/services/management.cpp

Fri, 03 Apr 2015 15:40:35 +0200

author
jbachorik
date
Fri, 03 Apr 2015 15:40:35 +0200
changeset 8081
eba0866f9379
parent 7632
ffae627760ca
child 8604
04d83ba48607
child 9979
9f2b95a3c80b
permissions
-rw-r--r--

8074368: ThreadMXBean.getThreadInfo() corrupts memory when called with empty array for thread ids
Reviewed-by: mchung

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

mercurial