duke@435: /* mikael@4153: * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_SERVICES_LOWMEMORYDETECTOR_HPP stefank@2314: #define SHARE_VM_SERVICES_LOWMEMORYDETECTOR_HPP stefank@2314: stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "services/memoryPool.hpp" stefank@2314: #include "services/memoryService.hpp" stefank@2314: duke@435: // Low Memory Detection Support duke@435: // Two memory alarms in the JDK (we called them sensors). duke@435: // - Heap memory sensor duke@435: // - Non-heap memory sensor duke@435: // When the VM detects if the memory usage of a memory pool has reached duke@435: // or exceeded its threshold, it will trigger the sensor for the type duke@435: // of the memory pool (heap or nonheap or both). duke@435: // duke@435: // If threshold == -1, no low memory detection is supported and duke@435: // the threshold value is not allowed to be changed. duke@435: // If threshold == 0, no low memory detection is performed for duke@435: // that memory pool. The threshold can be set to any non-negative duke@435: // value. duke@435: // duke@435: // The default threshold of the Hotspot memory pools are: duke@435: // Eden space -1 duke@435: // Survivor space 1 -1 duke@435: // Survivor space 2 -1 duke@435: // Old generation 0 duke@435: // Perm generation 0 duke@435: // CodeCache 0 duke@435: // duke@435: // For heap memory, detection will be performed when GC finishes duke@435: // and also in the slow path allocation. duke@435: // For Code cache, detection will be performed in the allocation duke@435: // and deallocation. duke@435: // duke@435: // May need to deal with hysteresis effect. duke@435: // kamg@2511: // Memory detection code runs in the Service thread (serviceThread.hpp). duke@435: duke@435: class OopClosure; duke@435: class MemoryPool; duke@435: zgu@3900: class ThresholdSupport : public CHeapObj { duke@435: private: duke@435: bool _support_high_threshold; duke@435: bool _support_low_threshold; duke@435: size_t _high_threshold; duke@435: size_t _low_threshold; duke@435: public: duke@435: ThresholdSupport(bool support_high, bool support_low) { duke@435: _support_high_threshold = support_high; duke@435: _support_low_threshold = support_low; duke@435: _high_threshold = 0; duke@435: _low_threshold= 0; duke@435: } duke@435: duke@435: size_t high_threshold() const { return _high_threshold; } duke@435: size_t low_threshold() const { return _low_threshold; } duke@435: bool is_high_threshold_supported() { return _support_high_threshold; } duke@435: bool is_low_threshold_supported() { return _support_low_threshold; } duke@435: duke@435: bool is_high_threshold_crossed(MemoryUsage usage) { duke@435: if (_support_high_threshold && _high_threshold > 0) { duke@435: return (usage.used() >= _high_threshold); duke@435: } duke@435: return false; duke@435: } duke@435: bool is_low_threshold_crossed(MemoryUsage usage) { duke@435: if (_support_low_threshold && _low_threshold > 0) { duke@435: return (usage.used() < _low_threshold); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: size_t set_high_threshold(size_t new_threshold) { duke@435: assert(_support_high_threshold, "can only be set if supported"); duke@435: assert(new_threshold >= _low_threshold, "new_threshold must be >= _low_threshold"); duke@435: size_t prev = _high_threshold; duke@435: _high_threshold = new_threshold; duke@435: return prev; duke@435: } duke@435: duke@435: size_t set_low_threshold(size_t new_threshold) { duke@435: assert(_support_low_threshold, "can only be set if supported"); duke@435: assert(new_threshold <= _high_threshold, "new_threshold must be <= _high_threshold"); duke@435: size_t prev = _low_threshold; duke@435: _low_threshold = new_threshold; duke@435: return prev; duke@435: } duke@435: }; duke@435: zgu@3900: class SensorInfo : public CHeapObj { duke@435: private: duke@435: instanceOop _sensor_obj; duke@435: bool _sensor_on; duke@435: size_t _sensor_count; duke@435: duke@435: // before the actual sensor on flag and sensor count are set duke@435: // we maintain the number of pending triggers and clears. duke@435: // _pending_trigger_count means the number of pending triggers duke@435: // and the sensor count should be incremented by the same number. duke@435: duke@435: int _pending_trigger_count; duke@435: duke@435: // _pending_clear_count takes precedence if it's > 0 which duke@435: // indicates the resulting sensor will be off duke@435: // Sensor trigger requests will reset this clear count to duke@435: // indicate the resulting flag should be on. duke@435: duke@435: int _pending_clear_count; duke@435: duke@435: MemoryUsage _usage; duke@435: duke@435: void clear(int count, TRAPS); duke@435: void trigger(int count, TRAPS); duke@435: public: duke@435: SensorInfo(); duke@435: void set_sensor(instanceOop sensor) { duke@435: assert(_sensor_obj == NULL, "Should be set only once"); duke@435: _sensor_obj = sensor; duke@435: } duke@435: duke@435: bool has_pending_requests() { duke@435: return (_pending_trigger_count > 0 || _pending_clear_count > 0); duke@435: } duke@435: duke@435: int pending_trigger_count() { return _pending_trigger_count; } duke@435: int pending_clear_count() { return _pending_clear_count; } duke@435: duke@435: // When this method is used, the memory usage is monitored duke@435: // as a gauge attribute. High and low thresholds are designed duke@435: // to provide a hysteresis mechanism to avoid repeated triggering duke@435: // of notifications when the attribute value makes small oscillations duke@435: // around the high or low threshold value. duke@435: // duke@435: // The sensor will be triggered if: duke@435: // (1) the usage is crossing above the high threshold and duke@435: // the sensor is currently off and no pending duke@435: // trigger requests; or duke@435: // (2) the usage is crossing above the high threshold and duke@435: // the sensor will be off (i.e. sensor is currently on duke@435: // and has pending clear requests). duke@435: // duke@435: // Subsequent crossings of the high threshold value do not cause duke@435: // any triggers unless the usage becomes less than the low threshold. duke@435: // duke@435: // The sensor will be cleared if: duke@435: // (1) the usage is crossing below the low threshold and duke@435: // the sensor is currently on and no pending duke@435: // clear requests; or duke@435: // (2) the usage is crossing below the low threshold and duke@435: // the sensor will be on (i.e. sensor is currently off duke@435: // and has pending trigger requests). duke@435: // duke@435: // Subsequent crossings of the low threshold value do not cause duke@435: // any clears unless the usage becomes greater than or equal duke@435: // to the high threshold. duke@435: // duke@435: // If the current level is between high and low threhsold, no change. duke@435: // duke@435: void set_gauge_sensor_level(MemoryUsage usage, ThresholdSupport* high_low_threshold); duke@435: duke@435: // When this method is used, the memory usage is monitored as a duke@435: // simple counter attribute. The sensor will be triggered duke@435: // whenever the usage is crossing the threshold to keep track duke@435: // of the number of times the VM detects such a condition occurs. duke@435: // duke@435: // The sensor will be triggered if: duke@435: // - the usage is crossing above the high threshold regardless duke@435: // of the current sensor state. duke@435: // duke@435: // The sensor will be cleared if: duke@435: // (1) the usage is crossing below the low threshold and duke@435: // the sensor is currently on; or duke@435: // (2) the usage is crossing below the low threshold and duke@435: // the sensor will be on (i.e. sensor is currently off duke@435: // and has pending trigger requests). duke@435: // duke@435: void set_counter_sensor_level(MemoryUsage usage, ThresholdSupport* counter_threshold); duke@435: duke@435: void process_pending_requests(TRAPS); duke@435: void oops_do(OopClosure* f); duke@435: duke@435: #ifndef PRODUCT duke@435: // printing on default output stream; duke@435: void print(); duke@435: #endif // PRODUCT duke@435: }; duke@435: duke@435: class LowMemoryDetector : public AllStatic { kamg@2511: friend class LowMemoryDetectorDisabler; kamg@2511: friend class ServiceThread; duke@435: private: duke@435: // true if any collected heap has low memory detection enabled duke@435: static volatile bool _enabled_for_collected_pools; duke@435: // > 0 if temporary disabed duke@435: static volatile jint _disabled_count; duke@435: duke@435: static void check_memory_usage(); duke@435: static bool has_pending_requests(); duke@435: static bool temporary_disabled() { return _disabled_count > 0; } duke@435: static void disable() { Atomic::inc(&_disabled_count); } duke@435: static void enable() { Atomic::dec(&_disabled_count); } kamg@2511: static void process_sensor_changes(TRAPS); duke@435: duke@435: public: duke@435: static void detect_low_memory(); duke@435: static void detect_low_memory(MemoryPool* pool); duke@435: static void detect_after_gc_memory(MemoryPool* pool); duke@435: duke@435: static bool is_enabled(MemoryPool* pool) { duke@435: // low memory detection is enabled for collected memory pools duke@435: // iff one of the collected memory pool has a sensor and the duke@435: // threshold set non-zero duke@435: if (pool->usage_sensor() == NULL) { duke@435: return false; duke@435: } else { duke@435: ThresholdSupport* threshold_support = pool->usage_threshold(); duke@435: return (threshold_support->is_high_threshold_supported() ? duke@435: (threshold_support->high_threshold() > 0) : false); duke@435: } duke@435: } duke@435: duke@435: // indicates if low memory detection is enabled for any collected duke@435: // memory pools duke@435: static inline bool is_enabled_for_collected_pools() { duke@435: return !temporary_disabled() && _enabled_for_collected_pools; duke@435: } duke@435: duke@435: // recompute enabled flag duke@435: static void recompute_enabled_for_collected_pools(); duke@435: duke@435: // low memory detection for collected memory pools. duke@435: static inline void detect_low_memory_for_collected_pools() { duke@435: // no-op if low memory detection not enabled duke@435: if (!is_enabled_for_collected_pools()) { duke@435: return; duke@435: } duke@435: int num_memory_pools = MemoryService::num_memory_pools(); duke@435: for (int i=0; iis_collected_pool() && is_enabled(pool)) { duke@435: size_t used = pool->used_in_bytes(); duke@435: size_t high = pool->usage_threshold()->high_threshold(); duke@435: if (used > high) { duke@435: detect_low_memory(pool); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: }; duke@435: duke@435: class LowMemoryDetectorDisabler: public StackObj { duke@435: public: duke@435: LowMemoryDetectorDisabler() duke@435: { duke@435: LowMemoryDetector::disable(); duke@435: } duke@435: ~LowMemoryDetectorDisabler() duke@435: { duke@435: assert(LowMemoryDetector::temporary_disabled(), "should be disabled!"); duke@435: LowMemoryDetector::enable(); duke@435: } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_SERVICES_LOWMEMORYDETECTOR_HPP