rev 53582 : imported patch rename

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