blob: 7dc79afccedc019835a1aefa7d4bc5fd5222a9bc [file] [log] [blame]
Lokesh Gidra606bd942022-05-23 19:00:09 +00001/*
2 * Copyright 2022 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_BASE_GC_VISITED_ARENA_POOL_H_
18#define ART_RUNTIME_BASE_GC_VISITED_ARENA_POOL_H_
19
20#include "base/casts.h"
21#include "base/arena_allocator.h"
22#include "base/locks.h"
23#include "base/mem_map.h"
24
25#include <set>
26
27namespace art {
28
29// GcVisitedArenaPool can be used for tracking allocations so that they can
30// be visited during GC to update the GC-roots inside them.
31
32// An Arena which tracks its allocations.
33class TrackedArena final : public Arena {
34 public:
35 TrackedArena(uint8_t* start, size_t size);
36
37 template <typename PageVisitor>
38 void VisitRoots(PageVisitor& visitor) const REQUIRES_SHARED(Locks::mutator_lock_) {
39 DCHECK_ALIGNED(Size(), kPageSize);
40 DCHECK_ALIGNED(Begin(), kPageSize);
41 int nr_pages = Size() / kPageSize;
42 uint8_t* page_begin = Begin();
43 for (int i = 0; i < nr_pages && first_obj_array_[i] != nullptr; i++, page_begin += kPageSize) {
44 visitor(page_begin, first_obj_array_[i]);
45 }
46 }
47
48 // Set 'obj_begin' in first_obj_array_ in every element for which it's the
49 // first object.
50 void SetFirstObject(uint8_t* obj_begin, uint8_t* obj_end);
51
52 void Release() override;
53
54 private:
55 // first_obj_array_[i] is the object that overlaps with the ith page's
56 // beginning, i.e. first_obj_array_[i] <= ith page_begin.
57 std::unique_ptr<uint8_t*[]> first_obj_array_;
58};
59
60// An arena-pool wherein allocations can be tracked so that the GC can visit all
61// the GC roots. All the arenas are allocated in one sufficiently large memory
62// range to avoid multiple calls to mremapped/mprotected syscalls.
63class GcVisitedArenaPool final : public ArenaPool {
64 public:
65 explicit GcVisitedArenaPool(bool low_4gb = false, const char* name = "LinearAlloc");
66 virtual ~GcVisitedArenaPool();
67 Arena* AllocArena(size_t size) override;
68 void FreeArenaChain(Arena* first) override;
69 size_t GetBytesAllocated() const override;
70 void ReclaimMemory() override {}
71 void LockReclaimMemory() override {}
72 void TrimMaps() override {}
73
74 template <typename PageVisitor>
75 void VisitRoots(PageVisitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
76 std::lock_guard<std::mutex> lock(lock_);
77 for (auto& arena : allocated_arenas_) {
78 arena.VisitRoots(visitor);
79 }
80 }
81
82 private:
83 void FreeRangeLocked(uint8_t* range_begin, size_t range_size) REQUIRES(lock_);
84 // Add a map to the pool of at least min_size
85 void AddMap(size_t min_size) REQUIRES(lock_);
86
87 class Chunk {
88 public:
89 Chunk(uint8_t* addr, size_t size) : addr_(addr), size_(size) {}
90 uint8_t* addr_;
91 size_t size_;
92 };
93
94 class LessByChunkAddr {
95 public:
96 bool operator()(const Chunk* a, const Chunk* b) const {
97 return std::less<uint8_t*>{}(a->addr_, b->addr_);
98 }
99 };
100
101 class LessByChunkSize {
102 public:
103 // Since two chunks could have the same size, use addr when that happens.
104 bool operator()(const Chunk* a, const Chunk* b) const {
105 return std::less<size_t>{}(a->size_, b->size_)
106 || (std::equal_to<size_t>{}(a->size_, b->size_)
107 && std::less<uint8_t*>{}(a->addr_, b->addr_));
108 }
109 };
110
111 class LessByArenaAddr {
112 public:
113 bool operator()(const TrackedArena& a, const TrackedArena& b) const {
114 return std::less<uint8_t*>{}(a.Begin(), b.Begin());
115 }
116 };
117
118 // Use a std::mutex here as Arenas are second-from-the-bottom when using MemMaps, and MemMap
119 // itself uses std::mutex scoped to within an allocate/free only.
120 mutable std::mutex lock_;
121 std::vector<MemMap> maps_ GUARDED_BY(lock_);
122 std::set<Chunk*, LessByChunkSize> best_fit_allocs_ GUARDED_BY(lock_);
123 std::set<Chunk*, LessByChunkAddr> free_chunks_ GUARDED_BY(lock_);
124 // Set of allocated arenas. It's required to be able to find the arena
125 // corresponding to a given address.
126 // TODO: We can manage without this set if we decide to have a large
127 // 'first-object' array for the entire space, instead of per arena. Analyse
128 // which approach is better.
129 std::set<TrackedArena, LessByArenaAddr> allocated_arenas_ GUARDED_BY(lock_);
130 // Number of bytes allocated so far.
131 size_t bytes_allocated_ GUARDED_BY(lock_);
132 const char* name_;
133 const bool low_4gb_;
134
135 DISALLOW_COPY_AND_ASSIGN(GcVisitedArenaPool);
136};
137
138} // namespace art
139
140#endif // ART_RUNTIME_BASE_GC_VISITED_ARENA_POOL_H_