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