1 /*
   2  * Copyright (c) 2001, 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/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 // Package for pair of refinement thread activation and deactivation
 194 // thresholds.  The activation and deactivation levels are resp. the first
 195 // and second values of the pair.
 196 typedef Pair<size_t, size_t> Thresholds;
 197 inline size_t activation_level(const Thresholds& t) { return t.first; }
 198 inline size_t deactivation_level(const Thresholds& t) { return t.second; }
 199 
 200 static Thresholds calc_thresholds(size_t green_zone,
 201                                   size_t yellow_zone,
 202                                   uint worker_id) {
 203   double yellow_size = yellow_zone - green_zone;
 204   double step = yellow_size / G1ConcurrentRefine::max_num_threads();
 205   if (worker_id == 0) {
 206     // Potentially activate worker 0 more aggressively, to keep
 207     // available buffers near green_zone value.  When yellow_size is
 208     // large we don't want to allow a full step to accumulate before
 209     // doing any processing, as that might lead to significantly more
 210     // than green_zone buffers to be processed during scanning.
 211     step = MIN2(step, ParallelGCThreads / 2.0);
 212   }
 213   size_t activate_offset = static_cast<size_t>(ceil(step * (worker_id + 1)));
 214   size_t deactivate_offset = static_cast<size_t>(floor(step * worker_id));
 215   return Thresholds(green_zone + activate_offset,
 216                     green_zone + deactivate_offset);
 217 }
 218 
 219 G1ConcurrentRefine::G1ConcurrentRefine(size_t green_zone,
 220                                        size_t yellow_zone,
 221                                        size_t red_zone,
 222                                        size_t min_yellow_zone_size) :
 223   _thread_control(),
 224   _green_zone(green_zone),
 225   _yellow_zone(yellow_zone),
 226   _red_zone(red_zone),
 227   _min_yellow_zone_size(min_yellow_zone_size)
 228 {
 229   assert_zone_constraints_gyr(green_zone, yellow_zone, red_zone);
 230 }
 231 
 232 jint G1ConcurrentRefine::initialize() {
 233   return _thread_control.initialize(this, max_num_threads());
 234 }
 235 
 236 static size_t buffers_to_cards(size_t value) {
 237   return value * G1UpdateBufferSize;
 238 }
 239 
 240 static size_t calc_min_yellow_zone_size() {
 241   size_t step = buffers_to_cards(G1ConcRefinementThresholdStep);
 242   uint n_workers = G1ConcurrentRefine::max_num_threads();
 243   if ((max_yellow_zone / step) < n_workers) {
 244     return max_yellow_zone;
 245   } else {
 246     return step * n_workers;
 247   }
 248 }
 249 
 250 static size_t calc_init_green_zone() {
 251   size_t green = G1ConcRefinementGreenZone;
 252   if (FLAG_IS_DEFAULT(G1ConcRefinementGreenZone)) {
 253     green = ParallelGCThreads;
 254   }
 255   green = buffers_to_cards(green);
 256   return MIN2(green, max_green_zone);
 257 }
 258 
 259 static size_t calc_init_yellow_zone(size_t green, size_t min_size) {
 260   size_t config = buffers_to_cards(G1ConcRefinementYellowZone);
 261   size_t size = 0;
 262   if (FLAG_IS_DEFAULT(G1ConcRefinementYellowZone)) {
 263     size = green * 2;
 264   } else if (green < config) {
 265     size = config - green;
 266   }
 267   size = MAX2(size, min_size);
 268   size = MIN2(size, max_yellow_zone);
 269   return MIN2(green + size, max_yellow_zone);
 270 }
 271 
 272 static size_t calc_init_red_zone(size_t green, size_t yellow) {
 273   size_t size = yellow - green;
 274   if (!FLAG_IS_DEFAULT(G1ConcRefinementRedZone)) {
 275     size_t config = buffers_to_cards(G1ConcRefinementRedZone);
 276     if (yellow < config) {
 277       size = MAX2(size, config - yellow);
 278     }
 279   }
 280   return MIN2(yellow + size, max_red_zone);
 281 }
 282 
 283 G1ConcurrentRefine* G1ConcurrentRefine::create(jint* ecode) {
 284   size_t min_yellow_zone_size = calc_min_yellow_zone_size();
 285   size_t green_zone = calc_init_green_zone();
 286   size_t yellow_zone = calc_init_yellow_zone(green_zone, min_yellow_zone_size);
 287   size_t red_zone = calc_init_red_zone(green_zone, yellow_zone);
 288 
 289   LOG_ZONES("Initial Refinement Zones: "
 290             "green: " SIZE_FORMAT ", "
 291             "yellow: " SIZE_FORMAT ", "
 292             "red: " SIZE_FORMAT ", "
 293             "min yellow size: " SIZE_FORMAT,
 294             green_zone, yellow_zone, red_zone, min_yellow_zone_size);
 295 
 296   G1ConcurrentRefine* cr = new G1ConcurrentRefine(green_zone,
 297                                                   yellow_zone,
 298                                                   red_zone,
 299                                                   min_yellow_zone_size);
 300 
 301   if (cr == NULL) {
 302     *ecode = JNI_ENOMEM;
 303     vm_shutdown_during_initialization("Could not create G1ConcurrentRefine");
 304     return NULL;
 305   }
 306 
 307   *ecode = cr->initialize();
 308   return cr;
 309 }
 310 
 311 void G1ConcurrentRefine::stop() {
 312   _thread_control.stop();
 313 }
 314 
 315 G1ConcurrentRefine::~G1ConcurrentRefine() {
 316 }
 317 
 318 void G1ConcurrentRefine::threads_do(ThreadClosure *tc) {
 319   _thread_control.worker_threads_do(tc);
 320 }
 321 
 322 uint G1ConcurrentRefine::max_num_threads() {
 323   return G1ConcRefinementThreads;
 324 }
 325 
 326 void G1ConcurrentRefine::print_threads_on(outputStream* st) const {
 327   _thread_control.print_on(st);
 328 }
 329 
 330 static size_t calc_new_green_zone(size_t green,
 331                                   double logged_cards_scan_time,
 332                                   size_t processed_logged_cards,
 333                                   double goal_ms) {
 334   // Adjust green zone based on whether we're meeting the time goal.
 335   // Limit to max_green_zone.
 336   const double inc_k = 1.1, dec_k = 0.9;
 337   if (logged_cards_scan_time > goal_ms) {
 338     if (green > 0) {
 339       green = static_cast<size_t>(green * dec_k);
 340     }
 341   } else if (logged_cards_scan_time < goal_ms &&
 342              processed_logged_cards > green) {
 343     green = static_cast<size_t>(MAX2(green * inc_k, green + 1.0));
 344     green = MIN2(green, max_green_zone);
 345   }
 346   return green;
 347 }
 348 
 349 static size_t calc_new_yellow_zone(size_t green, size_t min_yellow_size) {
 350   size_t size = green * 2;
 351   size = MAX2(size, min_yellow_size);
 352   return MIN2(green + size, max_yellow_zone);
 353 }
 354 
 355 static size_t calc_new_red_zone(size_t green, size_t yellow) {
 356   return MIN2(yellow + (yellow - green), max_red_zone);
 357 }
 358 
 359 void G1ConcurrentRefine::update_zones(double logged_cards_scan_time,
 360                                       size_t processed_logged_cards,
 361                                       double goal_ms) {
 362   log_trace( CTRL_TAGS )("Updating Refinement Zones: "
 363                          "logged cards scan time: %.3fms, "
 364                          "processed cards: " SIZE_FORMAT ", "
 365                          "goal time: %.3fms",
 366                          logged_cards_scan_time,
 367                          processed_logged_cards,
 368                          goal_ms);
 369 
 370   _green_zone = calc_new_green_zone(_green_zone,
 371                                     logged_cards_scan_time,
 372                                     processed_logged_cards,
 373                                     goal_ms);
 374   _yellow_zone = calc_new_yellow_zone(_green_zone, _min_yellow_zone_size);
 375   _red_zone = calc_new_red_zone(_green_zone, _yellow_zone);
 376 
 377   assert_zone_constraints_gyr(_green_zone, _yellow_zone, _red_zone);
 378   LOG_ZONES("Updated Refinement Zones: "
 379             "green: " SIZE_FORMAT ", "
 380             "yellow: " SIZE_FORMAT ", "
 381             "red: " SIZE_FORMAT,
 382             _green_zone, _yellow_zone, _red_zone);
 383 }
 384 
 385 void G1ConcurrentRefine::adjust(double logged_cards_scan_time,
 386                                 size_t processed_logged_cards,
 387                                 double goal_ms) {
 388   G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
 389 
 390   if (G1UseAdaptiveConcRefinement) {
 391     update_zones(logged_cards_scan_time, processed_logged_cards, goal_ms);
 392 
 393     // Change the barrier params
 394     if (max_num_threads() == 0) {
 395       // Disable dcqs notification when there are no threads to notify.
 396       dcqs.set_process_cards_threshold(G1DirtyCardQueueSet::ProcessCardsThresholdNever);
 397     } else {
 398       // Worker 0 is the primary; wakeup is via dcqs notification.
 399       STATIC_ASSERT(max_yellow_zone <= INT_MAX);
 400       size_t activate = activation_threshold(0);
 401       dcqs.set_process_cards_threshold(activate);
 402     }
 403     dcqs.set_max_cards(red_zone());
 404   }
 405 
 406   size_t curr_queue_size = dcqs.num_cards();
 407   if ((dcqs.max_cards() > 0) &&
 408       (curr_queue_size >= yellow_zone())) {
 409     dcqs.set_max_cards_padding(curr_queue_size);
 410   } else {
 411     dcqs.set_max_cards_padding(0);
 412   }
 413   dcqs.notify_if_necessary();
 414 }
 415 
 416 G1ConcurrentRefine::RefinementStats G1ConcurrentRefine::total_refinement_stats() const {
 417   struct CollectData : public ThreadClosure {
 418     Tickspan _total_time;
 419     size_t _total_cards;
 420     CollectData() : _total_time(), _total_cards(0) {}
 421     virtual void do_thread(Thread* t) {
 422       G1ConcurrentRefineThread* crt = static_cast<G1ConcurrentRefineThread*>(t);
 423       _total_time += crt->total_refinement_time();
 424       _total_cards += crt->total_refined_cards();
 425     }
 426   } collector;
 427   // Cast away const so we can call non-modifying closure on threads.
 428   const_cast<G1ConcurrentRefine*>(this)->threads_do(&collector);
 429   return RefinementStats(collector._total_time, collector._total_cards);
 430 }
 431 
 432 size_t G1ConcurrentRefine::activation_threshold(uint worker_id) const {
 433   Thresholds thresholds = calc_thresholds(_green_zone, _yellow_zone, worker_id);
 434   return activation_level(thresholds);
 435 }
 436 
 437 size_t G1ConcurrentRefine::deactivation_threshold(uint worker_id) const {
 438   Thresholds thresholds = calc_thresholds(_green_zone, _yellow_zone, worker_id);
 439   return deactivation_level(thresholds);
 440 }
 441 
 442 uint G1ConcurrentRefine::worker_id_offset() {
 443   return G1DirtyCardQueueSet::num_par_ids();
 444 }
 445 
 446 void G1ConcurrentRefine::maybe_activate_more_threads(uint worker_id, size_t num_cur_buffers) {
 447   if (num_cur_buffers > activation_threshold(worker_id + 1)) {
 448     _thread_control.maybe_activate_next(worker_id);
 449   }
 450 }
 451 
 452 bool G1ConcurrentRefine::do_refinement_step(uint worker_id,
 453                                             size_t* total_refined_cards) {
 454   G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
 455 
 456   size_t curr_cards = dcqs.num_cards();
 457   // If the number of the cards falls down into the yellow zone,
 458   // that means that the transition period after the evacuation pause has ended.
 459   // Since the value written to the DCQS is the same for all threads, there is no
 460   // need to synchronize.
 461   if (dcqs.max_cards_padding() > 0 && curr_cards <= yellow_zone()) {
 462     dcqs.set_max_cards_padding(0);
 463   }
 464 
 465   maybe_activate_more_threads(worker_id, curr_cards);
 466 
 467   // Process the next buffer, if there are enough left.
 468   return dcqs.refine_completed_buffer_concurrently(worker_id + worker_id_offset(),
 469                                                    deactivation_threshold(worker_id),
 470                                                    total_refined_cards);
 471 }