1 /*
  2  * Copyright (c) 2015, 2017, 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_SHARED_CARDTABLEMODREFBSFORCTRS_HPP
 26 #define SHARE_VM_GC_SHARED_CARDTABLEMODREFBSFORCTRS_HPP
 27 
 28 #include "gc/shared/cardTableModRefBS.hpp"
 29 
 30 class CardTableRS;
 31 class DirtyCardToOopClosure;
 32 class OopsInGenClosure;
 33 
 34 // A specialization for the CardTableRS gen rem set.
 35 class CardTableModRefBSForCTRS: public CardTableModRefBS {
 36   friend class CardTableRS;
 37 
 38 public:
 39   CardTableModRefBSForCTRS(MemRegion whole_heap);
 40   ~CardTableModRefBSForCTRS();
 41 
 42   virtual void initialize();
 43 
 44   void set_CTRS(CardTableRS* rs) { _rs = rs; }
 45 
 46 private:
 47   CardTableRS* _rs;
 48 
 49   // *** Support for parallel card scanning.
 50 
 51   // dirty and precleaned are equivalent wrt younger_refs_iter.
 52   static bool card_is_dirty_wrt_gen_iter(jbyte cv) {
 53     return cv == dirty_card || cv == precleaned_card;
 54   }
 55 
 56   // Returns "true" iff the value "cv" will cause the card containing it
 57   // to be scanned in the current traversal.  May be overridden by
 58   // subtypes.
 59   bool card_will_be_scanned(jbyte cv);
 60 
 61   // Returns "true" iff the value "cv" may have represented a dirty card at
 62   // some point.
 63   bool card_may_have_been_dirty(jbyte cv);
 64 
 65   // Iterate over the portion of the card-table which covers the given
 66   // region mr in the given space and apply cl to any dirty sub-regions
 67   // of mr. Clears the dirty cards as they are processed.
 68   void non_clean_card_iterate_possibly_parallel(Space* sp, MemRegion mr,
 69                                                 OopsInGenClosure* cl, CardTableRS* ct,
 70                                                 uint n_threads);
 71 
 72   // Work method used to implement non_clean_card_iterate_possibly_parallel()
 73   // above in the parallel case.
 74   void non_clean_card_iterate_parallel_work(Space* sp, MemRegion mr,
 75                                             OopsInGenClosure* cl, CardTableRS* ct,
 76                                             uint n_threads);
 77 
 78   // This is an array, one element per covered region of the card table.
 79   // Each entry is itself an array, with one element per chunk in the
 80   // covered region.  Each entry of these arrays is the lowest non-clean
 81   // card of the corresponding chunk containing part of an object from the
 82   // previous chunk, or else NULL.
 83   typedef jbyte*  CardPtr;
 84   typedef CardPtr* CardArr;
 85   CardArr* _lowest_non_clean;
 86   size_t*  _lowest_non_clean_chunk_size;
 87   uintptr_t* _lowest_non_clean_base_chunk_index;
 88   volatile int* _last_LNC_resizing_collection;
 89 
 90   // Initializes "lowest_non_clean" to point to the array for the region
 91   // covering "sp", and "lowest_non_clean_base_chunk_index" to the chunk
 92   // index of the corresponding to the first element of that array.
 93   // Ensures that these arrays are of sufficient size, allocating if necessary.
 94   // May be called by several threads concurrently.
 95   void get_LNC_array_for_space(Space* sp,
 96                                jbyte**& lowest_non_clean,
 97                                uintptr_t& lowest_non_clean_base_chunk_index,
 98                                size_t& lowest_non_clean_chunk_size);
 99 
100   // Returns the number of chunks necessary to cover "mr".
101   size_t chunks_to_cover(MemRegion mr) {
102     return (size_t)(addr_to_chunk_index(mr.last()) -
103                     addr_to_chunk_index(mr.start()) + 1);
104   }
105 
106   // Returns the index of the chunk in a stride which
107   // covers the given address.
108   uintptr_t addr_to_chunk_index(const void* addr) {
109     uintptr_t card = (uintptr_t) byte_for(addr);
110     return card / ParGCCardsPerStrideChunk;
111   }
112 
113   // Apply cl, which must either itself apply dcto_cl or be dcto_cl,
114   // to the cards in the stride (of n_strides) within the given space.
115   void process_stride(Space* sp,
116                       MemRegion used,
117                       jint stride, int n_strides,
118                       OopsInGenClosure* cl,
119                       CardTableRS* ct,
120                       jbyte** lowest_non_clean,
121                       uintptr_t lowest_non_clean_base_chunk_index,
122                       size_t lowest_non_clean_chunk_size);
123 
124   // Makes sure that chunk boundaries are handled appropriately, by
125   // adjusting the min_done of dcto_cl, and by using a special card-table
126   // value to indicate how min_done should be set.
127   void process_chunk_boundaries(Space* sp,
128                                 DirtyCardToOopClosure* dcto_cl,
129                                 MemRegion chunk_mr,
130                                 MemRegion used,
131                                 jbyte** lowest_non_clean,
132                                 uintptr_t lowest_non_clean_base_chunk_index,
133                                 size_t    lowest_non_clean_chunk_size);
134 
135 };
136 
137 template<>
138 struct BarrierSet::GetName<CardTableModRefBSForCTRS> {
139   static const BarrierSet::Name value = BarrierSet::CardTableForRS;
140 };
141 
142 template<>
143 struct BarrierSet::GetType<BarrierSet::CardTableForRS> {
144   typedef CardTableModRefBSForCTRS type;
145 };
146 
147 #endif // SHARE_VM_GC_SHARED_CARDTABLEMODREFBSFORCTRS_HPP