Path: blob/master/src/hotspot/share/services/gcNotifier.cpp
41144 views
/*1* Copyright (c) 2011, 2021, 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/javaClasses.hpp"26#include "classfile/vmClasses.hpp"27#include "classfile/vmSymbols.hpp"28#include "oops/objArrayOop.inline.hpp"29#include "oops/oop.inline.hpp"30#include "runtime/handles.inline.hpp"31#include "runtime/java.hpp"32#include "runtime/javaCalls.hpp"33#include "runtime/mutex.hpp"34#include "runtime/mutexLocker.hpp"35#include "services/gcNotifier.hpp"36#include "services/management.hpp"37#include "services/memoryService.hpp"38#include "memoryManager.hpp"39#include "memory/oopFactory.hpp"40#include "memory/resourceArea.hpp"4142GCNotificationRequest *GCNotifier::first_request = NULL;43GCNotificationRequest *GCNotifier::last_request = NULL;4445void GCNotifier::pushNotification(GCMemoryManager *mgr, const char *action, const char *cause) {46// Make a copy of the last GC statistics47// GC may occur between now and the creation of the notification48int num_pools = MemoryService::num_memory_pools();49// stat is deallocated inside GCNotificationRequest50GCStatInfo* stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(num_pools);51mgr->get_last_gc_stat(stat);52// timestamp is current time in ms53GCNotificationRequest *request = new GCNotificationRequest(os::javaTimeMillis(),mgr,action,cause,stat);54addRequest(request);55}5657void GCNotifier::addRequest(GCNotificationRequest *request) {58MutexLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);59if(first_request == NULL) {60first_request = request;61} else {62last_request->next = request;63}64last_request = request;65Notification_lock->notify_all();66}6768GCNotificationRequest *GCNotifier::getRequest() {69MutexLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);70GCNotificationRequest *request = first_request;71if(first_request != NULL) {72first_request = first_request->next;73}74return request;75}7677bool GCNotifier::has_event() {78return first_request != NULL;79}8081static Handle getGcInfoBuilder(GCMemoryManager *gcManager,TRAPS) {8283Klass* gcMBeanKlass = Management::com_sun_management_internal_GarbageCollectorExtImpl_klass(CHECK_NH);8485instanceOop i = gcManager->get_memory_manager_instance(THREAD);86instanceHandle ih(THREAD, i);8788JavaValue result(T_OBJECT);89JavaCallArguments args(ih);9091JavaCalls::call_virtual(&result,92gcMBeanKlass,93vmSymbols::getGcInfoBuilder_name(),94vmSymbols::getGcInfoBuilder_signature(),95&args,96CHECK_NH);97return Handle(THREAD, result.get_oop());98}99100static Handle createGcInfo(GCMemoryManager *gcManager, GCStatInfo *gcStatInfo,TRAPS) {101102// Fill the arrays of MemoryUsage objects with before and after GC103// per pool memory usage104105InstanceKlass* mu_klass = Management::java_lang_management_MemoryUsage_klass(CHECK_NH);106107// The array allocations below should use a handle containing mu_klass108// as the first allocation could trigger a GC, causing the actual109// klass oop to move, and leaving mu_klass pointing to the old110// location.111objArrayOop bu = oopFactory::new_objArray(mu_klass, MemoryService::num_memory_pools(), CHECK_NH);112objArrayHandle usage_before_gc_ah(THREAD, bu);113objArrayOop au = oopFactory::new_objArray(mu_klass, MemoryService::num_memory_pools(), CHECK_NH);114objArrayHandle usage_after_gc_ah(THREAD, au);115116for (int i = 0; i < MemoryService::num_memory_pools(); i++) {117Handle before_usage = MemoryService::create_MemoryUsage_obj(gcStatInfo->before_gc_usage_for_pool(i), CHECK_NH);118Handle after_usage;119120MemoryUsage u = gcStatInfo->after_gc_usage_for_pool(i);121if (u.max_size() == 0 && u.used() > 0) {122// If max size == 0, this pool is a survivor space.123// Set max size = -1 since the pools will be swapped after GC.124MemoryUsage usage(u.init_size(), u.used(), u.committed(), (size_t)-1);125after_usage = MemoryService::create_MemoryUsage_obj(usage, CHECK_NH);126} else {127after_usage = MemoryService::create_MemoryUsage_obj(u, CHECK_NH);128}129usage_before_gc_ah->obj_at_put(i, before_usage());130usage_after_gc_ah->obj_at_put(i, after_usage());131}132133// Current implementation only has 1 attribute (number of GC threads)134// The type is 'I'135objArrayOop extra_args_array = oopFactory::new_objArray(vmClasses::Integer_klass(), 1, CHECK_NH);136objArrayHandle extra_array (THREAD, extra_args_array);137138JavaCallArguments argsInt;139argsInt.push_int(gcManager->num_gc_threads());140Handle extra_arg_val = JavaCalls::construct_new_instance(141vmClasses::Integer_klass(),142vmSymbols::int_void_signature(),143&argsInt,144CHECK_NH);145146extra_array->obj_at_put(0,extra_arg_val());147148InstanceKlass* gcInfoklass = Management::com_sun_management_GcInfo_klass(CHECK_NH);149150JavaCallArguments constructor_args(16);151constructor_args.push_oop(getGcInfoBuilder(gcManager,THREAD));152constructor_args.push_long(gcStatInfo->gc_index());153constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->start_time()));154constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->end_time()));155constructor_args.push_oop(usage_before_gc_ah);156constructor_args.push_oop(usage_after_gc_ah);157constructor_args.push_oop(extra_array);158159return JavaCalls::construct_new_instance(160gcInfoklass,161vmSymbols::com_sun_management_GcInfo_constructor_signature(),162&constructor_args,163THREAD);164}165166void GCNotifier::sendNotification(TRAPS) {167GCNotifier::sendNotificationInternal(THREAD);168// Clearing pending exception to avoid premature termination of169// the service thread170if (HAS_PENDING_EXCEPTION) {171CLEAR_PENDING_EXCEPTION;172}173}174175class NotificationMark : public StackObj {176// This class is used in GCNotifier::sendNotificationInternal to ensure that177// the GCNotificationRequest object is properly cleaned up, whatever path178// is used to exit the method.179GCNotificationRequest* _request;180public:181NotificationMark(GCNotificationRequest* r) {182_request = r;183}184~NotificationMark() {185assert(_request != NULL, "Sanity check");186delete _request;187}188};189190void GCNotifier::sendNotificationInternal(TRAPS) {191ResourceMark rm(THREAD);192HandleMark hm(THREAD);193GCNotificationRequest *request = getRequest();194if (request != NULL) {195NotificationMark nm(request);196Handle objGcInfo = createGcInfo(request->gcManager, request->gcStatInfo, CHECK);197198Handle objName = java_lang_String::create_from_str(request->gcManager->name(), CHECK);199Handle objAction = java_lang_String::create_from_str(request->gcAction, CHECK);200Handle objCause = java_lang_String::create_from_str(request->gcCause, CHECK);201InstanceKlass* gc_mbean_klass = Management::com_sun_management_internal_GarbageCollectorExtImpl_klass(CHECK);202203instanceOop gc_mbean = request->gcManager->get_memory_manager_instance(THREAD);204instanceHandle gc_mbean_h(THREAD, gc_mbean);205if (!gc_mbean_h->is_a(gc_mbean_klass)) {206THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),207"This GCMemoryManager doesn't have a GarbageCollectorMXBean");208}209210JavaValue result(T_VOID);211JavaCallArguments args(gc_mbean_h);212args.push_long(request->timestamp);213args.push_oop(objName);214args.push_oop(objAction);215args.push_oop(objCause);216args.push_oop(objGcInfo);217218JavaCalls::call_virtual(&result,219gc_mbean_klass,220vmSymbols::createGCNotification_name(),221vmSymbols::createGCNotification_signature(),222&args,223CHECK);224}225}226227228229