hotspot/src/share/vm/services/lowMemoryDetector.cpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 4571 80b553bddc26
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_lowMemoryDetector.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
LowMemoryDetectorThread* LowMemoryDetector::_detector_thread = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
volatile bool LowMemoryDetector::_enabled_for_collected_pools = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
volatile jint LowMemoryDetector::_disabled_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
void LowMemoryDetector::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  EXCEPTION_MARK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  instanceKlassHandle klass (THREAD,  SystemDictionary::thread_klass());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  instanceHandle thread_oop = klass->allocate_instance_handle(CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  const char thread_name[] = "Low Memory Detector";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  Handle string = java_lang_String::create_from_str(thread_name, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  // Initialize thread_oop to put it into the system threadGroup
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  Handle thread_group (THREAD, Universe::system_thread_group());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  JavaValue result(T_VOID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  JavaCalls::call_special(&result, thread_oop,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
                          klass,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
                          vmSymbolHandles::object_initializer_name(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
                          vmSymbolHandles::threadgroup_string_void_signature(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
                          thread_group,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
                          string,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
                          CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    MutexLocker mu(Threads_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    _detector_thread = new LowMemoryDetectorThread(&low_memory_detector_thread_entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    // At this point it may be possible that no osthread was created for the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    // JavaThread due to lack of memory. We would have to throw an exception
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    // in that case. However, since this must work and we do not allow
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    // exceptions anyway, check and abort if this fails.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    if (_detector_thread == NULL || _detector_thread->osthread() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
      vm_exit_during_initialization("java.lang.OutOfMemoryError",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
                                    "unable to create new native thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
    java_lang_Thread::set_thread(thread_oop(), _detector_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
    java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    java_lang_Thread::set_daemon(thread_oop());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    _detector_thread->set_threadObj(thread_oop());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    Threads::add(_detector_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    Thread::start(_detector_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
bool LowMemoryDetector::has_pending_requests() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  assert(LowMemory_lock->owned_by_self(), "Must own LowMemory_lock");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  bool has_requests = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  int num_memory_pools = MemoryService::num_memory_pools();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  for (int i = 0; i < num_memory_pools; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
    MemoryPool* pool = MemoryService::get_memory_pool(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
    SensorInfo* sensor = pool->usage_sensor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    if (sensor != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
      has_requests = has_requests || sensor->has_pending_requests();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    SensorInfo* gc_sensor = pool->gc_usage_sensor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    if (gc_sensor != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
      has_requests = has_requests || gc_sensor->has_pending_requests();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  return has_requests;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
void LowMemoryDetector::low_memory_detector_thread_entry(JavaThread* jt, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  while (true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    bool   sensors_changed = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
      // _no_safepoint_check_flag is used here as LowMemory_lock is a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
      // special lock and the VMThread may acquire this lock at safepoint.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
      // Need state transition ThreadBlockInVM so that this thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
      // will be handled by safepoint correctly when this thread is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
      // notified at a safepoint.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
      // This ThreadBlockInVM object is not also considered to be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
      // suspend-equivalent because LowMemoryDetector threads are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
      // not visible to external suspension.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
      ThreadBlockInVM tbivm(jt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      MutexLockerEx ml(LowMemory_lock, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
      while (!(sensors_changed = has_pending_requests())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
        // wait until one of the sensors has pending requests
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
        LowMemory_lock->wait(Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
      ResourceMark rm(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
      HandleMark hm(THREAD);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
      // No need to hold LowMemory_lock to call out to Java
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      int num_memory_pools = MemoryService::num_memory_pools();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      for (int i = 0; i < num_memory_pools; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
        MemoryPool* pool = MemoryService::get_memory_pool(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
        SensorInfo* sensor = pool->usage_sensor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
        SensorInfo* gc_sensor = pool->gc_usage_sensor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
        if (sensor != NULL && sensor->has_pending_requests()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
          sensor->process_pending_requests(CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
        if (gc_sensor != NULL && gc_sensor->has_pending_requests()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
          gc_sensor->process_pending_requests(CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
// This method could be called from any Java threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
// and also VMThread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
void LowMemoryDetector::detect_low_memory() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  MutexLockerEx ml(LowMemory_lock, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  bool has_pending_requests = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  int num_memory_pools = MemoryService::num_memory_pools();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  for (int i = 0; i < num_memory_pools; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    MemoryPool* pool = MemoryService::get_memory_pool(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    SensorInfo* sensor = pool->usage_sensor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    if (sensor != NULL &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
        pool->usage_threshold()->is_high_threshold_supported() &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
        pool->usage_threshold()->high_threshold() != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
      MemoryUsage usage = pool->get_memory_usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
      sensor->set_gauge_sensor_level(usage,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
                                     pool->usage_threshold());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
      has_pending_requests = has_pending_requests || sensor->has_pending_requests();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  if (has_pending_requests) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    LowMemory_lock->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
// This method could be called from any Java threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
// and also VMThread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
void LowMemoryDetector::detect_low_memory(MemoryPool* pool) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  SensorInfo* sensor = pool->usage_sensor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  if (sensor == NULL ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
      !pool->usage_threshold()->is_high_threshold_supported() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
      pool->usage_threshold()->high_threshold() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    MutexLockerEx ml(LowMemory_lock, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
    MemoryUsage usage = pool->get_memory_usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    sensor->set_gauge_sensor_level(usage,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
                                   pool->usage_threshold());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    if (sensor->has_pending_requests()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
      // notify sensor state update
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
      LowMemory_lock->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
// Only called by VMThread at GC time
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
void LowMemoryDetector::detect_after_gc_memory(MemoryPool* pool) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  SensorInfo* sensor = pool->gc_usage_sensor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  if (sensor == NULL ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
      !pool->gc_usage_threshold()->is_high_threshold_supported() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
      pool->gc_usage_threshold()->high_threshold() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    MutexLockerEx ml(LowMemory_lock, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    MemoryUsage usage = pool->get_last_collection_usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    sensor->set_counter_sensor_level(usage, pool->gc_usage_threshold());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
    if (sensor->has_pending_requests()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
      // notify sensor state update
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
      LowMemory_lock->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
// recompute enabled flag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
void LowMemoryDetector::recompute_enabled_for_collected_pools() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  bool enabled = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  int num_memory_pools = MemoryService::num_memory_pools();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  for (int i=0; i<num_memory_pools; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
    MemoryPool* pool = MemoryService::get_memory_pool(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
    if (pool->is_collected_pool() && is_enabled(pool)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
      enabled = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  _enabled_for_collected_pools = enabled;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
SensorInfo::SensorInfo() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  _sensor_obj = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  _sensor_on = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  _sensor_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  _pending_trigger_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  _pending_clear_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
// When this method is used, the memory usage is monitored
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
// as a gauge attribute.  Sensor notifications (trigger or
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
// clear) is only emitted at the first time it crosses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
// a threshold.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
// High and low thresholds are designed to provide a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
// hysteresis mechanism to avoid repeated triggering
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
// of notifications when the attribute value makes small oscillations
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
// around the high or low threshold value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
// The sensor will be triggered if:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
//  (1) the usage is crossing above the high threshold and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
//      the sensor is currently off and no pending
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
//      trigger requests; or
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
//  (2) the usage is crossing above the high threshold and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
//      the sensor will be off (i.e. sensor is currently on
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
//      and has pending clear requests).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
// Subsequent crossings of the high threshold value do not cause
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
// any triggers unless the usage becomes less than the low threshold.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
// The sensor will be cleared if:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
//  (1) the usage is crossing below the low threshold and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
//      the sensor is currently on and no pending
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
//      clear requests; or
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
//  (2) the usage is crossing below the low threshold and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
//      the sensor will be on (i.e. sensor is currently off
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
//      and has pending trigger requests).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
// Subsequent crossings of the low threshold value do not cause
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
// any clears unless the usage becomes greater than or equal
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
// to the high threshold.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
// If the current level is between high and low threhsold, no change.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
void SensorInfo::set_gauge_sensor_level(MemoryUsage usage, ThresholdSupport* high_low_threshold) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  assert(high_low_threshold->is_high_threshold_supported(), "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  bool is_over_high = high_low_threshold->is_high_threshold_crossed(usage);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  bool is_below_low = high_low_threshold->is_low_threshold_crossed(usage);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  assert(!(is_over_high && is_below_low), "Can't be both true");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  if (is_over_high &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
        ((!_sensor_on && _pending_trigger_count == 0) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
         _pending_clear_count > 0)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
    // low memory detected and need to increment the trigger pending count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    // if the sensor is off or will be off due to _pending_clear_ > 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    // Request to trigger the sensor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
    _pending_trigger_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
    _usage = usage;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
    if (_pending_clear_count > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
      // non-zero pending clear requests indicates that there are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
      // pending requests to clear this sensor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
      // This trigger request needs to clear this clear count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
      // since the resulting sensor flag should be on.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
      _pending_clear_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  } else if (is_below_low &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
               ((_sensor_on && _pending_clear_count == 0) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
                (_pending_trigger_count > 0 && _pending_clear_count == 0))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
    // memory usage returns below the threshold
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    // Request to clear the sensor if the sensor is on or will be on due to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    // _pending_trigger_count > 0 and also no clear request
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
    _pending_clear_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
// When this method is used, the memory usage is monitored as a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
// simple counter attribute.  The sensor will be triggered
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
// whenever the usage is crossing the threshold to keep track
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
// of the number of times the VM detects such a condition occurs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
// High and low thresholds are designed to provide a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
// hysteresis mechanism to avoid repeated triggering
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
// of notifications when the attribute value makes small oscillations
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
// around the high or low threshold value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
// The sensor will be triggered if:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
//   - the usage is crossing above the high threshold regardless
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
//     of the current sensor state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
// The sensor will be cleared if:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
//  (1) the usage is crossing below the low threshold and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
//      the sensor is currently on; or
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
//  (2) the usage is crossing below the low threshold and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
//      the sensor will be on (i.e. sensor is currently off
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
//      and has pending trigger requests).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
void SensorInfo::set_counter_sensor_level(MemoryUsage usage, ThresholdSupport* counter_threshold) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  assert(counter_threshold->is_high_threshold_supported(), "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  bool is_over_high = counter_threshold->is_high_threshold_crossed(usage);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  bool is_below_low = counter_threshold->is_low_threshold_crossed(usage);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  assert(!(is_over_high && is_below_low), "Can't be both true");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  if (is_over_high) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    _pending_trigger_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    _usage = usage;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    _pending_clear_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  } else if (is_below_low && (_sensor_on || _pending_trigger_count > 0)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    _pending_clear_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
void SensorInfo::oops_do(OopClosure* f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  f->do_oop((oop*) &_sensor_obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
void SensorInfo::process_pending_requests(TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  if (!has_pending_requests()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  int pending_count = pending_trigger_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  if (pending_clear_count() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    clear(pending_count, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    trigger(pending_count, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
void SensorInfo::trigger(int count, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  assert(count <= _pending_trigger_count, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  if (_sensor_obj != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    klassOop k = Management::sun_management_Sensor_klass(CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    instanceKlassHandle sensorKlass (THREAD, k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    Handle sensor_h(THREAD, _sensor_obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
    Handle usage_h = MemoryService::create_MemoryUsage_obj(_usage, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    JavaValue result(T_VOID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
    JavaCallArguments args(sensor_h);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
    args.push_int((int) count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    args.push_oop(usage_h);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    JavaCalls::call_virtual(&result,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
                            sensorKlass,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
                            vmSymbolHandles::trigger_name(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
                            vmSymbolHandles::trigger_method_signature(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
                            &args,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
                            CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
    // Holds LowMemory_lock and update the sensor state
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    MutexLockerEx ml(LowMemory_lock, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    _sensor_on = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    _sensor_count += count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    _pending_trigger_count = _pending_trigger_count - count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
void SensorInfo::clear(int count, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  if (_sensor_obj != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    klassOop k = Management::sun_management_Sensor_klass(CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    instanceKlassHandle sensorKlass (THREAD, k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
    Handle sensor(THREAD, _sensor_obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
    JavaValue result(T_VOID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
    JavaCallArguments args(sensor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    args.push_int((int) count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
    JavaCalls::call_virtual(&result,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
                            sensorKlass,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
                            vmSymbolHandles::clear_name(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
                            vmSymbolHandles::int_void_signature(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
                            &args,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
                            CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    // Holds LowMemory_lock and update the sensor state
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
    MutexLockerEx ml(LowMemory_lock, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
    _sensor_on = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
    _pending_clear_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
    _pending_trigger_count = _pending_trigger_count - count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
//--------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
// Non-product code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
void SensorInfo::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  tty->print_cr("%s count = %ld pending_triggers = %ld pending_clears = %ld",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
                (_sensor_on ? "on" : "off"),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
                _sensor_count, _pending_trigger_count, _pending_clear_count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
#endif // PRODUCT