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/shared/gcServicabilitySupport.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 GCMemoryManager* MemoryService::_minor_gc_manager      = NULL;
  51 GCMemoryManager* MemoryService::_major_gc_manager      = NULL;
  52 MemoryManager*   MemoryService::_code_cache_manager    = NULL;
  53 GrowableArray<MemoryPool*>* MemoryService::_code_heap_pools =
  54     new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryPool*>(init_code_heap_pools_size, true);
  55 MemoryPool*      MemoryService::_metaspace_pool        = NULL;
  56 MemoryPool*      MemoryService::_compressed_class_pool = NULL;
  57 
  58 class GcThreadCountClosure: public ThreadClosure {
  59  private:
  60   int _count;
  61  public:
  62   GcThreadCountClosure() : _count(0) {};
  63   void do_thread(Thread* thread);
  64   int count() { return _count; }
  65 };
  66 
  67 void GcThreadCountClosure::do_thread(Thread* thread) {
  68   _count++;
  69 }
  70 
  71 void MemoryService::set_universe_heap(CollectedHeap* heap) {
  72   _minor_gc_manager = GC::gc()->servicability_support()->create_minor_gc_manager();
  73   _major_gc_manager = GC::gc()->servicability_support()->create_major_gc_manager();
  74   _managers_list->append(_minor_gc_manager);
  75   _managers_list->append(_major_gc_manager);
  76 
  77   GC::gc()->servicability_support()->add_memory_pools(_pools_list,
  78                                                       _minor_gc_manager,
  79                                                       _major_gc_manager);
  80 
  81   // set the GC thread count
  82   GcThreadCountClosure gctcc;
  83   heap->gc_threads_do(&gctcc);
  84   int count = gctcc.count();
  85   if (count > 0) {
  86     _minor_gc_manager->set_num_gc_threads(count);
  87     _major_gc_manager->set_num_gc_threads(count);
  88   }
  89 
  90   // All memory pools and memory managers are initialized.
  91   //
  92   _minor_gc_manager->initialize_gc_stat_info();
  93   _major_gc_manager->initialize_gc_stat_info();
  94 }
  95 
  96 void MemoryService::add_code_heap_memory_pool(CodeHeap* heap, const char* name) {
  97   // Create new memory pool for this heap
  98   MemoryPool* code_heap_pool = new CodeHeapPool(heap, name, true /* support_usage_threshold */);
  99 
 100   // Append to lists
 101   _code_heap_pools->append(code_heap_pool);
 102   _pools_list->append(code_heap_pool);
 103 
 104   if (_code_cache_manager == NULL) {
 105     // Create CodeCache memory manager
 106     _code_cache_manager = MemoryManager::get_code_cache_memory_manager();
 107     _managers_list->append(_code_cache_manager);
 108   }
 109 
 110   _code_cache_manager->add_pool(code_heap_pool);
 111 }
 112 
 113 void MemoryService::add_metaspace_memory_pools() {
 114   MemoryManager* mgr = MemoryManager::get_metaspace_memory_manager();
 115 
 116   _metaspace_pool = new MetaspacePool();
 117   mgr->add_pool(_metaspace_pool);
 118   _pools_list->append(_metaspace_pool);
 119 
 120   if (UseCompressedClassPointers) {
 121     _compressed_class_pool = new CompressedKlassSpacePool();
 122     mgr->add_pool(_compressed_class_pool);
 123     _pools_list->append(_compressed_class_pool);
 124   }
 125 
 126   _managers_list->append(mgr);
 127 }
 128 
 129 MemoryManager* MemoryService::get_memory_manager(instanceHandle mh) {
 130   for (int i = 0; i < _managers_list->length(); i++) {
 131     MemoryManager* mgr = _managers_list->at(i);
 132     if (mgr->is_manager(mh)) {
 133       return mgr;
 134     }
 135   }
 136   return NULL;
 137 }
 138 
 139 MemoryPool* MemoryService::get_memory_pool(instanceHandle ph) {
 140   for (int i = 0; i < _pools_list->length(); i++) {
 141     MemoryPool* pool = _pools_list->at(i);
 142     if (pool->is_pool(ph)) {
 143       return pool;
 144     }
 145   }
 146   return NULL;
 147 }
 148 
 149 void MemoryService::track_memory_usage() {
 150   // Track the peak memory usage
 151   for (int i = 0; i < _pools_list->length(); i++) {
 152     MemoryPool* pool = _pools_list->at(i);
 153     pool->record_peak_memory_usage();
 154   }
 155 
 156   // Detect low memory
 157   LowMemoryDetector::detect_low_memory();
 158 }
 159 
 160 void MemoryService::track_memory_pool_usage(MemoryPool* pool) {
 161   // Track the peak memory usage
 162   pool->record_peak_memory_usage();
 163 
 164   // Detect low memory
 165   if (LowMemoryDetector::is_enabled(pool)) {
 166     LowMemoryDetector::detect_low_memory(pool);
 167   }
 168 }
 169 
 170 void MemoryService::gc_begin(bool fullGC, bool recordGCBeginTime,
 171                              bool recordAccumulatedGCTime,
 172                              bool recordPreGCUsage, bool recordPeakUsage) {
 173 
 174   GCMemoryManager* mgr;
 175   if (fullGC) {
 176     mgr = _major_gc_manager;
 177   } else {
 178     mgr = _minor_gc_manager;
 179   }
 180   assert(mgr->is_gc_memory_manager(), "Sanity check");
 181   mgr->gc_begin(recordGCBeginTime, recordPreGCUsage, recordAccumulatedGCTime);
 182 
 183   // Track the peak memory usage when GC begins
 184   if (recordPeakUsage) {
 185     for (int i = 0; i < _pools_list->length(); i++) {
 186       MemoryPool* pool = _pools_list->at(i);
 187       pool->record_peak_memory_usage();
 188     }
 189   }
 190 }
 191 
 192 void MemoryService::gc_end(bool fullGC, bool recordPostGCUsage,
 193                            bool recordAccumulatedGCTime,
 194                            bool recordGCEndTime, bool countCollection,
 195                            GCCause::Cause cause) {
 196 
 197   GCMemoryManager* mgr;
 198   if (fullGC) {
 199     mgr = (GCMemoryManager*) _major_gc_manager;
 200   } else {
 201     mgr = (GCMemoryManager*) _minor_gc_manager;
 202   }
 203   assert(mgr->is_gc_memory_manager(), "Sanity check");
 204 
 205   // register the GC end statistics and memory usage
 206   mgr->gc_end(recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,
 207               countCollection, cause);
 208 }
 209 
 210 void MemoryService::oops_do(OopClosure* f) {
 211   int i;
 212 
 213   for (i = 0; i < _pools_list->length(); i++) {
 214     MemoryPool* pool = _pools_list->at(i);
 215     pool->oops_do(f);
 216   }
 217   for (i = 0; i < _managers_list->length(); i++) {
 218     MemoryManager* mgr = _managers_list->at(i);
 219     mgr->oops_do(f);
 220   }
 221 }
 222 
 223 bool MemoryService::set_verbose(bool verbose) {
 224   MutexLocker m(Management_lock);
 225   // verbose will be set to the previous value
 226   if (verbose) {
 227     LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(gc));
 228   } else {
 229     LogConfiguration::configure_stdout(LogLevel::Off, true, LOG_TAGS(gc));
 230   }
 231   ClassLoadingService::reset_trace_class_unloading();
 232 
 233   return verbose;
 234 }
 235 
 236 Handle MemoryService::create_MemoryUsage_obj(MemoryUsage usage, TRAPS) {
 237   InstanceKlass* ik = Management::java_lang_management_MemoryUsage_klass(CHECK_NH);
 238 
 239   instanceHandle obj = ik->allocate_instance_handle(CHECK_NH);
 240 
 241   JavaValue result(T_VOID);
 242   JavaCallArguments args(10);
 243   args.push_oop(obj);                         // receiver
 244   args.push_long(usage.init_size_as_jlong()); // Argument 1
 245   args.push_long(usage.used_as_jlong());      // Argument 2
 246   args.push_long(usage.committed_as_jlong()); // Argument 3
 247   args.push_long(usage.max_size_as_jlong());  // Argument 4
 248 
 249   JavaCalls::call_special(&result,
 250                           ik,
 251                           vmSymbols::object_initializer_name(),
 252                           vmSymbols::long_long_long_long_void_signature(),
 253                           &args,
 254                           CHECK_NH);
 255   return obj;
 256 }
 257 
 258 // GC manager type depends on the type of Generation. Depending on the space
 259 // availability and vm options the gc uses major gc manager or minor gc
 260 // manager or both. The type of gc manager depends on the generation kind.
 261 // For DefNew and ParNew generation doing scavenge gc uses minor gc manager (so
 262 // _fullGC is set to false ) and for other generation kinds doing
 263 // mark-sweep-compact uses major gc manager (so _fullGC is set to true).
 264 TraceMemoryManagerStats::TraceMemoryManagerStats(Generation::Name kind, GCCause::Cause cause) {
 265   switch (kind) {
 266     case Generation::DefNew:
 267 #if INCLUDE_ALL_GCS
 268     case Generation::ParNew:
 269 #endif // INCLUDE_ALL_GCS
 270       _fullGC = false;
 271       break;
 272     case Generation::MarkSweepCompact:
 273 #if INCLUDE_ALL_GCS
 274     case Generation::ConcurrentMarkSweep:
 275 #endif // INCLUDE_ALL_GCS
 276       _fullGC = true;
 277       break;
 278     default:
 279       _fullGC = false;
 280       assert(false, "Unrecognized gc generation kind.");
 281   }
 282   // this has to be called in a stop the world pause and represent
 283   // an entire gc pause, start to finish:
 284   initialize(_fullGC, cause, true, true, true, true, true, true, true);
 285 }
 286 
 287 TraceMemoryManagerStats::TraceMemoryManagerStats(bool fullGC,
 288                                                  GCCause::Cause cause,
 289                                                  bool recordGCBeginTime,
 290                                                  bool recordPreGCUsage,
 291                                                  bool recordPeakUsage,
 292                                                  bool recordPostGCUsage,
 293                                                  bool recordAccumulatedGCTime,
 294                                                  bool recordGCEndTime,
 295                                                  bool countCollection) {
 296   initialize(fullGC, cause, recordGCBeginTime, recordPreGCUsage, recordPeakUsage,
 297              recordPostGCUsage, recordAccumulatedGCTime, recordGCEndTime,
 298              countCollection);
 299 }
 300 
 301 // for a subclass to create then initialize an instance before invoking
 302 // the MemoryService
 303 void TraceMemoryManagerStats::initialize(bool fullGC,
 304                                          GCCause::Cause cause,
 305                                          bool recordGCBeginTime,
 306                                          bool recordPreGCUsage,
 307                                          bool recordPeakUsage,
 308                                          bool recordPostGCUsage,
 309                                          bool recordAccumulatedGCTime,
 310                                          bool recordGCEndTime,
 311                                          bool countCollection) {
 312   _fullGC = fullGC;
 313   _recordGCBeginTime = recordGCBeginTime;
 314   _recordPreGCUsage = recordPreGCUsage;
 315   _recordPeakUsage = recordPeakUsage;
 316   _recordPostGCUsage = recordPostGCUsage;
 317   _recordAccumulatedGCTime = recordAccumulatedGCTime;
 318   _recordGCEndTime = recordGCEndTime;
 319   _countCollection = countCollection;
 320   _cause = cause;
 321 
 322   MemoryService::gc_begin(_fullGC, _recordGCBeginTime, _recordAccumulatedGCTime,
 323                           _recordPreGCUsage, _recordPeakUsage);
 324 }
 325 
 326 TraceMemoryManagerStats::~TraceMemoryManagerStats() {
 327   MemoryService::gc_end(_fullGC, _recordPostGCUsage, _recordAccumulatedGCTime,
 328                         _recordGCEndTime, _countCollection, _cause);
 329 }