0N/A/*
2076N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "classfile/vmSymbols.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/interfaceSupport.hpp"
1879N/A#include "runtime/java.hpp"
1879N/A#include "runtime/javaCalls.hpp"
1879N/A#include "runtime/mutex.hpp"
1879N/A#include "runtime/mutexLocker.hpp"
1879N/A#include "services/lowMemoryDetector.hpp"
1879N/A#include "services/management.hpp"
0N/A
0N/Avolatile bool LowMemoryDetector::_enabled_for_collected_pools = false;
0N/Avolatile jint LowMemoryDetector::_disabled_count = 0;
0N/A
0N/Abool LowMemoryDetector::has_pending_requests() {
2076N/A assert(Service_lock->owned_by_self(), "Must own Service_lock");
0N/A bool has_requests = false;
0N/A int num_memory_pools = MemoryService::num_memory_pools();
0N/A for (int i = 0; i < num_memory_pools; i++) {
0N/A MemoryPool* pool = MemoryService::get_memory_pool(i);
0N/A SensorInfo* sensor = pool->usage_sensor();
0N/A if (sensor != NULL) {
0N/A has_requests = has_requests || sensor->has_pending_requests();
0N/A }
0N/A
0N/A SensorInfo* gc_sensor = pool->gc_usage_sensor();
0N/A if (gc_sensor != NULL) {
0N/A has_requests = has_requests || gc_sensor->has_pending_requests();
0N/A }
0N/A }
0N/A return has_requests;
0N/A}
0N/A
2076N/Avoid LowMemoryDetector::process_sensor_changes(TRAPS) {
2076N/A ResourceMark rm(THREAD);
2076N/A HandleMark hm(THREAD);
0N/A
2076N/A // No need to hold Service_lock to call out to Java
2076N/A int num_memory_pools = MemoryService::num_memory_pools();
2076N/A for (int i = 0; i < num_memory_pools; i++) {
2076N/A MemoryPool* pool = MemoryService::get_memory_pool(i);
2076N/A SensorInfo* sensor = pool->usage_sensor();
2076N/A SensorInfo* gc_sensor = pool->gc_usage_sensor();
2076N/A if (sensor != NULL && sensor->has_pending_requests()) {
2076N/A sensor->process_pending_requests(CHECK);
0N/A }
2076N/A if (gc_sensor != NULL && gc_sensor->has_pending_requests()) {
2076N/A gc_sensor->process_pending_requests(CHECK);
0N/A }
0N/A }
0N/A}
0N/A
0N/A// This method could be called from any Java threads
0N/A// and also VMThread.
0N/Avoid LowMemoryDetector::detect_low_memory() {
2076N/A MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
0N/A
0N/A bool has_pending_requests = false;
0N/A int num_memory_pools = MemoryService::num_memory_pools();
0N/A for (int i = 0; i < num_memory_pools; i++) {
0N/A MemoryPool* pool = MemoryService::get_memory_pool(i);
0N/A SensorInfo* sensor = pool->usage_sensor();
0N/A if (sensor != NULL &&
0N/A pool->usage_threshold()->is_high_threshold_supported() &&
0N/A pool->usage_threshold()->high_threshold() != 0) {
0N/A MemoryUsage usage = pool->get_memory_usage();
0N/A sensor->set_gauge_sensor_level(usage,
0N/A pool->usage_threshold());
0N/A has_pending_requests = has_pending_requests || sensor->has_pending_requests();
0N/A }
0N/A }
0N/A
0N/A if (has_pending_requests) {
2076N/A Service_lock->notify_all();
0N/A }
0N/A}
0N/A
0N/A// This method could be called from any Java threads
0N/A// and also VMThread.
0N/Avoid LowMemoryDetector::detect_low_memory(MemoryPool* pool) {
0N/A SensorInfo* sensor = pool->usage_sensor();
0N/A if (sensor == NULL ||
0N/A !pool->usage_threshold()->is_high_threshold_supported() ||
0N/A pool->usage_threshold()->high_threshold() == 0) {
0N/A return;
0N/A }
0N/A
0N/A {
2076N/A MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
0N/A
0N/A MemoryUsage usage = pool->get_memory_usage();
0N/A sensor->set_gauge_sensor_level(usage,
0N/A pool->usage_threshold());
0N/A if (sensor->has_pending_requests()) {
0N/A // notify sensor state update
2076N/A Service_lock->notify_all();
0N/A }
0N/A }
0N/A}
0N/A
0N/A// Only called by VMThread at GC time
0N/Avoid LowMemoryDetector::detect_after_gc_memory(MemoryPool* pool) {
0N/A SensorInfo* sensor = pool->gc_usage_sensor();
0N/A if (sensor == NULL ||
0N/A !pool->gc_usage_threshold()->is_high_threshold_supported() ||
0N/A pool->gc_usage_threshold()->high_threshold() == 0) {
0N/A return;
0N/A }
0N/A
0N/A {
2076N/A MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
0N/A
0N/A MemoryUsage usage = pool->get_last_collection_usage();
0N/A sensor->set_counter_sensor_level(usage, pool->gc_usage_threshold());
0N/A
0N/A if (sensor->has_pending_requests()) {
0N/A // notify sensor state update
2076N/A Service_lock->notify_all();
0N/A }
0N/A }
0N/A}
0N/A
0N/A// recompute enabled flag
0N/Avoid LowMemoryDetector::recompute_enabled_for_collected_pools() {
0N/A bool enabled = false;
0N/A int num_memory_pools = MemoryService::num_memory_pools();
0N/A for (int i=0; i<num_memory_pools; i++) {
0N/A MemoryPool* pool = MemoryService::get_memory_pool(i);
0N/A if (pool->is_collected_pool() && is_enabled(pool)) {
0N/A enabled = true;
0N/A break;
0N/A }
0N/A }
0N/A _enabled_for_collected_pools = enabled;
0N/A}
0N/A
0N/ASensorInfo::SensorInfo() {
0N/A _sensor_obj = NULL;
0N/A _sensor_on = false;
0N/A _sensor_count = 0;
0N/A _pending_trigger_count = 0;
0N/A _pending_clear_count = 0;
0N/A}
0N/A
0N/A// When this method is used, the memory usage is monitored
0N/A// as a gauge attribute. Sensor notifications (trigger or
0N/A// clear) is only emitted at the first time it crosses
0N/A// a threshold.
0N/A//
0N/A// High and low thresholds are designed to provide a
0N/A// hysteresis mechanism to avoid repeated triggering
0N/A// of notifications when the attribute value makes small oscillations
0N/A// around the high or low threshold value.
0N/A//
0N/A// The sensor will be triggered if:
0N/A// (1) the usage is crossing above the high threshold and
0N/A// the sensor is currently off and no pending
0N/A// trigger requests; or
0N/A// (2) the usage is crossing above the high threshold and
0N/A// the sensor will be off (i.e. sensor is currently on
0N/A// and has pending clear requests).
0N/A//
0N/A// Subsequent crossings of the high threshold value do not cause
0N/A// any triggers unless the usage becomes less than the low threshold.
0N/A//
0N/A// The sensor will be cleared if:
0N/A// (1) the usage is crossing below the low threshold and
0N/A// the sensor is currently on and no pending
0N/A// clear requests; or
0N/A// (2) the usage is crossing below the low threshold and
0N/A// the sensor will be on (i.e. sensor is currently off
0N/A// and has pending trigger requests).
0N/A//
0N/A// Subsequent crossings of the low threshold value do not cause
0N/A// any clears unless the usage becomes greater than or equal
0N/A// to the high threshold.
0N/A//
0N/A// If the current level is between high and low threhsold, no change.
0N/A//
0N/Avoid SensorInfo::set_gauge_sensor_level(MemoryUsage usage, ThresholdSupport* high_low_threshold) {
0N/A assert(high_low_threshold->is_high_threshold_supported(), "just checking");
0N/A
0N/A bool is_over_high = high_low_threshold->is_high_threshold_crossed(usage);
0N/A bool is_below_low = high_low_threshold->is_low_threshold_crossed(usage);
0N/A
0N/A assert(!(is_over_high && is_below_low), "Can't be both true");
0N/A
0N/A if (is_over_high &&
0N/A ((!_sensor_on && _pending_trigger_count == 0) ||
0N/A _pending_clear_count > 0)) {
0N/A // low memory detected and need to increment the trigger pending count
0N/A // if the sensor is off or will be off due to _pending_clear_ > 0
0N/A // Request to trigger the sensor
0N/A _pending_trigger_count++;
0N/A _usage = usage;
0N/A
0N/A if (_pending_clear_count > 0) {
0N/A // non-zero pending clear requests indicates that there are
0N/A // pending requests to clear this sensor.
0N/A // This trigger request needs to clear this clear count
0N/A // since the resulting sensor flag should be on.
0N/A _pending_clear_count = 0;
0N/A }
0N/A } else if (is_below_low &&
0N/A ((_sensor_on && _pending_clear_count == 0) ||
0N/A (_pending_trigger_count > 0 && _pending_clear_count == 0))) {
0N/A // memory usage returns below the threshold
0N/A // Request to clear the sensor if the sensor is on or will be on due to
0N/A // _pending_trigger_count > 0 and also no clear request
0N/A _pending_clear_count++;
0N/A }
0N/A}
0N/A
0N/A// When this method is used, the memory usage is monitored as a
0N/A// simple counter attribute. The sensor will be triggered
0N/A// whenever the usage is crossing the threshold to keep track
0N/A// of the number of times the VM detects such a condition occurs.
0N/A//
0N/A// High and low thresholds are designed to provide a
0N/A// hysteresis mechanism to avoid repeated triggering
0N/A// of notifications when the attribute value makes small oscillations
0N/A// around the high or low threshold value.
0N/A//
0N/A// The sensor will be triggered if:
0N/A// - the usage is crossing above the high threshold regardless
0N/A// of the current sensor state.
0N/A//
0N/A// The sensor will be cleared if:
0N/A// (1) the usage is crossing below the low threshold and
0N/A// the sensor is currently on; or
0N/A// (2) the usage is crossing below the low threshold and
0N/A// the sensor will be on (i.e. sensor is currently off
0N/A// and has pending trigger requests).
0N/Avoid SensorInfo::set_counter_sensor_level(MemoryUsage usage, ThresholdSupport* counter_threshold) {
0N/A assert(counter_threshold->is_high_threshold_supported(), "just checking");
0N/A
0N/A bool is_over_high = counter_threshold->is_high_threshold_crossed(usage);
0N/A bool is_below_low = counter_threshold->is_low_threshold_crossed(usage);
0N/A
0N/A assert(!(is_over_high && is_below_low), "Can't be both true");
0N/A
0N/A if (is_over_high) {
0N/A _pending_trigger_count++;
0N/A _usage = usage;
0N/A _pending_clear_count = 0;
0N/A } else if (is_below_low && (_sensor_on || _pending_trigger_count > 0)) {
0N/A _pending_clear_count++;
0N/A }
0N/A}
0N/A
0N/Avoid SensorInfo::oops_do(OopClosure* f) {
0N/A f->do_oop((oop*) &_sensor_obj);
0N/A}
0N/A
0N/Avoid SensorInfo::process_pending_requests(TRAPS) {
0N/A if (!has_pending_requests()) {
0N/A return;
0N/A }
0N/A
0N/A int pending_count = pending_trigger_count();
0N/A if (pending_clear_count() > 0) {
0N/A clear(pending_count, CHECK);
0N/A } else {
0N/A trigger(pending_count, CHECK);
0N/A }
0N/A
0N/A}
0N/A
0N/Avoid SensorInfo::trigger(int count, TRAPS) {
0N/A assert(count <= _pending_trigger_count, "just checking");
0N/A
0N/A if (_sensor_obj != NULL) {
0N/A klassOop k = Management::sun_management_Sensor_klass(CHECK);
0N/A instanceKlassHandle sensorKlass (THREAD, k);
0N/A Handle sensor_h(THREAD, _sensor_obj);
0N/A Handle usage_h = MemoryService::create_MemoryUsage_obj(_usage, CHECK);
0N/A
0N/A JavaValue result(T_VOID);
0N/A JavaCallArguments args(sensor_h);
0N/A args.push_int((int) count);
0N/A args.push_oop(usage_h);
0N/A
0N/A JavaCalls::call_virtual(&result,
0N/A sensorKlass,
2062N/A vmSymbols::trigger_name(),
2062N/A vmSymbols::trigger_method_signature(),
0N/A &args,
0N/A CHECK);
0N/A }
0N/A
0N/A {
2076N/A // Holds Service_lock and update the sensor state
2076N/A MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
0N/A _sensor_on = true;
0N/A _sensor_count += count;
0N/A _pending_trigger_count = _pending_trigger_count - count;
0N/A }
0N/A}
0N/A
0N/Avoid SensorInfo::clear(int count, TRAPS) {
0N/A if (_sensor_obj != NULL) {
0N/A klassOop k = Management::sun_management_Sensor_klass(CHECK);
0N/A instanceKlassHandle sensorKlass (THREAD, k);
0N/A Handle sensor(THREAD, _sensor_obj);
0N/A
0N/A JavaValue result(T_VOID);
0N/A JavaCallArguments args(sensor);
0N/A args.push_int((int) count);
0N/A JavaCalls::call_virtual(&result,
0N/A sensorKlass,
2062N/A vmSymbols::clear_name(),
2062N/A vmSymbols::int_void_signature(),
0N/A &args,
0N/A CHECK);
0N/A }
0N/A
0N/A {
2076N/A // Holds Service_lock and update the sensor state
2076N/A MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
0N/A _sensor_on = false;
0N/A _pending_clear_count = 0;
0N/A _pending_trigger_count = _pending_trigger_count - count;
0N/A }
0N/A}
0N/A
0N/A//--------------------------------------------------------------
0N/A// Non-product code
0N/A
0N/A#ifndef PRODUCT
0N/Avoid SensorInfo::print() {
0N/A tty->print_cr("%s count = %ld pending_triggers = %ld pending_clears = %ld",
0N/A (_sensor_on ? "on" : "off"),
0N/A _sensor_count, _pending_trigger_count, _pending_clear_count);
0N/A}
0N/A
0N/A#endif // PRODUCT