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