Wed, 27 Aug 2014 09:36:55 +0200
Merge
duke@435 | 1 | /* |
mikael@4153 | 2 | * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #ifndef SHARE_VM_SERVICES_LOWMEMORYDETECTOR_HPP |
stefank@2314 | 26 | #define SHARE_VM_SERVICES_LOWMEMORYDETECTOR_HPP |
stefank@2314 | 27 | |
stefank@2314 | 28 | #include "memory/allocation.hpp" |
stefank@2314 | 29 | #include "services/memoryPool.hpp" |
stefank@2314 | 30 | #include "services/memoryService.hpp" |
stefank@2314 | 31 | |
duke@435 | 32 | // Low Memory Detection Support |
duke@435 | 33 | // Two memory alarms in the JDK (we called them sensors). |
duke@435 | 34 | // - Heap memory sensor |
duke@435 | 35 | // - Non-heap memory sensor |
duke@435 | 36 | // When the VM detects if the memory usage of a memory pool has reached |
duke@435 | 37 | // or exceeded its threshold, it will trigger the sensor for the type |
duke@435 | 38 | // of the memory pool (heap or nonheap or both). |
duke@435 | 39 | // |
duke@435 | 40 | // If threshold == -1, no low memory detection is supported and |
duke@435 | 41 | // the threshold value is not allowed to be changed. |
duke@435 | 42 | // If threshold == 0, no low memory detection is performed for |
duke@435 | 43 | // that memory pool. The threshold can be set to any non-negative |
duke@435 | 44 | // value. |
duke@435 | 45 | // |
duke@435 | 46 | // The default threshold of the Hotspot memory pools are: |
duke@435 | 47 | // Eden space -1 |
duke@435 | 48 | // Survivor space 1 -1 |
duke@435 | 49 | // Survivor space 2 -1 |
duke@435 | 50 | // Old generation 0 |
duke@435 | 51 | // Perm generation 0 |
duke@435 | 52 | // CodeCache 0 |
duke@435 | 53 | // |
duke@435 | 54 | // For heap memory, detection will be performed when GC finishes |
duke@435 | 55 | // and also in the slow path allocation. |
duke@435 | 56 | // For Code cache, detection will be performed in the allocation |
duke@435 | 57 | // and deallocation. |
duke@435 | 58 | // |
duke@435 | 59 | // May need to deal with hysteresis effect. |
duke@435 | 60 | // |
kamg@2511 | 61 | // Memory detection code runs in the Service thread (serviceThread.hpp). |
duke@435 | 62 | |
duke@435 | 63 | class OopClosure; |
duke@435 | 64 | class MemoryPool; |
duke@435 | 65 | |
zgu@3900 | 66 | class ThresholdSupport : public CHeapObj<mtInternal> { |
duke@435 | 67 | private: |
duke@435 | 68 | bool _support_high_threshold; |
duke@435 | 69 | bool _support_low_threshold; |
duke@435 | 70 | size_t _high_threshold; |
duke@435 | 71 | size_t _low_threshold; |
duke@435 | 72 | public: |
duke@435 | 73 | ThresholdSupport(bool support_high, bool support_low) { |
duke@435 | 74 | _support_high_threshold = support_high; |
duke@435 | 75 | _support_low_threshold = support_low; |
duke@435 | 76 | _high_threshold = 0; |
duke@435 | 77 | _low_threshold= 0; |
duke@435 | 78 | } |
duke@435 | 79 | |
duke@435 | 80 | size_t high_threshold() const { return _high_threshold; } |
duke@435 | 81 | size_t low_threshold() const { return _low_threshold; } |
duke@435 | 82 | bool is_high_threshold_supported() { return _support_high_threshold; } |
duke@435 | 83 | bool is_low_threshold_supported() { return _support_low_threshold; } |
duke@435 | 84 | |
duke@435 | 85 | bool is_high_threshold_crossed(MemoryUsage usage) { |
duke@435 | 86 | if (_support_high_threshold && _high_threshold > 0) { |
duke@435 | 87 | return (usage.used() >= _high_threshold); |
duke@435 | 88 | } |
duke@435 | 89 | return false; |
duke@435 | 90 | } |
duke@435 | 91 | bool is_low_threshold_crossed(MemoryUsage usage) { |
duke@435 | 92 | if (_support_low_threshold && _low_threshold > 0) { |
duke@435 | 93 | return (usage.used() < _low_threshold); |
duke@435 | 94 | } |
duke@435 | 95 | return false; |
duke@435 | 96 | } |
duke@435 | 97 | |
duke@435 | 98 | size_t set_high_threshold(size_t new_threshold) { |
duke@435 | 99 | assert(_support_high_threshold, "can only be set if supported"); |
duke@435 | 100 | assert(new_threshold >= _low_threshold, "new_threshold must be >= _low_threshold"); |
duke@435 | 101 | size_t prev = _high_threshold; |
duke@435 | 102 | _high_threshold = new_threshold; |
duke@435 | 103 | return prev; |
duke@435 | 104 | } |
duke@435 | 105 | |
duke@435 | 106 | size_t set_low_threshold(size_t new_threshold) { |
duke@435 | 107 | assert(_support_low_threshold, "can only be set if supported"); |
duke@435 | 108 | assert(new_threshold <= _high_threshold, "new_threshold must be <= _high_threshold"); |
duke@435 | 109 | size_t prev = _low_threshold; |
duke@435 | 110 | _low_threshold = new_threshold; |
duke@435 | 111 | return prev; |
duke@435 | 112 | } |
duke@435 | 113 | }; |
duke@435 | 114 | |
zgu@3900 | 115 | class SensorInfo : public CHeapObj<mtInternal> { |
duke@435 | 116 | private: |
duke@435 | 117 | instanceOop _sensor_obj; |
duke@435 | 118 | bool _sensor_on; |
duke@435 | 119 | size_t _sensor_count; |
duke@435 | 120 | |
duke@435 | 121 | // before the actual sensor on flag and sensor count are set |
duke@435 | 122 | // we maintain the number of pending triggers and clears. |
duke@435 | 123 | // _pending_trigger_count means the number of pending triggers |
duke@435 | 124 | // and the sensor count should be incremented by the same number. |
duke@435 | 125 | |
duke@435 | 126 | int _pending_trigger_count; |
duke@435 | 127 | |
duke@435 | 128 | // _pending_clear_count takes precedence if it's > 0 which |
duke@435 | 129 | // indicates the resulting sensor will be off |
duke@435 | 130 | // Sensor trigger requests will reset this clear count to |
duke@435 | 131 | // indicate the resulting flag should be on. |
duke@435 | 132 | |
duke@435 | 133 | int _pending_clear_count; |
duke@435 | 134 | |
duke@435 | 135 | MemoryUsage _usage; |
duke@435 | 136 | |
duke@435 | 137 | void clear(int count, TRAPS); |
duke@435 | 138 | void trigger(int count, TRAPS); |
duke@435 | 139 | public: |
duke@435 | 140 | SensorInfo(); |
duke@435 | 141 | void set_sensor(instanceOop sensor) { |
duke@435 | 142 | assert(_sensor_obj == NULL, "Should be set only once"); |
duke@435 | 143 | _sensor_obj = sensor; |
duke@435 | 144 | } |
duke@435 | 145 | |
duke@435 | 146 | bool has_pending_requests() { |
duke@435 | 147 | return (_pending_trigger_count > 0 || _pending_clear_count > 0); |
duke@435 | 148 | } |
duke@435 | 149 | |
duke@435 | 150 | int pending_trigger_count() { return _pending_trigger_count; } |
duke@435 | 151 | int pending_clear_count() { return _pending_clear_count; } |
duke@435 | 152 | |
duke@435 | 153 | // When this method is used, the memory usage is monitored |
duke@435 | 154 | // as a gauge attribute. High and low thresholds are designed |
duke@435 | 155 | // to provide a hysteresis mechanism to avoid repeated triggering |
duke@435 | 156 | // of notifications when the attribute value makes small oscillations |
duke@435 | 157 | // around the high or low threshold value. |
duke@435 | 158 | // |
duke@435 | 159 | // The sensor will be triggered if: |
duke@435 | 160 | // (1) the usage is crossing above the high threshold and |
duke@435 | 161 | // the sensor is currently off and no pending |
duke@435 | 162 | // trigger requests; or |
duke@435 | 163 | // (2) the usage is crossing above the high threshold and |
duke@435 | 164 | // the sensor will be off (i.e. sensor is currently on |
duke@435 | 165 | // and has pending clear requests). |
duke@435 | 166 | // |
duke@435 | 167 | // Subsequent crossings of the high threshold value do not cause |
duke@435 | 168 | // any triggers unless the usage becomes less than the low threshold. |
duke@435 | 169 | // |
duke@435 | 170 | // The sensor will be cleared if: |
duke@435 | 171 | // (1) the usage is crossing below the low threshold and |
duke@435 | 172 | // the sensor is currently on and no pending |
duke@435 | 173 | // clear requests; or |
duke@435 | 174 | // (2) the usage is crossing below the low threshold and |
duke@435 | 175 | // the sensor will be on (i.e. sensor is currently off |
duke@435 | 176 | // and has pending trigger requests). |
duke@435 | 177 | // |
duke@435 | 178 | // Subsequent crossings of the low threshold value do not cause |
duke@435 | 179 | // any clears unless the usage becomes greater than or equal |
duke@435 | 180 | // to the high threshold. |
duke@435 | 181 | // |
duke@435 | 182 | // If the current level is between high and low threhsold, no change. |
duke@435 | 183 | // |
duke@435 | 184 | void set_gauge_sensor_level(MemoryUsage usage, ThresholdSupport* high_low_threshold); |
duke@435 | 185 | |
duke@435 | 186 | // When this method is used, the memory usage is monitored as a |
duke@435 | 187 | // simple counter attribute. The sensor will be triggered |
duke@435 | 188 | // whenever the usage is crossing the threshold to keep track |
duke@435 | 189 | // of the number of times the VM detects such a condition occurs. |
duke@435 | 190 | // |
duke@435 | 191 | // The sensor will be triggered if: |
duke@435 | 192 | // - the usage is crossing above the high threshold regardless |
duke@435 | 193 | // of the current sensor state. |
duke@435 | 194 | // |
duke@435 | 195 | // The sensor will be cleared if: |
duke@435 | 196 | // (1) the usage is crossing below the low threshold and |
duke@435 | 197 | // the sensor is currently on; or |
duke@435 | 198 | // (2) the usage is crossing below the low threshold and |
duke@435 | 199 | // the sensor will be on (i.e. sensor is currently off |
duke@435 | 200 | // and has pending trigger requests). |
duke@435 | 201 | // |
duke@435 | 202 | void set_counter_sensor_level(MemoryUsage usage, ThresholdSupport* counter_threshold); |
duke@435 | 203 | |
duke@435 | 204 | void process_pending_requests(TRAPS); |
duke@435 | 205 | void oops_do(OopClosure* f); |
duke@435 | 206 | |
duke@435 | 207 | #ifndef PRODUCT |
duke@435 | 208 | // printing on default output stream; |
duke@435 | 209 | void print(); |
duke@435 | 210 | #endif // PRODUCT |
duke@435 | 211 | }; |
duke@435 | 212 | |
duke@435 | 213 | class LowMemoryDetector : public AllStatic { |
kamg@2511 | 214 | friend class LowMemoryDetectorDisabler; |
kamg@2511 | 215 | friend class ServiceThread; |
duke@435 | 216 | private: |
duke@435 | 217 | // true if any collected heap has low memory detection enabled |
duke@435 | 218 | static volatile bool _enabled_for_collected_pools; |
duke@435 | 219 | // > 0 if temporary disabed |
duke@435 | 220 | static volatile jint _disabled_count; |
duke@435 | 221 | |
duke@435 | 222 | static void check_memory_usage(); |
duke@435 | 223 | static bool has_pending_requests(); |
duke@435 | 224 | static bool temporary_disabled() { return _disabled_count > 0; } |
duke@435 | 225 | static void disable() { Atomic::inc(&_disabled_count); } |
duke@435 | 226 | static void enable() { Atomic::dec(&_disabled_count); } |
kamg@2511 | 227 | static void process_sensor_changes(TRAPS); |
duke@435 | 228 | |
duke@435 | 229 | public: |
duke@435 | 230 | static void detect_low_memory(); |
duke@435 | 231 | static void detect_low_memory(MemoryPool* pool); |
duke@435 | 232 | static void detect_after_gc_memory(MemoryPool* pool); |
duke@435 | 233 | |
duke@435 | 234 | static bool is_enabled(MemoryPool* pool) { |
duke@435 | 235 | // low memory detection is enabled for collected memory pools |
duke@435 | 236 | // iff one of the collected memory pool has a sensor and the |
duke@435 | 237 | // threshold set non-zero |
duke@435 | 238 | if (pool->usage_sensor() == NULL) { |
duke@435 | 239 | return false; |
duke@435 | 240 | } else { |
duke@435 | 241 | ThresholdSupport* threshold_support = pool->usage_threshold(); |
duke@435 | 242 | return (threshold_support->is_high_threshold_supported() ? |
duke@435 | 243 | (threshold_support->high_threshold() > 0) : false); |
duke@435 | 244 | } |
duke@435 | 245 | } |
duke@435 | 246 | |
duke@435 | 247 | // indicates if low memory detection is enabled for any collected |
duke@435 | 248 | // memory pools |
duke@435 | 249 | static inline bool is_enabled_for_collected_pools() { |
duke@435 | 250 | return !temporary_disabled() && _enabled_for_collected_pools; |
duke@435 | 251 | } |
duke@435 | 252 | |
duke@435 | 253 | // recompute enabled flag |
duke@435 | 254 | static void recompute_enabled_for_collected_pools(); |
duke@435 | 255 | |
duke@435 | 256 | // low memory detection for collected memory pools. |
duke@435 | 257 | static inline void detect_low_memory_for_collected_pools() { |
duke@435 | 258 | // no-op if low memory detection not enabled |
duke@435 | 259 | if (!is_enabled_for_collected_pools()) { |
duke@435 | 260 | return; |
duke@435 | 261 | } |
duke@435 | 262 | int num_memory_pools = MemoryService::num_memory_pools(); |
duke@435 | 263 | for (int i=0; i<num_memory_pools; i++) { |
duke@435 | 264 | MemoryPool* pool = MemoryService::get_memory_pool(i); |
duke@435 | 265 | |
duke@435 | 266 | // if low memory detection is enabled then check if the |
duke@435 | 267 | // current used exceeds the high threshold |
duke@435 | 268 | if (pool->is_collected_pool() && is_enabled(pool)) { |
duke@435 | 269 | size_t used = pool->used_in_bytes(); |
duke@435 | 270 | size_t high = pool->usage_threshold()->high_threshold(); |
duke@435 | 271 | if (used > high) { |
duke@435 | 272 | detect_low_memory(pool); |
duke@435 | 273 | } |
duke@435 | 274 | } |
duke@435 | 275 | } |
duke@435 | 276 | } |
duke@435 | 277 | }; |
duke@435 | 278 | |
duke@435 | 279 | class LowMemoryDetectorDisabler: public StackObj { |
duke@435 | 280 | public: |
duke@435 | 281 | LowMemoryDetectorDisabler() |
duke@435 | 282 | { |
duke@435 | 283 | LowMemoryDetector::disable(); |
duke@435 | 284 | } |
duke@435 | 285 | ~LowMemoryDetectorDisabler() |
duke@435 | 286 | { |
duke@435 | 287 | assert(LowMemoryDetector::temporary_disabled(), "should be disabled!"); |
duke@435 | 288 | LowMemoryDetector::enable(); |
duke@435 | 289 | } |
duke@435 | 290 | }; |
stefank@2314 | 291 | |
stefank@2314 | 292 | #endif // SHARE_VM_SERVICES_LOWMEMORYDETECTOR_HPP |