Commit f779fba4 authored by Omer Katz's avatar Omer Katz Committed by V8 LUCI CQ

cppgc: Implement incremental sweeping in tasks.

This was missing from Ulan's implementation for
CollectCustomSpaceStatisticsAtLastGC.

Bug: chromium:1056170, chromium:1181269
Change-Id: I72354e4f04873095eac5cb39ed188ed83de0bd26
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2880219Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Omer Katz <omerkatz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#74483}
parent 91119c19
......@@ -5,6 +5,7 @@
#include "src/heap/cppgc-js/cpp-heap.h"
#include <cstdint>
#include <memory>
#include <numeric>
#include "include/cppgc/heap-consistency.h"
......@@ -30,6 +31,7 @@
#include "src/heap/cppgc/marking-visitor.h"
#include "src/heap/cppgc/object-allocator.h"
#include "src/heap/cppgc/prefinalizer-handler.h"
#include "src/heap/cppgc/raw-heap.h"
#include "src/heap/cppgc/stats-collector.h"
#include "src/heap/cppgc/sweeper.h"
#include "src/heap/embedder-tracing.h"
......@@ -491,15 +493,15 @@ void CppHeap::FinalizeIncrementalGarbageCollectionForTesting(
sweeper_.FinishIfRunning();
}
void CppHeap::CollectCustomSpaceStatisticsAtLastGC(
namespace {
void ReportCustomSpaceStatistics(
cppgc::internal::RawHeap& raw_heap,
std::vector<cppgc::CustomSpaceIndex> custom_spaces,
std::unique_ptr<CustomSpaceStatisticsReceiver> receiver) {
// TODO(1181269): Use tasks to help the sweeper incrementally instead of
// finalizing atomically.
sweeper().FinishIfRunning();
for (auto custom_space_index : custom_spaces) {
const cppgc::internal::BaseSpace* space =
raw_heap().CustomSpace(custom_space_index);
raw_heap.CustomSpace(custom_space_index);
size_t allocated_bytes = std::accumulate(
space->begin(), space->end(), 0, [](size_t sum, auto* page) {
return sum + page->AllocatedBytesAtLastGC();
......@@ -508,5 +510,61 @@ void CppHeap::CollectCustomSpaceStatisticsAtLastGC(
}
}
class CollectCustomSpaceStatisticsAtLastGCTask final : public v8::Task {
public:
static constexpr v8::base::TimeDelta kTaskDelayMs =
v8::base::TimeDelta::FromMilliseconds(10);
CollectCustomSpaceStatisticsAtLastGCTask(
cppgc::internal::HeapBase& heap,
std::vector<cppgc::CustomSpaceIndex> custom_spaces,
std::unique_ptr<CustomSpaceStatisticsReceiver> receiver)
: heap_(heap),
custom_spaces_(std::move(custom_spaces)),
receiver_(std::move(receiver)) {}
void Run() final {
cppgc::internal::Sweeper& sweeper = heap_.sweeper();
if (sweeper.PerformSweepOnMutatorThread(kStepSizeMs.InSecondsF())) {
heap_.platform()->GetForegroundTaskRunner()->PostDelayedTask(
std::make_unique<CollectCustomSpaceStatisticsAtLastGCTask>(
heap_, std::move(custom_spaces_), std::move(receiver_)),
kTaskDelayMs.InSecondsF());
} else {
ReportCustomSpaceStatistics(heap_.raw_heap(), std::move(custom_spaces_),
std::move(receiver_));
}
}
private:
static constexpr v8::base::TimeDelta kStepSizeMs =
v8::base::TimeDelta::FromMilliseconds(5);
cppgc::internal::HeapBase& heap_;
std::vector<cppgc::CustomSpaceIndex> custom_spaces_;
std::unique_ptr<CustomSpaceStatisticsReceiver> receiver_;
};
constexpr v8::base::TimeDelta
CollectCustomSpaceStatisticsAtLastGCTask::kTaskDelayMs;
constexpr v8::base::TimeDelta
CollectCustomSpaceStatisticsAtLastGCTask::kStepSizeMs;
} // namespace
void CppHeap::CollectCustomSpaceStatisticsAtLastGC(
std::vector<cppgc::CustomSpaceIndex> custom_spaces,
std::unique_ptr<CustomSpaceStatisticsReceiver> receiver) {
if (sweeper().IsSweepingInProgress()) {
platform()->GetForegroundTaskRunner()->PostDelayedTask(
std::make_unique<CollectCustomSpaceStatisticsAtLastGCTask>(
AsBase(), std::move(custom_spaces), std::move(receiver)),
CollectCustomSpaceStatisticsAtLastGCTask::kTaskDelayMs.InSecondsF());
return;
}
ReportCustomSpaceStatistics(raw_heap(), std::move(custom_spaces),
std::move(receiver));
}
} // namespace internal
} // namespace v8
......@@ -52,6 +52,7 @@ namespace internal {
V(MarkVisitRememberedSets) \
V(SweepInvokePreFinalizers) \
V(SweepIdleStep) \
V(SweepInTask) \
V(SweepOnAllocation) \
V(SweepFinalize)
......
......@@ -665,6 +665,33 @@ class Sweeper::SweeperImpl final {
bool IsSweepingInProgress() const { return is_in_progress_; }
bool PerformSweepOnMutatorThread(double deadline_in_seconds,
StatsCollector::ScopeId internal_scope_id) {
if (!is_in_progress_) return true;
MutatorThreadSweepingScope sweeping_in_progresss(*this);
bool sweep_complete;
{
StatsCollector::EnabledScope stats_scope(
stats_collector_, StatsCollector::kIncrementalSweep);
MutatorThreadSweeper sweeper(&space_states_, platform_);
{
StatsCollector::EnabledScope stats_scope(
stats_collector_, internal_scope_id, "deltaInSeconds",
deadline_in_seconds - platform_->MonotonicallyIncreasingTime());
sweep_complete = sweeper.SweepWithDeadline(deadline_in_seconds);
}
if (sweep_complete) {
FinalizeSweep();
}
}
if (sweep_complete) NotifyDone();
return sweep_complete;
}
private:
class MutatorThreadSweepingScope final {
public:
......@@ -701,33 +728,12 @@ class Sweeper::SweeperImpl final {
private:
void Run(double deadline_in_seconds) override {
if (handle_.IsCanceled() || !sweeper_->is_in_progress_) return;
if (handle_.IsCanceled()) return;
MutatorThreadSweepingScope sweeping_in_progresss(*sweeper_);
bool sweep_complete;
{
StatsCollector::EnabledScope stats_scope(
sweeper_->stats_collector_, StatsCollector::kIncrementalSweep);
MutatorThreadSweeper sweeper(&sweeper_->space_states_,
sweeper_->platform_);
{
StatsCollector::EnabledScope stats_scope(
sweeper_->stats_collector_, StatsCollector::kSweepIdleStep,
"idleDeltaInSeconds",
(deadline_in_seconds -
sweeper_->platform_->MonotonicallyIncreasingTime()));
sweep_complete = sweeper.SweepWithDeadline(deadline_in_seconds);
}
if (sweep_complete) {
sweeper_->FinalizeSweep();
} else {
sweeper_->ScheduleIncrementalSweeping();
}
if (!sweeper_->PerformSweepOnMutatorThread(
deadline_in_seconds, StatsCollector::kSweepIdleStep)) {
sweeper_->ScheduleIncrementalSweeping();
}
if (sweep_complete) sweeper_->NotifyDone();
}
Handle GetHandle() const { return handle_; }
......@@ -807,5 +813,10 @@ bool Sweeper::IsSweepingInProgress() const {
return impl_->IsSweepingInProgress();
}
bool Sweeper::PerformSweepOnMutatorThread(double deadline_in_seconds) {
return impl_->PerformSweepOnMutatorThread(deadline_in_seconds,
StatsCollector::kSweepInTask);
}
} // namespace internal
} // namespace cppgc
......@@ -9,6 +9,7 @@
#include "include/cppgc/heap.h"
#include "src/base/macros.h"
#include "src/base/platform/time.h"
namespace cppgc {
......@@ -49,6 +50,9 @@ class V8_EXPORT_PRIVATE Sweeper final {
bool IsSweepingOnMutatorThread() const;
bool IsSweepingInProgress() const;
// Assist with sweeping. Returns true if sweeping is done.
bool PerformSweepOnMutatorThread(double deadline_in_seconds);
private:
void WaitForConcurrentSweepingForTesting();
......
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