src/share/vm/services/management.cpp

Wed, 21 Aug 2013 22:12:11 -0700

author
poonam
date
Wed, 21 Aug 2013 22:12:11 -0700
changeset 5571
a70566600baf
parent 5472
c01913206da5
child 5790
72b7e96c1922
permissions
-rw-r--r--

8020530: Non heap memory size calculated incorrectly
Reviewed-by: coleenp, sla
Contributed-by: vladimir.kempik@oracle.com

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

mercurial