Compacting collector.
The compacting collector is currently similar to semispace. It works by
copying objects back and forth between two bump pointer spaces. There
are types of objects which are "non-movable" due to current runtime
limitations. These are Classes, Methods, and Fields.
Bump pointer spaces are a new type of continuous alloc space which have
no lock in the allocation code path. When you allocate from these it uses
atomic operations to increase an index. Traversing the objects in the bump
pointer space relies on Object::SizeOf matching the allocated size exactly.
Runtime changes:
JNI::GetArrayElements returns copies objects if you attempt to get the
backing data of a movable array. For GetArrayElementsCritical, we return
direct backing storage for any types of arrays, but temporarily disable
the GC until the critical region is completed.
Added a new runtime call called VisitObjects, this is used in place of
the old pattern which was flushing the allocation stack and walking
the bitmaps.
Changed image writer to be compaction safe and use object monitor word
for forwarding addresses.
Added a bunch of added SIRTs to ClassLinker, MethodLinker, etc..
TODO: Enable switching allocators, compacting on background, etc..
Bug: 8981901
Change-Id: I3c886fd322a6eef2b99388d19a765042ec26ab99
diff --git a/runtime/gc/space/bump_pointer_space.h b/runtime/gc/space/bump_pointer_space.h
new file mode 100644
index 0000000..0faac0c
--- /dev/null
+++ b/runtime/gc/space/bump_pointer_space.h
@@ -0,0 +1,149 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_H_
+#define ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_H_
+
+#include "space.h"
+
+namespace art {
+namespace gc {
+
+namespace collector {
+ class MarkSweep;
+} // namespace collector
+
+namespace space {
+
+// A bump pointer space is a space where objects may be allocated and garbage collected.
+class BumpPointerSpace : public ContinuousMemMapAllocSpace {
+ public:
+ typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg);
+
+ SpaceType GetType() const {
+ return kSpaceTypeBumpPointerSpace;
+ }
+
+ // Create a bump pointer space with the requested sizes. The requested base address is not
+ // guaranteed to be granted, if it is required, the caller should call Begin on the returned
+ // space to confirm the request was granted.
+ static BumpPointerSpace* Create(const std::string& name, size_t capacity, byte* requested_begin);
+
+ // Allocate num_bytes, returns nullptr if the space is full.
+ virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated);
+ mirror::Object* AllocNonvirtual(size_t num_bytes);
+
+ // Return the storage space required by obj.
+ virtual size_t AllocationSize(const mirror::Object* obj)
+ SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+
+ // Nos unless we support free lists.
+ virtual size_t Free(Thread*, mirror::Object*) {
+ return 0;
+ }
+ virtual size_t FreeList(Thread*, size_t, mirror::Object**) {
+ return 0;
+ }
+
+ size_t AllocationSizeNonvirtual(const mirror::Object* obj)
+ SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+ return obj->SizeOf();
+ }
+
+ // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
+ // maximum reserved size of the heap.
+ void ClearGrowthLimit() {
+ growth_end_ = Limit();
+ }
+
+ // Override capacity so that we only return the possibly limited capacity
+ size_t Capacity() const {
+ return growth_end_ - begin_;
+ }
+
+ // The total amount of memory reserved for the space.
+ size_t NonGrowthLimitCapacity() const {
+ return GetMemMap()->Size();
+ }
+
+ accounting::SpaceBitmap* GetLiveBitmap() const {
+ return nullptr;
+ }
+
+ accounting::SpaceBitmap* GetMarkBitmap() const {
+ return nullptr;
+ }
+
+ // Clear the memory and reset the pointer to the start of the space.
+ void Clear();
+
+ void Dump(std::ostream& os) const;
+
+ uint64_t GetBytesAllocated() {
+ return Size();
+ }
+
+ uint64_t GetObjectsAllocated() {
+ return num_objects_allocated_;
+ }
+
+ uint64_t GetTotalBytesAllocated() {
+ return total_bytes_allocated_;
+ }
+
+ uint64_t GetTotalObjectsAllocated() {
+ return total_objects_allocated_;
+ }
+
+ bool Contains(const mirror::Object* obj) const {
+ const byte* byte_obj = reinterpret_cast<const byte*>(obj);
+ return byte_obj >= Begin() && byte_obj < End();
+ }
+
+ // TODO: Change this? Mainly used for compacting to a particular region of memory.
+ BumpPointerSpace(const std::string& name, byte* begin, byte* limit);
+
+ // Return the object which comes after obj, while ensuring alignment.
+ static mirror::Object* GetNextObject(mirror::Object* obj)
+ SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
+
+ protected:
+ BumpPointerSpace(const std::string& name, MemMap* mem_map);
+
+ size_t InternalAllocationSize(const mirror::Object* obj);
+ mirror::Object* AllocWithoutGrowthLocked(size_t num_bytes, size_t* bytes_allocated)
+ EXCLUSIVE_LOCKS_REQUIRED(lock_);
+
+ // Approximate number of bytes which have been allocated into the space.
+ AtomicInteger num_objects_allocated_;
+ AtomicInteger total_bytes_allocated_;
+ AtomicInteger total_objects_allocated_;
+
+ // Alignment.
+ static constexpr size_t kAlignment = 8;
+
+ byte* growth_end_;
+
+ private:
+ friend class collector::MarkSweep;
+ DISALLOW_COPY_AND_ASSIGN(BumpPointerSpace);
+};
+
+} // namespace space
+} // namespace gc
+} // namespace art
+
+#endif // ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_H_