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