1 /*
   2  * Copyright (c) 2017, 2018, 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 #include "precompiled.hpp"
  25 #include "gc/shared/generationCounters.hpp"
  26 #include "gc/shared/hSpaceCounters.hpp"
  27 #include "gc/z/zCollectedHeap.hpp"
  28 #include "gc/z/zHeap.inline.hpp"
  29 #include "gc/z/zServiceability.hpp"
  30 #include "memory/metaspaceCounters.hpp"
  31 #include "runtime/perfData.hpp"
  32 
  33 class ZOldGenerationCounters : public GenerationCounters {
  34 public:
  35   ZOldGenerationCounters(const char* name, size_t min_capacity, size_t max_capacity) :
  36     // The "1, 1" parameters are for the n-th generation (=1) with 1 space.
  37     GenerationCounters(name,
  38                        1 /* ordinal */,
  39                        1 /* spaces */,
  40                        min_capacity /* min_capacity */,
  41                        max_capacity /* max_capacity */,
  42                        min_capacity /* curr_capacity */) {}
  43 
  44   virtual void update_all() {
  45     size_t committed = ZHeap::heap()->capacity();
  46     _current_size->set_value(committed);
  47   }
  48 };
  49 
  50 // Class to expose perf counters used by jstat.
  51 class ZServiceabilityCounters : public CHeapObj<mtGC> {
  52 private:
  53   ZOldGenerationCounters _old_collection_counters;
  54   HSpaceCounters         _old_space_counters;
  55 
  56 public:
  57   ZServiceabilityCounters(size_t min_capacity, size_t max_capacity);
  58 
  59   void update_sizes();
  60 };
  61 
  62 ZServiceabilityCounters::ZServiceabilityCounters(size_t min_capacity, size_t max_capacity) :
  63     // generation.1
  64     _old_collection_counters("old",
  65                              min_capacity,
  66                              max_capacity),
  67     // generation.1.space.0
  68     _old_space_counters(_old_collection_counters.name_space(),
  69                         "space",
  70                         0 /* ordinal */,
  71                         max_capacity /* max_capacity */,
  72                         min_capacity /* init_capacity */) {}
  73 
  74 void ZServiceabilityCounters::update_sizes() {
  75   if (UsePerfData) {
  76     size_t capacity = ZHeap::heap()->capacity();
  77     size_t used = MIN2(ZHeap::heap()->used(), capacity);
  78 
  79     _old_space_counters.update_capacity(capacity);
  80     _old_space_counters.update_used(used);
  81 
  82     _old_collection_counters.update_all();
  83 
  84     MetaspaceCounters::update_performance_counters();
  85     CompressedClassSpaceCounters::update_performance_counters();
  86   }
  87 }
  88 
  89 ZServiceabilityMemoryPool::ZServiceabilityMemoryPool(size_t min_capacity, size_t max_capacity) :
  90     CollectedMemoryPool("ZHeap",
  91                         min_capacity,
  92                         max_capacity,
  93                         true /* support_usage_threshold */) {}
  94 
  95 size_t ZServiceabilityMemoryPool::used_in_bytes() {
  96   return ZHeap::heap()->used();
  97 }
  98 
  99 MemoryUsage ZServiceabilityMemoryPool::get_memory_usage() {
 100   const size_t committed = ZHeap::heap()->capacity();
 101   const size_t used      = MIN2(ZHeap::heap()->used(), committed);
 102 
 103   return MemoryUsage(initial_size(), used, committed, max_size());
 104 }
 105 
 106 ZServiceabilityMemoryManager::ZServiceabilityMemoryManager(ZServiceabilityMemoryPool* pool)
 107     : GCMemoryManager("ZGC", "end of major GC") {
 108   add_pool(pool);
 109 }
 110 
 111 ZServiceability::ZServiceability(size_t min_capacity, size_t max_capacity) :
 112     _min_capacity(min_capacity),
 113     _max_capacity(max_capacity),
 114     _memory_pool(_min_capacity, _max_capacity),
 115     _memory_manager(&_memory_pool),
 116     _counters(NULL) {}
 117 
 118 void ZServiceability::initialize() {
 119   _counters = new ZServiceabilityCounters(_min_capacity, _max_capacity);
 120 }
 121 
 122 MemoryPool* ZServiceability::memory_pool() {
 123   return &_memory_pool;
 124 }
 125 
 126 GCMemoryManager* ZServiceability::memory_manager() {
 127   return &_memory_manager;
 128 }
 129 
 130 ZServiceabilityCounters* ZServiceability::counters() {
 131   return _counters;
 132 }
 133 
 134 ZServiceabilityMemoryUsageTracker::~ZServiceabilityMemoryUsageTracker() {
 135   MemoryService::track_memory_usage();
 136 }
 137 
 138 ZServiceabilityManagerStatsTracer::ZServiceabilityManagerStatsTracer(bool is_gc_begin, bool is_gc_end) :
 139     _stats(ZHeap::heap()->serviceability_memory_manager(),
 140            ZCollectedHeap::heap()->gc_cause() /* cause */,
 141            is_gc_begin /* recordGCBeginTime */,
 142            is_gc_begin /* recordPreGCUsage */,
 143            true        /* recordPeakUsage */,
 144            is_gc_end   /* recordPostGCusage */,
 145            true        /* recordAccumulatedGCTime */,
 146            is_gc_end   /* recordGCEndTime */,
 147            is_gc_end   /* countCollection */) {}
 148 
 149 ZServiceabilityCountersTracer::ZServiceabilityCountersTracer() {
 150   // Nothing to trace with TraceCollectorStats, since ZGC has
 151   // neither a young collector or a full collector.
 152 }
 153 
 154 ZServiceabilityCountersTracer::~ZServiceabilityCountersTracer() {
 155   ZHeap::heap()->serviceability_counters()->update_sizes();
 156 }