fparain@2888: /* kevinw@5474: * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. fparain@2888: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. fparain@2888: * fparain@2888: * This code is free software; you can redistribute it and/or modify it fparain@2888: * under the terms of the GNU General Public License version 2 only, as fparain@2888: * published by the Free Software Foundation. fparain@2888: * fparain@2888: * This code is distributed in the hope that it will be useful, but WITHOUT fparain@2888: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or fparain@2888: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License fparain@2888: * version 2 for more details (a copy is included in the LICENSE file that fparain@2888: * accompanied this code). fparain@2888: * fparain@2888: * You should have received a copy of the GNU General Public License version fparain@2888: * 2 along with this work; if not, write to the Free Software Foundation, fparain@2888: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. fparain@2888: * fparain@2888: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA fparain@2888: * or visit www.oracle.com if you need additional information or have any fparain@2888: * questions. fparain@2888: * fparain@2888: */ fparain@2888: fparain@2888: #include "precompiled.hpp" fparain@2888: #include "classfile/systemDictionary.hpp" fparain@2888: #include "classfile/vmSymbols.hpp" fparain@2888: #include "oops/oop.inline.hpp" fparain@2888: #include "runtime/interfaceSupport.hpp" fparain@2888: #include "runtime/java.hpp" fparain@2888: #include "runtime/javaCalls.hpp" fparain@2888: #include "runtime/mutex.hpp" fparain@2888: #include "runtime/mutexLocker.hpp" fparain@2888: #include "services/gcNotifier.hpp" fparain@2888: #include "services/management.hpp" fparain@2888: #include "services/memoryService.hpp" fparain@2888: #include "memoryManager.hpp" fparain@2888: #include "memory/oopFactory.hpp" fparain@2888: fparain@2888: GCNotificationRequest *GCNotifier::first_request = NULL; fparain@2888: GCNotificationRequest *GCNotifier::last_request = NULL; fparain@2888: fparain@2888: void GCNotifier::pushNotification(GCMemoryManager *mgr, const char *action, const char *cause) { fparain@2888: // Make a copy of the last GC statistics fparain@2888: // GC may occur between now and the creation of the notification fparain@2888: int num_pools = MemoryService::num_memory_pools(); dsamersoff@3471: // stat is deallocated inside GCNotificationRequest zgu@3900: GCStatInfo* stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(num_pools); fparain@2888: mgr->get_last_gc_stat(stat); fparain@2888: GCNotificationRequest *request = new GCNotificationRequest(os::javaTimeMillis(),mgr,action,cause,stat); fparain@2888: addRequest(request); fparain@2888: } fparain@2888: fparain@2888: void GCNotifier::addRequest(GCNotificationRequest *request) { fparain@2888: MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); fparain@2888: if(first_request == NULL) { fparain@2888: first_request = request; fparain@2888: } else { fparain@2888: last_request->next = request; fparain@2888: } fparain@2888: last_request = request; fparain@2888: Service_lock->notify_all(); fparain@2888: } fparain@2888: fparain@2888: GCNotificationRequest *GCNotifier::getRequest() { fparain@2888: MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); fparain@2888: GCNotificationRequest *request = first_request; fparain@2888: if(first_request != NULL) { fparain@2888: first_request = first_request->next; fparain@2888: } fparain@2888: return request; fparain@2888: } fparain@2888: fparain@2888: bool GCNotifier::has_event() { fparain@2888: return first_request != NULL; fparain@2888: } fparain@2888: fparain@2888: static Handle getGcInfoBuilder(GCMemoryManager *gcManager,TRAPS) { fparain@2888: coleenp@4037: Klass* k = Management::sun_management_GarbageCollectorImpl_klass(CHECK_NH); fparain@2888: instanceKlassHandle gcMBeanKlass (THREAD, k); fparain@2888: fparain@2888: instanceOop i = gcManager->get_memory_manager_instance(THREAD); fparain@2888: instanceHandle ih(THREAD, i); fparain@2888: fparain@2888: JavaValue result(T_OBJECT); fparain@2888: JavaCallArguments args(ih); fparain@2888: fparain@2888: JavaCalls::call_virtual(&result, fparain@2888: gcMBeanKlass, fparain@2888: vmSymbols::getGcInfoBuilder_name(), fparain@2888: vmSymbols::getGcInfoBuilder_signature(), fparain@2888: &args, fparain@2888: CHECK_NH); fparain@2888: return Handle(THREAD,(oop)result.get_jobject()); fparain@2888: } fparain@2888: fparain@2888: static Handle createGcInfo(GCMemoryManager *gcManager, GCStatInfo *gcStatInfo,TRAPS) { fparain@2888: fparain@2888: // Fill the arrays of MemoryUsage objects with before and after GC fparain@2888: // per pool memory usage fparain@2888: coleenp@4037: Klass* mu_klass = Management::java_lang_management_MemoryUsage_klass(CHECK_NH); johnc@3027: instanceKlassHandle mu_kh(THREAD, mu_klass); johnc@3027: johnc@3027: // The array allocations below should use a handle containing mu_klass johnc@3027: // as the first allocation could trigger a GC, causing the actual johnc@3027: // klass oop to move, and leaving mu_klass pointing to the old johnc@3027: // location. johnc@3027: objArrayOop bu = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH); fparain@2888: objArrayHandle usage_before_gc_ah(THREAD, bu); johnc@3027: objArrayOop au = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH); fparain@2888: objArrayHandle usage_after_gc_ah(THREAD, au); fparain@2888: fparain@2888: for (int i = 0; i < MemoryService::num_memory_pools(); i++) { fparain@2888: Handle before_usage = MemoryService::create_MemoryUsage_obj(gcStatInfo->before_gc_usage_for_pool(i), CHECK_NH); fparain@2888: Handle after_usage; fparain@2888: fparain@2888: MemoryUsage u = gcStatInfo->after_gc_usage_for_pool(i); fparain@2888: if (u.max_size() == 0 && u.used() > 0) { fparain@2888: // If max size == 0, this pool is a survivor space. fparain@2888: // Set max size = -1 since the pools will be swapped after GC. fparain@2888: MemoryUsage usage(u.init_size(), u.used(), u.committed(), (size_t)-1); fparain@2888: after_usage = MemoryService::create_MemoryUsage_obj(usage, CHECK_NH); fparain@2888: } else { fparain@2888: after_usage = MemoryService::create_MemoryUsage_obj(u, CHECK_NH); fparain@2888: } fparain@2888: usage_before_gc_ah->obj_at_put(i, before_usage()); fparain@2888: usage_after_gc_ah->obj_at_put(i, after_usage()); fparain@2888: } fparain@2888: fparain@2888: // Current implementation only has 1 attribute (number of GC threads) fparain@2888: // The type is 'I' fparain@2888: objArrayOop extra_args_array = oopFactory::new_objArray(SystemDictionary::Integer_klass(), 1, CHECK_NH); fparain@2888: objArrayHandle extra_array (THREAD, extra_args_array); coleenp@4037: Klass* itKlass = SystemDictionary::Integer_klass(); fparain@2888: instanceKlassHandle intK(THREAD, itKlass); fparain@2888: fparain@2888: instanceHandle extra_arg_val = intK->allocate_instance_handle(CHECK_NH); fparain@2888: fparain@2888: { fparain@2888: JavaValue res(T_VOID); fparain@2888: JavaCallArguments argsInt; fparain@2888: argsInt.push_oop(extra_arg_val); fparain@2888: argsInt.push_int(gcManager->num_gc_threads()); fparain@2888: fparain@2888: JavaCalls::call_special(&res, fparain@2888: intK, fparain@2888: vmSymbols::object_initializer_name(), fparain@2888: vmSymbols::int_void_signature(), fparain@2888: &argsInt, fparain@2888: CHECK_NH); fparain@2888: } fparain@2888: extra_array->obj_at_put(0,extra_arg_val()); fparain@2888: coleenp@4037: Klass* gcInfoklass = Management::com_sun_management_GcInfo_klass(CHECK_NH); johnc@3027: instanceKlassHandle ik(THREAD, gcInfoklass); fparain@2888: fparain@2888: Handle gcInfo_instance = ik->allocate_instance_handle(CHECK_NH); fparain@2888: fparain@2888: JavaValue constructor_result(T_VOID); fparain@2888: JavaCallArguments constructor_args(16); fparain@2888: constructor_args.push_oop(gcInfo_instance); fparain@2888: constructor_args.push_oop(getGcInfoBuilder(gcManager,THREAD)); fparain@2888: constructor_args.push_long(gcStatInfo->gc_index()); fparain@3685: constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->start_time())); fparain@3685: constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->end_time())); fparain@2888: constructor_args.push_oop(usage_before_gc_ah); fparain@2888: constructor_args.push_oop(usage_after_gc_ah); fparain@2888: constructor_args.push_oop(extra_array); fparain@2888: fparain@2888: JavaCalls::call_special(&constructor_result, fparain@2888: ik, fparain@2888: vmSymbols::object_initializer_name(), fparain@2888: vmSymbols::com_sun_management_GcInfo_constructor_signature(), fparain@2888: &constructor_args, fparain@2888: CHECK_NH); fparain@2888: fparain@2888: return Handle(gcInfo_instance()); fparain@2888: } fparain@2888: fparain@2888: void GCNotifier::sendNotification(TRAPS) { fparain@3549: GCNotifier::sendNotificationInternal(THREAD); fparain@3549: // Clearing pending exception to avoid premature termination of fparain@3549: // the service thread fparain@3549: if (HAS_PENDING_EXCEPTION) { fparain@3549: CLEAR_PENDING_EXCEPTION; fparain@3549: } fparain@3549: } fparain@3549: fparain@3549: class NotificationMark : public StackObj { fparain@3549: // This class is used in GCNotifier::sendNotificationInternal to ensure that fparain@3549: // the GCNotificationRequest object is properly cleaned up, whatever path fparain@3549: // is used to exit the method. fparain@3549: GCNotificationRequest* _request; fparain@3549: public: fparain@3549: NotificationMark(GCNotificationRequest* r) { fparain@3549: _request = r; fparain@3549: } fparain@3549: ~NotificationMark() { fparain@3549: assert(_request != NULL, "Sanity check"); fparain@3549: delete _request; fparain@3549: } fparain@3549: }; fparain@3549: fparain@3549: void GCNotifier::sendNotificationInternal(TRAPS) { fparain@2888: ResourceMark rm(THREAD); fparain@3549: HandleMark hm(THREAD); fparain@2888: GCNotificationRequest *request = getRequest(); fparain@3549: if (request != NULL) { fparain@3549: NotificationMark nm(request); kevinw@5668: Handle objGcInfo = createGcInfo(request->gcManager, request->gcStatInfo, CHECK); fparain@2888: kevinw@5474: Handle objName = java_lang_String::create_from_str(request->gcManager->name(), CHECK); kevinw@5474: Handle objAction = java_lang_String::create_from_str(request->gcAction, CHECK); kevinw@5474: Handle objCause = java_lang_String::create_from_str(request->gcCause, CHECK); fparain@2888: coleenp@4037: Klass* k = Management::sun_management_GarbageCollectorImpl_klass(CHECK); fparain@3549: instanceKlassHandle gc_mbean_klass(THREAD, k); fparain@2888: fparain@2888: instanceOop gc_mbean = request->gcManager->get_memory_manager_instance(THREAD); fparain@2888: instanceHandle gc_mbean_h(THREAD, gc_mbean); fparain@2888: if (!gc_mbean_h->is_a(k)) { fparain@2888: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), fparain@2888: "This GCMemoryManager doesn't have a GarbageCollectorMXBean"); fparain@2888: } fparain@2888: fparain@2888: JavaValue result(T_VOID); fparain@2888: JavaCallArguments args(gc_mbean_h); fparain@2888: args.push_long(request->timestamp); fparain@2888: args.push_oop(objName); fparain@2888: args.push_oop(objAction); fparain@2888: args.push_oop(objCause); fparain@2888: args.push_oop(objGcInfo); fparain@2888: fparain@2888: JavaCalls::call_virtual(&result, fparain@2888: gc_mbean_klass, fparain@2888: vmSymbols::createGCNotification_name(), fparain@2888: vmSymbols::createGCNotification_signature(), fparain@2888: &args, fparain@2888: CHECK); fparain@2888: } fparain@2888: } fparain@2888: