Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_HEAP_H_ |
| 18 | #define ART_SRC_HEAP_H_ |
| 19 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 22 | #include "globals.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 23 | #include "object_bitmap.h" |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 24 | #include "offsets.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 26 | #define VERIFY_OBJECT_ENABLED 0 |
| 27 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 30 | class Class; |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 31 | class Mutex; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 32 | class Object; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 33 | class Space; |
| 34 | class HeapBitmap; |
| 35 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 36 | class Heap { |
| 37 | public: |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 38 | static const size_t kInitialSize = 4 * MB; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 39 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 40 | static const size_t kMaximumSize = 16 * MB; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 42 | typedef void (RootVisitor)(const Object* root, void* arg); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 43 | typedef bool (IsMarkedTester)(const Object* object, void* arg); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 44 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 45 | // Create a heap with the requested sizes. The possible empty |
| 46 | // image_file_names names specify Spaces to load based on |
| 47 | // ImageWriter output. |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 48 | static void Init(size_t starting_size, size_t maximum_size, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 49 | const std::vector<std::string>& image_file_names); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 50 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 51 | static void Destroy(); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 52 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 53 | // Allocates and initializes storage for an object instance. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 54 | static Object* AllocObject(Class* klass, size_t num_bytes); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 55 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 56 | // Check sanity of given reference. Requires the heap lock. |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 57 | #if VERIFY_OBJECT_ENABLED |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 58 | static void VerifyObject(const Object *obj); |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 59 | #else |
| 60 | static void VerifyObject(const Object *obj) {} |
| 61 | #endif |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 62 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 63 | // Check sanity of all live references. Requires the heap lock. |
| 64 | static void VerifyHeap(); |
| 65 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 66 | // A weaker test than VerifyObject that doesn't require the heap lock, |
| 67 | // and doesn't abort on error, allowing the caller to report more |
| 68 | // meaningful diagnostics. |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 69 | static bool IsHeapAddress(const Object* obj); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 70 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 71 | // Initiates an explicit garbage collection. |
| 72 | static void CollectGarbage(); |
| 73 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 74 | // Implements java.lang.Runtime.maxMemory. |
| 75 | static int64_t GetMaxMemory(); |
| 76 | // Implements java.lang.Runtime.totalMemory. |
| 77 | static int64_t GetTotalMemory(); |
| 78 | // Implements java.lang.Runtime.freeMemory. |
| 79 | static int64_t GetFreeMemory(); |
| 80 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 81 | // Implements VMDebug.countInstancesOfClass. |
| 82 | static int64_t CountInstances(Class* c, bool count_assignable); |
| 83 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 84 | // Implements dalvik.system.VMRuntime.clearGrowthLimit. |
| 85 | static void ClearGrowthLimit() { |
| 86 | UNIMPLEMENTED(WARNING); |
| 87 | } |
| 88 | // Implements dalvik.system.VMRuntime.getTargetHeapUtilization. |
| 89 | static float GetTargetHeapUtilization() { |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 90 | return target_utilization_; |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 91 | } |
| 92 | // Implements dalvik.system.VMRuntime.setTargetHeapUtilization. |
| 93 | static void SetTargetHeapUtilization(float target) { |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 94 | target_utilization_ = target; |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 95 | } |
Shih-wei Liao | 7f1caab | 2011-10-06 12:11:04 -0700 | [diff] [blame] | 96 | // Sets the maximum number of bytes that the heap is allowed to allocate |
| 97 | // from the system. Clamps to the appropriate maximum value. |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 98 | static void SetIdealFootprint(size_t max_allowed_footprint); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 99 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 100 | // Blocks the caller until the garbage collector becomes idle. |
| 101 | static void WaitForConcurrentGcToComplete(); |
| 102 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 103 | static void Lock(); |
| 104 | |
| 105 | static void Unlock(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 106 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 107 | static const std::vector<Space*>& GetSpaces() { |
| 108 | return spaces_; |
| 109 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 110 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 111 | static HeapBitmap* GetLiveBits() { |
| 112 | return live_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 113 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 114 | |
| 115 | static HeapBitmap* GetMarkBits() { |
| 116 | return mark_bitmap_; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 117 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 118 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 119 | static void SetWellKnownClasses(Class* java_lang_ref_FinalizerReference, |
| 120 | Class* java_lang_ref_ReferenceQueue); |
| 121 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 122 | static void SetReferenceOffsets(MemberOffset reference_referent_offset, |
| 123 | MemberOffset reference_queue_offset, |
| 124 | MemberOffset reference_queueNext_offset, |
| 125 | MemberOffset reference_pendingNext_offset, |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 126 | MemberOffset finalizer_reference_zombie_offset); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 127 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 128 | static Object* GetReferenceReferent(Object* reference); |
| 129 | static void ClearReferenceReferent(Object* reference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 130 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 131 | // Returns true if the reference object has not yet been enqueued. |
| 132 | static bool IsEnqueuable(const Object* ref); |
| 133 | static void EnqueueReference(Object* ref, Object** list); |
| 134 | static void EnqueuePendingReference(Object* ref, Object** list); |
| 135 | static Object* DequeuePendingReference(Object** list); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 136 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 137 | static MemberOffset GetReferencePendingNextOffset() { |
| 138 | DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 139 | return reference_pendingNext_offset_; |
| 140 | } |
| 141 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 142 | static MemberOffset GetFinalizerReferenceZombieOffset() { |
| 143 | DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 144 | return finalizer_reference_zombie_offset_; |
| 145 | } |
| 146 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 147 | static void EnableObjectValidation() { |
| 148 | verify_objects_ = true; |
| 149 | } |
| 150 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 151 | static void DisableObjectValidation() { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 152 | verify_objects_ = false; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 155 | // Callers must hold the heap lock. |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 156 | static void RecordFreeLocked(size_t freed_objects, size_t freed_bytes); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 157 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 158 | // Must be called if a field of an Object in the heap changes, and before any GC safe-point. |
| 159 | // The call is not needed if NULL is stored in the field. |
Elliott Hughes | 3a4f8df | 2011-09-13 15:22:36 -0700 | [diff] [blame] | 160 | static void WriteBarrier(const Object* object) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 161 | #ifdef CONCURRENT_GARBAGE_COLLECTOR |
| 162 | // TODO: we need card marking for a concurrent collector. |
| 163 | UNIMPLEMENTED(FATAL); |
| 164 | #endif |
Elliott Hughes | 3a4f8df | 2011-09-13 15:22:36 -0700 | [diff] [blame] | 165 | } |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 166 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 167 | static void AddFinalizerReference(Object* object); |
| 168 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 169 | private: |
| 170 | // Allocates uninitialized storage. |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 171 | static Object* AllocateLocked(size_t num_bytes); |
| 172 | static Object* AllocateLocked(Space* space, size_t num_bytes); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 173 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 174 | // Pushes a list of cleared references out to the managed heap. |
| 175 | static void EnqueueClearedReferences(Object** cleared_references); |
| 176 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 177 | static void RecordAllocationLocked(Space* space, const Object* object); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 178 | static void RecordImageAllocations(Space* space); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 179 | |
| 180 | static void CollectGarbageInternal(); |
| 181 | |
| 182 | static void GrowForUtilization(); |
| 183 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 184 | static void VerifyObjectLocked(const Object *obj); |
| 185 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 186 | static void VerificationCallback(Object* obj, void* arg); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 187 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 188 | static Mutex* lock_; |
| 189 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 190 | static std::vector<Space*> spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 191 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 192 | // default Space for allocations |
| 193 | static Space* alloc_space_; |
| 194 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 195 | static HeapBitmap* mark_bitmap_; |
| 196 | |
| 197 | static HeapBitmap* live_bitmap_; |
| 198 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 199 | // The maximum size of the heap in bytes. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 200 | static size_t maximum_size_; |
| 201 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 202 | // True while the garbage collector is running. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 203 | static bool is_gc_running_; |
| 204 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 205 | // Number of bytes allocated. Adjusted after each allocation and |
| 206 | // free. |
| 207 | static size_t num_bytes_allocated_; |
| 208 | |
| 209 | // Number of objects allocated. Adjusted after each allocation and |
| 210 | // free. |
| 211 | static size_t num_objects_allocated_; |
| 212 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 213 | static Class* java_lang_ref_FinalizerReference_; |
| 214 | static Class* java_lang_ref_ReferenceQueue_; |
| 215 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 216 | // offset of java.lang.ref.Reference.referent |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 217 | static MemberOffset reference_referent_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 218 | |
| 219 | // offset of java.lang.ref.Reference.queue |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 220 | static MemberOffset reference_queue_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 221 | |
| 222 | // offset of java.lang.ref.Reference.queueNext |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 223 | static MemberOffset reference_queueNext_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 224 | |
| 225 | // offset of java.lang.ref.Reference.pendingNext |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 226 | static MemberOffset reference_pendingNext_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 227 | |
| 228 | // offset of java.lang.ref.FinalizerReference.zombie |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 229 | static MemberOffset finalizer_reference_zombie_offset_; |
| 230 | |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 231 | // Target ideal heap utilization ratio |
| 232 | static float target_utilization_; |
| 233 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 234 | static bool verify_objects_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 235 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 236 | DISALLOW_IMPLICIT_CONSTRUCTORS(Heap); |
| 237 | }; |
| 238 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 239 | } // namespace art |
| 240 | |
| 241 | #endif // ART_SRC_HEAP_H_ |