src/share/vm/services/lowMemoryDetector.cpp

Mon, 28 May 2018 10:33:52 +0800

author
aoqi
date
Mon, 28 May 2018 10:33:52 +0800
changeset 9041
95a08233f46c
parent 8876
8a9294fa59d6
parent 6876
710a3c8b516e
permissions
-rw-r--r--

Merge

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

mercurial