Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/services/gcNotifier.cpp
41144 views
1
/*
2
* Copyright (c) 2011, 2021, 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/javaClasses.hpp"
27
#include "classfile/vmClasses.hpp"
28
#include "classfile/vmSymbols.hpp"
29
#include "oops/objArrayOop.inline.hpp"
30
#include "oops/oop.inline.hpp"
31
#include "runtime/handles.inline.hpp"
32
#include "runtime/java.hpp"
33
#include "runtime/javaCalls.hpp"
34
#include "runtime/mutex.hpp"
35
#include "runtime/mutexLocker.hpp"
36
#include "services/gcNotifier.hpp"
37
#include "services/management.hpp"
38
#include "services/memoryService.hpp"
39
#include "memoryManager.hpp"
40
#include "memory/oopFactory.hpp"
41
#include "memory/resourceArea.hpp"
42
43
GCNotificationRequest *GCNotifier::first_request = NULL;
44
GCNotificationRequest *GCNotifier::last_request = NULL;
45
46
void GCNotifier::pushNotification(GCMemoryManager *mgr, const char *action, const char *cause) {
47
// Make a copy of the last GC statistics
48
// GC may occur between now and the creation of the notification
49
int num_pools = MemoryService::num_memory_pools();
50
// stat is deallocated inside GCNotificationRequest
51
GCStatInfo* stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(num_pools);
52
mgr->get_last_gc_stat(stat);
53
// timestamp is current time in ms
54
GCNotificationRequest *request = new GCNotificationRequest(os::javaTimeMillis(),mgr,action,cause,stat);
55
addRequest(request);
56
}
57
58
void GCNotifier::addRequest(GCNotificationRequest *request) {
59
MutexLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);
60
if(first_request == NULL) {
61
first_request = request;
62
} else {
63
last_request->next = request;
64
}
65
last_request = request;
66
Notification_lock->notify_all();
67
}
68
69
GCNotificationRequest *GCNotifier::getRequest() {
70
MutexLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);
71
GCNotificationRequest *request = first_request;
72
if(first_request != NULL) {
73
first_request = first_request->next;
74
}
75
return request;
76
}
77
78
bool GCNotifier::has_event() {
79
return first_request != NULL;
80
}
81
82
static Handle getGcInfoBuilder(GCMemoryManager *gcManager,TRAPS) {
83
84
Klass* gcMBeanKlass = Management::com_sun_management_internal_GarbageCollectorExtImpl_klass(CHECK_NH);
85
86
instanceOop i = gcManager->get_memory_manager_instance(THREAD);
87
instanceHandle ih(THREAD, i);
88
89
JavaValue result(T_OBJECT);
90
JavaCallArguments args(ih);
91
92
JavaCalls::call_virtual(&result,
93
gcMBeanKlass,
94
vmSymbols::getGcInfoBuilder_name(),
95
vmSymbols::getGcInfoBuilder_signature(),
96
&args,
97
CHECK_NH);
98
return Handle(THREAD, result.get_oop());
99
}
100
101
static Handle createGcInfo(GCMemoryManager *gcManager, GCStatInfo *gcStatInfo,TRAPS) {
102
103
// Fill the arrays of MemoryUsage objects with before and after GC
104
// per pool memory usage
105
106
InstanceKlass* mu_klass = Management::java_lang_management_MemoryUsage_klass(CHECK_NH);
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_klass, MemoryService::num_memory_pools(), CHECK_NH);
113
objArrayHandle usage_before_gc_ah(THREAD, bu);
114
objArrayOop au = oopFactory::new_objArray(mu_klass, 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(vmClasses::Integer_klass(), 1, CHECK_NH);
137
objArrayHandle extra_array (THREAD, extra_args_array);
138
139
JavaCallArguments argsInt;
140
argsInt.push_int(gcManager->num_gc_threads());
141
Handle extra_arg_val = JavaCalls::construct_new_instance(
142
vmClasses::Integer_klass(),
143
vmSymbols::int_void_signature(),
144
&argsInt,
145
CHECK_NH);
146
147
extra_array->obj_at_put(0,extra_arg_val());
148
149
InstanceKlass* gcInfoklass = Management::com_sun_management_GcInfo_klass(CHECK_NH);
150
151
JavaCallArguments constructor_args(16);
152
constructor_args.push_oop(getGcInfoBuilder(gcManager,THREAD));
153
constructor_args.push_long(gcStatInfo->gc_index());
154
constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->start_time()));
155
constructor_args.push_long(Management::ticks_to_ms(gcStatInfo->end_time()));
156
constructor_args.push_oop(usage_before_gc_ah);
157
constructor_args.push_oop(usage_after_gc_ah);
158
constructor_args.push_oop(extra_array);
159
160
return JavaCalls::construct_new_instance(
161
gcInfoklass,
162
vmSymbols::com_sun_management_GcInfo_constructor_signature(),
163
&constructor_args,
164
THREAD);
165
}
166
167
void GCNotifier::sendNotification(TRAPS) {
168
GCNotifier::sendNotificationInternal(THREAD);
169
// Clearing pending exception to avoid premature termination of
170
// the service thread
171
if (HAS_PENDING_EXCEPTION) {
172
CLEAR_PENDING_EXCEPTION;
173
}
174
}
175
176
class NotificationMark : public StackObj {
177
// This class is used in GCNotifier::sendNotificationInternal to ensure that
178
// the GCNotificationRequest object is properly cleaned up, whatever path
179
// is used to exit the method.
180
GCNotificationRequest* _request;
181
public:
182
NotificationMark(GCNotificationRequest* r) {
183
_request = r;
184
}
185
~NotificationMark() {
186
assert(_request != NULL, "Sanity check");
187
delete _request;
188
}
189
};
190
191
void GCNotifier::sendNotificationInternal(TRAPS) {
192
ResourceMark rm(THREAD);
193
HandleMark hm(THREAD);
194
GCNotificationRequest *request = getRequest();
195
if (request != NULL) {
196
NotificationMark nm(request);
197
Handle objGcInfo = createGcInfo(request->gcManager, request->gcStatInfo, CHECK);
198
199
Handle objName = java_lang_String::create_from_str(request->gcManager->name(), CHECK);
200
Handle objAction = java_lang_String::create_from_str(request->gcAction, CHECK);
201
Handle objCause = java_lang_String::create_from_str(request->gcCause, CHECK);
202
InstanceKlass* gc_mbean_klass = Management::com_sun_management_internal_GarbageCollectorExtImpl_klass(CHECK);
203
204
instanceOop gc_mbean = request->gcManager->get_memory_manager_instance(THREAD);
205
instanceHandle gc_mbean_h(THREAD, gc_mbean);
206
if (!gc_mbean_h->is_a(gc_mbean_klass)) {
207
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
208
"This GCMemoryManager doesn't have a GarbageCollectorMXBean");
209
}
210
211
JavaValue result(T_VOID);
212
JavaCallArguments args(gc_mbean_h);
213
args.push_long(request->timestamp);
214
args.push_oop(objName);
215
args.push_oop(objAction);
216
args.push_oop(objCause);
217
args.push_oop(objGcInfo);
218
219
JavaCalls::call_virtual(&result,
220
gc_mbean_klass,
221
vmSymbols::createGCNotification_name(),
222
vmSymbols::createGCNotification_signature(),
223
&args,
224
CHECK);
225
}
226
}
227
228
229