src/share/vm/services/lowMemoryDetector.cpp

Thu, 12 Mar 2009 18:16:36 -0700

author
trims
date
Thu, 12 Mar 2009 18:16:36 -0700
changeset 1063
7bb995fbd3c0
parent 435
a61af66fc99e
child 1577
4ce7240d622c
permissions
-rw-r--r--

Merge

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

mercurial