src/share/vm/services/lowMemoryDetector.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 4153
b9a9ed0f8eeb
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

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

mercurial