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 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 20 | #include <iosfwd> |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 21 | #include <string> |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 24 | #include "card_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | #include "globals.h" |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 26 | #include "gtest/gtest.h" |
Elliott Hughes | 5e71b52 | 2011-10-20 13:12:32 -0700 | [diff] [blame] | 27 | #include "heap_bitmap.h" |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 28 | #include "mutex.h" |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 29 | #include "offsets.h" |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 30 | #include "safe_map.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 32 | #define VERIFY_OBJECT_ENABLED 0 |
| 33 | |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame^] | 34 | // Fast verification means we do not verify the classes of objects. |
| 35 | #define VERIFY_OBJECT_FAST 1 |
| 36 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 37 | namespace art { |
| 38 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 39 | class AllocSpace; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 40 | class Class; |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 41 | class HeapBitmap; |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 42 | class ImageSpace; |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 43 | class MarkStack; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 44 | class ModUnionTable; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 45 | class Object; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 46 | class Space; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 47 | class SpaceTest; |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 48 | class Thread; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 49 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 50 | typedef std::vector<Space*> Spaces; |
| 51 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 52 | class LOCKABLE Heap { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 53 | public: |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 54 | static const size_t kInitialSize = 2 * MB; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 55 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 56 | static const size_t kMaximumSize = 32 * MB; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 57 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 58 | typedef void (RootVisitor)(const Object* root, void* arg); |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 59 | typedef bool (IsMarkedTester)(const Object* object, void* arg); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 60 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 61 | // Create a heap with the requested sizes. The possible empty |
| 62 | // image_file_names names specify Spaces to load based on |
| 63 | // ImageWriter output. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 64 | explicit Heap(size_t starting_size, size_t growth_limit, size_t capacity, |
| 65 | const std::string& image_file_name); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 66 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 67 | ~Heap(); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 68 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 69 | // Allocates and initializes storage for an object instance. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 70 | Object* AllocObject(Class* klass, size_t num_bytes); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 71 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 72 | // Check sanity of given reference. Requires the heap lock. |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 73 | #if VERIFY_OBJECT_ENABLED |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 74 | void VerifyObject(const Object* o); |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 75 | #else |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 76 | void VerifyObject(const Object*) {} |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 77 | #endif |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 78 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 79 | // Check sanity of all live references. Requires the heap lock. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 80 | void VerifyHeap(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 81 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 82 | // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock, |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 83 | // and doesn't abort on error, allowing the caller to report more |
| 84 | // meaningful diagnostics. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 85 | bool IsHeapAddress(const Object* obj); |
| 86 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 87 | // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses). |
| 88 | // Requires the heap lock to be held. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 89 | bool IsLiveObjectLocked(const Object* obj); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 90 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 91 | // Initiates an explicit garbage collection. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 92 | void CollectGarbage(bool clear_soft_references); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 93 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 94 | // Does a concurrent GC, should only be called by the GC daemon thread |
| 95 | // through runtime. |
| 96 | void ConcurrentGC(); |
| 97 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 98 | // Implements java.lang.Runtime.maxMemory. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 99 | int64_t GetMaxMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 100 | // Implements java.lang.Runtime.totalMemory. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 101 | int64_t GetTotalMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 102 | // Implements java.lang.Runtime.freeMemory. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 103 | int64_t GetFreeMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 104 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 105 | // Implements VMDebug.countInstancesOfClass. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 106 | int64_t CountInstances(Class* c, bool count_assignable); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 107 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 108 | // Removes the growth limit on the alloc space so it may grow to its maximum capacity. Used to |
| 109 | // implement dalvik.system.VMRuntime.clearGrowthLimit. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 110 | void ClearGrowthLimit(); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 111 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 112 | // Target ideal heap utilization ratio, implements |
| 113 | // dalvik.system.VMRuntime.getTargetHeapUtilization. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 114 | float GetTargetHeapUtilization() { |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 115 | return target_utilization_; |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 116 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 117 | // Set target ideal heap utilization ratio, implements |
| 118 | // dalvik.system.VMRuntime.setTargetHeapUtilization. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 119 | void SetTargetHeapUtilization(float target) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 120 | DCHECK_GT(target, 0.0f); // asserted in Java code |
| 121 | DCHECK_LT(target, 1.0f); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 122 | target_utilization_ = target; |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 123 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 124 | |
| 125 | // For the alloc space, sets the maximum number of bytes that the heap is allowed to allocate |
| 126 | // from the system. Doesn't allow the space to exceed its growth limit. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 127 | void SetIdealFootprint(size_t max_allowed_footprint); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 128 | |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 129 | // Blocks the caller until the garbage collector becomes idle and returns |
| 130 | // true if we waited for the GC to complete. |
| 131 | bool WaitForConcurrentGcToComplete(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 132 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 133 | pid_t GetLockOwner(); // For SignalCatcher. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 134 | void AssertLockHeld() { |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 135 | lock_->AssertHeld(); |
| 136 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 137 | void AssertLockNotHeld() { |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 138 | lock_->AssertNotHeld(); |
| 139 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 140 | |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame] | 141 | const Spaces& GetSpaces() { |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 142 | return spaces_; |
| 143 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 144 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 145 | void SetReferenceOffsets(MemberOffset reference_referent_offset, |
| 146 | MemberOffset reference_queue_offset, |
| 147 | MemberOffset reference_queueNext_offset, |
| 148 | MemberOffset reference_pendingNext_offset, |
| 149 | MemberOffset finalizer_reference_zombie_offset); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 150 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 151 | Object* GetReferenceReferent(Object* reference); |
| 152 | void ClearReferenceReferent(Object* reference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 153 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 154 | // Returns true if the reference object has not yet been enqueued. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 155 | bool IsEnqueuable(const Object* ref); |
| 156 | void EnqueueReference(Object* ref, Object** list); |
| 157 | void EnqueuePendingReference(Object* ref, Object** list); |
| 158 | Object* DequeuePendingReference(Object** list); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 159 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 160 | MemberOffset GetReferencePendingNextOffset() { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 161 | DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 162 | return reference_pendingNext_offset_; |
| 163 | } |
| 164 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 165 | MemberOffset GetFinalizerReferenceZombieOffset() { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 166 | DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 167 | return finalizer_reference_zombie_offset_; |
| 168 | } |
| 169 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 170 | void EnableObjectValidation() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 171 | #if VERIFY_OBJECT_ENABLED |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 172 | VerifyHeap(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 173 | #endif |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 174 | verify_objects_ = true; |
| 175 | } |
| 176 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 177 | void DisableObjectValidation() { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 178 | verify_objects_ = false; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 181 | // Callers must hold the heap lock. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 182 | void RecordFreeLocked(size_t freed_objects, size_t freed_bytes); |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 183 | |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 184 | // Must be called if a field of an Object in the heap changes, and before any GC safe-point. |
| 185 | // The call is not needed if NULL is stored in the field. |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 186 | void WriteBarrierField(const Object* dst, MemberOffset /*offset*/, const Object* /*new_value*/) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 187 | if (!card_marking_disabled_) { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 188 | card_table_->MarkCard(dst); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | // Write barrier for array operations that update many field positions |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 193 | void WriteBarrierArray(const Object* dst, int /*start_offset*/, size_t /*length TODO: element_count or byte_count?*/) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 194 | if (UNLIKELY(!card_marking_disabled_)) { |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 195 | card_table_->MarkCard(dst); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 199 | CardTable* GetCardTable() { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 200 | return card_table_.get(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 203 | void DisableCardMarking() { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 204 | // TODO: we shouldn't need to disable card marking, this is here to help the image_writer |
| 205 | card_marking_disabled_ = true; |
Elliott Hughes | 3a4f8df | 2011-09-13 15:22:36 -0700 | [diff] [blame] | 206 | } |
Elliott Hughes | 5ea047b | 2011-09-13 14:38:18 -0700 | [diff] [blame] | 207 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 208 | void AddFinalizerReference(Thread* self, Object* object); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 209 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 210 | size_t GetBytesAllocated() { return num_bytes_allocated_; } |
| 211 | size_t GetObjectsAllocated() { return num_objects_allocated_; } |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 212 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 213 | size_t GetConcurrentStartSize() const { return concurrent_start_size_; } |
| 214 | |
| 215 | void SetConcurrentStartSize(size_t size) { |
| 216 | concurrent_start_size_ = size; |
| 217 | } |
| 218 | |
| 219 | size_t GetConcurrentMinFree() const { return concurrent_min_free_; } |
| 220 | |
| 221 | void SetConcurrentMinFree(size_t size) { |
| 222 | concurrent_min_free_ = size; |
| 223 | } |
| 224 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 225 | // Functions for getting the bitmap which corresponds to an object's address. |
| 226 | // This is probably slow, TODO: use better data structure like binary tree . |
| 227 | Space* FindSpaceFromObject(const Object*) const; |
| 228 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 229 | void DumpForSigQuit(std::ostream& os); |
| 230 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 231 | void Trim(AllocSpace* alloc_space); |
| 232 | |
| 233 | HeapBitmap* GetLiveBitmap() { |
| 234 | return live_bitmap_.get(); |
| 235 | } |
| 236 | |
| 237 | HeapBitmap* GetMarkBitmap() { |
| 238 | return mark_bitmap_.get(); |
| 239 | } |
| 240 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 241 | void PreZygoteFork(); |
| 242 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 243 | // DEPRECATED: Should remove in "near" future when support for multiple image spaces is added. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 244 | // Assumes there is only one image space. |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 245 | ImageSpace* GetImageSpace(); |
| 246 | AllocSpace* GetAllocSpace(); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 247 | void DumpSpaces(); |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 248 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 249 | private: |
| 250 | // Allocates uninitialized storage. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 251 | Object* AllocateLocked(size_t num_bytes); |
| 252 | Object* AllocateLocked(AllocSpace* space, size_t num_bytes); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 253 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 254 | void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
| 255 | void Unlock() UNLOCK_FUNCTION(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 256 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 257 | // Pushes a list of cleared references out to the managed heap. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 258 | void EnqueueClearedReferences(Object** cleared_references); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 259 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 260 | void RequestHeapTrim(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 261 | void RequestConcurrentGC(); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 262 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 263 | void RecordAllocationLocked(AllocSpace* space, const Object* object); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 264 | |
Elliott Hughes | f834936 | 2012-06-18 15:00:06 -0700 | [diff] [blame] | 265 | // TODO: can we teach GCC to understand the weird locking in here? |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 266 | void CollectGarbageInternal(bool partial_gc, bool concurrent, bool clear_soft_references) NO_THREAD_SAFETY_ANALYSIS; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 267 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 268 | // Given the current contents of the alloc space, increase the allowed heap footprint to match |
| 269 | // the target utilization ratio. This should only be called immediately after a full garbage |
| 270 | // collection. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 271 | void GrowForUtilization(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 272 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 273 | size_t GetPercentFree(); |
| 274 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 275 | void AddSpace(Space* space); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 276 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 277 | void VerifyObjectLocked(const Object *obj); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 278 | |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 279 | void VerifyHeapLocked(); |
| 280 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 281 | static void VerificationCallback(Object* obj, void* arg); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 282 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 283 | UniquePtr<Mutex> lock_; |
| 284 | UniquePtr<ConditionVariable> condition_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 285 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 286 | Spaces spaces_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 287 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 288 | // The alloc space which we are currently allocating into. |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 289 | AllocSpace* alloc_space_; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 290 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 291 | // The mod-union table remembers all of the referneces from the image space to the alloc / |
| 292 | // zygote spaces. |
| 293 | UniquePtr<ModUnionTable> mod_union_table_; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 294 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 295 | // This table holds all of the references from the zygote space to the alloc space. |
| 296 | UniquePtr<ModUnionTable> zygote_mod_union_table_; |
| 297 | |
| 298 | UniquePtr<CardTable> card_table_; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 299 | |
| 300 | // Used by the image writer to disable card marking on copied objects |
| 301 | // TODO: remove |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 302 | bool card_marking_disabled_; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 303 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 304 | // True while the garbage collector is running. |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 305 | volatile bool is_gc_running_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 306 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 307 | // Bytes until concurrent GC starts. |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 308 | size_t concurrent_start_bytes_; |
| 309 | size_t concurrent_start_size_; |
| 310 | size_t concurrent_min_free_; |
| 311 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 312 | UniquePtr<HeapBitmap> live_bitmap_; |
| 313 | UniquePtr<HeapBitmap> mark_bitmap_; |
| 314 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 315 | // True while the garbage collector is trying to signal the GC daemon thread. |
| 316 | // This flag is needed to prevent recursion from occurring when the JNI calls |
| 317 | // allocate memory and request another GC. |
| 318 | bool try_running_gc_; |
| 319 | |
| 320 | // Used to ensure that we don't ever recursively request GC. |
| 321 | bool requesting_gc_; |
| 322 | |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 323 | // Mark stack that we reuse to avoid re-allocating the mark stack |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 324 | UniquePtr<MarkStack> mark_stack_; |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 325 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 326 | // Number of bytes allocated. Adjusted after each allocation and free. |
| 327 | size_t num_bytes_allocated_; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 328 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 329 | // Number of objects allocated. Adjusted after each allocation and free. |
| 330 | size_t num_objects_allocated_; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 331 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 332 | // Last trim time |
| 333 | uint64_t last_trim_time_; |
| 334 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 335 | // offset of java.lang.ref.Reference.referent |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 336 | MemberOffset reference_referent_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 337 | |
| 338 | // offset of java.lang.ref.Reference.queue |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 339 | MemberOffset reference_queue_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 340 | |
| 341 | // offset of java.lang.ref.Reference.queueNext |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 342 | MemberOffset reference_queueNext_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 343 | |
| 344 | // offset of java.lang.ref.Reference.pendingNext |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 345 | MemberOffset reference_pendingNext_offset_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 346 | |
| 347 | // offset of java.lang.ref.FinalizerReference.zombie |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 348 | MemberOffset finalizer_reference_zombie_offset_; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 349 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 350 | // If we have a zygote space. |
| 351 | bool have_zygote_space_; |
| 352 | |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 353 | // Target ideal heap utilization ratio |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 354 | float target_utilization_; |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 355 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 356 | bool verify_objects_; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 357 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 358 | friend class ScopedHeapLock; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 359 | FRIEND_TEST(SpaceTest, AllocAndFree); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 360 | FRIEND_TEST(SpaceTest, AllocAndFreeList); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 361 | FRIEND_TEST(SpaceTest, ZygoteSpace); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 362 | friend class SpaceTest; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 363 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 364 | DISALLOW_IMPLICIT_CONSTRUCTORS(Heap); |
| 365 | }; |
| 366 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 367 | } // namespace art |
| 368 | |
| 369 | #endif // ART_SRC_HEAP_H_ |