Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 1 | /* |
| 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 Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 17 | #ifndef ART_SRC_GC_SPACE_H_ |
| 18 | #define ART_SRC_GC_SPACE_H_ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 22 | #include "../mutex.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 23 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 24 | #include "globals.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 25 | #include "image.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 26 | #include "macros.h" |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 27 | #include "dlmalloc.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 28 | #include "mem_map.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 32 | static const bool kDebugSpaces = kIsDebugBuild; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 33 | |
| 34 | class DlMallocSpace; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 35 | class ImageSpace; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 36 | class LargeObjectSpace; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 37 | class Object; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 38 | class SpaceBitmap; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 39 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 40 | enum GcRetentionPolicy { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 41 | kGcRetentionPolicyNeverCollect, |
| 42 | kGcRetentionPolicyAlwaysCollect, |
| 43 | kGcRetentionPolicyFullCollect, // Collect only for full GC |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 44 | }; |
| 45 | std::ostream& operator<<(std::ostream& os, const GcRetentionPolicy& policy); |
| 46 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 47 | enum SpaceType { |
| 48 | kSpaceTypeImageSpace, |
| 49 | kSpaceTypeAllocSpace, |
| 50 | kSpaceTypeZygoteSpace, |
| 51 | kSpaceTypeLargeObjectSpace, |
| 52 | }; |
| 53 | std::ostream& operator<<(std::ostream& os, const SpaceType& space_type); |
| 54 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 55 | // A space contains memory allocated for managed objects. |
| 56 | class Space { |
| 57 | public: |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 58 | virtual bool CanAllocateInto() const = 0; |
| 59 | virtual bool IsCompactible() const = 0; |
| 60 | virtual bool Contains(const Object* obj) const = 0; |
| 61 | virtual SpaceType GetType() const = 0; |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 62 | virtual GcRetentionPolicy GetGcRetentionPolicy() const = 0; |
| 63 | virtual std::string GetName() const = 0; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 64 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 65 | ImageSpace* AsImageSpace(); |
| 66 | DlMallocSpace* AsAllocSpace(); |
| 67 | DlMallocSpace* AsZygoteSpace(); |
| 68 | LargeObjectSpace* AsLargeObjectSpace(); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 69 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 70 | bool IsImageSpace() const { |
| 71 | return GetType() == kSpaceTypeImageSpace; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 74 | bool IsAllocSpace() const { |
| 75 | return GetType() == kSpaceTypeAllocSpace || GetType() == kSpaceTypeZygoteSpace; |
| 76 | } |
| 77 | |
| 78 | bool IsZygoteSpace() const { |
| 79 | return GetType() == kSpaceTypeZygoteSpace; |
| 80 | } |
| 81 | |
| 82 | bool IsLargeObjectSpace() const { |
| 83 | return GetType() == kSpaceTypeLargeObjectSpace; |
| 84 | } |
| 85 | |
| 86 | virtual void Dump(std::ostream& /* os */) const { } |
| 87 | |
| 88 | virtual ~Space() {} |
| 89 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 90 | protected: |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 91 | Space() { } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 92 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 93 | private: |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 94 | DISALLOW_COPY_AND_ASSIGN(Space); |
| 95 | }; |
| 96 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 97 | // AllocSpace interface. |
| 98 | class AllocSpace { |
| 99 | public: |
| 100 | virtual bool CanAllocateInto() const { |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | // General statistics |
| 105 | virtual uint64_t GetNumBytesAllocated() const = 0; |
| 106 | virtual uint64_t GetNumObjectsAllocated() const = 0; |
| 107 | virtual uint64_t GetTotalBytesAllocated() const = 0; |
| 108 | virtual uint64_t GetTotalObjectsAllocated() const = 0; |
| 109 | |
| 110 | // Allocate num_bytes without allowing growth. |
| 111 | virtual Object* Alloc(Thread* self, size_t num_bytes) = 0; |
| 112 | |
| 113 | // Return the storage space required by obj. |
| 114 | virtual size_t AllocationSize(const Object* obj) = 0; |
| 115 | |
| 116 | // Returns how many bytes were freed. |
| 117 | virtual size_t Free(Thread* self, Object* ptr) = 0; |
| 118 | |
| 119 | // Returns how many bytes were freed. |
| 120 | virtual size_t FreeList(Thread* self, size_t num_ptrs, Object** ptrs) = 0; |
| 121 | |
| 122 | protected: |
| 123 | AllocSpace() {} |
| 124 | virtual ~AllocSpace() {} |
| 125 | |
| 126 | private: |
| 127 | DISALLOW_COPY_AND_ASSIGN(AllocSpace); |
| 128 | }; |
| 129 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 130 | // Continuous spaces have bitmaps, and an address range. |
| 131 | class ContinuousSpace : public Space { |
| 132 | public: |
| 133 | // Address at which the space begins |
| 134 | byte* Begin() const { |
| 135 | return begin_; |
| 136 | } |
| 137 | |
| 138 | // Address at which the space ends, which may vary as the space is filled. |
| 139 | byte* End() const { |
| 140 | return end_; |
| 141 | } |
| 142 | |
| 143 | // Current size of space |
| 144 | size_t Size() const { |
| 145 | return End() - Begin(); |
| 146 | } |
| 147 | |
| 148 | virtual SpaceBitmap* GetLiveBitmap() const = 0; |
| 149 | virtual SpaceBitmap* GetMarkBitmap() const = 0; |
| 150 | |
| 151 | // Is object within this space? |
| 152 | bool HasAddress(const Object* obj) const { |
| 153 | const byte* byte_ptr = reinterpret_cast<const byte*>(obj); |
| 154 | return Begin() <= byte_ptr && byte_ptr < End(); |
| 155 | } |
| 156 | |
| 157 | virtual bool Contains(const Object* obj) const { |
| 158 | return HasAddress(obj); |
| 159 | } |
| 160 | |
| 161 | virtual ~ContinuousSpace() {} |
| 162 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 163 | virtual std::string GetName() const { |
| 164 | return name_; |
| 165 | } |
| 166 | |
| 167 | virtual GcRetentionPolicy GetGcRetentionPolicy() const { |
| 168 | return gc_retention_policy_; |
| 169 | } |
| 170 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 171 | protected: |
| 172 | ContinuousSpace(const std::string& name, byte* begin, byte* end, |
| 173 | GcRetentionPolicy gc_retention_policy); |
| 174 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 175 | std::string name_; |
| 176 | GcRetentionPolicy gc_retention_policy_; |
| 177 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 178 | // The beginning of the storage for fast access. |
| 179 | byte* begin_; |
| 180 | |
| 181 | // Current end of the space. |
| 182 | byte* end_; |
| 183 | |
| 184 | private: |
| 185 | DISALLOW_COPY_AND_ASSIGN(ContinuousSpace); |
| 186 | }; |
| 187 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 188 | class DiscontinuousSpace : public virtual Space { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 189 | public: |
| 190 | // Is object within this space? |
| 191 | virtual bool Contains(const Object* obj) const = 0; |
| 192 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 193 | virtual std::string GetName() const { |
| 194 | return name_; |
| 195 | } |
| 196 | |
| 197 | virtual GcRetentionPolicy GetGcRetentionPolicy() const { |
| 198 | return gc_retention_policy_; |
| 199 | } |
| 200 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 201 | protected: |
| 202 | DiscontinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy); |
| 203 | |
| 204 | private: |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 205 | std::string name_; |
| 206 | GcRetentionPolicy gc_retention_policy_; |
| 207 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 208 | DISALLOW_COPY_AND_ASSIGN(DiscontinuousSpace); |
| 209 | }; |
| 210 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 211 | std::ostream& operator<<(std::ostream& os, const Space& space); |
| 212 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 213 | class MemMapSpace : public ContinuousSpace { |
| 214 | public: |
| 215 | // Maximum which the mapped space can grow to. |
| 216 | virtual size_t Capacity() const { |
| 217 | return mem_map_->Size(); |
| 218 | } |
| 219 | |
| 220 | // Size of the space without a limit on its growth. By default this is just the Capacity, but |
| 221 | // for the allocation space we support starting with a small heap and then extending it. |
| 222 | virtual size_t NonGrowthLimitCapacity() const { |
| 223 | return Capacity(); |
| 224 | } |
| 225 | |
| 226 | protected: |
| 227 | MemMapSpace(const std::string& name, MemMap* mem_map, size_t initial_size, |
| 228 | GcRetentionPolicy gc_retention_policy); |
| 229 | |
| 230 | MemMap* GetMemMap() { |
| 231 | return mem_map_.get(); |
| 232 | } |
| 233 | |
| 234 | const MemMap* GetMemMap() const { |
| 235 | return mem_map_.get(); |
| 236 | } |
| 237 | |
| 238 | private: |
| 239 | // Underlying storage of the space |
| 240 | UniquePtr<MemMap> mem_map_; |
| 241 | |
| 242 | DISALLOW_COPY_AND_ASSIGN(MemMapSpace); |
| 243 | }; |
| 244 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 245 | // An alloc space is a space where objects may be allocated and garbage collected. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 246 | class DlMallocSpace : public MemMapSpace, public AllocSpace { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 247 | public: |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 248 | typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg); |
| 249 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 250 | virtual bool CanAllocateInto() const { |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | virtual bool IsCompactible() const { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | virtual SpaceType GetType() const { |
| 259 | return kSpaceTypeAllocSpace; |
| 260 | } |
| 261 | |
| 262 | // Create a AllocSpace with the requested sizes. The requested |
| 263 | // base address is not guaranteed to be granted, if it is required, |
| 264 | // the caller should call Begin on the returned space to confirm |
| 265 | // the request was granted. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 266 | static DlMallocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit, |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 267 | size_t capacity, byte* requested_begin); |
| 268 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 269 | // Allocate num_bytes without allowing the underlying mspace to grow. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 270 | virtual Object* AllocWithGrowth(Thread* self, size_t num_bytes); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 271 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 272 | // Allocate num_bytes allowing the underlying mspace to grow. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 273 | virtual Object* Alloc(Thread* self, size_t num_bytes); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 274 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 275 | // Return the storage space required by obj. |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 276 | virtual size_t AllocationSize(const Object* obj); |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 277 | virtual size_t Free(Thread* self, Object* ptr); |
| 278 | virtual size_t FreeList(Thread* self, size_t num_ptrs, Object** ptrs); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 279 | |
| 280 | void* MoreCore(intptr_t increment); |
| 281 | |
| 282 | void* GetMspace() const { |
| 283 | return mspace_; |
| 284 | } |
| 285 | |
Elliott Hughes | 9eebd3b | 2012-06-08 13:56:31 -0700 | [diff] [blame] | 286 | // Hands unused pages back to the system. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 287 | void Trim(); |
| 288 | |
| 289 | // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 290 | // in use, indicated by num_bytes equaling zero. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 291 | void Walk(WalkCallback callback, void* arg); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 292 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 293 | // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 294 | size_t GetFootprintLimit(); |
| 295 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 296 | // Set the maximum number of bytes that the heap is allowed to obtain from the system via |
| 297 | // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When |
| 298 | // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 299 | void SetFootprintLimit(size_t limit); |
| 300 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 301 | // Removes the fork time growth limit on capacity, allowing the application to allocate up to the |
| 302 | // maximum reserved size of the heap. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 303 | void ClearGrowthLimit() { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 304 | growth_limit_ = NonGrowthLimitCapacity(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // Override capacity so that we only return the possibly limited capacity |
| 308 | virtual size_t Capacity() const { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 309 | return growth_limit_; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 312 | // The total amount of memory reserved for the alloc space |
| 313 | virtual size_t NonGrowthLimitCapacity() const { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 314 | return GetMemMap()->Size(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 317 | virtual SpaceBitmap* GetLiveBitmap() const { |
| 318 | return live_bitmap_.get(); |
| 319 | } |
| 320 | |
| 321 | virtual SpaceBitmap* GetMarkBitmap() const { |
| 322 | return mark_bitmap_.get(); |
| 323 | } |
| 324 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 325 | virtual void Dump(std::ostream& os) const; |
| 326 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 327 | void SetGrowthLimit(size_t growth_limit); |
| 328 | |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 329 | // Swap the live and mark bitmaps of this space. This is used by the GC for concurrent sweeping. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 330 | virtual void SwapBitmaps(); |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 331 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 332 | // Turn ourself into a zygote space and return a new alloc space which has our unused memory. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 333 | DlMallocSpace* CreateZygoteSpace(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 334 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 335 | void SetGcRetentionPolicy(GcRetentionPolicy gc_retention_policy) { |
| 336 | gc_retention_policy_ = gc_retention_policy; |
| 337 | } |
| 338 | |
| 339 | virtual uint64_t GetNumBytesAllocated() const { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 340 | return num_bytes_allocated_; |
| 341 | } |
| 342 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 343 | virtual uint64_t GetNumObjectsAllocated() const { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 344 | return num_objects_allocated_; |
| 345 | } |
| 346 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 347 | virtual uint64_t GetTotalBytesAllocated() const { |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 348 | return total_bytes_allocated_; |
| 349 | } |
| 350 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 351 | virtual uint64_t GetTotalObjectsAllocated() const { |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 352 | return total_objects_allocated_; |
| 353 | } |
| 354 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 355 | private: |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 356 | size_t InternalAllocationSize(const Object* obj); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 357 | Object* AllocWithoutGrowthLocked(size_t num_bytes) EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 358 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 359 | UniquePtr<SpaceBitmap> live_bitmap_; |
| 360 | UniquePtr<SpaceBitmap> mark_bitmap_; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 361 | UniquePtr<SpaceBitmap> temp_bitmap_; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 362 | |
| 363 | // Approximate number of bytes which have been allocated into the space. |
| 364 | size_t num_bytes_allocated_; |
| 365 | size_t num_objects_allocated_; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 366 | size_t total_bytes_allocated_; |
| 367 | size_t total_objects_allocated_; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 368 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 369 | static size_t bitmap_index_; |
| 370 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 371 | DlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin, byte* end, |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 372 | size_t growth_limit); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 373 | |
| 374 | bool Init(size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base); |
| 375 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 376 | static void* CreateMallocSpace(void* base, size_t morecore_start, size_t initial_size); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 377 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 378 | // The boundary tag overhead. |
| 379 | static const size_t kChunkOverhead = kWordSize; |
| 380 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 381 | // Used to ensure mutual exclusion when the allocation spaces data structures are being modified. |
Mathieu Chartier | 664bebf | 2012-11-12 16:54:11 -0800 | [diff] [blame^] | 382 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 383 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 384 | // Underlying malloc space |
| 385 | void* const mspace_; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 386 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 387 | // The capacity of the alloc space until such time that ClearGrowthLimit is called. |
| 388 | // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth |
| 389 | // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote. |
| 390 | // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_ |
| 391 | // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_, |
| 392 | // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared |
| 393 | // one time by a call to ClearGrowthLimit. |
| 394 | size_t growth_limit_; |
| 395 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 396 | friend class MarkSweep; |
| 397 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 398 | DISALLOW_COPY_AND_ASSIGN(DlMallocSpace); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 399 | }; |
| 400 | |
| 401 | // An image space is a space backed with a memory mapped image |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 402 | class ImageSpace : public MemMapSpace { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 403 | public: |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 404 | virtual bool CanAllocateInto() const { |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | virtual bool IsCompactible() const { |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | virtual SpaceType GetType() const { |
| 413 | return kSpaceTypeImageSpace; |
| 414 | } |
| 415 | |
| 416 | // create a Space from an image file. cannot be used for future allocation or collected. |
| 417 | static ImageSpace* Create(const std::string& image) |
| 418 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 419 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 420 | const ImageHeader& GetImageHeader() const { |
| 421 | return *reinterpret_cast<ImageHeader*>(Begin()); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 422 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 423 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 424 | const std::string GetImageFilename() const { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 425 | return GetName(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 426 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 427 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 428 | // Mark the objects defined in this space in the given live bitmap |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 429 | void RecordImageAllocations(SpaceBitmap* live_bitmap) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 430 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 431 | |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 432 | virtual SpaceBitmap* GetLiveBitmap() const { |
| 433 | return live_bitmap_.get(); |
| 434 | } |
| 435 | |
| 436 | virtual SpaceBitmap* GetMarkBitmap() const { |
| 437 | // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of |
| 438 | // special cases to test against. |
| 439 | return live_bitmap_.get(); |
| 440 | } |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 441 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 442 | virtual void Dump(std::ostream& os) const; |
| 443 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 444 | private: |
| 445 | friend class Space; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 446 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 447 | UniquePtr<SpaceBitmap> live_bitmap_; |
| 448 | static size_t bitmap_index_; |
| 449 | |
| 450 | ImageSpace(const std::string& name, MemMap* mem_map); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 451 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 452 | DISALLOW_COPY_AND_ASSIGN(ImageSpace); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 453 | }; |
| 454 | |
Elliott Hughes | 9eebd3b | 2012-06-08 13:56:31 -0700 | [diff] [blame] | 455 | // Callback for dlmalloc_inspect_all or mspace_inspect_all that will madvise(2) unused |
| 456 | // pages back to the kernel. |
| 457 | void MspaceMadviseCallback(void* start, void* end, size_t used_bytes, void* /*arg*/); |
Elliott Hughes | 9eebd3b | 2012-06-08 13:56:31 -0700 | [diff] [blame] | 458 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 459 | } // namespace art |
| 460 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 461 | #endif // ART_SRC_GC_SPACE_H_ |