1 /*
2  * Copyright (C) 2012 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 "sticky_mark_sweep.h"
18 
19 #include "gc/accounting/atomic_stack.h"
20 #include "gc/accounting/card_table.h"
21 #include "gc/heap.h"
22 #include "gc/space/large_object_space.h"
23 #include "gc/space/space-inl.h"
24 #include "runtime.h"
25 #include "thread-current-inl.h"
26 
27 namespace art {
28 namespace gc {
29 namespace collector {
30 
StickyMarkSweep(Heap * heap,bool is_concurrent,const std::string & name_prefix)31 StickyMarkSweep::StickyMarkSweep(Heap* heap, bool is_concurrent, const std::string& name_prefix)
32     : PartialMarkSweep(heap, is_concurrent, name_prefix.empty() ? "sticky " : name_prefix) {
33   cumulative_timings_.SetName(GetName());
34 }
35 
BindBitmaps()36 void StickyMarkSweep::BindBitmaps() {
37   PartialMarkSweep::BindBitmaps();
38   WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
39   // For sticky GC, we want to bind the bitmaps of all spaces as the allocation stack lets us
40   // know what was allocated since the last GC. A side-effect of binding the allocation space mark
41   // and live bitmap is that marking the objects will place them in the live bitmap.
42   for (const auto& space : GetHeap()->GetContinuousSpaces()) {
43     if (space->IsContinuousMemMapAllocSpace() &&
44         space->GetGcRetentionPolicy() == space::kGcRetentionPolicyAlwaysCollect) {
45       DCHECK(space->IsContinuousMemMapAllocSpace());
46       space->AsContinuousMemMapAllocSpace()->BindLiveToMarkBitmap();
47     }
48   }
49   for (const auto& space : GetHeap()->GetDiscontinuousSpaces()) {
50     CHECK(space->IsLargeObjectSpace());
51     space->AsLargeObjectSpace()->CopyLiveToMarked();
52   }
53 }
54 
MarkReachableObjects()55 void StickyMarkSweep::MarkReachableObjects() {
56   // All reachable objects must be referenced by a root or a dirty card, so we can clear the mark
57   // stack here since all objects in the mark stack will get scanned by the card scanning anyways.
58   // TODO: Not put these objects in the mark stack in the first place.
59   mark_stack_->Reset();
60   RecursiveMarkDirtyObjects(false, accounting::CardTable::kCardDirty - 1);
61 }
62 
MarkConcurrentRoots(VisitRootFlags flags)63 void StickyMarkSweep::MarkConcurrentRoots(VisitRootFlags flags) {
64   TimingLogger::ScopedTiming t(__FUNCTION__, GetTimings());
65   // Visit all runtime roots and clear dirty flags including class loader. This is done to prevent
66   // incorrect class unloading since the GC does not card mark when storing the class during
67   // object allocation. Doing this for each allocation would be slow.
68   // Since the card is not dirty, it means the object may not get scanned. This can cause class
69   // unloading to occur even though the class and class loader are reachable through the object's
70   // class.
71   Runtime::Current()->VisitConcurrentRoots(
72       this,
73       static_cast<VisitRootFlags>(flags | kVisitRootFlagClassLoader));
74 }
75 
Sweep(bool swap_bitmaps ATTRIBUTE_UNUSED)76 void StickyMarkSweep::Sweep(bool swap_bitmaps ATTRIBUTE_UNUSED) {
77   SweepArray(GetHeap()->GetLiveStack(), false);
78 }
79 
80 }  // namespace collector
81 }  // namespace gc
82 }  // namespace art
83