1 /*
  2  * Copyright (c) 2015, 2019, Red Hat, Inc. 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 "gc/shared/collectorCounters.hpp"
 27 #include "gc/shared/generationCounters.hpp"
 28 #include "gc/shared/hSpaceCounters.hpp"
 29 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
 30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 31 #include "gc/shenandoah/shenandoahHeapRegionCounters.hpp"
 32 #include "memory/metaspaceCounters.hpp"
 33 #include "services/memoryService.hpp"
 34 
 35 class ShenandoahYoungGenerationCounters : public GenerationCounters {
 36 public:
 37   ShenandoahYoungGenerationCounters() :
 38           GenerationCounters("Young", 0, 0, 0, (size_t)0, (size_t)0) {};
 39 
 40   void update_all() override {
 41     // no update
 42   }
 43 };
 44 
 45 class ShenandoahGenerationCounters : public GenerationCounters {
 46 private:
 47   ShenandoahHeap* _heap;
 48 public:
 49   explicit ShenandoahGenerationCounters(ShenandoahHeap* heap) :
 50           GenerationCounters("Heap", 1, 1, heap->initial_capacity(), heap->max_capacity(), heap->capacity()),
 51           _heap(heap)
 52   {};
 53 
 54   void update_all() override {
 55     _current_size->set_value(_heap->capacity());
 56   }
 57 };
 58 
 59 ShenandoahMonitoringSupport::ShenandoahMonitoringSupport(ShenandoahHeap* heap) :
 60         _partial_counters(nullptr),
 61         _full_counters(nullptr),
 62         _counters_update_task(this)
 63 {
 64   // Collection counters do not fit Shenandoah very well.
 65   // We record partial cycles as "young", and full cycles (including full STW GC) as "old".
 66   _partial_counters  = new CollectorCounters("Shenandoah partial", 0);
 67   _full_counters     = new CollectorCounters("Shenandoah full",    1);
 68 
 69   // We report young gen as unused.
 70   _young_counters = new ShenandoahYoungGenerationCounters();
 71   _heap_counters  = new ShenandoahGenerationCounters(heap);
 72   _space_counters = new HSpaceCounters(_heap_counters->name_space(), "Heap", 0, heap->max_capacity(), heap->initial_capacity());
 73 
 74   _heap_region_counters = new ShenandoahHeapRegionCounters();
 75 
 76   _counters_update_task.enroll();
 77 }
 78 
 79 CollectorCounters* ShenandoahMonitoringSupport::stw_collection_counters() {
 80   return _full_counters;
 81 }
 82 
 83 CollectorCounters* ShenandoahMonitoringSupport::full_stw_collection_counters() {
 84   return _full_counters;
 85 }
 86 
 87 CollectorCounters* ShenandoahMonitoringSupport::concurrent_collection_counters() {
 88   return _full_counters;
 89 }
 90 
 91 CollectorCounters* ShenandoahMonitoringSupport::partial_collection_counters() {
 92   return _partial_counters;
 93 }
 94 
 95 void ShenandoahMonitoringSupport::update_counters() {
 96   MemoryService::track_memory_usage();
 97 
 98   if (UsePerfData) {
 99     ShenandoahHeap* heap = ShenandoahHeap::heap();
100     size_t used = heap->used();
101     size_t capacity = heap->max_capacity();
102     _heap_counters->update_all();
103     _space_counters->update_all(capacity, used);
104     _heap_region_counters->update();
105 
106     MetaspaceCounters::update_performance_counters();
107   }
108 }
109 
110 void ShenandoahMonitoringSupport::notify_heap_changed() {
111   _counters_update_task.notify_heap_changed();
112 }
113 
114 void ShenandoahMonitoringSupport::set_forced_counters_update(bool value) {
115   _counters_update_task.set_forced_counters_update(value);
116 }
117 
118 void ShenandoahMonitoringSupport::handle_force_counters_update() {
119   _counters_update_task.handle_force_counters_update();
120 }
121 
122 void ShenandoahPeriodicCountersUpdateTask::task() {
123   handle_force_counters_update();
124   handle_counters_update();
125 }
126 
127 void ShenandoahPeriodicCountersUpdateTask::handle_counters_update() {
128   if (_do_counters_update.is_set()) {
129     _do_counters_update.unset();
130     _monitoring_support->update_counters();
131   }
132 }
133 
134 void ShenandoahPeriodicCountersUpdateTask::handle_force_counters_update() {
135   if (_force_counters_update.is_set()) {
136     _do_counters_update.unset(); // reset these too, we do update now!
137     _monitoring_support->update_counters();
138   }
139 }
140 
141 void ShenandoahPeriodicCountersUpdateTask::notify_heap_changed() {
142   if (_do_counters_update.is_unset()) {
143     _do_counters_update.set();
144   }
145 }
146 
147 void ShenandoahPeriodicCountersUpdateTask::set_forced_counters_update(bool value) {
148   _force_counters_update.set_cond(value);
149 }