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