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/shenandoahAsserts.hpp"
 27 #include "gc/shenandoah/shenandoahMmuTracker.hpp"
 28 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
 29 #include "gc/shenandoah/shenandoahOldGeneration.hpp"
 30 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
 31 #include "logging/log.hpp"
 32 #include "runtime/os.hpp"
 33 #include "runtime/task.hpp"
 34 
 35 class ShenandoahMmuTask : public PeriodicTask {
 36   ShenandoahMmuTracker* _mmu_tracker;
 37 public:
 38   explicit ShenandoahMmuTask(ShenandoahMmuTracker* mmu_tracker) :
 39     PeriodicTask(GCPauseIntervalMillis), _mmu_tracker(mmu_tracker) {}
 40 
 41   void task() override {
 42     _mmu_tracker->report();
 43   }
 44 };
 45 
 46 class ThreadTimeAccumulator : public ThreadClosure {
 47  public:
 48   size_t total_time;
 49   ThreadTimeAccumulator() : total_time(0) {}
 50   void do_thread(Thread* thread) override {
 51     total_time += os::thread_cpu_time(thread);
 52   }
 53 };
 54 
 55 ShenandoahMmuTracker::ShenandoahMmuTracker() :
 56     _most_recent_timestamp(0.0),
 57     _most_recent_gc_time(0.0),
 58     _most_recent_gcu(0.0),
 59     _most_recent_mutator_time(0.0),
 60     _most_recent_mu(0.0),
 61     _most_recent_periodic_time_stamp(0.0),
 62     _most_recent_periodic_gc_time(0.0),
 63     _most_recent_periodic_mutator_time(0.0),
 64     _mmu_periodic_task(new ShenandoahMmuTask(this)) {
 65 }
 66 
 67 ShenandoahMmuTracker::~ShenandoahMmuTracker() {
 68   _mmu_periodic_task->disenroll();
 69   delete _mmu_periodic_task;
 70 }
 71 
 72 void ShenandoahMmuTracker::fetch_cpu_times(double &gc_time, double &mutator_time) {
 73   ThreadTimeAccumulator cl;
 74   // We include only the gc threads because those are the only threads
 75   // we are responsible for.
 76   ShenandoahHeap::heap()->gc_threads_do(&cl);
 77   double most_recent_gc_thread_time = double(cl.total_time) / NANOSECS_PER_SEC;
 78   gc_time = most_recent_gc_thread_time;
 79 
 80   double process_real_time(0.0), process_user_time(0.0), process_system_time(0.0);
 81   bool valid = os::getTimesSecs(&process_real_time, &process_user_time, &process_system_time);
 82   assert(valid, "don't know why this would not be valid");
 83   mutator_time =(process_user_time + process_system_time) - most_recent_gc_thread_time;
 84 }
 85 
 86 void ShenandoahMmuTracker::update_utilization(size_t gcid, const char* msg) {
 87   double current = os::elapsedTime();
 88   _most_recent_gcid = gcid;
 89   _most_recent_is_full = false;
 90 
 91   if (gcid == 0) {
 92     fetch_cpu_times(_most_recent_gc_time, _most_recent_mutator_time);
 93 
 94     _most_recent_timestamp = current;
 95   } else {
 96     double gc_cycle_period = current - _most_recent_timestamp;
 97     _most_recent_timestamp = current;
 98 
 99     double gc_thread_time, mutator_thread_time;
100     fetch_cpu_times(gc_thread_time, mutator_thread_time);
101     double gc_time = gc_thread_time - _most_recent_gc_time;
102     _most_recent_gc_time = gc_thread_time;
103     _most_recent_gcu = gc_time / (_active_processors * gc_cycle_period);
104     double mutator_time = mutator_thread_time - _most_recent_mutator_time;
105     _most_recent_mutator_time = mutator_thread_time;
106     _most_recent_mu = mutator_time / (_active_processors * gc_cycle_period);
107     log_info(gc, ergo)("At end of %s: GCU: %.1f%%, MU: %.1f%% during period of %.3fs",
108                        msg, _most_recent_gcu * 100, _most_recent_mu * 100, gc_cycle_period);
109   }
110 }
111 
112 void ShenandoahMmuTracker::record_young(size_t gcid) {
113   update_utilization(gcid, "Concurrent Young GC");
114 }
115 
116 void ShenandoahMmuTracker::record_global(size_t gcid) {
117   update_utilization(gcid, "Concurrent Global GC");
118 }
119 
120 void ShenandoahMmuTracker::record_bootstrap(size_t gcid) {
121   // Not likely that this will represent an "ideal" GCU, but doesn't hurt to try
122   update_utilization(gcid, "Concurrent Bootstrap GC");
123 }
124 
125 void ShenandoahMmuTracker::record_old_marking_increment(bool old_marking_done) {
126   // No special processing for old marking
127   double now = os::elapsedTime();
128   double duration = now - _most_recent_timestamp;
129 
130   double gc_time, mutator_time;
131   fetch_cpu_times(gc_time, mutator_time);
132   double gcu = (gc_time - _most_recent_gc_time) / duration;
133   double mu = (mutator_time - _most_recent_mutator_time) / duration;
134   log_info(gc, ergo)("At end of %s: GCU: %.1f%%, MU: %.1f%% for duration %.3fs (totals to be subsumed in next gc report)",
135                      old_marking_done? "last OLD marking increment": "OLD marking increment",
136                      gcu * 100, mu * 100, duration);
137 }
138 
139 void ShenandoahMmuTracker::record_mixed(size_t gcid) {
140   update_utilization(gcid, "Mixed Concurrent GC");
141 }
142 
143 void ShenandoahMmuTracker::record_degenerated(size_t gcid, bool is_old_bootstrap) {
144   if ((gcid == _most_recent_gcid) && _most_recent_is_full) {
145     // Do nothing.  This is a redundant recording for the full gc that just completed.
146     // TODO: avoid making the call to record_degenerated() in the case that this degenerated upgraded to full gc.
147   } else if (is_old_bootstrap) {
148     update_utilization(gcid, "Degenerated Bootstrap Old GC");
149   } else {
150     update_utilization(gcid, "Degenerated Young GC");
151   }
152 }
153 
154 void ShenandoahMmuTracker::record_full(size_t gcid) {
155   update_utilization(gcid, "Full GC");
156   _most_recent_is_full = true;
157 }
158 
159 void ShenandoahMmuTracker::report() {
160   // This is only called by the periodic thread.
161   double current = os::elapsedTime();
162   double time_delta = current - _most_recent_periodic_time_stamp;
163   _most_recent_periodic_time_stamp = current;
164 
165   double gc_time, mutator_time;
166   fetch_cpu_times(gc_time, mutator_time);
167 
168   double gc_delta = gc_time - _most_recent_periodic_gc_time;
169   _most_recent_periodic_gc_time = gc_time;
170 
171   double mutator_delta = mutator_time - _most_recent_periodic_mutator_time;
172   _most_recent_periodic_mutator_time = mutator_time;
173 
174   double mu = mutator_delta / (_active_processors * time_delta);
175   double gcu = gc_delta / (_active_processors * time_delta);
176   log_info(gc)("Periodic Sample: GCU = %.3f%%, MU = %.3f%% during most recent %.1fs", gcu * 100, mu * 100, time_delta);
177 }
178 
179 void ShenandoahMmuTracker::initialize() {
180   // initialize static data
181   _active_processors = os::initial_active_processor_count();
182 
183   _most_recent_periodic_time_stamp = os::elapsedTime();
184   fetch_cpu_times(_most_recent_periodic_gc_time, _most_recent_periodic_mutator_time);
185   _mmu_periodic_task->enroll();
186 }
187 
188 ShenandoahGenerationSizer::ShenandoahGenerationSizer()
189   : _sizer_kind(SizerDefaults),
190     _min_desired_young_regions(0),
191     _max_desired_young_regions(0) {
192 
193   if (FLAG_IS_CMDLINE(NewRatio)) {
194     if (FLAG_IS_CMDLINE(NewSize) || FLAG_IS_CMDLINE(MaxNewSize)) {
195       log_warning(gc, ergo)("-XX:NewSize and -XX:MaxNewSize override -XX:NewRatio");
196     } else {
197       _sizer_kind = SizerNewRatio;
198       return;
199     }
200   }
201 
202   if (NewSize > MaxNewSize) {
203     if (FLAG_IS_CMDLINE(MaxNewSize)) {
204       log_warning(gc, ergo)("NewSize (" SIZE_FORMAT "k) is greater than the MaxNewSize (" SIZE_FORMAT "k). "
205                             "A new max generation size of " SIZE_FORMAT "k will be used.",
206                             NewSize/K, MaxNewSize/K, NewSize/K);
207     }
208     FLAG_SET_ERGO(MaxNewSize, NewSize);
209   }
210 
211   if (FLAG_IS_CMDLINE(NewSize)) {
212     _min_desired_young_regions = MAX2(uint(NewSize / ShenandoahHeapRegion::region_size_bytes()), 1U);
213     if (FLAG_IS_CMDLINE(MaxNewSize)) {
214       _max_desired_young_regions = MAX2(uint(MaxNewSize / ShenandoahHeapRegion::region_size_bytes()), 1U);
215       _sizer_kind = SizerMaxAndNewSize;
216     } else {
217       _sizer_kind = SizerNewSizeOnly;
218     }
219   } else if (FLAG_IS_CMDLINE(MaxNewSize)) {
220     _max_desired_young_regions = MAX2(uint(MaxNewSize / ShenandoahHeapRegion::region_size_bytes()), 1U);
221     _sizer_kind = SizerMaxNewSizeOnly;
222   }
223 }
224 
225 size_t ShenandoahGenerationSizer::calculate_min_young_regions(size_t heap_region_count) {
226   size_t min_young_regions = (heap_region_count * ShenandoahMinYoungPercentage) / 100;
227   return MAX2(min_young_regions, (size_t) 1U);
228 }
229 
230 size_t ShenandoahGenerationSizer::calculate_max_young_regions(size_t heap_region_count) {
231   size_t max_young_regions = (heap_region_count * ShenandoahMaxYoungPercentage) / 100;
232   return MAX2(max_young_regions, (size_t) 1U);
233 }
234 
235 void ShenandoahGenerationSizer::recalculate_min_max_young_length(size_t heap_region_count) {
236   assert(heap_region_count > 0, "Heap must be initialized");
237 
238   switch (_sizer_kind) {
239     case SizerDefaults:
240       _min_desired_young_regions = calculate_min_young_regions(heap_region_count);
241       _max_desired_young_regions = calculate_max_young_regions(heap_region_count);
242       break;
243     case SizerNewSizeOnly:
244       _max_desired_young_regions = calculate_max_young_regions(heap_region_count);
245       _max_desired_young_regions = MAX2(_min_desired_young_regions, _max_desired_young_regions);
246       break;
247     case SizerMaxNewSizeOnly:
248       _min_desired_young_regions = calculate_min_young_regions(heap_region_count);
249       _min_desired_young_regions = MIN2(_min_desired_young_regions, _max_desired_young_regions);
250       break;
251     case SizerMaxAndNewSize:
252       // Do nothing. Values set on the command line, don't update them at runtime.
253       break;
254     case SizerNewRatio:
255       _min_desired_young_regions = MAX2(uint(heap_region_count / (NewRatio + 1)), 1U);
256       _max_desired_young_regions = _min_desired_young_regions;
257       break;
258     default:
259       ShouldNotReachHere();
260   }
261 
262   assert(_min_desired_young_regions <= _max_desired_young_regions, "Invalid min/max young gen size values");
263 }
264 
265 void ShenandoahGenerationSizer::heap_size_changed(size_t heap_size) {
266   recalculate_min_max_young_length(heap_size / ShenandoahHeapRegion::region_size_bytes());
267 }
268 
269 bool ShenandoahGenerationSizer::transfer_regions(ShenandoahGeneration* src, ShenandoahGeneration* dst, size_t regions) const {
270   const size_t bytes_to_transfer = regions * ShenandoahHeapRegion::region_size_bytes();
271 
272   if (src->free_unaffiliated_regions() < regions) {
273     // Source does not have enough free regions for this transfer. The caller should have
274     // already capped the transfer based on available unaffiliated regions.
275     return false;
276   }
277 
278   if (dst->max_capacity() + bytes_to_transfer > max_size_for(dst)) {
279     // This transfer would cause the destination generation to grow above its configured maximum size.
280     return false;
281   }
282 
283   if (src->max_capacity() - bytes_to_transfer < min_size_for(src)) {
284     // This transfer would cause the source generation to shrink below its configured minimum size.
285     return false;
286   }
287 
288   src->decrease_capacity(bytes_to_transfer);
289   dst->increase_capacity(bytes_to_transfer);
290   const size_t new_size = dst->max_capacity();
291   log_info(gc)("Transfer " SIZE_FORMAT " region(s) from %s to %s, yielding increased size: " PROPERFMT,
292                regions, src->name(), dst->name(), PROPERFMTARGS(new_size));
293   return true;
294 }
295 
296 
297 size_t ShenandoahGenerationSizer::max_size_for(ShenandoahGeneration* generation) const {
298   switch (generation->type()) {
299     case YOUNG:
300       return max_young_size();
301     case OLD:
302       return min_young_size();
303     default:
304       ShouldNotReachHere();
305       return 0;
306   }
307 }
308 
309 size_t ShenandoahGenerationSizer::min_size_for(ShenandoahGeneration* generation) const {
310   switch (generation->type()) {
311     case YOUNG:
312       return min_young_size();
313     case OLD:
314       return ShenandoahHeap::heap()->max_capacity() - max_young_size();
315     default:
316       ShouldNotReachHere();
317       return 0;
318   }
319 }
320 
321 
322 // Returns true iff transfer is successful
323 bool ShenandoahGenerationSizer::transfer_to_old(size_t regions) const {
324   ShenandoahHeap* heap = ShenandoahHeap::heap();
325   return transfer_regions(heap->young_generation(), heap->old_generation(), regions);
326 }
327 
328 // This is used when promoting humongous or highly utilized regular regions in place.  It is not required in this situation
329 // that the transferred regions be unaffiliated.
330 void ShenandoahGenerationSizer::force_transfer_to_old(size_t regions) const {
331   ShenandoahHeap* heap = ShenandoahHeap::heap();
332   ShenandoahGeneration* old_gen = heap->old_generation();
333   ShenandoahGeneration* young_gen = heap->young_generation();
334   const size_t bytes_to_transfer = regions * ShenandoahHeapRegion::region_size_bytes();
335 
336   young_gen->decrease_capacity(bytes_to_transfer);
337   old_gen->increase_capacity(bytes_to_transfer);
338   const size_t new_size = old_gen->max_capacity();
339   log_info(gc)("Forcing transfer of " SIZE_FORMAT " region(s) from %s to %s, yielding increased size: " PROPERFMT,
340                regions, young_gen->name(), old_gen->name(), PROPERFMTARGS(new_size));
341 }
342 
343 
344 bool ShenandoahGenerationSizer::transfer_to_young(size_t regions) const {
345   ShenandoahHeap* heap = ShenandoahHeap::heap();
346   return transfer_regions(heap->old_generation(), heap->young_generation(), regions);
347 }
348 
349 size_t ShenandoahGenerationSizer::min_young_size() const {
350   return min_young_regions() * ShenandoahHeapRegion::region_size_bytes();
351 }
352 
353 size_t ShenandoahGenerationSizer::max_young_size() const {
354   return max_young_regions() * ShenandoahHeapRegion::region_size_bytes();
355 }