< prev index next >

src/hotspot/share/services/memoryService.cpp

Print this page
rev 47957 : 8191564: Refactor GC related servicability code into GC specific subclasses


   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 "gc/parallel/mutableSpace.hpp"
  29 #include "gc/serial/defNewGeneration.hpp"
  30 #include "gc/serial/tenuredGeneration.hpp"
  31 #include "gc/shared/collectorPolicy.hpp"
  32 #include "gc/shared/genCollectedHeap.hpp"
  33 #include "gc/shared/generation.hpp"
  34 #include "gc/shared/generationSpec.hpp"
  35 #include "logging/logConfiguration.hpp"
  36 #include "memory/heap.hpp"
  37 #include "memory/memRegion.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "runtime/globals.hpp"
  40 #include "runtime/javaCalls.hpp"
  41 #include "services/classLoadingService.hpp"
  42 #include "services/lowMemoryDetector.hpp"
  43 #include "services/management.hpp"
  44 #include "services/memoryManager.hpp"
  45 #include "services/memoryPool.hpp"
  46 #include "services/memoryService.hpp"
  47 #include "utilities/growableArray.hpp"
  48 #include "utilities/macros.hpp"
  49 #if INCLUDE_ALL_GCS
  50 #include "gc/cms/concurrentMarkSweepGeneration.hpp"
  51 #include "gc/cms/parNewGeneration.hpp"
  52 #include "gc/g1/g1CollectedHeap.inline.hpp"
  53 #include "gc/parallel/parallelScavengeHeap.hpp"
  54 #include "gc/parallel/psOldGen.hpp"
  55 #include "gc/parallel/psYoungGen.hpp"
  56 #include "services/g1MemoryPool.hpp"
  57 #include "services/psMemoryPool.hpp"
  58 #endif // INCLUDE_ALL_GCS
  59 
  60 GrowableArray<MemoryPool*>* MemoryService::_pools_list =
  61   new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryPool*>(init_pools_list_size, true);
  62 GrowableArray<MemoryManager*>* MemoryService::_managers_list =
  63   new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryManager*>(init_managers_list_size, true);
  64 
  65 GCMemoryManager* MemoryService::_minor_gc_manager      = NULL;
  66 GCMemoryManager* MemoryService::_major_gc_manager      = NULL;
  67 MemoryManager*   MemoryService::_code_cache_manager    = NULL;
  68 GrowableArray<MemoryPool*>* MemoryService::_code_heap_pools =
  69     new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryPool*>(init_code_heap_pools_size, true);
  70 MemoryPool*      MemoryService::_metaspace_pool        = NULL;
  71 MemoryPool*      MemoryService::_compressed_class_pool = NULL;
  72 
  73 class GcThreadCountClosure: public ThreadClosure {
  74  private:
  75   int _count;
  76  public:
  77   GcThreadCountClosure() : _count(0) {};
  78   void do_thread(Thread* thread);
  79   int count() { return _count; }
  80 };
  81 
  82 void GcThreadCountClosure::do_thread(Thread* thread) {
  83   _count++;
  84 }
  85 
  86 void MemoryService::set_universe_heap(CollectedHeap* heap) {
  87   CollectedHeap::Name kind = heap->kind();
  88   switch (kind) {
  89     case CollectedHeap::SerialHeap :
  90     case CollectedHeap::CMSHeap : {
  91       add_gen_collected_heap_info(GenCollectedHeap::heap());
  92       break;
  93     }
  94 #if INCLUDE_ALL_GCS
  95     case CollectedHeap::ParallelScavengeHeap : {
  96       add_parallel_scavenge_heap_info(ParallelScavengeHeap::heap());
  97       break;
  98     }
  99     case CollectedHeap::G1CollectedHeap : {
 100       add_g1_heap_info(G1CollectedHeap::heap());
 101       break;
 102     }
 103 #endif // INCLUDE_ALL_GCS
 104     default: {
 105       guarantee(false, "Unrecognized kind of heap");
 106     }
 107   }
 108 
 109   // set the GC thread count
 110   GcThreadCountClosure gctcc;
 111   heap->gc_threads_do(&gctcc);
 112   int count = gctcc.count();





 113   if (count > 0) {
 114     _minor_gc_manager->set_num_gc_threads(count);
 115     _major_gc_manager->set_num_gc_threads(count);
 116   }
 117 
 118   // All memory pools and memory managers are initialized.
 119   //
 120   _minor_gc_manager->initialize_gc_stat_info();
 121   _major_gc_manager->initialize_gc_stat_info();
 122 }
 123 
 124 // Add memory pools for GenCollectedHeap
 125 // This function currently only supports two generations collected heap.
 126 // The collector for GenCollectedHeap will have two memory managers.
 127 void MemoryService::add_gen_collected_heap_info(GenCollectedHeap* heap) {
 128   CollectorPolicy* policy = heap->collector_policy();
 129 
 130   assert(policy->is_generation_policy(), "Only support two generations");
 131   GenCollectorPolicy* gen_policy = policy->as_generation_policy();
 132   if (gen_policy != NULL) {
 133     Generation::Name kind = gen_policy->young_gen_spec()->name();
 134     switch (kind) {
 135       case Generation::DefNew:
 136         _minor_gc_manager = MemoryManager::get_copy_memory_manager();
 137         break;
 138 #if INCLUDE_ALL_GCS
 139       case Generation::ParNew:
 140         _minor_gc_manager = MemoryManager::get_parnew_memory_manager();
 141         break;
 142 #endif // INCLUDE_ALL_GCS
 143       default:
 144         guarantee(false, "Unrecognized generation spec");
 145         break;
 146     }
 147     if (policy->is_mark_sweep_policy()) {
 148       _major_gc_manager = MemoryManager::get_msc_memory_manager();
 149 #if INCLUDE_ALL_GCS
 150     } else if (policy->is_concurrent_mark_sweep_policy()) {
 151       _major_gc_manager = MemoryManager::get_cms_memory_manager();
 152 #endif // INCLUDE_ALL_GCS
 153     } else {
 154       guarantee(false, "Unknown two-gen policy");
 155     }
 156   } else {
 157     guarantee(false, "Non two-gen policy");
 158   }
 159   _managers_list->append(_minor_gc_manager);
 160   _managers_list->append(_major_gc_manager);
 161 
 162   add_generation_memory_pool(heap->young_gen(), _major_gc_manager, _minor_gc_manager);
 163   add_generation_memory_pool(heap->old_gen(), _major_gc_manager);
 164 }
 165 
 166 #if INCLUDE_ALL_GCS
 167 // Add memory pools for ParallelScavengeHeap
 168 // This function currently only supports two generations collected heap.
 169 // The collector for ParallelScavengeHeap will have two memory managers.
 170 void MemoryService::add_parallel_scavenge_heap_info(ParallelScavengeHeap* heap) {
 171   // Two managers to keep statistics about _minor_gc_manager and _major_gc_manager GC.
 172   _minor_gc_manager = MemoryManager::get_psScavenge_memory_manager();
 173   _major_gc_manager = MemoryManager::get_psMarkSweep_memory_manager();
 174   _managers_list->append(_minor_gc_manager);
 175   _managers_list->append(_major_gc_manager);
 176 
 177   add_psYoung_memory_pool(heap->young_gen(), _major_gc_manager, _minor_gc_manager);
 178   add_psOld_memory_pool(heap->old_gen(), _major_gc_manager);
 179 }
 180 
 181 void MemoryService::add_g1_heap_info(G1CollectedHeap* g1h) {
 182   assert(UseG1GC, "sanity");
 183 
 184   _minor_gc_manager = MemoryManager::get_g1YoungGen_memory_manager();
 185   _major_gc_manager = MemoryManager::get_g1OldGen_memory_manager();
 186   _managers_list->append(_minor_gc_manager);
 187   _managers_list->append(_major_gc_manager);
 188 
 189   add_g1YoungGen_memory_pool(g1h, _major_gc_manager, _minor_gc_manager);
 190   add_g1OldGen_memory_pool(g1h, _major_gc_manager);
 191 }
 192 #endif // INCLUDE_ALL_GCS
 193 
 194 MemoryPool* MemoryService::add_gen(Generation* gen,
 195                                    const char* name,
 196                                    bool is_heap,
 197                                    bool support_usage_threshold) {
 198 
 199   MemoryPool::PoolType type = (is_heap ? MemoryPool::Heap : MemoryPool::NonHeap);
 200   GenerationPool* pool = new GenerationPool(gen, name, type, support_usage_threshold);
 201   _pools_list->append(pool);
 202   return (MemoryPool*) pool;
 203 }
 204 
 205 MemoryPool* MemoryService::add_space(ContiguousSpace* space,
 206                                      const char* name,
 207                                      bool is_heap,
 208                                      size_t max_size,
 209                                      bool support_usage_threshold) {
 210   MemoryPool::PoolType type = (is_heap ? MemoryPool::Heap : MemoryPool::NonHeap);
 211   ContiguousSpacePool* pool = new ContiguousSpacePool(space, name, type, max_size, support_usage_threshold);
 212 
 213   _pools_list->append(pool);
 214   return (MemoryPool*) pool;
 215 }
 216 
 217 MemoryPool* MemoryService::add_survivor_spaces(DefNewGeneration* young_gen,
 218                                                const char* name,
 219                                                bool is_heap,
 220                                                size_t max_size,
 221                                                bool support_usage_threshold) {
 222   MemoryPool::PoolType type = (is_heap ? MemoryPool::Heap : MemoryPool::NonHeap);
 223   SurvivorContiguousSpacePool* pool = new SurvivorContiguousSpacePool(young_gen, name, type, max_size, support_usage_threshold);
 224 
 225   _pools_list->append(pool);
 226   return (MemoryPool*) pool;
 227 }
 228 
 229 #if INCLUDE_ALL_GCS
 230 MemoryPool* MemoryService::add_cms_space(CompactibleFreeListSpace* space,
 231                                          const char* name,
 232                                          bool is_heap,
 233                                          size_t max_size,
 234                                          bool support_usage_threshold) {
 235   MemoryPool::PoolType type = (is_heap ? MemoryPool::Heap : MemoryPool::NonHeap);
 236   CompactibleFreeListSpacePool* pool = new CompactibleFreeListSpacePool(space, name, type, max_size, support_usage_threshold);
 237   _pools_list->append(pool);
 238   return (MemoryPool*) pool;
 239 }
 240 #endif // INCLUDE_ALL_GCS
 241 
 242 // Add memory pool(s) for one generation
 243 void MemoryService::add_generation_memory_pool(Generation* gen,
 244                                                MemoryManager* major_mgr,
 245                                                MemoryManager* minor_mgr) {
 246   guarantee(gen != NULL, "No generation for memory pool");
 247   Generation::Name kind = gen->kind();
 248   int index = _pools_list->length();
 249 
 250   switch (kind) {
 251     case Generation::DefNew: {
 252       assert(major_mgr != NULL && minor_mgr != NULL, "Should have two managers");
 253       DefNewGeneration* young_gen = (DefNewGeneration*) gen;
 254       // Add a memory pool for each space and young gen doesn't
 255       // support low memory detection as it is expected to get filled up.
 256       MemoryPool* eden = add_space(young_gen->eden(),
 257                                    "Eden Space",
 258                                    true, /* is_heap */
 259                                    young_gen->max_eden_size(),
 260                                    false /* support_usage_threshold */);
 261       MemoryPool* survivor = add_survivor_spaces(young_gen,
 262                                                  "Survivor Space",
 263                                                  true, /* is_heap */
 264                                                  young_gen->max_survivor_size(),
 265                                                  false /* support_usage_threshold */);
 266       break;
 267     }
 268 
 269 #if INCLUDE_ALL_GCS
 270     case Generation::ParNew:
 271     {
 272       assert(major_mgr != NULL && minor_mgr != NULL, "Should have two managers");
 273       // Add a memory pool for each space and young gen doesn't
 274       // support low memory detection as it is expected to get filled up.
 275       ParNewGeneration* parnew_gen = (ParNewGeneration*) gen;
 276       MemoryPool* eden = add_space(parnew_gen->eden(),
 277                                    "Par Eden Space",
 278                                    true /* is_heap */,
 279                                    parnew_gen->max_eden_size(),
 280                                    false /* support_usage_threshold */);
 281       MemoryPool* survivor = add_survivor_spaces(parnew_gen,
 282                                                  "Par Survivor Space",
 283                                                  true, /* is_heap */
 284                                                  parnew_gen->max_survivor_size(),
 285                                                  false /* support_usage_threshold */);
 286 
 287       break;
 288     }
 289 #endif // INCLUDE_ALL_GCS
 290 
 291     case Generation::MarkSweepCompact: {
 292       assert(major_mgr != NULL && minor_mgr == NULL, "Should have only one manager");
 293       add_gen(gen,
 294               "Tenured Gen",
 295               true, /* is_heap */
 296               true  /* support_usage_threshold */);
 297       break;
 298     }
 299 
 300 #if INCLUDE_ALL_GCS
 301     case Generation::ConcurrentMarkSweep:
 302     {
 303       assert(major_mgr != NULL && minor_mgr == NULL, "Should have only one manager");
 304       ConcurrentMarkSweepGeneration* cms = (ConcurrentMarkSweepGeneration*) gen;
 305       MemoryPool* pool = add_cms_space(cms->cmsSpace(),
 306                                        "CMS Old Gen",
 307                                        true, /* is_heap */
 308                                        cms->reserved().byte_size(),
 309                                        true  /* support_usage_threshold */);
 310       break;
 311     }
 312 #endif // INCLUDE_ALL_GCS
 313 
 314     default:
 315       assert(false, "should not reach here");
 316       // no memory pool added for others
 317       break;
 318   }
 319 
 320   assert(major_mgr != NULL, "Should have at least one manager");
 321   // Link managers and the memory pools together
 322   for (int i = index; i < _pools_list->length(); i++) {
 323     MemoryPool* pool = _pools_list->at(i);
 324     major_mgr->add_pool(pool);
 325     if (minor_mgr != NULL) {
 326       minor_mgr->add_pool(pool);
 327     }
 328   }
 329 }
 330 
 331 
 332 #if INCLUDE_ALL_GCS
 333 void MemoryService::add_psYoung_memory_pool(PSYoungGen* young_gen, MemoryManager* major_mgr, MemoryManager* minor_mgr) {
 334   assert(major_mgr != NULL && minor_mgr != NULL, "Should have two managers");
 335 
 336   // Add a memory pool for each space and young gen doesn't
 337   // support low memory detection as it is expected to get filled up.
 338   EdenMutableSpacePool* eden = new EdenMutableSpacePool(young_gen,
 339                                                         young_gen->eden_space(),
 340                                                         "PS Eden Space",
 341                                                         MemoryPool::Heap,
 342                                                         false /* support_usage_threshold */);
 343 
 344   SurvivorMutableSpacePool* survivor = new SurvivorMutableSpacePool(young_gen,
 345                                                                     "PS Survivor Space",
 346                                                                     MemoryPool::Heap,
 347                                                                     false /* support_usage_threshold */);
 348 
 349   major_mgr->add_pool(eden);
 350   major_mgr->add_pool(survivor);
 351   minor_mgr->add_pool(eden);
 352   minor_mgr->add_pool(survivor);
 353   _pools_list->append(eden);
 354   _pools_list->append(survivor);
 355 }
 356 
 357 void MemoryService::add_psOld_memory_pool(PSOldGen* old_gen, MemoryManager* mgr) {
 358   PSGenerationPool* old_gen_pool = new PSGenerationPool(old_gen,
 359                                                        "PS Old Gen",
 360                                                        MemoryPool::Heap,
 361                                                        true /* support_usage_threshold */);
 362   mgr->add_pool(old_gen_pool);
 363   _pools_list->append(old_gen_pool);
 364 }
 365 
 366 void MemoryService::add_g1YoungGen_memory_pool(G1CollectedHeap* g1h,
 367                                                MemoryManager* major_mgr,
 368                                                MemoryManager* minor_mgr) {
 369   assert(major_mgr != NULL && minor_mgr != NULL, "should have two managers");
 370 
 371   G1EdenPool* eden = new G1EdenPool(g1h);
 372   G1SurvivorPool* survivor = new G1SurvivorPool(g1h);
 373 
 374   major_mgr->add_pool(eden);
 375   major_mgr->add_pool(survivor);
 376   minor_mgr->add_pool(eden);
 377   minor_mgr->add_pool(survivor);
 378   _pools_list->append(eden);
 379   _pools_list->append(survivor);
 380 }
 381 
 382 void MemoryService::add_g1OldGen_memory_pool(G1CollectedHeap* g1h,
 383                                              MemoryManager* mgr) {
 384   assert(mgr != NULL, "should have one manager");
 385 
 386   G1OldGenPool* old_gen = new G1OldGenPool(g1h);
 387   mgr->add_pool(old_gen);
 388   _pools_list->append(old_gen);
 389 }
 390 #endif // INCLUDE_ALL_GCS
 391 
 392 void MemoryService::add_code_heap_memory_pool(CodeHeap* heap, const char* name) {
 393   // Create new memory pool for this heap
 394   MemoryPool* code_heap_pool = new CodeHeapPool(heap, name, true /* support_usage_threshold */);
 395 
 396   // Append to lists
 397   _code_heap_pools->append(code_heap_pool);
 398   _pools_list->append(code_heap_pool);
 399 
 400   if (_code_cache_manager == NULL) {
 401     // Create CodeCache memory manager
 402     _code_cache_manager = MemoryManager::get_code_cache_memory_manager();
 403     _managers_list->append(_code_cache_manager);
 404   }
 405 
 406   _code_cache_manager->add_pool(code_heap_pool);
 407 }
 408 
 409 void MemoryService::add_metaspace_memory_pools() {
 410   MemoryManager* mgr = MemoryManager::get_metaspace_memory_manager();
 411 


 446   // Track the peak memory usage
 447   for (int i = 0; i < _pools_list->length(); i++) {
 448     MemoryPool* pool = _pools_list->at(i);
 449     pool->record_peak_memory_usage();
 450   }
 451 
 452   // Detect low memory
 453   LowMemoryDetector::detect_low_memory();
 454 }
 455 
 456 void MemoryService::track_memory_pool_usage(MemoryPool* pool) {
 457   // Track the peak memory usage
 458   pool->record_peak_memory_usage();
 459 
 460   // Detect low memory
 461   if (LowMemoryDetector::is_enabled(pool)) {
 462     LowMemoryDetector::detect_low_memory(pool);
 463   }
 464 }
 465 
 466 void MemoryService::gc_begin(bool fullGC, bool recordGCBeginTime,
 467                              bool recordAccumulatedGCTime,
 468                              bool recordPreGCUsage, bool recordPeakUsage) {
 469 
 470   GCMemoryManager* mgr;
 471   if (fullGC) {
 472     mgr = _major_gc_manager;
 473   } else {
 474     mgr = _minor_gc_manager;
 475   }
 476   assert(mgr->is_gc_memory_manager(), "Sanity check");
 477   mgr->gc_begin(recordGCBeginTime, recordPreGCUsage, recordAccumulatedGCTime);
 478 
 479   // Track the peak memory usage when GC begins
 480   if (recordPeakUsage) {
 481     for (int i = 0; i < _pools_list->length(); i++) {
 482       MemoryPool* pool = _pools_list->at(i);
 483       pool->record_peak_memory_usage();
 484     }
 485   }
 486 }
 487 
 488 void MemoryService::gc_end(bool fullGC, bool recordPostGCUsage,
 489                            bool recordAccumulatedGCTime,
 490                            bool recordGCEndTime, bool countCollection,
 491                            GCCause::Cause cause) {
 492 
 493   GCMemoryManager* mgr;
 494   if (fullGC) {
 495     mgr = (GCMemoryManager*) _major_gc_manager;
 496   } else {
 497     mgr = (GCMemoryManager*) _minor_gc_manager;
 498   }
 499   assert(mgr->is_gc_memory_manager(), "Sanity check");
 500 
 501   // register the GC end statistics and memory usage
 502   mgr->gc_end(recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,
 503               countCollection, cause);
 504 }
 505 
 506 void MemoryService::oops_do(OopClosure* f) {
 507   int i;
 508 
 509   for (i = 0; i < _pools_list->length(); i++) {
 510     MemoryPool* pool = _pools_list->at(i);
 511     pool->oops_do(f);
 512   }
 513   for (i = 0; i < _managers_list->length(); i++) {
 514     MemoryManager* mgr = _managers_list->at(i);
 515     mgr->oops_do(f);
 516   }
 517 }
 518 


 534 
 535   instanceHandle obj = ik->allocate_instance_handle(CHECK_NH);
 536 
 537   JavaValue result(T_VOID);
 538   JavaCallArguments args(10);
 539   args.push_oop(obj);                         // receiver
 540   args.push_long(usage.init_size_as_jlong()); // Argument 1
 541   args.push_long(usage.used_as_jlong());      // Argument 2
 542   args.push_long(usage.committed_as_jlong()); // Argument 3
 543   args.push_long(usage.max_size_as_jlong());  // Argument 4
 544 
 545   JavaCalls::call_special(&result,
 546                           ik,
 547                           vmSymbols::object_initializer_name(),
 548                           vmSymbols::long_long_long_long_void_signature(),
 549                           &args,
 550                           CHECK_NH);
 551   return obj;
 552 }
 553 
 554 // GC manager type depends on the type of Generation. Depending on the space
 555 // availability and vm options the gc uses major gc manager or minor gc
 556 // manager or both. The type of gc manager depends on the generation kind.
 557 // For DefNew and ParNew generation doing scavenge gc uses minor gc manager (so
 558 // _fullGC is set to false ) and for other generation kinds doing
 559 // mark-sweep-compact uses major gc manager (so _fullGC is set to true).
 560 TraceMemoryManagerStats::TraceMemoryManagerStats(Generation::Name kind, GCCause::Cause cause) {
 561   switch (kind) {
 562     case Generation::DefNew:
 563 #if INCLUDE_ALL_GCS
 564     case Generation::ParNew:
 565 #endif // INCLUDE_ALL_GCS
 566       _fullGC = false;
 567       break;
 568     case Generation::MarkSweepCompact:
 569 #if INCLUDE_ALL_GCS
 570     case Generation::ConcurrentMarkSweep:
 571 #endif // INCLUDE_ALL_GCS
 572       _fullGC = true;
 573       break;
 574     default:
 575       _fullGC = false;
 576       assert(false, "Unrecognized gc generation kind.");
 577   }
 578   // this has to be called in a stop the world pause and represent
 579   // an entire gc pause, start to finish:
 580   initialize(_fullGC, cause, true, true, true, true, true, true, true);
 581 }
 582 
 583 TraceMemoryManagerStats::TraceMemoryManagerStats(bool fullGC,
 584                                                  GCCause::Cause cause,
 585                                                  bool recordGCBeginTime,
 586                                                  bool recordPreGCUsage,
 587                                                  bool recordPeakUsage,
 588                                                  bool recordPostGCUsage,
 589                                                  bool recordAccumulatedGCTime,
 590                                                  bool recordGCEndTime,
 591                                                  bool countCollection) {
 592   initialize(fullGC, cause, recordGCBeginTime, recordPreGCUsage, recordPeakUsage,
 593              recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,
 594              countCollection);
 595 }
 596 
 597 // for a subclass to create then initialize an instance before invoking
 598 // the MemoryService
 599 void TraceMemoryManagerStats::initialize(bool fullGC,
 600                                          GCCause::Cause cause,
 601                                          bool recordGCBeginTime,
 602                                          bool recordPreGCUsage,
 603                                          bool recordPeakUsage,
 604                                          bool recordPostGCUsage,
 605                                          bool recordAccumulatedGCTime,
 606                                          bool recordGCEndTime,
 607                                          bool countCollection) {
 608   _fullGC = fullGC;
 609   _recordGCBeginTime = recordGCBeginTime;
 610   _recordPreGCUsage = recordPreGCUsage;
 611   _recordPeakUsage = recordPeakUsage;
 612   _recordPostGCUsage = recordPostGCUsage;
 613   _recordAccumulatedGCTime = recordAccumulatedGCTime;
 614   _recordGCEndTime = recordGCEndTime;
 615   _countCollection = countCollection;
 616   _cause = cause;
 617 
 618   MemoryService::gc_begin(_fullGC, _recordGCBeginTime, _recordAccumulatedGCTime,
 619                           _recordPreGCUsage, _recordPeakUsage);
 620 }
 621 
 622 TraceMemoryManagerStats::~TraceMemoryManagerStats() {
 623   MemoryService::gc_end(_fullGC, _recordPostGCUsage, _recordAccumulatedGCTime,
 624                         _recordGCEndTime, _countCollection, _cause);
 625 }


   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 "gc/shared/collectedHeap.hpp"




  29 #include "gc/shared/generation.hpp"

  30 #include "logging/logConfiguration.hpp"
  31 #include "memory/heap.hpp"
  32 #include "memory/memRegion.hpp"
  33 #include "oops/oop.inline.hpp"
  34 #include "runtime/globals.hpp"
  35 #include "runtime/javaCalls.hpp"
  36 #include "services/classLoadingService.hpp"
  37 #include "services/lowMemoryDetector.hpp"
  38 #include "services/management.hpp"
  39 #include "services/memoryManager.hpp"
  40 #include "services/memoryPool.hpp"
  41 #include "services/memoryService.hpp"
  42 #include "utilities/growableArray.hpp"
  43 #include "utilities/macros.hpp"










  44 
  45 GrowableArray<MemoryPool*>* MemoryService::_pools_list =
  46   new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryPool*>(init_pools_list_size, true);
  47 GrowableArray<MemoryManager*>* MemoryService::_managers_list =
  48   new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryManager*>(init_managers_list_size, true);
  49 


  50 MemoryManager*   MemoryService::_code_cache_manager    = NULL;
  51 GrowableArray<MemoryPool*>* MemoryService::_code_heap_pools =
  52     new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryPool*>(init_code_heap_pools_size, true);
  53 MemoryPool*      MemoryService::_metaspace_pool        = NULL;
  54 MemoryPool*      MemoryService::_compressed_class_pool = NULL;
  55 
  56 class GcThreadCountClosure: public ThreadClosure {
  57  private:
  58   int _count;
  59  public:
  60   GcThreadCountClosure() : _count(0) {};
  61   void do_thread(Thread* thread);
  62   int count() { return _count; }
  63 };
  64 
  65 void GcThreadCountClosure::do_thread(Thread* thread) {
  66   _count++;
  67 }
  68 
  69 void MemoryService::set_universe_heap(CollectedHeap* heap) {
  70   ResourceMark rm; // For internal allocations in GrowableArray.
  71   GrowableArray<MemoryManager*> gc_mem_mgrs = heap->memory_managers();
  72   _managers_list->appendAll(&gc_mem_mgrs);
  73 
  74   GrowableArray<MemoryPool*> gc_mem_pools = heap->memory_pools();
  75   _pools_list->appendAll(&gc_mem_pools);















  76 
  77   // set the GC thread count
  78   GcThreadCountClosure gctcc;
  79   heap->gc_threads_do(&gctcc);
  80   int count = gctcc.count();
  81 
  82   int num_mgrs = gc_mem_mgrs.length();
  83   for (int i = 0; i < num_mgrs; i++) {
  84     assert(gc_mem_mgrs.at(i)->is_gc_memory_manager(), "expect GC memory manager");
  85     GCMemoryManager* gc_mgr = (GCMemoryManager*) gc_mem_mgrs.at(i);
  86     if (count > 0) {
  87       gc_mgr->set_num_gc_threads(count);

  88     }

  89     // All memory pools and memory managers are initialized.
  90     gc_mgr->initialize_gc_stat_info();
















































































































































































































  91   }
  92 }
  93 





























































  94 void MemoryService::add_code_heap_memory_pool(CodeHeap* heap, const char* name) {
  95   // Create new memory pool for this heap
  96   MemoryPool* code_heap_pool = new CodeHeapPool(heap, name, true /* support_usage_threshold */);
  97 
  98   // Append to lists
  99   _code_heap_pools->append(code_heap_pool);
 100   _pools_list->append(code_heap_pool);
 101 
 102   if (_code_cache_manager == NULL) {
 103     // Create CodeCache memory manager
 104     _code_cache_manager = MemoryManager::get_code_cache_memory_manager();
 105     _managers_list->append(_code_cache_manager);
 106   }
 107 
 108   _code_cache_manager->add_pool(code_heap_pool);
 109 }
 110 
 111 void MemoryService::add_metaspace_memory_pools() {
 112   MemoryManager* mgr = MemoryManager::get_metaspace_memory_manager();
 113 


 148   // Track the peak memory usage
 149   for (int i = 0; i < _pools_list->length(); i++) {
 150     MemoryPool* pool = _pools_list->at(i);
 151     pool->record_peak_memory_usage();
 152   }
 153 
 154   // Detect low memory
 155   LowMemoryDetector::detect_low_memory();
 156 }
 157 
 158 void MemoryService::track_memory_pool_usage(MemoryPool* pool) {
 159   // Track the peak memory usage
 160   pool->record_peak_memory_usage();
 161 
 162   // Detect low memory
 163   if (LowMemoryDetector::is_enabled(pool)) {
 164     LowMemoryDetector::detect_low_memory(pool);
 165   }
 166 }
 167 
 168 void MemoryService::gc_begin(GCMemoryManager* mgr, bool recordGCBeginTime,
 169                              bool recordAccumulatedGCTime,
 170                              bool recordPreGCUsage, bool recordPeakUsage) {
 171 






 172   assert(mgr->is_gc_memory_manager(), "Sanity check");
 173   mgr->gc_begin(recordGCBeginTime, recordPreGCUsage, recordAccumulatedGCTime);
 174 
 175   // Track the peak memory usage when GC begins
 176   if (recordPeakUsage) {
 177     for (int i = 0; i < _pools_list->length(); i++) {
 178       MemoryPool* pool = _pools_list->at(i);
 179       pool->record_peak_memory_usage();
 180     }
 181   }
 182 }
 183 
 184 void MemoryService::gc_end(GCMemoryManager* mgr, bool recordPostGCUsage,
 185                            bool recordAccumulatedGCTime,
 186                            bool recordGCEndTime, bool countCollection,
 187                            GCCause::Cause cause) {
 188 






 189   assert(mgr->is_gc_memory_manager(), "Sanity check");
 190 
 191   // register the GC end statistics and memory usage
 192   mgr->gc_end(recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,
 193                   countCollection, cause);
 194 }
 195 
 196 void MemoryService::oops_do(OopClosure* f) {
 197   int i;
 198 
 199   for (i = 0; i < _pools_list->length(); i++) {
 200     MemoryPool* pool = _pools_list->at(i);
 201     pool->oops_do(f);
 202   }
 203   for (i = 0; i < _managers_list->length(); i++) {
 204     MemoryManager* mgr = _managers_list->at(i);
 205     mgr->oops_do(f);
 206   }
 207 }
 208 


 224 
 225   instanceHandle obj = ik->allocate_instance_handle(CHECK_NH);
 226 
 227   JavaValue result(T_VOID);
 228   JavaCallArguments args(10);
 229   args.push_oop(obj);                         // receiver
 230   args.push_long(usage.init_size_as_jlong()); // Argument 1
 231   args.push_long(usage.used_as_jlong());      // Argument 2
 232   args.push_long(usage.committed_as_jlong()); // Argument 3
 233   args.push_long(usage.max_size_as_jlong());  // Argument 4
 234 
 235   JavaCalls::call_special(&result,
 236                           ik,
 237                           vmSymbols::object_initializer_name(),
 238                           vmSymbols::long_long_long_long_void_signature(),
 239                           &args,
 240                           CHECK_NH);
 241   return obj;
 242 }
 243 
 244 TraceMemoryManagerStats::TraceMemoryManagerStats(GCMemoryManager* gc_mem_mgr,





























 245                                                  GCCause::Cause cause,
 246                                                  bool recordGCBeginTime,
 247                                                  bool recordPreGCUsage,
 248                                                  bool recordPeakUsage,
 249                                                  bool recordPostGCUsage,
 250                                                  bool recordAccumulatedGCTime,
 251                                                  bool recordGCEndTime,
 252                                                  bool countCollection) {
 253   initialize(gc_mem_mgr, cause, recordGCBeginTime, recordPreGCUsage, recordPeakUsage,
 254              recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,
 255              countCollection);
 256 }
 257 
 258 // for a subclass to create then initialize an instance before invoking
 259 // the MemoryService
 260 void TraceMemoryManagerStats::initialize(GCMemoryManager* gc_mem_mgr,
 261                                          GCCause::Cause cause,
 262                                          bool recordGCBeginTime,
 263                                          bool recordPreGCUsage,
 264                                          bool recordPeakUsage,
 265                                          bool recordPostGCUsage,
 266                                          bool recordAccumulatedGCTime,
 267                                          bool recordGCEndTime,
 268                                          bool countCollection) {
 269   _gc_mem_mgr = gc_mem_mgr;
 270   _recordGCBeginTime = recordGCBeginTime;
 271   _recordPreGCUsage = recordPreGCUsage;
 272   _recordPeakUsage = recordPeakUsage;
 273   _recordPostGCUsage = recordPostGCUsage;
 274   _recordAccumulatedGCTime = recordAccumulatedGCTime;
 275   _recordGCEndTime = recordGCEndTime;
 276   _countCollection = countCollection;
 277   _cause = cause;
 278 
 279   MemoryService::gc_begin(_gc_mem_mgr, _recordGCBeginTime, _recordAccumulatedGCTime,
 280                           _recordPreGCUsage, _recordPeakUsage);
 281 }
 282 
 283 TraceMemoryManagerStats::~TraceMemoryManagerStats() {
 284   MemoryService::gc_end(_gc_mem_mgr, _recordPostGCUsage, _recordAccumulatedGCTime,
 285                         _recordGCEndTime, _countCollection, _cause);
 286 }
< prev index next >