1 /*
   2  * Copyright (c) 2016, 2020, 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_card_merge_to_scan_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_card_scan_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 static double young_other_cost_per_region_ms_defaults[] = {
  66   0.3, 0.2, 0.2, 0.15, 0.15, 0.12, 0.12, 0.1
  67 };
  68 
  69 static double non_young_other_cost_per_region_ms_defaults[] = {
  70   1.0, 0.7, 0.7, 0.5, 0.5, 0.42, 0.42, 0.30
  71 };
  72 
  73 G1Analytics::G1Analytics(const G1Predictions* predictor) :
  74     _predictor(predictor),
  75     _recent_gc_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)),
  76     _concurrent_mark_remark_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)),
  77     _concurrent_mark_cleanup_times_ms(new TruncatedSeq(NumPrevPausesForHeuristics)),
  78     _alloc_rate_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  79     _prev_collection_pause_end_ms(0.0),
  80     _rs_length_diff_seq(new TruncatedSeq(TruncatedSeqLength)),
  81     _concurrent_refine_rate_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  82     _dirtied_cards_rate_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  83     _young_card_merge_to_scan_ratio_seq(new TruncatedSeq(TruncatedSeqLength)),
  84     _mixed_card_merge_to_scan_ratio_seq(new TruncatedSeq(TruncatedSeqLength)),
  85     _young_cost_per_card_scan_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  86     _mixed_cost_per_card_scan_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  87     _young_cost_per_card_merge_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  88     _mixed_cost_per_card_merge_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  89     _copy_cost_per_byte_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  90     _constant_other_time_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  91     _young_other_cost_per_region_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  92     _non_young_other_cost_per_region_ms_seq(new TruncatedSeq(TruncatedSeqLength)),
  93     _pending_cards_seq(new TruncatedSeq(TruncatedSeqLength)),
  94     _rs_length_seq(new TruncatedSeq(TruncatedSeqLength)),
  95     _cost_per_byte_ms_during_cm_seq(new TruncatedSeq(TruncatedSeqLength)),
  96     _recent_prev_end_times_for_all_gcs_sec(new TruncatedSeq(NumPrevPausesForHeuristics)),
  97     _long_term_pause_time_ratio(0.0),
  98     _short_term_pause_time_ratio(0.0) {
  99 
 100   // Seed sequences with initial values.
 101   _recent_prev_end_times_for_all_gcs_sec->add(os::elapsedTime());
 102   _prev_collection_pause_end_ms = os::elapsedTime() * 1000.0;
 103 
 104   int index = MIN2(ParallelGCThreads - 1, 7u);
 105 
 106   _rs_length_diff_seq->add(rs_length_diff_defaults[index]);
 107   // Start with inverse of maximum STW cost.
 108   _concurrent_refine_rate_ms_seq->add(1/cost_per_logged_card_ms_defaults[0]);
 109   // Some applications have very low rates for logging cards.
 110   _dirtied_cards_rate_ms_seq->add(0.0);
 111   _young_card_merge_to_scan_ratio_seq->add(young_card_merge_to_scan_ratio_defaults[index]);
 112   _young_cost_per_card_scan_ms_seq->add(young_only_cost_per_card_scan_ms_defaults[index]);
 113 
 114   _copy_cost_per_byte_ms_seq->add(cost_per_byte_ms_defaults[index]);
 115   _constant_other_time_ms_seq->add(constant_other_time_ms_defaults[index]);
 116   _young_other_cost_per_region_ms_seq->add(young_other_cost_per_region_ms_defaults[index]);
 117   _non_young_other_cost_per_region_ms_seq->add(non_young_other_cost_per_region_ms_defaults[index]);
 118 
 119   // start conservatively (around 50ms is about right)
 120   _concurrent_mark_remark_times_ms->add(0.05);
 121   _concurrent_mark_cleanup_times_ms->add(0.20);
 122 }
 123 
 124 bool G1Analytics::enough_samples_available(TruncatedSeq const* seq) const {
 125   return seq->num() >= 3;
 126 }
 127 
 128 double G1Analytics::predict_in_unit_interval(TruncatedSeq const* seq) const {
 129   return _predictor->predict_in_unit_interval(seq);
 130 }
 131 
 132 size_t G1Analytics::predict_size(TruncatedSeq const* seq) const {
 133   return (size_t)predict_zero_bounded(seq);
 134 }
 135 
 136 double G1Analytics::predict_zero_bounded(TruncatedSeq const* seq) const {
 137   return _predictor->predict_zero_bounded(seq);
 138 }
 139 
 140 int G1Analytics::num_alloc_rate_ms() const {
 141   return _alloc_rate_ms_seq->num();
 142 }
 143 
 144 void G1Analytics::report_concurrent_mark_remark_times_ms(double ms) {
 145   _concurrent_mark_remark_times_ms->add(ms);
 146 }
 147 
 148 void G1Analytics::report_alloc_rate_ms(double alloc_rate) {
 149   _alloc_rate_ms_seq->add(alloc_rate);
 150 }
 151 
 152 void G1Analytics::compute_pause_time_ratios(double end_time_sec, double pause_time_ms) {
 153   double long_interval_ms = (end_time_sec - oldest_known_gc_end_time_sec()) * 1000.0;
 154   _long_term_pause_time_ratio = _recent_gc_times_ms->sum() / long_interval_ms;
 155   _long_term_pause_time_ratio = clamp(_long_term_pause_time_ratio, 0.0, 1.0);
 156 
 157   double short_interval_ms = (end_time_sec - most_recent_gc_end_time_sec()) * 1000.0;
 158   _short_term_pause_time_ratio = pause_time_ms / short_interval_ms;
 159   _short_term_pause_time_ratio = clamp(_short_term_pause_time_ratio, 0.0, 1.0);
 160 }
 161 
 162 void G1Analytics::report_concurrent_refine_rate_ms(double cards_per_ms) {
 163   _concurrent_refine_rate_ms_seq->add(cards_per_ms);
 164 }
 165 
 166 void G1Analytics::report_dirtied_cards_rate_ms(double cards_per_ms) {
 167   _dirtied_cards_rate_ms_seq->add(cards_per_ms);
 168 }
 169 
 170 void G1Analytics::report_cost_per_card_scan_ms(double cost_per_card_ms, bool for_young_gc) {
 171   if (for_young_gc) {
 172     _young_cost_per_card_scan_ms_seq->add(cost_per_card_ms);
 173   } else {
 174     _mixed_cost_per_card_scan_ms_seq->add(cost_per_card_ms);
 175   }
 176 }
 177 
 178 void G1Analytics::report_cost_per_card_merge_ms(double cost_per_card_ms, bool for_young_gc) {
 179   if (for_young_gc) {
 180     _young_cost_per_card_merge_ms_seq->add(cost_per_card_ms);
 181   } else {
 182     _mixed_cost_per_card_merge_ms_seq->add(cost_per_card_ms);
 183   }
 184 }
 185 
 186 void G1Analytics::report_card_merge_to_scan_ratio(double merge_to_scan_ratio, bool for_young_gc) {
 187   if (for_young_gc) {
 188     _young_card_merge_to_scan_ratio_seq->add(merge_to_scan_ratio);
 189   } else {
 190     _mixed_card_merge_to_scan_ratio_seq->add(merge_to_scan_ratio);
 191   }
 192 }
 193 
 194 void G1Analytics::report_rs_length_diff(double rs_length_diff) {
 195   _rs_length_diff_seq->add(rs_length_diff);
 196 }
 197 
 198 void G1Analytics::report_cost_per_byte_ms(double cost_per_byte_ms, bool mark_or_rebuild_in_progress) {
 199   if (mark_or_rebuild_in_progress) {
 200     _cost_per_byte_ms_during_cm_seq->add(cost_per_byte_ms);
 201   } else {
 202     _copy_cost_per_byte_ms_seq->add(cost_per_byte_ms);
 203   }
 204 }
 205 
 206 void G1Analytics::report_young_other_cost_per_region_ms(double other_cost_per_region_ms) {
 207   _young_other_cost_per_region_ms_seq->add(other_cost_per_region_ms);
 208 }
 209 
 210 void G1Analytics::report_non_young_other_cost_per_region_ms(double other_cost_per_region_ms) {
 211   _non_young_other_cost_per_region_ms_seq->add(other_cost_per_region_ms);
 212 }
 213 
 214 void G1Analytics::report_constant_other_time_ms(double constant_other_time_ms) {
 215   _constant_other_time_ms_seq->add(constant_other_time_ms);
 216 }
 217 
 218 void G1Analytics::report_pending_cards(double pending_cards) {
 219   _pending_cards_seq->add(pending_cards);
 220 }
 221 
 222 void G1Analytics::report_rs_length(double rs_length) {
 223   _rs_length_seq->add(rs_length);
 224 }
 225 
 226 double G1Analytics::predict_alloc_rate_ms() const {
 227   return predict_zero_bounded(_alloc_rate_ms_seq);
 228 }
 229 
 230 double G1Analytics::predict_concurrent_refine_rate_ms() const {
 231   return predict_zero_bounded(_concurrent_refine_rate_ms_seq);
 232 }
 233 
 234 double G1Analytics::predict_dirtied_cards_rate_ms() const {
 235   return predict_zero_bounded(_dirtied_cards_rate_ms_seq);
 236 }
 237 
 238 double G1Analytics::predict_young_card_merge_to_scan_ratio() const {
 239   return predict_in_unit_interval(_young_card_merge_to_scan_ratio_seq);
 240 }
 241 
 242 size_t G1Analytics::predict_scan_card_num(size_t rs_length, bool for_young_gc) const {
 243   if (for_young_gc || !enough_samples_available(_mixed_card_merge_to_scan_ratio_seq)) {
 244     return (size_t)(rs_length * predict_young_card_merge_to_scan_ratio());
 245   } else {
 246     return (size_t)(rs_length * predict_in_unit_interval(_mixed_card_merge_to_scan_ratio_seq));
 247   }
 248 }
 249 
 250 double G1Analytics::predict_card_merge_time_ms(size_t card_num, bool for_young_gc) const {
 251   if (for_young_gc || !enough_samples_available(_mixed_cost_per_card_merge_ms_seq)) {
 252     return card_num * predict_zero_bounded(_young_cost_per_card_merge_ms_seq);
 253   } else {
 254     return card_num * predict_zero_bounded(_mixed_cost_per_card_merge_ms_seq);
 255   }
 256 }
 257 
 258 double G1Analytics::predict_card_scan_time_ms(size_t card_num, bool for_young_gc) const {
 259   if (for_young_gc || !enough_samples_available(_mixed_cost_per_card_scan_ms_seq)) {
 260     return card_num * predict_zero_bounded(_young_cost_per_card_scan_ms_seq);
 261   } else {
 262     return card_num * predict_zero_bounded(_mixed_cost_per_card_scan_ms_seq);
 263   }
 264 }
 265 
 266 double G1Analytics::predict_object_copy_time_ms_during_cm(size_t bytes_to_copy) const {
 267   if (!enough_samples_available(_cost_per_byte_ms_during_cm_seq)) {
 268     return (1.1 * bytes_to_copy) * predict_zero_bounded(_copy_cost_per_byte_ms_seq);
 269   } else {
 270     return bytes_to_copy * predict_zero_bounded(_cost_per_byte_ms_during_cm_seq);
 271   }
 272 }
 273 
 274 double G1Analytics::predict_object_copy_time_ms(size_t bytes_to_copy, bool during_concurrent_mark) const {
 275   if (during_concurrent_mark) {
 276     return predict_object_copy_time_ms_during_cm(bytes_to_copy);
 277   } else {
 278     return bytes_to_copy * predict_zero_bounded(_copy_cost_per_byte_ms_seq);
 279   }
 280 }
 281 
 282 double G1Analytics::predict_constant_other_time_ms() const {
 283   return predict_zero_bounded(_constant_other_time_ms_seq);
 284 }
 285 
 286 double G1Analytics::predict_young_other_time_ms(size_t young_num) const {
 287   return young_num * predict_zero_bounded(_young_other_cost_per_region_ms_seq);
 288 }
 289 
 290 double G1Analytics::predict_non_young_other_time_ms(size_t non_young_num) const {
 291   return non_young_num * predict_zero_bounded(_non_young_other_cost_per_region_ms_seq);
 292 }
 293 
 294 double G1Analytics::predict_remark_time_ms() const {
 295   return predict_zero_bounded(_concurrent_mark_remark_times_ms);
 296 }
 297 
 298 double G1Analytics::predict_cleanup_time_ms() const {
 299   return predict_zero_bounded(_concurrent_mark_cleanup_times_ms);
 300 }
 301 
 302 size_t G1Analytics::predict_rs_length() const {
 303   return predict_size(_rs_length_seq) + predict_size(_rs_length_diff_seq);
 304 }
 305 
 306 size_t G1Analytics::predict_pending_cards() const {
 307   return predict_size(_pending_cards_seq);
 308 }
 309 
 310 double G1Analytics::oldest_known_gc_end_time_sec() const {
 311   return _recent_prev_end_times_for_all_gcs_sec->oldest();
 312 }
 313 
 314 double G1Analytics::most_recent_gc_end_time_sec() const {
 315   return _recent_prev_end_times_for_all_gcs_sec->last();
 316 }
 317 
 318 void G1Analytics::update_recent_gc_times(double end_time_sec,
 319                                          double pause_time_ms) {
 320   _recent_gc_times_ms->add(pause_time_ms);
 321   _recent_prev_end_times_for_all_gcs_sec->add(end_time_sec);
 322   _prev_collection_pause_end_ms = end_time_sec * 1000.0;
 323 }
 324 
 325 void G1Analytics::report_concurrent_mark_cleanup_times_ms(double ms) {
 326   _concurrent_mark_cleanup_times_ms->add(ms);
 327 }