From 2601a515f98d643ce740e53954c8de7a9c515308 Mon Sep 17 00:00:00 2001 From: Matt Loring Date: Thu, 3 Aug 2017 07:05:36 -0700 Subject: [PATCH] deps: backport 6e9e2e5 from upstream V8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Original commit message: [heap] Move SweeperTask to CancelableTask This mitigates the problem of blocking on the main thread when the platform is unable to execute background tasks in a timely manner. Bug: v8:6655 Change-Id: Icdaae744ee73146b86b9a28c8035138746721971 Reviewed-on: https://chromium-review.googlesource.com/595467 Commit-Queue: Michael Lippautz Reviewed-by: Ulan Degenbaev Cr-Commit-Position: refs/heads/master@{#47036} Backport-PR-URL: https://github.com/nodejs/node/pull/15393 PR-URL: https://github.com/nodejs/node/pull/14001 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis Reviewed-By: Tobias Nießen --- deps/v8/src/heap/mark-compact.cc | 40 ++++++++++++++++++++------------ deps/v8/src/heap/mark-compact.h | 13 ++++++----- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/deps/v8/src/heap/mark-compact.cc b/deps/v8/src/heap/mark-compact.cc index cc47333f1dfb1e..73840639cead57 100644 --- a/deps/v8/src/heap/mark-compact.cc +++ b/deps/v8/src/heap/mark-compact.cc @@ -9,6 +9,7 @@ #include "src/base/atomicops.h" #include "src/base/bits.h" #include "src/base/sys-info.h" +#include "src/cancelable-task.h" #include "src/code-stubs.h" #include "src/compilation-cache.h" #include "src/deoptimizer.h" @@ -569,12 +570,14 @@ void MarkCompactCollector::ClearMarkbits() { heap_->lo_space()->ClearMarkingStateOfLiveObjects(); } -class MarkCompactCollector::Sweeper::SweeperTask : public v8::Task { +class MarkCompactCollector::Sweeper::SweeperTask final : public CancelableTask { public: - SweeperTask(Sweeper* sweeper, base::Semaphore* pending_sweeper_tasks, + SweeperTask(Isolate* isolate, Sweeper* sweeper, + base::Semaphore* pending_sweeper_tasks, base::AtomicNumber* num_sweeping_tasks, AllocationSpace space_to_start) - : sweeper_(sweeper), + : CancelableTask(isolate), + sweeper_(sweeper), pending_sweeper_tasks_(pending_sweeper_tasks), num_sweeping_tasks_(num_sweeping_tasks), space_to_start_(space_to_start) {} @@ -582,8 +585,7 @@ class MarkCompactCollector::Sweeper::SweeperTask : public v8::Task { virtual ~SweeperTask() {} private: - // v8::Task overrides. - void Run() override { + void RunInternal() final { DCHECK_GE(space_to_start_, FIRST_SPACE); DCHECK_LE(space_to_start_, LAST_PAGED_SPACE); const int offset = space_to_start_ - FIRST_SPACE; @@ -598,9 +600,9 @@ class MarkCompactCollector::Sweeper::SweeperTask : public v8::Task { pending_sweeper_tasks_->Signal(); } - Sweeper* sweeper_; - base::Semaphore* pending_sweeper_tasks_; - base::AtomicNumber* num_sweeping_tasks_; + Sweeper* const sweeper_; + base::Semaphore* const pending_sweeper_tasks_; + base::AtomicNumber* const num_sweeping_tasks_; AllocationSpace space_to_start_; DISALLOW_COPY_AND_ASSIGN(SweeperTask); @@ -618,15 +620,19 @@ void MarkCompactCollector::Sweeper::StartSweeping() { } void MarkCompactCollector::Sweeper::StartSweeperTasks() { + DCHECK_EQ(0, num_tasks_); + DCHECK_EQ(0, num_sweeping_tasks_.Value()); if (FLAG_concurrent_sweeping && sweeping_in_progress_) { ForAllSweepingSpaces([this](AllocationSpace space) { if (space == NEW_SPACE) return; num_sweeping_tasks_.Increment(1); - semaphore_counter_++; + SweeperTask* task = new SweeperTask(heap_->isolate(), this, + &pending_sweeper_tasks_semaphore_, + &num_sweeping_tasks_, space); + DCHECK_LT(num_tasks_, kMaxSweeperTasks); + task_ids_[num_tasks_++] = task->id(); V8::GetCurrentPlatform()->CallOnBackgroundThread( - new SweeperTask(this, &pending_sweeper_tasks_semaphore_, - &num_sweeping_tasks_, space), - v8::Platform::kShortRunningTask); + task, v8::Platform::kShortRunningTask); }); } } @@ -671,10 +677,14 @@ void MarkCompactCollector::Sweeper::EnsureCompleted() { [this](AllocationSpace space) { ParallelSweepSpace(space, 0); }); if (FLAG_concurrent_sweeping) { - while (semaphore_counter_ > 0) { - pending_sweeper_tasks_semaphore_.Wait(); - semaphore_counter_--; + for (int i = 0; i < num_tasks_; i++) { + if (heap_->isolate()->cancelable_task_manager()->TryAbort(task_ids_[i]) != + CancelableTaskManager::kTaskAborted) { + pending_sweeper_tasks_semaphore_.Wait(); + } } + num_tasks_ = 0; + num_sweeping_tasks_.SetValue(0); } ForAllSweepingSpaces([this](AllocationSpace space) { diff --git a/deps/v8/src/heap/mark-compact.h b/deps/v8/src/heap/mark-compact.h index 937dad1a91677f..9ea365e405d799 100644 --- a/deps/v8/src/heap/mark-compact.h +++ b/deps/v8/src/heap/mark-compact.h @@ -491,8 +491,6 @@ class MarkCompactCollector final : public MarkCompactCollectorBase { class Sweeper { public: - class SweeperTask; - enum FreeListRebuildingMode { REBUILD_FREE_LIST, IGNORE_FREE_LIST }; enum ClearOldToNewSlotsMode { DO_NOT_CLEAR, @@ -508,8 +506,8 @@ class MarkCompactCollector final : public MarkCompactCollectorBase { explicit Sweeper(Heap* heap) : heap_(heap), + num_tasks_(0), pending_sweeper_tasks_semaphore_(0), - semaphore_counter_(0), sweeping_in_progress_(false), num_sweeping_tasks_(0) {} @@ -535,7 +533,10 @@ class MarkCompactCollector final : public MarkCompactCollectorBase { Page* GetSweptPageSafe(PagedSpace* space); private: + class SweeperTask; + static const int kAllocationSpaces = LAST_PAGED_SPACE + 1; + static const int kMaxSweeperTasks = kAllocationSpaces; static ClearOldToNewSlotsMode GetClearOldToNewSlotsMode(Page* p); @@ -550,10 +551,10 @@ class MarkCompactCollector final : public MarkCompactCollectorBase { void PrepareToBeSweptPage(AllocationSpace space, Page* page); - Heap* heap_; + Heap* const heap_; + int num_tasks_; + uint32_t task_ids_[kMaxSweeperTasks]; base::Semaphore pending_sweeper_tasks_semaphore_; - // Counter is only used for waiting on the semaphore. - intptr_t semaphore_counter_; base::Mutex mutex_; SweptList swept_list_[kAllocationSpaces]; SweepingList sweeping_list_[kAllocationSpaces];