Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/services/gcNotifier.cpp
32285 views
/*1* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/systemDictionary.hpp"26#include "classfile/vmSymbols.hpp"27#include "oops/oop.inline.hpp"28#include "runtime/interfaceSupport.hpp"29#include "runtime/java.hpp"30#include "runtime/javaCalls.hpp"31#include "runtime/mutex.hpp"32#include "runtime/mutexLocker.hpp"33#include "services/gcNotifier.hpp"34#include "services/management.hpp"35#include "services/memoryService.hpp"36#include "memoryManager.hpp"37#include "memory/oopFactory.hpp"3839GCNotificationRequest *GCNotifier::first_request = NULL;40GCNotificationRequest *GCNotifier::last_request = NULL;4142void GCNotifier::pushNotification(GCMemoryManager *mgr, const char *action, const char *cause) {43// Make a copy of the last GC statistics44// GC may occur between now and the creation of the notification45int num_pools = MemoryService::num_memory_pools();46// stat is deallocated inside GCNotificationRequest47GCStatInfo* stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(num_pools);48mgr->get_last_gc_stat(stat);49GCNotificationRequest *request = new GCNotificationRequest(os::javaTimeMillis(),mgr,action,cause,stat);50addRequest(request);51}5253void GCNotifier::addRequest(GCNotificationRequest *request) {54MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);55if(first_request == NULL) {56first_request = request;57} else {58last_request->next = request;59}60last_request = request;61Service_lock->notify_all();62}6364GCNotificationRequest *GCNotifier::getRequest() {65MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);66GCNotificationRequest *request = first_request;67if(first_request != NULL) {68first_request = first_request->next;69}70return request;71}7273bool GCNotifier::has_event() {74return first_request != NULL;75}7677static Handle getGcInfoBuilder(GCMemoryManager *gcManager,TRAPS) {7879Klass* k = Management::sun_management_GarbageCollectorImpl_klass(CHECK_NH);80instanceKlassHandle gcMBeanKlass (THREAD, k);8182instanceOop i = gcManager->get_memory_manager_instance(THREAD);83instanceHandle ih(THREAD, i);8485JavaValue result(T_OBJECT);86JavaCallArguments args(ih);8788JavaCalls::call_virtual(&result,89gcMBeanKlass,90vmSymbols::getGcInfoBuilder_name(),91vmSymbols::getGcInfoBuilder_signature(),92&args,93CHECK_NH);94return Handle(THREAD,(oop)result.get_jobject());95}9697static Handle createGcInfo(GCMemoryManager *gcManager, GCStatInfo *gcStatInfo,TRAPS) {9899// Fill the arrays of MemoryUsage objects with before and after GC100// per pool memory usage101102Klass* mu_klass = Management::java_lang_management_MemoryUsage_klass(CHECK_NH);103instanceKlassHandle mu_kh(THREAD, mu_klass);104105// The array allocations below should use a handle containing mu_klass106// as the first allocation could trigger a GC, causing the actual107// klass oop to move, and leaving mu_klass pointing to the old108// location.109objArrayOop bu = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH);110objArrayHandle usage_before_gc_ah(THREAD, bu);111objArrayOop au = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH);112objArrayHandle usage_after_gc_ah(THREAD, au);113114for (int i = 0; i < MemoryService::num_memory_pools(); i++) {115Handle before_usage = MemoryService::create_MemoryUsage_obj(gcStatInfo->before_gc_usage_for_pool(i), CHECK_NH);116Handle after_usage;117118MemoryUsage u = gcStatInfo->after_gc_usage_for_pool(i);119if (u.max_size() == 0 && u.used() > 0) {120// If max size == 0, this pool is a survivor space.121// Set max size = -1 since the pools will be swapped after GC.122MemoryUsage usage(u.init_size(), u.used(), u.committed(), (size_t)-1);123after_usage = MemoryService::create_MemoryUsage_obj(usage, CHECK_NH);124} else {125after_usage = MemoryService::create_MemoryUsage_obj(u, CHECK_NH);126}127usage_before_gc_ah->obj_at_put(i, before_usage());128usage_after_gc_ah->obj_at_put(i, after_usage());129}130131// Current implementation only has 1 attribute (number of GC threads)132// The type is 'I'133objArrayOop extra_args_array = oopFactory::new_objArray(SystemDictionary::Integer_klass(), 1, CHECK_NH);134objArrayHandle extra_array (THREAD, extra_args_array);135Klass* itKlass = SystemDictionary::Integer_klass();136instanceKlassHandle intK(THREAD, itKlass);137138instanceHandle extra_arg_val = intK->allocate_instance_handle(CHECK_NH);139140{141JavaValue res(T_VOID);142JavaCallArguments argsInt;143argsInt.push_oop(extra_arg_val);144argsInt.push_int(gcManager->num_gc_threads());145146JavaCalls::call_special(&res,147intK,148vmSymbols::object_initializer_name(),149vmSymbols::int_void_signature(),150&argsInt,151CHECK_NH);152}153extra_array->obj_at_put(0,extra_arg_val());154155Klass* gcInfoklass = Management::com_sun_management_GcInfo_klass(CHECK_NH);156instanceKlassHandle ik(THREAD, gcInfoklass);157158Handle gcInfo_instance = ik->allocate_instance_handle(CHECK_NH);159160JavaValue constructor_result(T_VOID);161JavaCallArguments constructor_args(16);162constructor_args.push_oop(gcInfo_instance);163constructor_args.push_oop(getGcInfoBuilder(gcManager,THREAD));164constructor_args.push_long(gcStatInfo->gc_index());165constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->start_time()));166constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->end_time()));167constructor_args.push_oop(usage_before_gc_ah);168constructor_args.push_oop(usage_after_gc_ah);169constructor_args.push_oop(extra_array);170171JavaCalls::call_special(&constructor_result,172ik,173vmSymbols::object_initializer_name(),174vmSymbols::com_sun_management_GcInfo_constructor_signature(),175&constructor_args,176CHECK_NH);177178return Handle(gcInfo_instance());179}180181void GCNotifier::sendNotification(TRAPS) {182GCNotifier::sendNotificationInternal(THREAD);183// Clearing pending exception to avoid premature termination of184// the service thread185if (HAS_PENDING_EXCEPTION) {186CLEAR_PENDING_EXCEPTION;187}188}189190class NotificationMark : public StackObj {191// This class is used in GCNotifier::sendNotificationInternal to ensure that192// the GCNotificationRequest object is properly cleaned up, whatever path193// is used to exit the method.194GCNotificationRequest* _request;195public:196NotificationMark(GCNotificationRequest* r) {197_request = r;198}199~NotificationMark() {200assert(_request != NULL, "Sanity check");201delete _request;202}203};204205void GCNotifier::sendNotificationInternal(TRAPS) {206ResourceMark rm(THREAD);207HandleMark hm(THREAD);208GCNotificationRequest *request = getRequest();209if (request != NULL) {210NotificationMark nm(request);211Handle objGcInfo = createGcInfo(request->gcManager, request->gcStatInfo, CHECK);212213Handle objName = java_lang_String::create_from_str(request->gcManager->name(), CHECK);214Handle objAction = java_lang_String::create_from_str(request->gcAction, CHECK);215Handle objCause = java_lang_String::create_from_str(request->gcCause, CHECK);216217Klass* k = Management::sun_management_GarbageCollectorImpl_klass(CHECK);218instanceKlassHandle gc_mbean_klass(THREAD, k);219220instanceOop gc_mbean = request->gcManager->get_memory_manager_instance(THREAD);221instanceHandle gc_mbean_h(THREAD, gc_mbean);222if (!gc_mbean_h->is_a(k)) {223THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),224"This GCMemoryManager doesn't have a GarbageCollectorMXBean");225}226227JavaValue result(T_VOID);228JavaCallArguments args(gc_mbean_h);229args.push_long(request->timestamp);230args.push_oop(objName);231args.push_oop(objAction);232args.push_oop(objCause);233args.push_oop(objGcInfo);234235JavaCalls::call_virtual(&result,236gc_mbean_klass,237vmSymbols::createGCNotification_name(),238vmSymbols::createGCNotification_signature(),239&args,240CHECK);241}242}243244245246