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  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "gc_implementation/shared/mutableSpace.hpp"
  29 #include "memory/collectorPolicy.hpp"
  30 #include "memory/defNewGeneration.hpp"
  31 #include "memory/genCollectedHeap.hpp"
  32 #include "memory/generation.hpp"
  33 #include "memory/generationSpec.hpp"
  34 #include "memory/heap.hpp"
  35 #include "memory/memRegion.hpp"
  36 #include "memory/tenuredGeneration.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "runtime/globals.hpp"
  39 #include "runtime/javaCalls.hpp"
  40 #include "services/classLoadingService.hpp"
  41 #include "services/lowMemoryDetector.hpp"
  42 #include "services/management.hpp"
  43 #include "services/memoryManager.hpp"
  44 #include "services/memoryPool.hpp"
  45 #include "services/memoryService.hpp"
  46 #include "utilities/growableArray.hpp"
  47 #include "utilities/macros.hpp"
  48 #if INCLUDE_ALL_GCS
  49 #include "gc_implementation/shenandoah/shenandoahHeap.inline.hpp"
  50 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.hpp"
  51 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  52 #include "gc_implementation/parNew/parNewGeneration.hpp"
  53 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
  54 #include "gc_implementation/parallelScavenge/psOldGen.hpp"
  55 #include "gc_implementation/parallelScavenge/psYoungGen.hpp"
  56 #include "services/g1MemoryPool.hpp"
  57 #include "services/psMemoryPool.hpp"
  58 #include "services/shenandoahMemoryPool.hpp"
  59 #endif // INCLUDE_ALL_GCS
  60 
  61 GrowableArray<MemoryPool*>* MemoryService::_pools_list =
  62   new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryPool*>(init_pools_list_size, true);
  63 GrowableArray<MemoryManager*>* MemoryService::_managers_list =
  64   new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryManager*>(init_managers_list_size, true);
  65 
  66 GCMemoryManager* MemoryService::_minor_gc_manager      = NULL;
  67 GCMemoryManager* MemoryService::_major_gc_manager      = NULL;
  68 MemoryPool*      MemoryService::_code_heap_pool        = NULL;
  69 MemoryPool*      MemoryService::_metaspace_pool        = NULL;
  70 MemoryPool*      MemoryService::_compressed_class_pool = NULL;
  71 
  72 class GcThreadCountClosure: public ThreadClosure {
  73  private:
  74   int _count;
  75  public:
  76   GcThreadCountClosure() : _count(0) {};
  77   void do_thread(Thread* thread);
  78   int count() { return _count; }
  79 };
  80 
  81 void GcThreadCountClosure::do_thread(Thread* thread) {
  82   _count++;
  83 }
  84 
  85 void MemoryService::set_universe_heap(CollectedHeap* heap) {
  86   CollectedHeap::Name kind = heap->kind();
  87   switch (kind) {
  88     case CollectedHeap::GenCollectedHeap : {
  89       add_gen_collected_heap_info(GenCollectedHeap::heap());
  90       break;
  91     }
  92 #if INCLUDE_ALL_GCS
  93     case CollectedHeap::ParallelScavengeHeap : {
  94       add_parallel_scavenge_heap_info(ParallelScavengeHeap::heap());
  95       break;
  96     }
  97     case CollectedHeap::G1CollectedHeap : {
  98       add_g1_heap_info(G1CollectedHeap::heap());
  99       break;
 100     }
 101   case CollectedHeap::ShenandoahHeap : {
 102     add_shenandoah_heap_info(ShenandoahHeap::heap());
 103     break;
 104   }
 105 #endif // INCLUDE_ALL_GCS
 106     default: {
 107       guarantee(false, "Unrecognized kind of heap");
 108     }
 109   }
 110 
 111   // set the GC thread count
 112   GcThreadCountClosure gctcc;
 113   heap->gc_threads_do(&gctcc);
 114   int count = gctcc.count();
 115   if (count > 0) {
 116     _minor_gc_manager->set_num_gc_threads(count);
 117     _major_gc_manager->set_num_gc_threads(count);
 118   }
 119 
 120   // All memory pools and memory managers are initialized.
 121   //
 122   _minor_gc_manager->initialize_gc_stat_info();
 123   _major_gc_manager->initialize_gc_stat_info();
 124 }
 125 
 126 // Add memory pools for GenCollectedHeap
 127 // This function currently only supports two generations collected heap.
 128 // The collector for GenCollectedHeap will have two memory managers.
 129 void MemoryService::add_gen_collected_heap_info(GenCollectedHeap* heap) {
 130   CollectorPolicy* policy = heap->collector_policy();
 131 
 132   assert(policy->is_two_generation_policy(), "Only support two generations");
 133   guarantee(heap->n_gens() == 2, "Only support two-generation heap");
 134 
 135   TwoGenerationCollectorPolicy* two_gen_policy = policy->as_two_generation_policy();
 136   if (two_gen_policy != NULL) {
 137     GenerationSpec** specs = two_gen_policy->generations();
 138     Generation::Name kind = specs[0]->name();
 139     switch (kind) {
 140       case Generation::DefNew:
 141         _minor_gc_manager = MemoryManager::get_copy_memory_manager();
 142         break;
 143 #if INCLUDE_ALL_GCS
 144       case Generation::ParNew:
 145       case Generation::ASParNew:
 146         _minor_gc_manager = MemoryManager::get_parnew_memory_manager();
 147         break;
 148 #endif // INCLUDE_ALL_GCS
 149       default:
 150         guarantee(false, "Unrecognized generation spec");
 151         break;
 152     }
 153     if (policy->is_mark_sweep_policy()) {
 154       _major_gc_manager = MemoryManager::get_msc_memory_manager();
 155 #if INCLUDE_ALL_GCS
 156     } else if (policy->is_concurrent_mark_sweep_policy()) {
 157       _major_gc_manager = MemoryManager::get_cms_memory_manager();
 158 #endif // INCLUDE_ALL_GCS
 159     } else {
 160       guarantee(false, "Unknown two-gen policy");
 161     }
 162   } else {
 163     guarantee(false, "Non two-gen policy");
 164   }
 165   _managers_list->append(_minor_gc_manager);
 166   _managers_list->append(_major_gc_manager);
 167 
 168   add_generation_memory_pool(heap->get_gen(minor), _major_gc_manager, _minor_gc_manager);
 169   add_generation_memory_pool(heap->get_gen(major), _major_gc_manager);
 170 }
 171 
 172 #if INCLUDE_ALL_GCS
 173 // Add memory pools for ParallelScavengeHeap
 174 // This function currently only supports two generations collected heap.
 175 // The collector for ParallelScavengeHeap will have two memory managers.
 176 void MemoryService::add_parallel_scavenge_heap_info(ParallelScavengeHeap* heap) {
 177   // Two managers to keep statistics about _minor_gc_manager and _major_gc_manager GC.
 178   _minor_gc_manager = MemoryManager::get_psScavenge_memory_manager();
 179   _major_gc_manager = MemoryManager::get_psMarkSweep_memory_manager();
 180   _managers_list->append(_minor_gc_manager);
 181   _managers_list->append(_major_gc_manager);
 182 
 183   add_psYoung_memory_pool(heap->young_gen(), _major_gc_manager, _minor_gc_manager);
 184   add_psOld_memory_pool(heap->old_gen(), _major_gc_manager);
 185 }
 186 
 187 void MemoryService::add_g1_heap_info(G1CollectedHeap* g1h) {
 188   assert(UseG1GC, "sanity");
 189 
 190   _minor_gc_manager = MemoryManager::get_g1YoungGen_memory_manager();
 191   _major_gc_manager = MemoryManager::get_g1OldGen_memory_manager();
 192   _managers_list->append(_minor_gc_manager);
 193   _managers_list->append(_major_gc_manager);
 194 
 195   add_g1YoungGen_memory_pool(g1h, _major_gc_manager, _minor_gc_manager);
 196   add_g1OldGen_memory_pool(g1h, _major_gc_manager, _minor_gc_manager);
 197 }
 198 
 199 void MemoryService::add_shenandoah_heap_info(ShenandoahHeap* heap) {
 200   assert(UseShenandoahGC, "sanity");
 201 
 202   // We reuse the "minor/major" names, even though they make little sense
 203   // in Shenandoah. JDK 10+ makes this right, but not JDK 9-.
 204   _major_gc_manager = MemoryManager::get_shenandoah_pauses_memory_manager();
 205   _minor_gc_manager = MemoryManager::get_shenandoah_cycles_memory_manager();
 206   _managers_list->append(_major_gc_manager);
 207   _managers_list->append(_minor_gc_manager);
 208 
 209   ShenandoahMemoryPool* pool = new ShenandoahMemoryPool(heap);
 210   _pools_list->append(pool);
 211 
 212   _major_gc_manager->add_pool(pool);
 213   _minor_gc_manager->add_pool(pool);
 214 }
 215 
 216 #endif // INCLUDE_ALL_GCS
 217 
 218 MemoryPool* MemoryService::add_gen(Generation* gen,
 219                                    const char* name,
 220                                    bool is_heap,
 221                                    bool support_usage_threshold) {
 222 
 223   MemoryPool::PoolType type = (is_heap ? MemoryPool::Heap : MemoryPool::NonHeap);
 224   GenerationPool* pool = new GenerationPool(gen, name, type, support_usage_threshold);
 225   _pools_list->append(pool);
 226   return (MemoryPool*) pool;
 227 }
 228 
 229 MemoryPool* MemoryService::add_space(ContiguousSpace* space,
 230                                      const char* name,
 231                                      bool is_heap,
 232                                      size_t max_size,
 233                                      bool support_usage_threshold) {
 234   MemoryPool::PoolType type = (is_heap ? MemoryPool::Heap : MemoryPool::NonHeap);
 235   ContiguousSpacePool* pool = new ContiguousSpacePool(space, name, type, max_size, support_usage_threshold);
 236 
 237   _pools_list->append(pool);
 238   return (MemoryPool*) pool;
 239 }
 240 
 241 MemoryPool* MemoryService::add_survivor_spaces(DefNewGeneration* gen,
 242                                                const char* name,
 243                                                bool is_heap,
 244                                                size_t max_size,
 245                                                bool support_usage_threshold) {
 246   MemoryPool::PoolType type = (is_heap ? MemoryPool::Heap : MemoryPool::NonHeap);
 247   SurvivorContiguousSpacePool* pool = new SurvivorContiguousSpacePool(gen, name, type, max_size, support_usage_threshold);
 248 
 249   _pools_list->append(pool);
 250   return (MemoryPool*) pool;
 251 }
 252 
 253 #if INCLUDE_ALL_GCS
 254 MemoryPool* MemoryService::add_cms_space(CompactibleFreeListSpace* space,
 255                                          const char* name,
 256                                          bool is_heap,
 257                                          size_t max_size,
 258                                          bool support_usage_threshold) {
 259   MemoryPool::PoolType type = (is_heap ? MemoryPool::Heap : MemoryPool::NonHeap);
 260   CompactibleFreeListSpacePool* pool = new CompactibleFreeListSpacePool(space, name, type, max_size, support_usage_threshold);
 261   _pools_list->append(pool);
 262   return (MemoryPool*) pool;
 263 }
 264 #endif // INCLUDE_ALL_GCS
 265 
 266 // Add memory pool(s) for one generation
 267 void MemoryService::add_generation_memory_pool(Generation* gen,
 268                                                GCMemoryManager* major_mgr,
 269                                                GCMemoryManager* minor_mgr) {
 270   guarantee(gen != NULL, "No generation for memory pool");
 271   Generation::Name kind = gen->kind();
 272   int index = _pools_list->length();
 273 
 274   switch (kind) {
 275     case Generation::DefNew: {
 276       assert(major_mgr != NULL && minor_mgr != NULL, "Should have two managers");
 277       DefNewGeneration* young_gen = (DefNewGeneration*) gen;
 278       // Add a memory pool for each space and young gen doesn't
 279       // support low memory detection as it is expected to get filled up.
 280       MemoryPool* eden = add_space(young_gen->eden(),
 281                                    "Eden Space",
 282                                    true, /* is_heap */
 283                                    young_gen->max_eden_size(),
 284                                    false /* support_usage_threshold */);
 285       MemoryPool* survivor = add_survivor_spaces(young_gen,
 286                                                  "Survivor Space",
 287                                                  true, /* is_heap */
 288                                                  young_gen->max_survivor_size(),
 289                                                  false /* support_usage_threshold */);
 290       break;
 291     }
 292 
 293 #if INCLUDE_ALL_GCS
 294     case Generation::ParNew:
 295     case Generation::ASParNew:
 296     {
 297       assert(major_mgr != NULL && minor_mgr != NULL, "Should have two managers");
 298       // Add a memory pool for each space and young gen doesn't
 299       // support low memory detection as it is expected to get filled up.
 300       ParNewGeneration* parnew_gen = (ParNewGeneration*) gen;
 301       MemoryPool* eden = add_space(parnew_gen->eden(),
 302                                    "Par Eden Space",
 303                                    true /* is_heap */,
 304                                    parnew_gen->max_eden_size(),
 305                                    false /* support_usage_threshold */);
 306       MemoryPool* survivor = add_survivor_spaces(parnew_gen,
 307                                                  "Par Survivor Space",
 308                                                  true, /* is_heap */
 309                                                  parnew_gen->max_survivor_size(),
 310                                                  false /* support_usage_threshold */);
 311 
 312       break;
 313     }
 314 #endif // INCLUDE_ALL_GCS
 315 
 316     case Generation::MarkSweepCompact: {
 317       assert(major_mgr != NULL && minor_mgr == NULL, "Should have only one manager");
 318       add_gen(gen,
 319               "Tenured Gen",
 320               true, /* is_heap */
 321               true  /* support_usage_threshold */);
 322       break;
 323     }
 324 
 325 #if INCLUDE_ALL_GCS
 326     case Generation::ConcurrentMarkSweep:
 327     case Generation::ASConcurrentMarkSweep:
 328     {
 329       assert(major_mgr != NULL && minor_mgr == NULL, "Should have only one manager");
 330       ConcurrentMarkSweepGeneration* cms = (ConcurrentMarkSweepGeneration*) gen;
 331       MemoryPool* pool = add_cms_space(cms->cmsSpace(),
 332                                        "CMS Old Gen",
 333                                        true, /* is_heap */
 334                                        cms->reserved().byte_size(),
 335                                        true  /* support_usage_threshold */);
 336       break;
 337     }
 338 #endif // INCLUDE_ALL_GCS
 339 
 340     default:
 341       assert(false, "should not reach here");
 342       // no memory pool added for others
 343       break;
 344   }
 345 
 346   assert(major_mgr != NULL, "Should have at least one manager");
 347   // Link managers and the memory pools together
 348   for (int i = index; i < _pools_list->length(); i++) {
 349     MemoryPool* pool = _pools_list->at(i);
 350     major_mgr->add_pool(pool);
 351     if (minor_mgr != NULL) {
 352       minor_mgr->add_pool(pool);
 353     }
 354   }
 355 }
 356 
 357 
 358 #if INCLUDE_ALL_GCS
 359 void MemoryService::add_psYoung_memory_pool(PSYoungGen* gen,
 360                                             GCMemoryManager* major_mgr,
 361                                             GCMemoryManager* minor_mgr) {
 362   assert(major_mgr != NULL && minor_mgr != NULL, "Should have two managers");
 363 
 364   // Add a memory pool for each space and young gen doesn't
 365   // support low memory detection as it is expected to get filled up.
 366   EdenMutableSpacePool* eden = new EdenMutableSpacePool(gen,
 367                                                         gen->eden_space(),
 368                                                         "PS Eden Space",
 369                                                         MemoryPool::Heap,
 370                                                         false /* support_usage_threshold */);
 371 
 372   SurvivorMutableSpacePool* survivor = new SurvivorMutableSpacePool(gen,
 373                                                                     "PS Survivor Space",
 374                                                                     MemoryPool::Heap,
 375                                                                     false /* support_usage_threshold */);
 376 
 377   major_mgr->add_pool(eden);
 378   major_mgr->add_pool(survivor);
 379   minor_mgr->add_pool(eden);
 380   minor_mgr->add_pool(survivor);
 381   _pools_list->append(eden);
 382   _pools_list->append(survivor);
 383 }
 384 
 385 void MemoryService::add_psOld_memory_pool(PSOldGen* gen, GCMemoryManager* mgr) {
 386   PSGenerationPool* old_gen = new PSGenerationPool(gen,
 387                                                    "PS Old Gen",
 388                                                    MemoryPool::Heap,
 389                                                    true /* support_usage_threshold */);
 390   mgr->add_pool(old_gen);
 391   _pools_list->append(old_gen);
 392 }
 393 
 394 void MemoryService::add_g1YoungGen_memory_pool(G1CollectedHeap* g1h,
 395                                                GCMemoryManager* major_mgr,
 396                                                GCMemoryManager* minor_mgr) {
 397   assert(major_mgr != NULL && minor_mgr != NULL, "should have two managers");
 398 
 399   G1EdenPool* eden = new G1EdenPool(g1h);
 400   G1SurvivorPool* survivor = new G1SurvivorPool(g1h);
 401 
 402   major_mgr->add_pool(eden);
 403   major_mgr->add_pool(survivor);
 404   minor_mgr->add_pool(eden);
 405   minor_mgr->add_pool(survivor);
 406   _pools_list->append(eden);
 407   _pools_list->append(survivor);
 408 }
 409 
 410 void MemoryService::add_g1OldGen_memory_pool(G1CollectedHeap* g1h,
 411                                              GCMemoryManager* major_mgr,
 412                                              GCMemoryManager* minor_mgr) {
 413   assert(major_mgr != NULL && minor_mgr != NULL, "should have two managers");
 414 
 415   G1OldGenPool* old_gen = new G1OldGenPool(g1h);
 416   major_mgr->add_pool(old_gen);
 417   minor_mgr->add_pool(old_gen, false /* always_affected_by_gc */);
 418   _pools_list->append(old_gen);
 419 }
 420 #endif // INCLUDE_ALL_GCS
 421 
 422 void MemoryService::add_code_heap_memory_pool(CodeHeap* heap) {
 423   _code_heap_pool = new CodeHeapPool(heap,
 424                                      "Code Cache",
 425                                      true /* support_usage_threshold */);
 426   MemoryManager* mgr = MemoryManager::get_code_cache_memory_manager();
 427   mgr->add_pool(_code_heap_pool);
 428 
 429   _pools_list->append(_code_heap_pool);
 430   _managers_list->append(mgr);
 431 }
 432 
 433 void MemoryService::add_metaspace_memory_pools() {
 434   MemoryManager* mgr = MemoryManager::get_metaspace_memory_manager();
 435 
 436   _metaspace_pool = new MetaspacePool();
 437   mgr->add_pool(_metaspace_pool);
 438   _pools_list->append(_metaspace_pool);
 439 
 440   if (UseCompressedClassPointers) {
 441     _compressed_class_pool = new CompressedKlassSpacePool();
 442     mgr->add_pool(_compressed_class_pool);
 443     _pools_list->append(_compressed_class_pool);
 444   }
 445 
 446   _managers_list->append(mgr);
 447 }
 448 
 449 MemoryManager* MemoryService::get_memory_manager(instanceHandle mh) {
 450   for (int i = 0; i < _managers_list->length(); i++) {
 451     MemoryManager* mgr = _managers_list->at(i);
 452     if (mgr->is_manager(mh)) {
 453       return mgr;
 454     }
 455   }
 456   return NULL;
 457 }
 458 
 459 MemoryPool* MemoryService::get_memory_pool(instanceHandle ph) {
 460   for (int i = 0; i < _pools_list->length(); i++) {
 461     MemoryPool* pool = _pools_list->at(i);
 462     if (pool->is_pool(ph)) {
 463       return pool;
 464     }
 465   }
 466   return NULL;
 467 }
 468 
 469 void MemoryService::track_memory_usage() {
 470   // Track the peak memory usage
 471   for (int i = 0; i < _pools_list->length(); i++) {
 472     MemoryPool* pool = _pools_list->at(i);
 473     pool->record_peak_memory_usage();
 474   }
 475 
 476   // Detect low memory
 477   LowMemoryDetector::detect_low_memory();
 478 }
 479 
 480 void MemoryService::track_memory_pool_usage(MemoryPool* pool) {
 481   // Track the peak memory usage
 482   pool->record_peak_memory_usage();
 483 
 484   // Detect low memory
 485   if (LowMemoryDetector::is_enabled(pool)) {
 486     LowMemoryDetector::detect_low_memory(pool);
 487   }
 488 }
 489 
 490 void MemoryService::gc_begin(bool fullGC, bool recordGCBeginTime,
 491                              bool recordAccumulatedGCTime,
 492                              bool recordPreGCUsage, bool recordPeakUsage) {
 493 
 494   GCMemoryManager* mgr;
 495   if (fullGC) {
 496     mgr = _major_gc_manager;
 497   } else {
 498     mgr = _minor_gc_manager;
 499   }
 500   assert(mgr->is_gc_memory_manager(), "Sanity check");
 501   mgr->gc_begin(recordGCBeginTime, recordPreGCUsage, recordAccumulatedGCTime);
 502 
 503   // Track the peak memory usage when GC begins
 504   if (recordPeakUsage) {
 505     for (int i = 0; i < _pools_list->length(); i++) {
 506       MemoryPool* pool = _pools_list->at(i);
 507       pool->record_peak_memory_usage();
 508     }
 509   }
 510 }
 511 
 512 void MemoryService::gc_end(bool fullGC, bool recordPostGCUsage,
 513                            bool recordAccumulatedGCTime,
 514                            bool recordGCEndTime, bool countCollection,
 515                            GCCause::Cause cause,
 516                            bool allMemoryPoolsAffected) {
 517 
 518   GCMemoryManager* mgr;
 519   if (fullGC) {
 520     mgr = (GCMemoryManager*) _major_gc_manager;
 521   } else {
 522     mgr = (GCMemoryManager*) _minor_gc_manager;
 523   }
 524   assert(mgr->is_gc_memory_manager(), "Sanity check");
 525 
 526   // register the GC end statistics and memory usage
 527   mgr->gc_end(recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,
 528               countCollection, cause, allMemoryPoolsAffected);
 529 }
 530 
 531 void MemoryService::oops_do(OopClosure* f) {
 532   int i;
 533 
 534   for (i = 0; i < _pools_list->length(); i++) {
 535     MemoryPool* pool = _pools_list->at(i);
 536     pool->oops_do(f);
 537   }
 538   for (i = 0; i < _managers_list->length(); i++) {
 539     MemoryManager* mgr = _managers_list->at(i);
 540     mgr->oops_do(f);
 541   }
 542 }
 543 
 544 bool MemoryService::set_verbose(bool verbose) {
 545   MutexLocker m(Management_lock);
 546   // verbose will be set to the previous value
 547   bool succeed = CommandLineFlags::boolAtPut((char*)"PrintGC", &verbose, Flag::MANAGEMENT);
 548   assert(succeed, "Setting PrintGC flag fails");
 549   ClassLoadingService::reset_trace_class_unloading();
 550 
 551   return verbose;
 552 }
 553 
 554 Handle MemoryService::create_MemoryUsage_obj(MemoryUsage usage, TRAPS) {
 555   Klass* k = Management::java_lang_management_MemoryUsage_klass(CHECK_NH);
 556   instanceKlassHandle ik(THREAD, k);
 557 
 558   instanceHandle obj = ik->allocate_instance_handle(CHECK_NH);
 559 
 560   JavaValue result(T_VOID);
 561   JavaCallArguments args(10);
 562   args.push_oop(obj);                         // receiver
 563   args.push_long(usage.init_size_as_jlong()); // Argument 1
 564   args.push_long(usage.used_as_jlong());      // Argument 2
 565   args.push_long(usage.committed_as_jlong()); // Argument 3
 566   args.push_long(usage.max_size_as_jlong());  // Argument 4
 567 
 568   JavaCalls::call_special(&result,
 569                           ik,
 570                           vmSymbols::object_initializer_name(),
 571                           vmSymbols::long_long_long_long_void_signature(),
 572                           &args,
 573                           CHECK_NH);
 574   return obj;
 575 }
 576 //
 577 // GC manager type depends on the type of Generation. Depending on the space
 578 // availablity and vm options the gc uses major gc manager or minor gc
 579 // manager or both. The type of gc manager depends on the generation kind.
 580 // For DefNew, ParNew and ASParNew generation doing scavenge gc uses minor
 581 // gc manager (so _fullGC is set to false ) and for other generation kinds
 582 // doing mark-sweep-compact uses major gc manager (so _fullGC is set
 583 // to true).
 584 TraceMemoryManagerStats::TraceMemoryManagerStats(Generation::Name kind, GCCause::Cause cause) {
 585   switch (kind) {
 586     case Generation::DefNew:
 587 #if INCLUDE_ALL_GCS
 588     case Generation::ParNew:
 589     case Generation::ASParNew:
 590 #endif // INCLUDE_ALL_GCS
 591       _fullGC=false;
 592       break;
 593     case Generation::MarkSweepCompact:
 594 #if INCLUDE_ALL_GCS
 595     case Generation::ConcurrentMarkSweep:
 596     case Generation::ASConcurrentMarkSweep:
 597 #endif // INCLUDE_ALL_GCS
 598       _fullGC=true;
 599       break;
 600     default:
 601       assert(false, "Unrecognized gc generation kind.");
 602   }
 603   // this has to be called in a stop the world pause and represent
 604   // an entire gc pause, start to finish:
 605   initialize(_fullGC, cause, true, true, true, true, true, true, true, true);
 606 }
 607 TraceMemoryManagerStats::TraceMemoryManagerStats(bool fullGC,
 608                                                  GCCause::Cause cause,
 609                                                  bool allMemoryPoolsAffected,
 610                                                  bool recordGCBeginTime,
 611                                                  bool recordPreGCUsage,
 612                                                  bool recordPeakUsage,
 613                                                  bool recordPostGCUsage,
 614                                                  bool recordAccumulatedGCTime,
 615                                                  bool recordGCEndTime,
 616                                                  bool countCollection) {
 617   initialize(fullGC, cause, allMemoryPoolsAffected,
 618              recordGCBeginTime, recordPreGCUsage, recordPeakUsage,
 619              recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,
 620              countCollection);
 621 }
 622 
 623 // for a subclass to create then initialize an instance before invoking
 624 // the MemoryService
 625 void TraceMemoryManagerStats::initialize(bool fullGC,
 626                                          GCCause::Cause cause,
 627                                          bool allMemoryPoolsAffected,
 628                                          bool recordGCBeginTime,
 629                                          bool recordPreGCUsage,
 630                                          bool recordPeakUsage,
 631                                          bool recordPostGCUsage,
 632                                          bool recordAccumulatedGCTime,
 633                                          bool recordGCEndTime,
 634                                          bool countCollection) {
 635   _fullGC = fullGC;
 636   _allMemoryPoolsAffected = allMemoryPoolsAffected;
 637   _recordGCBeginTime = recordGCBeginTime;
 638   _recordPreGCUsage = recordPreGCUsage;
 639   _recordPeakUsage = recordPeakUsage;
 640   _recordPostGCUsage = recordPostGCUsage;
 641   _recordAccumulatedGCTime = recordAccumulatedGCTime;
 642   _recordGCEndTime = recordGCEndTime;
 643   _countCollection = countCollection;
 644   _cause = cause;
 645 
 646   MemoryService::gc_begin(_fullGC, _recordGCBeginTime, _recordAccumulatedGCTime,
 647                           _recordPreGCUsage, _recordPeakUsage);
 648 }
 649 
 650 TraceMemoryManagerStats::~TraceMemoryManagerStats() {
 651   MemoryService::gc_end(_fullGC, _recordPostGCUsage, _recordAccumulatedGCTime,
 652                         _recordGCEndTime, _countCollection, _cause, _allMemoryPoolsAffected);
 653 }