aoqi@0: /* aoqi@0: * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "compiler/compileBroker.hpp" aoqi@0: #include "memory/iterator.hpp" aoqi@0: #include "memory/oopFactory.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "oops/klass.hpp" aoqi@0: #include "oops/objArrayKlass.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/arguments.hpp" aoqi@0: #include "runtime/globals.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/interfaceSupport.hpp" aoqi@0: #include "runtime/javaCalls.hpp" aoqi@0: #include "runtime/jniHandles.hpp" aoqi@0: #include "runtime/os.hpp" aoqi@0: #include "runtime/serviceThread.hpp" goetz@6911: #include "runtime/thread.inline.hpp" aoqi@0: #include "services/classLoadingService.hpp" aoqi@0: #include "services/diagnosticCommand.hpp" aoqi@0: #include "services/diagnosticFramework.hpp" aoqi@0: #include "services/heapDumper.hpp" aoqi@0: #include "services/jmm.h" aoqi@0: #include "services/lowMemoryDetector.hpp" aoqi@0: #include "services/gcNotifier.hpp" aoqi@0: #include "services/nmtDCmd.hpp" aoqi@0: #include "services/management.hpp" aoqi@0: #include "services/memoryManager.hpp" aoqi@0: #include "services/memoryPool.hpp" aoqi@0: #include "services/memoryService.hpp" aoqi@0: #include "services/runtimeService.hpp" aoqi@0: #include "services/threadService.hpp" aoqi@0: #include "utilities/macros.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: PerfVariable* Management::_begin_vm_creation_time = NULL; aoqi@0: PerfVariable* Management::_end_vm_creation_time = NULL; aoqi@0: PerfVariable* Management::_vm_init_done_time = NULL; aoqi@0: aoqi@0: Klass* Management::_sensor_klass = NULL; aoqi@0: Klass* Management::_threadInfo_klass = NULL; aoqi@0: Klass* Management::_memoryUsage_klass = NULL; aoqi@0: Klass* Management::_memoryPoolMXBean_klass = NULL; aoqi@0: Klass* Management::_memoryManagerMXBean_klass = NULL; aoqi@0: Klass* Management::_garbageCollectorMXBean_klass = NULL; aoqi@0: Klass* Management::_managementFactory_klass = NULL; aoqi@0: Klass* Management::_garbageCollectorImpl_klass = NULL; aoqi@0: Klass* Management::_gcInfo_klass = NULL; aoqi@0: Klass* Management::_diagnosticCommandImpl_klass = NULL; aoqi@0: Klass* Management::_managementFactoryHelper_klass = NULL; aoqi@0: aoqi@0: aoqi@0: jmmOptionalSupport Management::_optional_support = {0}; aoqi@0: TimeStamp Management::_stamp; aoqi@0: aoqi@0: void management_init() { aoqi@0: #if INCLUDE_MANAGEMENT aoqi@0: Management::init(); aoqi@0: ThreadService::init(); aoqi@0: RuntimeService::init(); aoqi@0: ClassLoadingService::init(); aoqi@0: #else aoqi@0: ThreadService::init(); aoqi@0: // Make sure the VM version is initialized aoqi@0: // This is normally called by RuntimeService::init(). aoqi@0: // Since that is conditionalized out, we need to call it here. aoqi@0: Abstract_VM_Version::initialize(); aoqi@0: #endif // INCLUDE_MANAGEMENT aoqi@0: } aoqi@0: aoqi@0: #if INCLUDE_MANAGEMENT aoqi@0: aoqi@0: void Management::init() { aoqi@0: EXCEPTION_MARK; aoqi@0: aoqi@0: // These counters are for java.lang.management API support. aoqi@0: // They are created even if -XX:-UsePerfData is set and in aoqi@0: // that case, they will be allocated on C heap. aoqi@0: aoqi@0: _begin_vm_creation_time = aoqi@0: PerfDataManager::create_variable(SUN_RT, "createVmBeginTime", aoqi@0: PerfData::U_None, CHECK); aoqi@0: aoqi@0: _end_vm_creation_time = aoqi@0: PerfDataManager::create_variable(SUN_RT, "createVmEndTime", aoqi@0: PerfData::U_None, CHECK); aoqi@0: aoqi@0: _vm_init_done_time = aoqi@0: PerfDataManager::create_variable(SUN_RT, "vmInitDoneTime", aoqi@0: PerfData::U_None, CHECK); aoqi@0: aoqi@0: // Initialize optional support aoqi@0: _optional_support.isLowMemoryDetectionSupported = 1; aoqi@0: _optional_support.isCompilationTimeMonitoringSupported = 1; aoqi@0: _optional_support.isThreadContentionMonitoringSupported = 1; aoqi@0: aoqi@0: if (os::is_thread_cpu_time_supported()) { aoqi@0: _optional_support.isCurrentThreadCpuTimeSupported = 1; aoqi@0: _optional_support.isOtherThreadCpuTimeSupported = 1; aoqi@0: } else { aoqi@0: _optional_support.isCurrentThreadCpuTimeSupported = 0; aoqi@0: _optional_support.isOtherThreadCpuTimeSupported = 0; aoqi@0: } aoqi@0: aoqi@0: _optional_support.isBootClassPathSupported = 1; aoqi@0: _optional_support.isObjectMonitorUsageSupported = 1; aoqi@0: #if INCLUDE_SERVICES aoqi@0: // This depends on the heap inspector aoqi@0: _optional_support.isSynchronizerUsageSupported = 1; aoqi@0: #endif // INCLUDE_SERVICES aoqi@0: _optional_support.isThreadAllocatedMemorySupported = 1; aoqi@0: _optional_support.isRemoteDiagnosticCommandsSupported = 1; aoqi@0: aoqi@0: // Registration of the diagnostic commands aoqi@0: DCmdRegistrant::register_dcmds(); aoqi@0: DCmdRegistrant::register_dcmds_ext(); aoqi@0: uint32_t full_export = DCmd_Source_Internal | DCmd_Source_AttachAPI aoqi@0: | DCmd_Source_MBean; aoqi@0: DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); aoqi@0: } aoqi@0: aoqi@0: void Management::initialize(TRAPS) { aoqi@0: // Start the service thread aoqi@0: ServiceThread::initialize(); aoqi@0: aoqi@0: if (ManagementServer) { aoqi@0: ResourceMark rm(THREAD); aoqi@0: HandleMark hm(THREAD); aoqi@0: aoqi@0: // Load and initialize the sun.management.Agent class aoqi@0: // invoke startAgent method to start the management server aoqi@0: Handle loader = Handle(THREAD, SystemDictionary::java_system_loader()); bpittore@7632: Klass* k = SystemDictionary::resolve_or_null(vmSymbols::sun_management_Agent(), aoqi@0: loader, aoqi@0: Handle(), bpittore@7632: THREAD); bpittore@7632: if (k == NULL) { bpittore@7632: vm_exit_during_initialization("Management agent initialization failure: " bpittore@7632: "class sun.management.Agent not found."); bpittore@7632: } aoqi@0: instanceKlassHandle ik (THREAD, k); aoqi@0: aoqi@0: JavaValue result(T_VOID); aoqi@0: JavaCalls::call_static(&result, aoqi@0: ik, aoqi@0: vmSymbols::startAgent_name(), aoqi@0: vmSymbols::void_method_signature(), aoqi@0: CHECK); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void Management::get_optional_support(jmmOptionalSupport* support) { aoqi@0: memcpy(support, &_optional_support, sizeof(jmmOptionalSupport)); aoqi@0: } aoqi@0: aoqi@0: Klass* Management::load_and_initialize_klass(Symbol* sh, TRAPS) { aoqi@0: Klass* k = SystemDictionary::resolve_or_fail(sh, true, CHECK_NULL); aoqi@0: instanceKlassHandle ik (THREAD, k); aoqi@0: if (ik->should_be_initialized()) { aoqi@0: ik->initialize(CHECK_NULL); aoqi@0: } aoqi@0: // If these classes change to not be owned by the boot loader, they need aoqi@0: // to be walked to keep their class loader alive in oops_do. aoqi@0: assert(ik->class_loader() == NULL, "need to follow in oops_do"); aoqi@0: return ik(); aoqi@0: } aoqi@0: aoqi@0: void Management::record_vm_startup_time(jlong begin, jlong duration) { aoqi@0: // if the performance counter is not initialized, aoqi@0: // then vm initialization failed; simply return. aoqi@0: if (_begin_vm_creation_time == NULL) return; aoqi@0: aoqi@0: _begin_vm_creation_time->set_value(begin); aoqi@0: _end_vm_creation_time->set_value(begin + duration); aoqi@0: PerfMemory::set_accessible(true); aoqi@0: } aoqi@0: aoqi@0: jlong Management::timestamp() { aoqi@0: TimeStamp t; aoqi@0: t.update(); aoqi@0: return t.ticks() - _stamp.ticks(); aoqi@0: } aoqi@0: aoqi@0: void Management::oops_do(OopClosure* f) { aoqi@0: MemoryService::oops_do(f); aoqi@0: ThreadService::oops_do(f); aoqi@0: } aoqi@0: aoqi@0: Klass* Management::java_lang_management_ThreadInfo_klass(TRAPS) { aoqi@0: if (_threadInfo_klass == NULL) { aoqi@0: _threadInfo_klass = load_and_initialize_klass(vmSymbols::java_lang_management_ThreadInfo(), CHECK_NULL); aoqi@0: } aoqi@0: return _threadInfo_klass; aoqi@0: } aoqi@0: aoqi@0: Klass* Management::java_lang_management_MemoryUsage_klass(TRAPS) { aoqi@0: if (_memoryUsage_klass == NULL) { aoqi@0: _memoryUsage_klass = load_and_initialize_klass(vmSymbols::java_lang_management_MemoryUsage(), CHECK_NULL); aoqi@0: } aoqi@0: return _memoryUsage_klass; aoqi@0: } aoqi@0: aoqi@0: Klass* Management::java_lang_management_MemoryPoolMXBean_klass(TRAPS) { aoqi@0: if (_memoryPoolMXBean_klass == NULL) { aoqi@0: _memoryPoolMXBean_klass = load_and_initialize_klass(vmSymbols::java_lang_management_MemoryPoolMXBean(), CHECK_NULL); aoqi@0: } aoqi@0: return _memoryPoolMXBean_klass; aoqi@0: } aoqi@0: aoqi@0: Klass* Management::java_lang_management_MemoryManagerMXBean_klass(TRAPS) { aoqi@0: if (_memoryManagerMXBean_klass == NULL) { aoqi@0: _memoryManagerMXBean_klass = load_and_initialize_klass(vmSymbols::java_lang_management_MemoryManagerMXBean(), CHECK_NULL); aoqi@0: } aoqi@0: return _memoryManagerMXBean_klass; aoqi@0: } aoqi@0: aoqi@0: Klass* Management::java_lang_management_GarbageCollectorMXBean_klass(TRAPS) { aoqi@0: if (_garbageCollectorMXBean_klass == NULL) { aoqi@0: _garbageCollectorMXBean_klass = load_and_initialize_klass(vmSymbols::java_lang_management_GarbageCollectorMXBean(), CHECK_NULL); aoqi@0: } aoqi@0: return _garbageCollectorMXBean_klass; aoqi@0: } aoqi@0: aoqi@0: Klass* Management::sun_management_Sensor_klass(TRAPS) { aoqi@0: if (_sensor_klass == NULL) { aoqi@0: _sensor_klass = load_and_initialize_klass(vmSymbols::sun_management_Sensor(), CHECK_NULL); aoqi@0: } aoqi@0: return _sensor_klass; aoqi@0: } aoqi@0: aoqi@0: Klass* Management::sun_management_ManagementFactory_klass(TRAPS) { aoqi@0: if (_managementFactory_klass == NULL) { aoqi@0: _managementFactory_klass = load_and_initialize_klass(vmSymbols::sun_management_ManagementFactory(), CHECK_NULL); aoqi@0: } aoqi@0: return _managementFactory_klass; aoqi@0: } aoqi@0: aoqi@0: Klass* Management::sun_management_GarbageCollectorImpl_klass(TRAPS) { aoqi@0: if (_garbageCollectorImpl_klass == NULL) { aoqi@0: _garbageCollectorImpl_klass = load_and_initialize_klass(vmSymbols::sun_management_GarbageCollectorImpl(), CHECK_NULL); aoqi@0: } aoqi@0: return _garbageCollectorImpl_klass; aoqi@0: } aoqi@0: aoqi@0: Klass* Management::com_sun_management_GcInfo_klass(TRAPS) { aoqi@0: if (_gcInfo_klass == NULL) { aoqi@0: _gcInfo_klass = load_and_initialize_klass(vmSymbols::com_sun_management_GcInfo(), CHECK_NULL); aoqi@0: } aoqi@0: return _gcInfo_klass; aoqi@0: } aoqi@0: aoqi@0: Klass* Management::sun_management_DiagnosticCommandImpl_klass(TRAPS) { aoqi@0: if (_diagnosticCommandImpl_klass == NULL) { aoqi@0: _diagnosticCommandImpl_klass = load_and_initialize_klass(vmSymbols::sun_management_DiagnosticCommandImpl(), CHECK_NULL); aoqi@0: } aoqi@0: return _diagnosticCommandImpl_klass; aoqi@0: } aoqi@0: aoqi@0: Klass* Management::sun_management_ManagementFactoryHelper_klass(TRAPS) { aoqi@0: if (_managementFactoryHelper_klass == NULL) { aoqi@0: _managementFactoryHelper_klass = load_and_initialize_klass(vmSymbols::sun_management_ManagementFactoryHelper(), CHECK_NULL); aoqi@0: } aoqi@0: return _managementFactoryHelper_klass; aoqi@0: } aoqi@0: aoqi@0: static void initialize_ThreadInfo_constructor_arguments(JavaCallArguments* args, ThreadSnapshot* snapshot, TRAPS) { aoqi@0: Handle snapshot_thread(THREAD, snapshot->threadObj()); aoqi@0: aoqi@0: jlong contended_time; aoqi@0: jlong waited_time; aoqi@0: if (ThreadService::is_thread_monitoring_contention()) { aoqi@0: contended_time = Management::ticks_to_ms(snapshot->contended_enter_ticks()); aoqi@0: waited_time = Management::ticks_to_ms(snapshot->monitor_wait_ticks() + snapshot->sleep_ticks()); aoqi@0: } else { aoqi@0: // set them to -1 if thread contention monitoring is disabled. aoqi@0: contended_time = max_julong; aoqi@0: waited_time = max_julong; aoqi@0: } aoqi@0: aoqi@0: int thread_status = snapshot->thread_status(); aoqi@0: assert((thread_status & JMM_THREAD_STATE_FLAG_MASK) == 0, "Flags already set in thread_status in Thread object"); aoqi@0: if (snapshot->is_ext_suspended()) { aoqi@0: thread_status |= JMM_THREAD_STATE_FLAG_SUSPENDED; aoqi@0: } aoqi@0: if (snapshot->is_in_native()) { aoqi@0: thread_status |= JMM_THREAD_STATE_FLAG_NATIVE; aoqi@0: } aoqi@0: aoqi@0: ThreadStackTrace* st = snapshot->get_stack_trace(); aoqi@0: Handle stacktrace_h; aoqi@0: if (st != NULL) { aoqi@0: stacktrace_h = st->allocate_fill_stack_trace_element_array(CHECK); aoqi@0: } else { aoqi@0: stacktrace_h = Handle(); aoqi@0: } aoqi@0: aoqi@0: args->push_oop(snapshot_thread); aoqi@0: args->push_int(thread_status); aoqi@0: args->push_oop(Handle(THREAD, snapshot->blocker_object())); aoqi@0: args->push_oop(Handle(THREAD, snapshot->blocker_object_owner())); aoqi@0: args->push_long(snapshot->contended_enter_count()); aoqi@0: args->push_long(contended_time); aoqi@0: args->push_long(snapshot->monitor_wait_count() + snapshot->sleep_count()); aoqi@0: args->push_long(waited_time); aoqi@0: args->push_oop(stacktrace_h); aoqi@0: } aoqi@0: aoqi@0: // Helper function to construct a ThreadInfo object aoqi@0: instanceOop Management::create_thread_info_instance(ThreadSnapshot* snapshot, TRAPS) { aoqi@0: Klass* k = Management::java_lang_management_ThreadInfo_klass(CHECK_NULL); aoqi@0: instanceKlassHandle ik (THREAD, k); aoqi@0: aoqi@0: JavaValue result(T_VOID); aoqi@0: JavaCallArguments args(14); aoqi@0: aoqi@0: // First allocate a ThreadObj object and aoqi@0: // push the receiver as the first argument aoqi@0: Handle element = ik->allocate_instance_handle(CHECK_NULL); aoqi@0: args.push_oop(element); aoqi@0: aoqi@0: // initialize the arguments for the ThreadInfo constructor aoqi@0: initialize_ThreadInfo_constructor_arguments(&args, snapshot, CHECK_NULL); aoqi@0: aoqi@0: // Call ThreadInfo constructor with no locked monitors and synchronizers aoqi@0: JavaCalls::call_special(&result, aoqi@0: ik, aoqi@0: vmSymbols::object_initializer_name(), aoqi@0: vmSymbols::java_lang_management_ThreadInfo_constructor_signature(), aoqi@0: &args, aoqi@0: CHECK_NULL); aoqi@0: aoqi@0: return (instanceOop) element(); aoqi@0: } aoqi@0: aoqi@0: instanceOop Management::create_thread_info_instance(ThreadSnapshot* snapshot, aoqi@0: objArrayHandle monitors_array, aoqi@0: typeArrayHandle depths_array, aoqi@0: objArrayHandle synchronizers_array, aoqi@0: TRAPS) { aoqi@0: Klass* k = Management::java_lang_management_ThreadInfo_klass(CHECK_NULL); aoqi@0: instanceKlassHandle ik (THREAD, k); aoqi@0: aoqi@0: JavaValue result(T_VOID); aoqi@0: JavaCallArguments args(17); aoqi@0: aoqi@0: // First allocate a ThreadObj object and aoqi@0: // push the receiver as the first argument aoqi@0: Handle element = ik->allocate_instance_handle(CHECK_NULL); aoqi@0: args.push_oop(element); aoqi@0: aoqi@0: // initialize the arguments for the ThreadInfo constructor aoqi@0: initialize_ThreadInfo_constructor_arguments(&args, snapshot, CHECK_NULL); aoqi@0: aoqi@0: // push the locked monitors and synchronizers in the arguments aoqi@0: args.push_oop(monitors_array); aoqi@0: args.push_oop(depths_array); aoqi@0: args.push_oop(synchronizers_array); aoqi@0: aoqi@0: // Call ThreadInfo constructor with locked monitors and synchronizers aoqi@0: JavaCalls::call_special(&result, aoqi@0: ik, aoqi@0: vmSymbols::object_initializer_name(), aoqi@0: vmSymbols::java_lang_management_ThreadInfo_with_locks_constructor_signature(), aoqi@0: &args, aoqi@0: CHECK_NULL); aoqi@0: aoqi@0: return (instanceOop) element(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: static GCMemoryManager* get_gc_memory_manager_from_jobject(jobject mgr, TRAPS) { aoqi@0: if (mgr == NULL) { aoqi@0: THROW_(vmSymbols::java_lang_NullPointerException(), NULL); aoqi@0: } aoqi@0: oop mgr_obj = JNIHandles::resolve(mgr); aoqi@0: instanceHandle h(THREAD, (instanceOop) mgr_obj); aoqi@0: aoqi@0: Klass* k = Management::java_lang_management_GarbageCollectorMXBean_klass(CHECK_NULL); aoqi@0: if (!h->is_a(k)) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "the object is not an instance of java.lang.management.GarbageCollectorMXBean class", aoqi@0: NULL); aoqi@0: } aoqi@0: aoqi@0: MemoryManager* gc = MemoryService::get_memory_manager(h); aoqi@0: if (gc == NULL || !gc->is_gc_memory_manager()) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Invalid GC memory manager", aoqi@0: NULL); aoqi@0: } aoqi@0: return (GCMemoryManager*) gc; aoqi@0: } aoqi@0: aoqi@0: static MemoryPool* get_memory_pool_from_jobject(jobject obj, TRAPS) { aoqi@0: if (obj == NULL) { aoqi@0: THROW_(vmSymbols::java_lang_NullPointerException(), NULL); aoqi@0: } aoqi@0: aoqi@0: oop pool_obj = JNIHandles::resolve(obj); aoqi@0: assert(pool_obj->is_instance(), "Should be an instanceOop"); aoqi@0: instanceHandle ph(THREAD, (instanceOop) pool_obj); aoqi@0: aoqi@0: return MemoryService::get_memory_pool(ph); aoqi@0: } aoqi@0: sla@7107: #endif // INCLUDE_MANAGEMENT sla@7107: aoqi@0: static void validate_thread_id_array(typeArrayHandle ids_ah, TRAPS) { aoqi@0: int num_threads = ids_ah->length(); aoqi@0: aoqi@0: // Validate input thread IDs aoqi@0: int i = 0; aoqi@0: for (i = 0; i < num_threads; i++) { aoqi@0: jlong tid = ids_ah->long_at(i); aoqi@0: if (tid <= 0) { aoqi@0: // throw exception if invalid thread id. aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Invalid thread ID entry"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: sla@7107: #if INCLUDE_MANAGEMENT sla@7107: aoqi@0: static void validate_thread_info_array(objArrayHandle infoArray_h, TRAPS) { aoqi@0: // check if the element of infoArray is of type ThreadInfo class aoqi@0: Klass* threadinfo_klass = Management::java_lang_management_ThreadInfo_klass(CHECK); aoqi@0: Klass* element_klass = ObjArrayKlass::cast(infoArray_h->klass())->element_klass(); aoqi@0: if (element_klass != threadinfo_klass) { aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "infoArray element type is not ThreadInfo class"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: static MemoryManager* get_memory_manager_from_jobject(jobject obj, TRAPS) { aoqi@0: if (obj == NULL) { aoqi@0: THROW_(vmSymbols::java_lang_NullPointerException(), NULL); aoqi@0: } aoqi@0: aoqi@0: oop mgr_obj = JNIHandles::resolve(obj); aoqi@0: assert(mgr_obj->is_instance(), "Should be an instanceOop"); aoqi@0: instanceHandle mh(THREAD, (instanceOop) mgr_obj); aoqi@0: aoqi@0: return MemoryService::get_memory_manager(mh); aoqi@0: } aoqi@0: aoqi@0: // Returns a version string and sets major and minor version if aoqi@0: // the input parameters are non-null. aoqi@0: JVM_LEAF(jint, jmm_GetVersion(JNIEnv *env)) aoqi@0: return JMM_VERSION; aoqi@0: JVM_END aoqi@0: aoqi@0: // Gets the list of VM monitoring and management optional supports aoqi@0: // Returns 0 if succeeded; otherwise returns non-zero. aoqi@0: JVM_LEAF(jint, jmm_GetOptionalSupport(JNIEnv *env, jmmOptionalSupport* support)) aoqi@0: if (support == NULL) { aoqi@0: return -1; aoqi@0: } aoqi@0: Management::get_optional_support(support); aoqi@0: return 0; aoqi@0: JVM_END aoqi@0: aoqi@0: // Returns a java.lang.String object containing the input arguments to the VM. aoqi@0: JVM_ENTRY(jobject, jmm_GetInputArguments(JNIEnv *env)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: if (Arguments::num_jvm_args() == 0 && Arguments::num_jvm_flags() == 0) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: char** vm_flags = Arguments::jvm_flags_array(); aoqi@0: char** vm_args = Arguments::jvm_args_array(); aoqi@0: int num_flags = Arguments::num_jvm_flags(); aoqi@0: int num_args = Arguments::num_jvm_args(); aoqi@0: aoqi@0: size_t length = 1; // null terminator aoqi@0: int i; aoqi@0: for (i = 0; i < num_flags; i++) { aoqi@0: length += strlen(vm_flags[i]); aoqi@0: } aoqi@0: for (i = 0; i < num_args; i++) { aoqi@0: length += strlen(vm_args[i]); aoqi@0: } aoqi@0: // add a space between each argument aoqi@0: length += num_flags + num_args - 1; aoqi@0: aoqi@0: // Return the list of input arguments passed to the VM aoqi@0: // and preserve the order that the VM processes. aoqi@0: char* args = NEW_RESOURCE_ARRAY(char, length); aoqi@0: args[0] = '\0'; aoqi@0: // concatenate all jvm_flags aoqi@0: if (num_flags > 0) { aoqi@0: strcat(args, vm_flags[0]); aoqi@0: for (i = 1; i < num_flags; i++) { aoqi@0: strcat(args, " "); aoqi@0: strcat(args, vm_flags[i]); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (num_args > 0 && num_flags > 0) { aoqi@0: // append a space if args already contains one or more jvm_flags aoqi@0: strcat(args, " "); aoqi@0: } aoqi@0: aoqi@0: // concatenate all jvm_args aoqi@0: if (num_args > 0) { aoqi@0: strcat(args, vm_args[0]); aoqi@0: for (i = 1; i < num_args; i++) { aoqi@0: strcat(args, " "); aoqi@0: strcat(args, vm_args[i]); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: Handle hargs = java_lang_String::create_from_platform_dependent_str(args, CHECK_NULL); aoqi@0: return JNIHandles::make_local(env, hargs()); aoqi@0: JVM_END aoqi@0: aoqi@0: // Returns an array of java.lang.String object containing the input arguments to the VM. aoqi@0: JVM_ENTRY(jobjectArray, jmm_GetInputArgumentArray(JNIEnv *env)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: if (Arguments::num_jvm_args() == 0 && Arguments::num_jvm_flags() == 0) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: char** vm_flags = Arguments::jvm_flags_array(); aoqi@0: char** vm_args = Arguments::jvm_args_array(); aoqi@0: int num_flags = Arguments::num_jvm_flags(); aoqi@0: int num_args = Arguments::num_jvm_args(); aoqi@0: aoqi@0: instanceKlassHandle ik (THREAD, SystemDictionary::String_klass()); aoqi@0: objArrayOop r = oopFactory::new_objArray(ik(), num_args + num_flags, CHECK_NULL); aoqi@0: objArrayHandle result_h(THREAD, r); aoqi@0: aoqi@0: int index = 0; aoqi@0: for (int j = 0; j < num_flags; j++, index++) { aoqi@0: Handle h = java_lang_String::create_from_platform_dependent_str(vm_flags[j], CHECK_NULL); aoqi@0: result_h->obj_at_put(index, h()); aoqi@0: } aoqi@0: for (int i = 0; i < num_args; i++, index++) { aoqi@0: Handle h = java_lang_String::create_from_platform_dependent_str(vm_args[i], CHECK_NULL); aoqi@0: result_h->obj_at_put(index, h()); aoqi@0: } aoqi@0: return (jobjectArray) JNIHandles::make_local(env, result_h()); aoqi@0: JVM_END aoqi@0: aoqi@0: // Returns an array of java/lang/management/MemoryPoolMXBean object aoqi@0: // one for each memory pool if obj == null; otherwise returns aoqi@0: // an array of memory pools for a given memory manager if aoqi@0: // it is a valid memory manager. aoqi@0: JVM_ENTRY(jobjectArray, jmm_GetMemoryPools(JNIEnv* env, jobject obj)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: int num_memory_pools; aoqi@0: MemoryManager* mgr = NULL; aoqi@0: if (obj == NULL) { aoqi@0: num_memory_pools = MemoryService::num_memory_pools(); aoqi@0: } else { aoqi@0: mgr = get_memory_manager_from_jobject(obj, CHECK_NULL); aoqi@0: if (mgr == NULL) { aoqi@0: return NULL; aoqi@0: } aoqi@0: num_memory_pools = mgr->num_memory_pools(); aoqi@0: } aoqi@0: aoqi@0: // Allocate the resulting MemoryPoolMXBean[] object aoqi@0: Klass* k = Management::java_lang_management_MemoryPoolMXBean_klass(CHECK_NULL); aoqi@0: instanceKlassHandle ik (THREAD, k); aoqi@0: objArrayOop r = oopFactory::new_objArray(ik(), num_memory_pools, CHECK_NULL); aoqi@0: objArrayHandle poolArray(THREAD, r); aoqi@0: aoqi@0: if (mgr == NULL) { aoqi@0: // Get all memory pools aoqi@0: for (int i = 0; i < num_memory_pools; i++) { aoqi@0: MemoryPool* pool = MemoryService::get_memory_pool(i); aoqi@0: instanceOop p = pool->get_memory_pool_instance(CHECK_NULL); aoqi@0: instanceHandle ph(THREAD, p); aoqi@0: poolArray->obj_at_put(i, ph()); aoqi@0: } aoqi@0: } else { aoqi@0: // Get memory pools managed by a given memory manager aoqi@0: for (int i = 0; i < num_memory_pools; i++) { aoqi@0: MemoryPool* pool = mgr->get_memory_pool(i); aoqi@0: instanceOop p = pool->get_memory_pool_instance(CHECK_NULL); aoqi@0: instanceHandle ph(THREAD, p); aoqi@0: poolArray->obj_at_put(i, ph()); aoqi@0: } aoqi@0: } aoqi@0: return (jobjectArray) JNIHandles::make_local(env, poolArray()); aoqi@0: JVM_END aoqi@0: aoqi@0: // Returns an array of java/lang/management/MemoryManagerMXBean object aoqi@0: // one for each memory manager if obj == null; otherwise returns aoqi@0: // an array of memory managers for a given memory pool if aoqi@0: // it is a valid memory pool. aoqi@0: JVM_ENTRY(jobjectArray, jmm_GetMemoryManagers(JNIEnv* env, jobject obj)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: int num_mgrs; aoqi@0: MemoryPool* pool = NULL; aoqi@0: if (obj == NULL) { aoqi@0: num_mgrs = MemoryService::num_memory_managers(); aoqi@0: } else { aoqi@0: pool = get_memory_pool_from_jobject(obj, CHECK_NULL); aoqi@0: if (pool == NULL) { aoqi@0: return NULL; aoqi@0: } aoqi@0: num_mgrs = pool->num_memory_managers(); aoqi@0: } aoqi@0: aoqi@0: // Allocate the resulting MemoryManagerMXBean[] object aoqi@0: Klass* k = Management::java_lang_management_MemoryManagerMXBean_klass(CHECK_NULL); aoqi@0: instanceKlassHandle ik (THREAD, k); aoqi@0: objArrayOop r = oopFactory::new_objArray(ik(), num_mgrs, CHECK_NULL); aoqi@0: objArrayHandle mgrArray(THREAD, r); aoqi@0: aoqi@0: if (pool == NULL) { aoqi@0: // Get all memory managers aoqi@0: for (int i = 0; i < num_mgrs; i++) { aoqi@0: MemoryManager* mgr = MemoryService::get_memory_manager(i); aoqi@0: instanceOop p = mgr->get_memory_manager_instance(CHECK_NULL); aoqi@0: instanceHandle ph(THREAD, p); aoqi@0: mgrArray->obj_at_put(i, ph()); aoqi@0: } aoqi@0: } else { aoqi@0: // Get memory managers for a given memory pool aoqi@0: for (int i = 0; i < num_mgrs; i++) { aoqi@0: MemoryManager* mgr = pool->get_memory_manager(i); aoqi@0: instanceOop p = mgr->get_memory_manager_instance(CHECK_NULL); aoqi@0: instanceHandle ph(THREAD, p); aoqi@0: mgrArray->obj_at_put(i, ph()); aoqi@0: } aoqi@0: } aoqi@0: return (jobjectArray) JNIHandles::make_local(env, mgrArray()); aoqi@0: JVM_END aoqi@0: aoqi@0: aoqi@0: // Returns a java/lang/management/MemoryUsage object containing the memory usage aoqi@0: // of a given memory pool. aoqi@0: JVM_ENTRY(jobject, jmm_GetMemoryPoolUsage(JNIEnv* env, jobject obj)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: MemoryPool* pool = get_memory_pool_from_jobject(obj, CHECK_NULL); aoqi@0: if (pool != NULL) { aoqi@0: MemoryUsage usage = pool->get_memory_usage(); aoqi@0: Handle h = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL); aoqi@0: return JNIHandles::make_local(env, h()); aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: JVM_END aoqi@0: aoqi@0: // Returns a java/lang/management/MemoryUsage object containing the memory usage aoqi@0: // of a given memory pool. aoqi@0: JVM_ENTRY(jobject, jmm_GetPeakMemoryPoolUsage(JNIEnv* env, jobject obj)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: MemoryPool* pool = get_memory_pool_from_jobject(obj, CHECK_NULL); aoqi@0: if (pool != NULL) { aoqi@0: MemoryUsage usage = pool->get_peak_memory_usage(); aoqi@0: Handle h = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL); aoqi@0: return JNIHandles::make_local(env, h()); aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: JVM_END aoqi@0: aoqi@0: // Returns a java/lang/management/MemoryUsage object containing the memory usage aoqi@0: // of a given memory pool after most recent GC. aoqi@0: JVM_ENTRY(jobject, jmm_GetPoolCollectionUsage(JNIEnv* env, jobject obj)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: MemoryPool* pool = get_memory_pool_from_jobject(obj, CHECK_NULL); aoqi@0: if (pool != NULL && pool->is_collected_pool()) { aoqi@0: MemoryUsage usage = pool->get_last_collection_usage(); aoqi@0: Handle h = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL); aoqi@0: return JNIHandles::make_local(env, h()); aoqi@0: } else { aoqi@0: return NULL; aoqi@0: } aoqi@0: JVM_END aoqi@0: aoqi@0: // Sets the memory pool sensor for a threshold type aoqi@0: JVM_ENTRY(void, jmm_SetPoolSensor(JNIEnv* env, jobject obj, jmmThresholdType type, jobject sensorObj)) aoqi@0: if (obj == NULL || sensorObj == NULL) { aoqi@0: THROW(vmSymbols::java_lang_NullPointerException()); aoqi@0: } aoqi@0: aoqi@0: Klass* sensor_klass = Management::sun_management_Sensor_klass(CHECK); aoqi@0: oop s = JNIHandles::resolve(sensorObj); aoqi@0: assert(s->is_instance(), "Sensor should be an instanceOop"); aoqi@0: instanceHandle sensor_h(THREAD, (instanceOop) s); aoqi@0: if (!sensor_h->is_a(sensor_klass)) { aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Sensor is not an instance of sun.management.Sensor class"); aoqi@0: } aoqi@0: aoqi@0: MemoryPool* mpool = get_memory_pool_from_jobject(obj, CHECK); aoqi@0: assert(mpool != NULL, "MemoryPool should exist"); aoqi@0: aoqi@0: switch (type) { aoqi@0: case JMM_USAGE_THRESHOLD_HIGH: aoqi@0: case JMM_USAGE_THRESHOLD_LOW: aoqi@0: // have only one sensor for threshold high and low aoqi@0: mpool->set_usage_sensor_obj(sensor_h); aoqi@0: break; aoqi@0: case JMM_COLLECTION_USAGE_THRESHOLD_HIGH: aoqi@0: case JMM_COLLECTION_USAGE_THRESHOLD_LOW: aoqi@0: // have only one sensor for threshold high and low aoqi@0: mpool->set_gc_usage_sensor_obj(sensor_h); aoqi@0: break; aoqi@0: default: aoqi@0: assert(false, "Unrecognized type"); aoqi@0: } aoqi@0: aoqi@0: JVM_END aoqi@0: aoqi@0: aoqi@0: // Sets the threshold of a given memory pool. aoqi@0: // Returns the previous threshold. aoqi@0: // aoqi@0: // Input parameters: aoqi@0: // pool - the MemoryPoolMXBean object aoqi@0: // type - threshold type aoqi@0: // threshold - the new threshold (must not be negative) aoqi@0: // aoqi@0: JVM_ENTRY(jlong, jmm_SetPoolThreshold(JNIEnv* env, jobject obj, jmmThresholdType type, jlong threshold)) aoqi@0: if (threshold < 0) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Invalid threshold value", aoqi@0: -1); aoqi@0: } aoqi@0: aoqi@0: if ((size_t)threshold > max_uintx) { aoqi@0: stringStream st; aoqi@0: st.print("Invalid valid threshold value. Threshold value (" UINT64_FORMAT ") > max value of size_t (" SIZE_FORMAT ")", (size_t)threshold, max_uintx); aoqi@0: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), st.as_string(), -1); aoqi@0: } aoqi@0: aoqi@0: MemoryPool* pool = get_memory_pool_from_jobject(obj, CHECK_(0L)); aoqi@0: assert(pool != NULL, "MemoryPool should exist"); aoqi@0: aoqi@0: jlong prev = 0; aoqi@0: switch (type) { aoqi@0: case JMM_USAGE_THRESHOLD_HIGH: aoqi@0: if (!pool->usage_threshold()->is_high_threshold_supported()) { aoqi@0: return -1; aoqi@0: } aoqi@0: prev = pool->usage_threshold()->set_high_threshold((size_t) threshold); aoqi@0: break; aoqi@0: aoqi@0: case JMM_USAGE_THRESHOLD_LOW: aoqi@0: if (!pool->usage_threshold()->is_low_threshold_supported()) { aoqi@0: return -1; aoqi@0: } aoqi@0: prev = pool->usage_threshold()->set_low_threshold((size_t) threshold); aoqi@0: break; aoqi@0: aoqi@0: case JMM_COLLECTION_USAGE_THRESHOLD_HIGH: aoqi@0: if (!pool->gc_usage_threshold()->is_high_threshold_supported()) { aoqi@0: return -1; aoqi@0: } aoqi@0: // return and the new threshold is effective for the next GC aoqi@0: return pool->gc_usage_threshold()->set_high_threshold((size_t) threshold); aoqi@0: aoqi@0: case JMM_COLLECTION_USAGE_THRESHOLD_LOW: aoqi@0: if (!pool->gc_usage_threshold()->is_low_threshold_supported()) { aoqi@0: return -1; aoqi@0: } aoqi@0: // return and the new threshold is effective for the next GC aoqi@0: return pool->gc_usage_threshold()->set_low_threshold((size_t) threshold); aoqi@0: aoqi@0: default: aoqi@0: assert(false, "Unrecognized type"); aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: // When the threshold is changed, reevaluate if the low memory aoqi@0: // detection is enabled. aoqi@0: if (prev != threshold) { aoqi@0: LowMemoryDetector::recompute_enabled_for_collected_pools(); aoqi@0: LowMemoryDetector::detect_low_memory(pool); aoqi@0: } aoqi@0: return prev; aoqi@0: JVM_END aoqi@0: aoqi@0: // Returns a java/lang/management/MemoryUsage object representing aoqi@0: // the memory usage for the heap or non-heap memory. aoqi@0: JVM_ENTRY(jobject, jmm_GetMemoryUsage(JNIEnv* env, jboolean heap)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: // Calculate the memory usage aoqi@0: size_t total_init = 0; aoqi@0: size_t total_used = 0; aoqi@0: size_t total_committed = 0; aoqi@0: size_t total_max = 0; aoqi@0: bool has_undefined_init_size = false; aoqi@0: bool has_undefined_max_size = false; aoqi@0: aoqi@0: for (int i = 0; i < MemoryService::num_memory_pools(); i++) { aoqi@0: MemoryPool* pool = MemoryService::get_memory_pool(i); aoqi@0: if ((heap && pool->is_heap()) || (!heap && pool->is_non_heap())) { aoqi@0: MemoryUsage u = pool->get_memory_usage(); aoqi@0: total_used += u.used(); aoqi@0: total_committed += u.committed(); aoqi@0: aoqi@0: if (u.init_size() == (size_t)-1) { aoqi@0: has_undefined_init_size = true; aoqi@0: } aoqi@0: if (!has_undefined_init_size) { aoqi@0: total_init += u.init_size(); aoqi@0: } aoqi@0: aoqi@0: if (u.max_size() == (size_t)-1) { aoqi@0: has_undefined_max_size = true; aoqi@0: } aoqi@0: if (!has_undefined_max_size) { aoqi@0: total_max += u.max_size(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // if any one of the memory pool has undefined init_size or max_size, aoqi@0: // set it to -1 aoqi@0: if (has_undefined_init_size) { aoqi@0: total_init = (size_t)-1; aoqi@0: } aoqi@0: if (has_undefined_max_size) { aoqi@0: total_max = (size_t)-1; aoqi@0: } aoqi@0: aoqi@0: MemoryUsage usage((heap ? InitialHeapSize : total_init), aoqi@0: total_used, aoqi@0: total_committed, aoqi@0: (heap ? Universe::heap()->max_capacity() : total_max)); aoqi@0: aoqi@0: Handle obj = MemoryService::create_MemoryUsage_obj(usage, CHECK_NULL); aoqi@0: return JNIHandles::make_local(env, obj()); aoqi@0: JVM_END aoqi@0: aoqi@0: // Returns the boolean value of a given attribute. aoqi@0: JVM_LEAF(jboolean, jmm_GetBoolAttribute(JNIEnv *env, jmmBoolAttribute att)) aoqi@0: switch (att) { aoqi@0: case JMM_VERBOSE_GC: aoqi@0: return MemoryService::get_verbose(); aoqi@0: case JMM_VERBOSE_CLASS: aoqi@0: return ClassLoadingService::get_verbose(); aoqi@0: case JMM_THREAD_CONTENTION_MONITORING: aoqi@0: return ThreadService::is_thread_monitoring_contention(); aoqi@0: case JMM_THREAD_CPU_TIME: aoqi@0: return ThreadService::is_thread_cpu_time_enabled(); aoqi@0: case JMM_THREAD_ALLOCATED_MEMORY: aoqi@0: return ThreadService::is_thread_allocated_memory_enabled(); aoqi@0: default: aoqi@0: assert(0, "Unrecognized attribute"); aoqi@0: return false; aoqi@0: } aoqi@0: JVM_END aoqi@0: aoqi@0: // Sets the given boolean attribute and returns the previous value. aoqi@0: JVM_ENTRY(jboolean, jmm_SetBoolAttribute(JNIEnv *env, jmmBoolAttribute att, jboolean flag)) aoqi@0: switch (att) { aoqi@0: case JMM_VERBOSE_GC: aoqi@0: return MemoryService::set_verbose(flag != 0); aoqi@0: case JMM_VERBOSE_CLASS: aoqi@0: return ClassLoadingService::set_verbose(flag != 0); aoqi@0: case JMM_THREAD_CONTENTION_MONITORING: aoqi@0: return ThreadService::set_thread_monitoring_contention(flag != 0); aoqi@0: case JMM_THREAD_CPU_TIME: aoqi@0: return ThreadService::set_thread_cpu_time_enabled(flag != 0); aoqi@0: case JMM_THREAD_ALLOCATED_MEMORY: aoqi@0: return ThreadService::set_thread_allocated_memory_enabled(flag != 0); aoqi@0: default: aoqi@0: assert(0, "Unrecognized attribute"); aoqi@0: return false; aoqi@0: } aoqi@0: JVM_END aoqi@0: aoqi@0: aoqi@0: static jlong get_gc_attribute(GCMemoryManager* mgr, jmmLongAttribute att) { aoqi@0: switch (att) { aoqi@0: case JMM_GC_TIME_MS: aoqi@0: return mgr->gc_time_ms(); aoqi@0: aoqi@0: case JMM_GC_COUNT: aoqi@0: return mgr->gc_count(); aoqi@0: aoqi@0: case JMM_GC_EXT_ATTRIBUTE_INFO_SIZE: aoqi@0: // current implementation only has 1 ext attribute aoqi@0: return 1; aoqi@0: aoqi@0: default: aoqi@0: assert(0, "Unrecognized GC attribute"); aoqi@0: return -1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: class VmThreadCountClosure: public ThreadClosure { aoqi@0: private: aoqi@0: int _count; aoqi@0: public: aoqi@0: VmThreadCountClosure() : _count(0) {}; aoqi@0: void do_thread(Thread* thread); aoqi@0: int count() { return _count; } aoqi@0: }; aoqi@0: aoqi@0: void VmThreadCountClosure::do_thread(Thread* thread) { aoqi@0: // exclude externally visible JavaThreads aoqi@0: if (thread->is_Java_thread() && !thread->is_hidden_from_external_view()) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: _count++; aoqi@0: } aoqi@0: aoqi@0: static jint get_vm_thread_count() { aoqi@0: VmThreadCountClosure vmtcc; aoqi@0: { aoqi@0: MutexLockerEx ml(Threads_lock); aoqi@0: Threads::threads_do(&vmtcc); aoqi@0: } aoqi@0: aoqi@0: return vmtcc.count(); aoqi@0: } aoqi@0: aoqi@0: static jint get_num_flags() { aoqi@0: // last flag entry is always NULL, so subtract 1 aoqi@0: int nFlags = (int) Flag::numFlags - 1; aoqi@0: int count = 0; aoqi@0: for (int i = 0; i < nFlags; i++) { aoqi@0: Flag* flag = &Flag::flags[i]; aoqi@0: // Exclude the locked (diagnostic, experimental) flags aoqi@0: if (flag->is_unlocked() || flag->is_unlocker()) { aoqi@0: count++; aoqi@0: } aoqi@0: } aoqi@0: return count; aoqi@0: } aoqi@0: aoqi@0: static jlong get_long_attribute(jmmLongAttribute att) { aoqi@0: switch (att) { aoqi@0: case JMM_CLASS_LOADED_COUNT: aoqi@0: return ClassLoadingService::loaded_class_count(); aoqi@0: aoqi@0: case JMM_CLASS_UNLOADED_COUNT: aoqi@0: return ClassLoadingService::unloaded_class_count(); aoqi@0: aoqi@0: case JMM_THREAD_TOTAL_COUNT: aoqi@0: return ThreadService::get_total_thread_count(); aoqi@0: aoqi@0: case JMM_THREAD_LIVE_COUNT: aoqi@0: return ThreadService::get_live_thread_count(); aoqi@0: aoqi@0: case JMM_THREAD_PEAK_COUNT: aoqi@0: return ThreadService::get_peak_thread_count(); aoqi@0: aoqi@0: case JMM_THREAD_DAEMON_COUNT: aoqi@0: return ThreadService::get_daemon_thread_count(); aoqi@0: aoqi@0: case JMM_JVM_INIT_DONE_TIME_MS: aoqi@0: return Management::vm_init_done_time(); aoqi@0: aoqi@0: case JMM_JVM_UPTIME_MS: aoqi@0: return Management::ticks_to_ms(os::elapsed_counter()); aoqi@0: aoqi@0: case JMM_COMPILE_TOTAL_TIME_MS: aoqi@0: return Management::ticks_to_ms(CompileBroker::total_compilation_ticks()); aoqi@0: aoqi@0: case JMM_OS_PROCESS_ID: aoqi@0: return os::current_process_id(); aoqi@0: aoqi@0: // Hotspot-specific counters aoqi@0: case JMM_CLASS_LOADED_BYTES: aoqi@0: return ClassLoadingService::loaded_class_bytes(); aoqi@0: aoqi@0: case JMM_CLASS_UNLOADED_BYTES: aoqi@0: return ClassLoadingService::unloaded_class_bytes(); aoqi@0: aoqi@0: case JMM_SHARED_CLASS_LOADED_COUNT: aoqi@0: return ClassLoadingService::loaded_shared_class_count(); aoqi@0: aoqi@0: case JMM_SHARED_CLASS_UNLOADED_COUNT: aoqi@0: return ClassLoadingService::unloaded_shared_class_count(); aoqi@0: aoqi@0: aoqi@0: case JMM_SHARED_CLASS_LOADED_BYTES: aoqi@0: return ClassLoadingService::loaded_shared_class_bytes(); aoqi@0: aoqi@0: case JMM_SHARED_CLASS_UNLOADED_BYTES: aoqi@0: return ClassLoadingService::unloaded_shared_class_bytes(); aoqi@0: aoqi@0: case JMM_TOTAL_CLASSLOAD_TIME_MS: aoqi@0: return ClassLoader::classloader_time_ms(); aoqi@0: aoqi@0: case JMM_VM_GLOBAL_COUNT: aoqi@0: return get_num_flags(); aoqi@0: aoqi@0: case JMM_SAFEPOINT_COUNT: aoqi@0: return RuntimeService::safepoint_count(); aoqi@0: aoqi@0: case JMM_TOTAL_SAFEPOINTSYNC_TIME_MS: aoqi@0: return RuntimeService::safepoint_sync_time_ms(); aoqi@0: aoqi@0: case JMM_TOTAL_STOPPED_TIME_MS: aoqi@0: return RuntimeService::safepoint_time_ms(); aoqi@0: aoqi@0: case JMM_TOTAL_APP_TIME_MS: aoqi@0: return RuntimeService::application_time_ms(); aoqi@0: aoqi@0: case JMM_VM_THREAD_COUNT: aoqi@0: return get_vm_thread_count(); aoqi@0: aoqi@0: case JMM_CLASS_INIT_TOTAL_COUNT: aoqi@0: return ClassLoader::class_init_count(); aoqi@0: aoqi@0: case JMM_CLASS_INIT_TOTAL_TIME_MS: aoqi@0: return ClassLoader::class_init_time_ms(); aoqi@0: aoqi@0: case JMM_CLASS_VERIFY_TOTAL_TIME_MS: aoqi@0: return ClassLoader::class_verify_time_ms(); aoqi@0: aoqi@0: case JMM_METHOD_DATA_SIZE_BYTES: aoqi@0: return ClassLoadingService::class_method_data_size(); aoqi@0: aoqi@0: case JMM_OS_MEM_TOTAL_PHYSICAL_BYTES: aoqi@0: return os::physical_memory(); aoqi@0: aoqi@0: default: aoqi@0: return -1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Returns the long value of a given attribute. aoqi@0: JVM_ENTRY(jlong, jmm_GetLongAttribute(JNIEnv *env, jobject obj, jmmLongAttribute att)) aoqi@0: if (obj == NULL) { aoqi@0: return get_long_attribute(att); aoqi@0: } else { aoqi@0: GCMemoryManager* mgr = get_gc_memory_manager_from_jobject(obj, CHECK_(0L)); aoqi@0: if (mgr != NULL) { aoqi@0: return get_gc_attribute(mgr, att); aoqi@0: } aoqi@0: } aoqi@0: return -1; aoqi@0: JVM_END aoqi@0: aoqi@0: // Gets the value of all attributes specified in the given array aoqi@0: // and sets the value in the result array. aoqi@0: // Returns the number of attributes found. aoqi@0: JVM_ENTRY(jint, jmm_GetLongAttributes(JNIEnv *env, aoqi@0: jobject obj, aoqi@0: jmmLongAttribute* atts, aoqi@0: jint count, aoqi@0: jlong* result)) aoqi@0: aoqi@0: int num_atts = 0; aoqi@0: if (obj == NULL) { aoqi@0: for (int i = 0; i < count; i++) { aoqi@0: result[i] = get_long_attribute(atts[i]); aoqi@0: if (result[i] != -1) { aoqi@0: num_atts++; aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: GCMemoryManager* mgr = get_gc_memory_manager_from_jobject(obj, CHECK_0); aoqi@0: for (int i = 0; i < count; i++) { aoqi@0: result[i] = get_gc_attribute(mgr, atts[i]); aoqi@0: if (result[i] != -1) { aoqi@0: num_atts++; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return num_atts; aoqi@0: JVM_END aoqi@0: aoqi@0: // Helper function to do thread dump for a specific list of threads aoqi@0: static void do_thread_dump(ThreadDumpResult* dump_result, aoqi@0: typeArrayHandle ids_ah, // array of thread ID (long[]) aoqi@0: int num_threads, aoqi@0: int max_depth, aoqi@0: bool with_locked_monitors, aoqi@0: bool with_locked_synchronizers, aoqi@0: TRAPS) { aoqi@0: aoqi@0: // First get an array of threadObj handles. aoqi@0: // A JavaThread may terminate before we get the stack trace. aoqi@0: GrowableArray* thread_handle_array = new GrowableArray(num_threads); aoqi@0: { aoqi@0: MutexLockerEx ml(Threads_lock); aoqi@0: for (int i = 0; i < num_threads; i++) { aoqi@0: jlong tid = ids_ah->long_at(i); sla@7107: JavaThread* jt = Threads::find_java_thread_from_java_tid(tid); aoqi@0: oop thread_obj = (jt != NULL ? jt->threadObj() : (oop)NULL); aoqi@0: instanceHandle threadObj_h(THREAD, (instanceOop) thread_obj); aoqi@0: thread_handle_array->append(threadObj_h); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Obtain thread dumps and thread snapshot information aoqi@0: VM_ThreadDump op(dump_result, aoqi@0: thread_handle_array, aoqi@0: num_threads, aoqi@0: max_depth, /* stack depth */ aoqi@0: with_locked_monitors, aoqi@0: with_locked_synchronizers); aoqi@0: VMThread::execute(&op); aoqi@0: } aoqi@0: aoqi@0: // Gets an array of ThreadInfo objects. Each element is the ThreadInfo aoqi@0: // for the thread ID specified in the corresponding entry in aoqi@0: // the given array of thread IDs; or NULL if the thread does not exist aoqi@0: // or has terminated. aoqi@0: // aoqi@0: // Input parameters: aoqi@0: // ids - array of thread IDs aoqi@0: // maxDepth - the maximum depth of stack traces to be dumped: aoqi@0: // maxDepth == -1 requests to dump entire stack trace. aoqi@0: // maxDepth == 0 requests no stack trace. aoqi@0: // infoArray - array of ThreadInfo objects aoqi@0: // aoqi@0: // QQQ - Why does this method return a value instead of void? aoqi@0: JVM_ENTRY(jint, jmm_GetThreadInfo(JNIEnv *env, jlongArray ids, jint maxDepth, jobjectArray infoArray)) aoqi@0: // Check if threads is null aoqi@0: if (ids == NULL || infoArray == NULL) { aoqi@0: THROW_(vmSymbols::java_lang_NullPointerException(), -1); aoqi@0: } aoqi@0: aoqi@0: if (maxDepth < -1) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Invalid maxDepth", -1); aoqi@0: } aoqi@0: aoqi@0: ResourceMark rm(THREAD); aoqi@0: typeArrayOop ta = typeArrayOop(JNIHandles::resolve_non_null(ids)); aoqi@0: typeArrayHandle ids_ah(THREAD, ta); aoqi@0: aoqi@0: oop infoArray_obj = JNIHandles::resolve_non_null(infoArray); aoqi@0: objArrayOop oa = objArrayOop(infoArray_obj); aoqi@0: objArrayHandle infoArray_h(THREAD, oa); aoqi@0: aoqi@0: // validate the thread id array aoqi@0: validate_thread_id_array(ids_ah, CHECK_0); aoqi@0: aoqi@0: // validate the ThreadInfo[] parameters aoqi@0: validate_thread_info_array(infoArray_h, CHECK_0); aoqi@0: aoqi@0: // infoArray must be of the same length as the given array of thread IDs aoqi@0: int num_threads = ids_ah->length(); aoqi@0: if (num_threads != infoArray_h->length()) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "The length of the given ThreadInfo array does not match the length of the given array of thread IDs", -1); aoqi@0: } aoqi@0: aoqi@0: if (JDK_Version::is_gte_jdk16x_version()) { aoqi@0: // make sure the AbstractOwnableSynchronizer klass is loaded before taking thread snapshots aoqi@0: java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(CHECK_0); aoqi@0: } aoqi@0: aoqi@0: // Must use ThreadDumpResult to store the ThreadSnapshot. aoqi@0: // GC may occur after the thread snapshots are taken but before aoqi@0: // this function returns. The threadObj and other oops kept aoqi@0: // in the ThreadSnapshot are marked and adjusted during GC. aoqi@0: ThreadDumpResult dump_result(num_threads); aoqi@0: aoqi@0: if (maxDepth == 0) { aoqi@0: // no stack trace dumped - do not need to stop the world aoqi@0: { aoqi@0: MutexLockerEx ml(Threads_lock); aoqi@0: for (int i = 0; i < num_threads; i++) { aoqi@0: jlong tid = ids_ah->long_at(i); sla@7107: JavaThread* jt = Threads::find_java_thread_from_java_tid(tid); aoqi@0: ThreadSnapshot* ts; aoqi@0: if (jt == NULL) { aoqi@0: // if the thread does not exist or now it is terminated, aoqi@0: // create dummy snapshot aoqi@0: ts = new ThreadSnapshot(); aoqi@0: } else { aoqi@0: ts = new ThreadSnapshot(jt); aoqi@0: } aoqi@0: dump_result.add_thread_snapshot(ts); aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: // obtain thread dump with the specific list of threads with stack trace aoqi@0: do_thread_dump(&dump_result, aoqi@0: ids_ah, aoqi@0: num_threads, aoqi@0: maxDepth, aoqi@0: false, /* no locked monitor */ aoqi@0: false, /* no locked synchronizers */ aoqi@0: CHECK_0); aoqi@0: } aoqi@0: aoqi@0: int num_snapshots = dump_result.num_snapshots(); aoqi@0: assert(num_snapshots == num_threads, "Must match the number of thread snapshots"); aoqi@0: int index = 0; aoqi@0: for (ThreadSnapshot* ts = dump_result.snapshots(); ts != NULL; index++, ts = ts->next()) { aoqi@0: // For each thread, create an java/lang/management/ThreadInfo object aoqi@0: // and fill with the thread information aoqi@0: aoqi@0: if (ts->threadObj() == NULL) { aoqi@0: // if the thread does not exist or now it is terminated, set threadinfo to NULL aoqi@0: infoArray_h->obj_at_put(index, NULL); aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: // Create java.lang.management.ThreadInfo object aoqi@0: instanceOop info_obj = Management::create_thread_info_instance(ts, CHECK_0); aoqi@0: infoArray_h->obj_at_put(index, info_obj); aoqi@0: } aoqi@0: return 0; aoqi@0: JVM_END aoqi@0: aoqi@0: // Dump thread info for the specified threads. aoqi@0: // It returns an array of ThreadInfo objects. Each element is the ThreadInfo aoqi@0: // for the thread ID specified in the corresponding entry in aoqi@0: // the given array of thread IDs; or NULL if the thread does not exist aoqi@0: // or has terminated. aoqi@0: // aoqi@0: // Input parameter: aoqi@0: // ids - array of thread IDs; NULL indicates all live threads aoqi@0: // locked_monitors - if true, dump locked object monitors aoqi@0: // locked_synchronizers - if true, dump locked JSR-166 synchronizers aoqi@0: // aoqi@0: JVM_ENTRY(jobjectArray, jmm_DumpThreads(JNIEnv *env, jlongArray thread_ids, jboolean locked_monitors, jboolean locked_synchronizers)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: if (JDK_Version::is_gte_jdk16x_version()) { aoqi@0: // make sure the AbstractOwnableSynchronizer klass is loaded before taking thread snapshots aoqi@0: java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(CHECK_NULL); aoqi@0: } aoqi@0: aoqi@0: typeArrayOop ta = typeArrayOop(JNIHandles::resolve(thread_ids)); aoqi@0: int num_threads = (ta != NULL ? ta->length() : 0); aoqi@0: typeArrayHandle ids_ah(THREAD, ta); aoqi@0: aoqi@0: ThreadDumpResult dump_result(num_threads); // can safepoint aoqi@0: aoqi@0: if (ids_ah() != NULL) { aoqi@0: aoqi@0: // validate the thread id array aoqi@0: validate_thread_id_array(ids_ah, CHECK_NULL); aoqi@0: aoqi@0: // obtain thread dump of a specific list of threads aoqi@0: do_thread_dump(&dump_result, aoqi@0: ids_ah, aoqi@0: num_threads, aoqi@0: -1, /* entire stack */ aoqi@0: (locked_monitors ? true : false), /* with locked monitors */ aoqi@0: (locked_synchronizers ? true : false), /* with locked synchronizers */ aoqi@0: CHECK_NULL); aoqi@0: } else { aoqi@0: // obtain thread dump of all threads aoqi@0: VM_ThreadDump op(&dump_result, aoqi@0: -1, /* entire stack */ aoqi@0: (locked_monitors ? true : false), /* with locked monitors */ aoqi@0: (locked_synchronizers ? true : false) /* with locked synchronizers */); aoqi@0: VMThread::execute(&op); aoqi@0: } aoqi@0: aoqi@0: int num_snapshots = dump_result.num_snapshots(); aoqi@0: aoqi@0: // create the result ThreadInfo[] object aoqi@0: Klass* k = Management::java_lang_management_ThreadInfo_klass(CHECK_NULL); aoqi@0: instanceKlassHandle ik (THREAD, k); aoqi@0: objArrayOop r = oopFactory::new_objArray(ik(), num_snapshots, CHECK_NULL); aoqi@0: objArrayHandle result_h(THREAD, r); aoqi@0: aoqi@0: int index = 0; aoqi@0: for (ThreadSnapshot* ts = dump_result.snapshots(); ts != NULL; ts = ts->next(), index++) { aoqi@0: if (ts->threadObj() == NULL) { aoqi@0: // if the thread does not exist or now it is terminated, set threadinfo to NULL aoqi@0: result_h->obj_at_put(index, NULL); aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: ThreadStackTrace* stacktrace = ts->get_stack_trace(); aoqi@0: assert(stacktrace != NULL, "Must have a stack trace dumped"); aoqi@0: aoqi@0: // Create Object[] filled with locked monitors aoqi@0: // Create int[] filled with the stack depth where a monitor was locked aoqi@0: int num_frames = stacktrace->get_stack_depth(); aoqi@0: int num_locked_monitors = stacktrace->num_jni_locked_monitors(); aoqi@0: aoqi@0: // Count the total number of locked monitors aoqi@0: for (int i = 0; i < num_frames; i++) { aoqi@0: StackFrameInfo* frame = stacktrace->stack_frame_at(i); aoqi@0: num_locked_monitors += frame->num_locked_monitors(); aoqi@0: } aoqi@0: aoqi@0: objArrayHandle monitors_array; aoqi@0: typeArrayHandle depths_array; aoqi@0: objArrayHandle synchronizers_array; aoqi@0: aoqi@0: if (locked_monitors) { aoqi@0: // Constructs Object[] and int[] to contain the object monitor and the stack depth aoqi@0: // where the thread locked it aoqi@0: objArrayOop array = oopFactory::new_objArray(SystemDictionary::Object_klass(), num_locked_monitors, CHECK_NULL); aoqi@0: objArrayHandle mh(THREAD, array); aoqi@0: monitors_array = mh; aoqi@0: aoqi@0: typeArrayOop tarray = oopFactory::new_typeArray(T_INT, num_locked_monitors, CHECK_NULL); aoqi@0: typeArrayHandle dh(THREAD, tarray); aoqi@0: depths_array = dh; aoqi@0: aoqi@0: int count = 0; aoqi@0: int j = 0; aoqi@0: for (int depth = 0; depth < num_frames; depth++) { aoqi@0: StackFrameInfo* frame = stacktrace->stack_frame_at(depth); aoqi@0: int len = frame->num_locked_monitors(); aoqi@0: GrowableArray* locked_monitors = frame->locked_monitors(); aoqi@0: for (j = 0; j < len; j++) { aoqi@0: oop monitor = locked_monitors->at(j); aoqi@0: assert(monitor != NULL && monitor->is_instance(), "must be a Java object"); aoqi@0: monitors_array->obj_at_put(count, monitor); aoqi@0: depths_array->int_at_put(count, depth); aoqi@0: count++; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: GrowableArray* jni_locked_monitors = stacktrace->jni_locked_monitors(); aoqi@0: for (j = 0; j < jni_locked_monitors->length(); j++) { aoqi@0: oop object = jni_locked_monitors->at(j); aoqi@0: assert(object != NULL && object->is_instance(), "must be a Java object"); aoqi@0: monitors_array->obj_at_put(count, object); aoqi@0: // Monitor locked via JNI MonitorEnter call doesn't have stack depth info aoqi@0: depths_array->int_at_put(count, -1); aoqi@0: count++; aoqi@0: } aoqi@0: assert(count == num_locked_monitors, "number of locked monitors doesn't match"); aoqi@0: } aoqi@0: aoqi@0: if (locked_synchronizers) { aoqi@0: // Create Object[] filled with locked JSR-166 synchronizers aoqi@0: assert(ts->threadObj() != NULL, "Must be a valid JavaThread"); aoqi@0: ThreadConcurrentLocks* tcl = ts->get_concurrent_locks(); aoqi@0: GrowableArray* locks = (tcl != NULL ? tcl->owned_locks() : NULL); aoqi@0: int num_locked_synchronizers = (locks != NULL ? locks->length() : 0); aoqi@0: aoqi@0: objArrayOop array = oopFactory::new_objArray(SystemDictionary::Object_klass(), num_locked_synchronizers, CHECK_NULL); aoqi@0: objArrayHandle sh(THREAD, array); aoqi@0: synchronizers_array = sh; aoqi@0: aoqi@0: for (int k = 0; k < num_locked_synchronizers; k++) { aoqi@0: synchronizers_array->obj_at_put(k, locks->at(k)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Create java.lang.management.ThreadInfo object aoqi@0: instanceOop info_obj = Management::create_thread_info_instance(ts, aoqi@0: monitors_array, aoqi@0: depths_array, aoqi@0: synchronizers_array, aoqi@0: CHECK_NULL); aoqi@0: result_h->obj_at_put(index, info_obj); aoqi@0: } aoqi@0: aoqi@0: return (jobjectArray) JNIHandles::make_local(env, result_h()); aoqi@0: JVM_END aoqi@0: aoqi@0: // Returns an array of Class objects. aoqi@0: JVM_ENTRY(jobjectArray, jmm_GetLoadedClasses(JNIEnv *env)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: LoadedClassesEnumerator lce(THREAD); // Pass current Thread as parameter aoqi@0: aoqi@0: int num_classes = lce.num_loaded_classes(); aoqi@0: objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), num_classes, CHECK_0); aoqi@0: objArrayHandle classes_ah(THREAD, r); aoqi@0: aoqi@0: for (int i = 0; i < num_classes; i++) { aoqi@0: KlassHandle kh = lce.get_klass(i); aoqi@0: oop mirror = kh()->java_mirror(); aoqi@0: classes_ah->obj_at_put(i, mirror); aoqi@0: } aoqi@0: aoqi@0: return (jobjectArray) JNIHandles::make_local(env, classes_ah()); aoqi@0: JVM_END aoqi@0: aoqi@0: // Reset statistic. Return true if the requested statistic is reset. aoqi@0: // Otherwise, return false. aoqi@0: // aoqi@0: // Input parameters: aoqi@0: // obj - specify which instance the statistic associated with to be reset aoqi@0: // For PEAK_POOL_USAGE stat, obj is required to be a memory pool object. aoqi@0: // For THREAD_CONTENTION_COUNT and TIME stat, obj is required to be a thread ID. aoqi@0: // type - the type of statistic to be reset aoqi@0: // aoqi@0: JVM_ENTRY(jboolean, jmm_ResetStatistic(JNIEnv *env, jvalue obj, jmmStatisticType type)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: switch (type) { aoqi@0: case JMM_STAT_PEAK_THREAD_COUNT: aoqi@0: ThreadService::reset_peak_thread_count(); aoqi@0: return true; aoqi@0: aoqi@0: case JMM_STAT_THREAD_CONTENTION_COUNT: aoqi@0: case JMM_STAT_THREAD_CONTENTION_TIME: { aoqi@0: jlong tid = obj.j; aoqi@0: if (tid < 0) { aoqi@0: THROW_(vmSymbols::java_lang_IllegalArgumentException(), JNI_FALSE); aoqi@0: } aoqi@0: aoqi@0: // Look for the JavaThread of this given tid aoqi@0: MutexLockerEx ml(Threads_lock); aoqi@0: if (tid == 0) { aoqi@0: // reset contention statistics for all threads if tid == 0 aoqi@0: for (JavaThread* java_thread = Threads::first(); java_thread != NULL; java_thread = java_thread->next()) { aoqi@0: if (type == JMM_STAT_THREAD_CONTENTION_COUNT) { aoqi@0: ThreadService::reset_contention_count_stat(java_thread); aoqi@0: } else { aoqi@0: ThreadService::reset_contention_time_stat(java_thread); aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: // reset contention statistics for a given thread sla@7107: JavaThread* java_thread = Threads::find_java_thread_from_java_tid(tid); aoqi@0: if (java_thread == NULL) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: if (type == JMM_STAT_THREAD_CONTENTION_COUNT) { aoqi@0: ThreadService::reset_contention_count_stat(java_thread); aoqi@0: } else { aoqi@0: ThreadService::reset_contention_time_stat(java_thread); aoqi@0: } aoqi@0: } aoqi@0: return true; aoqi@0: break; aoqi@0: } aoqi@0: case JMM_STAT_PEAK_POOL_USAGE: { aoqi@0: jobject o = obj.l; aoqi@0: if (o == NULL) { aoqi@0: THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE); aoqi@0: } aoqi@0: aoqi@0: oop pool_obj = JNIHandles::resolve(o); aoqi@0: assert(pool_obj->is_instance(), "Should be an instanceOop"); aoqi@0: instanceHandle ph(THREAD, (instanceOop) pool_obj); aoqi@0: aoqi@0: MemoryPool* pool = MemoryService::get_memory_pool(ph); aoqi@0: if (pool != NULL) { aoqi@0: pool->reset_peak_memory_usage(); aoqi@0: return true; aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: case JMM_STAT_GC_STAT: { aoqi@0: jobject o = obj.l; aoqi@0: if (o == NULL) { aoqi@0: THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE); aoqi@0: } aoqi@0: aoqi@0: GCMemoryManager* mgr = get_gc_memory_manager_from_jobject(o, CHECK_0); aoqi@0: if (mgr != NULL) { aoqi@0: mgr->reset_gc_stat(); aoqi@0: return true; aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: default: aoqi@0: assert(0, "Unknown Statistic Type"); aoqi@0: } aoqi@0: return false; aoqi@0: JVM_END aoqi@0: aoqi@0: // Returns the fast estimate of CPU time consumed by aoqi@0: // a given thread (in nanoseconds). aoqi@0: // If thread_id == 0, return CPU time for the current thread. aoqi@0: JVM_ENTRY(jlong, jmm_GetThreadCpuTime(JNIEnv *env, jlong thread_id)) aoqi@0: if (!os::is_thread_cpu_time_supported()) { aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: if (thread_id < 0) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Invalid thread ID", -1); aoqi@0: } aoqi@0: aoqi@0: JavaThread* java_thread = NULL; aoqi@0: if (thread_id == 0) { aoqi@0: // current thread aoqi@0: return os::current_thread_cpu_time(); aoqi@0: } else { aoqi@0: MutexLockerEx ml(Threads_lock); sla@7107: java_thread = Threads::find_java_thread_from_java_tid(thread_id); aoqi@0: if (java_thread != NULL) { aoqi@0: return os::thread_cpu_time((Thread*) java_thread); aoqi@0: } aoqi@0: } aoqi@0: return -1; aoqi@0: JVM_END aoqi@0: aoqi@0: // Returns a String array of all VM global flag names aoqi@0: JVM_ENTRY(jobjectArray, jmm_GetVMGlobalNames(JNIEnv *env)) aoqi@0: // last flag entry is always NULL, so subtract 1 aoqi@0: int nFlags = (int) Flag::numFlags - 1; aoqi@0: // allocate a temp array aoqi@0: objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), aoqi@0: nFlags, CHECK_0); aoqi@0: objArrayHandle flags_ah(THREAD, r); aoqi@0: int num_entries = 0; aoqi@0: for (int i = 0; i < nFlags; i++) { aoqi@0: Flag* flag = &Flag::flags[i]; aoqi@0: // Exclude notproduct and develop flags in product builds. aoqi@0: if (flag->is_constant_in_binary()) { aoqi@0: continue; aoqi@0: } aoqi@0: // Exclude the locked (experimental, diagnostic) flags aoqi@0: if (flag->is_unlocked() || flag->is_unlocker()) { aoqi@0: Handle s = java_lang_String::create_from_str(flag->_name, CHECK_0); aoqi@0: flags_ah->obj_at_put(num_entries, s()); aoqi@0: num_entries++; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (num_entries < nFlags) { aoqi@0: // Return array of right length aoqi@0: objArrayOop res = oopFactory::new_objArray(SystemDictionary::String_klass(), num_entries, CHECK_0); aoqi@0: for(int i = 0; i < num_entries; i++) { aoqi@0: res->obj_at_put(i, flags_ah->obj_at(i)); aoqi@0: } aoqi@0: return (jobjectArray)JNIHandles::make_local(env, res); aoqi@0: } aoqi@0: aoqi@0: return (jobjectArray)JNIHandles::make_local(env, flags_ah()); aoqi@0: JVM_END aoqi@0: aoqi@0: // Utility function used by jmm_GetVMGlobals. Returns false if flag type aoqi@0: // can't be determined, true otherwise. If false is returned, then *global aoqi@0: // will be incomplete and invalid. aoqi@0: bool add_global_entry(JNIEnv* env, Handle name, jmmVMGlobal *global, Flag *flag, TRAPS) { aoqi@0: Handle flag_name; aoqi@0: if (name() == NULL) { aoqi@0: flag_name = java_lang_String::create_from_str(flag->_name, CHECK_false); aoqi@0: } else { aoqi@0: flag_name = name; aoqi@0: } aoqi@0: global->name = (jstring)JNIHandles::make_local(env, flag_name()); aoqi@0: aoqi@0: if (flag->is_bool()) { aoqi@0: global->value.z = flag->get_bool() ? JNI_TRUE : JNI_FALSE; aoqi@0: global->type = JMM_VMGLOBAL_TYPE_JBOOLEAN; aoqi@0: } else if (flag->is_intx()) { aoqi@0: global->value.j = (jlong)flag->get_intx(); aoqi@0: global->type = JMM_VMGLOBAL_TYPE_JLONG; aoqi@0: } else if (flag->is_uintx()) { aoqi@0: global->value.j = (jlong)flag->get_uintx(); aoqi@0: global->type = JMM_VMGLOBAL_TYPE_JLONG; aoqi@0: } else if (flag->is_uint64_t()) { aoqi@0: global->value.j = (jlong)flag->get_uint64_t(); aoqi@0: global->type = JMM_VMGLOBAL_TYPE_JLONG; aoqi@0: } else if (flag->is_ccstr()) { aoqi@0: Handle str = java_lang_String::create_from_str(flag->get_ccstr(), CHECK_false); aoqi@0: global->value.l = (jobject)JNIHandles::make_local(env, str()); aoqi@0: global->type = JMM_VMGLOBAL_TYPE_JSTRING; aoqi@0: } else { aoqi@0: global->type = JMM_VMGLOBAL_TYPE_UNKNOWN; aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: global->writeable = flag->is_writeable(); aoqi@0: global->external = flag->is_external(); aoqi@0: switch (flag->get_origin()) { aoqi@0: case Flag::DEFAULT: aoqi@0: global->origin = JMM_VMGLOBAL_ORIGIN_DEFAULT; aoqi@0: break; aoqi@0: case Flag::COMMAND_LINE: aoqi@0: global->origin = JMM_VMGLOBAL_ORIGIN_COMMAND_LINE; aoqi@0: break; aoqi@0: case Flag::ENVIRON_VAR: aoqi@0: global->origin = JMM_VMGLOBAL_ORIGIN_ENVIRON_VAR; aoqi@0: break; aoqi@0: case Flag::CONFIG_FILE: aoqi@0: global->origin = JMM_VMGLOBAL_ORIGIN_CONFIG_FILE; aoqi@0: break; aoqi@0: case Flag::MANAGEMENT: aoqi@0: global->origin = JMM_VMGLOBAL_ORIGIN_MANAGEMENT; aoqi@0: break; aoqi@0: case Flag::ERGONOMIC: aoqi@0: global->origin = JMM_VMGLOBAL_ORIGIN_ERGONOMIC; aoqi@0: break; aoqi@0: default: aoqi@0: global->origin = JMM_VMGLOBAL_ORIGIN_OTHER; aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: // Fill globals array of count length with jmmVMGlobal entries aoqi@0: // specified by names. If names == NULL, fill globals array aoqi@0: // with all Flags. Return value is number of entries aoqi@0: // created in globals. aoqi@0: // If a Flag with a given name in an array element does not aoqi@0: // exist, globals[i].name will be set to NULL. aoqi@0: JVM_ENTRY(jint, jmm_GetVMGlobals(JNIEnv *env, aoqi@0: jobjectArray names, aoqi@0: jmmVMGlobal *globals, aoqi@0: jint count)) aoqi@0: aoqi@0: aoqi@0: if (globals == NULL) { aoqi@0: THROW_(vmSymbols::java_lang_NullPointerException(), 0); aoqi@0: } aoqi@0: aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: if (names != NULL) { aoqi@0: // return the requested globals aoqi@0: objArrayOop ta = objArrayOop(JNIHandles::resolve_non_null(names)); aoqi@0: objArrayHandle names_ah(THREAD, ta); aoqi@0: // Make sure we have a String array aoqi@0: Klass* element_klass = ObjArrayKlass::cast(names_ah->klass())->element_klass(); aoqi@0: if (element_klass != SystemDictionary::String_klass()) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Array element type is not String class", 0); aoqi@0: } aoqi@0: aoqi@0: int names_length = names_ah->length(); aoqi@0: int num_entries = 0; aoqi@0: for (int i = 0; i < names_length && i < count; i++) { aoqi@0: oop s = names_ah->obj_at(i); aoqi@0: if (s == NULL) { aoqi@0: THROW_(vmSymbols::java_lang_NullPointerException(), 0); aoqi@0: } aoqi@0: aoqi@0: Handle sh(THREAD, s); aoqi@0: char* str = java_lang_String::as_utf8_string(s); aoqi@0: Flag* flag = Flag::find_flag(str, strlen(str)); aoqi@0: if (flag != NULL && aoqi@0: add_global_entry(env, sh, &globals[i], flag, THREAD)) { aoqi@0: num_entries++; aoqi@0: } else { aoqi@0: globals[i].name = NULL; aoqi@0: } aoqi@0: } aoqi@0: return num_entries; aoqi@0: } else { aoqi@0: // return all globals if names == NULL aoqi@0: aoqi@0: // last flag entry is always NULL, so subtract 1 aoqi@0: int nFlags = (int) Flag::numFlags - 1; aoqi@0: Handle null_h; aoqi@0: int num_entries = 0; aoqi@0: for (int i = 0; i < nFlags && num_entries < count; i++) { aoqi@0: Flag* flag = &Flag::flags[i]; aoqi@0: // Exclude notproduct and develop flags in product builds. aoqi@0: if (flag->is_constant_in_binary()) { aoqi@0: continue; aoqi@0: } aoqi@0: // Exclude the locked (diagnostic, experimental) flags aoqi@0: if ((flag->is_unlocked() || flag->is_unlocker()) && aoqi@0: add_global_entry(env, null_h, &globals[num_entries], flag, THREAD)) { aoqi@0: num_entries++; aoqi@0: } aoqi@0: } aoqi@0: return num_entries; aoqi@0: } aoqi@0: JVM_END aoqi@0: aoqi@0: JVM_ENTRY(void, jmm_SetVMGlobal(JNIEnv *env, jstring flag_name, jvalue new_value)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: oop fn = JNIHandles::resolve_external_guard(flag_name); aoqi@0: if (fn == NULL) { aoqi@0: THROW_MSG(vmSymbols::java_lang_NullPointerException(), aoqi@0: "The flag name cannot be null."); aoqi@0: } aoqi@0: char* name = java_lang_String::as_utf8_string(fn); aoqi@0: Flag* flag = Flag::find_flag(name, strlen(name)); aoqi@0: if (flag == NULL) { aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Flag does not exist."); aoqi@0: } aoqi@0: if (!flag->is_writeable()) { aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "This flag is not writeable."); aoqi@0: } aoqi@0: iignatyev@6962: bool succeed = false; aoqi@0: if (flag->is_bool()) { aoqi@0: bool bvalue = (new_value.z == JNI_TRUE ? true : false); aoqi@0: succeed = CommandLineFlags::boolAtPut(name, &bvalue, Flag::MANAGEMENT); aoqi@0: } else if (flag->is_intx()) { aoqi@0: intx ivalue = (intx)new_value.j; aoqi@0: succeed = CommandLineFlags::intxAtPut(name, &ivalue, Flag::MANAGEMENT); aoqi@0: } else if (flag->is_uintx()) { aoqi@0: uintx uvalue = (uintx)new_value.j; aoqi@0: aoqi@0: if (strncmp(name, "MaxHeapFreeRatio", 17) == 0) { aoqi@0: FormatBuffer<80> err_msg("%s", ""); aoqi@0: if (!Arguments::verify_MaxHeapFreeRatio(err_msg, uvalue)) { aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), err_msg.buffer()); aoqi@0: } aoqi@0: } else if (strncmp(name, "MinHeapFreeRatio", 17) == 0) { aoqi@0: FormatBuffer<80> err_msg("%s", ""); aoqi@0: if (!Arguments::verify_MinHeapFreeRatio(err_msg, uvalue)) { aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), err_msg.buffer()); aoqi@0: } aoqi@0: } aoqi@0: succeed = CommandLineFlags::uintxAtPut(name, &uvalue, Flag::MANAGEMENT); aoqi@0: } else if (flag->is_uint64_t()) { aoqi@0: uint64_t uvalue = (uint64_t)new_value.j; aoqi@0: succeed = CommandLineFlags::uint64_tAtPut(name, &uvalue, Flag::MANAGEMENT); aoqi@0: } else if (flag->is_ccstr()) { aoqi@0: oop str = JNIHandles::resolve_external_guard(new_value.l); aoqi@0: if (str == NULL) { aoqi@0: THROW(vmSymbols::java_lang_NullPointerException()); aoqi@0: } aoqi@0: ccstr svalue = java_lang_String::as_utf8_string(str); aoqi@0: succeed = CommandLineFlags::ccstrAtPut(name, &svalue, Flag::MANAGEMENT); iignatyev@6962: if (succeed) { iignatyev@6962: FREE_C_HEAP_ARRAY(char, svalue, mtInternal); iignatyev@6962: } aoqi@0: } aoqi@0: assert(succeed, "Setting flag should succeed"); aoqi@0: JVM_END aoqi@0: aoqi@0: class ThreadTimesClosure: public ThreadClosure { aoqi@0: private: aoqi@0: objArrayHandle _names_strings; aoqi@0: char **_names_chars; aoqi@0: typeArrayHandle _times; aoqi@0: int _names_len; aoqi@0: int _times_len; aoqi@0: int _count; aoqi@0: aoqi@0: public: aoqi@0: ThreadTimesClosure(objArrayHandle names, typeArrayHandle times); aoqi@0: ~ThreadTimesClosure(); aoqi@0: virtual void do_thread(Thread* thread); aoqi@0: void do_unlocked(); aoqi@0: int count() { return _count; } aoqi@0: }; aoqi@0: aoqi@0: ThreadTimesClosure::ThreadTimesClosure(objArrayHandle names, aoqi@0: typeArrayHandle times) { aoqi@0: assert(names() != NULL, "names was NULL"); aoqi@0: assert(times() != NULL, "times was NULL"); aoqi@0: _names_strings = names; aoqi@0: _names_len = names->length(); aoqi@0: _names_chars = NEW_C_HEAP_ARRAY(char*, _names_len, mtInternal); aoqi@0: _times = times; aoqi@0: _times_len = times->length(); aoqi@0: _count = 0; aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // Called with Threads_lock held aoqi@0: // aoqi@0: void ThreadTimesClosure::do_thread(Thread* thread) { aoqi@0: assert(thread != NULL, "thread was NULL"); aoqi@0: aoqi@0: // exclude externally visible JavaThreads aoqi@0: if (thread->is_Java_thread() && !thread->is_hidden_from_external_view()) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (_count >= _names_len || _count >= _times_len) { aoqi@0: // skip if the result array is not big enough aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: EXCEPTION_MARK; aoqi@0: ResourceMark rm(THREAD); // thread->name() uses ResourceArea aoqi@0: aoqi@0: assert(thread->name() != NULL, "All threads should have a name"); aoqi@0: _names_chars[_count] = strdup(thread->name()); aoqi@0: _times->long_at_put(_count, os::is_thread_cpu_time_supported() ? aoqi@0: os::thread_cpu_time(thread) : -1); aoqi@0: _count++; aoqi@0: } aoqi@0: aoqi@0: // Called without Threads_lock, we can allocate String objects. aoqi@0: void ThreadTimesClosure::do_unlocked() { aoqi@0: aoqi@0: EXCEPTION_MARK; aoqi@0: for (int i = 0; i < _count; i++) { aoqi@0: Handle s = java_lang_String::create_from_str(_names_chars[i], CHECK); aoqi@0: _names_strings->obj_at_put(i, s()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ThreadTimesClosure::~ThreadTimesClosure() { aoqi@0: for (int i = 0; i < _count; i++) { aoqi@0: free(_names_chars[i]); aoqi@0: } aoqi@0: FREE_C_HEAP_ARRAY(char *, _names_chars, mtInternal); aoqi@0: } aoqi@0: aoqi@0: // Fills names with VM internal thread names and times with the corresponding aoqi@0: // CPU times. If names or times is NULL, a NullPointerException is thrown. aoqi@0: // If the element type of names is not String, an IllegalArgumentException is aoqi@0: // thrown. aoqi@0: // If an array is not large enough to hold all the entries, only the entries aoqi@0: // that fit will be returned. Return value is the number of VM internal aoqi@0: // threads entries. aoqi@0: JVM_ENTRY(jint, jmm_GetInternalThreadTimes(JNIEnv *env, aoqi@0: jobjectArray names, aoqi@0: jlongArray times)) aoqi@0: if (names == NULL || times == NULL) { aoqi@0: THROW_(vmSymbols::java_lang_NullPointerException(), 0); aoqi@0: } aoqi@0: objArrayOop na = objArrayOop(JNIHandles::resolve_non_null(names)); aoqi@0: objArrayHandle names_ah(THREAD, na); aoqi@0: aoqi@0: // Make sure we have a String array aoqi@0: Klass* element_klass = ObjArrayKlass::cast(names_ah->klass())->element_klass(); aoqi@0: if (element_klass != SystemDictionary::String_klass()) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Array element type is not String class", 0); aoqi@0: } aoqi@0: aoqi@0: typeArrayOop ta = typeArrayOop(JNIHandles::resolve_non_null(times)); aoqi@0: typeArrayHandle times_ah(THREAD, ta); aoqi@0: aoqi@0: ThreadTimesClosure ttc(names_ah, times_ah); aoqi@0: { aoqi@0: MutexLockerEx ml(Threads_lock); aoqi@0: Threads::threads_do(&ttc); aoqi@0: } aoqi@0: ttc.do_unlocked(); aoqi@0: return ttc.count(); aoqi@0: JVM_END aoqi@0: aoqi@0: static Handle find_deadlocks(bool object_monitors_only, TRAPS) { aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: VM_FindDeadlocks op(!object_monitors_only /* also check concurrent locks? */); aoqi@0: VMThread::execute(&op); aoqi@0: aoqi@0: DeadlockCycle* deadlocks = op.result(); aoqi@0: if (deadlocks == NULL) { aoqi@0: // no deadlock found and return aoqi@0: return Handle(); aoqi@0: } aoqi@0: aoqi@0: int num_threads = 0; aoqi@0: DeadlockCycle* cycle; aoqi@0: for (cycle = deadlocks; cycle != NULL; cycle = cycle->next()) { aoqi@0: num_threads += cycle->num_threads(); aoqi@0: } aoqi@0: aoqi@0: objArrayOop r = oopFactory::new_objArray(SystemDictionary::Thread_klass(), num_threads, CHECK_NH); aoqi@0: objArrayHandle threads_ah(THREAD, r); aoqi@0: aoqi@0: int index = 0; aoqi@0: for (cycle = deadlocks; cycle != NULL; cycle = cycle->next()) { aoqi@0: GrowableArray* deadlock_threads = cycle->threads(); aoqi@0: int len = deadlock_threads->length(); aoqi@0: for (int i = 0; i < len; i++) { aoqi@0: threads_ah->obj_at_put(index, deadlock_threads->at(i)->threadObj()); aoqi@0: index++; aoqi@0: } aoqi@0: } aoqi@0: return threads_ah; aoqi@0: } aoqi@0: aoqi@0: // Finds cycles of threads that are deadlocked involved in object monitors aoqi@0: // and JSR-166 synchronizers. aoqi@0: // Returns an array of Thread objects which are in deadlock, if any. aoqi@0: // Otherwise, returns NULL. aoqi@0: // aoqi@0: // Input parameter: aoqi@0: // object_monitors_only - if true, only check object monitors aoqi@0: // aoqi@0: JVM_ENTRY(jobjectArray, jmm_FindDeadlockedThreads(JNIEnv *env, jboolean object_monitors_only)) aoqi@0: Handle result = find_deadlocks(object_monitors_only != 0, CHECK_0); aoqi@0: return (jobjectArray) JNIHandles::make_local(env, result()); aoqi@0: JVM_END aoqi@0: aoqi@0: // Finds cycles of threads that are deadlocked on monitor locks aoqi@0: // Returns an array of Thread objects which are in deadlock, if any. aoqi@0: // Otherwise, returns NULL. aoqi@0: JVM_ENTRY(jobjectArray, jmm_FindMonitorDeadlockedThreads(JNIEnv *env)) aoqi@0: Handle result = find_deadlocks(true, CHECK_0); aoqi@0: return (jobjectArray) JNIHandles::make_local(env, result()); aoqi@0: JVM_END aoqi@0: aoqi@0: // Gets the information about GC extension attributes including aoqi@0: // the name of the attribute, its type, and a short description. aoqi@0: // aoqi@0: // Input parameters: aoqi@0: // mgr - GC memory manager aoqi@0: // info - caller allocated array of jmmExtAttributeInfo aoqi@0: // count - number of elements of the info array aoqi@0: // aoqi@0: // Returns the number of GC extension attributes filled in the info array; or aoqi@0: // -1 if info is not big enough aoqi@0: // aoqi@0: JVM_ENTRY(jint, jmm_GetGCExtAttributeInfo(JNIEnv *env, jobject mgr, jmmExtAttributeInfo* info, jint count)) aoqi@0: // All GC memory managers have 1 attribute (number of GC threads) aoqi@0: if (count == 0) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: if (info == NULL) { aoqi@0: THROW_(vmSymbols::java_lang_NullPointerException(), 0); aoqi@0: } aoqi@0: aoqi@0: info[0].name = "GcThreadCount"; aoqi@0: info[0].type = 'I'; aoqi@0: info[0].description = "Number of GC threads"; aoqi@0: return 1; aoqi@0: JVM_END aoqi@0: aoqi@0: // verify the given array is an array of java/lang/management/MemoryUsage objects aoqi@0: // of a given length and return the objArrayOop aoqi@0: static objArrayOop get_memory_usage_objArray(jobjectArray array, int length, TRAPS) { aoqi@0: if (array == NULL) { aoqi@0: THROW_(vmSymbols::java_lang_NullPointerException(), 0); aoqi@0: } aoqi@0: aoqi@0: objArrayOop oa = objArrayOop(JNIHandles::resolve_non_null(array)); aoqi@0: objArrayHandle array_h(THREAD, oa); aoqi@0: aoqi@0: // array must be of the given length aoqi@0: if (length != array_h->length()) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "The length of the given MemoryUsage array does not match the number of memory pools.", 0); aoqi@0: } aoqi@0: aoqi@0: // check if the element of array is of type MemoryUsage class aoqi@0: Klass* usage_klass = Management::java_lang_management_MemoryUsage_klass(CHECK_0); aoqi@0: Klass* element_klass = ObjArrayKlass::cast(array_h->klass())->element_klass(); aoqi@0: if (element_klass != usage_klass) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "The element type is not MemoryUsage class", 0); aoqi@0: } aoqi@0: aoqi@0: return array_h(); aoqi@0: } aoqi@0: aoqi@0: // Gets the statistics of the last GC of a given GC memory manager. aoqi@0: // Input parameters: aoqi@0: // obj - GarbageCollectorMXBean object aoqi@0: // gc_stat - caller allocated jmmGCStat where: aoqi@0: // a. before_gc_usage - array of MemoryUsage objects aoqi@0: // b. after_gc_usage - array of MemoryUsage objects aoqi@0: // c. gc_ext_attributes_values_size is set to the aoqi@0: // gc_ext_attribute_values array allocated aoqi@0: // d. gc_ext_attribute_values is a caller allocated array of jvalue. aoqi@0: // aoqi@0: // On return, aoqi@0: // gc_index == 0 indicates no GC statistics available aoqi@0: // aoqi@0: // before_gc_usage and after_gc_usage - filled with per memory pool aoqi@0: // before and after GC usage in the same order as the memory pools aoqi@0: // returned by GetMemoryPools for a given GC memory manager. aoqi@0: // num_gc_ext_attributes indicates the number of elements in aoqi@0: // the gc_ext_attribute_values array is filled; or aoqi@0: // -1 if the gc_ext_attributes_values array is not big enough aoqi@0: // aoqi@0: JVM_ENTRY(void, jmm_GetLastGCStat(JNIEnv *env, jobject obj, jmmGCStat *gc_stat)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: if (gc_stat->gc_ext_attribute_values_size > 0 && gc_stat->gc_ext_attribute_values == NULL) { aoqi@0: THROW(vmSymbols::java_lang_NullPointerException()); aoqi@0: } aoqi@0: aoqi@0: // Get the GCMemoryManager aoqi@0: GCMemoryManager* mgr = get_gc_memory_manager_from_jobject(obj, CHECK); aoqi@0: aoqi@0: // Make a copy of the last GC statistics aoqi@0: // GC may occur while constructing the last GC information aoqi@0: int num_pools = MemoryService::num_memory_pools(); aoqi@0: GCStatInfo stat(num_pools); aoqi@0: if (mgr->get_last_gc_stat(&stat) == 0) { aoqi@0: gc_stat->gc_index = 0; aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: gc_stat->gc_index = stat.gc_index(); aoqi@0: gc_stat->start_time = Management::ticks_to_ms(stat.start_time()); aoqi@0: gc_stat->end_time = Management::ticks_to_ms(stat.end_time()); aoqi@0: aoqi@0: // Current implementation does not have GC extension attributes aoqi@0: gc_stat->num_gc_ext_attributes = 0; aoqi@0: aoqi@0: // Fill the arrays of MemoryUsage objects with before and after GC aoqi@0: // per pool memory usage aoqi@0: objArrayOop bu = get_memory_usage_objArray(gc_stat->usage_before_gc, aoqi@0: num_pools, aoqi@0: CHECK); aoqi@0: objArrayHandle usage_before_gc_ah(THREAD, bu); aoqi@0: aoqi@0: objArrayOop au = get_memory_usage_objArray(gc_stat->usage_after_gc, aoqi@0: num_pools, aoqi@0: CHECK); aoqi@0: objArrayHandle usage_after_gc_ah(THREAD, au); aoqi@0: aoqi@0: for (int i = 0; i < num_pools; i++) { aoqi@0: Handle before_usage = MemoryService::create_MemoryUsage_obj(stat.before_gc_usage_for_pool(i), CHECK); aoqi@0: Handle after_usage; aoqi@0: aoqi@0: MemoryUsage u = stat.after_gc_usage_for_pool(i); aoqi@0: if (u.max_size() == 0 && u.used() > 0) { aoqi@0: // If max size == 0, this pool is a survivor space. aoqi@0: // Set max size = -1 since the pools will be swapped after GC. aoqi@0: MemoryUsage usage(u.init_size(), u.used(), u.committed(), (size_t)-1); aoqi@0: after_usage = MemoryService::create_MemoryUsage_obj(usage, CHECK); aoqi@0: } else { aoqi@0: after_usage = MemoryService::create_MemoryUsage_obj(stat.after_gc_usage_for_pool(i), CHECK); aoqi@0: } aoqi@0: usage_before_gc_ah->obj_at_put(i, before_usage()); aoqi@0: usage_after_gc_ah->obj_at_put(i, after_usage()); aoqi@0: } aoqi@0: aoqi@0: if (gc_stat->gc_ext_attribute_values_size > 0) { aoqi@0: // Current implementation only has 1 attribute (number of GC threads) aoqi@0: // The type is 'I' aoqi@0: gc_stat->gc_ext_attribute_values[0].i = mgr->num_gc_threads(); aoqi@0: } aoqi@0: JVM_END aoqi@0: aoqi@0: JVM_ENTRY(void, jmm_SetGCNotificationEnabled(JNIEnv *env, jobject obj, jboolean enabled)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: // Get the GCMemoryManager aoqi@0: GCMemoryManager* mgr = get_gc_memory_manager_from_jobject(obj, CHECK); aoqi@0: mgr->set_notification_enabled(enabled?true:false); aoqi@0: JVM_END aoqi@0: aoqi@0: // Dump heap - Returns 0 if succeeds. aoqi@0: JVM_ENTRY(jint, jmm_DumpHeap0(JNIEnv *env, jstring outputfile, jboolean live)) aoqi@0: #if INCLUDE_SERVICES aoqi@0: ResourceMark rm(THREAD); aoqi@0: oop on = JNIHandles::resolve_external_guard(outputfile); aoqi@0: if (on == NULL) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_NullPointerException(), aoqi@0: "Output file name cannot be null.", -1); aoqi@0: } aoqi@0: char* name = java_lang_String::as_platform_dependent_str(on, CHECK_(-1)); aoqi@0: if (name == NULL) { aoqi@0: THROW_MSG_(vmSymbols::java_lang_NullPointerException(), aoqi@0: "Output file name cannot be null.", -1); aoqi@0: } aoqi@0: HeapDumper dumper(live ? true : false); aoqi@0: if (dumper.dump(name) != 0) { aoqi@0: const char* errmsg = dumper.error_as_C_string(); aoqi@0: THROW_MSG_(vmSymbols::java_io_IOException(), errmsg, -1); aoqi@0: } aoqi@0: return 0; aoqi@0: #else // INCLUDE_SERVICES aoqi@0: return -1; aoqi@0: #endif // INCLUDE_SERVICES aoqi@0: JVM_END aoqi@0: aoqi@0: JVM_ENTRY(jobjectArray, jmm_GetDiagnosticCommands(JNIEnv *env)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: GrowableArray* dcmd_list = DCmdFactory::DCmd_list(DCmd_Source_MBean); aoqi@0: objArrayOop cmd_array_oop = oopFactory::new_objArray(SystemDictionary::String_klass(), aoqi@0: dcmd_list->length(), CHECK_NULL); aoqi@0: objArrayHandle cmd_array(THREAD, cmd_array_oop); aoqi@0: for (int i = 0; i < dcmd_list->length(); i++) { aoqi@0: oop cmd_name = java_lang_String::create_oop_from_str(dcmd_list->at(i), CHECK_NULL); aoqi@0: cmd_array->obj_at_put(i, cmd_name); aoqi@0: } aoqi@0: return (jobjectArray) JNIHandles::make_local(env, cmd_array()); aoqi@0: JVM_END aoqi@0: aoqi@0: JVM_ENTRY(void, jmm_GetDiagnosticCommandInfo(JNIEnv *env, jobjectArray cmds, aoqi@0: dcmdInfo* infoArray)) aoqi@0: if (cmds == NULL || infoArray == NULL) { aoqi@0: THROW(vmSymbols::java_lang_NullPointerException()); aoqi@0: } aoqi@0: aoqi@0: ResourceMark rm(THREAD); aoqi@0: aoqi@0: objArrayOop ca = objArrayOop(JNIHandles::resolve_non_null(cmds)); aoqi@0: objArrayHandle cmds_ah(THREAD, ca); aoqi@0: aoqi@0: // Make sure we have a String array aoqi@0: Klass* element_klass = ObjArrayKlass::cast(cmds_ah->klass())->element_klass(); aoqi@0: if (element_klass != SystemDictionary::String_klass()) { aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Array element type is not String class"); aoqi@0: } aoqi@0: aoqi@0: GrowableArray* info_list = DCmdFactory::DCmdInfo_list(DCmd_Source_MBean); aoqi@0: aoqi@0: int num_cmds = cmds_ah->length(); aoqi@0: for (int i = 0; i < num_cmds; i++) { aoqi@0: oop cmd = cmds_ah->obj_at(i); aoqi@0: if (cmd == NULL) { aoqi@0: THROW_MSG(vmSymbols::java_lang_NullPointerException(), aoqi@0: "Command name cannot be null."); aoqi@0: } aoqi@0: char* cmd_name = java_lang_String::as_utf8_string(cmd); aoqi@0: if (cmd_name == NULL) { aoqi@0: THROW_MSG(vmSymbols::java_lang_NullPointerException(), aoqi@0: "Command name cannot be null."); aoqi@0: } aoqi@0: int pos = info_list->find((void*)cmd_name,DCmdInfo::by_name); aoqi@0: if (pos == -1) { aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Unknown diagnostic command"); aoqi@0: } aoqi@0: DCmdInfo* info = info_list->at(pos); aoqi@0: infoArray[i].name = info->name(); aoqi@0: infoArray[i].description = info->description(); aoqi@0: infoArray[i].impact = info->impact(); aoqi@0: JavaPermission p = info->permission(); aoqi@0: infoArray[i].permission_class = p._class; aoqi@0: infoArray[i].permission_name = p._name; aoqi@0: infoArray[i].permission_action = p._action; aoqi@0: infoArray[i].num_arguments = info->num_arguments(); aoqi@0: infoArray[i].enabled = info->is_enabled(); aoqi@0: } aoqi@0: JVM_END aoqi@0: aoqi@0: JVM_ENTRY(void, jmm_GetDiagnosticCommandArgumentsInfo(JNIEnv *env, aoqi@0: jstring command, dcmdArgInfo* infoArray)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: oop cmd = JNIHandles::resolve_external_guard(command); aoqi@0: if (cmd == NULL) { aoqi@0: THROW_MSG(vmSymbols::java_lang_NullPointerException(), aoqi@0: "Command line cannot be null."); aoqi@0: } aoqi@0: char* cmd_name = java_lang_String::as_utf8_string(cmd); aoqi@0: if (cmd_name == NULL) { aoqi@0: THROW_MSG(vmSymbols::java_lang_NullPointerException(), aoqi@0: "Command line content cannot be null."); aoqi@0: } aoqi@0: DCmd* dcmd = NULL; aoqi@0: DCmdFactory*factory = DCmdFactory::factory(DCmd_Source_MBean, cmd_name, aoqi@0: strlen(cmd_name)); aoqi@0: if (factory != NULL) { aoqi@0: dcmd = factory->create_resource_instance(NULL); aoqi@0: } aoqi@0: if (dcmd == NULL) { aoqi@0: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), aoqi@0: "Unknown diagnostic command"); aoqi@0: } aoqi@0: DCmdMark mark(dcmd); aoqi@0: GrowableArray* array = dcmd->argument_info_array(); aoqi@0: if (array->length() == 0) { aoqi@0: return; aoqi@0: } aoqi@0: for (int i = 0; i < array->length(); i++) { aoqi@0: infoArray[i].name = array->at(i)->name(); aoqi@0: infoArray[i].description = array->at(i)->description(); aoqi@0: infoArray[i].type = array->at(i)->type(); aoqi@0: infoArray[i].default_string = array->at(i)->default_string(); aoqi@0: infoArray[i].mandatory = array->at(i)->is_mandatory(); aoqi@0: infoArray[i].option = array->at(i)->is_option(); aoqi@0: infoArray[i].multiple = array->at(i)->is_multiple(); aoqi@0: infoArray[i].position = array->at(i)->position(); aoqi@0: } aoqi@0: return; aoqi@0: JVM_END aoqi@0: aoqi@0: JVM_ENTRY(jstring, jmm_ExecuteDiagnosticCommand(JNIEnv *env, jstring commandline)) aoqi@0: ResourceMark rm(THREAD); aoqi@0: oop cmd = JNIHandles::resolve_external_guard(commandline); aoqi@0: if (cmd == NULL) { aoqi@0: THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(), aoqi@0: "Command line cannot be null."); aoqi@0: } aoqi@0: char* cmdline = java_lang_String::as_utf8_string(cmd); aoqi@0: if (cmdline == NULL) { aoqi@0: THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(), aoqi@0: "Command line content cannot be null."); aoqi@0: } aoqi@0: bufferedStream output; aoqi@0: DCmd::parse_and_execute(DCmd_Source_MBean, &output, cmdline, ' ', CHECK_NULL); aoqi@0: oop result = java_lang_String::create_oop_from_str(output.as_string(), CHECK_NULL); aoqi@0: return (jstring) JNIHandles::make_local(env, result); aoqi@0: JVM_END aoqi@0: aoqi@0: JVM_ENTRY(void, jmm_SetDiagnosticFrameworkNotificationEnabled(JNIEnv *env, jboolean enabled)) aoqi@0: DCmdFactory::set_jmx_notification_enabled(enabled?true:false); aoqi@0: JVM_END aoqi@0: aoqi@0: jlong Management::ticks_to_ms(jlong ticks) { aoqi@0: assert(os::elapsed_frequency() > 0, "Must be non-zero"); aoqi@0: return (jlong)(((double)ticks / (double)os::elapsed_frequency()) aoqi@0: * (double)1000.0); aoqi@0: } sla@7107: #endif // INCLUDE_MANAGEMENT aoqi@0: sla@7107: // Gets an array containing the amount of memory allocated on the Java sla@7107: // heap for a set of threads (in bytes). Each element of the array is sla@7107: // the amount of memory allocated for the thread ID specified in the sla@7107: // corresponding entry in the given array of thread IDs; or -1 if the sla@7107: // thread does not exist or has terminated. sla@7107: JVM_ENTRY(void, jmm_GetThreadAllocatedMemory(JNIEnv *env, jlongArray ids, sla@7107: jlongArray sizeArray)) sla@7107: // Check if threads is null sla@7107: if (ids == NULL || sizeArray == NULL) { sla@7107: THROW(vmSymbols::java_lang_NullPointerException()); sla@7107: } sla@7107: sla@7107: ResourceMark rm(THREAD); sla@7107: typeArrayOop ta = typeArrayOop(JNIHandles::resolve_non_null(ids)); sla@7107: typeArrayHandle ids_ah(THREAD, ta); sla@7107: sla@7107: typeArrayOop sa = typeArrayOop(JNIHandles::resolve_non_null(sizeArray)); sla@7107: typeArrayHandle sizeArray_h(THREAD, sa); sla@7107: sla@7107: // validate the thread id array sla@7107: validate_thread_id_array(ids_ah, CHECK); sla@7107: sla@7107: // sizeArray must be of the same length as the given array of thread IDs sla@7107: int num_threads = ids_ah->length(); sla@7107: if (num_threads != sizeArray_h->length()) { sla@7107: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), sla@7107: "The length of the given long array does not match the length of " sla@7107: "the given array of thread IDs"); sla@7107: } sla@7107: sla@7107: MutexLockerEx ml(Threads_lock); sla@7107: for (int i = 0; i < num_threads; i++) { sla@7107: JavaThread* java_thread = Threads::find_java_thread_from_java_tid(ids_ah->long_at(i)); sla@7107: if (java_thread != NULL) { sla@7107: sizeArray_h->long_at_put(i, java_thread->cooked_allocated_bytes()); sla@7107: } sla@7107: } sla@7107: JVM_END sla@7107: sla@7107: // Returns the CPU time consumed by a given thread (in nanoseconds). sla@7107: // If thread_id == 0, CPU time for the current thread is returned. sla@7107: // If user_sys_cpu_time = true, user level and system CPU time of sla@7107: // a given thread is returned; otherwise, only user level CPU time sla@7107: // is returned. sla@7107: JVM_ENTRY(jlong, jmm_GetThreadCpuTimeWithKind(JNIEnv *env, jlong thread_id, jboolean user_sys_cpu_time)) sla@7107: if (!os::is_thread_cpu_time_supported()) { sla@7107: return -1; sla@7107: } sla@7107: sla@7107: if (thread_id < 0) { sla@7107: THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), sla@7107: "Invalid thread ID", -1); sla@7107: } sla@7107: sla@7107: JavaThread* java_thread = NULL; sla@7107: if (thread_id == 0) { sla@7107: // current thread sla@7107: return os::current_thread_cpu_time(user_sys_cpu_time != 0); sla@7107: } else { sla@7107: MutexLockerEx ml(Threads_lock); sla@7107: java_thread = Threads::find_java_thread_from_java_tid(thread_id); sla@7107: if (java_thread != NULL) { sla@7107: return os::thread_cpu_time((Thread*) java_thread, user_sys_cpu_time != 0); sla@7107: } sla@7107: } sla@7107: return -1; sla@7107: JVM_END sla@7107: sla@7107: // Gets an array containing the CPU times consumed by a set of threads sla@7107: // (in nanoseconds). Each element of the array is the CPU time for the sla@7107: // thread ID specified in the corresponding entry in the given array sla@7107: // of thread IDs; or -1 if the thread does not exist or has terminated. sla@7107: // If user_sys_cpu_time = true, the sum of user level and system CPU time sla@7107: // for the given thread is returned; otherwise, only user level CPU time sla@7107: // is returned. sla@7107: JVM_ENTRY(void, jmm_GetThreadCpuTimesWithKind(JNIEnv *env, jlongArray ids, sla@7107: jlongArray timeArray, sla@7107: jboolean user_sys_cpu_time)) sla@7107: // Check if threads is null sla@7107: if (ids == NULL || timeArray == NULL) { sla@7107: THROW(vmSymbols::java_lang_NullPointerException()); sla@7107: } sla@7107: sla@7107: ResourceMark rm(THREAD); sla@7107: typeArrayOop ta = typeArrayOop(JNIHandles::resolve_non_null(ids)); sla@7107: typeArrayHandle ids_ah(THREAD, ta); sla@7107: sla@7107: typeArrayOop tia = typeArrayOop(JNIHandles::resolve_non_null(timeArray)); sla@7107: typeArrayHandle timeArray_h(THREAD, tia); sla@7107: sla@7107: // validate the thread id array sla@7107: validate_thread_id_array(ids_ah, CHECK); sla@7107: sla@7107: // timeArray must be of the same length as the given array of thread IDs sla@7107: int num_threads = ids_ah->length(); sla@7107: if (num_threads != timeArray_h->length()) { sla@7107: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), sla@7107: "The length of the given long array does not match the length of " sla@7107: "the given array of thread IDs"); sla@7107: } sla@7107: sla@7107: MutexLockerEx ml(Threads_lock); sla@7107: for (int i = 0; i < num_threads; i++) { sla@7107: JavaThread* java_thread = Threads::find_java_thread_from_java_tid(ids_ah->long_at(i)); sla@7107: if (java_thread != NULL) { sla@7107: timeArray_h->long_at_put(i, os::thread_cpu_time((Thread*)java_thread, sla@7107: user_sys_cpu_time != 0)); sla@7107: } sla@7107: } sla@7107: JVM_END sla@7107: sla@7107: sla@7107: sla@7107: #if INCLUDE_MANAGEMENT aoqi@0: const struct jmmInterface_1_ jmm_interface = { aoqi@0: NULL, aoqi@0: NULL, aoqi@0: jmm_GetVersion, aoqi@0: jmm_GetOptionalSupport, aoqi@0: jmm_GetInputArguments, aoqi@0: jmm_GetThreadInfo, aoqi@0: jmm_GetInputArgumentArray, aoqi@0: jmm_GetMemoryPools, aoqi@0: jmm_GetMemoryManagers, aoqi@0: jmm_GetMemoryPoolUsage, aoqi@0: jmm_GetPeakMemoryPoolUsage, aoqi@0: jmm_GetThreadAllocatedMemory, aoqi@0: jmm_GetMemoryUsage, aoqi@0: jmm_GetLongAttribute, aoqi@0: jmm_GetBoolAttribute, aoqi@0: jmm_SetBoolAttribute, aoqi@0: jmm_GetLongAttributes, aoqi@0: jmm_FindMonitorDeadlockedThreads, aoqi@0: jmm_GetThreadCpuTime, aoqi@0: jmm_GetVMGlobalNames, aoqi@0: jmm_GetVMGlobals, aoqi@0: jmm_GetInternalThreadTimes, aoqi@0: jmm_ResetStatistic, aoqi@0: jmm_SetPoolSensor, aoqi@0: jmm_SetPoolThreshold, aoqi@0: jmm_GetPoolCollectionUsage, aoqi@0: jmm_GetGCExtAttributeInfo, aoqi@0: jmm_GetLastGCStat, aoqi@0: jmm_GetThreadCpuTimeWithKind, aoqi@0: jmm_GetThreadCpuTimesWithKind, aoqi@0: jmm_DumpHeap0, aoqi@0: jmm_FindDeadlockedThreads, aoqi@0: jmm_SetVMGlobal, aoqi@0: NULL, aoqi@0: jmm_DumpThreads, aoqi@0: jmm_SetGCNotificationEnabled, aoqi@0: jmm_GetDiagnosticCommands, aoqi@0: jmm_GetDiagnosticCommandInfo, aoqi@0: jmm_GetDiagnosticCommandArgumentsInfo, aoqi@0: jmm_ExecuteDiagnosticCommand, aoqi@0: jmm_SetDiagnosticFrameworkNotificationEnabled aoqi@0: }; aoqi@0: #endif // INCLUDE_MANAGEMENT aoqi@0: aoqi@0: void* Management::get_jmm_interface(int version) { aoqi@0: #if INCLUDE_MANAGEMENT aoqi@0: if (version == JMM_VERSION_1_0) { aoqi@0: return (void*) &jmm_interface; aoqi@0: } aoqi@0: #endif // INCLUDE_MANAGEMENT aoqi@0: return NULL; aoqi@0: }