1 /*
   2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/g1/g1Analytics.hpp"
  27 #include "gc/g1/g1Predictions.hpp"
  28 #include "runtime/globals.hpp"
  29 #include "runtime/os.hpp"
  30 #include "utilities/debug.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 #include "utilities/numberSeq.hpp"
  33 
  34 // Different defaults for different number of GC threads
  35 // They were chosen by running GCOld and SPECjbb on debris with different
  36 //   numbers of GC threads and choosing them based on the results
  37 
  38 // all the same
  39 static double rs_length_diff_defaults[] = {
  40   0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
  41 };
  42 
  43 static double cost_per_logged_card_ms_defaults[] = {
  44   0.01, 0.005, 0.005, 0.003, 0.003, 0.002, 0.002, 0.0015
  45 };
  46 
  47 // all the same
  48 static double young_cards_per_entry_ratio_defaults[] = {
  49   1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
  50 };
  51 
  52 static double young_only_cost_per_remset_card_ms_defaults[] = {
  53   0.015, 0.01, 0.01, 0.008, 0.008, 0.0055, 0.0055, 0.005
  54 };
  55 
  56 static double cost_per_byte_ms_defaults[] = {
  57   0.00006, 0.00003, 0.00003, 0.000015, 0.000015, 0.00001, 0.00001, 0.000009
  58 };
  59 
  60 // these should be pretty consistent
  61 static double constant_other_time_ms_defaults[] = {
  62   5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0
  63 };
  64 
  65 
  66 static double young_other_cost_per_region_ms_defaults[] = {
  67   0.3, 0.2, 0.2, 0.15, 0.15, 0.12, 0.12, 0.1
  68 };
  69 
  70 static double non_young_other_cost_per_region_ms_defaults[] = {
  71   1.0, 0.7, 0.7, 0.5, 0.5, 0.42, 0.42, 0.30
  72 };
  73 
  74 G1Analytics::G1Analytics(const G1Predictions* predictor) :
  75     _predictor(predictor),
  76     _recent_gc_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)),
  77     _concurrent_mark_remark_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)),
  78     _concurrent_mark_cleanup_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)),
  79     _alloc_rate_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  80     _prev_collection_pause_end_ms(0.0),
  81     _rs_length_diff_seq(new TruncatedSeq(TruncatedSeqLength)),
  82     _concurrent_refine_rate_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  83     _logged_cards_rate_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  84     _cost_per_logged_card_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  85     _cost_scan_hcc_seq(new TruncatedSeq(TruncatedSeqLength)),
  86     _young_cards_per_entry_ratio_seq(new TruncatedSeq(TruncatedSeqLength)),
  87     _mixed_cards_per_entry_ratio_seq(new TruncatedSeq(TruncatedSeqLength)),
  88     _young_only_cost_per_remset_card_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  89     _mixed_cost_per_remset_card_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  90     _cost_per_byte_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  91     _constant_other_time_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  92     _young_other_cost_per_region_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  93     _non_young_other_cost_per_region_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  94     _pending_cards_seq(new TruncatedSeq(TruncatedSeqLength)),
  95     _rs_length_seq(new TruncatedSeq(TruncatedSeqLength)),
  96     _cost_per_byte_ms_during_cm_seq(new TruncatedSeq(TruncatedSeqLength)),
  97     _recent_prev_end_times_for_all_gcs_sec(new TruncatedSeq(NumPrevPausesForHeuristics)),
  98     _recent_avg_pause_time_ratio(0.0),
  99     _last_pause_time_ratio(0.0) {
 100 
 101   // Seed sequences with initial values.
 102   _recent_prev_end_times_for_all_gcs_sec->add(os::elapsedTime());
 103   _prev_collection_pause_end_ms = os::elapsedTime() * 1000.0;
 104 
 105   int index = MIN2(ParallelGCThreads - 1, 7u);
 106 
 107   _rs_length_diff_seq->add(rs_length_diff_defaults[index]);
 108   // Start with inverse of maximum STW cost.
 109   _concurrent_refine_rate_ms_seq->add(1/cost_per_logged_card_ms_defaults[0]);
 110   // Some applications have very low rates for logging cards.
 111   _logged_cards_rate_ms_seq->add(0.0);
 112   _cost_per_logged_card_ms_seq->add(cost_per_logged_card_ms_defaults[index]);
 113   _cost_scan_hcc_seq->add(0.0);
 114   _young_cards_per_entry_ratio_seq->add(young_cards_per_entry_ratio_defaults[index]);
 115   _young_only_cost_per_remset_card_ms_seq->add(young_only_cost_per_remset_card_ms_defaults[index]);
 116   _cost_per_byte_ms_seq->add(cost_per_byte_ms_defaults[index]);
 117   _constant_other_time_ms_seq->add(constant_other_time_ms_defaults[index]);
 118   _young_other_cost_per_region_ms_seq->add(young_other_cost_per_region_ms_defaults[index]);
 119   _non_young_other_cost_per_region_ms_seq->add(non_young_other_cost_per_region_ms_defaults[index]);
 120 
 121   // start conservatively (around 50ms is about right)
 122   _concurrent_mark_remark_times_ms->add(0.05);
 123   _concurrent_mark_cleanup_times_ms->add(0.20);
 124 }
 125 
 126 double G1Analytics::get_new_prediction(TruncatedSeq const* seq) const {
 127   return _predictor->get_new_prediction(seq);
 128 }
 129 
 130 size_t G1Analytics::get_new_size_prediction(TruncatedSeq const* seq) const {
 131   return (size_t)get_new_prediction(seq);
 132 }
 133 
 134 int G1Analytics::num_alloc_rate_ms() const {
 135   return _alloc_rate_ms_seq->num();
 136 }
 137 
 138 void G1Analytics::report_concurrent_mark_remark_times_ms(double ms) {
 139   _concurrent_mark_remark_times_ms->add(ms);
 140 }
 141 
 142 void G1Analytics::report_alloc_rate_ms(double alloc_rate) {
 143   _alloc_rate_ms_seq->add(alloc_rate);
 144 }
 145 
 146 void G1Analytics::compute_pause_time_ratio(double interval_ms, double pause_time_ms) {
 147   _recent_avg_pause_time_ratio = _recent_gc_times_ms->sum() / interval_ms;
 148 
 149   // Clamp the result to [0.0 ... 1.0] to filter out nonsensical results due to bad input.
 150   _recent_avg_pause_time_ratio = clamp(_recent_avg_pause_time_ratio, 0.0, 1.0);
 151 
 152   // Compute the ratio of just this last pause time to the entire time range stored
 153   // in the vectors. Comparing this pause to the entire range, rather than only the
 154   // most recent interval, has the effect of smoothing over a possible transient 'burst'
 155   // of more frequent pauses that don't really reflect a change in heap occupancy.
 156   // This reduces the likelihood of a needless heap expansion being triggered.
 157   _last_pause_time_ratio =
 158     (pause_time_ms * _recent_prev_end_times_for_all_gcs_sec->num()) / interval_ms;
 159 }
 160 
 161 void G1Analytics::report_concurrent_refine_rate_ms(double cards_per_ms) {
 162   _concurrent_refine_rate_ms_seq->add(cards_per_ms);
 163 }
 164 
 165 void G1Analytics::report_logged_cards_rate_ms(double cards_per_ms) {
 166   _logged_cards_rate_ms_seq->add(cards_per_ms);
 167 }
 168 
 169 void G1Analytics::report_cost_per_logged_card_ms(double cost_per_logged_card_ms) {
 170   _cost_per_logged_card_ms_seq->add(cost_per_logged_card_ms);
 171 }
 172 
 173 void G1Analytics::report_cost_scan_hcc(double cost_scan_hcc) {
 174   _cost_scan_hcc_seq->add(cost_scan_hcc);
 175 }
 176 
 177 void G1Analytics::report_cost_per_remset_card_ms(double cost_per_remset_card_ms, bool for_young_gc) {
 178   if (for_young_gc) {
 179     _young_only_cost_per_remset_card_ms_seq->add(cost_per_remset_card_ms);
 180   } else {
 181     _mixed_cost_per_remset_card_ms_seq->add(cost_per_remset_card_ms);
 182   }
 183 }
 184 
 185 void G1Analytics::report_cards_per_entry_ratio(double cards_per_entry_ratio, bool for_young_gc) {
 186   if (for_young_gc) {
 187     _young_cards_per_entry_ratio_seq->add(cards_per_entry_ratio);
 188   } else {
 189     _mixed_cards_per_entry_ratio_seq->add(cards_per_entry_ratio);
 190   }
 191 }
 192 
 193 void G1Analytics::report_rs_length_diff(double rs_length_diff) {
 194   _rs_length_diff_seq->add(rs_length_diff);
 195 }
 196 
 197 void G1Analytics::report_cost_per_byte_ms(double cost_per_byte_ms, bool mark_or_rebuild_in_progress) {
 198   if (mark_or_rebuild_in_progress) {
 199     _cost_per_byte_ms_during_cm_seq->add(cost_per_byte_ms);
 200   } else {
 201     _cost_per_byte_ms_seq->add(cost_per_byte_ms);
 202   }
 203 }
 204 
 205 void G1Analytics::report_young_other_cost_per_region_ms(double other_cost_per_region_ms) {
 206   _young_other_cost_per_region_ms_seq->add(other_cost_per_region_ms);
 207 }
 208 
 209 void G1Analytics::report_non_young_other_cost_per_region_ms(double other_cost_per_region_ms) {
 210   _non_young_other_cost_per_region_ms_seq->add(other_cost_per_region_ms);
 211 }
 212 
 213 void G1Analytics::report_constant_other_time_ms(double constant_other_time_ms) {
 214   _constant_other_time_ms_seq->add(constant_other_time_ms);
 215 }
 216 
 217 void G1Analytics::report_pending_cards(double pending_cards) {
 218   _pending_cards_seq->add(pending_cards);
 219 }
 220 
 221 void G1Analytics::report_rs_length(double rs_length) {
 222   _rs_length_seq->add(rs_length);
 223 }
 224 
 225 double G1Analytics::predict_alloc_rate_ms() const {
 226   return get_new_prediction(_alloc_rate_ms_seq);
 227 }
 228 
 229 double G1Analytics::predict_concurrent_refine_rate_ms() const {
 230   return get_new_prediction(_concurrent_refine_rate_ms_seq);
 231 }
 232 
 233 double G1Analytics::predict_logged_cards_rate_ms() const {
 234   return get_new_prediction(_logged_cards_rate_ms_seq);
 235 }
 236 
 237 double G1Analytics::predict_cost_per_logged_card_ms() const {
 238   return get_new_prediction(_cost_per_logged_card_ms_seq);
 239 }
 240 
 241 double G1Analytics::predict_scan_hcc_ms() const {
 242   return get_new_prediction(_cost_scan_hcc_seq);
 243 }
 244 
 245 double G1Analytics::predict_rs_update_time_ms(size_t pending_cards) const {
 246   return pending_cards * predict_cost_per_logged_card_ms() + predict_scan_hcc_ms();
 247 }
 248 
 249 double G1Analytics::predict_young_cards_per_entry_ratio() const {
 250   return get_new_prediction(_young_cards_per_entry_ratio_seq);
 251 }
 252 
 253 double G1Analytics::predict_mixed_cards_per_entry_ratio() const {
 254   if (_mixed_cards_per_entry_ratio_seq->num() < 2) {
 255     return predict_young_cards_per_entry_ratio();
 256   } else {
 257     return get_new_prediction(_mixed_cards_per_entry_ratio_seq);
 258   }
 259 }
 260 
 261 size_t G1Analytics::predict_card_num(size_t rs_length, bool for_young_gc) const {
 262   if (for_young_gc) {
 263     return (size_t) (rs_length * predict_young_cards_per_entry_ratio());
 264   } else {
 265     return (size_t) (rs_length * predict_mixed_cards_per_entry_ratio());
 266   }
 267 }
 268 
 269 double G1Analytics::predict_rs_scan_time_ms(size_t card_num, bool for_young_gc) const {
 270   if (for_young_gc) {
 271     return card_num * get_new_prediction(_young_only_cost_per_remset_card_ms_seq);
 272   } else {
 273     return predict_mixed_rs_scan_time_ms(card_num);
 274   }
 275 }
 276 
 277 double G1Analytics::predict_mixed_rs_scan_time_ms(size_t card_num) const {
 278   if (_mixed_cost_per_remset_card_ms_seq->num() < 3) {
 279     return card_num * get_new_prediction(_young_only_cost_per_remset_card_ms_seq);
 280   } else {
 281     return card_num * get_new_prediction(_mixed_cost_per_remset_card_ms_seq);
 282   }
 283 }
 284 
 285 double G1Analytics::predict_object_copy_time_ms_during_cm(size_t bytes_to_copy) const {
 286   if (_cost_per_byte_ms_during_cm_seq->num() < 3) {
 287     return (1.1 * bytes_to_copy) * get_new_prediction(_cost_per_byte_ms_seq);
 288   } else {
 289     return bytes_to_copy * get_new_prediction(_cost_per_byte_ms_during_cm_seq);
 290   }
 291 }
 292 
 293 double G1Analytics::predict_object_copy_time_ms(size_t bytes_to_copy, bool during_concurrent_mark) const {
 294   if (during_concurrent_mark) {
 295     return predict_object_copy_time_ms_during_cm(bytes_to_copy);
 296   } else {
 297     return bytes_to_copy * get_new_prediction(_cost_per_byte_ms_seq);
 298   }
 299 }
 300 
 301 double G1Analytics::predict_cost_per_byte_ms() const {
 302   return get_new_prediction(_cost_per_byte_ms_seq);
 303 }
 304 
 305 double G1Analytics::predict_constant_other_time_ms() const {
 306   return get_new_prediction(_constant_other_time_ms_seq);
 307 }
 308 
 309 double G1Analytics::predict_young_other_time_ms(size_t young_num) const {
 310   return young_num * get_new_prediction(_young_other_cost_per_region_ms_seq);
 311 }
 312 
 313 double G1Analytics::predict_non_young_other_time_ms(size_t non_young_num) const {
 314   return non_young_num * get_new_prediction(_non_young_other_cost_per_region_ms_seq);
 315 }
 316 
 317 double G1Analytics::predict_remark_time_ms() const {
 318   return get_new_prediction(_concurrent_mark_remark_times_ms);
 319 }
 320 
 321 double G1Analytics::predict_cleanup_time_ms() const {
 322   return get_new_prediction(_concurrent_mark_cleanup_times_ms);
 323 }
 324 
 325 size_t G1Analytics::predict_rs_length() const {
 326   return get_new_size_prediction(_rs_length_seq) + get_new_prediction(_rs_length_diff_seq);
 327 }
 328 
 329 size_t G1Analytics::predict_pending_cards() const {
 330   return get_new_size_prediction(_pending_cards_seq);
 331 }
 332 
 333 double G1Analytics::last_known_gc_end_time_sec() const {
 334   return _recent_prev_end_times_for_all_gcs_sec->oldest();
 335 }
 336 
 337 void G1Analytics::update_recent_gc_times(double end_time_sec,
 338                                          double pause_time_ms) {
 339   _recent_gc_times_ms->add(pause_time_ms);
 340   _recent_prev_end_times_for_all_gcs_sec->add(end_time_sec);
 341   _prev_collection_pause_end_ms = end_time_sec * 1000.0;
 342 }
 343 
 344 void G1Analytics::report_concurrent_mark_cleanup_times_ms(double ms) {
 345   _concurrent_mark_cleanup_times_ms->add(ms);
 346 }