blob: ccc2d23771e426968d0586b6862fa2e4809aceb2 [file] [log] [blame]
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001/*
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 Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_HEAP_H_
18#define ART_SRC_HEAP_H_
19
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080020#include <string>
Carl Shapiro58551df2011-07-24 03:09:51 -070021#include <vector>
22
Ian Rogers5d76c432011-10-31 21:42:49 -070023#include "card_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "globals.h"
Ian Rogers30fab402012-01-23 15:43:46 -080025#include "gtest/gtest.h"
Elliott Hughes5e71b522011-10-20 13:12:32 -070026#include "heap_bitmap.h"
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080027#include "mutex.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070028#include "offsets.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070029
Elliott Hughes3e465b12011-09-02 18:26:12 -070030#define VERIFY_OBJECT_ENABLED 0
31
Carl Shapiro1fb86202011-06-27 17:43:13 -070032namespace art {
33
Ian Rogers30fab402012-01-23 15:43:46 -080034class AllocSpace;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070035class Class;
36class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070037class Space;
Ian Rogers5d4bdc22011-11-02 22:15:43 -070038class Thread;
Carl Shapiro69759ea2011-07-21 18:13:35 -070039class HeapBitmap;
Ian Rogers30fab402012-01-23 15:43:46 -080040class SpaceTest;
Carl Shapiro69759ea2011-07-21 18:13:35 -070041
Carl Shapiro1fb86202011-06-27 17:43:13 -070042class Heap {
43 public:
Ian Rogers30fab402012-01-23 15:43:46 -080044 static const size_t kInitialSize = 2 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070045
Ian Rogers30fab402012-01-23 15:43:46 -080046 static const size_t kMaximumSize = 32 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070047
Elliott Hughes410c0c82011-09-01 17:58:25 -070048 typedef void (RootVisitor)(const Object* root, void* arg);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070049 typedef bool (IsMarkedTester)(const Object* object, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070050
Brian Carlstrom58ae9412011-10-04 00:56:06 -070051 // Create a heap with the requested sizes. The possible empty
52 // image_file_names names specify Spaces to load based on
53 // ImageWriter output.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080054 explicit Heap(size_t starting_size, size_t growth_limit, size_t capacity,
55 const std::string& image_file_name);
Carl Shapiro61e019d2011-07-14 16:53:09 -070056
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080057 ~Heap();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070058
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070059 // Allocates and initializes storage for an object instance.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080060 Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070061
Elliott Hughesa2501992011-08-26 19:39:54 -070062 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -070063#if VERIFY_OBJECT_ENABLED
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080064 void VerifyObject(const Object *obj);
Elliott Hughes3e465b12011-09-02 18:26:12 -070065#else
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080066 void VerifyObject(const Object *obj) {}
Elliott Hughes3e465b12011-09-02 18:26:12 -070067#endif
Ian Rogers408f79a2011-08-23 18:22:33 -070068
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070069 // Check sanity of all live references. Requires the heap lock.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080070 void VerifyHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070071
Elliott Hughes6a5bd492011-10-28 14:33:57 -070072 // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
Elliott Hughesa2501992011-08-26 19:39:54 -070073 // and doesn't abort on error, allowing the caller to report more
74 // meaningful diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080075 bool IsHeapAddress(const Object* obj);
76
Elliott Hughes6a5bd492011-10-28 14:33:57 -070077 // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses).
78 // Requires the heap lock to be held.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080079 bool IsLiveObjectLocked(const Object* obj);
Elliott Hughesa2501992011-08-26 19:39:54 -070080
Carl Shapiro69759ea2011-07-21 18:13:35 -070081 // Initiates an explicit garbage collection.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080082 void CollectGarbage(bool clear_soft_references);
Carl Shapiro69759ea2011-07-21 18:13:35 -070083
Elliott Hughesbf86d042011-08-31 17:53:14 -070084 // Implements java.lang.Runtime.maxMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080085 int64_t GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070086 // Implements java.lang.Runtime.totalMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080087 int64_t GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070088 // Implements java.lang.Runtime.freeMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080089 int64_t GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -070090
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070091 // Implements VMDebug.countInstancesOfClass.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080092 int64_t CountInstances(Class* c, bool count_assignable);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070093
Ian Rogers3bb17a62012-01-27 23:56:44 -080094 // Removes the growth limit on the alloc space so it may grow to its maximum capacity. Used to
95 // implement dalvik.system.VMRuntime.clearGrowthLimit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080096 void ClearGrowthLimit();
jeffhaoc1160702011-10-27 15:48:45 -070097
Ian Rogers30fab402012-01-23 15:43:46 -080098 // Target ideal heap utilization ratio, implements
99 // dalvik.system.VMRuntime.getTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800100 float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -0700101 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700102 }
Ian Rogers30fab402012-01-23 15:43:46 -0800103 // Set target ideal heap utilization ratio, implements
104 // dalvik.system.VMRuntime.setTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800105 void SetTargetHeapUtilization(float target) {
Ian Rogers30fab402012-01-23 15:43:46 -0800106 DCHECK_GT(target, 0.0f); // asserted in Java code
107 DCHECK_LT(target, 1.0f);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700108 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700109 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800110
111 // For the alloc space, sets the maximum number of bytes that the heap is allowed to allocate
112 // from the system. Doesn't allow the space to exceed its growth limit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800113 void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700114
Carl Shapiro69759ea2011-07-21 18:13:35 -0700115 // Blocks the caller until the garbage collector becomes idle.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800116 void WaitForConcurrentGcToComplete();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700117
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800118 pid_t GetLockOwner(); // For SignalCatcher.
119 void Lock();
120 void Unlock();
121 void AssertLockHeld() {
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800122 lock_->AssertHeld();
123 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800124 void AssertLockNotHeld() {
Brian Carlstromcd74c4b2012-01-23 13:21:00 -0800125 lock_->AssertNotHeld();
126 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700127
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800128 const std::vector<Space*>& GetSpaces() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700129 return spaces_;
130 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700131
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800132 HeapBitmap* GetLiveBits() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700133 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700134 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700135
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800136 HeapBitmap* GetMarkBits() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700137 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700138 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700139
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800140 void SetWellKnownClasses(Class* java_lang_ref_FinalizerReference,
Elliott Hughesadb460d2011-10-05 17:02:34 -0700141 Class* java_lang_ref_ReferenceQueue);
142
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800143 void SetReferenceOffsets(MemberOffset reference_referent_offset,
144 MemberOffset reference_queue_offset,
145 MemberOffset reference_queueNext_offset,
146 MemberOffset reference_pendingNext_offset,
147 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700148
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800149 Object* GetReferenceReferent(Object* reference);
150 void ClearReferenceReferent(Object* reference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700151
Elliott Hughesadb460d2011-10-05 17:02:34 -0700152 // Returns true if the reference object has not yet been enqueued.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800153 bool IsEnqueuable(const Object* ref);
154 void EnqueueReference(Object* ref, Object** list);
155 void EnqueuePendingReference(Object* ref, Object** list);
156 Object* DequeuePendingReference(Object** list);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700157
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800158 MemberOffset GetReferencePendingNextOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700159 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700160 return reference_pendingNext_offset_;
161 }
162
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800163 MemberOffset GetFinalizerReferenceZombieOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700164 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700165 return finalizer_reference_zombie_offset_;
166 }
167
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800168 void EnableObjectValidation() {
Ian Rogers30fab402012-01-23 15:43:46 -0800169#if VERIFY_OBJECT_ENABLED
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800170 VerifyHeap();
Ian Rogers30fab402012-01-23 15:43:46 -0800171#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700172 verify_objects_ = true;
173 }
174
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800175 void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700176 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700177 }
178
Elliott Hughes92b3b562011-09-08 16:32:26 -0700179 // Callers must hold the heap lock.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800180 void RecordFreeLocked(size_t freed_objects, size_t freed_bytes);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700181
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700182 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
183 // The call is not needed if NULL is stored in the field.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800184 void WriteBarrierField(const Object* dest, MemberOffset offset, const Object* new_val) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700185 if (!card_marking_disabled_) {
186 card_table_->MarkCard(dest);
187 }
188 }
189
190 // Write barrier for array operations that update many field positions
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800191 void WriteBarrierArray(const Object* dest, int pos, size_t len) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700192 if (UNLIKELY(!card_marking_disabled_)) {
193 card_table_->MarkCard(dest);
194 }
195 }
196
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800197 CardTable* GetCardTable() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700198 return card_table_;
199 }
200
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800201 void DisableCardMarking() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700202 // TODO: we shouldn't need to disable card marking, this is here to help the image_writer
203 card_marking_disabled_ = true;
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700204 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700205
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800206 void AddFinalizerReference(Thread* self, Object* object);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700207
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800208 size_t GetBytesAllocated() { return num_bytes_allocated_; }
209 size_t GetObjectsAllocated() { return num_objects_allocated_; }
Elliott Hughes7162ad92011-10-27 14:08:42 -0700210
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800211 AllocSpace* GetAllocSpace() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700212 return alloc_space_;
213 }
214
Carl Shapiro58551df2011-07-24 03:09:51 -0700215 private:
216 // Allocates uninitialized storage.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800217 Object* AllocateLocked(size_t num_bytes);
218 Object* AllocateLocked(AllocSpace* space, size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700219
Elliott Hughesadb460d2011-10-05 17:02:34 -0700220 // Pushes a list of cleared references out to the managed heap.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800221 void EnqueueClearedReferences(Object** cleared_references);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700222
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800223 void RequestHeapTrim();
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800224
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800225 void RecordAllocationLocked(AllocSpace* space, const Object* object);
226 void RecordImageAllocations(Space* space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700227
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800228 void CollectGarbageInternal(bool clear_soft_references);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700229
Ian Rogers3bb17a62012-01-27 23:56:44 -0800230 // Given the current contents of the alloc space, increase the allowed heap footprint to match
231 // the target utilization ratio. This should only be called immediately after a full garbage
232 // collection.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800233 void GrowForUtilization();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700234
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800235 void AddSpace(Space* space);
Ian Rogers30fab402012-01-23 15:43:46 -0800236
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800237 void VerifyObjectLocked(const Object *obj);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700238
Brian Carlstrom78128a62011-09-15 17:21:19 -0700239 static void VerificationCallback(Object* obj, void* arg);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700240
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800241 Mutex* lock_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800243 std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700244
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700245 // default Space for allocations
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800246 AllocSpace* alloc_space_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700247
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800248 HeapBitmap* mark_bitmap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700249
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800250 HeapBitmap* live_bitmap_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700251
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800252 CardTable* card_table_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700253
254 // Used by the image writer to disable card marking on copied objects
255 // TODO: remove
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800256 bool card_marking_disabled_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700257
Carl Shapiro58551df2011-07-24 03:09:51 -0700258 // True while the garbage collector is running.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800259 bool is_gc_running_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700260
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800261 // Number of bytes allocated. Adjusted after each allocation and free.
262 size_t num_bytes_allocated_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700263
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800264 // Number of objects allocated. Adjusted after each allocation and free.
265 size_t num_objects_allocated_;
Carl Shapiro58551df2011-07-24 03:09:51 -0700266
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800267 Class* java_lang_ref_FinalizerReference_;
268 Class* java_lang_ref_ReferenceQueue_;
Elliott Hughesadb460d2011-10-05 17:02:34 -0700269
Brian Carlstrom1f870082011-08-23 16:02:11 -0700270 // offset of java.lang.ref.Reference.referent
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800271 MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700272
273 // offset of java.lang.ref.Reference.queue
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800274 MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700275
276 // offset of java.lang.ref.Reference.queueNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800277 MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700278
279 // offset of java.lang.ref.Reference.pendingNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800280 MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700281
282 // offset of java.lang.ref.FinalizerReference.zombie
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800283 MemberOffset finalizer_reference_zombie_offset_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700284
Brian Carlstrom395520e2011-09-25 19:35:00 -0700285 // Target ideal heap utilization ratio
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800286 float target_utilization_;
Brian Carlstrom395520e2011-09-25 19:35:00 -0700287
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800288 bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700289
Ian Rogers30fab402012-01-23 15:43:46 -0800290 FRIEND_TEST(SpaceTest, AllocAndFree);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800291 FRIEND_TEST(SpaceTest, AllocAndFreeList);
292 friend class SpaceTest;
Ian Rogers30fab402012-01-23 15:43:46 -0800293
Carl Shapiro69759ea2011-07-21 18:13:35 -0700294 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
295};
296
Carl Shapiro1fb86202011-06-27 17:43:13 -0700297} // namespace art
298
299#endif // ART_SRC_HEAP_H_