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 #ifndef SHARE_VM_GC_SHENANDOAH_SHENANDOAH_COLLECTOR_POLICY_HPP
  25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAH_COLLECTOR_POLICY_HPP
  26 
  27 #include "gc/shared/gcTrace.hpp"
  28 #include "gc/shared/gcTimer.hpp"
  29 #include "gc/shared/collectorPolicy.hpp"
  30 #include "runtime/arguments.hpp"
  31 #include "utilities/numberSeq.hpp"
  32 
  33 class ShenandoahCollectionSet;
  34 class ShenandoahFreeSet;
  35 class ShenandoahHeap;
  36 class ShenandoahHeuristics;
  37 class ShenandoahPhaseTimes;
  38 
  39 class ShenandoahCollectorPolicy: public CollectorPolicy {
  40 public:
  41   enum TimingPhase {
  42     total_pause_gross,
  43     total_pause,
  44 
  45     init_mark_gross,
  46     init_mark,
  47     accumulate_stats,
  48     make_parsable,
  49     clear_liveness,
  50 
  51     // Per-thread timer block, should have "roots" counters in consistent order
  52     scan_roots,
  53     scan_thread_roots,
  54     scan_code_roots,
  55     scan_string_table_roots,
  56     scan_universe_roots,
  57     scan_jni_roots,
  58     scan_jni_weak_roots,
  59     scan_synchronizer_roots,
  60     scan_flat_profiler_roots,
  61     scan_management_roots,
  62     scan_system_dictionary_roots,
  63     scan_cldg_roots,
  64     scan_jvmti_roots,
  65 
  66     resize_tlabs,
  67 
  68     final_mark_gross,
  69     final_mark,
  70 
  71     // Per-thread timer block, should have "roots" counters in consistent order
  72     update_roots,
  73     update_thread_roots,
  74     update_code_roots,
  75     update_string_table_roots,
  76     update_universe_roots,
  77     update_jni_roots,
  78     update_jni_weak_roots,
  79     update_synchronizer_roots,
  80     update_flat_profiler_roots,
  81     update_management_roots,
  82     update_system_dictionary_roots,
  83     update_cldg_roots,
  84     update_jvmti_roots,
  85 
  86     finish_queues,
  87     weakrefs,
  88     class_unloading,
  89     prepare_evac,
  90     recycle_regions,
  91 
  92     // Per-thread timer block, should have "roots" counters in consistent order
  93     init_evac,
  94     evac_thread_roots,
  95     evac_code_roots,
  96     evac_string_table_roots,
  97     evac_universe_roots,
  98     evac_jni_roots,
  99     evac_jni_weak_roots,
 100     evac_synchronizer_roots,
 101     evac_flat_profiler_roots,
 102     evac_management_roots,
 103     evac_system_dictionary_roots,
 104     evac_cldg_roots,
 105     evac_jvmti_roots,
 106 
 107     init_update_refs_gross,
 108     init_update_refs,
 109 
 110     final_update_refs_gross,
 111     final_update_refs,
 112 
 113     // Per-thread timer block, should have "roots" counters in consistent order
 114     final_update_refs_roots,
 115     final_update_refs_thread_roots,
 116     final_update_refs_code_roots,
 117     final_update_refs_string_table_roots,
 118     final_update_refs_universe_roots,
 119     final_update_refs_jni_roots,
 120     final_update_refs_jni_weak_roots,
 121     final_update_refs_synchronizer_roots,
 122     final_update_refs_flat_profiler_roots,
 123     final_update_refs_management_roots,
 124     final_update_refs_system_dict_roots,
 125     final_update_refs_cldg_roots,
 126     final_update_refs_jvmti_roots,
 127 
 128     conc_mark,
 129     conc_evac,
 130     conc_update_refs,
 131     reset_bitmaps,
 132 
 133     partial_gc_gross,
 134     partial_gc,
 135     partial_gc_prepare,
 136 
 137     // Per-thread timer block, should have "roots" counters in consistent order
 138     partial_gc_work,
 139     partial_gc_thread_roots,
 140     partial_gc_code_roots,
 141     partial_gc_string_table_roots,
 142     partial_gc_universe_roots,
 143     partial_gc_jni_roots,
 144     partial_gc_jni_weak_roots,
 145     partial_gc_synchronizer_roots,
 146     partial_gc_flat_profiler_roots,
 147     partial_gc_management_roots,
 148     partial_gc_system_dict_roots,
 149     partial_gc_cldg_roots,
 150     partial_gc_jvmti_roots,
 151 
 152     partial_gc_recycle,
 153 
 154     full_gc,
 155     full_gc_heapdumps,
 156     full_gc_prepare,
 157     full_gc_mark,
 158     full_gc_mark_finish_queues,
 159     full_gc_mark_weakrefs,
 160     full_gc_mark_class_unloading,
 161     full_gc_calculate_addresses,
 162     full_gc_adjust_pointers,
 163     full_gc_copy_objects,
 164 
 165     _num_phases
 166   };
 167 
 168 private:
 169   struct TimingData {
 170     HdrSeq _secs;
 171     double _start;
 172     size_t _count;
 173   };
 174 
 175 private:
 176   TimingData _timing_data[_num_phases];
 177   const char* _phase_names[_num_phases];
 178 
 179   size_t _user_requested_gcs;
 180   size_t _allocation_failure_gcs;
 181   size_t _degenerated_cm;
 182   size_t _successful_cm;
 183 
 184   ShenandoahHeuristics* _heuristics;
 185   ShenandoahTracer* _tracer;
 186   STWGCTimer* _stw_timer;
 187   ConcurrentGCTimer* _conc_timer;
 188 
 189   bool _conc_gc_aborted;
 190 
 191   size_t _cycle_counter;
 192 
 193   ShenandoahPhaseTimes* _phase_times;
 194 
 195 public:
 196   ShenandoahCollectorPolicy();
 197 
 198   ShenandoahPhaseTimes* phase_times();
 199 
 200   virtual ShenandoahCollectorPolicy* as_pgc_policy();
 201 
 202   BarrierSet::Name barrier_set_name();
 203 
 204   HeapWord* mem_allocate_work(size_t size,
 205                               bool is_tlab,
 206                               bool* gc_overhead_limit_was_exceeded);
 207 
 208   HeapWord* satisfy_failed_allocation(size_t size, bool is_tlab);
 209 
 210   void initialize_alignments();
 211 
 212   void post_heap_initialize();
 213 
 214   void record_gc_start();
 215   void record_gc_end();
 216 
 217   void record_phase_start(TimingPhase phase);
 218   void record_phase_end(TimingPhase phase);
 219 
 220   void record_workers_start(TimingPhase phase);
 221   void record_workers_end(TimingPhase phase);
 222 
 223   void report_concgc_cancelled();
 224 
 225   void record_user_requested_gc();
 226   void record_allocation_failure_gc();
 227 
 228   void record_bytes_allocated(size_t bytes);
 229   void record_bytes_reclaimed(size_t bytes);
 230   void record_bytes_start_CM(size_t bytes);
 231   void record_bytes_end_CM(size_t bytes);
 232   bool should_start_concurrent_mark(size_t used, size_t capacity);
 233   bool should_start_partial_gc();
 234 
 235   // Returns true when there should be a separate concurrent reference
 236   // updating phase after evacuation.
 237   bool update_refs_early();
 238 
 239   bool handover_cancelled_marking();
 240 
 241   void record_cm_cancelled();
 242   void record_cm_success();
 243   void record_cm_degenerated();
 244   void record_full_gc();
 245 
 246   void choose_collection_set(ShenandoahCollectionSet* collection_set, int* connections=NULL);
 247   void choose_free_set(ShenandoahFreeSet* free_set);
 248 
 249   bool process_references();
 250   bool unload_classes();
 251 
 252   void print_tracing_info(outputStream* out);
 253 
 254   GCTimer* conc_timer(){return _conc_timer;}
 255   GCTimer* stw_timer() {return _stw_timer;}
 256   ShenandoahTracer* tracer() {return _tracer;}
 257 
 258   void set_conc_gc_aborted() { _conc_gc_aborted = true;}
 259   void clear_conc_gc_aborted() {_conc_gc_aborted = false;}
 260 
 261   void increase_cycle_counter();
 262   size_t cycle_counter() const;
 263 
 264 
 265   // Calculate the number of workers for initial marking
 266   static uint calc_workers_for_init_marking(uint active_workers,
 267                                             uint application_workers);
 268 
 269   // Calculate the number of workers for concurrent marking
 270   static uint calc_workers_for_conc_marking(uint active_workers,
 271                                             uint application_workers);
 272 
 273   // Calculate the number of workers for final marking
 274   static uint calc_workers_for_final_marking(uint active_workers,
 275                                              uint application_workers);
 276 
 277   // Calculate workers for concurrent evacuation (concurrent GC)
 278   static uint calc_workers_for_conc_evacuation(uint active_workers,
 279                                                uint application_workers);
 280 
 281   // Calculate workers for parallel evaculation (full GC)
 282   static uint calc_workers_for_parallel_evacuation(uint active_workers,
 283                                                    uint application_workers);
 284 
 285 private:
 286   static uint calc_workers_for_java_threads(uint application_workers);
 287   static uint calc_workers_for_live_set(size_t live_data);
 288 
 289   static uint calc_default_active_workers(uint total_workers,
 290                                     uint min_workers,
 291                                     uint active_workers,
 292                                     uint application_workers,
 293                                     uint workers_by_java_threads,
 294                                     uint workers_by_liveset);
 295 
 296   static uint calc_workers_for_evacuation(bool full_gc,
 297                                     uint total_workers,
 298                                     uint active_workers,
 299                                     uint application_workers);
 300 
 301   void print_summary_sd(outputStream* out, const char* str, const HdrSeq* seq);
 302 };
 303 
 304 
 305 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAH_COLLECTOR_POLICY_HPP