Skip to content

Commit

Permalink
deps: backport 6e9e2e5 from upstream V8
Browse files Browse the repository at this point in the history
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 <mlippautz@chromium.org>
  Reviewed-by: Ulan Degenbaev <ulan@chromium.org>
  Cr-Commit-Position: refs/heads/master@{#47036}

Backport-PR-URL: #15393
PR-URL: #14001
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
  • Loading branch information
Matt Loring authored and MylesBorins committed Sep 28, 2017
1 parent c183134 commit 1836bcc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
40 changes: 25 additions & 15 deletions deps/v8/src/heap/mark-compact.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -569,21 +570,22 @@ 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<intptr_t>* 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) {}

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;
Expand All @@ -598,9 +600,9 @@ class MarkCompactCollector::Sweeper::SweeperTask : public v8::Task {
pending_sweeper_tasks_->Signal();
}

Sweeper* sweeper_;
base::Semaphore* pending_sweeper_tasks_;
base::AtomicNumber<intptr_t>* num_sweeping_tasks_;
Sweeper* const sweeper_;
base::Semaphore* const pending_sweeper_tasks_;
base::AtomicNumber<intptr_t>* const num_sweeping_tasks_;
AllocationSpace space_to_start_;

DISALLOW_COPY_AND_ASSIGN(SweeperTask);
Expand All @@ -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);
});
}
}
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 7 additions & 6 deletions deps/v8/src/heap/mark-compact.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {}

Expand All @@ -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);

Expand All @@ -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];
Expand Down

0 comments on commit 1836bcc

Please sign in to comment.