src/share/vm/services/lowMemoryDetector.hpp

Thu, 27 May 2010 19:08:38 -0700

author
trims
date
Thu, 27 May 2010 19:08:38 -0700
changeset 1907
c18cbe5936b8
parent 435
a61af66fc99e
child 2314
f95d63e2154a
permissions
-rw-r--r--

6941466: Oracle rebranding changes for Hotspot repositories
Summary: Change all the Sun copyrights to Oracle copyright
Reviewed-by: ohair

duke@435 1 /*
trims@1907 2 * Copyright (c) 2003, 2007, 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
duke@435 25 // Low Memory Detection Support
duke@435 26 // Two memory alarms in the JDK (we called them sensors).
duke@435 27 // - Heap memory sensor
duke@435 28 // - Non-heap memory sensor
duke@435 29 // When the VM detects if the memory usage of a memory pool has reached
duke@435 30 // or exceeded its threshold, it will trigger the sensor for the type
duke@435 31 // of the memory pool (heap or nonheap or both).
duke@435 32 //
duke@435 33 // If threshold == -1, no low memory detection is supported and
duke@435 34 // the threshold value is not allowed to be changed.
duke@435 35 // If threshold == 0, no low memory detection is performed for
duke@435 36 // that memory pool. The threshold can be set to any non-negative
duke@435 37 // value.
duke@435 38 //
duke@435 39 // The default threshold of the Hotspot memory pools are:
duke@435 40 // Eden space -1
duke@435 41 // Survivor space 1 -1
duke@435 42 // Survivor space 2 -1
duke@435 43 // Old generation 0
duke@435 44 // Perm generation 0
duke@435 45 // CodeCache 0
duke@435 46 //
duke@435 47 // For heap memory, detection will be performed when GC finishes
duke@435 48 // and also in the slow path allocation.
duke@435 49 // For Code cache, detection will be performed in the allocation
duke@435 50 // and deallocation.
duke@435 51 //
duke@435 52 // May need to deal with hysteresis effect.
duke@435 53 //
duke@435 54
duke@435 55 class LowMemoryDetectorThread;
duke@435 56 class OopClosure;
duke@435 57 class MemoryPool;
duke@435 58
duke@435 59 class ThresholdSupport : public CHeapObj {
duke@435 60 private:
duke@435 61 bool _support_high_threshold;
duke@435 62 bool _support_low_threshold;
duke@435 63 size_t _high_threshold;
duke@435 64 size_t _low_threshold;
duke@435 65 public:
duke@435 66 ThresholdSupport(bool support_high, bool support_low) {
duke@435 67 _support_high_threshold = support_high;
duke@435 68 _support_low_threshold = support_low;
duke@435 69 _high_threshold = 0;
duke@435 70 _low_threshold= 0;
duke@435 71 }
duke@435 72
duke@435 73 size_t high_threshold() const { return _high_threshold; }
duke@435 74 size_t low_threshold() const { return _low_threshold; }
duke@435 75 bool is_high_threshold_supported() { return _support_high_threshold; }
duke@435 76 bool is_low_threshold_supported() { return _support_low_threshold; }
duke@435 77
duke@435 78 bool is_high_threshold_crossed(MemoryUsage usage) {
duke@435 79 if (_support_high_threshold && _high_threshold > 0) {
duke@435 80 return (usage.used() >= _high_threshold);
duke@435 81 }
duke@435 82 return false;
duke@435 83 }
duke@435 84 bool is_low_threshold_crossed(MemoryUsage usage) {
duke@435 85 if (_support_low_threshold && _low_threshold > 0) {
duke@435 86 return (usage.used() < _low_threshold);
duke@435 87 }
duke@435 88 return false;
duke@435 89 }
duke@435 90
duke@435 91 size_t set_high_threshold(size_t new_threshold) {
duke@435 92 assert(_support_high_threshold, "can only be set if supported");
duke@435 93 assert(new_threshold >= _low_threshold, "new_threshold must be >= _low_threshold");
duke@435 94 size_t prev = _high_threshold;
duke@435 95 _high_threshold = new_threshold;
duke@435 96 return prev;
duke@435 97 }
duke@435 98
duke@435 99 size_t set_low_threshold(size_t new_threshold) {
duke@435 100 assert(_support_low_threshold, "can only be set if supported");
duke@435 101 assert(new_threshold <= _high_threshold, "new_threshold must be <= _high_threshold");
duke@435 102 size_t prev = _low_threshold;
duke@435 103 _low_threshold = new_threshold;
duke@435 104 return prev;
duke@435 105 }
duke@435 106 };
duke@435 107
duke@435 108 class SensorInfo : public CHeapObj {
duke@435 109 private:
duke@435 110 instanceOop _sensor_obj;
duke@435 111 bool _sensor_on;
duke@435 112 size_t _sensor_count;
duke@435 113
duke@435 114 // before the actual sensor on flag and sensor count are set
duke@435 115 // we maintain the number of pending triggers and clears.
duke@435 116 // _pending_trigger_count means the number of pending triggers
duke@435 117 // and the sensor count should be incremented by the same number.
duke@435 118
duke@435 119 int _pending_trigger_count;
duke@435 120
duke@435 121 // _pending_clear_count takes precedence if it's > 0 which
duke@435 122 // indicates the resulting sensor will be off
duke@435 123 // Sensor trigger requests will reset this clear count to
duke@435 124 // indicate the resulting flag should be on.
duke@435 125
duke@435 126 int _pending_clear_count;
duke@435 127
duke@435 128 MemoryUsage _usage;
duke@435 129
duke@435 130 void clear(int count, TRAPS);
duke@435 131 void trigger(int count, TRAPS);
duke@435 132 public:
duke@435 133 SensorInfo();
duke@435 134 void set_sensor(instanceOop sensor) {
duke@435 135 assert(_sensor_obj == NULL, "Should be set only once");
duke@435 136 _sensor_obj = sensor;
duke@435 137 }
duke@435 138
duke@435 139 bool has_pending_requests() {
duke@435 140 return (_pending_trigger_count > 0 || _pending_clear_count > 0);
duke@435 141 }
duke@435 142
duke@435 143 int pending_trigger_count() { return _pending_trigger_count; }
duke@435 144 int pending_clear_count() { return _pending_clear_count; }
duke@435 145
duke@435 146 // When this method is used, the memory usage is monitored
duke@435 147 // as a gauge attribute. High and low thresholds are designed
duke@435 148 // to provide a hysteresis mechanism to avoid repeated triggering
duke@435 149 // of notifications when the attribute value makes small oscillations
duke@435 150 // around the high or low threshold value.
duke@435 151 //
duke@435 152 // The sensor will be triggered if:
duke@435 153 // (1) the usage is crossing above the high threshold and
duke@435 154 // the sensor is currently off and no pending
duke@435 155 // trigger requests; or
duke@435 156 // (2) the usage is crossing above the high threshold and
duke@435 157 // the sensor will be off (i.e. sensor is currently on
duke@435 158 // and has pending clear requests).
duke@435 159 //
duke@435 160 // Subsequent crossings of the high threshold value do not cause
duke@435 161 // any triggers unless the usage becomes less than the low threshold.
duke@435 162 //
duke@435 163 // The sensor will be cleared if:
duke@435 164 // (1) the usage is crossing below the low threshold and
duke@435 165 // the sensor is currently on and no pending
duke@435 166 // clear requests; or
duke@435 167 // (2) the usage is crossing below the low threshold and
duke@435 168 // the sensor will be on (i.e. sensor is currently off
duke@435 169 // and has pending trigger requests).
duke@435 170 //
duke@435 171 // Subsequent crossings of the low threshold value do not cause
duke@435 172 // any clears unless the usage becomes greater than or equal
duke@435 173 // to the high threshold.
duke@435 174 //
duke@435 175 // If the current level is between high and low threhsold, no change.
duke@435 176 //
duke@435 177 void set_gauge_sensor_level(MemoryUsage usage, ThresholdSupport* high_low_threshold);
duke@435 178
duke@435 179 // When this method is used, the memory usage is monitored as a
duke@435 180 // simple counter attribute. The sensor will be triggered
duke@435 181 // whenever the usage is crossing the threshold to keep track
duke@435 182 // of the number of times the VM detects such a condition occurs.
duke@435 183 //
duke@435 184 // The sensor will be triggered if:
duke@435 185 // - the usage is crossing above the high threshold regardless
duke@435 186 // of the current sensor state.
duke@435 187 //
duke@435 188 // The sensor will be cleared if:
duke@435 189 // (1) the usage is crossing below the low threshold and
duke@435 190 // the sensor is currently on; or
duke@435 191 // (2) the usage is crossing below the low threshold and
duke@435 192 // the sensor will be on (i.e. sensor is currently off
duke@435 193 // and has pending trigger requests).
duke@435 194 //
duke@435 195 void set_counter_sensor_level(MemoryUsage usage, ThresholdSupport* counter_threshold);
duke@435 196
duke@435 197 void process_pending_requests(TRAPS);
duke@435 198 void oops_do(OopClosure* f);
duke@435 199
duke@435 200 #ifndef PRODUCT
duke@435 201 // printing on default output stream;
duke@435 202 void print();
duke@435 203 #endif // PRODUCT
duke@435 204 };
duke@435 205
duke@435 206 class LowMemoryDetector : public AllStatic {
duke@435 207 friend class LowMemoryDetectorDisabler;
duke@435 208 private:
duke@435 209 // true if any collected heap has low memory detection enabled
duke@435 210 static volatile bool _enabled_for_collected_pools;
duke@435 211 // > 0 if temporary disabed
duke@435 212 static volatile jint _disabled_count;
duke@435 213
duke@435 214 static LowMemoryDetectorThread* _detector_thread;
duke@435 215 static void low_memory_detector_thread_entry(JavaThread* thread, TRAPS);
duke@435 216 static void check_memory_usage();
duke@435 217 static bool has_pending_requests();
duke@435 218 static bool temporary_disabled() { return _disabled_count > 0; }
duke@435 219 static void disable() { Atomic::inc(&_disabled_count); }
duke@435 220 static void enable() { Atomic::dec(&_disabled_count); }
duke@435 221
duke@435 222 public:
duke@435 223 static void initialize();
duke@435 224 static void detect_low_memory();
duke@435 225 static void detect_low_memory(MemoryPool* pool);
duke@435 226 static void detect_after_gc_memory(MemoryPool* pool);
duke@435 227
duke@435 228 static bool is_enabled(MemoryPool* pool) {
duke@435 229 // low memory detection is enabled for collected memory pools
duke@435 230 // iff one of the collected memory pool has a sensor and the
duke@435 231 // threshold set non-zero
duke@435 232 if (pool->usage_sensor() == NULL) {
duke@435 233 return false;
duke@435 234 } else {
duke@435 235 ThresholdSupport* threshold_support = pool->usage_threshold();
duke@435 236 return (threshold_support->is_high_threshold_supported() ?
duke@435 237 (threshold_support->high_threshold() > 0) : false);
duke@435 238 }
duke@435 239 }
duke@435 240
duke@435 241 // indicates if low memory detection is enabled for any collected
duke@435 242 // memory pools
duke@435 243 static inline bool is_enabled_for_collected_pools() {
duke@435 244 return !temporary_disabled() && _enabled_for_collected_pools;
duke@435 245 }
duke@435 246
duke@435 247 // recompute enabled flag
duke@435 248 static void recompute_enabled_for_collected_pools();
duke@435 249
duke@435 250 // low memory detection for collected memory pools.
duke@435 251 static inline void detect_low_memory_for_collected_pools() {
duke@435 252 // no-op if low memory detection not enabled
duke@435 253 if (!is_enabled_for_collected_pools()) {
duke@435 254 return;
duke@435 255 }
duke@435 256 int num_memory_pools = MemoryService::num_memory_pools();
duke@435 257 for (int i=0; i<num_memory_pools; i++) {
duke@435 258 MemoryPool* pool = MemoryService::get_memory_pool(i);
duke@435 259
duke@435 260 // if low memory detection is enabled then check if the
duke@435 261 // current used exceeds the high threshold
duke@435 262 if (pool->is_collected_pool() && is_enabled(pool)) {
duke@435 263 size_t used = pool->used_in_bytes();
duke@435 264 size_t high = pool->usage_threshold()->high_threshold();
duke@435 265 if (used > high) {
duke@435 266 detect_low_memory(pool);
duke@435 267 }
duke@435 268 }
duke@435 269 }
duke@435 270 }
duke@435 271
duke@435 272 };
duke@435 273
duke@435 274 class LowMemoryDetectorDisabler: public StackObj {
duke@435 275 public:
duke@435 276 LowMemoryDetectorDisabler()
duke@435 277 {
duke@435 278 LowMemoryDetector::disable();
duke@435 279 }
duke@435 280 ~LowMemoryDetectorDisabler()
duke@435 281 {
duke@435 282 assert(LowMemoryDetector::temporary_disabled(), "should be disabled!");
duke@435 283 LowMemoryDetector::enable();
duke@435 284 }
duke@435 285 };

mercurial