1 /*
2  * Copyright (C) 2014 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  */
16 
17 #include "zygote_space.h"
18 
19 #include "base/mutex-inl.h"
20 #include "base/utils.h"
21 #include "gc/accounting/card_table-inl.h"
22 #include "gc/accounting/space_bitmap-inl.h"
23 #include "gc/heap.h"
24 #include "mirror/object-readbarrier-inl.h"
25 #include "runtime.h"
26 #include "thread-current-inl.h"
27 
28 namespace art {
29 namespace gc {
30 namespace space {
31 
32 class CountObjectsAllocated {
33  public:
CountObjectsAllocated(size_t * objects_allocated)34   explicit CountObjectsAllocated(size_t* objects_allocated)
35       : objects_allocated_(objects_allocated) {}
36 
operator ()(mirror::Object * obj ATTRIBUTE_UNUSED) const37   void operator()(mirror::Object* obj ATTRIBUTE_UNUSED) const {
38     ++*objects_allocated_;
39   }
40 
41  private:
42   size_t* const objects_allocated_;
43 };
44 
Create(const std::string & name,MemMap && mem_map,accounting::ContinuousSpaceBitmap && live_bitmap,accounting::ContinuousSpaceBitmap && mark_bitmap)45 ZygoteSpace* ZygoteSpace::Create(const std::string& name,
46                                  MemMap&& mem_map,
47                                  accounting::ContinuousSpaceBitmap&& live_bitmap,
48                                  accounting::ContinuousSpaceBitmap&& mark_bitmap) {
49   DCHECK(live_bitmap.IsValid());
50   DCHECK(mark_bitmap.IsValid());
51   size_t objects_allocated = 0;
52   CountObjectsAllocated visitor(&objects_allocated);
53   ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
54   live_bitmap.VisitMarkedRange(reinterpret_cast<uintptr_t>(mem_map.Begin()),
55                                reinterpret_cast<uintptr_t>(mem_map.End()), visitor);
56   ZygoteSpace* zygote_space = new ZygoteSpace(name, std::move(mem_map), objects_allocated);
57   zygote_space->live_bitmap_ = std::move(live_bitmap);
58   zygote_space->mark_bitmap_ = std::move(mark_bitmap);
59   return zygote_space;
60 }
61 
SetMarkBitInLiveObjects()62 void ZygoteSpace::SetMarkBitInLiveObjects() {
63   GetLiveBitmap()->VisitMarkedRange(reinterpret_cast<uintptr_t>(Begin()),
64                                     reinterpret_cast<uintptr_t>(Limit()),
65                                     [](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
66                                       bool success = obj->AtomicSetMarkBit(0, 1);
67                                       CHECK(success);
68                                     });
69 }
70 
Clear()71 void ZygoteSpace::Clear() {
72   UNIMPLEMENTED(FATAL);
73   UNREACHABLE();
74 }
75 
ZygoteSpace(const std::string & name,MemMap && mem_map,size_t objects_allocated)76 ZygoteSpace::ZygoteSpace(const std::string& name, MemMap&& mem_map, size_t objects_allocated)
77     : ContinuousMemMapAllocSpace(name,
78                                  std::move(mem_map),
79                                  mem_map.Begin(),
80                                  mem_map.End(),
81                                  mem_map.End(),
82                                  kGcRetentionPolicyFullCollect),
83       objects_allocated_(objects_allocated) {
84 }
85 
Dump(std::ostream & os) const86 void ZygoteSpace::Dump(std::ostream& os) const {
87   os << GetType()
88       << " begin=" << reinterpret_cast<void*>(Begin())
89       << ",end=" << reinterpret_cast<void*>(End())
90       << ",size=" << PrettySize(Size())
91       << ",name=\"" << GetName() << "\"]";
92 }
93 
Alloc(Thread *,size_t,size_t *,size_t *,size_t *)94 mirror::Object* ZygoteSpace::Alloc(Thread*, size_t, size_t*, size_t*, size_t*) {
95   UNIMPLEMENTED(FATAL);
96   UNREACHABLE();
97 }
98 
AllocationSize(mirror::Object *,size_t *)99 size_t ZygoteSpace::AllocationSize(mirror::Object*, size_t*) {
100   UNIMPLEMENTED(FATAL);
101   UNREACHABLE();
102 }
103 
Free(Thread *,mirror::Object *)104 size_t ZygoteSpace::Free(Thread*, mirror::Object*) {
105   UNIMPLEMENTED(FATAL);
106   UNREACHABLE();
107 }
108 
FreeList(Thread *,size_t,mirror::Object **)109 size_t ZygoteSpace::FreeList(Thread*, size_t, mirror::Object**) {
110   UNIMPLEMENTED(FATAL);
111   UNREACHABLE();
112 }
113 
LogFragmentationAllocFailure(std::ostream &,size_t)114 void ZygoteSpace::LogFragmentationAllocFailure(std::ostream&, size_t) {
115   UNIMPLEMENTED(FATAL);
116   UNREACHABLE();
117 }
118 
SweepCallback(size_t num_ptrs,mirror::Object ** ptrs,void * arg)119 void ZygoteSpace::SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg) {
120   SweepCallbackContext* context = static_cast<SweepCallbackContext*>(arg);
121   DCHECK(context->space->IsZygoteSpace());
122   ZygoteSpace* zygote_space = context->space->AsZygoteSpace();
123   Locks::heap_bitmap_lock_->AssertExclusiveHeld(context->self);
124   accounting::CardTable* card_table = Runtime::Current()->GetHeap()->GetCardTable();
125   // If the bitmaps aren't swapped we need to clear the bits since the GC isn't going to re-swap
126   // the bitmaps as an optimization.
127   if (!context->swap_bitmaps) {
128     accounting::ContinuousSpaceBitmap* bitmap = zygote_space->GetLiveBitmap();
129     for (size_t i = 0; i < num_ptrs; ++i) {
130       bitmap->Clear(ptrs[i]);
131     }
132   }
133   // We don't free any actual memory to avoid dirtying the shared zygote pages.
134   for (size_t i = 0; i < num_ptrs; ++i) {
135     // Need to mark the card since this will update the mod-union table next GC cycle.
136     card_table->MarkCard(ptrs[i]);
137   }
138   zygote_space->objects_allocated_.fetch_sub(num_ptrs);
139 }
140 
141 }  // namespace space
142 }  // namespace gc
143 }  // namespace art
144