xref: /aosp_15_r20/art/runtime/gc/accounting/card_table-inl.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "card_table.h"
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "base/atomic.h"
25*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/mem_map.h"
27*795d594fSAndroid Build Coastguard Worker #include "space_bitmap.h"
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
30*795d594fSAndroid Build Coastguard Worker namespace gc {
31*795d594fSAndroid Build Coastguard Worker namespace accounting {
32*795d594fSAndroid Build Coastguard Worker 
byte_cas(uint8_t old_value,uint8_t new_value,uint8_t * address)33*795d594fSAndroid Build Coastguard Worker static inline bool byte_cas(uint8_t old_value, uint8_t new_value, uint8_t* address) {
34*795d594fSAndroid Build Coastguard Worker #if defined(__i386__) || defined(__x86_64__)
35*795d594fSAndroid Build Coastguard Worker   Atomic<uint8_t>* byte_atomic = reinterpret_cast<Atomic<uint8_t>*>(address);
36*795d594fSAndroid Build Coastguard Worker   return byte_atomic->CompareAndSetWeakRelaxed(old_value, new_value);
37*795d594fSAndroid Build Coastguard Worker #else
38*795d594fSAndroid Build Coastguard Worker   // Little endian means most significant byte is on the left.
39*795d594fSAndroid Build Coastguard Worker   const size_t shift_in_bytes = reinterpret_cast<uintptr_t>(address) % sizeof(uintptr_t);
40*795d594fSAndroid Build Coastguard Worker   // Align the address down.
41*795d594fSAndroid Build Coastguard Worker   address -= shift_in_bytes;
42*795d594fSAndroid Build Coastguard Worker   const size_t shift_in_bits = shift_in_bytes * kBitsPerByte;
43*795d594fSAndroid Build Coastguard Worker   Atomic<uintptr_t>* word_atomic = reinterpret_cast<Atomic<uintptr_t>*>(address);
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker   // Word with the byte we are trying to cas cleared.
46*795d594fSAndroid Build Coastguard Worker   const uintptr_t cur_word = word_atomic->load(std::memory_order_relaxed) &
47*795d594fSAndroid Build Coastguard Worker       ~(static_cast<uintptr_t>(0xFF) << shift_in_bits);
48*795d594fSAndroid Build Coastguard Worker   const uintptr_t old_word = cur_word | (static_cast<uintptr_t>(old_value) << shift_in_bits);
49*795d594fSAndroid Build Coastguard Worker   const uintptr_t new_word = cur_word | (static_cast<uintptr_t>(new_value) << shift_in_bits);
50*795d594fSAndroid Build Coastguard Worker   return word_atomic->CompareAndSetWeakRelaxed(old_word, new_word);
51*795d594fSAndroid Build Coastguard Worker #endif
52*795d594fSAndroid Build Coastguard Worker }
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker template <bool kClearCard, typename Visitor>
Scan(ContinuousSpaceBitmap * bitmap,uint8_t * const scan_begin,uint8_t * const scan_end,const Visitor & visitor,const uint8_t minimum_age)55*795d594fSAndroid Build Coastguard Worker inline size_t CardTable::Scan(ContinuousSpaceBitmap* bitmap,
56*795d594fSAndroid Build Coastguard Worker                               uint8_t* const scan_begin,
57*795d594fSAndroid Build Coastguard Worker                               uint8_t* const scan_end,
58*795d594fSAndroid Build Coastguard Worker                               const Visitor& visitor,
59*795d594fSAndroid Build Coastguard Worker                               const uint8_t minimum_age) {
60*795d594fSAndroid Build Coastguard Worker   DCHECK_GE(scan_begin, reinterpret_cast<uint8_t*>(bitmap->HeapBegin()));
61*795d594fSAndroid Build Coastguard Worker   // scan_end is the byte after the last byte we scan.
62*795d594fSAndroid Build Coastguard Worker   DCHECK_LE(scan_end, reinterpret_cast<uint8_t*>(bitmap->HeapLimit()));
63*795d594fSAndroid Build Coastguard Worker   uint8_t* const card_begin = CardFromAddr(scan_begin);
64*795d594fSAndroid Build Coastguard Worker   uint8_t* const card_end = CardFromAddr(AlignUp(scan_end, kCardSize));
65*795d594fSAndroid Build Coastguard Worker   uint8_t* card_cur = card_begin;
66*795d594fSAndroid Build Coastguard Worker   CheckCardValid(card_cur);
67*795d594fSAndroid Build Coastguard Worker   CheckCardValid(card_end);
68*795d594fSAndroid Build Coastguard Worker   size_t cards_scanned = 0;
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker   // Handle any unaligned cards at the start.
71*795d594fSAndroid Build Coastguard Worker   while (!IsAligned<sizeof(intptr_t)>(card_cur) && card_cur < card_end) {
72*795d594fSAndroid Build Coastguard Worker     if (*card_cur >= minimum_age) {
73*795d594fSAndroid Build Coastguard Worker       uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
74*795d594fSAndroid Build Coastguard Worker       bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
75*795d594fSAndroid Build Coastguard Worker       ++cards_scanned;
76*795d594fSAndroid Build Coastguard Worker     }
77*795d594fSAndroid Build Coastguard Worker     ++card_cur;
78*795d594fSAndroid Build Coastguard Worker   }
79*795d594fSAndroid Build Coastguard Worker 
80*795d594fSAndroid Build Coastguard Worker   if (card_cur < card_end) {
81*795d594fSAndroid Build Coastguard Worker     DCHECK_ALIGNED(card_cur, sizeof(intptr_t));
82*795d594fSAndroid Build Coastguard Worker     uint8_t* aligned_end = card_end -
83*795d594fSAndroid Build Coastguard Worker         (reinterpret_cast<uintptr_t>(card_end) & (sizeof(uintptr_t) - 1));
84*795d594fSAndroid Build Coastguard Worker     DCHECK_LE(card_cur, aligned_end);
85*795d594fSAndroid Build Coastguard Worker 
86*795d594fSAndroid Build Coastguard Worker     uintptr_t* word_end = reinterpret_cast<uintptr_t*>(aligned_end);
87*795d594fSAndroid Build Coastguard Worker     for (uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur); word_cur < word_end;
88*795d594fSAndroid Build Coastguard Worker         ++word_cur) {
89*795d594fSAndroid Build Coastguard Worker       while (LIKELY(*word_cur == 0)) {
90*795d594fSAndroid Build Coastguard Worker         ++word_cur;
91*795d594fSAndroid Build Coastguard Worker         if (UNLIKELY(word_cur >= word_end)) {
92*795d594fSAndroid Build Coastguard Worker           goto exit_for;
93*795d594fSAndroid Build Coastguard Worker         }
94*795d594fSAndroid Build Coastguard Worker       }
95*795d594fSAndroid Build Coastguard Worker 
96*795d594fSAndroid Build Coastguard Worker       // Find the first dirty card.
97*795d594fSAndroid Build Coastguard Worker       uintptr_t start_word = *word_cur;
98*795d594fSAndroid Build Coastguard Worker       uintptr_t start =
99*795d594fSAndroid Build Coastguard Worker           reinterpret_cast<uintptr_t>(AddrFromCard(reinterpret_cast<uint8_t*>(word_cur)));
100*795d594fSAndroid Build Coastguard Worker       // TODO: Investigate if processing continuous runs of dirty cards with
101*795d594fSAndroid Build Coastguard Worker       // a single bitmap visit is more efficient.
102*795d594fSAndroid Build Coastguard Worker       for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
103*795d594fSAndroid Build Coastguard Worker         if (static_cast<uint8_t>(start_word) >= minimum_age) {
104*795d594fSAndroid Build Coastguard Worker           auto* card = reinterpret_cast<uint8_t*>(word_cur) + i;
105*795d594fSAndroid Build Coastguard Worker           DCHECK(*card == static_cast<uint8_t>(start_word) || *card == kCardDirty)
106*795d594fSAndroid Build Coastguard Worker               << "card " << static_cast<size_t>(*card) << " intptr_t " << (start_word & 0xFF);
107*795d594fSAndroid Build Coastguard Worker           bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
108*795d594fSAndroid Build Coastguard Worker           ++cards_scanned;
109*795d594fSAndroid Build Coastguard Worker         }
110*795d594fSAndroid Build Coastguard Worker         start_word >>= 8;
111*795d594fSAndroid Build Coastguard Worker         start += kCardSize;
112*795d594fSAndroid Build Coastguard Worker       }
113*795d594fSAndroid Build Coastguard Worker     }
114*795d594fSAndroid Build Coastguard Worker     exit_for:
115*795d594fSAndroid Build Coastguard Worker 
116*795d594fSAndroid Build Coastguard Worker     // Handle any unaligned cards at the end.
117*795d594fSAndroid Build Coastguard Worker     card_cur = reinterpret_cast<uint8_t*>(word_end);
118*795d594fSAndroid Build Coastguard Worker     while (card_cur < card_end) {
119*795d594fSAndroid Build Coastguard Worker       if (*card_cur >= minimum_age) {
120*795d594fSAndroid Build Coastguard Worker         uintptr_t start = reinterpret_cast<uintptr_t>(AddrFromCard(card_cur));
121*795d594fSAndroid Build Coastguard Worker         bitmap->VisitMarkedRange(start, start + kCardSize, visitor);
122*795d594fSAndroid Build Coastguard Worker         ++cards_scanned;
123*795d594fSAndroid Build Coastguard Worker       }
124*795d594fSAndroid Build Coastguard Worker       ++card_cur;
125*795d594fSAndroid Build Coastguard Worker     }
126*795d594fSAndroid Build Coastguard Worker   }
127*795d594fSAndroid Build Coastguard Worker 
128*795d594fSAndroid Build Coastguard Worker   if (kClearCard) {
129*795d594fSAndroid Build Coastguard Worker     ClearCardRange(scan_begin, scan_end);
130*795d594fSAndroid Build Coastguard Worker   }
131*795d594fSAndroid Build Coastguard Worker 
132*795d594fSAndroid Build Coastguard Worker   return cards_scanned;
133*795d594fSAndroid Build Coastguard Worker }
134*795d594fSAndroid Build Coastguard Worker 
135*795d594fSAndroid Build Coastguard Worker template <typename Visitor, typename ModifiedVisitor>
ModifyCardsAtomic(uint8_t * scan_begin,uint8_t * scan_end,const Visitor & visitor,const ModifiedVisitor & modified)136*795d594fSAndroid Build Coastguard Worker inline void CardTable::ModifyCardsAtomic(uint8_t* scan_begin,
137*795d594fSAndroid Build Coastguard Worker                                          uint8_t* scan_end,
138*795d594fSAndroid Build Coastguard Worker                                          const Visitor& visitor,
139*795d594fSAndroid Build Coastguard Worker                                          const ModifiedVisitor& modified) {
140*795d594fSAndroid Build Coastguard Worker   uint8_t* card_cur = CardFromAddr(scan_begin);
141*795d594fSAndroid Build Coastguard Worker   uint8_t* card_end = CardFromAddr(AlignUp(scan_end, kCardSize));
142*795d594fSAndroid Build Coastguard Worker   CheckCardValid(card_cur);
143*795d594fSAndroid Build Coastguard Worker   CheckCardValid(card_end);
144*795d594fSAndroid Build Coastguard Worker   DCHECK(visitor(kCardClean) == kCardClean);
145*795d594fSAndroid Build Coastguard Worker 
146*795d594fSAndroid Build Coastguard Worker   // Handle any unaligned cards at the start.
147*795d594fSAndroid Build Coastguard Worker   while (!IsAligned<sizeof(intptr_t)>(card_cur) && card_cur < card_end) {
148*795d594fSAndroid Build Coastguard Worker     uint8_t expected, new_value;
149*795d594fSAndroid Build Coastguard Worker     do {
150*795d594fSAndroid Build Coastguard Worker       expected = *card_cur;
151*795d594fSAndroid Build Coastguard Worker       new_value = visitor(expected);
152*795d594fSAndroid Build Coastguard Worker     } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_cur)));
153*795d594fSAndroid Build Coastguard Worker     if (expected != new_value) {
154*795d594fSAndroid Build Coastguard Worker       modified(card_cur, expected, new_value);
155*795d594fSAndroid Build Coastguard Worker     }
156*795d594fSAndroid Build Coastguard Worker     ++card_cur;
157*795d594fSAndroid Build Coastguard Worker   }
158*795d594fSAndroid Build Coastguard Worker 
159*795d594fSAndroid Build Coastguard Worker   // Handle unaligned cards at the end.
160*795d594fSAndroid Build Coastguard Worker   while (!IsAligned<sizeof(intptr_t)>(card_end) && card_end > card_cur) {
161*795d594fSAndroid Build Coastguard Worker     --card_end;
162*795d594fSAndroid Build Coastguard Worker     uint8_t expected, new_value;
163*795d594fSAndroid Build Coastguard Worker     do {
164*795d594fSAndroid Build Coastguard Worker       expected = *card_end;
165*795d594fSAndroid Build Coastguard Worker       new_value = visitor(expected);
166*795d594fSAndroid Build Coastguard Worker     } while (expected != new_value && UNLIKELY(!byte_cas(expected, new_value, card_end)));
167*795d594fSAndroid Build Coastguard Worker     if (expected != new_value) {
168*795d594fSAndroid Build Coastguard Worker       modified(card_end, expected, new_value);
169*795d594fSAndroid Build Coastguard Worker     }
170*795d594fSAndroid Build Coastguard Worker   }
171*795d594fSAndroid Build Coastguard Worker 
172*795d594fSAndroid Build Coastguard Worker   // Now we have the words, we can process words in parallel.
173*795d594fSAndroid Build Coastguard Worker   uintptr_t* word_cur = reinterpret_cast<uintptr_t*>(card_cur);
174*795d594fSAndroid Build Coastguard Worker   uintptr_t* word_end = reinterpret_cast<uintptr_t*>(card_end);
175*795d594fSAndroid Build Coastguard Worker   // TODO: This is not big endian safe.
176*795d594fSAndroid Build Coastguard Worker   union {
177*795d594fSAndroid Build Coastguard Worker     uintptr_t expected_word;
178*795d594fSAndroid Build Coastguard Worker     uint8_t expected_bytes[sizeof(uintptr_t)];
179*795d594fSAndroid Build Coastguard Worker   };
180*795d594fSAndroid Build Coastguard Worker   union {
181*795d594fSAndroid Build Coastguard Worker     uintptr_t new_word;
182*795d594fSAndroid Build Coastguard Worker     uint8_t new_bytes[sizeof(uintptr_t)];
183*795d594fSAndroid Build Coastguard Worker   };
184*795d594fSAndroid Build Coastguard Worker 
185*795d594fSAndroid Build Coastguard Worker   // TODO: Parallelize.
186*795d594fSAndroid Build Coastguard Worker   while (word_cur < word_end) {
187*795d594fSAndroid Build Coastguard Worker     while (true) {
188*795d594fSAndroid Build Coastguard Worker       expected_word = *word_cur;
189*795d594fSAndroid Build Coastguard Worker       static_assert(kCardClean == 0);
190*795d594fSAndroid Build Coastguard Worker       if (LIKELY(expected_word == 0 /* All kCardClean */ )) {
191*795d594fSAndroid Build Coastguard Worker         break;
192*795d594fSAndroid Build Coastguard Worker       }
193*795d594fSAndroid Build Coastguard Worker       for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
194*795d594fSAndroid Build Coastguard Worker         new_bytes[i] = visitor(expected_bytes[i]);
195*795d594fSAndroid Build Coastguard Worker       }
196*795d594fSAndroid Build Coastguard Worker       Atomic<uintptr_t>* atomic_word = reinterpret_cast<Atomic<uintptr_t>*>(word_cur);
197*795d594fSAndroid Build Coastguard Worker       if (LIKELY(atomic_word->CompareAndSetWeakRelaxed(expected_word, new_word))) {
198*795d594fSAndroid Build Coastguard Worker         for (size_t i = 0; i < sizeof(uintptr_t); ++i) {
199*795d594fSAndroid Build Coastguard Worker           const uint8_t expected_byte = expected_bytes[i];
200*795d594fSAndroid Build Coastguard Worker           const uint8_t new_byte = new_bytes[i];
201*795d594fSAndroid Build Coastguard Worker           if (expected_byte != new_byte) {
202*795d594fSAndroid Build Coastguard Worker             modified(reinterpret_cast<uint8_t*>(word_cur) + i, expected_byte, new_byte);
203*795d594fSAndroid Build Coastguard Worker           }
204*795d594fSAndroid Build Coastguard Worker         }
205*795d594fSAndroid Build Coastguard Worker         break;
206*795d594fSAndroid Build Coastguard Worker       }
207*795d594fSAndroid Build Coastguard Worker     }
208*795d594fSAndroid Build Coastguard Worker     ++word_cur;
209*795d594fSAndroid Build Coastguard Worker   }
210*795d594fSAndroid Build Coastguard Worker }
211*795d594fSAndroid Build Coastguard Worker 
AddrFromCard(const uint8_t * card_addr)212*795d594fSAndroid Build Coastguard Worker inline void* CardTable::AddrFromCard(const uint8_t *card_addr) const {
213*795d594fSAndroid Build Coastguard Worker   DCHECK(IsValidCard(card_addr))
214*795d594fSAndroid Build Coastguard Worker     << " card_addr: " << reinterpret_cast<const void*>(card_addr)
215*795d594fSAndroid Build Coastguard Worker     << " begin: " << reinterpret_cast<void*>(mem_map_.Begin() + offset_)
216*795d594fSAndroid Build Coastguard Worker     << " end: " << reinterpret_cast<void*>(mem_map_.End());
217*795d594fSAndroid Build Coastguard Worker   uintptr_t offset = card_addr - biased_begin_;
218*795d594fSAndroid Build Coastguard Worker   return reinterpret_cast<void*>(offset << kCardShift);
219*795d594fSAndroid Build Coastguard Worker }
220*795d594fSAndroid Build Coastguard Worker 
CardFromAddr(const void * addr)221*795d594fSAndroid Build Coastguard Worker inline uint8_t* CardTable::CardFromAddr(const void *addr) const {
222*795d594fSAndroid Build Coastguard Worker   uint8_t *card_addr = biased_begin_ + (reinterpret_cast<uintptr_t>(addr) >> kCardShift);
223*795d594fSAndroid Build Coastguard Worker   // Check that the caller was asking for an address covered by the card table.
224*795d594fSAndroid Build Coastguard Worker   DCHECK(IsValidCard(card_addr)) << "addr: " << addr
225*795d594fSAndroid Build Coastguard Worker       << " card_addr: " << reinterpret_cast<void*>(card_addr);
226*795d594fSAndroid Build Coastguard Worker   return card_addr;
227*795d594fSAndroid Build Coastguard Worker }
228*795d594fSAndroid Build Coastguard Worker 
IsValidCard(const uint8_t * card_addr)229*795d594fSAndroid Build Coastguard Worker inline bool CardTable::IsValidCard(const uint8_t* card_addr) const {
230*795d594fSAndroid Build Coastguard Worker   uint8_t* begin = mem_map_.Begin() + offset_;
231*795d594fSAndroid Build Coastguard Worker   uint8_t* end = mem_map_.End();
232*795d594fSAndroid Build Coastguard Worker   return card_addr >= begin && card_addr < end;
233*795d594fSAndroid Build Coastguard Worker }
234*795d594fSAndroid Build Coastguard Worker 
CheckCardValid(uint8_t * card)235*795d594fSAndroid Build Coastguard Worker inline void CardTable::CheckCardValid(uint8_t* card) const {
236*795d594fSAndroid Build Coastguard Worker   DCHECK(IsValidCard(card))
237*795d594fSAndroid Build Coastguard Worker       << " card_addr: " << reinterpret_cast<const void*>(card)
238*795d594fSAndroid Build Coastguard Worker       << " begin: " << reinterpret_cast<void*>(mem_map_.Begin() + offset_)
239*795d594fSAndroid Build Coastguard Worker       << " end: " << reinterpret_cast<void*>(mem_map_.End());
240*795d594fSAndroid Build Coastguard Worker }
241*795d594fSAndroid Build Coastguard Worker 
242*795d594fSAndroid Build Coastguard Worker }  // namespace accounting
243*795d594fSAndroid Build Coastguard Worker }  // namespace gc
244*795d594fSAndroid Build Coastguard Worker }  // namespace art
245*795d594fSAndroid Build Coastguard Worker 
246*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_GC_ACCOUNTING_CARD_TABLE_INL_H_
247