blob: 3132911c1fe884583df39a83e2d18ffb069b2fe7 [file] [log] [blame]
Elliott Hughes307f75d2011-10-12 18:04:40 -07001/*
2 * Copyright (C) 2011 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 */
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
17#ifndef ART_SRC_SPACE_H_
18#define ART_SRC_SPACE_H_
19
Elliott Hughes307f75d2011-10-12 18:04:40 -070020#include <string>
21
Elliott Hughes90a33692011-08-30 13:27:07 -070022#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "globals.h"
Brian Carlstroma663ea52011-08-19 23:33:41 -070024#include "image.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "macros.h"
Ian Rogers30fab402012-01-23 15:43:46 -080026#include "dlmalloc.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070027#include "mem_map.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070028
29namespace art {
30
Ian Rogers30fab402012-01-23 15:43:46 -080031class AllocSpace;
32class ImageSpace;
Carl Shapiro69759ea2011-07-21 18:13:35 -070033class Object;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070034class SpaceBitmap;
Carl Shapiro69759ea2011-07-21 18:13:35 -070035
Mathieu Chartiercc236d72012-07-20 10:29:05 -070036enum GcRetentionPolicy {
37 GCRP_NEVER_COLLECT,
38 GCRP_ALWAYS_COLLECT,
39 GCRP_FULL_COLLECT,
40};
41std::ostream& operator<<(std::ostream& os, const GcRetentionPolicy& policy);
42
Carl Shapiro69759ea2011-07-21 18:13:35 -070043// A space contains memory allocated for managed objects.
44class Space {
45 public:
Ian Rogers30fab402012-01-23 15:43:46 -080046 // Create a AllocSpace with the requested sizes. The requested
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070047 // base address is not guaranteed to be granted, if it is required,
Ian Rogers30fab402012-01-23 15:43:46 -080048 // the caller should call Begin on the returned space to confirm
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070049 // the request was granted.
Ian Rogers30fab402012-01-23 15:43:46 -080050 static AllocSpace* CreateAllocSpace(const std::string& name, size_t initial_size,
51 size_t growth_limit, size_t capacity,
52 byte* requested_begin);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070053
54 // create a Space from an image file. cannot be used for future allocation or collected.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 static ImageSpace* CreateImageSpace(const std::string& image)
Ian Rogersb726dcb2012-09-05 08:57:23 -070056 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -070057
Ian Rogers30fab402012-01-23 15:43:46 -080058 virtual ~Space() {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070059
Ian Rogers30fab402012-01-23 15:43:46 -080060 const std::string& GetSpaceName() const {
61 return name_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070062 }
63
Ian Rogers30fab402012-01-23 15:43:46 -080064 // Address at which the space begins
65 byte* Begin() const {
66 return begin_;
67 }
68
69 // Address at which the space ends, which may vary as the space is filled
70 byte* End() const {
71 return end_;
72 }
73
74 // Is object within this space?
75 bool Contains(const Object* obj) const {
76 const byte* byte_ptr = reinterpret_cast<const byte*>(obj);
77 return Begin() <= byte_ptr && byte_ptr < End();
78 }
79
80 // Current size of space
81 size_t Size() const {
82 return End() - Begin();
83 }
84
85 // Maximum size of space
86 virtual size_t Capacity() const {
87 return mem_map_->Size();
88 }
89
Ian Rogers3bb17a62012-01-27 23:56:44 -080090 // Size of the space without a limit on its growth. By default this is just the Capacity, but
91 // for the allocation space we support starting with a small heap and then extending it.
92 virtual size_t NonGrowthLimitCapacity() const {
Ian Rogers30fab402012-01-23 15:43:46 -080093 return Capacity();
94 }
95
Mathieu Chartiercc236d72012-07-20 10:29:05 -070096 GcRetentionPolicy GetGcRetentionPolicy() const {
97 return gc_retention_policy_;
98 }
99
100 void SetGcRetentionPolicy(GcRetentionPolicy gc_retention_policy) {
101 gc_retention_policy_ = gc_retention_policy;
102 }
103
Ian Rogers30fab402012-01-23 15:43:46 -0800104 ImageSpace* AsImageSpace() {
105 DCHECK(IsImageSpace());
106 return down_cast<ImageSpace*>(this);
107 }
108
109 AllocSpace* AsAllocSpace() {
110 DCHECK(IsAllocSpace());
111 return down_cast<AllocSpace*>(this);
112 }
113
114 virtual bool IsAllocSpace() const = 0;
115 virtual bool IsImageSpace() const = 0;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700116 virtual bool IsZygoteSpace() const = 0;
Ian Rogers30fab402012-01-23 15:43:46 -0800117
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700118 virtual SpaceBitmap* GetLiveBitmap() const = 0;
119 virtual SpaceBitmap* GetMarkBitmap() const = 0;
120
121 const std::string GetName() const {
122 return name_;
123 }
124
Ian Rogers30fab402012-01-23 15:43:46 -0800125 protected:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126 Space(const std::string& name, MemMap* mem_map, byte* begin, byte* end,
127 GcRetentionPolicy gc_retention_policy)
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700128 : name_(name),
129 mem_map_(mem_map),
130 begin_(begin),
131 end_(end),
132 gc_retention_policy_(gc_retention_policy) {}
Ian Rogers30fab402012-01-23 15:43:46 -0800133
134 std::string name_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700135
Ian Rogers30fab402012-01-23 15:43:46 -0800136 // Underlying storage of the space
137 UniquePtr<MemMap> mem_map_;
138
139 // The beginning of the storage for fast access (always equals mem_map_->GetAddress())
140 byte* const begin_;
141
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700142 // Current end of the space.
Ian Rogers30fab402012-01-23 15:43:46 -0800143 byte* end_;
144
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700145 // Garbage collection retention policy, used to figure out when we should sweep over this space.
146 GcRetentionPolicy gc_retention_policy_;
147
Ian Rogers30fab402012-01-23 15:43:46 -0800148 DISALLOW_COPY_AND_ASSIGN(Space);
149};
150
151std::ostream& operator<<(std::ostream& os, const Space& space);
152
153// An alloc space is a space where objects may be allocated and garbage collected.
154class AllocSpace : public Space {
155 public:
Ian Rogers3bb17a62012-01-27 23:56:44 -0800156 // Allocate num_bytes without allowing the underlying mspace to grow.
Ian Rogers30fab402012-01-23 15:43:46 -0800157 Object* AllocWithGrowth(size_t num_bytes);
158
Ian Rogers3bb17a62012-01-27 23:56:44 -0800159 // Allocate num_bytes allowing the underlying mspace to grow.
Ian Rogers30fab402012-01-23 15:43:46 -0800160 Object* AllocWithoutGrowth(size_t num_bytes);
161
Ian Rogers3bb17a62012-01-27 23:56:44 -0800162 // Return the storage space required by obj.
Ian Rogers30fab402012-01-23 15:43:46 -0800163 size_t AllocationSize(const Object* obj);
164
165 void Free(Object* ptr);
166
167 void FreeList(size_t num_ptrs, Object** ptrs);
168
169 void* MoreCore(intptr_t increment);
170
171 void* GetMspace() const {
172 return mspace_;
173 }
174
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700175 // Hands unused pages back to the system.
Ian Rogers30fab402012-01-23 15:43:46 -0800176 void Trim();
177
178 // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be
Ian Rogers3bb17a62012-01-27 23:56:44 -0800179 // in use, indicated by num_bytes equaling zero.
Ian Rogers30fab402012-01-23 15:43:46 -0800180 void Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg),
181 void* arg);
182
Ian Rogers3bb17a62012-01-27 23:56:44 -0800183 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore.
Ian Rogers30fab402012-01-23 15:43:46 -0800184 size_t GetFootprintLimit();
185
Ian Rogers3bb17a62012-01-27 23:56:44 -0800186 // Set the maximum number of bytes that the heap is allowed to obtain from the system via
187 // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When
188 // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow.
Ian Rogers30fab402012-01-23 15:43:46 -0800189 void SetFootprintLimit(size_t limit);
190
Ian Rogers3bb17a62012-01-27 23:56:44 -0800191 // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
192 // maximum reserved size of the heap.
Ian Rogers30fab402012-01-23 15:43:46 -0800193 void ClearGrowthLimit() {
Ian Rogers3bb17a62012-01-27 23:56:44 -0800194 growth_limit_ = NonGrowthLimitCapacity();
Ian Rogers30fab402012-01-23 15:43:46 -0800195 }
196
197 // Override capacity so that we only return the possibly limited capacity
198 virtual size_t Capacity() const {
jeffhaoc1160702011-10-27 15:48:45 -0700199 return growth_limit_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700200 }
201
Ian Rogers3bb17a62012-01-27 23:56:44 -0800202 // The total amount of memory reserved for the alloc space
203 virtual size_t NonGrowthLimitCapacity() const {
Ian Rogers30fab402012-01-23 15:43:46 -0800204 return mem_map_->End() - mem_map_->Begin();
Ian Rogers5d76c432011-10-31 21:42:49 -0700205 }
206
Ian Rogers30fab402012-01-23 15:43:46 -0800207 virtual bool IsAllocSpace() const {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700208 return gc_retention_policy_ != GCRP_NEVER_COLLECT;
Elliott Hughes307f75d2011-10-12 18:04:40 -0700209 }
210
Ian Rogers30fab402012-01-23 15:43:46 -0800211 virtual bool IsImageSpace() const {
212 return false;
Ian Rogers5d76c432011-10-31 21:42:49 -0700213 }
214
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700215 virtual bool IsZygoteSpace() const {
216 return gc_retention_policy_ == GCRP_FULL_COLLECT;
217 }
218
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700219 virtual SpaceBitmap* GetLiveBitmap() const {
220 return live_bitmap_.get();
221 }
222
223 virtual SpaceBitmap* GetMarkBitmap() const {
224 return mark_bitmap_.get();
225 }
226
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700227 void SetGrowthLimit(size_t growth_limit);
228
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700229 // Swap the live and mark bitmaps of this space. This is used by the GC for concurrent sweeping.
230 void SwapBitmaps();
231
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700232 // Turn ourself into a zygote space and return a new alloc space which has our unused memory.
233 AllocSpace* CreateZygoteSpace();
234
Carl Shapiro69759ea2011-07-21 18:13:35 -0700235 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700236 Object* AllocWithoutGrowthLocked(size_t num_bytes) EXCLUSIVE_LOCKS_REQUIRED(lock_);
237
Ian Rogers30fab402012-01-23 15:43:46 -0800238 friend class Space;
239
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700240 UniquePtr<SpaceBitmap> live_bitmap_;
241 UniquePtr<SpaceBitmap> mark_bitmap_;
242 static size_t bitmap_index_;
243
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700244 AllocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin, byte* end,
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700245 size_t growth_limit);
Ian Rogers30fab402012-01-23 15:43:46 -0800246
247 bool Init(size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base);
248
Ian Rogers3bb17a62012-01-27 23:56:44 -0800249 static void* CreateMallocSpace(void* base, size_t morecore_start, size_t initial_size);
Ian Rogers30fab402012-01-23 15:43:46 -0800250
Carl Shapiro58551df2011-07-24 03:09:51 -0700251 // The boundary tag overhead.
252 static const size_t kChunkOverhead = kWordSize;
253
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700254 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
255 Mutex lock_;
256
Ian Rogers30fab402012-01-23 15:43:46 -0800257 // Underlying malloc space
258 void* const mspace_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700259
Ian Rogers30fab402012-01-23 15:43:46 -0800260 // The capacity of the alloc space until such time that ClearGrowthLimit is called.
261 // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth
262 // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote.
263 // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_
264 // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_,
265 // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared
266 // one time by a call to ClearGrowthLimit.
267 size_t growth_limit_;
268
269 DISALLOW_COPY_AND_ASSIGN(AllocSpace);
270};
271
272// An image space is a space backed with a memory mapped image
273class ImageSpace : public Space {
274 public:
275 const ImageHeader& GetImageHeader() const {
276 return *reinterpret_cast<ImageHeader*>(Begin());
Elliott Hughes307f75d2011-10-12 18:04:40 -0700277 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700278
Ian Rogers30fab402012-01-23 15:43:46 -0800279 const std::string& GetImageFilename() const {
280 return name_;
281 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700282
Ian Rogers30fab402012-01-23 15:43:46 -0800283 // Mark the objects defined in this space in the given live bitmap
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700284 void RecordImageAllocations(SpaceBitmap* live_bitmap) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700285 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700286
Ian Rogers30fab402012-01-23 15:43:46 -0800287 virtual bool IsAllocSpace() const {
288 return false;
289 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700290
Ian Rogers30fab402012-01-23 15:43:46 -0800291 virtual bool IsImageSpace() const {
292 return true;
293 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700294
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700295 virtual bool IsZygoteSpace() const {
296 return false;
297 }
298
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700299 virtual SpaceBitmap* GetLiveBitmap() const {
300 return live_bitmap_.get();
301 }
302
303 virtual SpaceBitmap* GetMarkBitmap() const {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700304 // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of
305 // special cases to test against.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700306 return live_bitmap_.get();
307 }
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700308
Ian Rogers30fab402012-01-23 15:43:46 -0800309 private:
310 friend class Space;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700311
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700312 UniquePtr<SpaceBitmap> live_bitmap_;
313 static size_t bitmap_index_;
314
315 ImageSpace(const std::string& name, MemMap* mem_map);
Elliott Hughes307f75d2011-10-12 18:04:40 -0700316
Ian Rogers30fab402012-01-23 15:43:46 -0800317 DISALLOW_COPY_AND_ASSIGN(ImageSpace);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700318};
319
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700320// Callback for dlmalloc_inspect_all or mspace_inspect_all that will madvise(2) unused
321// pages back to the kernel.
322void MspaceMadviseCallback(void* start, void* end, size_t used_bytes, void* /*arg*/);
Elliott Hughes9eebd3b2012-06-08 13:56:31 -0700323
Carl Shapiro69759ea2011-07-21 18:13:35 -0700324} // namespace art
325
326#endif // ART_SRC_SPACE_H_