1 /*
   2  * Copyright (c) 2013, 2015, Red Hat, Inc. and/or its affiliates.
   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 #include "gc/shared/gcPolicyCounters.hpp"
  26 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  27 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  30 #include "gc/shenandoah/shenandoahPartialGC.hpp"
  31 #include "gc/shenandoah/shenandoahPhaseTimes.hpp"
  32 #include "runtime/os.hpp"
  33 
  34 class ShenandoahHeuristics : public CHeapObj<mtGC> {
  35 
  36   NumberSeq _allocation_rate_bytes;
  37   NumberSeq _reclamation_rate_bytes;
  38 
  39   size_t _bytes_allocated_since_CM;
  40   size_t _bytes_reclaimed_this_cycle;
  41 
  42 protected:
  43   typedef struct {
  44     size_t region_number;
  45     size_t garbage;
  46   } RegionGarbage;
  47 
  48   static int compare_by_garbage(RegionGarbage a, RegionGarbage b) {
  49     if (a.garbage > b.garbage)
  50       return -1;
  51     else if (b.garbage < a.garbage)
  52       return 1;
  53     else return 0;
  54   }
  55 
  56   RegionGarbage* get_region_garbage_cache(size_t num) {
  57     RegionGarbage* res = _region_garbage;
  58     if (res == NULL) {
  59       res = NEW_C_HEAP_ARRAY(RegionGarbage, num, mtGC);
  60       _region_garbage_size = num;
  61     } else if (_region_garbage_size < num) {
  62       REALLOC_C_HEAP_ARRAY(RegionGarbage, _region_garbage, num, mtGC);
  63       _region_garbage_size = num;
  64     }
  65     return res;
  66   }
  67 
  68   RegionGarbage* _region_garbage;
  69   size_t _region_garbage_size;
  70 
  71   size_t _bytes_allocated_start_CM;
  72   size_t _bytes_allocated_during_CM;
  73 
  74   size_t _bytes_allocated_after_last_gc;
  75 
  76   uint _cancelled_cm_cycles_in_a_row;
  77   uint _successful_cm_cycles_in_a_row;
  78 
  79   size_t _bytes_in_cset;
  80 
  81 public:
  82 
  83   ShenandoahHeuristics();
  84   ~ShenandoahHeuristics();
  85 
  86   void record_bytes_allocated(size_t bytes);
  87   void record_bytes_reclaimed(size_t bytes);
  88   void record_bytes_start_CM(size_t bytes);
  89   void record_bytes_end_CM(size_t bytes);
  90 
  91   void record_gc_start() {
  92     // Do nothing.
  93   }
  94 
  95   void record_gc_end() {
  96     _bytes_allocated_after_last_gc = ShenandoahHeap::heap()->used();
  97   }
  98 
  99   size_t bytes_in_cset() const { return _bytes_in_cset; }
 100 
 101   virtual void print_thresholds() {
 102   }
 103 
 104   virtual bool should_start_concurrent_mark(size_t used, size_t capacity) const=0;
 105 
 106   virtual bool update_refs_early() {
 107     return ShenandoahUpdateRefsEarly;
 108   }
 109 
 110   virtual bool should_start_partial_gc() {
 111     return false;
 112   }
 113 
 114   virtual bool handover_cancelled_marking() {
 115     return _cancelled_cm_cycles_in_a_row <= ShenandoahFullGCThreshold;
 116   }
 117 
 118   virtual void record_cm_cancelled() {
 119     _cancelled_cm_cycles_in_a_row++;
 120     _successful_cm_cycles_in_a_row = 0;
 121   }
 122 
 123   virtual void record_cm_success() {
 124     _cancelled_cm_cycles_in_a_row = 0;
 125     _successful_cm_cycles_in_a_row++;
 126   }
 127 
 128   virtual void record_full_gc() {
 129     _bytes_in_cset = 0;
 130   }
 131 
 132   virtual void start_choose_collection_set() {
 133   }
 134   virtual void end_choose_collection_set() {
 135   }
 136   virtual bool region_in_collection_set(ShenandoahHeapRegion* r, size_t immediate_garbage) = 0;
 137 
 138   virtual void choose_collection_set(ShenandoahCollectionSet* collection_set, int* connections=NULL);
 139   virtual void choose_free_set(ShenandoahFreeSet* free_set);
 140 
 141   virtual bool process_references() {
 142     if (ShenandoahRefProcFrequency == 0) return false;
 143     size_t cycle = ShenandoahHeap::heap()->shenandoahPolicy()->cycle_counter();
 144     // Process references every Nth GC cycle.
 145     return cycle % ShenandoahRefProcFrequency == 0;
 146   }
 147 
 148   virtual bool unload_classes() {
 149     if (ShenandoahUnloadClassesFrequency == 0) return false;
 150     size_t cycle = ShenandoahHeap::heap()->shenandoahPolicy()->cycle_counter();
 151     // Unload classes every Nth GC cycle.
 152     // This should not happen in the same cycle as process_references to amortize costs.
 153     // Offsetting by one is enough to break the rendezvous when periods are equal.
 154     // When periods are not equal, offsetting by one is just as good as any other guess.
 155     return (cycle + 1) % ShenandoahUnloadClassesFrequency == 0;
 156   }
 157 
 158   virtual bool needs_regions_sorted_by_garbage() {
 159     // Most of them do not.
 160     return false;
 161   }
 162 };
 163 
 164 ShenandoahHeuristics::ShenandoahHeuristics() :
 165   _bytes_allocated_since_CM(0),
 166   _bytes_reclaimed_this_cycle(0),
 167   _bytes_allocated_start_CM(0),
 168   _bytes_allocated_during_CM(0),
 169   _bytes_allocated_after_last_gc(0),
 170   _bytes_in_cset(0),
 171   _cancelled_cm_cycles_in_a_row(0),
 172   _successful_cm_cycles_in_a_row(0),
 173   _region_garbage(NULL),
 174   _region_garbage_size(0)
 175 {
 176 }
 177 
 178 ShenandoahHeuristics::~ShenandoahHeuristics() {
 179   if (_region_garbage != NULL) {
 180     FREE_C_HEAP_ARRAY(RegionGarbage, _region_garbage);
 181   }
 182 }
 183 
 184 void ShenandoahHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set, int* connections) {
 185   start_choose_collection_set();
 186 
 187   ShenandoahHeap* heap = ShenandoahHeap::heap();
 188 
 189   // Poll this before populating collection set.
 190   size_t total_garbage = heap->garbage();
 191 
 192   // Step 1. Build up the region candidates we care about, rejecting losers and accepting winners right away.
 193 
 194   ShenandoahHeapRegionSet* regions = heap->regions();
 195   size_t active = regions->active_regions();
 196 
 197   RegionGarbage* candidates = get_region_garbage_cache(active);
 198 
 199   size_t cand_idx = 0;
 200   _bytes_in_cset = 0;
 201 
 202   size_t immediate_garbage = 0;
 203   size_t immediate_regions = 0;
 204   for (size_t i = 0; i < active; i++) {
 205     ShenandoahHeapRegion* region = regions->get(i);
 206 
 207     if (! region->is_humongous() && ! region->is_pinned()) {
 208       if ((! region->is_empty()) && ! region->has_live()) {
 209         // We can recycle it right away and put it in the free set.
 210         immediate_regions++;
 211         immediate_garbage += region->garbage();
 212         heap->decrease_used(region->used());
 213         region->recycle();
 214         log_develop_trace(gc)("Choose region " SIZE_FORMAT " for immediate reclaim with garbage = " SIZE_FORMAT
 215                               " and live = " SIZE_FORMAT "\n",
 216                               region->region_number(), region->garbage(), region->get_live_data_bytes());
 217       } else {
 218         // This is our candidate for later consideration.
 219         candidates[cand_idx].region_number = region->region_number();
 220         candidates[cand_idx].garbage = region->garbage();
 221         cand_idx++;
 222       }
 223     } else {
 224       assert(region->has_live() || region->is_empty() || region->is_pinned() || region->is_humongous(), "check rejected");
 225       log_develop_trace(gc)("Rejected region " SIZE_FORMAT " with garbage = " SIZE_FORMAT
 226                             " and live = " SIZE_FORMAT "\n",
 227                             region->region_number(), region->garbage(), region->get_live_data_bytes());
 228     }
 229   }
 230 
 231   // Step 2. Process the remanining candidates, if any.
 232 
 233   if (cand_idx > 0) {
 234     if (needs_regions_sorted_by_garbage()) {
 235       QuickSort::sort<RegionGarbage>(candidates, (int)cand_idx, compare_by_garbage, false);
 236     }
 237 
 238     for (size_t i = 0; i < cand_idx; i++) {
 239       ShenandoahHeapRegion *region = regions->get_fast(candidates[i].region_number);
 240       if (region_in_collection_set(region, immediate_garbage)) {
 241         log_develop_trace(gc)("Choose region " SIZE_FORMAT " with garbage = " SIZE_FORMAT
 242                                       " and live = " SIZE_FORMAT "\n",
 243                               region->region_number(), region->garbage(), region->get_live_data_bytes());
 244         collection_set->add_region(region);
 245         region->set_in_collection_set(true);
 246         _bytes_in_cset += region->used();
 247       }
 248     }
 249   }
 250 
 251   end_choose_collection_set();
 252 
 253   log_info(gc, ergo)("Total Garbage: "SIZE_FORMAT"M",
 254                      total_garbage / M);
 255   log_info(gc, ergo)("Immediate Garbage: "SIZE_FORMAT"M, "SIZE_FORMAT" regions",
 256                      immediate_garbage / M, immediate_regions);
 257   log_info(gc, ergo)("Garbage to be collected: "SIZE_FORMAT"M ("SIZE_FORMAT"%% of total), "SIZE_FORMAT" regions",
 258                      collection_set->garbage() / M, collection_set->garbage() * 100 / MAX2(total_garbage, (size_t)1), collection_set->count());
 259   log_info(gc, ergo)("Live objects to be evacuated: "SIZE_FORMAT"M",
 260                      collection_set->live_data() / M);
 261   log_info(gc, ergo)("Live/garbage ratio in collected regions: "SIZE_FORMAT"%%",
 262                      collection_set->live_data() * 100 / MAX2(collection_set->garbage(), (size_t)1));
 263 }
 264 
 265 void ShenandoahHeuristics::choose_free_set(ShenandoahFreeSet* free_set) {
 266 
 267   ShenandoahHeapRegionSet* ordered_regions = ShenandoahHeap::heap()->regions();
 268   size_t i = 0;
 269   size_t end = ordered_regions->active_regions();
 270 
 271   ShenandoahHeap* heap = ShenandoahHeap::heap();
 272   while (i < end) {
 273     ShenandoahHeapRegion* region = ordered_regions->get(i++);
 274     if ((! heap->in_collection_set(region))
 275         && (! region->is_humongous())
 276         && (! region->is_pinned())) {
 277       free_set->add_region(region);
 278     }
 279   }
 280 }
 281 
 282 void ShenandoahCollectorPolicy::record_workers_start(TimingPhase phase) {
 283   for (uint i = 0; i < ShenandoahPhaseTimes::GCParPhasesSentinel; i++) {
 284     _phase_times->reset(i);
 285   }
 286 }
 287 
 288 void ShenandoahCollectorPolicy::record_workers_end(TimingPhase phase) {
 289   if (phase != _num_phases) {
 290     for (uint i = 0; i < ShenandoahPhaseTimes::GCParPhasesSentinel; i++) {
 291       double t = _phase_times->average(i);
 292       _timing_data[phase + i]._secs.add(t);
 293     }
 294   }
 295 }
 296 
 297 void ShenandoahCollectorPolicy::record_phase_start(TimingPhase phase) {
 298   _timing_data[phase]._start = os::elapsedTime();
 299 
 300 }
 301 
 302 void ShenandoahCollectorPolicy::record_phase_end(TimingPhase phase) {
 303   double end = os::elapsedTime();
 304   double elapsed = end - _timing_data[phase]._start;
 305   _timing_data[phase]._secs.add(elapsed);
 306 }
 307 
 308 void ShenandoahCollectorPolicy::record_gc_start() {
 309   _heuristics->record_gc_start();
 310 }
 311 
 312 void ShenandoahCollectorPolicy::record_gc_end() {
 313   _heuristics->record_gc_end();
 314 }
 315 
 316 void ShenandoahCollectorPolicy::report_concgc_cancelled() {
 317 }
 318 
 319 void ShenandoahHeuristics::record_bytes_allocated(size_t bytes) {
 320   _bytes_allocated_since_CM = bytes;
 321   _bytes_allocated_start_CM = bytes;
 322   _allocation_rate_bytes.add(bytes);
 323 }
 324 
 325 void ShenandoahHeuristics::record_bytes_reclaimed(size_t bytes) {
 326   _bytes_reclaimed_this_cycle = bytes;
 327   _reclamation_rate_bytes.add(bytes);
 328 }
 329 
 330 void ShenandoahHeuristics::record_bytes_start_CM(size_t bytes) {
 331   _bytes_allocated_start_CM = bytes;
 332 }
 333 
 334 void ShenandoahHeuristics::record_bytes_end_CM(size_t bytes) {
 335   _bytes_allocated_during_CM = (bytes > _bytes_allocated_start_CM) ? (bytes - _bytes_allocated_start_CM)
 336                                                                    : bytes;
 337 }
 338 
 339 class PassiveHeuristics : public ShenandoahHeuristics {
 340 public:
 341   PassiveHeuristics() : ShenandoahHeuristics() {
 342   }
 343 
 344   virtual bool region_in_collection_set(ShenandoahHeapRegion* r, size_t immediate_garbage) {
 345     return r->garbage() > 0;
 346   }
 347 
 348   virtual bool should_start_concurrent_mark(size_t used, size_t capacity) const {
 349     // Never do concurrent GCs.
 350     return false;
 351   }
 352 
 353   virtual bool process_references() {
 354     // Randomly process refs with 50% chance.
 355     return (os::random() & 1) == 1;
 356   }
 357 
 358   virtual bool unload_classes() {
 359     // Randomly unload classes with 50% chance.
 360     return (os::random() & 1) == 1;
 361   }
 362 };
 363 
 364 class AggressiveHeuristics : public ShenandoahHeuristics {
 365 public:
 366   AggressiveHeuristics() : ShenandoahHeuristics() {
 367   }
 368 
 369   virtual bool region_in_collection_set(ShenandoahHeapRegion* r, size_t immediate_garbage) {
 370     return r->garbage() > 0;
 371   }
 372 
 373   virtual bool should_start_concurrent_mark(size_t used, size_t capacity) const {
 374     return true;
 375   }
 376 
 377   virtual bool process_references() {
 378     // Randomly process refs with 50% chance.
 379     return (os::random() & 1) == 1;
 380   }
 381 
 382   virtual bool unload_classes() {
 383     // Randomly unload classes with 50% chance.
 384     return (os::random() & 1) == 1;
 385   }
 386 };
 387 
 388 class DynamicHeuristics : public ShenandoahHeuristics {
 389 public:
 390   DynamicHeuristics() : ShenandoahHeuristics() {
 391   }
 392 
 393   void print_thresholds() {
 394     log_info(gc, init)("Shenandoah heuristics thresholds: allocation "SIZE_FORMAT", free "SIZE_FORMAT", garbage "SIZE_FORMAT,
 395                        ShenandoahAllocationThreshold,
 396                        ShenandoahFreeThreshold,
 397                        ShenandoahGarbageThreshold);
 398   }
 399 
 400   virtual ~DynamicHeuristics() {}
 401 
 402   virtual bool should_start_concurrent_mark(size_t used, size_t capacity) const {
 403 
 404     bool shouldStartConcurrentMark = false;
 405 
 406     ShenandoahHeap* heap = ShenandoahHeap::heap();
 407     size_t free_capacity = heap->free_regions()->capacity();
 408     size_t free_used = heap->free_regions()->used();
 409     assert(free_used <= free_capacity, "must use less than capacity");
 410     size_t cset = MIN2(_bytes_in_cset, (ShenandoahCSetThreshold * capacity) / 100);
 411     size_t available =  free_capacity - free_used + cset;
 412     uintx threshold = ShenandoahFreeThreshold + ShenandoahCSetThreshold;
 413     size_t targetStartMarking = (capacity * threshold) / 100;
 414 
 415     size_t threshold_bytes_allocated = heap->capacity() * ShenandoahAllocationThreshold / 100;
 416     if (available < targetStartMarking &&
 417         heap->bytes_allocated_since_cm() > threshold_bytes_allocated)
 418     {
 419       // Need to check that an appropriate number of regions have
 420       // been allocated since last concurrent mark too.
 421       shouldStartConcurrentMark = true;
 422     }
 423 
 424     return shouldStartConcurrentMark;
 425   }
 426 
 427   virtual bool region_in_collection_set(ShenandoahHeapRegion* r, size_t immediate_garbage) {
 428     size_t threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100;
 429     return r->garbage() > threshold;
 430   }
 431 
 432 };
 433 
 434 
 435 class AdaptiveHeuristics : public ShenandoahHeuristics {
 436 private:
 437   uintx _free_threshold;
 438   TruncatedSeq* _cset_history;
 439 
 440 public:
 441   AdaptiveHeuristics() :
 442     ShenandoahHeuristics(),
 443     _free_threshold(ShenandoahInitFreeThreshold),
 444     _cset_history(new TruncatedSeq((uint)ShenandoahHappyCyclesThreshold)) {
 445 
 446     _cset_history->add((double) ShenandoahCSetThreshold);
 447     _cset_history->add((double) ShenandoahCSetThreshold);
 448   }
 449 
 450   virtual ~AdaptiveHeuristics() {
 451     delete _cset_history;
 452   }
 453 
 454   virtual bool region_in_collection_set(ShenandoahHeapRegion* r, size_t immediate_garbage) {
 455     size_t threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100;
 456     return r->garbage() > threshold;
 457   }
 458 
 459   virtual void record_cm_cancelled() {
 460     ShenandoahHeuristics::record_cm_cancelled();
 461     if (_free_threshold < ShenandoahMaxFreeThreshold) {
 462       _free_threshold++;
 463       log_info(gc,ergo)("increasing free threshold to: "UINTX_FORMAT, _free_threshold);
 464     }
 465   }
 466 
 467   virtual void record_cm_success() {
 468     ShenandoahHeuristics::record_cm_success();
 469     if (_successful_cm_cycles_in_a_row > ShenandoahHappyCyclesThreshold &&
 470         _free_threshold > ShenandoahMinFreeThreshold) {
 471       _free_threshold--;
 472       log_info(gc,ergo)("reducing free threshold to: "UINTX_FORMAT, _free_threshold);
 473       _successful_cm_cycles_in_a_row = 0;
 474     }
 475   }
 476 
 477   virtual bool should_start_concurrent_mark(size_t used, size_t capacity) const {
 478     bool shouldStartConcurrentMark = false;
 479 
 480     ShenandoahHeap* heap = ShenandoahHeap::heap();
 481     size_t free_capacity = heap->free_regions()->capacity();
 482     size_t free_used = heap->free_regions()->used();
 483     assert(free_used <= free_capacity, "must use less than capacity");
 484     // size_t cset_threshold = (size_t) _cset_history->maximum();
 485     size_t cset_threshold = (size_t) _cset_history->davg();
 486     size_t cset = MIN2(_bytes_in_cset, (cset_threshold * capacity) / 100);
 487     size_t available =  free_capacity - free_used + cset;
 488     uintx factor = _free_threshold + cset_threshold;
 489     size_t targetStartMarking = (capacity * factor) / 100;
 490 
 491     size_t threshold_bytes_allocated = heap->capacity() * ShenandoahAllocationThreshold / 100;
 492     if (available < targetStartMarking &&
 493         heap->bytes_allocated_since_cm() > threshold_bytes_allocated)
 494     {
 495       // Need to check that an appropriate number of regions have
 496       // been allocated since last concurrent mark too.
 497       shouldStartConcurrentMark = true;
 498     }
 499 
 500     if (shouldStartConcurrentMark) {
 501       log_info(gc,ergo)("predicted cset threshold: "SIZE_FORMAT, cset_threshold);
 502       log_info(gc,ergo)("Starting concurrent mark at "SIZE_FORMAT"K CSet ("SIZE_FORMAT"%%)", _bytes_in_cset / K, _bytes_in_cset * 100 / capacity);
 503       _cset_history->add((double) (_bytes_in_cset * 100 / capacity));
 504     }
 505     return shouldStartConcurrentMark;
 506   }
 507 
 508 };
 509 
 510 class GlobalHeuristics : public DynamicHeuristics {
 511 private:
 512   size_t _garbage;
 513   size_t _min_garbage;
 514 public:
 515   GlobalHeuristics() : DynamicHeuristics() {
 516     if (FLAG_IS_DEFAULT(ShenandoahGarbageThreshold)) {
 517       FLAG_SET_DEFAULT(ShenandoahGarbageThreshold, 90);
 518     }
 519   }
 520   virtual ~GlobalHeuristics() {}
 521 
 522   virtual void start_choose_collection_set() {
 523     _garbage = 0;
 524     size_t heap_garbage = ShenandoahHeap::heap()->garbage();
 525     _min_garbage =  heap_garbage * ShenandoahGarbageThreshold / 100;
 526   }
 527 
 528   virtual bool region_in_collection_set(ShenandoahHeapRegion* r, size_t immediate_garbage) {
 529     if (_garbage + immediate_garbage < _min_garbage && ! r->is_empty()) {
 530       _garbage += r->garbage();
 531       return true;
 532     } else {
 533       return false;
 534     }
 535   }
 536 
 537   virtual bool needs_regions_sorted_by_garbage() {
 538     return true;
 539   }
 540 };
 541 
 542 class RatioHeuristics : public DynamicHeuristics {
 543 private:
 544   size_t _garbage;
 545   size_t _live;
 546 public:
 547   RatioHeuristics() : DynamicHeuristics() {
 548     if (FLAG_IS_DEFAULT(ShenandoahGarbageThreshold)) {
 549       FLAG_SET_DEFAULT(ShenandoahGarbageThreshold, 95);
 550     }
 551   }
 552   virtual ~RatioHeuristics() {}
 553 
 554   virtual void start_choose_collection_set() {
 555     _garbage = 0;
 556     _live = 0;
 557   }
 558 
 559   virtual bool region_in_collection_set(ShenandoahHeapRegion* r, size_t immediate_garbage) {
 560     size_t min_ratio = 100 - ShenandoahGarbageThreshold;
 561     if (_live * 100 / MAX2(_garbage + immediate_garbage, (size_t)1) < min_ratio && ! r->is_empty()) {
 562       _garbage += r->garbage();
 563       _live += r->get_live_data_bytes();
 564       return true;
 565     } else {
 566       return false;
 567     }
 568   }
 569 
 570   virtual bool needs_regions_sorted_by_garbage() {
 571     return true;
 572   }
 573 };
 574 
 575 class ConnectionHeuristics : public ShenandoahHeuristics {
 576 private:
 577   size_t _max_live_data;
 578   double _used_threshold_factor;
 579   double _garbage_threshold_factor;
 580   double _allocation_threshold_factor;
 581 
 582   uintx _used_threshold;
 583   uintx _garbage_threshold;
 584   uintx _allocation_threshold;
 585 
 586 public:
 587   ConnectionHeuristics() : ShenandoahHeuristics() {
 588     _max_live_data = 0;
 589 
 590     _used_threshold = 0;
 591     _garbage_threshold = 0;
 592     _allocation_threshold = 0;
 593 
 594     _used_threshold_factor = 0.;
 595     _garbage_threshold_factor = 0.1;
 596     _allocation_threshold_factor = 0.;
 597   }
 598 
 599   virtual bool should_start_concurrent_mark(size_t used, size_t capacity) const {
 600     size_t half_gig = 64 * 1024 * 1024;
 601     size_t bytes_alloc = ShenandoahHeap::heap()->bytes_allocated_since_cm();
 602     bool result =  bytes_alloc > half_gig;
 603     if (result) tty->print("Starting a concurrent mark");
 604     return result;
 605   }
 606 
 607   bool maybe_add_heap_region(ShenandoahHeapRegion* hr, ShenandoahCollectionSet* collection_set) {
 608     if (!hr->is_humongous() && hr->has_live() && !collection_set->contains(hr)) {
 609       collection_set->add_region_check_for_duplicates(hr);
 610       hr->set_in_collection_set(true);
 611       return true;
 612     }
 613     return false;
 614   }
 615 
 616   virtual void choose_collection_set(ShenandoahCollectionSet* collection_set, int* connections) {
 617     ShenandoahHeapRegionSet* regions = ShenandoahHeap::heap()->regions();
 618     size_t active = regions->active_regions();
 619 
 620     RegionGarbage* sorted_by_garbage = get_region_garbage_cache(active);
 621     for (size_t i = 0; i < active; i++) {
 622       ShenandoahHeapRegion* r = regions->get_fast(i);
 623       sorted_by_garbage[i].region_number = r->region_number();
 624       sorted_by_garbage[i].garbage = r->garbage();
 625     }
 626 
 627     QuickSort::sort<RegionGarbage>(sorted_by_garbage, (int) active, compare_by_garbage, false);
 628 
 629     size_t num = ShenandoahHeap::heap()->num_regions();
 630     // simulate write heuristics by picking best region.
 631     int r = 0;
 632     ShenandoahHeapRegion* choosenOne = regions->get(sorted_by_garbage[0].region_number);
 633 
 634     while (! maybe_add_heap_region(choosenOne, collection_set)) {
 635       choosenOne = regions->get(sorted_by_garbage[++r].region_number);
 636     }
 637 
 638     size_t region_number = choosenOne->region_number();
 639     log_develop_trace(gc)("Adding choosen region " SIZE_FORMAT, region_number);
 640 
 641     // Add all the regions which point to this region.
 642     for (size_t i = 0; i < num; i++) {
 643       if (connections[i * num + region_number] > 0) {
 644         ShenandoahHeapRegion* candidate = regions->get(sorted_by_garbage[i].region_number);
 645         if (maybe_add_heap_region(candidate, collection_set)) {
 646           log_develop_trace(gc)("Adding region " SIZE_FORMAT " which points to the choosen region", i);
 647         }
 648       }
 649     }
 650 
 651     // Add all the regions they point to.
 652     for (size_t ci = 0; ci < collection_set->active_regions(); ci++) {
 653       ShenandoahHeapRegion* cs_heap_region = collection_set->get(ci);
 654       size_t cs_heap_region_number = cs_heap_region->region_number();
 655       for (size_t i = 0; i < num; i++) {
 656         if (connections[i * num + cs_heap_region_number] > 0) {
 657           ShenandoahHeapRegion* candidate = regions->get(sorted_by_garbage[i].region_number);
 658           if (maybe_add_heap_region(candidate, collection_set)) {
 659             log_develop_trace(gc)
 660               ("Adding region " SIZE_FORMAT " which is pointed to by region " SIZE_FORMAT, i, cs_heap_region_number);
 661           }
 662         }
 663       }
 664     }
 665     _max_live_data = MAX2(_max_live_data, collection_set->live_data());
 666     collection_set->print();
 667   }
 668 
 669   virtual bool region_in_collection_set(ShenandoahHeapRegion* r, size_t immediate_garbage) {
 670     assert(false, "Shouldn't get here");
 671     return false;
 672   }
 673 };
 674 class PartialHeuristics : public AdaptiveHeuristics {
 675 public:
 676   PartialHeuristics() : AdaptiveHeuristics() {
 677     if (FLAG_IS_DEFAULT(ShenandoahAllocationThreshold)) {
 678       FLAG_SET_DEFAULT(ShenandoahAllocationThreshold, 5);
 679     }
 680     FLAG_SET_DEFAULT(UseShenandoahMatrix, true);
 681     // TODO: Disable this optimization for now, as it also requires the matrix barriers.
 682     FLAG_SET_DEFAULT(ArrayCopyLoadStoreMaxElem, 0);
 683   }
 684 
 685   virtual ~PartialHeuristics() {}
 686 
 687   bool should_start_concurrent_mark(size_t used, size_t capacity) const {
 688     // Never do concurrent GCs.
 689     return false;
 690   }
 691 
 692   bool should_start_partial_gc() {
 693     ShenandoahHeap* heap = ShenandoahHeap::heap();
 694     size_t capacity = heap->capacity();
 695 
 696     size_t used = heap->used();
 697     return (used - _bytes_allocated_after_last_gc) * 100 / capacity > ShenandoahAllocationThreshold;
 698   }
 699 };
 700 
 701 ShenandoahCollectorPolicy::ShenandoahCollectorPolicy() :
 702   _cycle_counter(0),
 703   _successful_cm(0),
 704   _degenerated_cm(0)
 705 {
 706 
 707   ShenandoahHeapRegion::setup_heap_region_size(initial_heap_byte_size(), max_heap_byte_size());
 708 
 709   initialize_all();
 710 
 711   _tracer = new (ResourceObj::C_HEAP, mtGC) ShenandoahTracer();
 712   _stw_timer = new (ResourceObj::C_HEAP, mtGC) STWGCTimer();
 713   _conc_timer = new (ResourceObj::C_HEAP, mtGC) ConcurrentGCTimer();
 714   _user_requested_gcs = 0;
 715   _allocation_failure_gcs = 0;
 716   _conc_gc_aborted = false;
 717 
 718   _phase_names[total_pause]                     = "Total Pauses (net)";
 719   _phase_names[total_pause_gross]               = "Total Pauses (gross)";
 720   _phase_names[init_mark]                       = "Initial Mark Pauses (net)";
 721   _phase_names[init_mark_gross]                 = "Initial Mark Pauses (gross)";
 722   _phase_names[final_mark]                      = "Final Mark Pauses (net)";
 723   _phase_names[final_mark_gross]                = "Final Mark Pauses (gross)";
 724   _phase_names[accumulate_stats]                = "  Accumulate Stats";
 725   _phase_names[make_parsable]                   = "  Make Parsable";
 726   _phase_names[clear_liveness]                  = "  Clear Liveness";
 727   _phase_names[scan_roots]                      = "  Scan Roots";
 728   _phase_names[update_roots]                    = "  Update Roots";
 729   _phase_names[drain_satb]                      = "  Drain SATB";
 730   _phase_names[weakrefs]                        = "  Weak References";
 731   _phase_names[class_unloading]                 = "  Class Unloading";
 732   _phase_names[prepare_evac]                    = "  Prepare Evacuation";
 733   _phase_names[init_evac]                       = "  Initial Evacuation";
 734 
 735   _phase_names[scan_thread_roots]               = "    S: Thread Roots";
 736   _phase_names[scan_code_roots]                 = "    S: Code Cache Roots";
 737   _phase_names[scan_string_table_roots]         = "    S: String Table Roots";
 738   _phase_names[scan_universe_roots]             = "    S: Universe Roots";
 739   _phase_names[scan_jni_roots]                  = "    S: JNI Roots";
 740   _phase_names[scan_jni_weak_roots]             = "    S: JNI Weak Roots";
 741   _phase_names[scan_synchronizer_roots]         = "    S: Synchronizer Roots";
 742   _phase_names[scan_flat_profiler_roots]        = "    S: Flat Profiler Roots";
 743   _phase_names[scan_management_roots]           = "    S: Management Roots";
 744   _phase_names[scan_system_dictionary_roots]    = "    S: System Dict Roots";
 745   _phase_names[scan_cldg_roots]                 = "    S: CLDG Roots";
 746   _phase_names[scan_jvmti_roots]                = "    S: JVMTI Roots";
 747 
 748   _phase_names[update_thread_roots]             = "    U: Thread Roots";
 749   _phase_names[update_code_roots]               = "    U: Code Cache Roots";
 750   _phase_names[update_string_table_roots]       = "    U: String Table Roots";
 751   _phase_names[update_universe_roots]           = "    U: Universe Roots";
 752   _phase_names[update_jni_roots]                = "    U: JNI Roots";
 753   _phase_names[update_jni_weak_roots]           = "    U: JNI Weak Roots";
 754   _phase_names[update_synchronizer_roots]       = "    U: Synchronizer Roots";
 755   _phase_names[update_flat_profiler_roots]      = "    U: Flat Profiler Roots";
 756   _phase_names[update_management_roots]         = "    U: Management Roots";
 757   _phase_names[update_system_dictionary_roots]  = "    U: System Dict Roots";
 758   _phase_names[update_cldg_roots]               = "    U: CLDG Roots";
 759   _phase_names[update_jvmti_roots]              = "    U: JVMTI Roots";
 760 
 761   _phase_names[evac_thread_roots]               = "    E: Thread Roots";
 762   _phase_names[evac_code_roots]                 = "    E: Code Cache Roots";
 763   _phase_names[evac_string_table_roots]         = "    E: String Table Roots";
 764   _phase_names[evac_universe_roots]             = "    E: Universe Roots";
 765   _phase_names[evac_jni_roots]                  = "    E: JNI Roots";
 766   _phase_names[evac_jni_weak_roots]             = "    E: JNI Weak Roots";
 767   _phase_names[evac_synchronizer_roots]         = "    E: Synchronizer Roots";
 768   _phase_names[evac_flat_profiler_roots]        = "    E: Flat Profiler Roots";
 769   _phase_names[evac_management_roots]           = "    E: Management Roots";
 770   _phase_names[evac_system_dictionary_roots]    = "    E: System Dict Roots";
 771   _phase_names[evac_cldg_roots]                 = "    E: CLDG Roots";
 772   _phase_names[evac_jvmti_roots]                = "    E: JVMTI Roots";
 773 
 774   _phase_names[recycle_regions]                 = "  Recycle regions";
 775   _phase_names[reset_bitmaps]                   = "Reset Bitmaps";
 776   _phase_names[resize_tlabs]                    = "Resize TLABs";
 777 
 778   _phase_names[full_gc]                         = "Full GC";
 779   _phase_names[full_gc_heapdumps]               = "  Heap Dumps";
 780   _phase_names[full_gc_prepare]                 = "  Prepare";
 781   _phase_names[full_gc_mark]                    = "  Mark";
 782   _phase_names[full_gc_mark_drain_queues]       = "    Drain Queues";
 783   _phase_names[full_gc_mark_weakrefs]           = "    Weak References";
 784   _phase_names[full_gc_mark_class_unloading]    = "    Class Unloading";
 785   _phase_names[full_gc_calculate_addresses]     = "  Calculate Addresses";
 786   _phase_names[full_gc_adjust_pointers]         = "  Adjust Pointers";
 787   _phase_names[full_gc_copy_objects]            = "  Copy Objects";
 788 
 789   _phase_names[partial_gc]                      = "Partial GC";
 790   _phase_names[conc_mark]                       = "Concurrent Marking";
 791   _phase_names[conc_evac]                       = "Concurrent Evacuation";
 792 
 793   _phase_names[conc_update_refs]                = "Concurrent Update References";
 794   _phase_names[pre_update_refs]                 = "Pause Pre  Update References";
 795   _phase_names[post_update_refs]                = "Pause Post Update References";
 796 
 797   if (ShenandoahGCHeuristics != NULL) {
 798     if (strcmp(ShenandoahGCHeuristics, "aggressive") == 0) {
 799       log_info(gc, init)("Shenandoah heuristics: aggressive");
 800       _heuristics = new AggressiveHeuristics();
 801     } else if (strcmp(ShenandoahGCHeuristics, "dynamic") == 0) {
 802       log_info(gc, init)("Shenandoah heuristics: dynamic");
 803       _heuristics = new DynamicHeuristics();
 804     } else if (strcmp(ShenandoahGCHeuristics, "global") == 0) {
 805       log_info(gc, init)("Shenandoah heuristics: global");
 806       _heuristics = new GlobalHeuristics();
 807     } else if (strcmp(ShenandoahGCHeuristics, "ratio") == 0) {
 808       log_info(gc, init)("Shenandoah heuristics: ratio");
 809       _heuristics = new RatioHeuristics();
 810     } else if (strcmp(ShenandoahGCHeuristics, "adaptive") == 0) {
 811       log_info(gc, init)("Shenandoah heuristics: adaptive");
 812       _heuristics = new AdaptiveHeuristics();
 813     } else if (strcmp(ShenandoahGCHeuristics, "passive") == 0) {
 814       log_info(gc, init)("Shenandoah heuristics: passive");
 815       _heuristics = new PassiveHeuristics();
 816     } else if (strcmp(ShenandoahGCHeuristics, "connections") == 0) {
 817       log_info(gc, init)("Shenandoah heuristics: connections");
 818       _heuristics = new ConnectionHeuristics();
 819     } else if (strcmp(ShenandoahGCHeuristics, "partial") == 0) {
 820       log_info(gc, init)("Shenandoah heuristics: partial GC");
 821       _heuristics = new PartialHeuristics();
 822     } else {
 823       vm_exit_during_initialization("Unknown -XX:ShenandoahGCHeuristics option");
 824     }
 825     _heuristics->print_thresholds();
 826   } else {
 827       ShouldNotReachHere();
 828   }
 829   _phase_times = new ShenandoahPhaseTimes(MAX2(ConcGCThreads, ParallelGCThreads));
 830 }
 831 
 832 ShenandoahCollectorPolicy* ShenandoahCollectorPolicy::as_pgc_policy() {
 833   return this;
 834 }
 835 
 836 BarrierSet::Name ShenandoahCollectorPolicy::barrier_set_name() {
 837   return BarrierSet::ShenandoahBarrierSet;
 838 }
 839 
 840 HeapWord* ShenandoahCollectorPolicy::mem_allocate_work(size_t size,
 841                                                        bool is_tlab,
 842                                                        bool* gc_overhead_limit_was_exceeded) {
 843   guarantee(false, "Not using this policy feature yet.");
 844   return NULL;
 845 }
 846 
 847 HeapWord* ShenandoahCollectorPolicy::satisfy_failed_allocation(size_t size, bool is_tlab) {
 848   guarantee(false, "Not using this policy feature yet.");
 849   return NULL;
 850 }
 851 
 852 void ShenandoahCollectorPolicy::initialize_alignments() {
 853 
 854   // This is expected by our algorithm for ShenandoahHeap::heap_region_containing().
 855   _space_alignment = ShenandoahHeapRegion::region_size_bytes();
 856   _heap_alignment = ShenandoahHeapRegion::region_size_bytes();
 857 }
 858 
 859 void ShenandoahCollectorPolicy::post_heap_initialize() {
 860   // Nothing to do here (yet).
 861 }
 862 
 863 void ShenandoahCollectorPolicy::record_bytes_allocated(size_t bytes) {
 864   _heuristics->record_bytes_allocated(bytes);
 865 }
 866 
 867 void ShenandoahCollectorPolicy::record_bytes_start_CM(size_t bytes) {
 868   _heuristics->record_bytes_start_CM(bytes);
 869 }
 870 
 871 void ShenandoahCollectorPolicy::record_bytes_end_CM(size_t bytes) {
 872   _heuristics->record_bytes_end_CM(bytes);
 873 }
 874 
 875 void ShenandoahCollectorPolicy::record_bytes_reclaimed(size_t bytes) {
 876   _heuristics->record_bytes_reclaimed(bytes);
 877 }
 878 
 879 void ShenandoahCollectorPolicy::record_user_requested_gc() {
 880   _user_requested_gcs++;
 881 }
 882 
 883 void ShenandoahCollectorPolicy::record_allocation_failure_gc() {
 884   _allocation_failure_gcs++;
 885 }
 886 
 887 bool ShenandoahCollectorPolicy::should_start_concurrent_mark(size_t used,
 888                                                              size_t capacity) {
 889   return _heuristics->should_start_concurrent_mark(used, capacity);
 890 }
 891 
 892 bool ShenandoahCollectorPolicy::handover_cancelled_marking() {
 893   return _heuristics->handover_cancelled_marking();
 894 }
 895 
 896 bool ShenandoahCollectorPolicy::update_refs_early() {
 897   return _heuristics->update_refs_early();
 898 }
 899 
 900 void ShenandoahCollectorPolicy::record_cm_success() {
 901   _heuristics->record_cm_success();
 902   _successful_cm++;
 903 }
 904 
 905 void ShenandoahCollectorPolicy::record_cm_degenerated() {
 906   _degenerated_cm++;
 907 }
 908 
 909 void ShenandoahCollectorPolicy::record_cm_cancelled() {
 910   _heuristics->record_cm_cancelled();
 911 }
 912 
 913 void ShenandoahCollectorPolicy::record_full_gc() {
 914   _heuristics->record_full_gc();
 915 }
 916 
 917 void ShenandoahCollectorPolicy::choose_collection_set(ShenandoahCollectionSet* collection_set, int* connections) {
 918   _heuristics->choose_collection_set(collection_set, connections);
 919 }
 920 
 921 void ShenandoahCollectorPolicy::choose_free_set(ShenandoahFreeSet* free_set) {
 922    _heuristics->choose_free_set(free_set);
 923 }
 924 
 925 
 926 bool ShenandoahCollectorPolicy::process_references() {
 927   return _heuristics->process_references();
 928 }
 929 
 930 bool ShenandoahCollectorPolicy::unload_classes() {
 931   return _heuristics->unload_classes();
 932 }
 933 
 934 void ShenandoahCollectorPolicy::print_tracing_info(outputStream* out) {
 935   out->cr();
 936   out->print_cr("GC STATISTICS:");
 937   out->print_cr("  \"gross\" pauses include time to safepoint. \"net\" pauses are times spent in GC.");
 938   out->print_cr("  \"a\" is average time for each phase, look at levels to see if average makes sense.");
 939   out->print_cr("  \"lvls\" are quantiles: 0%% (minimum), 25%%, 50%% (median), 75%%, 100%% (maximum).");
 940   out->cr();
 941 
 942   for (uint i = 0; i < _num_phases; i++) {
 943     if (_timing_data[i]._secs.maximum() != 0) {
 944       print_summary_sd(out, _phase_names[i], &(_timing_data[i]._secs));
 945     }
 946   }
 947 
 948   out->cr();
 949   out->print_cr("" SIZE_FORMAT " allocation failure and " SIZE_FORMAT " user requested GCs", _allocation_failure_gcs, _user_requested_gcs);
 950   out->print_cr("" SIZE_FORMAT " successful and " SIZE_FORMAT " degenerated concurrent markings", _successful_cm, _degenerated_cm);
 951   out->cr();
 952 }
 953 
 954 void ShenandoahCollectorPolicy::print_summary_sd(outputStream* out, const char* str, const HdrSeq* seq)  {
 955   out->print_cr("%-27s = %8.2lf s (a = %8.0lf us) (n = "INT32_FORMAT_W(5)") (lvls, us = %8.0lf, %8.0lf, %8.0lf, %8.0lf, %8.0lf)",
 956           str,
 957           seq->sum(),
 958           seq->avg() * 1000000.0,
 959           seq->num(),
 960           seq->percentile(0)  * 1000000.0,
 961           seq->percentile(25) * 1000000.0,
 962           seq->percentile(50) * 1000000.0,
 963           seq->percentile(75) * 1000000.0,
 964           seq->maximum() * 1000000.0
 965   );
 966 }
 967 
 968 void ShenandoahCollectorPolicy::increase_cycle_counter() {
 969   _cycle_counter++;
 970 }
 971 
 972 size_t ShenandoahCollectorPolicy::cycle_counter() const {
 973   return _cycle_counter;
 974 }
 975 
 976  ShenandoahPhaseTimes* ShenandoahCollectorPolicy::phase_times() {
 977   return _phase_times;
 978 }
 979 
 980 
 981 uint ShenandoahCollectorPolicy::calc_workers_for_java_threads(uint application_workers) {
 982   return (uint)(ShenandoahGCWorkerPerJavaThread * application_workers);
 983 }
 984 
 985 uint ShenandoahCollectorPolicy::calc_workers_for_live_set(size_t live_data) {
 986   return (uint)(live_data / HeapSizePerGCThread);
 987 }
 988 
 989 
 990 uint ShenandoahCollectorPolicy::calc_default_active_workers(
 991                                                      uint total_workers,
 992                                                      uint min_workers,
 993                                                      uint active_workers,
 994                                                      uint application_workers,
 995                                                      uint  workers_by_java_threads,
 996                                                      uint  workers_by_liveset) {
 997   // If the user has turned off using a dynamic number of GC threads
 998   // or the users has requested a specific number, set the active
 999   // number of workers to all the workers.
1000   uint new_active_workers = total_workers;
1001   uint prev_active_workers = active_workers;
1002   uint active_workers_by_JT = 0;
1003   uint active_workers_by_liveset = 0;
1004 
1005   active_workers_by_JT = MAX2(workers_by_java_threads, min_workers);
1006 
1007   // Choose a number of GC threads based on the live set.
1008   active_workers_by_liveset =
1009       MAX2((uint) 2U, workers_by_liveset);
1010 
1011   uint max_active_workers =
1012     MAX2(active_workers_by_JT, active_workers_by_liveset);
1013 
1014   new_active_workers = MIN2(max_active_workers, total_workers);
1015 
1016   // Increase GC workers instantly but decrease them more
1017   // slowly.
1018   if (new_active_workers < prev_active_workers) {
1019     new_active_workers =
1020       MAX2(min_workers, (prev_active_workers + new_active_workers) / 2);
1021   }
1022 
1023   if (UseNUMA) {
1024     uint numa_groups = (uint)os::numa_get_groups_num();
1025     assert(numa_groups <= total_workers, "Not enough workers to cover all numa groups");
1026     new_active_workers = MAX2(new_active_workers, numa_groups);
1027   }
1028 
1029   // Check once more that the number of workers is within the limits.
1030   assert(min_workers <= total_workers, "Minimum workers not consistent with total workers");
1031   assert(new_active_workers >= min_workers, "Minimum workers not observed");
1032   assert(new_active_workers <= total_workers, "Total workers not observed");
1033 
1034   log_trace(gc, task)("ShenandoahCollectorPolicy::calc_default_active_workers() : "
1035      "active_workers(): " UINTX_FORMAT "  new_active_workers: " UINTX_FORMAT "  "
1036      "prev_active_workers: " UINTX_FORMAT "\n"
1037      " active_workers_by_JT: " UINTX_FORMAT "  active_workers_by_liveset: " UINTX_FORMAT,
1038      (uintx)active_workers, (uintx)new_active_workers, (uintx)prev_active_workers,
1039      (uintx)active_workers_by_JT, (uintx)active_workers_by_liveset);
1040   assert(new_active_workers > 0, "Always need at least 1");
1041   return new_active_workers;
1042 }
1043 
1044 /**
1045  * Initial marking phase also update references of live objects from previous concurrent GC cycle,
1046  * so we take Java threads and live set into account.
1047  */
1048 uint ShenandoahCollectorPolicy::calc_workers_for_init_marking(uint active_workers,
1049                                             uint application_workers) {
1050 
1051   if (!UseDynamicNumberOfGCThreads ||
1052      (!FLAG_IS_DEFAULT(ParallelGCThreads) && !ForceDynamicNumberOfGCThreads)) {
1053     assert(ParallelGCThreads > 0, "Always need at least 1");
1054     return ParallelGCThreads;
1055   } else {
1056     ShenandoahCollectorPolicy* policy = (ShenandoahCollectorPolicy*)ShenandoahHeap::heap()->collector_policy();
1057     size_t live_data = policy->_heuristics->bytes_in_cset();
1058 
1059     return calc_default_active_workers(ParallelGCThreads, (ParallelGCThreads > 1) ? 2 : 1,
1060       active_workers, application_workers,
1061       calc_workers_for_java_threads(application_workers),
1062       calc_workers_for_live_set(live_data));
1063   }
1064 }
1065 
1066 uint ShenandoahCollectorPolicy::calc_workers_for_conc_marking(uint active_workers,
1067                                             uint application_workers) {
1068 
1069   if (!UseDynamicNumberOfGCThreads ||
1070      (!FLAG_IS_DEFAULT(ConcGCThreads) && !ForceDynamicNumberOfGCThreads)) {
1071     assert(ConcGCThreads > 0, "Always need at least 1");
1072     return ConcGCThreads;
1073   } else {
1074     return calc_default_active_workers(ConcGCThreads,
1075       (ConcGCThreads > 1 ? 2 : 1), active_workers,
1076       application_workers, calc_workers_for_java_threads(application_workers), 0);
1077   }
1078 }
1079 
1080 uint ShenandoahCollectorPolicy::calc_workers_for_final_marking(uint active_workers,
1081                                             uint application_workers) {
1082 
1083   if (!UseDynamicNumberOfGCThreads ||
1084      (!FLAG_IS_DEFAULT(ParallelGCThreads) && !ForceDynamicNumberOfGCThreads)) {
1085     assert(ParallelGCThreads > 0, "Always need at least 1");
1086     return ParallelGCThreads;
1087   } else {
1088     return calc_default_active_workers(ParallelGCThreads,
1089       (ParallelGCThreads > 1 ? 2 : 1), active_workers,
1090       application_workers, calc_workers_for_java_threads(application_workers), 0);
1091   }
1092 }
1093 
1094   // Calculate workers for concurrent evacuation (concurrent GC)
1095 uint ShenandoahCollectorPolicy::calc_workers_for_conc_evacuation(uint active_workers,
1096                                             uint application_workers) {
1097   if (!UseDynamicNumberOfGCThreads ||
1098      (!FLAG_IS_DEFAULT(ConcGCThreads) && !ForceDynamicNumberOfGCThreads)) {
1099     assert(ConcGCThreads > 0, "Always need at least 1");
1100     return ConcGCThreads;
1101   } else {
1102     return calc_workers_for_evacuation(false, // not a full GC
1103       ConcGCThreads, active_workers, application_workers);
1104   }
1105 }
1106 
1107   // Calculate workers for parallel evaculation (full GC)
1108 uint ShenandoahCollectorPolicy::calc_workers_for_parallel_evacuation(uint active_workers,
1109                                             uint application_workers) {
1110   if (!UseDynamicNumberOfGCThreads ||
1111      (!FLAG_IS_DEFAULT(ParallelGCThreads) && !ForceDynamicNumberOfGCThreads)) {
1112     assert(ParallelGCThreads > 0, "Always need at least 1");
1113     return ParallelGCThreads;
1114   } else {
1115     return calc_workers_for_evacuation(true, // a full GC
1116       ParallelGCThreads, active_workers, application_workers);
1117   }
1118 }
1119 
1120 
1121 uint ShenandoahCollectorPolicy::calc_workers_for_evacuation(bool full_gc,
1122                                             uint total_workers,
1123                                             uint active_workers,
1124                                             uint application_workers) {
1125 
1126   // Calculation based on live set
1127   size_t live_data = 0;
1128   ShenandoahHeap* heap = ShenandoahHeap::heap();
1129   if (full_gc) {
1130     ShenandoahHeapRegionSet* regions = heap->regions();
1131     for (size_t index = 0; index < regions->active_regions(); index ++) {
1132       live_data += regions->get_fast(index)->get_live_data_bytes();
1133     }
1134   } else {
1135     ShenandoahCollectorPolicy* policy = (ShenandoahCollectorPolicy*)heap->collector_policy();
1136     live_data = policy->_heuristics->bytes_in_cset();
1137   }
1138 
1139   uint active_workers_by_liveset = calc_workers_for_live_set(live_data);
1140   return calc_default_active_workers(total_workers,
1141       (total_workers > 1 ? 2 : 1), active_workers,
1142       application_workers, 0, active_workers_by_liveset);
1143 }
1144 
1145 bool ShenandoahCollectorPolicy::should_start_partial_gc() {
1146   return _heuristics->should_start_partial_gc();
1147 }