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