src/share/vm/services/management.cpp

Tue, 17 Oct 2017 12:58:25 +0800

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

mercurial