src/share/vm/services/gcNotifier.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 5668
592520c14121
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/systemDictionary.hpp"
aoqi@0 27 #include "classfile/vmSymbols.hpp"
aoqi@0 28 #include "oops/oop.inline.hpp"
aoqi@0 29 #include "runtime/interfaceSupport.hpp"
aoqi@0 30 #include "runtime/java.hpp"
aoqi@0 31 #include "runtime/javaCalls.hpp"
aoqi@0 32 #include "runtime/mutex.hpp"
aoqi@0 33 #include "runtime/mutexLocker.hpp"
aoqi@0 34 #include "services/gcNotifier.hpp"
aoqi@0 35 #include "services/management.hpp"
aoqi@0 36 #include "services/memoryService.hpp"
aoqi@0 37 #include "memoryManager.hpp"
aoqi@0 38 #include "memory/oopFactory.hpp"
aoqi@0 39
aoqi@0 40 GCNotificationRequest *GCNotifier::first_request = NULL;
aoqi@0 41 GCNotificationRequest *GCNotifier::last_request = NULL;
aoqi@0 42
aoqi@0 43 void GCNotifier::pushNotification(GCMemoryManager *mgr, const char *action, const char *cause) {
aoqi@0 44 // Make a copy of the last GC statistics
aoqi@0 45 // GC may occur between now and the creation of the notification
aoqi@0 46 int num_pools = MemoryService::num_memory_pools();
aoqi@0 47 // stat is deallocated inside GCNotificationRequest
aoqi@0 48 GCStatInfo* stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(num_pools);
aoqi@0 49 mgr->get_last_gc_stat(stat);
aoqi@0 50 GCNotificationRequest *request = new GCNotificationRequest(os::javaTimeMillis(),mgr,action,cause,stat);
aoqi@0 51 addRequest(request);
aoqi@0 52 }
aoqi@0 53
aoqi@0 54 void GCNotifier::addRequest(GCNotificationRequest *request) {
aoqi@0 55 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 56 if(first_request == NULL) {
aoqi@0 57 first_request = request;
aoqi@0 58 } else {
aoqi@0 59 last_request->next = request;
aoqi@0 60 }
aoqi@0 61 last_request = request;
aoqi@0 62 Service_lock->notify_all();
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 GCNotificationRequest *GCNotifier::getRequest() {
aoqi@0 66 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 67 GCNotificationRequest *request = first_request;
aoqi@0 68 if(first_request != NULL) {
aoqi@0 69 first_request = first_request->next;
aoqi@0 70 }
aoqi@0 71 return request;
aoqi@0 72 }
aoqi@0 73
aoqi@0 74 bool GCNotifier::has_event() {
aoqi@0 75 return first_request != NULL;
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 static Handle getGcInfoBuilder(GCMemoryManager *gcManager,TRAPS) {
aoqi@0 79
aoqi@0 80 Klass* k = Management::sun_management_GarbageCollectorImpl_klass(CHECK_NH);
aoqi@0 81 instanceKlassHandle gcMBeanKlass (THREAD, k);
aoqi@0 82
aoqi@0 83 instanceOop i = gcManager->get_memory_manager_instance(THREAD);
aoqi@0 84 instanceHandle ih(THREAD, i);
aoqi@0 85
aoqi@0 86 JavaValue result(T_OBJECT);
aoqi@0 87 JavaCallArguments args(ih);
aoqi@0 88
aoqi@0 89 JavaCalls::call_virtual(&result,
aoqi@0 90 gcMBeanKlass,
aoqi@0 91 vmSymbols::getGcInfoBuilder_name(),
aoqi@0 92 vmSymbols::getGcInfoBuilder_signature(),
aoqi@0 93 &args,
aoqi@0 94 CHECK_NH);
aoqi@0 95 return Handle(THREAD,(oop)result.get_jobject());
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 static Handle createGcInfo(GCMemoryManager *gcManager, GCStatInfo *gcStatInfo,TRAPS) {
aoqi@0 99
aoqi@0 100 // Fill the arrays of MemoryUsage objects with before and after GC
aoqi@0 101 // per pool memory usage
aoqi@0 102
aoqi@0 103 Klass* mu_klass = Management::java_lang_management_MemoryUsage_klass(CHECK_NH);
aoqi@0 104 instanceKlassHandle mu_kh(THREAD, mu_klass);
aoqi@0 105
aoqi@0 106 // The array allocations below should use a handle containing mu_klass
aoqi@0 107 // as the first allocation could trigger a GC, causing the actual
aoqi@0 108 // klass oop to move, and leaving mu_klass pointing to the old
aoqi@0 109 // location.
aoqi@0 110 objArrayOop bu = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH);
aoqi@0 111 objArrayHandle usage_before_gc_ah(THREAD, bu);
aoqi@0 112 objArrayOop au = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH);
aoqi@0 113 objArrayHandle usage_after_gc_ah(THREAD, au);
aoqi@0 114
aoqi@0 115 for (int i = 0; i < MemoryService::num_memory_pools(); i++) {
aoqi@0 116 Handle before_usage = MemoryService::create_MemoryUsage_obj(gcStatInfo->before_gc_usage_for_pool(i), CHECK_NH);
aoqi@0 117 Handle after_usage;
aoqi@0 118
aoqi@0 119 MemoryUsage u = gcStatInfo->after_gc_usage_for_pool(i);
aoqi@0 120 if (u.max_size() == 0 && u.used() > 0) {
aoqi@0 121 // If max size == 0, this pool is a survivor space.
aoqi@0 122 // Set max size = -1 since the pools will be swapped after GC.
aoqi@0 123 MemoryUsage usage(u.init_size(), u.used(), u.committed(), (size_t)-1);
aoqi@0 124 after_usage = MemoryService::create_MemoryUsage_obj(usage, CHECK_NH);
aoqi@0 125 } else {
aoqi@0 126 after_usage = MemoryService::create_MemoryUsage_obj(u, CHECK_NH);
aoqi@0 127 }
aoqi@0 128 usage_before_gc_ah->obj_at_put(i, before_usage());
aoqi@0 129 usage_after_gc_ah->obj_at_put(i, after_usage());
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 // Current implementation only has 1 attribute (number of GC threads)
aoqi@0 133 // The type is 'I'
aoqi@0 134 objArrayOop extra_args_array = oopFactory::new_objArray(SystemDictionary::Integer_klass(), 1, CHECK_NH);
aoqi@0 135 objArrayHandle extra_array (THREAD, extra_args_array);
aoqi@0 136 Klass* itKlass = SystemDictionary::Integer_klass();
aoqi@0 137 instanceKlassHandle intK(THREAD, itKlass);
aoqi@0 138
aoqi@0 139 instanceHandle extra_arg_val = intK->allocate_instance_handle(CHECK_NH);
aoqi@0 140
aoqi@0 141 {
aoqi@0 142 JavaValue res(T_VOID);
aoqi@0 143 JavaCallArguments argsInt;
aoqi@0 144 argsInt.push_oop(extra_arg_val);
aoqi@0 145 argsInt.push_int(gcManager->num_gc_threads());
aoqi@0 146
aoqi@0 147 JavaCalls::call_special(&res,
aoqi@0 148 intK,
aoqi@0 149 vmSymbols::object_initializer_name(),
aoqi@0 150 vmSymbols::int_void_signature(),
aoqi@0 151 &argsInt,
aoqi@0 152 CHECK_NH);
aoqi@0 153 }
aoqi@0 154 extra_array->obj_at_put(0,extra_arg_val());
aoqi@0 155
aoqi@0 156 Klass* gcInfoklass = Management::com_sun_management_GcInfo_klass(CHECK_NH);
aoqi@0 157 instanceKlassHandle ik(THREAD, gcInfoklass);
aoqi@0 158
aoqi@0 159 Handle gcInfo_instance = ik->allocate_instance_handle(CHECK_NH);
aoqi@0 160
aoqi@0 161 JavaValue constructor_result(T_VOID);
aoqi@0 162 JavaCallArguments constructor_args(16);
aoqi@0 163 constructor_args.push_oop(gcInfo_instance);
aoqi@0 164 constructor_args.push_oop(getGcInfoBuilder(gcManager,THREAD));
aoqi@0 165 constructor_args.push_long(gcStatInfo->gc_index());
aoqi@0 166 constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->start_time()));
aoqi@0 167 constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->end_time()));
aoqi@0 168 constructor_args.push_oop(usage_before_gc_ah);
aoqi@0 169 constructor_args.push_oop(usage_after_gc_ah);
aoqi@0 170 constructor_args.push_oop(extra_array);
aoqi@0 171
aoqi@0 172 JavaCalls::call_special(&constructor_result,
aoqi@0 173 ik,
aoqi@0 174 vmSymbols::object_initializer_name(),
aoqi@0 175 vmSymbols::com_sun_management_GcInfo_constructor_signature(),
aoqi@0 176 &constructor_args,
aoqi@0 177 CHECK_NH);
aoqi@0 178
aoqi@0 179 return Handle(gcInfo_instance());
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 void GCNotifier::sendNotification(TRAPS) {
aoqi@0 183 GCNotifier::sendNotificationInternal(THREAD);
aoqi@0 184 // Clearing pending exception to avoid premature termination of
aoqi@0 185 // the service thread
aoqi@0 186 if (HAS_PENDING_EXCEPTION) {
aoqi@0 187 CLEAR_PENDING_EXCEPTION;
aoqi@0 188 }
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 class NotificationMark : public StackObj {
aoqi@0 192 // This class is used in GCNotifier::sendNotificationInternal to ensure that
aoqi@0 193 // the GCNotificationRequest object is properly cleaned up, whatever path
aoqi@0 194 // is used to exit the method.
aoqi@0 195 GCNotificationRequest* _request;
aoqi@0 196 public:
aoqi@0 197 NotificationMark(GCNotificationRequest* r) {
aoqi@0 198 _request = r;
aoqi@0 199 }
aoqi@0 200 ~NotificationMark() {
aoqi@0 201 assert(_request != NULL, "Sanity check");
aoqi@0 202 delete _request;
aoqi@0 203 }
aoqi@0 204 };
aoqi@0 205
aoqi@0 206 void GCNotifier::sendNotificationInternal(TRAPS) {
aoqi@0 207 ResourceMark rm(THREAD);
aoqi@0 208 HandleMark hm(THREAD);
aoqi@0 209 GCNotificationRequest *request = getRequest();
aoqi@0 210 if (request != NULL) {
aoqi@0 211 NotificationMark nm(request);
aoqi@0 212 Handle objGcInfo = createGcInfo(request->gcManager, request->gcStatInfo, CHECK);
aoqi@0 213
aoqi@0 214 Handle objName = java_lang_String::create_from_str(request->gcManager->name(), CHECK);
aoqi@0 215 Handle objAction = java_lang_String::create_from_str(request->gcAction, CHECK);
aoqi@0 216 Handle objCause = java_lang_String::create_from_str(request->gcCause, CHECK);
aoqi@0 217
aoqi@0 218 Klass* k = Management::sun_management_GarbageCollectorImpl_klass(CHECK);
aoqi@0 219 instanceKlassHandle gc_mbean_klass(THREAD, k);
aoqi@0 220
aoqi@0 221 instanceOop gc_mbean = request->gcManager->get_memory_manager_instance(THREAD);
aoqi@0 222 instanceHandle gc_mbean_h(THREAD, gc_mbean);
aoqi@0 223 if (!gc_mbean_h->is_a(k)) {
aoqi@0 224 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
aoqi@0 225 "This GCMemoryManager doesn't have a GarbageCollectorMXBean");
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 JavaValue result(T_VOID);
aoqi@0 229 JavaCallArguments args(gc_mbean_h);
aoqi@0 230 args.push_long(request->timestamp);
aoqi@0 231 args.push_oop(objName);
aoqi@0 232 args.push_oop(objAction);
aoqi@0 233 args.push_oop(objCause);
aoqi@0 234 args.push_oop(objGcInfo);
aoqi@0 235
aoqi@0 236 JavaCalls::call_virtual(&result,
aoqi@0 237 gc_mbean_klass,
aoqi@0 238 vmSymbols::createGCNotification_name(),
aoqi@0 239 vmSymbols::createGCNotification_signature(),
aoqi@0 240 &args,
aoqi@0 241 CHECK);
aoqi@0 242 }
aoqi@0 243 }
aoqi@0 244

mercurial