| 1 | /* | 
|---|
| 2 | * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. | 
|---|
| 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" | 
|---|
| 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" | 
|---|
| 41 |  | 
|---|
| 42 | GCNotificationRequest *GCNotifier::first_request = NULL; | 
|---|
| 43 | GCNotificationRequest *GCNotifier::last_request = NULL; | 
|---|
| 44 |  | 
|---|
| 45 | void GCNotifier::pushNotification(GCMemoryManager *mgr, const char *action, const char *cause) { | 
|---|
| 46 | // Make a copy of the last GC statistics | 
|---|
| 47 | // GC may occur between now and the creation of the notification | 
|---|
| 48 | int num_pools = MemoryService::num_memory_pools(); | 
|---|
| 49 | // stat is deallocated inside GCNotificationRequest | 
|---|
| 50 | GCStatInfo* stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(num_pools); | 
|---|
| 51 | mgr->get_last_gc_stat(stat); | 
|---|
| 52 | GCNotificationRequest *request = new GCNotificationRequest(os::javaTimeMillis(),mgr,action,cause,stat); | 
|---|
| 53 | addRequest(request); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | void GCNotifier::addRequest(GCNotificationRequest *request) { | 
|---|
| 57 | MutexLocker ml(Service_lock, Mutex::_no_safepoint_check_flag); | 
|---|
| 58 | if(first_request == NULL) { | 
|---|
| 59 | first_request = request; | 
|---|
| 60 | } else { | 
|---|
| 61 | last_request->next = request; | 
|---|
| 62 | } | 
|---|
| 63 | last_request = request; | 
|---|
| 64 | Service_lock->notify_all(); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | GCNotificationRequest *GCNotifier::getRequest() { | 
|---|
| 68 | MutexLocker ml(Service_lock, Mutex::_no_safepoint_check_flag); | 
|---|
| 69 | GCNotificationRequest *request = first_request; | 
|---|
| 70 | if(first_request != NULL) { | 
|---|
| 71 | first_request = first_request->next; | 
|---|
| 72 | } | 
|---|
| 73 | return request; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | bool GCNotifier::has_event() { | 
|---|
| 77 | return first_request != NULL; | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | static Handle getGcInfoBuilder(GCMemoryManager *gcManager,TRAPS) { | 
|---|
| 81 |  | 
|---|
| 82 | Klass* gcMBeanKlass = Management::com_sun_management_internal_GarbageCollectorExtImpl_klass(CHECK_NH); | 
|---|
| 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()); | 
|---|
| 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 |  | 
|---|
| 104 | InstanceKlass* mu_klass = Management::java_lang_management_MemoryUsage_klass(CHECK_NH); | 
|---|
| 105 |  | 
|---|
| 106 | // The array allocations below should use a handle containing mu_klass | 
|---|
| 107 | // as the first allocation could trigger a GC, causing the actual | 
|---|
| 108 | // klass oop to move, and leaving mu_klass pointing to the old | 
|---|
| 109 | // location. | 
|---|
| 110 | objArrayOop bu = oopFactory::new_objArray(mu_klass, MemoryService::num_memory_pools(), CHECK_NH); | 
|---|
| 111 | objArrayHandle usage_before_gc_ah(THREAD, bu); | 
|---|
| 112 | objArrayOop au = oopFactory::new_objArray(mu_klass, MemoryService::num_memory_pools(), CHECK_NH); | 
|---|
| 113 | objArrayHandle usage_after_gc_ah(THREAD, au); | 
|---|
| 114 |  | 
|---|
| 115 | for (int i = 0; i < MemoryService::num_memory_pools(); i++) { | 
|---|
| 116 | Handle before_usage = MemoryService::create_MemoryUsage_obj(gcStatInfo->before_gc_usage_for_pool(i), CHECK_NH); | 
|---|
| 117 | Handle after_usage; | 
|---|
| 118 |  | 
|---|
| 119 | MemoryUsage u = gcStatInfo->after_gc_usage_for_pool(i); | 
|---|
| 120 | if (u.max_size() == 0 && u.used() > 0) { | 
|---|
| 121 | // If max size == 0, this pool is a survivor space. | 
|---|
| 122 | // Set max size = -1 since the pools will be swapped after GC. | 
|---|
| 123 | MemoryUsage usage(u.init_size(), u.used(), u.committed(), (size_t)-1); | 
|---|
| 124 | after_usage = MemoryService::create_MemoryUsage_obj(usage, CHECK_NH); | 
|---|
| 125 | } else { | 
|---|
| 126 | after_usage = MemoryService::create_MemoryUsage_obj(u, CHECK_NH); | 
|---|
| 127 | } | 
|---|
| 128 | usage_before_gc_ah->obj_at_put(i, before_usage()); | 
|---|
| 129 | usage_after_gc_ah->obj_at_put(i, after_usage()); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | // Current implementation only has 1 attribute (number of GC threads) | 
|---|
| 133 | // The type is 'I' | 
|---|
| 134 | objArrayOop  = oopFactory::new_objArray(SystemDictionary::Integer_klass(), 1, CHECK_NH); | 
|---|
| 135 | objArrayHandle  (THREAD, extra_args_array); | 
|---|
| 136 |  | 
|---|
| 137 | JavaCallArguments argsInt; | 
|---|
| 138 | argsInt.push_int(gcManager->num_gc_threads()); | 
|---|
| 139 | Handle  = JavaCalls::construct_new_instance( | 
|---|
| 140 | SystemDictionary::Integer_klass(), | 
|---|
| 141 | vmSymbols::int_void_signature(), | 
|---|
| 142 | &argsInt, | 
|---|
| 143 | CHECK_NH); | 
|---|
| 144 |  | 
|---|
| 145 | extra_array->obj_at_put(0,extra_arg_val()); | 
|---|
| 146 |  | 
|---|
| 147 | InstanceKlass* gcInfoklass = Management::com_sun_management_GcInfo_klass(CHECK_NH); | 
|---|
| 148 |  | 
|---|
| 149 | JavaCallArguments constructor_args(16); | 
|---|
| 150 | constructor_args.push_oop(getGcInfoBuilder(gcManager,THREAD)); | 
|---|
| 151 | constructor_args.push_long(gcStatInfo->gc_index()); | 
|---|
| 152 | constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->start_time())); | 
|---|
| 153 | constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->end_time())); | 
|---|
| 154 | constructor_args.push_oop(usage_before_gc_ah); | 
|---|
| 155 | constructor_args.push_oop(usage_after_gc_ah); | 
|---|
| 156 | constructor_args.push_oop(extra_array); | 
|---|
| 157 |  | 
|---|
| 158 | return JavaCalls::construct_new_instance( | 
|---|
| 159 | gcInfoklass, | 
|---|
| 160 | vmSymbols::com_sun_management_GcInfo_constructor_signature(), | 
|---|
| 161 | &constructor_args, | 
|---|
| 162 | CHECK_NH); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | void GCNotifier::sendNotification(TRAPS) { | 
|---|
| 166 | GCNotifier::sendNotificationInternal(THREAD); | 
|---|
| 167 | // Clearing pending exception to avoid premature termination of | 
|---|
| 168 | // the service thread | 
|---|
| 169 | if (HAS_PENDING_EXCEPTION) { | 
|---|
| 170 | CLEAR_PENDING_EXCEPTION; | 
|---|
| 171 | } | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | class NotificationMark : public StackObj { | 
|---|
| 175 | // This class is used in GCNotifier::sendNotificationInternal to ensure that | 
|---|
| 176 | // the GCNotificationRequest object is properly cleaned up, whatever path | 
|---|
| 177 | // is used to exit the method. | 
|---|
| 178 | GCNotificationRequest* _request; | 
|---|
| 179 | public: | 
|---|
| 180 | NotificationMark(GCNotificationRequest* r) { | 
|---|
| 181 | _request = r; | 
|---|
| 182 | } | 
|---|
| 183 | ~NotificationMark() { | 
|---|
| 184 | assert(_request != NULL, "Sanity check"); | 
|---|
| 185 | delete _request; | 
|---|
| 186 | } | 
|---|
| 187 | }; | 
|---|
| 188 |  | 
|---|
| 189 | void GCNotifier::sendNotificationInternal(TRAPS) { | 
|---|
| 190 | ResourceMark rm(THREAD); | 
|---|
| 191 | HandleMark hm(THREAD); | 
|---|
| 192 | GCNotificationRequest *request = getRequest(); | 
|---|
| 193 | if (request != NULL) { | 
|---|
| 194 | NotificationMark nm(request); | 
|---|
| 195 | Handle objGcInfo = createGcInfo(request->gcManager, request->gcStatInfo, CHECK); | 
|---|
| 196 |  | 
|---|
| 197 | Handle objName = java_lang_String::create_from_str(request->gcManager->name(), CHECK); | 
|---|
| 198 | Handle objAction = java_lang_String::create_from_str(request->gcAction, CHECK); | 
|---|
| 199 | Handle objCause = java_lang_String::create_from_str(request->gcCause, CHECK); | 
|---|
| 200 | InstanceKlass* gc_mbean_klass = Management::com_sun_management_internal_GarbageCollectorExtImpl_klass(CHECK); | 
|---|
| 201 |  | 
|---|
| 202 | instanceOop gc_mbean = request->gcManager->get_memory_manager_instance(THREAD); | 
|---|
| 203 | instanceHandle gc_mbean_h(THREAD, gc_mbean); | 
|---|
| 204 | if (!gc_mbean_h->is_a(gc_mbean_klass)) { | 
|---|
| 205 | THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), | 
|---|
| 206 | "This GCMemoryManager doesn't have a GarbageCollectorMXBean"); | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | JavaValue result(T_VOID); | 
|---|
| 210 | JavaCallArguments args(gc_mbean_h); | 
|---|
| 211 | args.push_long(request->timestamp); | 
|---|
| 212 | args.push_oop(objName); | 
|---|
| 213 | args.push_oop(objAction); | 
|---|
| 214 | args.push_oop(objCause); | 
|---|
| 215 | args.push_oop(objGcInfo); | 
|---|
| 216 |  | 
|---|
| 217 | JavaCalls::call_virtual(&result, | 
|---|
| 218 | gc_mbean_klass, | 
|---|
| 219 | vmSymbols::createGCNotification_name(), | 
|---|
| 220 | vmSymbols::createGCNotification_signature(), | 
|---|
| 221 | &args, | 
|---|
| 222 | CHECK); | 
|---|
| 223 | } | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 |  | 
|---|