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 #ifndef SHARE_GC_G1_G1CONCURRENTREFINE_HPP
  26 #define SHARE_GC_G1_G1CONCURRENTREFINE_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "gc/g1/g1EpochSynchronizer.hpp"
  30 #include "utilities/globalDefinitions.hpp"
  31 #include "utilities/ticks.hpp"
  32 
  33 // Forward decl
  34 class G1ConcurrentRefine;
  35 class G1ConcurrentRefineThread;
  36 class outputStream;
  37 class ThreadClosure;
  38 
  39 // Helper class for refinement thread management. Used to start, stop and
  40 // iterate over them.
  41 class G1ConcurrentRefineThreadControl {
  42   G1ConcurrentRefine* _cr;
  43 
  44   G1ConcurrentRefineThread** _threads;
  45   uint _num_max_threads;
  46 
  47   // Create the refinement thread for the given worker id.
  48   // If initializing is true, ignore InjectGCWorkerCreationFailure.
  49   G1ConcurrentRefineThread* create_refinement_thread(uint worker_id, bool initializing);
  50 public:
  51   G1ConcurrentRefineThreadControl();
  52   ~G1ConcurrentRefineThreadControl();
  53 
  54   jint initialize(G1ConcurrentRefine* cr, uint num_max_threads);
  55 
  56   // If there is a "successor" thread that can be activated given the current id,
  57   // activate it.
  58   void maybe_activate_next(uint cur_worker_id);
  59 
  60   void print_on(outputStream* st) const;
  61   void worker_threads_do(ThreadClosure* tc);
  62   void stop();
  63 };
  64 
  65 // Controls refinement threads and their activation based on the number of
  66 // cards currently available in the global dirty card queue.
  67 // Refinement threads obtain work from the queue (a buffer at a time) based
  68 // on these thresholds. They are activated gradually based on the amount of
  69 // work to do.
  70 // Refinement thread n activates thread n+1 if the instance of this class determines there
  71 // is enough work available. Threads deactivate themselves if the current amount of
  72 // available cards falls below their individual threshold.
  73 class G1ConcurrentRefine : public CHeapObj<mtGC> {
  74   G1ConcurrentRefineThreadControl _thread_control;
  75   G1EpochSynchronizerCounters _synchronizer_counters;
  76   /*
  77    * The value of the completed dirty card queue length falls into one of 3 zones:
  78    * green, yellow, red. If the value is in [0, green) nothing is
  79    * done, the buffered cards are left unprocessed to enable the caching effect of the
  80    * dirtied cards. In the yellow zone [green, yellow) the concurrent refinement
  81    * threads are gradually activated. In [yellow, red) all threads are
  82    * running. If the length becomes red (max queue length) the mutators start
  83    * processing cards too.
  84    *
  85    * There are some interesting cases (when G1UseAdaptiveConcRefinement
  86    * is turned off):
  87    * 1) green = yellow = red = 0. In this case the mutator will process all
  88    *    cards. Except for those that are created by the deferred updates
  89    *    machinery during a collection.
  90    * 2) green = 0. Means no caching. Can be a good way to minimize the
  91    *    amount of time spent updating remembered sets during a collection.
  92    */
  93   size_t _green_zone;
  94   size_t _yellow_zone;
  95   size_t _red_zone;
  96   size_t _min_yellow_zone_size;
  97 
  98   G1ConcurrentRefine(size_t green_zone,
  99                      size_t yellow_zone,
 100                      size_t red_zone,
 101                      size_t min_yellow_zone_size);
 102 
 103   // Update green/yellow/red zone values based on how well goals are being met.
 104   void update_zones(double logged_cards_scan_time,
 105                     size_t processed_logged_cards,
 106                     double goal_ms);
 107 
 108   static uint worker_id_offset();
 109   void maybe_activate_more_threads(uint worker_id, size_t num_cur_cards);
 110 
 111   jint initialize();
 112 public:
 113   ~G1ConcurrentRefine();
 114 
 115   // Returns a G1ConcurrentRefine instance if succeeded to create/initialize the
 116   // G1ConcurrentRefine instance. Otherwise, returns NULL with error code.
 117   static G1ConcurrentRefine* create(jint* ecode);
 118 
 119   void stop();
 120 
 121   // Adjust refinement thresholds based on work done during the pause and the goal time.
 122   void adjust(double logged_cards_scan_time, size_t processed_logged_cards, double goal_ms);
 123 
 124   struct RefinementStats {
 125     Tickspan _time;
 126     size_t _cards;
 127     RefinementStats(Tickspan time, size_t cards) : _time(time), _cards(cards) {}
 128   };
 129 
 130   RefinementStats total_refinement_stats() const;
 131 
 132   // Cards in the dirty card queue set.
 133   size_t activation_threshold(uint worker_id) const;
 134   size_t deactivation_threshold(uint worker_id) const;
 135 
 136   // Perform a single refinement step; called by the refinement
 137   // threads.  Returns true if there was refinement work available.
 138   // Increments *total_refined_cards.
 139   bool do_refinement_step(uint worker_id, size_t* total_refined_cards);
 140 
 141   // Iterate over all concurrent refinement threads applying the given closure.
 142   void threads_do(ThreadClosure *tc);
 143 
 144   // Maximum number of refinement threads.
 145   static uint max_num_threads();
 146 
 147   void print_threads_on(outputStream* st) const;
 148 
 149   G1EpochSynchronizerCounters* synchronizer_counters() {
 150     return &_synchronizer_counters;
 151   }
 152 
 153   // Cards in the dirty card queue set.
 154   size_t green_zone() const      { return _green_zone;  }
 155   size_t yellow_zone() const     { return _yellow_zone; }
 156   size_t red_zone() const        { return _red_zone;    }
 157 };
 158 
 159 #endif // SHARE_GC_G1_G1CONCURRENTREFINE_HPP