1 /*
   2  * Copyright (c) 2001, 2015, 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_VM_GC_G1_CONCURRENTG1REFINE_HPP
  26 #define SHARE_VM_GC_G1_CONCURRENTG1REFINE_HPP
  27 
  28 #include "gc/g1/g1HotCardCache.hpp"
  29 #include "gc/g1/g1YoungRemSetSamplingThread.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "runtime/thread.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 // Forward decl
  35 class ConcurrentG1RefineThread;
  36 class G1CollectedHeap;
  37 class G1HotCardCache;
  38 class G1RegionToSpaceMapper;
  39 class G1RemSet;
  40 class DirtyCardQueue;
  41 
  42 class ConcurrentG1Refine: public CHeapObj<mtGC> {
  43   G1YoungRemSetSamplingThread* _sample_thread;
  44 
  45   ConcurrentG1RefineThread** _threads;
  46   uint _n_worker_threads;
  47  /*
  48   * The value of the update buffer queue length falls into one of 3 zones:
  49   * green, yellow, red. If the value is in [0, green) nothing is
  50   * done, the buffers are left unprocessed to enable the caching effect of the
  51   * dirtied cards. In the yellow zone [green, yellow) the concurrent refinement
  52   * threads are gradually activated. In [yellow, red) all threads are
  53   * running. If the length becomes red (max queue length) the mutators start
  54   * processing the buffers.
  55   *
  56   * There are some interesting cases (when G1UseAdaptiveConcRefinement
  57   * is turned off):
  58   * 1) green = yellow = red = 0. In this case the mutator will process all
  59   *    buffers. Except for those that are created by the deferred updates
  60   *    machinery during a collection.
  61   * 2) green = 0. Means no caching. Can be a good way to minimize the
  62   *    amount of time spent updating rsets during a collection.
  63   */
  64   int _green_zone;
  65   int _yellow_zone;
  66   int _red_zone;
  67 
  68   int _thread_threshold_step;
  69 
  70   // We delay the refinement of 'hot' cards using the hot card cache.
  71   G1HotCardCache _hot_card_cache;
  72 
  73   // Reset the threshold step value based of the current zone boundaries.
  74   void reset_threshold_step();
  75 
  76   ConcurrentG1Refine(G1CollectedHeap* g1h);
  77 
  78  public:
  79   ~ConcurrentG1Refine();
  80 
  81   // Returns ConcurrentG1Refine instance if succeeded to create/initialize ConcurrentG1Refine and ConcurrentG1RefineThread.
  82   // Otherwise, returns NULL with error code.
  83   static ConcurrentG1Refine* create(G1CollectedHeap* g1h, CardTableEntryClosure* refine_closure, jint* ecode);
  84 
  85   void init(G1RegionToSpaceMapper* card_counts_storage);
  86   void stop();
  87 
  88   void reinitialize_threads();
  89 
  90   // Iterate over all concurrent refinement threads
  91   void threads_do(ThreadClosure *tc);
  92 
  93   // Iterate over all worker refinement threads
  94   void worker_threads_do(ThreadClosure * tc);
  95 
  96   // The RS sampling thread has nothing to do with refinement, but is here for now.
  97   G1YoungRemSetSamplingThread * sampling_thread() const { return _sample_thread; }
  98 
  99   static uint thread_num();
 100 
 101   void print_worker_threads_on(outputStream* st) const;
 102 
 103   void set_green_zone(int x)  { _green_zone = x;  }
 104   void set_yellow_zone(int x) { _yellow_zone = x; }
 105   void set_red_zone(int x)    { _red_zone = x;    }
 106 
 107   int green_zone() const      { return _green_zone;  }
 108   int yellow_zone() const     { return _yellow_zone; }
 109   int red_zone() const        { return _red_zone;    }
 110 
 111   uint worker_thread_num() const { return _n_worker_threads; }
 112 
 113   int thread_threshold_step() const { return _thread_threshold_step; }
 114 
 115   G1HotCardCache* hot_card_cache() { return &_hot_card_cache; }
 116 
 117   static bool hot_card_cache_enabled() { return G1HotCardCache::default_use_cache(); }
 118 };
 119 
 120 #endif // SHARE_VM_GC_G1_CONCURRENTG1REFINE_HPP