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