src/share/vm/services/gcNotifier.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/services/gcNotifier.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,244 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "classfile/systemDictionary.hpp"
    1.30 +#include "classfile/vmSymbols.hpp"
    1.31 +#include "oops/oop.inline.hpp"
    1.32 +#include "runtime/interfaceSupport.hpp"
    1.33 +#include "runtime/java.hpp"
    1.34 +#include "runtime/javaCalls.hpp"
    1.35 +#include "runtime/mutex.hpp"
    1.36 +#include "runtime/mutexLocker.hpp"
    1.37 +#include "services/gcNotifier.hpp"
    1.38 +#include "services/management.hpp"
    1.39 +#include "services/memoryService.hpp"
    1.40 +#include "memoryManager.hpp"
    1.41 +#include "memory/oopFactory.hpp"
    1.42 +
    1.43 +GCNotificationRequest *GCNotifier::first_request = NULL;
    1.44 +GCNotificationRequest *GCNotifier::last_request = NULL;
    1.45 +
    1.46 +void GCNotifier::pushNotification(GCMemoryManager *mgr, const char *action, const char *cause) {
    1.47 +  // Make a copy of the last GC statistics
    1.48 +  // GC may occur between now and the creation of the notification
    1.49 +  int num_pools = MemoryService::num_memory_pools();
    1.50 +  // stat is deallocated inside GCNotificationRequest
    1.51 +  GCStatInfo* stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(num_pools);
    1.52 +  mgr->get_last_gc_stat(stat);
    1.53 +  GCNotificationRequest *request = new GCNotificationRequest(os::javaTimeMillis(),mgr,action,cause,stat);
    1.54 +  addRequest(request);
    1.55 + }
    1.56 +
    1.57 +void GCNotifier::addRequest(GCNotificationRequest *request) {
    1.58 +  MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
    1.59 +  if(first_request == NULL) {
    1.60 +    first_request = request;
    1.61 +  } else {
    1.62 +    last_request->next = request;
    1.63 +  }
    1.64 +  last_request = request;
    1.65 +  Service_lock->notify_all();
    1.66 +}
    1.67 +
    1.68 +GCNotificationRequest *GCNotifier::getRequest() {
    1.69 +  MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
    1.70 +  GCNotificationRequest *request = first_request;
    1.71 +  if(first_request != NULL) {
    1.72 +    first_request = first_request->next;
    1.73 +  }
    1.74 +  return request;
    1.75 +}
    1.76 +
    1.77 +bool GCNotifier::has_event() {
    1.78 +  return first_request != NULL;
    1.79 +}
    1.80 +
    1.81 +static Handle getGcInfoBuilder(GCMemoryManager *gcManager,TRAPS) {
    1.82 +
    1.83 +  Klass* k = Management::sun_management_GarbageCollectorImpl_klass(CHECK_NH);
    1.84 +  instanceKlassHandle gcMBeanKlass (THREAD, k);
    1.85 +
    1.86 +  instanceOop i = gcManager->get_memory_manager_instance(THREAD);
    1.87 +  instanceHandle ih(THREAD, i);
    1.88 +
    1.89 +  JavaValue result(T_OBJECT);
    1.90 +  JavaCallArguments args(ih);
    1.91 +
    1.92 +  JavaCalls::call_virtual(&result,
    1.93 +                          gcMBeanKlass,
    1.94 +                          vmSymbols::getGcInfoBuilder_name(),
    1.95 +                          vmSymbols::getGcInfoBuilder_signature(),
    1.96 +                          &args,
    1.97 +                          CHECK_NH);
    1.98 +  return Handle(THREAD,(oop)result.get_jobject());
    1.99 +}
   1.100 +
   1.101 +static Handle createGcInfo(GCMemoryManager *gcManager, GCStatInfo *gcStatInfo,TRAPS) {
   1.102 +
   1.103 +  // Fill the arrays of MemoryUsage objects with before and after GC
   1.104 +  // per pool memory usage
   1.105 +
   1.106 +  Klass* mu_klass = Management::java_lang_management_MemoryUsage_klass(CHECK_NH);
   1.107 +  instanceKlassHandle mu_kh(THREAD, mu_klass);
   1.108 +
   1.109 +  // The array allocations below should use a handle containing mu_klass
   1.110 +  // as the first allocation could trigger a GC, causing the actual
   1.111 +  // klass oop to move, and leaving mu_klass pointing to the old
   1.112 +  // location.
   1.113 +  objArrayOop bu = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH);
   1.114 +  objArrayHandle usage_before_gc_ah(THREAD, bu);
   1.115 +  objArrayOop au = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH);
   1.116 +  objArrayHandle usage_after_gc_ah(THREAD, au);
   1.117 +
   1.118 +  for (int i = 0; i < MemoryService::num_memory_pools(); i++) {
   1.119 +    Handle before_usage = MemoryService::create_MemoryUsage_obj(gcStatInfo->before_gc_usage_for_pool(i), CHECK_NH);
   1.120 +    Handle after_usage;
   1.121 +
   1.122 +    MemoryUsage u = gcStatInfo->after_gc_usage_for_pool(i);
   1.123 +    if (u.max_size() == 0 && u.used() > 0) {
   1.124 +      // If max size == 0, this pool is a survivor space.
   1.125 +      // Set max size = -1 since the pools will be swapped after GC.
   1.126 +      MemoryUsage usage(u.init_size(), u.used(), u.committed(), (size_t)-1);
   1.127 +      after_usage = MemoryService::create_MemoryUsage_obj(usage, CHECK_NH);
   1.128 +    } else {
   1.129 +        after_usage = MemoryService::create_MemoryUsage_obj(u, CHECK_NH);
   1.130 +    }
   1.131 +    usage_before_gc_ah->obj_at_put(i, before_usage());
   1.132 +    usage_after_gc_ah->obj_at_put(i, after_usage());
   1.133 +  }
   1.134 +
   1.135 +  // Current implementation only has 1 attribute (number of GC threads)
   1.136 +  // The type is 'I'
   1.137 +  objArrayOop extra_args_array = oopFactory::new_objArray(SystemDictionary::Integer_klass(), 1, CHECK_NH);
   1.138 +  objArrayHandle extra_array (THREAD, extra_args_array);
   1.139 +  Klass* itKlass = SystemDictionary::Integer_klass();
   1.140 +  instanceKlassHandle intK(THREAD, itKlass);
   1.141 +
   1.142 +  instanceHandle extra_arg_val = intK->allocate_instance_handle(CHECK_NH);
   1.143 +
   1.144 +  {
   1.145 +    JavaValue res(T_VOID);
   1.146 +    JavaCallArguments argsInt;
   1.147 +    argsInt.push_oop(extra_arg_val);
   1.148 +    argsInt.push_int(gcManager->num_gc_threads());
   1.149 +
   1.150 +    JavaCalls::call_special(&res,
   1.151 +                            intK,
   1.152 +                            vmSymbols::object_initializer_name(),
   1.153 +                            vmSymbols::int_void_signature(),
   1.154 +                            &argsInt,
   1.155 +                            CHECK_NH);
   1.156 +  }
   1.157 +  extra_array->obj_at_put(0,extra_arg_val());
   1.158 +
   1.159 +  Klass* gcInfoklass = Management::com_sun_management_GcInfo_klass(CHECK_NH);
   1.160 +  instanceKlassHandle ik(THREAD, gcInfoklass);
   1.161 +
   1.162 +  Handle gcInfo_instance = ik->allocate_instance_handle(CHECK_NH);
   1.163 +
   1.164 +  JavaValue constructor_result(T_VOID);
   1.165 +  JavaCallArguments constructor_args(16);
   1.166 +  constructor_args.push_oop(gcInfo_instance);
   1.167 +  constructor_args.push_oop(getGcInfoBuilder(gcManager,THREAD));
   1.168 +  constructor_args.push_long(gcStatInfo->gc_index());
   1.169 +  constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->start_time()));
   1.170 +  constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->end_time()));
   1.171 +  constructor_args.push_oop(usage_before_gc_ah);
   1.172 +  constructor_args.push_oop(usage_after_gc_ah);
   1.173 +  constructor_args.push_oop(extra_array);
   1.174 +
   1.175 +  JavaCalls::call_special(&constructor_result,
   1.176 +                          ik,
   1.177 +                          vmSymbols::object_initializer_name(),
   1.178 +                          vmSymbols::com_sun_management_GcInfo_constructor_signature(),
   1.179 +                          &constructor_args,
   1.180 +                          CHECK_NH);
   1.181 +
   1.182 +  return Handle(gcInfo_instance());
   1.183 +}
   1.184 +
   1.185 +void GCNotifier::sendNotification(TRAPS) {
   1.186 +  GCNotifier::sendNotificationInternal(THREAD);
   1.187 +  // Clearing pending exception to avoid premature termination of
   1.188 +  // the service thread
   1.189 +  if (HAS_PENDING_EXCEPTION) {
   1.190 +    CLEAR_PENDING_EXCEPTION;
   1.191 +  }
   1.192 +}
   1.193 +
   1.194 +class NotificationMark : public StackObj {
   1.195 +  // This class is used in GCNotifier::sendNotificationInternal to ensure that
   1.196 +  // the GCNotificationRequest object is properly cleaned up, whatever path
   1.197 +  // is used to exit the method.
   1.198 +  GCNotificationRequest* _request;
   1.199 +public:
   1.200 +  NotificationMark(GCNotificationRequest* r) {
   1.201 +    _request = r;
   1.202 +  }
   1.203 +  ~NotificationMark() {
   1.204 +    assert(_request != NULL, "Sanity check");
   1.205 +    delete _request;
   1.206 +  }
   1.207 +};
   1.208 +
   1.209 +void GCNotifier::sendNotificationInternal(TRAPS) {
   1.210 +  ResourceMark rm(THREAD);
   1.211 +  HandleMark hm(THREAD);
   1.212 +  GCNotificationRequest *request = getRequest();
   1.213 +  if (request != NULL) {
   1.214 +    NotificationMark nm(request);
   1.215 +    Handle objGcInfo = createGcInfo(request->gcManager, request->gcStatInfo, CHECK);
   1.216 +
   1.217 +    Handle objName = java_lang_String::create_from_str(request->gcManager->name(), CHECK);
   1.218 +    Handle objAction = java_lang_String::create_from_str(request->gcAction, CHECK);
   1.219 +    Handle objCause = java_lang_String::create_from_str(request->gcCause, CHECK);
   1.220 +
   1.221 +    Klass* k = Management::sun_management_GarbageCollectorImpl_klass(CHECK);
   1.222 +    instanceKlassHandle gc_mbean_klass(THREAD, k);
   1.223 +
   1.224 +    instanceOop gc_mbean = request->gcManager->get_memory_manager_instance(THREAD);
   1.225 +    instanceHandle gc_mbean_h(THREAD, gc_mbean);
   1.226 +    if (!gc_mbean_h->is_a(k)) {
   1.227 +      THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
   1.228 +                "This GCMemoryManager doesn't have a GarbageCollectorMXBean");
   1.229 +    }
   1.230 +
   1.231 +    JavaValue result(T_VOID);
   1.232 +    JavaCallArguments args(gc_mbean_h);
   1.233 +    args.push_long(request->timestamp);
   1.234 +    args.push_oop(objName);
   1.235 +    args.push_oop(objAction);
   1.236 +    args.push_oop(objCause);
   1.237 +    args.push_oop(objGcInfo);
   1.238 +
   1.239 +    JavaCalls::call_virtual(&result,
   1.240 +                            gc_mbean_klass,
   1.241 +                            vmSymbols::createGCNotification_name(),
   1.242 +                            vmSymbols::createGCNotification_signature(),
   1.243 +                            &args,
   1.244 +                            CHECK);
   1.245 +  }
   1.246 +}
   1.247 +

mercurial