Commit 69be929b authored by Michael Lippautz's avatar Michael Lippautz Committed by V8 LUCI CQ

cppgc: Fix marked bytes accounting for weak containers

Conceptually, Oilpan uses tri-color marking even though the
implementatin only uses a single mark bit. The difference between gray
and black is represented by the fact that an objet is contained within
a worklist.

Live bytes are accounted on gray->black transition and must only
happen once. This is generally implemented when retrieving an object
from the work list and processing it.

For weak containers this CL fixes the following issues:

1. Weak containers that are strongified during stack scanning were
   double accounted as they were just added to the marking worklist.
   Instead, directly process them during stack scanning.
2. Accounting was missing in case of purely weak collections without
   ephemeron tracing. In such a case, the backing store would not be
   added to a worklist and be considered as black immediately. The fix
   is to directly account the marked bytes in such a scenario.

Bug: chromium:1056170
Change-Id: I350ae1b90ad1753d024a3ce33fc3ec3126a2095d
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2900661Reviewed-by: 's avatarOmer Katz <omerkatz@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#74629}
parent 6d068fa7
......@@ -255,6 +255,7 @@ void MarkingStateBase::ProcessWeakContainer(const void* object,
// Only mark the container initially. Its buckets will be processed after
// marking.
if (!MarkNoPush(header)) return;
RegisterWeakContainer(header);
// Register final weak processing of the backing store.
......@@ -264,7 +265,13 @@ void MarkingStateBase::ProcessWeakContainer(const void* object,
// the TraceDescriptor will be nullptr. For ephemerons the callback will be
// non-nullptr so that the container is traced and the ephemeron pairs are
// processed.
if (desc.callback) PushMarked(header, desc);
if (desc.callback) {
PushMarked(header, desc);
} else {
// For weak containers, there's no trace callback and no processing loop to
// update the marked bytes, hence inline that here.
AccountMarkedBytes(header);
}
}
void MarkingStateBase::ProcessEphemeron(const void* key, const void* value,
......@@ -308,7 +315,7 @@ class MutatorMarkingState : public MarkingStateBase {
return MutatorMarkingState::MarkingStateBase::MarkNoPush(header);
}
inline void PushMarkedWeakContainer(HeapObjectHeader&);
inline void ReTraceMarkedWeakContainer(cppgc::Visitor&, HeapObjectHeader&);
inline void DynamicallyMarkAddress(ConstAddress);
......@@ -343,13 +350,13 @@ class MutatorMarkingState : public MarkingStateBase {
} recently_retraced_weak_containers_;
};
void MutatorMarkingState::PushMarkedWeakContainer(HeapObjectHeader& header) {
void MutatorMarkingState::ReTraceMarkedWeakContainer(cppgc::Visitor& visitor,
HeapObjectHeader& header) {
DCHECK(weak_containers_worklist_.Contains(&header));
recently_retraced_weak_containers_.Insert(&header);
PushMarked(
header,
{header.ObjectStart(),
GlobalGCInfoTable::GCInfoFromIndex(header.GetGCInfoIndex()).trace});
// Don't push to the marking worklist to avoid double accounting of marked
// bytes as the container is already accounted for.
header.Trace(&visitor);
}
void MutatorMarkingState::DynamicallyMarkAddress(ConstAddress address) {
......
......@@ -56,7 +56,7 @@ void ConservativeMarkingVisitor::VisitFullyConstructedConservatively(
HeapObjectHeader& header) {
if (header.IsMarked()) {
if (marking_state_.IsMarkedWeakContainer(header))
marking_state_.PushMarkedWeakContainer(header);
marking_state_.ReTraceMarkedWeakContainer(visitor_, header);
return;
}
ConservativeTracingVisitor::VisitFullyConstructedConservatively(header);
......
......@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "include/cppgc/allocation.h"
#include "src/base/macros.h"
#include "src/heap/cppgc/marker.h"
#include "src/heap/cppgc/marking-visitor.h"
#include "src/heap/cppgc/stats-collector.h"
......@@ -18,6 +19,8 @@ class WeakContainerTest : public testing::TestWithHeap {
using Config = Marker::MarkingConfig;
void StartMarking() {
CHECK_EQ(0u,
Heap::From(GetHeap())->AsBase().stats_collector()->marked_bytes());
Config config = {Config::CollectionType::kMajor,
Config::StackState::kNoHeapPointers,
Config::MarkingType::kIncremental};
......@@ -27,11 +30,23 @@ class WeakContainerTest : public testing::TestWithHeap {
void FinishMarking(Config::StackState stack_state) {
GetMarkerRef()->FinishMarking(stack_state);
marked_bytes_ =
Heap::From(GetHeap())->AsBase().stats_collector()->marked_bytes();
GetMarkerRef().reset();
Heap::From(GetHeap())->stats_collector()->NotifySweepingCompleted();
}
size_t GetMarkedBytes() const { return marked_bytes_; }
private:
size_t marked_bytes_ = 0;
};
template <typename T>
constexpr size_t SizeOf() {
return RoundUp<kAllocationGranularity>(sizeof(T) + sizeof(HeapObjectHeader));
}
class TraceableGCed : public GarbageCollected<TraceableGCed> {
public:
void Trace(cppgc::Visitor*) const { n_trace_calls++; }
......@@ -83,6 +98,7 @@ TEST_F(WeakContainerTest, TraceableGCedTraced) {
GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback, nullptr);
FinishMarking(Config::StackState::kNoHeapPointers);
EXPECT_NE(0u, TraceableGCed::n_trace_calls);
EXPECT_EQ(SizeOf<TraceableGCed>(), GetMarkedBytes());
access(obj);
}
......@@ -94,6 +110,7 @@ TEST_F(WeakContainerTest, NonTraceableGCedNotTraced) {
GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback, nullptr);
FinishMarking(Config::StackState::kNoHeapPointers);
EXPECT_EQ(0u, NonTraceableGCed::n_trace_calls);
EXPECT_EQ(SizeOf<NonTraceableGCed>(), GetMarkedBytes());
access(obj);
}
......@@ -105,33 +122,32 @@ TEST_F(WeakContainerTest, NonTraceableGCedNotTracedConservatively) {
GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback, nullptr);
FinishMarking(Config::StackState::kMayContainHeapPointers);
EXPECT_NE(0u, NonTraceableGCed::n_trace_calls);
EXPECT_EQ(SizeOf<NonTraceableGCed>(), GetMarkedBytes());
access(obj);
}
TEST_F(WeakContainerTest, PreciseGCTracesWeakContainerWhenTraced) {
TraceableGCed* obj =
MakeGarbageCollected<TraceableGCed>(GetAllocationHandle());
TraceableGCed::n_trace_calls = 0u;
StartMarking();
GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback, nullptr);
FinishMarking(Config::StackState::kNoHeapPointers);
EXPECT_EQ(1u, TraceableGCed::n_trace_calls);
EXPECT_EQ(SizeOf<TraceableGCed>(), GetMarkedBytes());
access(obj);
}
TEST_F(WeakContainerTest, ConservativeGCTracesWeakContainer) {
size_t trace_count_without_conservative;
{
TraceableGCed* obj =
MakeGarbageCollected<TraceableGCed>(GetAllocationHandle());
TraceableGCed::n_trace_calls = 0u;
StartMarking();
GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback,
nullptr);
FinishMarking(Config::StackState::kNoHeapPointers);
trace_count_without_conservative = TraceableGCed::n_trace_calls;
access(obj);
}
{
TraceableGCed* obj =
MakeGarbageCollected<TraceableGCed>(GetAllocationHandle());
TraceableGCed::n_trace_calls = 0u;
StartMarking();
GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback,
nullptr);
FinishMarking(Config::StackState::kMayContainHeapPointers);
EXPECT_LT(trace_count_without_conservative, TraceableGCed::n_trace_calls);
access(obj);
}
TraceableGCed* obj =
MakeGarbageCollected<TraceableGCed>(GetAllocationHandle());
TraceableGCed::n_trace_calls = 0u;
StartMarking();
GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback, nullptr);
FinishMarking(Config::StackState::kMayContainHeapPointers);
EXPECT_EQ(2u, TraceableGCed::n_trace_calls);
EXPECT_EQ(SizeOf<TraceableGCed>(), GetMarkedBytes());
access(obj);
}
TEST_F(WeakContainerTest, ConservativeGCTracesWeakContainerOnce) {
......@@ -146,6 +162,7 @@ TEST_F(WeakContainerTest, ConservativeGCTracesWeakContainerOnce) {
GetMarkerRef()->Visitor().TraceWeakContainer(obj, EmptyWeakCallback, nullptr);
FinishMarking(Config::StackState::kMayContainHeapPointers);
EXPECT_EQ(1u, NonTraceableGCed::n_trace_calls);
EXPECT_EQ(SizeOf<NonTraceableGCed>(), GetMarkedBytes());
access(obj);
}
......@@ -174,6 +191,7 @@ TEST_F(WeakContainerTest, WeakContainerWeakCallbackCalled) {
obj);
FinishMarking(Config::StackState::kMayContainHeapPointers);
EXPECT_NE(0u, WeakCallback::n_callback_called);
EXPECT_EQ(SizeOf<TraceableGCed>(), GetMarkedBytes());
EXPECT_EQ(obj, WeakCallback::obj);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment