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