1 /*
   2  * Copyright (c) 2001, 2016, 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/concurrentG1Refine.hpp"
  28 #include "gc/g1/concurrentG1RefineThread.hpp"
  29 #include "gc/g1/g1YoungRemSetSamplingThread.hpp"
  30 #include "logging/log.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 // Arbitrary but large limits, to simplify some of the zone calculations.
  39 // The general idea is to allow expressions like
  40 //   MIN2(x OP y, max_XXX_zone)
  41 // without needing to check for overflow in "x OP y", because the
  42 // ranges for x and y have been restricted.
  43 STATIC_ASSERT(sizeof(LP64_ONLY(jint) NOT_LP64(jshort)) <= (sizeof(size_t)/2));
  44 const size_t max_yellow_zone = LP64_ONLY(max_jint) NOT_LP64(max_jshort);
  45 const size_t max_green_zone = max_yellow_zone / 2;
  46 const size_t max_red_zone = INT_MAX; // For dcqs.set_max_completed_queue.
  47 STATIC_ASSERT(max_yellow_zone <= max_red_zone);
  48 
  49 // Range check assertions for green zone values.
  50 #define assert_zone_constraints_g(green)                        \
  51   do {                                                          \
  52     size_t azc_g_green = (green);                               \
  53     assert(azc_g_green <= max_green_zone,                       \
  54            "green exceeds max: " SIZE_FORMAT, azc_g_green);     \
  55   } while (0)
  56 
  57 // Range check assertions for green and yellow zone values.
  58 #define assert_zone_constraints_gy(green, yellow)                       \
  59   do {                                                                  \
  60     size_t azc_gy_green = (green);                                      \
  61     size_t azc_gy_yellow = (yellow);                                    \
  62     assert_zone_constraints_g(azc_gy_green);                            \
  63     assert(azc_gy_yellow <= max_yellow_zone,                            \
  64            "yellow exceeds max: " SIZE_FORMAT, azc_gy_yellow);          \
  65     assert(azc_gy_green <= azc_gy_yellow,                               \
  66            "green (" SIZE_FORMAT ") exceeds yellow (" SIZE_FORMAT ")",  \
  67            azc_gy_green, azc_gy_yellow);                                \
  68   } while (0)
  69 
  70 // Range check assertions for green, yellow, and red zone values.
  71 #define assert_zone_constraints_gyr(green, yellow, red)                 \
  72   do {                                                                  \
  73     size_t azc_gyr_green = (green);                                     \
  74     size_t azc_gyr_yellow = (yellow);                                   \
  75     size_t azc_gyr_red = (red);                                         \
  76     assert_zone_constraints_gy(azc_gyr_green, azc_gyr_yellow);          \
  77     assert(azc_gyr_red <= max_red_zone,                                 \
  78            "red exceeds max: " SIZE_FORMAT, azc_gyr_red);               \
  79     assert(azc_gyr_yellow <= azc_gyr_red,                               \
  80            "yellow (" SIZE_FORMAT ") exceeds red (" SIZE_FORMAT ")",    \
  81            azc_gyr_yellow, azc_gyr_red);                                \
  82   } while (0)
  83 
  84 // Logging tag sequence for refinement control updates.
  85 #define CTRL_TAGS gc, ergo, refine
  86 
  87 // For logging zone values, ensuring consistency of level and tags.
  88 #define LOG_ZONES(...) log_debug( CTRL_TAGS )(__VA_ARGS__)
  89 
  90 // Package for pair of refinement thread activation and deactivation
  91 // thresholds.  The activation and deactivation levels are resp. the first
  92 // and second values of the pair.
  93 typedef Pair<size_t, size_t> Thresholds;
  94 inline size_t activation_level(const Thresholds& t) { return t.first; }
  95 inline size_t deactivation_level(const Thresholds& t) { return t.second; }
  96 
  97 static Thresholds calc_thresholds(size_t green_zone,
  98                                   size_t yellow_zone,
  99                                   uint worker_i) {
 100   double yellow_size = yellow_zone - green_zone;
 101   double step = yellow_size / ConcurrentG1Refine::thread_num();
 102   if (worker_i == 0) {
 103     // Potentially activate worker 0 more aggressively, to keep
 104     // available buffers near green_zone value.  When yellow_size is
 105     // large we don't want to allow a full step to accumulate before
 106     // doing any processing, as that might lead to significantly more
 107     // than green_zone buffers to be processed by update_rs.
 108     step = MIN2(step, ParallelGCThreads / 2.0);
 109   }
 110   size_t activate_offset = static_cast<size_t>(ceil(step * (worker_i + 1)));
 111   size_t deactivate_offset = static_cast<size_t>(floor(step * worker_i));
 112   return Thresholds(green_zone + activate_offset,
 113                     green_zone + deactivate_offset);
 114 }
 115 
 116 ConcurrentG1Refine::ConcurrentG1Refine(size_t green_zone,
 117                                        size_t yellow_zone,
 118                                        size_t red_zone,
 119                                        size_t min_yellow_zone_size) :
 120   _threads(NULL),
 121   _sample_thread(NULL),
 122   _n_worker_threads(thread_num()),
 123   _green_zone(green_zone),
 124   _yellow_zone(yellow_zone),
 125   _red_zone(red_zone),
 126   _min_yellow_zone_size(min_yellow_zone_size)
 127 {
 128   assert_zone_constraints_gyr(green_zone, yellow_zone, red_zone);
 129 }
 130 
 131 static size_t calc_min_yellow_zone_size() {
 132   size_t step = G1ConcRefinementThresholdStep;
 133   uint n_workers = ConcurrentG1Refine::thread_num();
 134   if ((max_yellow_zone / step) < n_workers) {
 135     return max_yellow_zone;
 136   } else {
 137     return step * n_workers;
 138   }
 139 }
 140 
 141 static size_t calc_init_green_zone() {
 142   size_t green = G1ConcRefinementGreenZone;
 143   if (FLAG_IS_DEFAULT(G1ConcRefinementGreenZone)) {
 144     green = ParallelGCThreads;
 145   }
 146   return MIN2(green, max_green_zone);
 147 }
 148 
 149 static size_t calc_init_yellow_zone(size_t green, size_t min_size) {
 150   size_t config = G1ConcRefinementYellowZone;
 151   size_t size = 0;
 152   if (FLAG_IS_DEFAULT(G1ConcRefinementYellowZone)) {
 153     size = green * 2;
 154   } else if (green < config) {
 155     size = config - green;
 156   }
 157   size = MAX2(size, min_size);
 158   size = MIN2(size, max_yellow_zone);
 159   return MIN2(green + size, max_yellow_zone);
 160 }
 161 
 162 static size_t calc_init_red_zone(size_t green, size_t yellow) {
 163   size_t size = yellow - green;
 164   if (!FLAG_IS_DEFAULT(G1ConcRefinementRedZone)) {
 165     size_t config = G1ConcRefinementRedZone;
 166     if (yellow < config) {
 167       size = MAX2(size, config - yellow);
 168     }
 169   }
 170   return MIN2(yellow + size, max_red_zone);
 171 }
 172 
 173 ConcurrentG1Refine* ConcurrentG1Refine::create(CardTableEntryClosure* refine_closure,
 174                                                jint* ecode) {
 175   size_t min_yellow_zone_size = calc_min_yellow_zone_size();
 176   size_t green_zone = calc_init_green_zone();
 177   size_t yellow_zone = calc_init_yellow_zone(green_zone, min_yellow_zone_size);
 178   size_t red_zone = calc_init_red_zone(green_zone, yellow_zone);
 179 
 180   LOG_ZONES("Initial Refinement Zones: "
 181             "green: " SIZE_FORMAT ", "
 182             "yellow: " SIZE_FORMAT ", "
 183             "red: " SIZE_FORMAT ", "
 184             "min yellow size: " SIZE_FORMAT,
 185             green_zone, yellow_zone, red_zone, min_yellow_zone_size);
 186 
 187   ConcurrentG1Refine* cg1r = new ConcurrentG1Refine(green_zone,
 188                                                     yellow_zone,
 189                                                     red_zone,
 190                                                     min_yellow_zone_size);
 191 
 192   if (cg1r == NULL) {
 193     *ecode = JNI_ENOMEM;
 194     vm_shutdown_during_initialization("Could not create ConcurrentG1Refine");
 195     return NULL;
 196   }
 197 
 198   cg1r->_threads = NEW_C_HEAP_ARRAY_RETURN_NULL(ConcurrentG1RefineThread*, cg1r->_n_worker_threads, mtGC);
 199   if (cg1r->_threads == NULL) {
 200     *ecode = JNI_ENOMEM;
 201     vm_shutdown_during_initialization("Could not allocate an array for ConcurrentG1RefineThread");
 202     return NULL;
 203   }
 204 
 205   uint worker_id_offset = DirtyCardQueueSet::num_par_ids();
 206 
 207   ConcurrentG1RefineThread *next = NULL;
 208   for (uint i = cg1r->_n_worker_threads - 1; i != UINT_MAX; i--) {
 209     Thresholds thresholds = calc_thresholds(green_zone, yellow_zone, i);
 210     ConcurrentG1RefineThread* t =
 211       new ConcurrentG1RefineThread(cg1r,
 212                                    next,
 213                                    refine_closure,
 214                                    worker_id_offset,
 215                                    i,
 216                                    activation_level(thresholds),
 217                                    deactivation_level(thresholds));
 218     assert(t != NULL, "Conc refine should have been created");
 219     if (t->osthread() == NULL) {
 220       *ecode = JNI_ENOMEM;
 221       vm_shutdown_during_initialization("Could not create ConcurrentG1RefineThread");
 222       return NULL;
 223     }
 224 
 225     assert(t->cg1r() == cg1r, "Conc refine thread should refer to this");
 226     cg1r->_threads[i] = t;
 227     next = t;
 228   }
 229 
 230   cg1r->_sample_thread = new G1YoungRemSetSamplingThread();
 231   if (cg1r->_sample_thread->osthread() == NULL) {
 232     *ecode = JNI_ENOMEM;
 233     vm_shutdown_during_initialization("Could not create G1YoungRemSetSamplingThread");
 234     return NULL;
 235   }
 236 
 237   *ecode = JNI_OK;
 238   return cg1r;
 239 }
 240 
 241 void ConcurrentG1Refine::stop() {
 242   for (uint i = 0; i < _n_worker_threads; i++) {
 243     _threads[i]->stop();
 244   }
 245   _sample_thread->stop();
 246 }
 247 
 248 void ConcurrentG1Refine::update_thread_thresholds() {
 249   for (uint i = 0; i < _n_worker_threads; i++) {
 250     Thresholds thresholds = calc_thresholds(_green_zone, _yellow_zone, i);
 251     _threads[i]->update_thresholds(activation_level(thresholds),
 252                                    deactivation_level(thresholds));
 253   }
 254 }
 255 
 256 ConcurrentG1Refine::~ConcurrentG1Refine() {
 257   for (uint i = 0; i < _n_worker_threads; i++) {
 258     delete _threads[i];
 259   }
 260   FREE_C_HEAP_ARRAY(ConcurrentG1RefineThread*, _threads);
 261 
 262   delete _sample_thread;
 263 }
 264 
 265 void ConcurrentG1Refine::threads_do(ThreadClosure *tc) {
 266   worker_threads_do(tc);
 267   tc->do_thread(_sample_thread);
 268 }
 269 
 270 void ConcurrentG1Refine::worker_threads_do(ThreadClosure * tc) {
 271   for (uint i = 0; i < _n_worker_threads; i++) {
 272     tc->do_thread(_threads[i]);
 273   }
 274 }
 275 
 276 uint ConcurrentG1Refine::thread_num() {
 277   return G1ConcRefinementThreads;
 278 }
 279 
 280 void ConcurrentG1Refine::print_worker_threads_on(outputStream* st) const {
 281   for (uint i = 0; i < _n_worker_threads; ++i) {
 282     _threads[i]->print_on(st);
 283     st->cr();
 284   }
 285   _sample_thread->print_on(st);
 286   st->cr();
 287 }
 288 
 289 static size_t calc_new_green_zone(size_t green,
 290                                   double update_rs_time,
 291                                   size_t update_rs_processed_buffers,
 292                                   double goal_ms) {
 293   // Adjust green zone based on whether we're meeting the time goal.
 294   // Limit to max_green_zone.
 295   const double inc_k = 1.1, dec_k = 0.9;
 296   if (update_rs_time > goal_ms) {
 297     if (green > 0) {
 298       green = static_cast<size_t>(green * dec_k);
 299     }
 300   } else if (update_rs_time < goal_ms &&
 301              update_rs_processed_buffers > green) {
 302     green = static_cast<size_t>(MAX2(green * inc_k, green + 1.0));
 303     green = MIN2(green, max_green_zone);
 304   }
 305   return green;
 306 }
 307 
 308 static size_t calc_new_yellow_zone(size_t green, size_t min_yellow_size) {
 309   size_t size = green * 2;
 310   size = MAX2(size, min_yellow_size);
 311   return MIN2(green + size, max_yellow_zone);
 312 }
 313 
 314 static size_t calc_new_red_zone(size_t green, size_t yellow) {
 315   return MIN2(yellow + (yellow - green), max_red_zone);
 316 }
 317 
 318 void ConcurrentG1Refine::update_zones(double update_rs_time,
 319                                       size_t update_rs_processed_buffers,
 320                                       double goal_ms) {
 321   log_trace( CTRL_TAGS )("Updating Refinement Zones: "
 322                          "update_rs time: %.3fms, "
 323                          "update_rs buffers: " SIZE_FORMAT ", "
 324                          "update_rs goal time: %.3fms",
 325                          update_rs_time,
 326                          update_rs_processed_buffers,
 327                          goal_ms);
 328 
 329   _green_zone = calc_new_green_zone(_green_zone,
 330                                     update_rs_time,
 331                                     update_rs_processed_buffers,
 332                                     goal_ms);
 333   _yellow_zone = calc_new_yellow_zone(_green_zone, _min_yellow_zone_size);
 334   _red_zone = calc_new_red_zone(_green_zone, _yellow_zone);
 335 
 336   assert_zone_constraints_gyr(_green_zone, _yellow_zone, _red_zone);
 337   LOG_ZONES("Updated Refinement Zones: "
 338             "green: " SIZE_FORMAT ", "
 339             "yellow: " SIZE_FORMAT ", "
 340             "red: " SIZE_FORMAT,
 341             _green_zone, _yellow_zone, _red_zone);
 342 }
 343 
 344 void ConcurrentG1Refine::adjust(double update_rs_time,
 345                                 size_t update_rs_processed_buffers,
 346                                 double goal_ms) {
 347   DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
 348 
 349   if (G1UseAdaptiveConcRefinement) {
 350     update_zones(update_rs_time, update_rs_processed_buffers, goal_ms);
 351     update_thread_thresholds();
 352 
 353     // Change the barrier params
 354     if (_n_worker_threads == 0) {
 355       // Disable dcqs notification when there are no threads to notify.
 356       dcqs.set_process_completed_threshold(INT_MAX);
 357     } else {
 358       // Worker 0 is the primary; wakeup is via dcqs notification.
 359       STATIC_ASSERT(max_yellow_zone <= INT_MAX);
 360       size_t activate = _threads[0]->activation_threshold();
 361       dcqs.set_process_completed_threshold((int)activate);
 362     }
 363     dcqs.set_max_completed_queue((int)red_zone());
 364   }
 365 
 366   size_t curr_queue_size = dcqs.completed_buffers_num();
 367   if (curr_queue_size >= yellow_zone()) {
 368     dcqs.set_completed_queue_padding(curr_queue_size);
 369   } else {
 370     dcqs.set_completed_queue_padding(0);
 371   }
 372   dcqs.notify_if_necessary();
 373 }