blob: 1a7b1a374ee8a7fea599c05b975d7cb7a2f7e504 [file] [log] [blame]
Ian Rogers5d76c432011-10-31 21:42:49 -07001/*
2 * Copyright (C) 2010 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#include "card_table.h"
18
Elliott Hughes07ed66b2012-12-12 18:34:25 -080019#include "base/logging.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070020#include "card_table-inl.h"
21#include "gc/heap.h"
22#include "gc/space/space.h"
Ian Rogers5d76c432011-10-31 21:42:49 -070023#include "heap_bitmap.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010024#include "mem_map.h"
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080025#include "runtime.h"
Elliott Hughesf0605a02012-01-13 20:12:02 -080026#include "utils.h"
Ian Rogers5d76c432011-10-31 21:42:49 -070027
28namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070029namespace gc {
30namespace accounting {
31
Mathieu Chartier2c265012014-08-05 18:15:56 -070032constexpr size_t CardTable::kCardShift;
33constexpr size_t CardTable::kCardSize;
34constexpr uint8_t CardTable::kCardClean;
35constexpr uint8_t CardTable::kCardDirty;
36
Ian Rogers5d76c432011-10-31 21:42:49 -070037/*
38 * Maintain a card table from the write barrier. All writes of
Mathieu Chartier2cebb242015-04-21 16:50:40 -070039 * non-null values to heap addresses should go through an entry in
Ian Rogers5d76c432011-10-31 21:42:49 -070040 * WriteBarrier, and from there to here.
41 *
42 * The heap is divided into "cards" of GC_CARD_SIZE bytes, as
43 * determined by GC_CARD_SHIFT. The card table contains one byte of
44 * data per card, to be used by the GC. The value of the byte will be
45 * one of GC_CARD_CLEAN or GC_CARD_DIRTY.
46 *
Mathieu Chartier2cebb242015-04-21 16:50:40 -070047 * After any store of a non-null object pointer into a heap object,
Ian Rogers5d76c432011-10-31 21:42:49 -070048 * code is obliged to mark the card dirty. The setters in
49 * object.h [such as SetFieldObject] do this for you. The
50 * compiler also contains code to mark cards as dirty.
51 *
52 * The card table's base [the "biased card table"] gets set to a
53 * rather strange value. In order to keep the JIT from having to
54 * fabricate or load GC_DIRTY_CARD to store into the card table,
55 * biased base is within the mmap allocation at a point where its low
Ian Rogers30fab402012-01-23 15:43:46 -080056 * byte is equal to GC_DIRTY_CARD. See CardTable::Create for details.
Ian Rogers5d76c432011-10-31 21:42:49 -070057 */
58
Ian Rogers13735952014-10-08 12:43:28 -070059CardTable* CardTable::Create(const uint8_t* heap_begin, size_t heap_capacity) {
Ian Rogers5d76c432011-10-31 21:42:49 -070060 /* Set up the card table */
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070061 size_t capacity = heap_capacity / kCardSize;
Ian Rogers5d76c432011-10-31 21:42:49 -070062 /* Allocate an extra 256 bytes to allow fixed low-byte of base */
Ian Rogers8d31bbd2013-10-13 10:44:14 -070063 std::string error_msg;
Mathieu Chartier2c265012014-08-05 18:15:56 -070064 std::unique_ptr<MemMap> mem_map(
65 MemMap::MapAnonymous("card table", nullptr, capacity + 256, PROT_READ | PROT_WRITE,
Vladimir Marko5c42c292015-02-25 12:02:49 +000066 false, false, &error_msg));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070067 CHECK(mem_map.get() != nullptr) << "couldn't allocate card table: " << error_msg;
Ian Rogers30fab402012-01-23 15:43:46 -080068 // All zeros is the correct initial value; all clean. Anonymous mmaps are initialized to zero, we
69 // don't clear the card table to avoid unnecessary pages being allocated
Andreas Gampe575e78c2014-11-03 23:41:03 -080070 static_assert(kCardClean == 0, "kCardClean must be 0");
Ian Rogers30fab402012-01-23 15:43:46 -080071
Ian Rogers13735952014-10-08 12:43:28 -070072 uint8_t* cardtable_begin = mem_map->Begin();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070073 CHECK(cardtable_begin != nullptr);
Ian Rogers30fab402012-01-23 15:43:46 -080074
75 // We allocated up to a bytes worth of extra space to allow biased_begin's byte value to equal
Mathieu Chartier2c265012014-08-05 18:15:56 -070076 // kCardDirty, compute a offset value to make this the case
Ian Rogers30fab402012-01-23 15:43:46 -080077 size_t offset = 0;
Ian Rogers13735952014-10-08 12:43:28 -070078 uint8_t* biased_begin = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(cardtable_begin) -
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070079 (reinterpret_cast<uintptr_t>(heap_begin) >> kCardShift));
Mathieu Chartier2c265012014-08-05 18:15:56 -070080 uintptr_t biased_byte = reinterpret_cast<uintptr_t>(biased_begin) & 0xff;
81 if (biased_byte != kCardDirty) {
82 int delta = kCardDirty - biased_byte;
Ian Rogers30fab402012-01-23 15:43:46 -080083 offset = delta + (delta < 0 ? 0x100 : 0);
84 biased_begin += offset;
Ian Rogers5d76c432011-10-31 21:42:49 -070085 }
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 CHECK_EQ(reinterpret_cast<uintptr_t>(biased_begin) & 0xff, kCardDirty);
Ian Rogers30fab402012-01-23 15:43:46 -080087 return new CardTable(mem_map.release(), biased_begin, offset);
Ian Rogers5d76c432011-10-31 21:42:49 -070088}
89
Ian Rogers13735952014-10-08 12:43:28 -070090CardTable::CardTable(MemMap* mem_map, uint8_t* biased_begin, size_t offset)
Elliott Hughesa168c832012-06-12 15:34:20 -070091 : mem_map_(mem_map), biased_begin_(biased_begin), offset_(offset) {
Elliott Hughesa168c832012-06-12 15:34:20 -070092}
93
Vladimir Marko3481ba22015-04-13 12:22:36 +010094CardTable::~CardTable() {
95 // Destroys MemMap via std::unique_ptr<>.
96}
97
Ian Rogers1d54e732013-05-02 21:10:01 -070098void CardTable::ClearSpaceCards(space::ContinuousSpace* space) {
Mathieu Chartier262e5ff2012-06-01 17:35:38 -070099 // TODO: clear just the range of the table that has been modified
Ian Rogers13735952014-10-08 12:43:28 -0700100 uint8_t* card_start = CardFromAddr(space->Begin());
101 uint8_t* card_end = CardFromAddr(space->End()); // Make sure to round up.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700102 memset(reinterpret_cast<void*>(card_start), kCardClean, card_end - card_start);
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700103}
104
Ian Rogers5d76c432011-10-31 21:42:49 -0700105void CardTable::ClearCardTable() {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800106 static_assert(kCardClean == 0, "kCardClean must be 0");
Ian Rogersc5f17732014-06-05 20:48:42 -0700107 mem_map_->MadviseDontNeedAndZero();
Ian Rogers5d76c432011-10-31 21:42:49 -0700108}
109
Lei Li4add3b42015-01-15 11:55:26 +0800110void CardTable::ClearCardRange(uint8_t* start, uint8_t* end) {
111 if (!kMadviseZeroes) {
112 memset(start, 0, end - start);
113 return;
114 }
115 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(start), kCardSize);
116 CHECK_ALIGNED(reinterpret_cast<uintptr_t>(end), kCardSize);
117 static_assert(kCardClean == 0, "kCardClean must be 0");
118 uint8_t* start_card = CardFromAddr(start);
119 uint8_t* end_card = CardFromAddr(end);
120 uint8_t* round_start = AlignUp(start_card, kPageSize);
121 uint8_t* round_end = AlignDown(end_card, kPageSize);
122 if (round_start < round_end) {
123 madvise(round_start, round_end - round_start, MADV_DONTNEED);
124 }
125 // Handle unaligned regions at start / end.
126 memset(start_card, 0, std::min(round_start, end_card) - start_card);
127 memset(std::max(round_end, start_card), 0, end_card - std::max(round_end, start_card));
128}
129
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700130bool CardTable::AddrIsInCardTable(const void* addr) const {
131 return IsValidCard(biased_begin_ + ((uintptr_t)addr >> kCardShift));
132}
133
Ian Rogers13735952014-10-08 12:43:28 -0700134void CardTable::CheckAddrIsInCardTable(const uint8_t* addr) const {
135 uint8_t* card_addr = biased_begin_ + ((uintptr_t)addr >> kCardShift);
136 uint8_t* begin = mem_map_->Begin() + offset_;
137 uint8_t* end = mem_map_->End();
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700138 CHECK(AddrIsInCardTable(addr))
139 << "Card table " << this
140 << " begin: " << reinterpret_cast<void*>(begin)
141 << " end: " << reinterpret_cast<void*>(end)
142 << " card_addr: " << reinterpret_cast<void*>(card_addr)
143 << " heap begin: " << AddrFromCard(begin)
144 << " heap end: " << AddrFromCard(end)
145 << " addr: " << reinterpret_cast<const void*>(addr);
Ian Rogers5d76c432011-10-31 21:42:49 -0700146}
147
Ian Rogers5d76c432011-10-31 21:42:49 -0700148void CardTable::VerifyCardTable() {
149 UNIMPLEMENTED(WARNING) << "Card table verification";
150}
151
Ian Rogers1d54e732013-05-02 21:10:01 -0700152} // namespace accounting
153} // namespace gc
Ian Rogers5d76c432011-10-31 21:42:49 -0700154} // namespace art