1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_GC_ACCOUNTING_READ_BARRIER_TABLE_H_
18 #define ART_RUNTIME_GC_ACCOUNTING_READ_BARRIER_TABLE_H_
19 
20 #include <sys/mman.h>  // For the PROT_* and MAP_* constants.
21 
22 #include "base/bit_utils.h"
23 #include "base/mutex.h"
24 #include "gc/space/space.h"
25 #include "globals.h"
26 #include "mem_map.h"
27 
28 namespace art {
29 namespace gc {
30 namespace accounting {
31 
32 // Used to decide whether to take the read barrier fast/slow paths for
33 // kUseTableLookupReadBarrier. If an entry is set, take the read
34 // barrier slow path. There's an entry per region.
35 class ReadBarrierTable {
36  public:
ReadBarrierTable()37   ReadBarrierTable() {
38     size_t capacity = static_cast<size_t>(kHeapCapacity / kRegionSize);
39     DCHECK_EQ(kHeapCapacity / kRegionSize,
40               static_cast<uint64_t>(static_cast<size_t>(kHeapCapacity / kRegionSize)));
41     std::string error_msg;
42     MemMap* mem_map = MemMap::MapAnonymous("read barrier table", nullptr, capacity,
43                                            PROT_READ | PROT_WRITE, false, false, &error_msg);
44     CHECK(mem_map != nullptr && mem_map->Begin() != nullptr)
45         << "couldn't allocate read barrier table: " << error_msg;
46     mem_map_.reset(mem_map);
47   }
ClearForSpace(space::ContinuousSpace * space)48   void ClearForSpace(space::ContinuousSpace* space) {
49     uint8_t* entry_start = EntryFromAddr(space->Begin());
50     uint8_t* entry_end = EntryFromAddr(space->Limit());
51     memset(reinterpret_cast<void*>(entry_start), 0, entry_end - entry_start);
52   }
Clear(uint8_t * start_addr,uint8_t * end_addr)53   void Clear(uint8_t* start_addr, uint8_t* end_addr) {
54     DCHECK(IsValidHeapAddr(start_addr)) << start_addr;
55     DCHECK(IsValidHeapAddr(end_addr)) << end_addr;
56     DCHECK_ALIGNED(start_addr, kRegionSize);
57     DCHECK_ALIGNED(end_addr, kRegionSize);
58     uint8_t* entry_start = EntryFromAddr(start_addr);
59     uint8_t* entry_end = EntryFromAddr(end_addr);
60     memset(reinterpret_cast<void*>(entry_start), 0, entry_end - entry_start);
61   }
IsSet(const void * heap_addr)62   bool IsSet(const void* heap_addr) const {
63     DCHECK(IsValidHeapAddr(heap_addr)) << heap_addr;
64     uint8_t entry_value = *EntryFromAddr(heap_addr);
65     DCHECK(entry_value == 0 || entry_value == kSetEntryValue);
66     return entry_value == kSetEntryValue;
67   }
ClearAll()68   void ClearAll() {
69     mem_map_->MadviseDontNeedAndZero();
70   }
SetAll()71   void SetAll() {
72     memset(mem_map_->Begin(), kSetEntryValue, mem_map_->Size());
73   }
IsAllCleared()74   bool IsAllCleared() const {
75     for (uint32_t* p = reinterpret_cast<uint32_t*>(mem_map_->Begin());
76          p < reinterpret_cast<uint32_t*>(mem_map_->End()); ++p) {
77       if (*p != 0) {
78         return false;
79       }
80     }
81     return true;
82   }
83 
84   // This should match RegionSpace::kRegionSize. static_assert'ed in concurrent_copying.h.
85   static constexpr size_t kRegionSize = 256 * KB;
86 
87  private:
88   static constexpr uint64_t kHeapCapacity = 4ULL * GB;  // low 4gb.
89   static constexpr uint8_t kSetEntryValue = 0x01;
90 
EntryFromAddr(const void * heap_addr)91   uint8_t* EntryFromAddr(const void* heap_addr) const {
92     DCHECK(IsValidHeapAddr(heap_addr)) << heap_addr;
93     uint8_t* entry_addr = mem_map_->Begin() + reinterpret_cast<uintptr_t>(heap_addr) / kRegionSize;
94     DCHECK(IsValidEntry(entry_addr)) << "heap_addr: " << heap_addr
95                                      << " entry_addr: " << reinterpret_cast<void*>(entry_addr);
96     return entry_addr;
97   }
98 
IsValidHeapAddr(const void * heap_addr)99   bool IsValidHeapAddr(const void* heap_addr) const {
100 #ifdef __LP64__
101     return reinterpret_cast<uint64_t>(heap_addr) < kHeapCapacity;
102 #else
103     UNUSED(heap_addr);
104     return true;
105 #endif
106   }
107 
IsValidEntry(const uint8_t * entry_addr)108   bool IsValidEntry(const uint8_t* entry_addr) const {
109     uint8_t* begin = mem_map_->Begin();
110     uint8_t* end = mem_map_->End();
111     return entry_addr >= begin && entry_addr < end;
112   }
113 
114   std::unique_ptr<MemMap> mem_map_;
115 };
116 
117 }  // namespace accounting
118 }  // namespace gc
119 }  // namespace art
120 
121 #endif  // ART_RUNTIME_GC_ACCOUNTING_READ_BARRIER_TABLE_H_
122