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