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   size_t _degenerated_uprefs;
 185   size_t _successful_uprefs;
 186 
 187   ShenandoahHeuristics* _heuristics;
 188   ShenandoahTracer* _tracer;
 189   STWGCTimer* _stw_timer;
 190   ConcurrentGCTimer* _conc_timer;
 191 
 192   bool _conc_gc_aborted;
 193 
 194   size_t _cycle_counter;
 195 
 196   ShenandoahPhaseTimes* _phase_times;
 197 
 198 public:
 199   ShenandoahCollectorPolicy();
 200 
 201   ShenandoahPhaseTimes* phase_times();
 202 
 203   virtual ShenandoahCollectorPolicy* as_pgc_policy();
 204 
 205   BarrierSet::Name barrier_set_name();
 206 
 207   HeapWord* mem_allocate_work(size_t size,
 208                               bool is_tlab,
 209                               bool* gc_overhead_limit_was_exceeded);
 210 
 211   HeapWord* satisfy_failed_allocation(size_t size, bool is_tlab);
 212 
 213   void initialize_alignments();
 214 
 215   void post_heap_initialize();
 216 
 217   void record_gc_start();
 218   void record_gc_end();
 219 
 220   void record_phase_start(TimingPhase phase);
 221   void record_phase_end(TimingPhase phase);
 222 
 223   void record_workers_start(TimingPhase phase);
 224   void record_workers_end(TimingPhase phase);
 225 
 226   void report_concgc_cancelled();
 227 
 228   void record_user_requested_gc();
 229   void record_allocation_failure_gc();
 230 
 231   void record_bytes_allocated(size_t bytes);
 232   void record_bytes_reclaimed(size_t bytes);
 233   void record_bytes_start_CM(size_t bytes);
 234   void record_bytes_end_CM(size_t bytes);
 235   bool should_start_concurrent_mark(size_t used, size_t capacity);
 236   bool should_start_partial_gc();
 237 
 238   // Returns true when there should be a separate concurrent reference
 239   // updating phase after evacuation.
 240   bool update_refs_early();
 241 
 242   bool handover_cancelled_marking();
 243   bool handover_cancelled_uprefs();
 244 
 245   void record_cm_cancelled();
 246   void record_cm_success();
 247   void record_cm_degenerated();
 248   void record_full_gc();
 249   void record_uprefs_cancelled();
 250   void record_uprefs_success();
 251   void record_uprefs_degenerated();
 252 
 253   void choose_collection_set(ShenandoahCollectionSet* collection_set, int* connections=NULL);
 254   void choose_free_set(ShenandoahFreeSet* free_set);
 255 
 256   bool process_references();
 257   bool unload_classes();
 258 
 259   void print_tracing_info(outputStream* out);
 260 
 261   GCTimer* conc_timer(){return _conc_timer;}
 262   GCTimer* stw_timer() {return _stw_timer;}
 263   ShenandoahTracer* tracer() {return _tracer;}
 264 
 265   void set_conc_gc_aborted() { _conc_gc_aborted = true;}
 266   void clear_conc_gc_aborted() {_conc_gc_aborted = false;}
 267 
 268   void increase_cycle_counter();
 269   size_t cycle_counter() const;
 270 
 271 
 272   // Calculate the number of workers for initial marking
 273   static uint calc_workers_for_init_marking(uint active_workers,
 274                                             uint application_workers);
 275 
 276   // Calculate the number of workers for concurrent marking
 277   static uint calc_workers_for_conc_marking(uint active_workers,
 278                                             uint application_workers);
 279 
 280   // Calculate the number of workers for final marking
 281   static uint calc_workers_for_final_marking(uint active_workers,
 282                                              uint application_workers);
 283 
 284   // Calculate workers for concurrent evacuation (concurrent GC)
 285   static uint calc_workers_for_conc_evacuation(uint active_workers,
 286                                                uint application_workers);
 287 
 288   // Calculate workers for parallel evaculation (full GC)
 289   static uint calc_workers_for_parallel_evacuation(uint active_workers,
 290                                                    uint application_workers);
 291 
 292 private:
 293   static uint calc_workers_for_java_threads(uint application_workers);
 294   static uint calc_workers_for_live_set(size_t live_data);
 295 
 296   static uint calc_default_active_workers(uint total_workers,
 297                                     uint min_workers,
 298                                     uint active_workers,
 299                                     uint application_workers,
 300                                     uint workers_by_java_threads,
 301                                     uint workers_by_liveset);
 302 
 303   static uint calc_workers_for_evacuation(bool full_gc,
 304                                     uint total_workers,
 305                                     uint active_workers,
 306                                     uint application_workers);
 307 
 308   void print_summary_sd(outputStream* out, const char* str, const HdrSeq* seq);
 309 };
 310 
 311 
 312 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAH_COLLECTOR_POLICY_HPP