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_DIRTYCARDQUEUE_HPP
  26 #define SHARE_VM_GC_G1_DIRTYCARDQUEUE_HPP
  27 
  28 #include "gc/g1/ptrQueue.hpp"
  29 #include "memory/allocation.hpp"
  30 
  31 class FreeIdSet;
  32 
  33 // A closure class for processing card table entries.  Note that we don't
  34 // require these closure objects to be stack-allocated.
  35 class CardTableEntryClosure: public CHeapObj<mtGC> {
  36 public:
  37   // Process the card whose card table entry is "card_ptr".  If returns
  38   // "false", terminate the iteration early.
  39   virtual bool do_card_ptr(jbyte* card_ptr, uint worker_i = 0) = 0;
  40 };
  41 
  42 // A ptrQueue whose elements are "oops", pointers to object heads.
  43 class DirtyCardQueue: public PtrQueue {
  44 public:
  45   DirtyCardQueue(PtrQueueSet* qset_, bool perm = false) :
  46     // Dirty card queues are always active, so we create them with their
  47     // active field set to true.
  48     PtrQueue(qset_, perm, true /* active */) { }
  49 
  50   // Flush before destroying; queue may be used to capture pending work while
  51   // doing something else, with auto-flush on completion.
  52   ~DirtyCardQueue() { if (!is_permanent()) flush(); }
  53 
  54   // Process queue entries and release resources.
  55   void flush() { flush_impl(); }
  56 
  57   // Apply the closure to all elements, and reset the index to make the
  58   // buffer empty.  If a closure application returns "false", return
  59   // "false" immediately, halting the iteration.  If "consume" is true,
  60   // deletes processed entries from logs.
  61   bool apply_closure(CardTableEntryClosure* cl,
  62                      bool consume = true,
  63                      uint worker_i = 0);
  64 
  65   // Apply the closure to all elements of "buf", down to "index"
  66   // (inclusive.)  If returns "false", then a closure application returned
  67   // "false", and we return immediately.  If "consume" is true, entries are
  68   // set to NULL as they are processed, so they will not be processed again
  69   // later.
  70   static bool apply_closure_to_buffer(CardTableEntryClosure* cl,
  71                                       void** buf, size_t index, size_t sz,
  72                                       bool consume = true,
  73                                       uint worker_i = 0);
  74   void **get_buf() { return _buf;}
  75   void set_buf(void **buf) {_buf = buf;}
  76   size_t get_index() { return _index;}
  77   void reinitialize() { _buf = 0; _sz = 0; _index = 0;}
  78 };
  79 
  80 
  81 
  82 class DirtyCardQueueSet: public PtrQueueSet {
  83   // The closure used in mut_process_buffer().
  84   CardTableEntryClosure* _mut_process_closure;
  85 
  86   DirtyCardQueue _shared_dirty_card_queue;
  87 
  88   // Override.
  89   bool mut_process_buffer(void** buf);
  90 
  91   // Protected by the _cbl_mon.
  92   FreeIdSet* _free_ids;
  93 
  94   // The number of completed buffers processed by mutator and rs thread,
  95   // respectively.
  96   jint _processed_buffers_mut;
  97   jint _processed_buffers_rs_thread;
  98 
  99   // Current buffer node used for parallel iteration.
 100   BufferNode* volatile _cur_par_buffer_node;
 101 public:
 102   DirtyCardQueueSet(bool notify_when_complete = true);
 103 
 104   void initialize(CardTableEntryClosure* cl, Monitor* cbl_mon, Mutex* fl_lock,
 105                   int process_completed_threshold,
 106                   int max_completed_queue,
 107                   Mutex* lock, PtrQueueSet* fl_owner = NULL);
 108 
 109   // The number of parallel ids that can be claimed to allow collector or
 110   // mutator threads to do card-processing work.
 111   static uint num_par_ids();
 112 
 113   static void handle_zero_index_for_thread(JavaThread* t);
 114 
 115   // Apply the given closure to all entries in all currently-active buffers.
 116   // This should only be applied at a safepoint. (Currently must not be called
 117   // in parallel; this should change in the future.)  If "consume" is true,
 118   // processed entries are discarded.
 119   void iterate_closure_all_threads(CardTableEntryClosure* cl,
 120                                    bool consume = true,
 121                                    uint worker_i = 0);
 122 
 123   // If there exists some completed buffer, pop it, then apply the
 124   // specified closure to all its elements, nulling out those elements
 125   // processed.  If all elements are processed, returns "true".  If no
 126   // completed buffers exist, returns false.  If a completed buffer exists,
 127   // but is only partially completed before a "yield" happens, the
 128   // partially completed buffer (with its processed elements set to NULL)
 129   // is returned to the completed buffer set, and this call returns false.
 130   bool apply_closure_to_completed_buffer(CardTableEntryClosure* cl,
 131                                          uint worker_i = 0,
 132                                          int stop_at = 0,
 133                                          bool during_pause = false);
 134 
 135   // Helper routine for the above.
 136   bool apply_closure_to_completed_buffer_helper(CardTableEntryClosure* cl,
 137                                                 uint worker_i,
 138                                                 BufferNode* nd);
 139 
 140   BufferNode* get_completed_buffer(int stop_at);
 141 
 142   // Applies the current closure to all completed buffers,
 143   // non-consumptively.
 144   void apply_closure_to_all_completed_buffers(CardTableEntryClosure* cl);
 145 
 146   void reset_for_par_iteration() { _cur_par_buffer_node = _completed_buffers_head; }
 147   // Applies the current closure to all completed buffers, non-consumptively.
 148   // Parallel version.
 149   void par_apply_closure_to_all_completed_buffers(CardTableEntryClosure* cl);
 150 
 151   DirtyCardQueue* shared_dirty_card_queue() {
 152     return &_shared_dirty_card_queue;
 153   }
 154 
 155   // Deallocate any completed log buffers
 156   void clear();
 157 
 158   // If a full collection is happening, reset partial logs, and ignore
 159   // completed ones: the full collection will make them all irrelevant.
 160   void abandon_logs();
 161 
 162   // If any threads have partial logs, add them to the global list of logs.
 163   void concatenate_logs();
 164   void clear_n_completed_buffers() { _n_completed_buffers = 0;}
 165 
 166   jint processed_buffers_mut() {
 167     return _processed_buffers_mut;
 168   }
 169   jint processed_buffers_rs_thread() {
 170     return _processed_buffers_rs_thread;
 171   }
 172 
 173 };
 174 
 175 #endif // SHARE_VM_GC_G1_DIRTYCARDQUEUE_HPP