62 } 63 last_request = request; 64 Service_lock->notify_all(); 65 } 66 67 GCNotificationRequest *GCNotifier::getRequest() { 68 MutexLockerEx 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* k = Management::com_sun_management_internal_GarbageCollectorExtImpl_klass(CHECK_NH); 83 instanceKlassHandle gcMBeanKlass (THREAD, k); 84 85 instanceOop i = gcManager->get_memory_manager_instance(THREAD); 86 instanceHandle ih(THREAD, i); 87 88 JavaValue result(T_OBJECT); 89 JavaCallArguments args(ih); 90 91 JavaCalls::call_virtual(&result, 92 gcMBeanKlass, 93 vmSymbols::getGcInfoBuilder_name(), 94 vmSymbols::getGcInfoBuilder_signature(), 95 &args, 96 CHECK_NH); 97 return Handle(THREAD,(oop)result.get_jobject()); 98 } 99 100 static Handle createGcInfo(GCMemoryManager *gcManager, GCStatInfo *gcStatInfo,TRAPS) { 101 102 // Fill the arrays of MemoryUsage objects with before and after GC 103 // per pool memory usage 104 105 Klass* mu_klass = Management::java_lang_management_MemoryUsage_klass(CHECK_NH); 106 instanceKlassHandle mu_kh(THREAD, mu_klass); 107 108 // The array allocations below should use a handle containing mu_klass 109 // as the first allocation could trigger a GC, causing the actual 110 // klass oop to move, and leaving mu_klass pointing to the old 111 // location. 112 objArrayOop bu = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH); 113 objArrayHandle usage_before_gc_ah(THREAD, bu); 114 objArrayOop au = oopFactory::new_objArray(mu_kh(), MemoryService::num_memory_pools(), CHECK_NH); 115 objArrayHandle usage_after_gc_ah(THREAD, au); 116 117 for (int i = 0; i < MemoryService::num_memory_pools(); i++) { 118 Handle before_usage = MemoryService::create_MemoryUsage_obj(gcStatInfo->before_gc_usage_for_pool(i), CHECK_NH); 119 Handle after_usage; 120 121 MemoryUsage u = gcStatInfo->after_gc_usage_for_pool(i); 122 if (u.max_size() == 0 && u.used() > 0) { 123 // If max size == 0, this pool is a survivor space. 124 // Set max size = -1 since the pools will be swapped after GC. 125 MemoryUsage usage(u.init_size(), u.used(), u.committed(), (size_t)-1); 126 after_usage = MemoryService::create_MemoryUsage_obj(usage, CHECK_NH); 127 } else { 128 after_usage = MemoryService::create_MemoryUsage_obj(u, CHECK_NH); 129 } 130 usage_before_gc_ah->obj_at_put(i, before_usage()); 131 usage_after_gc_ah->obj_at_put(i, after_usage()); 132 } 133 134 // Current implementation only has 1 attribute (number of GC threads) 135 // The type is 'I' 136 objArrayOop extra_args_array = oopFactory::new_objArray(SystemDictionary::Integer_klass(), 1, CHECK_NH); 137 objArrayHandle extra_array (THREAD, extra_args_array); 138 Klass* itKlass = SystemDictionary::Integer_klass(); 139 instanceKlassHandle intK(THREAD, itKlass); 140 141 instanceHandle extra_arg_val = intK->allocate_instance_handle(CHECK_NH); 142 143 { 144 JavaValue res(T_VOID); 145 JavaCallArguments argsInt; 146 argsInt.push_oop(extra_arg_val); 147 argsInt.push_int(gcManager->num_gc_threads()); 148 149 JavaCalls::call_special(&res, 150 intK, 151 vmSymbols::object_initializer_name(), 152 vmSymbols::int_void_signature(), 153 &argsInt, 154 CHECK_NH); 155 } 156 extra_array->obj_at_put(0,extra_arg_val()); 157 158 Klass* gcInfoklass = Management::com_sun_management_GcInfo_klass(CHECK_NH); 159 instanceKlassHandle ik(THREAD, gcInfoklass); 160 161 Handle gcInfo_instance = ik->allocate_instance_handle(CHECK_NH); 162 163 JavaValue constructor_result(T_VOID); 164 JavaCallArguments constructor_args(16); 165 constructor_args.push_oop(gcInfo_instance); 166 constructor_args.push_oop(getGcInfoBuilder(gcManager,THREAD)); 167 constructor_args.push_long(gcStatInfo->gc_index()); 168 constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->start_time())); 169 constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->end_time())); 170 constructor_args.push_oop(usage_before_gc_ah); 171 constructor_args.push_oop(usage_after_gc_ah); 172 constructor_args.push_oop(extra_array); 173 174 JavaCalls::call_special(&constructor_result, 175 ik, 176 vmSymbols::object_initializer_name(), 177 vmSymbols::com_sun_management_GcInfo_constructor_signature(), 178 &constructor_args, 179 CHECK_NH); 180 181 return Handle(THREAD, gcInfo_instance()); 182 } 183 184 void GCNotifier::sendNotification(TRAPS) { 185 GCNotifier::sendNotificationInternal(THREAD); 186 // Clearing pending exception to avoid premature termination of 187 // the service thread 188 if (HAS_PENDING_EXCEPTION) { 189 CLEAR_PENDING_EXCEPTION; 190 } 191 } 192 193 class NotificationMark : public StackObj { 194 // This class is used in GCNotifier::sendNotificationInternal to ensure that 195 // the GCNotificationRequest object is properly cleaned up, whatever path 199 NotificationMark(GCNotificationRequest* r) { 200 _request = r; 201 } 202 ~NotificationMark() { 203 assert(_request != NULL, "Sanity check"); 204 delete _request; 205 } 206 }; 207 208 void GCNotifier::sendNotificationInternal(TRAPS) { 209 ResourceMark rm(THREAD); 210 HandleMark hm(THREAD); 211 GCNotificationRequest *request = getRequest(); 212 if (request != NULL) { 213 NotificationMark nm(request); 214 Handle objGcInfo = createGcInfo(request->gcManager, request->gcStatInfo, CHECK); 215 216 Handle objName = java_lang_String::create_from_str(request->gcManager->name(), CHECK); 217 Handle objAction = java_lang_String::create_from_str(request->gcAction, CHECK); 218 Handle objCause = java_lang_String::create_from_str(request->gcCause, CHECK); 219 Klass* k = Management::com_sun_management_internal_GarbageCollectorExtImpl_klass(CHECK); 220 221 instanceKlassHandle gc_mbean_klass(THREAD, k); 222 223 instanceOop gc_mbean = request->gcManager->get_memory_manager_instance(THREAD); 224 instanceHandle gc_mbean_h(THREAD, gc_mbean); 225 if (!gc_mbean_h->is_a(k)) { 226 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 227 "This GCMemoryManager doesn't have a GarbageCollectorMXBean"); 228 } 229 230 JavaValue result(T_VOID); 231 JavaCallArguments args(gc_mbean_h); 232 args.push_long(request->timestamp); 233 args.push_oop(objName); 234 args.push_oop(objAction); 235 args.push_oop(objCause); 236 args.push_oop(objGcInfo); 237 238 JavaCalls::call_virtual(&result, 239 gc_mbean_klass, 240 vmSymbols::createGCNotification_name(), 241 vmSymbols::createGCNotification_signature(), 242 &args, 243 CHECK); 244 } 245 } | 62 } 63 last_request = request; 64 Service_lock->notify_all(); 65 } 66 67 GCNotificationRequest *GCNotifier::getRequest() { 68 MutexLockerEx 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 extra_args_array = oopFactory::new_objArray(SystemDictionary::Integer_klass(), 1, CHECK_NH); 135 objArrayHandle extra_array (THREAD, extra_args_array); 136 InstanceKlass* intK = SystemDictionary::Integer_klass(); 137 138 instanceHandle extra_arg_val = intK->allocate_instance_handle(CHECK_NH); 139 140 { 141 JavaValue res(T_VOID); 142 JavaCallArguments argsInt; 143 argsInt.push_oop(extra_arg_val); 144 argsInt.push_int(gcManager->num_gc_threads()); 145 146 JavaCalls::call_special(&res, 147 intK, 148 vmSymbols::object_initializer_name(), 149 vmSymbols::int_void_signature(), 150 &argsInt, 151 CHECK_NH); 152 } 153 extra_array->obj_at_put(0,extra_arg_val()); 154 155 InstanceKlass* gcInfoklass = Management::com_sun_management_GcInfo_klass(CHECK_NH); 156 157 Handle gcInfo_instance = gcInfoklass->allocate_instance_handle(CHECK_NH); 158 159 JavaValue constructor_result(T_VOID); 160 JavaCallArguments constructor_args(16); 161 constructor_args.push_oop(gcInfo_instance); 162 constructor_args.push_oop(getGcInfoBuilder(gcManager,THREAD)); 163 constructor_args.push_long(gcStatInfo->gc_index()); 164 constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->start_time())); 165 constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->end_time())); 166 constructor_args.push_oop(usage_before_gc_ah); 167 constructor_args.push_oop(usage_after_gc_ah); 168 constructor_args.push_oop(extra_array); 169 170 JavaCalls::call_special(&constructor_result, 171 gcInfoklass, 172 vmSymbols::object_initializer_name(), 173 vmSymbols::com_sun_management_GcInfo_constructor_signature(), 174 &constructor_args, 175 CHECK_NH); 176 177 return Handle(THREAD, gcInfo_instance()); 178 } 179 180 void GCNotifier::sendNotification(TRAPS) { 181 GCNotifier::sendNotificationInternal(THREAD); 182 // Clearing pending exception to avoid premature termination of 183 // the service thread 184 if (HAS_PENDING_EXCEPTION) { 185 CLEAR_PENDING_EXCEPTION; 186 } 187 } 188 189 class NotificationMark : public StackObj { 190 // This class is used in GCNotifier::sendNotificationInternal to ensure that 191 // the GCNotificationRequest object is properly cleaned up, whatever path 195 NotificationMark(GCNotificationRequest* r) { 196 _request = r; 197 } 198 ~NotificationMark() { 199 assert(_request != NULL, "Sanity check"); 200 delete _request; 201 } 202 }; 203 204 void GCNotifier::sendNotificationInternal(TRAPS) { 205 ResourceMark rm(THREAD); 206 HandleMark hm(THREAD); 207 GCNotificationRequest *request = getRequest(); 208 if (request != NULL) { 209 NotificationMark nm(request); 210 Handle objGcInfo = createGcInfo(request->gcManager, request->gcStatInfo, CHECK); 211 212 Handle objName = java_lang_String::create_from_str(request->gcManager->name(), CHECK); 213 Handle objAction = java_lang_String::create_from_str(request->gcAction, CHECK); 214 Handle objCause = java_lang_String::create_from_str(request->gcCause, CHECK); 215 InstanceKlass* gc_mbean_klass = Management::com_sun_management_internal_GarbageCollectorExtImpl_klass(CHECK); 216 217 instanceOop gc_mbean = request->gcManager->get_memory_manager_instance(THREAD); 218 instanceHandle gc_mbean_h(THREAD, gc_mbean); 219 if (!gc_mbean_h->is_a(gc_mbean_klass)) { 220 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), 221 "This GCMemoryManager doesn't have a GarbageCollectorMXBean"); 222 } 223 224 JavaValue result(T_VOID); 225 JavaCallArguments args(gc_mbean_h); 226 args.push_long(request->timestamp); 227 args.push_oop(objName); 228 args.push_oop(objAction); 229 args.push_oop(objCause); 230 args.push_oop(objGcInfo); 231 232 JavaCalls::call_virtual(&result, 233 gc_mbean_klass, 234 vmSymbols::createGCNotification_name(), 235 vmSymbols::createGCNotification_signature(), 236 &args, 237 CHECK); 238 } 239 } |