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