Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef ART_RUNTIME_GC_COLLECTOR_CONCURRENT_COPYING_INL_H_ |
| 18 | #define ART_RUNTIME_GC_COLLECTOR_CONCURRENT_COPYING_INL_H_ |
| 19 | |
| 20 | #include "concurrent_copying.h" |
| 21 | |
| 22 | #include "gc/accounting/space_bitmap-inl.h" |
| 23 | #include "gc/heap.h" |
| 24 | #include "gc/space/region_space.h" |
| 25 | #include "lock_word.h" |
| 26 | |
| 27 | namespace art { |
| 28 | namespace gc { |
| 29 | namespace collector { |
| 30 | |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame^] | 31 | inline mirror::Object* ConcurrentCopying::MarkUnevacFromSpaceRegionOrImmuneSpace( |
| 32 | mirror::Object* ref, accounting::ContinuousSpaceBitmap* bitmap) { |
| 33 | // For the Baker-style RB, in a rare case, we could incorrectly change the object from white |
| 34 | // to gray even though the object has already been marked through. This happens if a mutator |
| 35 | // thread gets preempted before the AtomicSetReadBarrierPointer below, GC marks through the |
| 36 | // object (changes it from white to gray and back to white), and the thread runs and |
| 37 | // incorrectly changes it from white to gray. We need to detect such "false gray" cases and |
| 38 | // change the objects back to white at the end of marking. |
| 39 | if (kUseBakerReadBarrier) { |
| 40 | // Test the bitmap first to reduce the chance of false gray cases. |
| 41 | if (bitmap->Test(ref)) { |
| 42 | return ref; |
| 43 | } |
| 44 | } |
| 45 | // This may or may not succeed, which is ok because the object may already be gray. |
| 46 | bool cas_success = false; |
| 47 | if (kUseBakerReadBarrier) { |
| 48 | cas_success = ref->AtomicSetReadBarrierPointer(ReadBarrier::WhitePtr(), |
| 49 | ReadBarrier::GrayPtr()); |
| 50 | } |
| 51 | if (bitmap->AtomicTestAndSet(ref)) { |
| 52 | // Already marked. |
| 53 | if (kUseBakerReadBarrier && |
| 54 | cas_success && |
| 55 | // The object could be white here if a thread gets preempted after a success at the |
| 56 | // above AtomicSetReadBarrierPointer, GC has marked through it, and the thread runs up |
| 57 | // to this point. |
| 58 | ref->GetReadBarrierPointer() == ReadBarrier::GrayPtr()) { |
| 59 | // Register a "false-gray" object to change it from gray to white at the end of marking. |
| 60 | PushOntoFalseGrayStack(ref); |
| 61 | } |
| 62 | } else { |
| 63 | // Newly marked. |
| 64 | if (kUseBakerReadBarrier) { |
| 65 | DCHECK_EQ(ref->GetReadBarrierPointer(), ReadBarrier::GrayPtr()); |
| 66 | } |
| 67 | PushOntoMarkStack(ref); |
| 68 | } |
| 69 | return ref; |
| 70 | } |
| 71 | |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 72 | inline mirror::Object* ConcurrentCopying::Mark(mirror::Object* from_ref) { |
| 73 | if (from_ref == nullptr) { |
| 74 | return nullptr; |
| 75 | } |
| 76 | DCHECK(heap_->collector_type_ == kCollectorTypeCC); |
| 77 | if (UNLIKELY(kUseBakerReadBarrier && !is_active_)) { |
| 78 | // In the lock word forward address state, the read barrier bits |
| 79 | // in the lock word are part of the stored forwarding address and |
| 80 | // invalid. This is usually OK as the from-space copy of objects |
| 81 | // aren't accessed by mutators due to the to-space |
| 82 | // invariant. However, during the dex2oat image writing relocation |
| 83 | // and the zygote compaction, objects can be in the forward |
| 84 | // address state (to store the forward/relocation addresses) and |
| 85 | // they can still be accessed and the invalid read barrier bits |
| 86 | // are consulted. If they look like gray but aren't really, the |
| 87 | // read barriers slow path can trigger when it shouldn't. To guard |
| 88 | // against this, return here if the CC collector isn't running. |
| 89 | return from_ref; |
| 90 | } |
| 91 | DCHECK(region_space_ != nullptr) << "Read barrier slow path taken when CC isn't running?"; |
| 92 | space::RegionSpace::RegionType rtype = region_space_->GetRegionType(from_ref); |
| 93 | switch (rtype) { |
| 94 | case space::RegionSpace::RegionType::kRegionTypeToSpace: |
| 95 | // It's already marked. |
| 96 | return from_ref; |
| 97 | case space::RegionSpace::RegionType::kRegionTypeFromSpace: { |
| 98 | mirror::Object* to_ref = GetFwdPtr(from_ref); |
| 99 | if (kUseBakerReadBarrier) { |
| 100 | DCHECK_NE(to_ref, ReadBarrier::GrayPtr()) |
| 101 | << "from_ref=" << from_ref << " to_ref=" << to_ref; |
| 102 | } |
| 103 | if (to_ref == nullptr) { |
| 104 | // It isn't marked yet. Mark it by copying it to the to-space. |
| 105 | to_ref = Copy(from_ref); |
| 106 | } |
| 107 | DCHECK(region_space_->IsInToSpace(to_ref) || heap_->non_moving_space_->HasAddress(to_ref)) |
| 108 | << "from_ref=" << from_ref << " to_ref=" << to_ref; |
| 109 | return to_ref; |
| 110 | } |
| 111 | case space::RegionSpace::RegionType::kRegionTypeUnevacFromSpace: { |
Hiroshi Yamauchi | 8e67465 | 2015-12-22 11:09:18 -0800 | [diff] [blame^] | 112 | return MarkUnevacFromSpaceRegionOrImmuneSpace(from_ref, region_space_bitmap_); |
Hiroshi Yamauchi | 723e6ce | 2015-10-28 20:59:47 -0700 | [diff] [blame] | 113 | } |
| 114 | case space::RegionSpace::RegionType::kRegionTypeNone: |
| 115 | return MarkNonMoving(from_ref); |
| 116 | default: |
| 117 | UNREACHABLE(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | inline mirror::Object* ConcurrentCopying::GetFwdPtr(mirror::Object* from_ref) { |
| 122 | DCHECK(region_space_->IsInFromSpace(from_ref)); |
| 123 | LockWord lw = from_ref->GetLockWord(false); |
| 124 | if (lw.GetState() == LockWord::kForwardingAddress) { |
| 125 | mirror::Object* fwd_ptr = reinterpret_cast<mirror::Object*>(lw.ForwardingAddress()); |
| 126 | DCHECK(fwd_ptr != nullptr); |
| 127 | return fwd_ptr; |
| 128 | } else { |
| 129 | return nullptr; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } // namespace collector |
| 134 | } // namespace gc |
| 135 | } // namespace art |
| 136 | |
| 137 | #endif // ART_RUNTIME_GC_COLLECTOR_CONCURRENT_COPYING_INL_H_ |