1 /*
2  * Copyright (c) 2017, 2020, 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 ZGenerationCounters : public GenerationCounters {
34 public:
ZGenerationCounters(const char * name,int ordinal,int spaces,size_t min_capacity,size_t max_capacity,size_t curr_capacity)35   ZGenerationCounters(const char* name, int ordinal, int spaces,
36                       size_t min_capacity, size_t max_capacity, size_t curr_capacity) :
37       GenerationCounters(name, ordinal, spaces,
38                          min_capacity, max_capacity, curr_capacity) {}
39 
update_capacity(size_t capacity)40   void update_capacity(size_t capacity) {
41     _current_size->set_value(capacity);
42   }
43 };
44 
45 // Class to expose perf counters used by jstat.
46 class ZServiceabilityCounters : public CHeapObj<mtGC> {
47 private:
48   ZGenerationCounters _generation_counters;
49   HSpaceCounters      _space_counters;
50   CollectorCounters   _collector_counters;
51 
52 public:
53   ZServiceabilityCounters(size_t min_capacity, size_t max_capacity);
54 
55   CollectorCounters* collector_counters();
56 
57   void update_sizes();
58 };
59 
ZServiceabilityCounters(size_t min_capacity,size_t max_capacity)60 ZServiceabilityCounters::ZServiceabilityCounters(size_t min_capacity, size_t max_capacity) :
61     // generation.1
62     _generation_counters("old"        /* name */,
63                          1            /* ordinal */,
64                          1            /* spaces */,
65                          min_capacity /* min_capacity */,
66                          max_capacity /* max_capacity */,
67                          min_capacity /* curr_capacity */),
68     // generation.1.space.0
69     _space_counters(_generation_counters.name_space(),
70                     "space"      /* name */,
71                     0            /* ordinal */,
72                     max_capacity /* max_capacity */,
73                     min_capacity /* init_capacity */),
74     // gc.collector.2
75     _collector_counters("Z concurrent cycle pauses" /* name */,
76                         2                           /* ordinal */) {}
77 
collector_counters()78 CollectorCounters* ZServiceabilityCounters::collector_counters() {
79   return &_collector_counters;
80 }
81 
update_sizes()82 void ZServiceabilityCounters::update_sizes() {
83   if (UsePerfData) {
84     const size_t capacity = ZHeap::heap()->capacity();
85     const size_t used = MIN2(ZHeap::heap()->used(), capacity);
86 
87     _generation_counters.update_capacity(capacity);
88     _space_counters.update_capacity(capacity);
89     _space_counters.update_used(used);
90 
91     MetaspaceCounters::update_performance_counters();
92     CompressedClassSpaceCounters::update_performance_counters();
93   }
94 }
95 
ZServiceabilityMemoryPool(size_t min_capacity,size_t max_capacity)96 ZServiceabilityMemoryPool::ZServiceabilityMemoryPool(size_t min_capacity, size_t max_capacity) :
97     CollectedMemoryPool("ZHeap",
98                         min_capacity,
99                         max_capacity,
100                         true /* support_usage_threshold */) {}
101 
used_in_bytes()102 size_t ZServiceabilityMemoryPool::used_in_bytes() {
103   return ZHeap::heap()->used();
104 }
105 
get_memory_usage()106 MemoryUsage ZServiceabilityMemoryPool::get_memory_usage() {
107   const size_t committed = ZHeap::heap()->capacity();
108   const size_t used      = MIN2(ZHeap::heap()->used(), committed);
109 
110   return MemoryUsage(initial_size(), used, committed, max_size());
111 }
112 
ZServiceabilityMemoryManager(ZServiceabilityMemoryPool * pool)113 ZServiceabilityMemoryManager::ZServiceabilityMemoryManager(ZServiceabilityMemoryPool* pool)
114     : GCMemoryManager("ZGC", "end of major GC") {
115   add_pool(pool);
116 }
117 
ZServiceability(size_t min_capacity,size_t max_capacity)118 ZServiceability::ZServiceability(size_t min_capacity, size_t max_capacity) :
119     _min_capacity(min_capacity),
120     _max_capacity(max_capacity),
121     _memory_pool(_min_capacity, _max_capacity),
122     _memory_manager(&_memory_pool),
123     _counters(NULL) {}
124 
initialize()125 void ZServiceability::initialize() {
126   _counters = new ZServiceabilityCounters(_min_capacity, _max_capacity);
127 }
128 
memory_pool()129 MemoryPool* ZServiceability::memory_pool() {
130   return &_memory_pool;
131 }
132 
memory_manager()133 GCMemoryManager* ZServiceability::memory_manager() {
134   return &_memory_manager;
135 }
136 
counters()137 ZServiceabilityCounters* ZServiceability::counters() {
138   return _counters;
139 }
140 
ZServiceabilityCycleTracer()141 ZServiceabilityCycleTracer::ZServiceabilityCycleTracer() :
142     _memory_manager_stats(ZHeap::heap()->serviceability_memory_manager(),
143                           ZCollectedHeap::heap()->gc_cause(),
144                           true /* allMemoryPoolsAffected */,
145                           true /* recordGCBeginTime */,
146                           true /* recordPreGCUsage */,
147                           true /* recordPeakUsage */,
148                           true /* recordPostGCUsage */,
149                           true /* recordAccumulatedGCTime */,
150                           true /* recordGCEndTime */,
151                           true /* countCollection */) {}
152 
ZServiceabilityPauseTracer()153 ZServiceabilityPauseTracer::ZServiceabilityPauseTracer() :
154     _svc_gc_marker(SvcGCMarker::CONCURRENT),
155     _counters_stats(ZHeap::heap()->serviceability_counters()->collector_counters()) {}
156 
~ZServiceabilityPauseTracer()157 ZServiceabilityPauseTracer::~ZServiceabilityPauseTracer()  {
158   ZHeap::heap()->serviceability_counters()->update_sizes();
159   MemoryService::track_memory_usage();
160 }
161