< prev index next >

src/share/vm/services/memoryManager.cpp

Print this page


   1 /*
   2  * Copyright (c) 2003, 2014, 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  *


  32 #include "services/lowMemoryDetector.hpp"
  33 #include "services/management.hpp"
  34 #include "services/memoryManager.hpp"
  35 #include "services/memoryPool.hpp"
  36 #include "services/memoryService.hpp"
  37 #include "services/gcNotifier.hpp"
  38 #include "utilities/dtrace.hpp"
  39 
  40 #ifndef USDT2
  41 HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__begin, char*, int, char*, int,
  42   size_t, size_t, size_t, size_t);
  43 HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__end, char*, int, char*, int,
  44   size_t, size_t, size_t, size_t);
  45 #endif /* !USDT2 */
  46 
  47 MemoryManager::MemoryManager() {
  48   _num_pools = 0;
  49   (void)const_cast<instanceOop&>(_memory_mgr_obj = instanceOop(NULL));
  50 }
  51 
  52 void MemoryManager::add_pool(MemoryPool* pool) {
  53   assert(_num_pools < MemoryManager::max_num_pools, "_num_pools exceeds the max");
  54   if (_num_pools < MemoryManager::max_num_pools) {
  55     _pools[_num_pools] = pool;

  56     _num_pools++;
  57   }
  58   pool->add_manager(this);

  59 }
  60 
  61 MemoryManager* MemoryManager::get_code_cache_memory_manager() {
  62   return (MemoryManager*) new CodeCacheMemoryManager();
  63 }
  64 
  65 MemoryManager* MemoryManager::get_metaspace_memory_manager() {
  66   return (MemoryManager*) new MetaspaceMemoryManager();
  67 }
  68 
  69 GCMemoryManager* MemoryManager::get_copy_memory_manager() {
  70   return (GCMemoryManager*) new CopyMemoryManager();
  71 }
  72 
  73 GCMemoryManager* MemoryManager::get_msc_memory_manager() {
  74   return (GCMemoryManager*) new MSCMemoryManager();
  75 }
  76 
  77 GCMemoryManager* MemoryManager::get_parnew_memory_manager() {
  78   return (GCMemoryManager*) new ParNewMemoryManager();


 200   memset(_before_gc_usage_array, 0, len);
 201   memset(_after_gc_usage_array, 0, len);
 202 }
 203 
 204 
 205 GCMemoryManager::GCMemoryManager() : MemoryManager() {
 206   _num_collections = 0;
 207   _last_gc_stat = NULL;
 208   _last_gc_lock = new Mutex(Mutex::leaf, "_last_gc_lock", true);
 209   _current_gc_stat = NULL;
 210   _num_gc_threads = 1;
 211   _notification_enabled = false;
 212 }
 213 
 214 GCMemoryManager::~GCMemoryManager() {
 215   delete _last_gc_stat;
 216   delete _last_gc_lock;
 217   delete _current_gc_stat;
 218 }
 219 









 220 void GCMemoryManager::initialize_gc_stat_info() {
 221   assert(MemoryService::num_memory_pools() > 0, "should have one or more memory pools");
 222   _last_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());
 223   _current_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());
 224   // tracking concurrent collections we need two objects: one to update, and one to
 225   // hold the publicly available "last (completed) gc" information.
 226 }
 227 
 228 void GCMemoryManager::gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,
 229                                bool recordAccumulatedGCTime) {
 230   assert(_last_gc_stat != NULL && _current_gc_stat != NULL, "Just checking");
 231   if (recordAccumulatedGCTime) {
 232     _accumulated_timer.start();
 233   }
 234   // _num_collections now increases in gc_end, to count completed collections
 235   if (recordGCBeginTime) {
 236     _current_gc_stat->set_index(_num_collections+1);
 237     _current_gc_stat->set_start_time(Management::timestamp());
 238   }
 239 


 249         pool->name(), strlen(pool->name()),
 250         usage.init_size(), usage.used(),
 251         usage.committed(), usage.max_size());
 252 #else /* USDT2 */
 253       HOTSPOT_MEM_POOL_GC_BEGIN(
 254         (char *) name(), strlen(name()),
 255         (char *) pool->name(), strlen(pool->name()),
 256         usage.init_size(), usage.used(),
 257         usage.committed(), usage.max_size());
 258 #endif /* USDT2 */
 259     }
 260   }
 261 }
 262 
 263 // A collector MUST, even if it does not complete for some reason,
 264 // make a TraceMemoryManagerStats object where countCollection is true,
 265 // to ensure the current gc stat is placed in _last_gc_stat.
 266 void GCMemoryManager::gc_end(bool recordPostGCUsage,
 267                              bool recordAccumulatedGCTime,
 268                              bool recordGCEndTime, bool countCollection,
 269                              GCCause::Cause cause) {

 270   if (recordAccumulatedGCTime) {
 271     _accumulated_timer.stop();
 272   }
 273   if (recordGCEndTime) {
 274     _current_gc_stat->set_end_time(Management::timestamp());
 275   }
 276 
 277   if (recordPostGCUsage) {
 278     int i;
 279     // keep the last gc statistics for all memory pools
 280     for (i = 0; i < MemoryService::num_memory_pools(); i++) {
 281       MemoryPool* pool = MemoryService::get_memory_pool(i);
 282       MemoryUsage usage = pool->get_memory_usage();
 283 
 284 #ifndef USDT2
 285       HS_DTRACE_PROBE8(hotspot, mem__pool__gc__end,
 286         name(), strlen(name()),
 287         pool->name(), strlen(pool->name()),
 288         usage.init_size(), usage.used(),
 289         usage.committed(), usage.max_size());
 290 #else /* USDT2 */
 291       HOTSPOT_MEM_POOL_GC_END(
 292         (char *) name(), strlen(name()),
 293         (char *) pool->name(), strlen(pool->name()),
 294         usage.init_size(), usage.used(),
 295         usage.committed(), usage.max_size());
 296 #endif /* USDT2 */
 297 
 298       _current_gc_stat->set_after_gc_usage(i, usage);
 299     }
 300 
 301     // Set last collection usage of the memory pools managed by this collector
 302     for (i = 0; i < num_memory_pools(); i++) {
 303       MemoryPool* pool = get_memory_pool(i);
 304       MemoryUsage usage = pool->get_memory_usage();
 305 
 306       // Compare with GC usage threshold


 307       pool->set_last_collection_usage(usage);
 308       LowMemoryDetector::detect_after_gc_memory(pool);
 309     }
 310   }

 311 
 312   if (countCollection) {
 313     _num_collections++;
 314     // alternately update two objects making one public when complete
 315     {
 316       MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);
 317       GCStatInfo *tmp = _last_gc_stat;
 318       _last_gc_stat = _current_gc_stat;
 319       _current_gc_stat = tmp;
 320       // reset the current stat for diagnosability purposes
 321       _current_gc_stat->clear();
 322     }
 323 
 324     if (is_notification_enabled()) {
 325       bool isMajorGC = this == MemoryService::get_major_gc_manager();
 326       GCNotifier::pushNotification(this, isMajorGC ? "end of major GC" : "end of minor GC",
 327                                    GCCause::to_string(cause));
 328     }
 329   }
 330 }
   1 /*
   2  * Copyright (c) 2003, 2019, 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  *


  32 #include "services/lowMemoryDetector.hpp"
  33 #include "services/management.hpp"
  34 #include "services/memoryManager.hpp"
  35 #include "services/memoryPool.hpp"
  36 #include "services/memoryService.hpp"
  37 #include "services/gcNotifier.hpp"
  38 #include "utilities/dtrace.hpp"
  39 
  40 #ifndef USDT2
  41 HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__begin, char*, int, char*, int,
  42   size_t, size_t, size_t, size_t);
  43 HS_DTRACE_PROBE_DECL8(hotspot, mem__pool__gc__end, char*, int, char*, int,
  44   size_t, size_t, size_t, size_t);
  45 #endif /* !USDT2 */
  46 
  47 MemoryManager::MemoryManager() {
  48   _num_pools = 0;
  49   (void)const_cast<instanceOop&>(_memory_mgr_obj = instanceOop(NULL));
  50 }
  51 
  52 int MemoryManager::add_pool(MemoryPool* pool) {
  53   int index = _num_pools;
  54   assert(index < MemoryManager::max_num_pools, "_num_pools exceeds the max");
  55   if (index < MemoryManager::max_num_pools) {
  56     _pools[index] = pool;
  57     _num_pools++;
  58   }
  59   pool->add_manager(this);
  60   return index;
  61 }
  62 
  63 MemoryManager* MemoryManager::get_code_cache_memory_manager() {
  64   return (MemoryManager*) new CodeCacheMemoryManager();
  65 }
  66 
  67 MemoryManager* MemoryManager::get_metaspace_memory_manager() {
  68   return (MemoryManager*) new MetaspaceMemoryManager();
  69 }
  70 
  71 GCMemoryManager* MemoryManager::get_copy_memory_manager() {
  72   return (GCMemoryManager*) new CopyMemoryManager();
  73 }
  74 
  75 GCMemoryManager* MemoryManager::get_msc_memory_manager() {
  76   return (GCMemoryManager*) new MSCMemoryManager();
  77 }
  78 
  79 GCMemoryManager* MemoryManager::get_parnew_memory_manager() {
  80   return (GCMemoryManager*) new ParNewMemoryManager();


 202   memset(_before_gc_usage_array, 0, len);
 203   memset(_after_gc_usage_array, 0, len);
 204 }
 205 
 206 
 207 GCMemoryManager::GCMemoryManager() : MemoryManager() {
 208   _num_collections = 0;
 209   _last_gc_stat = NULL;
 210   _last_gc_lock = new Mutex(Mutex::leaf, "_last_gc_lock", true);
 211   _current_gc_stat = NULL;
 212   _num_gc_threads = 1;
 213   _notification_enabled = false;
 214 }
 215 
 216 GCMemoryManager::~GCMemoryManager() {
 217   delete _last_gc_stat;
 218   delete _last_gc_lock;
 219   delete _current_gc_stat;
 220 }
 221 
 222 void GCMemoryManager::add_pool(MemoryPool* pool) {
 223   add_pool(pool, true);
 224 }
 225 
 226 void GCMemoryManager::add_pool(MemoryPool* pool, bool always_affected_by_gc) {
 227   int index = MemoryManager::add_pool(pool);
 228   _pool_always_affected_by_gc[index] = always_affected_by_gc;
 229 }
 230 
 231 void GCMemoryManager::initialize_gc_stat_info() {
 232   assert(MemoryService::num_memory_pools() > 0, "should have one or more memory pools");
 233   _last_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());
 234   _current_gc_stat = new(ResourceObj::C_HEAP, mtGC) GCStatInfo(MemoryService::num_memory_pools());
 235   // tracking concurrent collections we need two objects: one to update, and one to
 236   // hold the publicly available "last (completed) gc" information.
 237 }
 238 
 239 void GCMemoryManager::gc_begin(bool recordGCBeginTime, bool recordPreGCUsage,
 240                                bool recordAccumulatedGCTime) {
 241   assert(_last_gc_stat != NULL && _current_gc_stat != NULL, "Just checking");
 242   if (recordAccumulatedGCTime) {
 243     _accumulated_timer.start();
 244   }
 245   // _num_collections now increases in gc_end, to count completed collections
 246   if (recordGCBeginTime) {
 247     _current_gc_stat->set_index(_num_collections+1);
 248     _current_gc_stat->set_start_time(Management::timestamp());
 249   }
 250 


 260         pool->name(), strlen(pool->name()),
 261         usage.init_size(), usage.used(),
 262         usage.committed(), usage.max_size());
 263 #else /* USDT2 */
 264       HOTSPOT_MEM_POOL_GC_BEGIN(
 265         (char *) name(), strlen(name()),
 266         (char *) pool->name(), strlen(pool->name()),
 267         usage.init_size(), usage.used(),
 268         usage.committed(), usage.max_size());
 269 #endif /* USDT2 */
 270     }
 271   }
 272 }
 273 
 274 // A collector MUST, even if it does not complete for some reason,
 275 // make a TraceMemoryManagerStats object where countCollection is true,
 276 // to ensure the current gc stat is placed in _last_gc_stat.
 277 void GCMemoryManager::gc_end(bool recordPostGCUsage,
 278                              bool recordAccumulatedGCTime,
 279                              bool recordGCEndTime, bool countCollection,
 280                              GCCause::Cause cause,
 281                              bool allMemoryPoolsAffected) {
 282   if (recordAccumulatedGCTime) {
 283     _accumulated_timer.stop();
 284   }
 285   if (recordGCEndTime) {
 286     _current_gc_stat->set_end_time(Management::timestamp());
 287   }
 288 
 289   if (recordPostGCUsage) {
 290     int i;
 291     // keep the last gc statistics for all memory pools
 292     for (i = 0; i < MemoryService::num_memory_pools(); i++) {
 293       MemoryPool* pool = MemoryService::get_memory_pool(i);
 294       MemoryUsage usage = pool->get_memory_usage();
 295 
 296 #ifndef USDT2
 297       HS_DTRACE_PROBE8(hotspot, mem__pool__gc__end,
 298         name(), strlen(name()),
 299         pool->name(), strlen(pool->name()),
 300         usage.init_size(), usage.used(),
 301         usage.committed(), usage.max_size());
 302 #else /* USDT2 */
 303       HOTSPOT_MEM_POOL_GC_END(
 304         (char *) name(), strlen(name()),
 305         (char *) pool->name(), strlen(pool->name()),
 306         usage.init_size(), usage.used(),
 307         usage.committed(), usage.max_size());
 308 #endif /* USDT2 */
 309 
 310       _current_gc_stat->set_after_gc_usage(i, usage);
 311     }
 312 
 313     // Set last collection usage of the memory pools managed by this collector
 314     for (i = 0; i < num_memory_pools(); i++) {
 315       MemoryPool* pool = get_memory_pool(i);
 316       MemoryUsage usage = pool->get_memory_usage();
 317 
 318       // Compare with GC usage threshold
 319       if (allMemoryPoolsAffected || pool_always_affected_by_gc(i)) {
 320         // Compare with GC usage threshold
 321         pool->set_last_collection_usage(usage);
 322         LowMemoryDetector::detect_after_gc_memory(pool);
 323       }
 324     }
 325   }
 326 
 327   if (countCollection) {
 328     _num_collections++;
 329     // alternately update two objects making one public when complete
 330     {
 331       MutexLockerEx ml(_last_gc_lock, Mutex::_no_safepoint_check_flag);
 332       GCStatInfo *tmp = _last_gc_stat;
 333       _last_gc_stat = _current_gc_stat;
 334       _current_gc_stat = tmp;
 335       // reset the current stat for diagnosability purposes
 336       _current_gc_stat->clear();
 337     }
 338 
 339     if (is_notification_enabled()) {
 340       bool isMajorGC = this == MemoryService::get_major_gc_manager();
 341       GCNotifier::pushNotification(this, isMajorGC ? "end of major GC" : "end of minor GC",
 342                                    GCCause::to_string(cause));
 343     }
 344   }
 345 }
< prev index next >