src/share/vm/services/lowMemoryDetector.cpp

Fri, 14 Jan 2011 13:47:53 -0500

author
coleenp
date
Fri, 14 Jan 2011 13:47:53 -0500
changeset 2463
17c778814856
parent 2314
f95d63e2154a
child 2497
3582bf76420e
permissions
-rw-r--r--

6811367: Fix code in HeapDumper::dump_heap() to avoid buffer overrun
Summary: Check buffer size before using and use dynamic buffer sizes for subsequent calls.
Reviewed-by: kamg, dholmes

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

mercurial