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