Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/services/lowMemoryDetector.cpp
41145 views
1
/*
2
* Copyright (c) 2003, 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/vmClasses.hpp"
27
#include "classfile/vmSymbols.hpp"
28
#include "memory/resourceArea.hpp"
29
#include "memory/universe.hpp"
30
#include "oops/oop.inline.hpp"
31
#include "oops/oopHandle.inline.hpp"
32
#include "runtime/handles.inline.hpp"
33
#include "runtime/interfaceSupport.inline.hpp"
34
#include "runtime/java.hpp"
35
#include "runtime/javaCalls.hpp"
36
#include "runtime/mutex.hpp"
37
#include "runtime/mutexLocker.hpp"
38
#include "services/lowMemoryDetector.hpp"
39
#include "services/management.hpp"
40
41
volatile bool LowMemoryDetector::_enabled_for_collected_pools = false;
42
43
bool LowMemoryDetector::has_pending_requests() {
44
assert(Notification_lock->owned_by_self(), "Must own Notification_lock");
45
bool has_requests = false;
46
int num_memory_pools = MemoryService::num_memory_pools();
47
for (int i = 0; i < num_memory_pools; i++) {
48
MemoryPool* pool = MemoryService::get_memory_pool(i);
49
SensorInfo* sensor = pool->usage_sensor();
50
if (sensor != NULL) {
51
has_requests = has_requests || sensor->has_pending_requests();
52
}
53
54
SensorInfo* gc_sensor = pool->gc_usage_sensor();
55
if (gc_sensor != NULL) {
56
has_requests = has_requests || gc_sensor->has_pending_requests();
57
}
58
}
59
return has_requests;
60
}
61
62
void LowMemoryDetector::process_sensor_changes(TRAPS) {
63
ResourceMark rm(THREAD);
64
HandleMark hm(THREAD);
65
66
// No need to hold Notification_lock to call out to Java
67
int num_memory_pools = MemoryService::num_memory_pools();
68
for (int i = 0; i < num_memory_pools; i++) {
69
MemoryPool* pool = MemoryService::get_memory_pool(i);
70
SensorInfo* sensor = pool->usage_sensor();
71
SensorInfo* gc_sensor = pool->gc_usage_sensor();
72
if (sensor != NULL && sensor->has_pending_requests()) {
73
sensor->process_pending_requests(CHECK);
74
}
75
if (gc_sensor != NULL && gc_sensor->has_pending_requests()) {
76
gc_sensor->process_pending_requests(CHECK);
77
}
78
}
79
}
80
81
// This method could be called from any Java threads
82
// and also VMThread.
83
void LowMemoryDetector::detect_low_memory() {
84
MutexLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);
85
86
bool has_pending_requests = false;
87
int num_memory_pools = MemoryService::num_memory_pools();
88
for (int i = 0; i < num_memory_pools; i++) {
89
MemoryPool* pool = MemoryService::get_memory_pool(i);
90
SensorInfo* sensor = pool->usage_sensor();
91
if (sensor != NULL &&
92
pool->usage_threshold()->is_high_threshold_supported() &&
93
pool->usage_threshold()->high_threshold() != 0) {
94
MemoryUsage usage = pool->get_memory_usage();
95
sensor->set_gauge_sensor_level(usage,
96
pool->usage_threshold());
97
has_pending_requests = has_pending_requests || sensor->has_pending_requests();
98
}
99
}
100
101
if (has_pending_requests) {
102
Notification_lock->notify_all();
103
}
104
}
105
106
// This method could be called from any Java threads
107
// and also VMThread.
108
void LowMemoryDetector::detect_low_memory(MemoryPool* pool) {
109
SensorInfo* sensor = pool->usage_sensor();
110
if (sensor == NULL ||
111
!pool->usage_threshold()->is_high_threshold_supported() ||
112
pool->usage_threshold()->high_threshold() == 0) {
113
return;
114
}
115
116
{
117
MutexLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);
118
119
MemoryUsage usage = pool->get_memory_usage();
120
sensor->set_gauge_sensor_level(usage,
121
pool->usage_threshold());
122
if (sensor->has_pending_requests()) {
123
// notify sensor state update
124
Notification_lock->notify_all();
125
}
126
}
127
}
128
129
// Only called by VMThread at GC time
130
void LowMemoryDetector::detect_after_gc_memory(MemoryPool* pool) {
131
SensorInfo* sensor = pool->gc_usage_sensor();
132
if (sensor == NULL ||
133
!pool->gc_usage_threshold()->is_high_threshold_supported() ||
134
pool->gc_usage_threshold()->high_threshold() == 0) {
135
return;
136
}
137
138
{
139
MutexLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);
140
141
MemoryUsage usage = pool->get_last_collection_usage();
142
sensor->set_counter_sensor_level(usage, pool->gc_usage_threshold());
143
144
if (sensor->has_pending_requests()) {
145
// notify sensor state update
146
Notification_lock->notify_all();
147
}
148
}
149
}
150
151
// recompute enabled flag
152
void LowMemoryDetector::recompute_enabled_for_collected_pools() {
153
bool enabled = false;
154
int num_memory_pools = MemoryService::num_memory_pools();
155
for (int i=0; i<num_memory_pools; i++) {
156
MemoryPool* pool = MemoryService::get_memory_pool(i);
157
if (pool->is_collected_pool() && is_enabled(pool)) {
158
enabled = true;
159
break;
160
}
161
}
162
_enabled_for_collected_pools = enabled;
163
}
164
165
SensorInfo::SensorInfo() {
166
_sensor_on = false;
167
_sensor_count = 0;
168
_pending_trigger_count = 0;
169
_pending_clear_count = 0;
170
}
171
172
void SensorInfo::set_sensor(instanceOop sensor) {
173
assert(_sensor_obj.peek() == NULL, "Should be set only once");
174
_sensor_obj = OopHandle(Universe::vm_global(), sensor);
175
}
176
177
178
// When this method is used, the memory usage is monitored
179
// as a gauge attribute. Sensor notifications (trigger or
180
// clear) is only emitted at the first time it crosses
181
// a threshold.
182
//
183
// High and low thresholds are designed to provide a
184
// hysteresis mechanism to avoid repeated triggering
185
// of notifications when the attribute value makes small oscillations
186
// around the high or low threshold value.
187
//
188
// The sensor will be triggered if:
189
// (1) the usage is crossing above the high threshold and
190
// the sensor is currently off and no pending
191
// trigger requests; or
192
// (2) the usage is crossing above the high threshold and
193
// the sensor will be off (i.e. sensor is currently on
194
// and has pending clear requests).
195
//
196
// Subsequent crossings of the high threshold value do not cause
197
// any triggers unless the usage becomes less than the low threshold.
198
//
199
// The sensor will be cleared if:
200
// (1) the usage is crossing below the low threshold and
201
// the sensor is currently on and no pending
202
// clear requests; or
203
// (2) the usage is crossing below the low threshold and
204
// the sensor will be on (i.e. sensor is currently off
205
// and has pending trigger requests).
206
//
207
// Subsequent crossings of the low threshold value do not cause
208
// any clears unless the usage becomes greater than or equal
209
// to the high threshold.
210
//
211
// If the current level is between high and low threshold, no change.
212
//
213
void SensorInfo::set_gauge_sensor_level(MemoryUsage usage, ThresholdSupport* high_low_threshold) {
214
assert(Notification_lock->owned_by_self(), "Must own Notification_lock");
215
assert(high_low_threshold->is_high_threshold_supported(), "just checking");
216
217
bool is_over_high = high_low_threshold->is_high_threshold_crossed(usage);
218
bool is_below_low = high_low_threshold->is_low_threshold_crossed(usage);
219
220
assert(!(is_over_high && is_below_low), "Can't be both true");
221
222
if (is_over_high &&
223
((!_sensor_on && _pending_trigger_count == 0) ||
224
_pending_clear_count > 0)) {
225
// low memory detected and need to increment the trigger pending count
226
// if the sensor is off or will be off due to _pending_clear_ > 0
227
// Request to trigger the sensor
228
_pending_trigger_count++;
229
_usage = usage;
230
231
if (_pending_clear_count > 0) {
232
// non-zero pending clear requests indicates that there are
233
// pending requests to clear this sensor.
234
// This trigger request needs to clear this clear count
235
// since the resulting sensor flag should be on.
236
_pending_clear_count = 0;
237
}
238
} else if (is_below_low &&
239
((_sensor_on && _pending_clear_count == 0) ||
240
(_pending_trigger_count > 0 && _pending_clear_count == 0))) {
241
// memory usage returns below the threshold
242
// Request to clear the sensor if the sensor is on or will be on due to
243
// _pending_trigger_count > 0 and also no clear request
244
_pending_clear_count++;
245
}
246
}
247
248
// When this method is used, the memory usage is monitored as a
249
// simple counter attribute. The sensor will be triggered
250
// whenever the usage is crossing the threshold to keep track
251
// of the number of times the VM detects such a condition occurs.
252
//
253
// High and low thresholds are designed to provide a
254
// hysteresis mechanism to avoid repeated triggering
255
// of notifications when the attribute value makes small oscillations
256
// around the high or low threshold value.
257
//
258
// The sensor will be triggered if:
259
// - the usage is crossing above the high threshold regardless
260
// of the current sensor state.
261
//
262
// The sensor will be cleared if:
263
// (1) the usage is crossing below the low threshold and
264
// the sensor is currently on; or
265
// (2) the usage is crossing below the low threshold and
266
// the sensor will be on (i.e. sensor is currently off
267
// and has pending trigger requests).
268
void SensorInfo::set_counter_sensor_level(MemoryUsage usage, ThresholdSupport* counter_threshold) {
269
assert(Notification_lock->owned_by_self(), "Must own Notification_lock");
270
assert(counter_threshold->is_high_threshold_supported(), "just checking");
271
272
bool is_over_high = counter_threshold->is_high_threshold_crossed(usage);
273
bool is_below_low = counter_threshold->is_low_threshold_crossed(usage);
274
275
assert(!(is_over_high && is_below_low), "Can't be both true");
276
277
if (is_over_high) {
278
_pending_trigger_count++;
279
_usage = usage;
280
_pending_clear_count = 0;
281
} else if (is_below_low && (_sensor_on || _pending_trigger_count > 0)) {
282
_pending_clear_count++;
283
}
284
}
285
286
void SensorInfo::process_pending_requests(TRAPS) {
287
int pending_count = pending_trigger_count();
288
if (pending_clear_count() > 0) {
289
clear(pending_count, CHECK);
290
} else {
291
trigger(pending_count, CHECK);
292
}
293
294
}
295
296
void SensorInfo::trigger(int count, TRAPS) {
297
assert(count <= _pending_trigger_count, "just checking");
298
Handle sensor_h(THREAD, _sensor_obj.resolve());
299
if (sensor_h() != NULL) {
300
InstanceKlass* sensorKlass = Management::sun_management_Sensor_klass(CHECK);
301
Symbol* trigger_method_signature;
302
303
JavaValue result(T_VOID);
304
JavaCallArguments args(sensor_h);
305
args.push_int((int) count);
306
307
Handle usage_h = MemoryService::create_MemoryUsage_obj(_usage, THREAD);
308
// Call Sensor::trigger(int, MemoryUsage) to send notification to listeners.
309
// When OOME occurs and fails to allocate MemoryUsage object, call
310
// Sensor::trigger(int) instead. The pending request will be processed
311
// but no notification will be sent.
312
if (HAS_PENDING_EXCEPTION) {
313
assert((PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())), "we expect only an OOME here");
314
CLEAR_PENDING_EXCEPTION;
315
trigger_method_signature = vmSymbols::int_void_signature();
316
} else {
317
trigger_method_signature = vmSymbols::trigger_method_signature();
318
args.push_oop(usage_h);
319
}
320
321
JavaCalls::call_virtual(&result,
322
sensorKlass,
323
vmSymbols::trigger_name(),
324
trigger_method_signature,
325
&args,
326
THREAD);
327
328
if (HAS_PENDING_EXCEPTION) {
329
// We just clear the OOM pending exception that we might have encountered
330
// in Java's tiggerAction(), and continue with updating the counters since
331
// the Java counters have been updated too.
332
assert((PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())), "we expect only an OOME here");
333
CLEAR_PENDING_EXCEPTION;
334
}
335
}
336
337
{
338
// Holds Notification_lock and update the sensor state
339
MutexLocker ml(THREAD, Notification_lock, Mutex::_no_safepoint_check_flag);
340
assert(_pending_trigger_count > 0, "Must have pending trigger");
341
_sensor_on = true;
342
_sensor_count += count;
343
_pending_trigger_count = _pending_trigger_count - count;
344
}
345
}
346
347
void SensorInfo::clear(int count, TRAPS) {
348
{
349
// Holds Notification_lock and update the sensor state
350
MutexLocker ml(THREAD, Notification_lock, Mutex::_no_safepoint_check_flag);
351
if (_pending_clear_count == 0) {
352
// Bail out if we lost a race to set_*_sensor_level() which may have
353
// reactivated the sensor in the meantime because it was triggered again.
354
return;
355
}
356
_sensor_on = false;
357
_sensor_count += count;
358
_pending_clear_count = 0;
359
_pending_trigger_count = _pending_trigger_count - count;
360
}
361
362
Handle sensor(THREAD, _sensor_obj.resolve());
363
if (sensor() != NULL) {
364
InstanceKlass* sensorKlass = Management::sun_management_Sensor_klass(CHECK);
365
JavaValue result(T_VOID);
366
JavaCallArguments args(sensor);
367
args.push_int((int) count);
368
JavaCalls::call_virtual(&result,
369
sensorKlass,
370
vmSymbols::clear_name(),
371
vmSymbols::int_void_signature(),
372
&args,
373
CHECK);
374
}
375
}
376
377
//--------------------------------------------------------------
378
// Non-product code
379
380
#ifndef PRODUCT
381
void SensorInfo::print() {
382
tty->print_cr("%s count = " SIZE_FORMAT " pending_triggers = %d pending_clears = %d",
383
(_sensor_on ? "on" : "off"),
384
_sensor_count, _pending_trigger_count, _pending_clear_count);
385
}
386
387
#endif // PRODUCT
388
389