src/share/vm/services/management.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7107
966601b12d4f
parent 6876
710a3c8b516e
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

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

mercurial