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:
ZOldGenerationCounters(const char * name,size_t min_capacity,size_t max_capacity)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 
update_all()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 
ZServiceabilityCounters(size_t min_capacity,size_t max_capacity)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 
update_sizes()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 
ZServiceabilityMemoryPool(size_t min_capacity,size_t max_capacity)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 
used_in_bytes()95 size_t ZServiceabilityMemoryPool::used_in_bytes() {
96   return ZHeap::heap()->used();
97 }
98 
get_memory_usage()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 
ZServiceabilityMemoryManager(ZServiceabilityMemoryPool * pool)106 ZServiceabilityMemoryManager::ZServiceabilityMemoryManager(ZServiceabilityMemoryPool* pool)
107     : GCMemoryManager("ZGC", "end of major GC") {
108   add_pool(pool);
109 }
110 
ZServiceability(size_t min_capacity,size_t max_capacity)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 
initialize()118 void ZServiceability::initialize() {
119   _counters = new ZServiceabilityCounters(_min_capacity, _max_capacity);
120 }
121 
memory_pool()122 MemoryPool* ZServiceability::memory_pool() {
123   return &_memory_pool;
124 }
125 
memory_manager()126 GCMemoryManager* ZServiceability::memory_manager() {
127   return &_memory_manager;
128 }
129 
counters()130 ZServiceabilityCounters* ZServiceability::counters() {
131   return _counters;
132 }
133 
~ZServiceabilityMemoryUsageTracker()134 ZServiceabilityMemoryUsageTracker::~ZServiceabilityMemoryUsageTracker() {
135   MemoryService::track_memory_usage();
136 }
137 
ZServiceabilityManagerStatsTracer(bool is_gc_begin,bool is_gc_end)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            true        /* allMemoryPoolsAffected */,
142            is_gc_begin /* recordGCBeginTime */,
143            is_gc_begin /* recordPreGCUsage */,
144            true        /* recordPeakUsage */,
145            is_gc_end   /* recordPostGCusage */,
146            true        /* recordAccumulatedGCTime */,
147            is_gc_end   /* recordGCEndTime */,
148            is_gc_end   /* countCollection */) {}
149 
ZServiceabilityCountersTracer()150 ZServiceabilityCountersTracer::ZServiceabilityCountersTracer() {
151   // Nothing to trace with TraceCollectorStats, since ZGC has
152   // neither a young collector nor a full collector.
153 }
154 
~ZServiceabilityCountersTracer()155 ZServiceabilityCountersTracer::~ZServiceabilityCountersTracer() {
156   ZHeap::heap()->serviceability_counters()->update_sizes();
157 }
158