lowMemoryDetector.hpp 10.2 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31
#ifndef SHARE_VM_SERVICES_LOWMEMORYDETECTOR_HPP
#define SHARE_VM_SERVICES_LOWMEMORYDETECTOR_HPP

#include "memory/allocation.hpp"
#include "services/memoryPool.hpp"
#include "services/memoryService.hpp"

D
duke 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
// Low Memory Detection Support
// Two memory alarms in the JDK (we called them sensors).
//   - Heap memory sensor
//   - Non-heap memory sensor
// When the VM detects if the memory usage of a memory pool has reached
// or exceeded its threshold, it will trigger the sensor for the type
// of the memory pool (heap or nonheap or both).
//
// If threshold == -1, no low memory detection is supported and
// the threshold value is not allowed to be changed.
// If threshold == 0, no low memory detection is performed for
// that memory pool.  The threshold can be set to any non-negative
// value.
//
// The default threshold of the Hotspot memory pools are:
//   Eden space        -1
//   Survivor space 1  -1
//   Survivor space 2  -1
//   Old generation    0
//   Perm generation   0
//   CodeCache         0
//
// For heap memory, detection will be performed when GC finishes
// and also in the slow path allocation.
// For Code cache, detection will be performed in the allocation
// and deallocation.
//
// May need to deal with hysteresis effect.
//
61
// Memory detection code runs in the Service thread (serviceThread.hpp).
D
duke 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

class OopClosure;
class MemoryPool;

class ThresholdSupport : public CHeapObj {
 private:
  bool            _support_high_threshold;
  bool            _support_low_threshold;
  size_t          _high_threshold;
  size_t          _low_threshold;
 public:
  ThresholdSupport(bool support_high, bool support_low) {
    _support_high_threshold = support_high;
    _support_low_threshold = support_low;
    _high_threshold = 0;
    _low_threshold= 0;
  }

  size_t      high_threshold() const        { return _high_threshold; }
  size_t      low_threshold()  const        { return _low_threshold; }
  bool        is_high_threshold_supported() { return _support_high_threshold; }
  bool        is_low_threshold_supported()  { return _support_low_threshold; }

  bool        is_high_threshold_crossed(MemoryUsage usage) {
    if (_support_high_threshold && _high_threshold > 0) {
      return (usage.used() >= _high_threshold);
    }
    return false;
  }
  bool        is_low_threshold_crossed(MemoryUsage usage) {
    if (_support_low_threshold && _low_threshold > 0) {
      return (usage.used() < _low_threshold);
    }
    return false;
  }

  size_t      set_high_threshold(size_t new_threshold) {
    assert(_support_high_threshold, "can only be set if supported");
    assert(new_threshold >= _low_threshold, "new_threshold must be >= _low_threshold");
    size_t prev = _high_threshold;
    _high_threshold = new_threshold;
    return prev;
  }

  size_t      set_low_threshold(size_t new_threshold) {
    assert(_support_low_threshold, "can only be set if supported");
    assert(new_threshold <= _high_threshold, "new_threshold must be <= _high_threshold");
    size_t prev = _low_threshold;
    _low_threshold = new_threshold;
    return prev;
  }
};

class SensorInfo : public CHeapObj {
private:
  instanceOop     _sensor_obj;
  bool            _sensor_on;
  size_t          _sensor_count;

  // before the actual sensor on flag and sensor count are set
  // we maintain the number of pending triggers and clears.
  // _pending_trigger_count means the number of pending triggers
  // and the sensor count should be incremented by the same number.

  int             _pending_trigger_count;

  // _pending_clear_count takes precedence if it's > 0 which
  // indicates the resulting sensor will be off
  // Sensor trigger requests will reset this clear count to
  // indicate the resulting flag should be on.

  int             _pending_clear_count;

  MemoryUsage     _usage;

  void clear(int count, TRAPS);
  void trigger(int count, TRAPS);
public:
  SensorInfo();
  void set_sensor(instanceOop sensor) {
    assert(_sensor_obj == NULL, "Should be set only once");
    _sensor_obj = sensor;
  }

  bool has_pending_requests() {
    return (_pending_trigger_count > 0 || _pending_clear_count > 0);
  }

  int pending_trigger_count()      { return _pending_trigger_count; }
  int pending_clear_count()        { return _pending_clear_count; }

  // When this method is used, the memory usage is monitored
  // as a gauge attribute.  High and low thresholds are designed
  // to provide a hysteresis mechanism to avoid repeated triggering
  // of notifications when the attribute value makes small oscillations
  // around the high or low threshold value.
  //
  // The sensor will be triggered if:
  //  (1) the usage is crossing above the high threshold and
  //      the sensor is currently off and no pending
  //      trigger requests; or
  //  (2) the usage is crossing above the high threshold and
  //      the sensor will be off (i.e. sensor is currently on
  //      and has pending clear requests).
  //
  // Subsequent crossings of the high threshold value do not cause
  // any triggers unless the usage becomes less than the low threshold.
  //
  // The sensor will be cleared if:
  //  (1) the usage is crossing below the low threshold and
  //      the sensor is currently on and no pending
  //      clear requests; or
  //  (2) the usage is crossing below the low threshold and
  //      the sensor will be on (i.e. sensor is currently off
  //      and has pending trigger requests).
  //
  // Subsequent crossings of the low threshold value do not cause
  // any clears unless the usage becomes greater than or equal
  // to the high threshold.
  //
  // If the current level is between high and low threhsold, no change.
  //
  void set_gauge_sensor_level(MemoryUsage usage, ThresholdSupport* high_low_threshold);

  // When this method is used, the memory usage is monitored as a
  // simple counter attribute.  The sensor will be triggered
  // whenever the usage is crossing the threshold to keep track
  // of the number of times the VM detects such a condition occurs.
  //
  // The sensor will be triggered if:
  //   - the usage is crossing above the high threshold regardless
  //     of the current sensor state.
  //
  // The sensor will be cleared if:
  //  (1) the usage is crossing below the low threshold and
  //      the sensor is currently on; or
  //  (2) the usage is crossing below the low threshold and
  //      the sensor will be on (i.e. sensor is currently off
  //      and has pending trigger requests).
  //
  void set_counter_sensor_level(MemoryUsage usage, ThresholdSupport* counter_threshold);

  void process_pending_requests(TRAPS);
  void oops_do(OopClosure* f);

#ifndef PRODUCT
  // printing on default output stream;
  void print();
#endif // PRODUCT
};

class LowMemoryDetector : public AllStatic {
214 215
  friend class LowMemoryDetectorDisabler;
  friend class ServiceThread;
D
duke 已提交
216 217 218 219 220 221 222 223 224 225 226
private:
  // true if any collected heap has low memory detection enabled
  static volatile bool _enabled_for_collected_pools;
  // > 0 if temporary disabed
  static volatile jint _disabled_count;

  static void check_memory_usage();
  static bool has_pending_requests();
  static bool temporary_disabled() { return _disabled_count > 0; }
  static void disable() { Atomic::inc(&_disabled_count); }
  static void enable() { Atomic::dec(&_disabled_count); }
227
  static void process_sensor_changes(TRAPS);
D
duke 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

public:
  static void detect_low_memory();
  static void detect_low_memory(MemoryPool* pool);
  static void detect_after_gc_memory(MemoryPool* pool);

  static bool is_enabled(MemoryPool* pool) {
    // low memory detection is enabled for collected memory pools
    // iff one of the collected memory pool has a sensor and the
    // threshold set non-zero
    if (pool->usage_sensor() == NULL) {
      return false;
    } else {
      ThresholdSupport* threshold_support = pool->usage_threshold();
      return (threshold_support->is_high_threshold_supported() ?
               (threshold_support->high_threshold() > 0) : false);
    }
  }

  // indicates if low memory detection is enabled for any collected
  // memory pools
  static inline bool is_enabled_for_collected_pools() {
    return !temporary_disabled() && _enabled_for_collected_pools;
  }

  // recompute enabled flag
  static void recompute_enabled_for_collected_pools();

  // low memory detection for collected memory pools.
  static inline void detect_low_memory_for_collected_pools() {
    // no-op if low memory detection not enabled
    if (!is_enabled_for_collected_pools()) {
      return;
    }
    int num_memory_pools = MemoryService::num_memory_pools();
    for (int i=0; i<num_memory_pools; i++) {
      MemoryPool* pool = MemoryService::get_memory_pool(i);

      // if low memory detection is enabled then check if the
      // current used exceeds the high threshold
      if (pool->is_collected_pool() && is_enabled(pool)) {
        size_t used = pool->used_in_bytes();
        size_t high = pool->usage_threshold()->high_threshold();
        if (used > high) {
          detect_low_memory(pool);
        }
      }
    }
  }
};

class LowMemoryDetectorDisabler: public StackObj {
public:
  LowMemoryDetectorDisabler()
  {
    LowMemoryDetector::disable();
  }
  ~LowMemoryDetectorDisabler()
  {
    assert(LowMemoryDetector::temporary_disabled(), "should be disabled!");
    LowMemoryDetector::enable();
  }
};
291 292

#endif // SHARE_VM_SERVICES_LOWMEMORYDETECTOR_HPP