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