1 /*
   2  * Copyright (c) 2018, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 
  26 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  27 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  28 #include "gc/shenandoah/shenandoahPacer.hpp"
  29 
  30 /*
  31  * In normal concurrent cycle, we have to pace the application to let GC finish.
  32  *
  33  * Here, we do not know how large would be the collection set, and what are the
  34  * relative performances of the each stage in the concurrent cycle, and so we have to
  35  * make some assumptions.
  36  *
  37  * For concurrent mark, there is no clear notion of progress. The moderately accurate
  38  * and easy to get metric is the amount of live objects the mark had encountered. But,
  39  * that does directly correlate with the used heap, because the heap might be fully
  40  * dead or fully alive. We cannot assume either of the extremes: we would either allow
  41  * application to run out of memory if we assume heap is fully dead but it is not, and,
  42  * conversely, we would pacify application excessively if we assume heap is fully alive
  43  * but it is not. So we need to guesstimate the particular expected value for heap liveness.
  44  * The best way to do this is apparently recording the past history.
  45  *
  46  * For concurrent evac and update-refs, we are walking the heap per-region, and so the
  47  * notion of progress is clear: we get reported the "used" size from the processed regions
  48  * and use the global heap-used as the baseline.
  49  *
  50  * The allocatable space when GC is running is "free" at the start of cycle, but the
  51  * accounted budget is based on "used". So, we need to adjust the tax knowing that.
  52  * Also, since we effectively count the used space three times (mark, evac, update-refs),
  53  * we need to multiply the tax by 3. Example: for 10 MB free and 90 MB used, GC would
  54  * come back with 3*90 MB budget, and thus for each 1 MB of allocation, we have to pay
  55  * 3*90 / 10 MBs. In the end, we would pay back the entire budget.
  56  */
  57 
  58 void ShenandoahPacer::setup_for_mark() {
  59   assert(ShenandoahPacing, "Only be here when pacing is enabled");
  60 
  61   size_t live = update_and_get_progress_history();
  62   size_t free = _heap->free_set()->available();
  63 
  64   size_t non_taxable = free * ShenandoahPacingCycleSlack / 100;
  65   size_t taxable = free - non_taxable;
  66 
  67   double tax = 1.0 * live / taxable; // base tax for available free space
  68   tax *= 3;                          // mark is phase 1 of 3, claim 1/3 of free for it
  69   tax *= ShenandoahPacingSurcharge;  // additional surcharge to help unclutter heap
  70 
  71   restart_with(non_taxable, tax);
  72 
  73   log_info(gc, ergo)("Pacer for Mark. Expected Live: " SIZE_FORMAT "%s, Free: " SIZE_FORMAT "%s, "
  74                      "Non-Taxable: " SIZE_FORMAT "%s, Alloc Tax Rate: %.1fx",
  75                      byte_size_in_proper_unit(live),        proper_unit_for_byte_size(live),
  76                      byte_size_in_proper_unit(free),        proper_unit_for_byte_size(free),
  77                      byte_size_in_proper_unit(non_taxable), proper_unit_for_byte_size(non_taxable),
  78                      tax);
  79 }
  80 
  81 void ShenandoahPacer::setup_for_evac() {
  82   assert(ShenandoahPacing, "Only be here when pacing is enabled");
  83 
  84   size_t used = _heap->collection_set()->used();
  85   size_t free = _heap->free_set()->available();
  86 
  87   size_t non_taxable = free * ShenandoahPacingCycleSlack / 100;
  88   size_t taxable = free - non_taxable;
  89 
  90   double tax = 1.0 * used / taxable; // base tax for available free space
  91   tax *= 2;                          // evac is phase 2 of 3, claim 1/2 of remaining free
  92   tax = MAX2<double>(1, tax);        // never allocate more than GC processes during the phase
  93   tax *= ShenandoahPacingSurcharge;  // additional surcharge to help unclutter heap
  94 
  95   restart_with(non_taxable, tax);
  96 
  97   log_info(gc, ergo)("Pacer for Evacuation. Used CSet: " SIZE_FORMAT "%s, Free: " SIZE_FORMAT "%s, "
  98                      "Non-Taxable: " SIZE_FORMAT "%s, Alloc Tax Rate: %.1fx",
  99                      byte_size_in_proper_unit(used),        proper_unit_for_byte_size(used),
 100                      byte_size_in_proper_unit(free),        proper_unit_for_byte_size(free),
 101                      byte_size_in_proper_unit(non_taxable), proper_unit_for_byte_size(non_taxable),
 102                      tax);
 103 }
 104 
 105 void ShenandoahPacer::setup_for_updaterefs() {
 106   assert(ShenandoahPacing, "Only be here when pacing is enabled");
 107 
 108   size_t used = _heap->used();
 109   size_t free = _heap->free_set()->available();
 110 
 111   size_t non_taxable = free * ShenandoahPacingCycleSlack / 100;
 112   size_t taxable = free - non_taxable;
 113 
 114   double tax = 1.0 * used / taxable; // base tax for available free space
 115   tax *= 1;                          // update-refs is phase 3 of 3, claim the remaining free
 116   tax = MAX2<double>(1, tax);        // never allocate more than GC processes during the phase
 117   tax *= ShenandoahPacingSurcharge;  // additional surcharge to help unclutter heap
 118 
 119   restart_with(non_taxable, tax);
 120 
 121   log_info(gc, ergo)("Pacer for Update Refs. Used: " SIZE_FORMAT "%s, Free: " SIZE_FORMAT "%s, "
 122                      "Non-Taxable: " SIZE_FORMAT "%s, Alloc Tax Rate: %.1fx",
 123                      byte_size_in_proper_unit(used),        proper_unit_for_byte_size(used),
 124                      byte_size_in_proper_unit(free),        proper_unit_for_byte_size(free),
 125                      byte_size_in_proper_unit(non_taxable), proper_unit_for_byte_size(non_taxable),
 126                      tax);
 127 }
 128 
 129 /*
 130  * In idle phase, we have to pace the application to let control thread react with GC start.
 131  *
 132  * Here, we have rendezvous with concurrent thread that adds up the budget as it acknowledges
 133  * it had seen recent allocations. It will naturally pace the allocations if control thread is
 134  * not catching up. To bootstrap this feedback cycle, we need to start with some initial budget
 135  * for applications to allocate at.
 136  */
 137 
 138 void ShenandoahPacer::setup_for_idle() {
 139   assert(ShenandoahPacing, "Only be here when pacing is enabled");
 140 
 141   size_t initial = _heap->max_capacity() / 100 * ShenandoahPacingIdleSlack;
 142   double tax = 1;
 143 
 144   restart_with(initial, tax);
 145 
 146   log_info(gc, ergo)("Pacer for Idle. Initial: " SIZE_FORMAT "%s, Alloc Tax Rate: %.1fx",
 147                      byte_size_in_proper_unit(initial), proper_unit_for_byte_size(initial),
 148                      tax);
 149 }
 150 
 151 /*
 152  * There is no useful notion of progress for these operations. To avoid stalling
 153  * the allocators unnecessarily, allow them to run unimpeded.
 154  */
 155 
 156 void ShenandoahPacer::setup_for_preclean() {
 157   assert(ShenandoahPacing, "Only be here when pacing is enabled");
 158 
 159   size_t initial = _heap->max_capacity();
 160   restart_with(initial, 1.0);
 161 
 162   log_info(gc, ergo)("Pacer for Precleaning. Non-Taxable: " SIZE_FORMAT "%s",
 163                      byte_size_in_proper_unit(initial), proper_unit_for_byte_size(initial));
 164 }
 165 
 166 void ShenandoahPacer::setup_for_reset() {
 167   assert(ShenandoahPacing, "Only be here when pacing is enabled");
 168 
 169   size_t initial = _heap->max_capacity();
 170   restart_with(initial, 1.0);
 171 
 172   log_info(gc, ergo)("Pacer for Reset. Non-Taxable: " SIZE_FORMAT "%s",
 173                      byte_size_in_proper_unit(initial), proper_unit_for_byte_size(initial));
 174 }
 175 
 176 size_t ShenandoahPacer::update_and_get_progress_history() {
 177   if (_progress == -1) {
 178     // First initialization, report some prior
 179     Atomic::store((intptr_t)PACING_PROGRESS_ZERO, &_progress);
 180     return (size_t) (_heap->max_capacity() * 0.1);
 181   } else {
 182     // Record history, and reply historical data
 183     _progress_history->add(_progress);
 184     Atomic::store((intptr_t)PACING_PROGRESS_ZERO, &_progress);
 185     return (size_t) (_progress_history->avg() * HeapWordSize);
 186   }
 187 }
 188 
 189 void ShenandoahPacer::restart_with(size_t non_taxable_bytes, double tax_rate) {
 190   size_t initial = (size_t)(non_taxable_bytes * tax_rate) >> LogHeapWordSize;
 191   STATIC_ASSERT(sizeof(size_t) <= sizeof(intptr_t));
 192   Atomic::xchg((intptr_t)initial, &_budget);
 193   Atomic::store(tax_rate, &_tax_rate);
 194   Atomic::inc(&_epoch);
 195 }
 196 
 197 bool ShenandoahPacer::claim_for_alloc(size_t words, bool force) {
 198   assert(ShenandoahPacing, "Only be here when pacing is enabled");
 199 
 200   intptr_t tax = MAX2<intptr_t>(1, words * Atomic::load(&_tax_rate));
 201 
 202   intptr_t cur = 0;
 203   intptr_t new_val = 0;
 204   do {
 205     cur = Atomic::load(&_budget);
 206     if (cur < tax && !force) {
 207       // Progress depleted, alas.
 208       return false;
 209     }
 210     new_val = cur - tax;
 211   } while (Atomic::cmpxchg(new_val, &_budget, cur) != cur);
 212   return true;
 213 }
 214 
 215 void ShenandoahPacer::unpace_for_alloc(intptr_t epoch, size_t words) {
 216   assert(ShenandoahPacing, "Only be here when pacing is enabled");
 217 
 218   if (_epoch != epoch) {
 219     // Stale ticket, no need to unpace.
 220     return;
 221   }
 222 
 223   intptr_t tax = MAX2<intptr_t>(1, words * Atomic::load(&_tax_rate));
 224   Atomic::add(tax, &_budget);
 225 }
 226 
 227 intptr_t ShenandoahPacer::epoch() {
 228   return Atomic::load(&_epoch);
 229 }
 230 
 231 void ShenandoahPacer::pace_for_alloc(size_t words) {
 232   assert(ShenandoahPacing, "Only be here when pacing is enabled");
 233 
 234   // Fast path: try to allocate right away
 235   if (claim_for_alloc(words, false)) {
 236     return;
 237   }
 238 
 239   // Threads that are attaching should not block at all: they are not
 240   // fully initialized yet. Calling sleep() on them would be awkward.
 241   // This is probably the path that allocates the thread oop itself.
 242   // Forcefully claim without waiting.
 243   if (JavaThread::current()->is_attaching_via_jni()) {
 244     claim_for_alloc(words, true);
 245     return;
 246   }
 247 
 248   size_t max = ShenandoahPacingMaxDelay;
 249   double start = os::elapsedTime();
 250 
 251   size_t total = 0;
 252   size_t cur = 0;
 253 
 254   while (true) {
 255     // We could instead assist GC, but this would suffice for now.
 256     // This code should also participate in safepointing.
 257     // Perform the exponential backoff, limited by max.
 258 
 259     cur = cur * 2;
 260     if (total + cur > max) {
 261       cur = (max > total) ? (max - total) : 0;
 262     }
 263     cur = MAX2<size_t>(1, cur);
 264 
 265     os::sleep(Thread::current(), cur, true);
 266 
 267     double end = os::elapsedTime();
 268     total = (size_t)((end - start) * 1000);
 269 
 270     if (total > max) {
 271       // Spent local time budget to wait for enough GC progress.
 272       // Breaking out and allocating anyway, which may mean we outpace GC,
 273       // and start Degenerated GC cycle.
 274       _delays.add(total);
 275 
 276       // Forcefully claim the budget: it may go negative at this point, and
 277       // GC should replenish for this and subsequent allocations
 278       claim_for_alloc(words, true);
 279       break;
 280     }
 281 
 282     if (claim_for_alloc(words, false)) {
 283       // Acquired enough permit, nice. Can allocate now.
 284       _delays.add(total);
 285       break;
 286     }
 287   }
 288 }
 289 
 290 void ShenandoahPacer::print_on(outputStream* out) const {
 291   out->print_cr("ALLOCATION PACING:");
 292   out->cr();
 293 
 294   out->print_cr("Max pacing delay is set for " UINTX_FORMAT " ms.", ShenandoahPacingMaxDelay);
 295   out->cr();
 296 
 297   out->print_cr("Higher delay would prevent application outpacing the GC, but it will hide the GC latencies");
 298   out->print_cr("from the STW pause times. Pacing affects the individual threads, and so it would also be");
 299   out->print_cr("invisible to the usual profiling tools, but would add up to end-to-end application latency.");
 300   out->print_cr("Raise max pacing delay with care.");
 301   out->cr();
 302 
 303   out->print_cr("Actual pacing delays histogram:");
 304   out->cr();
 305 
 306   out->print_cr("%10s - %10s  %12s%12s", "From", "To", "Count", "Sum");
 307 
 308   size_t total_count = 0;
 309   size_t total_sum = 0;
 310   for (int c = _delays.min_level(); c <= _delays.max_level(); c++) {
 311     int l = (c == 0) ? 0 : 1 << (c - 1);
 312     int r = 1 << c;
 313     size_t count = _delays.level(c);
 314     size_t sum   = count * (r - l) / 2;
 315     total_count += count;
 316     total_sum   += sum;
 317 
 318     out->print_cr("%7d ms - %7d ms: " SIZE_FORMAT_W(12) SIZE_FORMAT_W(12) " ms", l, r, count, sum);
 319   }
 320   out->print_cr("%23s: " SIZE_FORMAT_W(12) SIZE_FORMAT_W(12) " ms", "Total", total_count, total_sum);
 321   out->cr();
 322   out->print_cr("Pacing delays are measured from entering the pacing code till exiting it. Therefore,");
 323   out->print_cr("observed pacing delays may be higher than the threshold when paced thread spent more");
 324   out->print_cr("time in the pacing code. It usually happens when thread is de-scheduled while paced,");
 325   out->print_cr("OS takes longer to unblock the thread, or JVM experiences an STW pause.");
 326   out->cr();
 327 }