1 /*
   2  * Copyright (c) 2001, 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/g1BarrierSet.hpp"
  27 #include "gc/g1/g1ConcurrentRefine.hpp"
  28 #include "gc/g1/g1ConcurrentRefineThread.hpp"
  29 #include "gc/g1/g1DirtyCardQueue.hpp"
  30 #include "logging/log.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "memory/iterator.hpp"
  33 #include "runtime/java.hpp"
  34 #include "runtime/thread.hpp"
  35 #include "utilities/debug.hpp"
  36 #include "utilities/globalDefinitions.hpp"
  37 #include "utilities/pair.hpp"
  38 #include <math.h>
  39 
  40 G1ConcurrentRefineThread* G1ConcurrentRefineThreadControl::create_refinement_thread(uint worker_id, bool initializing) {
  41   G1ConcurrentRefineThread* result = NULL;
  42   if (initializing || !InjectGCWorkerCreationFailure) {
  43     result = new G1ConcurrentRefineThread(_cr, worker_id);
  44   }
  45   if (result == NULL || result->osthread() == NULL) {
  46     log_warning(gc)("Failed to create refinement thread %u, no more %s",
  47                     worker_id,
  48                     result == NULL ? "memory" : "OS threads");
  49   }
  50   return result;
  51 }
  52 
  53 G1ConcurrentRefineThreadControl::G1ConcurrentRefineThreadControl() :
  54   _cr(NULL),
  55   _threads(NULL),
  56   _num_max_threads(0)
  57 {
  58 }
  59 
  60 G1ConcurrentRefineThreadControl::~G1ConcurrentRefineThreadControl() {
  61   for (uint i = 0; i < _num_max_threads; i++) {
  62     G1ConcurrentRefineThread* t = _threads[i];
  63     if (t != NULL) {
  64       delete t;
  65     }
  66   }
  67   FREE_C_HEAP_ARRAY(G1ConcurrentRefineThread*, _threads);
  68 }
  69 
  70 jint G1ConcurrentRefineThreadControl::initialize(G1ConcurrentRefine* cr, uint num_max_threads) {
  71   assert(cr != NULL, "G1ConcurrentRefine must not be NULL");
  72   _cr = cr;
  73   _num_max_threads = num_max_threads;
  74 
  75   _threads = NEW_C_HEAP_ARRAY_RETURN_NULL(G1ConcurrentRefineThread*, num_max_threads, mtGC);
  76   if (_threads == NULL) {
  77     vm_shutdown_during_initialization("Could not allocate thread holder array.");
  78     return JNI_ENOMEM;
  79   }
  80 
  81   for (uint i = 0; i < num_max_threads; i++) {
  82     if (UseDynamicNumberOfGCThreads && i != 0 /* Always start first thread. */) {
  83       _threads[i] = NULL;
  84     } else {
  85       _threads[i] = create_refinement_thread(i, true);
  86       if (_threads[i] == NULL) {
  87         vm_shutdown_during_initialization("Could not allocate refinement threads.");
  88         return JNI_ENOMEM;
  89       }
  90     }
  91   }
  92 
  93   if (num_max_threads > 0) {
  94     G1BarrierSet::dirty_card_queue_set().set_primary_refinement_thread(_threads[0]);
  95   }
  96 
  97   return JNI_OK;
  98 }
  99 
 100 void G1ConcurrentRefineThreadControl::maybe_activate_next(uint cur_worker_id) {
 101   assert(cur_worker_id < _num_max_threads,
 102          "Activating another thread from %u not allowed since there can be at most %u",
 103          cur_worker_id, _num_max_threads);
 104   if (cur_worker_id == (_num_max_threads - 1)) {
 105     // Already the last thread, there is no more thread to activate.
 106     return;
 107   }
 108 
 109   uint worker_id = cur_worker_id + 1;
 110   G1ConcurrentRefineThread* thread_to_activate = _threads[worker_id];
 111   if (thread_to_activate == NULL) {
 112     // Still need to create the thread...
 113     _threads[worker_id] = create_refinement_thread(worker_id, false);
 114     thread_to_activate = _threads[worker_id];
 115   }
 116   if (thread_to_activate != NULL) {
 117     thread_to_activate->activate();
 118   }
 119 }
 120 
 121 void G1ConcurrentRefineThreadControl::print_on(outputStream* st) const {
 122   for (uint i = 0; i < _num_max_threads; ++i) {
 123     if (_threads[i] != NULL) {
 124       _threads[i]->print_on(st);
 125       st->cr();
 126     }
 127   }
 128 }
 129 
 130 void G1ConcurrentRefineThreadControl::worker_threads_do(ThreadClosure* tc) {
 131   for (uint i = 0; i < _num_max_threads; i++) {
 132     if (_threads[i] != NULL) {
 133       tc->do_thread(_threads[i]);
 134     }
 135   }
 136 }
 137 
 138 void G1ConcurrentRefineThreadControl::stop() {
 139   for (uint i = 0; i < _num_max_threads; i++) {
 140     if (_threads[i] != NULL) {
 141       _threads[i]->stop();
 142     }
 143   }
 144 }
 145 
 146 // Arbitrary but large limits, to simplify some of the zone calculations.
 147 // The general idea is to allow expressions like
 148 //   MIN2(x OP y, max_XXX_zone)
 149 // without needing to check for overflow in "x OP y", because the
 150 // ranges for x and y have been restricted.
 151 STATIC_ASSERT(sizeof(LP64_ONLY(jint) NOT_LP64(jshort)) <= (sizeof(size_t)/2));
 152 const size_t max_yellow_zone = LP64_ONLY(max_jint) NOT_LP64(max_jshort);
 153 const size_t max_green_zone = max_yellow_zone / 2;
 154 const size_t max_red_zone = INT_MAX; // For dcqs.set_max_cards.
 155 STATIC_ASSERT(max_yellow_zone <= max_red_zone);
 156 
 157 // Range check assertions for green zone values.
 158 #define assert_zone_constraints_g(green)                        \
 159   do {                                                          \
 160     size_t azc_g_green = (green);                               \
 161     assert(azc_g_green <= max_green_zone,                       \
 162            "green exceeds max: " SIZE_FORMAT, azc_g_green);     \
 163   } while (0)
 164 
 165 // Range check assertions for green and yellow zone values.
 166 #define assert_zone_constraints_gy(green, yellow)                       \
 167   do {                                                                  \
 168     size_t azc_gy_green = (green);                                      \
 169     size_t azc_gy_yellow = (yellow);                                    \
 170     assert_zone_constraints_g(azc_gy_green);                            \
 171     assert(azc_gy_yellow <= max_yellow_zone,                            \
 172            "yellow exceeds max: " SIZE_FORMAT, azc_gy_yellow);          \
 173     assert(azc_gy_green <= azc_gy_yellow,                               \
 174            "green (" SIZE_FORMAT ") exceeds yellow (" SIZE_FORMAT ")",  \
 175            azc_gy_green, azc_gy_yellow);                                \
 176   } while (0)
 177 
 178 // Range check assertions for green, yellow, and red zone values.
 179 #define assert_zone_constraints_gyr(green, yellow, red)                 \
 180   do {                                                                  \
 181     size_t azc_gyr_green = (green);                                     \
 182     size_t azc_gyr_yellow = (yellow);                                   \
 183     size_t azc_gyr_red = (red);                                         \
 184     assert_zone_constraints_gy(azc_gyr_green, azc_gyr_yellow);          \
 185     assert(azc_gyr_red <= max_red_zone,                                 \
 186            "red exceeds max: " SIZE_FORMAT, azc_gyr_red);               \
 187     assert(azc_gyr_yellow <= azc_gyr_red,                               \
 188            "yellow (" SIZE_FORMAT ") exceeds red (" SIZE_FORMAT ")",    \
 189            azc_gyr_yellow, azc_gyr_red);                                \
 190   } while (0)
 191 
 192 // Logging tag sequence for refinement control updates.
 193 #define CTRL_TAGS gc, ergo, refine
 194 
 195 // For logging zone values, ensuring consistency of level and tags.
 196 #define LOG_ZONES(...) log_debug( CTRL_TAGS )(__VA_ARGS__)
 197 
 198 static size_t buffers_to_cards(size_t value) {
 199   return value * G1UpdateBufferSize;
 200 }
 201 
 202 // Package for pair of refinement thread activation and deactivation
 203 // thresholds.  The activation and deactivation levels are resp. the first
 204 // and second values of the pair.
 205 typedef Pair<size_t, size_t> Thresholds;
 206 inline size_t activation_level(const Thresholds& t) { return t.first; }
 207 inline size_t deactivation_level(const Thresholds& t) { return t.second; }
 208 
 209 static Thresholds calc_thresholds(size_t green_zone,
 210                                   size_t yellow_zone,
 211                                   uint worker_id) {
 212   double yellow_size = yellow_zone - green_zone;
 213   double step = yellow_size / G1ConcurrentRefine::max_num_threads();
 214   if (worker_id == 0) {
 215     // Potentially activate worker 0 more aggressively, to keep
 216     // available buffers near green_zone value.  When yellow_size is
 217     // large we don't want to allow a full step to accumulate before
 218     // doing any processing, as that might lead to significantly more
 219     // than green_zone buffers to be processed during pause.  So limit
 220     // to an extra half buffer per pause-time processing thread.
 221     step = MIN2(step, buffers_to_cards(ParallelGCThreads) / 2.0);
 222   }
 223   size_t activate_offset = static_cast<size_t>(ceil(step * (worker_id + 1)));
 224   size_t deactivate_offset = static_cast<size_t>(floor(step * worker_id));
 225   return Thresholds(green_zone + activate_offset,
 226                     green_zone + deactivate_offset);
 227 }
 228 
 229 G1ConcurrentRefine::G1ConcurrentRefine(size_t green_zone,
 230                                        size_t yellow_zone,
 231                                        size_t red_zone,
 232                                        size_t min_yellow_zone_size) :
 233   _thread_control(),
 234   _green_zone(green_zone),
 235   _yellow_zone(yellow_zone),
 236   _red_zone(red_zone),
 237   _min_yellow_zone_size(min_yellow_zone_size)
 238 {
 239   assert_zone_constraints_gyr(green_zone, yellow_zone, red_zone);
 240 }
 241 
 242 jint G1ConcurrentRefine::initialize() {
 243   return _thread_control.initialize(this, max_num_threads());
 244 }
 245 
 246 static size_t calc_min_yellow_zone_size() {
 247   size_t step = buffers_to_cards(G1ConcRefinementThresholdStep);
 248   uint n_workers = G1ConcurrentRefine::max_num_threads();
 249   if ((max_yellow_zone / step) < n_workers) {
 250     return max_yellow_zone;
 251   } else {
 252     return step * n_workers;
 253   }
 254 }
 255 
 256 static size_t calc_init_green_zone() {
 257   size_t green = G1ConcRefinementGreenZone;
 258   if (FLAG_IS_DEFAULT(G1ConcRefinementGreenZone)) {
 259     green = ParallelGCThreads;
 260   }
 261   green = buffers_to_cards(green);
 262   return MIN2(green, max_green_zone);
 263 }
 264 
 265 static size_t calc_init_yellow_zone(size_t green, size_t min_size) {
 266   size_t config = buffers_to_cards(G1ConcRefinementYellowZone);
 267   size_t size = 0;
 268   if (FLAG_IS_DEFAULT(G1ConcRefinementYellowZone)) {
 269     size = green * 2;
 270   } else if (green < config) {
 271     size = config - green;
 272   }
 273   size = MAX2(size, min_size);
 274   size = MIN2(size, max_yellow_zone);
 275   return MIN2(green + size, max_yellow_zone);
 276 }
 277 
 278 static size_t calc_init_red_zone(size_t green, size_t yellow) {
 279   size_t size = yellow - green;
 280   if (!FLAG_IS_DEFAULT(G1ConcRefinementRedZone)) {
 281     size_t config = buffers_to_cards(G1ConcRefinementRedZone);
 282     if (yellow < config) {
 283       size = MAX2(size, config - yellow);
 284     }
 285   }
 286   return MIN2(yellow + size, max_red_zone);
 287 }
 288 
 289 G1ConcurrentRefine* G1ConcurrentRefine::create(jint* ecode) {
 290   size_t min_yellow_zone_size = calc_min_yellow_zone_size();
 291   size_t green_zone = calc_init_green_zone();
 292   size_t yellow_zone = calc_init_yellow_zone(green_zone, min_yellow_zone_size);
 293   size_t red_zone = calc_init_red_zone(green_zone, yellow_zone);
 294 
 295   LOG_ZONES("Initial Refinement Zones: "
 296             "green: " SIZE_FORMAT ", "
 297             "yellow: " SIZE_FORMAT ", "
 298             "red: " SIZE_FORMAT ", "
 299             "min yellow size: " SIZE_FORMAT,
 300             green_zone, yellow_zone, red_zone, min_yellow_zone_size);
 301 
 302   G1ConcurrentRefine* cr = new G1ConcurrentRefine(green_zone,
 303                                                   yellow_zone,
 304                                                   red_zone,
 305                                                   min_yellow_zone_size);
 306 
 307   if (cr == NULL) {
 308     *ecode = JNI_ENOMEM;
 309     vm_shutdown_during_initialization("Could not create G1ConcurrentRefine");
 310     return NULL;
 311   }
 312 
 313   *ecode = cr->initialize();
 314   return cr;
 315 }
 316 
 317 void G1ConcurrentRefine::stop() {
 318   _thread_control.stop();
 319 }
 320 
 321 G1ConcurrentRefine::~G1ConcurrentRefine() {
 322 }
 323 
 324 void G1ConcurrentRefine::threads_do(ThreadClosure *tc) {
 325   _thread_control.worker_threads_do(tc);
 326 }
 327 
 328 uint G1ConcurrentRefine::max_num_threads() {
 329   return G1ConcRefinementThreads;
 330 }
 331 
 332 void G1ConcurrentRefine::print_threads_on(outputStream* st) const {
 333   _thread_control.print_on(st);
 334 }
 335 
 336 static size_t calc_new_green_zone(size_t green,
 337                                   double logged_cards_scan_time,
 338                                   size_t processed_logged_cards,
 339                                   double goal_ms) {
 340   // Adjust green zone based on whether we're meeting the time goal.
 341   // Limit to max_green_zone.
 342   const double inc_k = 1.1, dec_k = 0.9;
 343   if (logged_cards_scan_time > goal_ms) {
 344     if (green > 0) {
 345       green = static_cast<size_t>(green * dec_k);
 346     }
 347   } else if (logged_cards_scan_time < goal_ms &&
 348              processed_logged_cards > green) {
 349     green = static_cast<size_t>(MAX2(green * inc_k, green + 1.0));
 350     green = MIN2(green, max_green_zone);
 351   }
 352   return green;
 353 }
 354 
 355 static size_t calc_new_yellow_zone(size_t green, size_t min_yellow_size) {
 356   size_t size = green * 2;
 357   size = MAX2(size, min_yellow_size);
 358   return MIN2(green + size, max_yellow_zone);
 359 }
 360 
 361 static size_t calc_new_red_zone(size_t green, size_t yellow) {
 362   return MIN2(yellow + (yellow - green), max_red_zone);
 363 }
 364 
 365 void G1ConcurrentRefine::update_zones(double logged_cards_scan_time,
 366                                       size_t processed_logged_cards,
 367                                       double goal_ms) {
 368   log_trace( CTRL_TAGS )("Updating Refinement Zones: "
 369                          "logged cards scan time: %.3fms, "
 370                          "processed cards: " SIZE_FORMAT ", "
 371                          "goal time: %.3fms",
 372                          logged_cards_scan_time,
 373                          processed_logged_cards,
 374                          goal_ms);
 375 
 376   _green_zone = calc_new_green_zone(_green_zone,
 377                                     logged_cards_scan_time,
 378                                     processed_logged_cards,
 379                                     goal_ms);
 380   _yellow_zone = calc_new_yellow_zone(_green_zone, _min_yellow_zone_size);
 381   _red_zone = calc_new_red_zone(_green_zone, _yellow_zone);
 382 
 383   assert_zone_constraints_gyr(_green_zone, _yellow_zone, _red_zone);
 384   LOG_ZONES("Updated Refinement Zones: "
 385             "green: " SIZE_FORMAT ", "
 386             "yellow: " SIZE_FORMAT ", "
 387             "red: " SIZE_FORMAT,
 388             _green_zone, _yellow_zone, _red_zone);
 389 }
 390 
 391 void G1ConcurrentRefine::adjust(double logged_cards_scan_time,
 392                                 size_t processed_logged_cards,
 393                                 double goal_ms) {
 394   G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
 395 
 396   if (G1UseAdaptiveConcRefinement) {
 397     update_zones(logged_cards_scan_time, processed_logged_cards, goal_ms);
 398 
 399     // Change the barrier params
 400     if (max_num_threads() == 0) {
 401       // Disable dcqs notification when there are no threads to notify.
 402       dcqs.set_process_cards_threshold(G1DirtyCardQueueSet::ProcessCardsThresholdNever);
 403     } else {
 404       // Worker 0 is the primary; wakeup is via dcqs notification.
 405       STATIC_ASSERT(max_yellow_zone <= INT_MAX);
 406       size_t activate = activation_threshold(0);
 407       dcqs.set_process_cards_threshold(activate);
 408     }
 409     dcqs.set_max_cards(red_zone());
 410   }
 411 
 412   size_t curr_queue_size = dcqs.num_cards();
 413   if ((dcqs.max_cards() > 0) &&
 414       (curr_queue_size >= yellow_zone())) {
 415     dcqs.set_max_cards_padding(curr_queue_size);
 416   } else {
 417     dcqs.set_max_cards_padding(0);
 418   }
 419   dcqs.notify_if_necessary();
 420 }
 421 
 422 G1ConcurrentRefine::RefinementStats G1ConcurrentRefine::total_refinement_stats() const {
 423   struct CollectData : public ThreadClosure {
 424     Tickspan _total_time;
 425     size_t _total_cards;
 426     CollectData() : _total_time(), _total_cards(0) {}
 427     virtual void do_thread(Thread* t) {
 428       G1ConcurrentRefineThread* crt = static_cast<G1ConcurrentRefineThread*>(t);
 429       _total_time += crt->total_refinement_time();
 430       _total_cards += crt->total_refined_cards();
 431     }
 432   } collector;
 433   // Cast away const so we can call non-modifying closure on threads.
 434   const_cast<G1ConcurrentRefine*>(this)->threads_do(&collector);
 435   return RefinementStats(collector._total_time, collector._total_cards);
 436 }
 437 
 438 size_t G1ConcurrentRefine::activation_threshold(uint worker_id) const {
 439   Thresholds thresholds = calc_thresholds(_green_zone, _yellow_zone, worker_id);
 440   return activation_level(thresholds);
 441 }
 442 
 443 size_t G1ConcurrentRefine::deactivation_threshold(uint worker_id) const {
 444   Thresholds thresholds = calc_thresholds(_green_zone, _yellow_zone, worker_id);
 445   return deactivation_level(thresholds);
 446 }
 447 
 448 uint G1ConcurrentRefine::worker_id_offset() {
 449   return G1DirtyCardQueueSet::num_par_ids();
 450 }
 451 
 452 void G1ConcurrentRefine::maybe_activate_more_threads(uint worker_id, size_t num_cur_cards) {
 453   if (num_cur_cards > activation_threshold(worker_id + 1)) {
 454     _thread_control.maybe_activate_next(worker_id);
 455   }
 456 }
 457 
 458 bool G1ConcurrentRefine::do_refinement_step(uint worker_id,
 459                                             size_t* total_refined_cards) {
 460   G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
 461 
 462   size_t curr_cards = dcqs.num_cards();
 463   // If the number of the cards falls down into the yellow zone,
 464   // that means that the transition period after the evacuation pause has ended.
 465   // Since the value written to the DCQS is the same for all threads, there is no
 466   // need to synchronize.
 467   if (dcqs.max_cards_padding() > 0 && curr_cards <= yellow_zone()) {
 468     dcqs.set_max_cards_padding(0);
 469   }
 470 
 471   maybe_activate_more_threads(worker_id, curr_cards);
 472 
 473   // Process the next buffer, if there are enough left.
 474   return dcqs.refine_completed_buffer_concurrently(worker_id + worker_id_offset(),
 475                                                    deactivation_threshold(worker_id),
 476                                                    total_refined_cards);
 477 }