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