1 /*
  2  * Copyright Amazon.com Inc. 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 
 26 #include "gc/shenandoah/shenandoahAgeCensus.hpp"
 27 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 28 #include "gc/shenandoah/shenandoahEvacTracker.hpp"
 29 #include "gc/shenandoah/shenandoahThreadLocalData.hpp"
 30 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
 31 #include "runtime/threadSMR.inline.hpp"
 32 #include "runtime/thread.hpp"
 33 
 34 ShenandoahEvacuationStats::ShenandoahEvacuationStats(bool generational)
 35   : _evacuations_completed(0), _bytes_completed(0),
 36     _evacuations_attempted(0), _bytes_attempted(0),
 37     _use_age_table(generational && (ShenandoahGenerationalCensusAtEvac || !ShenandoahGenerationalAdaptiveTenuring)) {
 38   if (_use_age_table) {
 39     _age_table = new AgeTable(false);
 40   }
 41 }
 42 
 43 AgeTable* ShenandoahEvacuationStats::age_table() const {
 44   assert(_use_age_table, "Don't call");
 45   return _age_table;
 46 }
 47 
 48 void ShenandoahEvacuationStats::begin_evacuation(size_t bytes) {
 49   ++_evacuations_attempted;
 50   _bytes_attempted += bytes;
 51 }
 52 
 53 void ShenandoahEvacuationStats::end_evacuation(size_t bytes) {
 54   ++_evacuations_completed;
 55   _bytes_completed += bytes;
 56 }
 57 
 58 void ShenandoahEvacuationStats::record_age(size_t bytes, uint age) {
 59   assert(_use_age_table, "Don't call!");
 60   if (age <= markWord::max_age) { // Filter age sentinel.
 61     _age_table->add(age, bytes >> LogBytesPerWord);
 62   }
 63 }
 64 
 65 void ShenandoahEvacuationStats::accumulate(const ShenandoahEvacuationStats* other) {
 66   _evacuations_completed += other->_evacuations_completed;
 67   _bytes_completed += other->_bytes_completed;
 68   _evacuations_attempted += other->_evacuations_attempted;
 69   _bytes_attempted += other->_bytes_attempted;
 70   if (_use_age_table) {
 71     _age_table->merge(other->age_table());
 72   }
 73 }
 74 
 75 void ShenandoahEvacuationStats::reset() {
 76   _evacuations_completed = _evacuations_attempted = 0;
 77   _bytes_completed = _bytes_attempted = 0;
 78   if (_use_age_table) {
 79     _age_table->clear();
 80   }
 81 }
 82 
 83 void ShenandoahEvacuationStats::print_on(outputStream* st) {
 84   size_t abandoned_size = _bytes_attempted - _bytes_completed;
 85   size_t abandoned_count = _evacuations_attempted - _evacuations_completed;
 86   st->print_cr("Evacuated " SIZE_FORMAT "%s across " SIZE_FORMAT " objects, "
 87             "abandoned " SIZE_FORMAT "%s across " SIZE_FORMAT " objects.",
 88             byte_size_in_proper_unit(_bytes_completed), proper_unit_for_byte_size(_bytes_completed),
 89             _evacuations_completed,
 90             byte_size_in_proper_unit(abandoned_size),   proper_unit_for_byte_size(abandoned_size),
 91             abandoned_count);
 92   if (_use_age_table) {
 93     _age_table->print_on(st, ShenandoahHeap::heap()->age_census()->tenuring_threshold());
 94   }
 95 }
 96 
 97 void ShenandoahEvacuationTracker::print_global_on(outputStream* st) {
 98   print_evacuations_on(st, &_workers_global, &_mutators_global);
 99 }
100 
101 void ShenandoahEvacuationTracker::print_evacuations_on(outputStream* st,
102                                                        ShenandoahEvacuationStats* workers,
103                                                        ShenandoahEvacuationStats* mutators) {
104   st->print("Workers: ");
105   workers->print_on(st);
106   st->cr();
107   st->print("Mutators: ");
108   mutators->print_on(st);
109   st->cr();
110 
111   ShenandoahHeap* heap = ShenandoahHeap::heap();
112   if (_generational) {
113     AgeTable young_region_ages(false);
114     for (uint i = 0; i < heap->num_regions(); ++i) {
115       ShenandoahHeapRegion* r = heap->get_region(i);
116       if (r->is_young()) {
117         young_region_ages.add(r->age(), r->get_live_data_words());
118       }
119     }
120     uint tenuring_threshold = heap->age_census()->tenuring_threshold();
121     st->print("Young regions: ");
122     young_region_ages.print_on(st, tenuring_threshold);
123     st->cr();
124   }
125 }
126 
127 class ShenandoahStatAggregator : public ThreadClosure {
128 public:
129   ShenandoahEvacuationStats* _target;
130   explicit ShenandoahStatAggregator(ShenandoahEvacuationStats* target) : _target(target) {}
131   virtual void do_thread(Thread* thread) override {
132     ShenandoahEvacuationStats* local = ShenandoahThreadLocalData::evacuation_stats(thread);
133     _target->accumulate(local);
134     local->reset();
135   }
136 };
137 
138 ShenandoahCycleStats ShenandoahEvacuationTracker::flush_cycle_to_global() {
139   ShenandoahEvacuationStats mutators(_generational), workers(_generational);
140 
141   ThreadsListHandle java_threads_iterator;
142   ShenandoahStatAggregator aggregate_mutators(&mutators);
143   java_threads_iterator.list()->threads_do(&aggregate_mutators);
144 
145   ShenandoahStatAggregator aggregate_workers(&workers);
146   ShenandoahHeap::heap()->gc_threads_do(&aggregate_workers);
147 
148   _mutators_global.accumulate(&mutators);
149   _workers_global.accumulate(&workers);
150 
151   if (_generational && (ShenandoahGenerationalCensusAtEvac || !ShenandoahGenerationalAdaptiveTenuring)) {
152     // Ingest population vectors into the heap's global census
153     // data, and use it to compute an appropriate tenuring threshold
154     // for use in the next cycle.
155     ShenandoahAgeCensus* census = ShenandoahHeap::heap()->age_census();
156     census->prepare_for_census_update();
157     // The first argument is used for any age 0 cohort population that we may otherwise have
158     // missed during the census. This is non-zero only when census happens at marking.
159     census->update_census(0, _mutators_global.age_table(), _workers_global.age_table());
160   }
161 
162   return {workers, mutators};
163 }
164 
165 void ShenandoahEvacuationTracker::begin_evacuation(Thread* thread, size_t bytes) {
166   ShenandoahThreadLocalData::begin_evacuation(thread, bytes);
167 }
168 
169 void ShenandoahEvacuationTracker::end_evacuation(Thread* thread, size_t bytes) {
170   ShenandoahThreadLocalData::end_evacuation(thread, bytes);
171 }
172 
173 void ShenandoahEvacuationTracker::record_age(Thread* thread, size_t bytes, uint age) {
174   ShenandoahThreadLocalData::record_age(thread, bytes, age);
175 }