Path: blob/master/src/hotspot/share/services/lowMemoryDetector.cpp
41145 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/vmClasses.hpp"26#include "classfile/vmSymbols.hpp"27#include "memory/resourceArea.hpp"28#include "memory/universe.hpp"29#include "oops/oop.inline.hpp"30#include "oops/oopHandle.inline.hpp"31#include "runtime/handles.inline.hpp"32#include "runtime/interfaceSupport.inline.hpp"33#include "runtime/java.hpp"34#include "runtime/javaCalls.hpp"35#include "runtime/mutex.hpp"36#include "runtime/mutexLocker.hpp"37#include "services/lowMemoryDetector.hpp"38#include "services/management.hpp"3940volatile bool LowMemoryDetector::_enabled_for_collected_pools = false;4142bool LowMemoryDetector::has_pending_requests() {43assert(Notification_lock->owned_by_self(), "Must own Notification_lock");44bool has_requests = false;45int num_memory_pools = MemoryService::num_memory_pools();46for (int i = 0; i < num_memory_pools; i++) {47MemoryPool* pool = MemoryService::get_memory_pool(i);48SensorInfo* sensor = pool->usage_sensor();49if (sensor != NULL) {50has_requests = has_requests || sensor->has_pending_requests();51}5253SensorInfo* gc_sensor = pool->gc_usage_sensor();54if (gc_sensor != NULL) {55has_requests = has_requests || gc_sensor->has_pending_requests();56}57}58return has_requests;59}6061void LowMemoryDetector::process_sensor_changes(TRAPS) {62ResourceMark rm(THREAD);63HandleMark hm(THREAD);6465// No need to hold Notification_lock to call out to Java66int num_memory_pools = MemoryService::num_memory_pools();67for (int i = 0; i < num_memory_pools; i++) {68MemoryPool* pool = MemoryService::get_memory_pool(i);69SensorInfo* sensor = pool->usage_sensor();70SensorInfo* gc_sensor = pool->gc_usage_sensor();71if (sensor != NULL && sensor->has_pending_requests()) {72sensor->process_pending_requests(CHECK);73}74if (gc_sensor != NULL && gc_sensor->has_pending_requests()) {75gc_sensor->process_pending_requests(CHECK);76}77}78}7980// This method could be called from any Java threads81// and also VMThread.82void LowMemoryDetector::detect_low_memory() {83MutexLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);8485bool has_pending_requests = false;86int num_memory_pools = MemoryService::num_memory_pools();87for (int i = 0; i < num_memory_pools; i++) {88MemoryPool* pool = MemoryService::get_memory_pool(i);89SensorInfo* sensor = pool->usage_sensor();90if (sensor != NULL &&91pool->usage_threshold()->is_high_threshold_supported() &&92pool->usage_threshold()->high_threshold() != 0) {93MemoryUsage usage = pool->get_memory_usage();94sensor->set_gauge_sensor_level(usage,95pool->usage_threshold());96has_pending_requests = has_pending_requests || sensor->has_pending_requests();97}98}99100if (has_pending_requests) {101Notification_lock->notify_all();102}103}104105// This method could be called from any Java threads106// and also VMThread.107void LowMemoryDetector::detect_low_memory(MemoryPool* pool) {108SensorInfo* sensor = pool->usage_sensor();109if (sensor == NULL ||110!pool->usage_threshold()->is_high_threshold_supported() ||111pool->usage_threshold()->high_threshold() == 0) {112return;113}114115{116MutexLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);117118MemoryUsage usage = pool->get_memory_usage();119sensor->set_gauge_sensor_level(usage,120pool->usage_threshold());121if (sensor->has_pending_requests()) {122// notify sensor state update123Notification_lock->notify_all();124}125}126}127128// Only called by VMThread at GC time129void LowMemoryDetector::detect_after_gc_memory(MemoryPool* pool) {130SensorInfo* sensor = pool->gc_usage_sensor();131if (sensor == NULL ||132!pool->gc_usage_threshold()->is_high_threshold_supported() ||133pool->gc_usage_threshold()->high_threshold() == 0) {134return;135}136137{138MutexLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);139140MemoryUsage usage = pool->get_last_collection_usage();141sensor->set_counter_sensor_level(usage, pool->gc_usage_threshold());142143if (sensor->has_pending_requests()) {144// notify sensor state update145Notification_lock->notify_all();146}147}148}149150// recompute enabled flag151void LowMemoryDetector::recompute_enabled_for_collected_pools() {152bool enabled = false;153int num_memory_pools = MemoryService::num_memory_pools();154for (int i=0; i<num_memory_pools; i++) {155MemoryPool* pool = MemoryService::get_memory_pool(i);156if (pool->is_collected_pool() && is_enabled(pool)) {157enabled = true;158break;159}160}161_enabled_for_collected_pools = enabled;162}163164SensorInfo::SensorInfo() {165_sensor_on = false;166_sensor_count = 0;167_pending_trigger_count = 0;168_pending_clear_count = 0;169}170171void SensorInfo::set_sensor(instanceOop sensor) {172assert(_sensor_obj.peek() == NULL, "Should be set only once");173_sensor_obj = OopHandle(Universe::vm_global(), sensor);174}175176177// When this method is used, the memory usage is monitored178// as a gauge attribute. Sensor notifications (trigger or179// clear) is only emitted at the first time it crosses180// a threshold.181//182// High and low thresholds are designed to provide a183// hysteresis mechanism to avoid repeated triggering184// of notifications when the attribute value makes small oscillations185// around the high or low threshold value.186//187// The sensor will be triggered if:188// (1) the usage is crossing above the high threshold and189// the sensor is currently off and no pending190// trigger requests; or191// (2) the usage is crossing above the high threshold and192// the sensor will be off (i.e. sensor is currently on193// and has pending clear requests).194//195// Subsequent crossings of the high threshold value do not cause196// any triggers unless the usage becomes less than the low threshold.197//198// The sensor will be cleared if:199// (1) the usage is crossing below the low threshold and200// the sensor is currently on and no pending201// clear requests; or202// (2) the usage is crossing below the low threshold and203// the sensor will be on (i.e. sensor is currently off204// and has pending trigger requests).205//206// Subsequent crossings of the low threshold value do not cause207// any clears unless the usage becomes greater than or equal208// to the high threshold.209//210// If the current level is between high and low threshold, no change.211//212void SensorInfo::set_gauge_sensor_level(MemoryUsage usage, ThresholdSupport* high_low_threshold) {213assert(Notification_lock->owned_by_self(), "Must own Notification_lock");214assert(high_low_threshold->is_high_threshold_supported(), "just checking");215216bool is_over_high = high_low_threshold->is_high_threshold_crossed(usage);217bool is_below_low = high_low_threshold->is_low_threshold_crossed(usage);218219assert(!(is_over_high && is_below_low), "Can't be both true");220221if (is_over_high &&222((!_sensor_on && _pending_trigger_count == 0) ||223_pending_clear_count > 0)) {224// low memory detected and need to increment the trigger pending count225// if the sensor is off or will be off due to _pending_clear_ > 0226// Request to trigger the sensor227_pending_trigger_count++;228_usage = usage;229230if (_pending_clear_count > 0) {231// non-zero pending clear requests indicates that there are232// pending requests to clear this sensor.233// This trigger request needs to clear this clear count234// since the resulting sensor flag should be on.235_pending_clear_count = 0;236}237} else if (is_below_low &&238((_sensor_on && _pending_clear_count == 0) ||239(_pending_trigger_count > 0 && _pending_clear_count == 0))) {240// memory usage returns below the threshold241// Request to clear the sensor if the sensor is on or will be on due to242// _pending_trigger_count > 0 and also no clear request243_pending_clear_count++;244}245}246247// When this method is used, the memory usage is monitored as a248// simple counter attribute. The sensor will be triggered249// whenever the usage is crossing the threshold to keep track250// of the number of times the VM detects such a condition occurs.251//252// High and low thresholds are designed to provide a253// hysteresis mechanism to avoid repeated triggering254// of notifications when the attribute value makes small oscillations255// around the high or low threshold value.256//257// The sensor will be triggered if:258// - the usage is crossing above the high threshold regardless259// of the current sensor state.260//261// The sensor will be cleared if:262// (1) the usage is crossing below the low threshold and263// the sensor is currently on; or264// (2) the usage is crossing below the low threshold and265// the sensor will be on (i.e. sensor is currently off266// and has pending trigger requests).267void SensorInfo::set_counter_sensor_level(MemoryUsage usage, ThresholdSupport* counter_threshold) {268assert(Notification_lock->owned_by_self(), "Must own Notification_lock");269assert(counter_threshold->is_high_threshold_supported(), "just checking");270271bool is_over_high = counter_threshold->is_high_threshold_crossed(usage);272bool is_below_low = counter_threshold->is_low_threshold_crossed(usage);273274assert(!(is_over_high && is_below_low), "Can't be both true");275276if (is_over_high) {277_pending_trigger_count++;278_usage = usage;279_pending_clear_count = 0;280} else if (is_below_low && (_sensor_on || _pending_trigger_count > 0)) {281_pending_clear_count++;282}283}284285void SensorInfo::process_pending_requests(TRAPS) {286int pending_count = pending_trigger_count();287if (pending_clear_count() > 0) {288clear(pending_count, CHECK);289} else {290trigger(pending_count, CHECK);291}292293}294295void SensorInfo::trigger(int count, TRAPS) {296assert(count <= _pending_trigger_count, "just checking");297Handle sensor_h(THREAD, _sensor_obj.resolve());298if (sensor_h() != NULL) {299InstanceKlass* sensorKlass = Management::sun_management_Sensor_klass(CHECK);300Symbol* trigger_method_signature;301302JavaValue result(T_VOID);303JavaCallArguments args(sensor_h);304args.push_int((int) count);305306Handle usage_h = MemoryService::create_MemoryUsage_obj(_usage, THREAD);307// Call Sensor::trigger(int, MemoryUsage) to send notification to listeners.308// When OOME occurs and fails to allocate MemoryUsage object, call309// Sensor::trigger(int) instead. The pending request will be processed310// but no notification will be sent.311if (HAS_PENDING_EXCEPTION) {312assert((PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())), "we expect only an OOME here");313CLEAR_PENDING_EXCEPTION;314trigger_method_signature = vmSymbols::int_void_signature();315} else {316trigger_method_signature = vmSymbols::trigger_method_signature();317args.push_oop(usage_h);318}319320JavaCalls::call_virtual(&result,321sensorKlass,322vmSymbols::trigger_name(),323trigger_method_signature,324&args,325THREAD);326327if (HAS_PENDING_EXCEPTION) {328// We just clear the OOM pending exception that we might have encountered329// in Java's tiggerAction(), and continue with updating the counters since330// the Java counters have been updated too.331assert((PENDING_EXCEPTION->is_a(vmClasses::OutOfMemoryError_klass())), "we expect only an OOME here");332CLEAR_PENDING_EXCEPTION;333}334}335336{337// Holds Notification_lock and update the sensor state338MutexLocker ml(THREAD, Notification_lock, Mutex::_no_safepoint_check_flag);339assert(_pending_trigger_count > 0, "Must have pending trigger");340_sensor_on = true;341_sensor_count += count;342_pending_trigger_count = _pending_trigger_count - count;343}344}345346void SensorInfo::clear(int count, TRAPS) {347{348// Holds Notification_lock and update the sensor state349MutexLocker ml(THREAD, Notification_lock, Mutex::_no_safepoint_check_flag);350if (_pending_clear_count == 0) {351// Bail out if we lost a race to set_*_sensor_level() which may have352// reactivated the sensor in the meantime because it was triggered again.353return;354}355_sensor_on = false;356_sensor_count += count;357_pending_clear_count = 0;358_pending_trigger_count = _pending_trigger_count - count;359}360361Handle sensor(THREAD, _sensor_obj.resolve());362if (sensor() != NULL) {363InstanceKlass* sensorKlass = Management::sun_management_Sensor_klass(CHECK);364JavaValue result(T_VOID);365JavaCallArguments args(sensor);366args.push_int((int) count);367JavaCalls::call_virtual(&result,368sensorKlass,369vmSymbols::clear_name(),370vmSymbols::int_void_signature(),371&args,372CHECK);373}374}375376//--------------------------------------------------------------377// Non-product code378379#ifndef PRODUCT380void SensorInfo::print() {381tty->print_cr("%s count = " SIZE_FORMAT " pending_triggers = %d pending_clears = %d",382(_sensor_on ? "on" : "off"),383_sensor_count, _pending_trigger_count, _pending_clear_count);384}385386#endif // PRODUCT387388389