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