Skip to content

Commit 55eed60

Browse files
Matt LoringMylesBorins
authored andcommitted
deps: backport f9c4b7a from upstream V8
Original commit message: [heap] Move UnmapFreeMemoryTask 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:6671 Change-Id: I741d4b7594e8d62721dad32cbfb19551ffacd0c3 Reviewed-on: https://chromium-review.googlesource.com/599528 Commit-Queue: Michael Lippautz <mlippautz@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#47126} 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>
1 parent b7f7d67 commit 55eed60

File tree

6 files changed

+40
-21
lines changed

6 files changed

+40
-21
lines changed

deps/v8/src/heap/heap.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ Heap::Heap()
161161
heap_iterator_depth_(0),
162162
local_embedder_heap_tracer_(nullptr),
163163
fast_promotion_mode_(false),
164+
use_tasks_(true),
164165
force_oom_(false),
165166
delay_sweeper_tasks_for_testing_(false),
166167
pending_layout_change_object_(nullptr) {
@@ -5823,6 +5824,7 @@ void Heap::RegisterExternallyReferencedObject(Object** object) {
58235824
}
58245825

58255826
void Heap::TearDown() {
5827+
use_tasks_ = false;
58265828
#ifdef VERIFY_HEAP
58275829
if (FLAG_verify_heap) {
58285830
Verify();

deps/v8/src/heap/heap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,8 @@ class Heap {
10161016
// Returns whether SetUp has been called.
10171017
bool HasBeenSetUp();
10181018

1019+
bool use_tasks() const { return use_tasks_; }
1020+
10191021
// ===========================================================================
10201022
// Getters for spaces. =======================================================
10211023
// ===========================================================================
@@ -2375,6 +2377,8 @@ class Heap {
23752377

23762378
bool fast_promotion_mode_;
23772379

2380+
bool use_tasks_;
2381+
23782382
// Used for testing purposes.
23792383
bool force_oom_;
23802384
bool delay_sweeper_tasks_for_testing_;

deps/v8/src/heap/spaces.cc

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ MemoryAllocator::MemoryAllocator(Isolate* isolate)
300300
size_executable_(0),
301301
lowest_ever_allocated_(reinterpret_cast<void*>(-1)),
302302
highest_ever_allocated_(reinterpret_cast<void*>(0)),
303-
unmapper_(this) {}
303+
unmapper_(isolate->heap(), this) {}
304304

305305
bool MemoryAllocator::SetUp(size_t capacity, size_t code_range_size) {
306306
capacity_ = RoundUp(capacity, Page::kPageSize);
@@ -332,40 +332,46 @@ void MemoryAllocator::TearDown() {
332332
code_range_ = nullptr;
333333
}
334334

335-
class MemoryAllocator::Unmapper::UnmapFreeMemoryTask : public v8::Task {
335+
class MemoryAllocator::Unmapper::UnmapFreeMemoryTask : public CancelableTask {
336336
public:
337-
explicit UnmapFreeMemoryTask(Unmapper* unmapper) : unmapper_(unmapper) {}
337+
explicit UnmapFreeMemoryTask(Isolate* isolate, Unmapper* unmapper)
338+
: CancelableTask(isolate), unmapper_(unmapper) {}
338339

339340
private:
340-
// v8::Task overrides.
341-
void Run() override {
341+
void RunInternal() override {
342342
unmapper_->PerformFreeMemoryOnQueuedChunks<FreeMode::kUncommitPooled>();
343343
unmapper_->pending_unmapping_tasks_semaphore_.Signal();
344344
}
345345

346-
Unmapper* unmapper_;
346+
Unmapper* const unmapper_;
347347
DISALLOW_COPY_AND_ASSIGN(UnmapFreeMemoryTask);
348348
};
349349

350350
void MemoryAllocator::Unmapper::FreeQueuedChunks() {
351351
ReconsiderDelayedChunks();
352-
if (FLAG_concurrent_sweeping) {
352+
if (heap_->use_tasks() && FLAG_concurrent_sweeping) {
353+
if (concurrent_unmapping_tasks_active_ >= kMaxUnmapperTasks) {
354+
// kMaxUnmapperTasks are already running. Avoid creating any more.
355+
return;
356+
}
357+
UnmapFreeMemoryTask* task = new UnmapFreeMemoryTask(heap_->isolate(), this);
358+
DCHECK_LT(concurrent_unmapping_tasks_active_, kMaxUnmapperTasks);
359+
task_ids_[concurrent_unmapping_tasks_active_++] = task->id();
353360
V8::GetCurrentPlatform()->CallOnBackgroundThread(
354-
new UnmapFreeMemoryTask(this), v8::Platform::kShortRunningTask);
355-
concurrent_unmapping_tasks_active_++;
361+
task, v8::Platform::kShortRunningTask);
356362
} else {
357363
PerformFreeMemoryOnQueuedChunks<FreeMode::kUncommitPooled>();
358364
}
359365
}
360366

361-
bool MemoryAllocator::Unmapper::WaitUntilCompleted() {
362-
bool waited = false;
363-
while (concurrent_unmapping_tasks_active_ > 0) {
364-
pending_unmapping_tasks_semaphore_.Wait();
365-
concurrent_unmapping_tasks_active_--;
366-
waited = true;
367+
void MemoryAllocator::Unmapper::WaitUntilCompleted() {
368+
for (int i = 0; i < concurrent_unmapping_tasks_active_; i++) {
369+
if (heap_->isolate()->cancelable_task_manager()->TryAbort(task_ids_[i]) !=
370+
CancelableTaskManager::kTaskAborted) {
371+
pending_unmapping_tasks_semaphore_.Wait();
372+
}
373+
concurrent_unmapping_tasks_active_ = 0;
367374
}
368-
return waited;
369375
}
370376

371377
template <MemoryAllocator::Unmapper::FreeMode mode>
@@ -392,7 +398,7 @@ void MemoryAllocator::Unmapper::PerformFreeMemoryOnQueuedChunks() {
392398
}
393399

394400
void MemoryAllocator::Unmapper::TearDown() {
395-
WaitUntilCompleted();
401+
CHECK_EQ(0, concurrent_unmapping_tasks_active_);
396402
ReconsiderDelayedChunks();
397403
CHECK(delayed_regular_chunks_.empty());
398404
PerformFreeMemoryOnQueuedChunks<FreeMode::kReleasePooled>();

deps/v8/src/heap/spaces.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "src/base/bits.h"
1616
#include "src/base/hashmap.h"
1717
#include "src/base/platform/mutex.h"
18+
#include "src/cancelable-task.h"
1819
#include "src/flags.h"
1920
#include "src/globals.h"
2021
#include "src/heap/heap.h"
@@ -1149,8 +1150,9 @@ class V8_EXPORT_PRIVATE MemoryAllocator {
11491150
public:
11501151
class UnmapFreeMemoryTask;
11511152

1152-
explicit Unmapper(MemoryAllocator* allocator)
1153-
: allocator_(allocator),
1153+
Unmapper(Heap* heap, MemoryAllocator* allocator)
1154+
: heap_(heap),
1155+
allocator_(allocator),
11541156
pending_unmapping_tasks_semaphore_(0),
11551157
concurrent_unmapping_tasks_active_(0) {
11561158
chunks_[kRegular].reserve(kReservedQueueingSlots);
@@ -1184,13 +1186,14 @@ class V8_EXPORT_PRIVATE MemoryAllocator {
11841186
}
11851187

11861188
void FreeQueuedChunks();
1187-
bool WaitUntilCompleted();
1189+
void WaitUntilCompleted();
11881190
void TearDown();
11891191

11901192
bool has_delayed_chunks() { return delayed_regular_chunks_.size() > 0; }
11911193

11921194
private:
11931195
static const int kReservedQueueingSlots = 64;
1196+
static const int kMaxUnmapperTasks = 24;
11941197

11951198
enum ChunkQueueType {
11961199
kRegular, // Pages of kPageSize that do not live in a CodeRange and
@@ -1229,13 +1232,15 @@ class V8_EXPORT_PRIVATE MemoryAllocator {
12291232
template <FreeMode mode>
12301233
void PerformFreeMemoryOnQueuedChunks();
12311234

1235+
Heap* const heap_;
1236+
MemoryAllocator* const allocator_;
12321237
base::Mutex mutex_;
1233-
MemoryAllocator* allocator_;
12341238
std::vector<MemoryChunk*> chunks_[kNumberOfChunkQueues];
12351239
// Delayed chunks cannot be processed in the current unmapping cycle because
12361240
// of dependencies such as an active sweeper.
12371241
// See MemoryAllocator::CanFreeMemoryChunk.
12381242
std::list<MemoryChunk*> delayed_regular_chunks_;
1243+
CancelableTaskManager::Id task_ids_[kMaxUnmapperTasks];
12391244
base::Semaphore pending_unmapping_tasks_semaphore_;
12401245
intptr_t concurrent_unmapping_tasks_active_;
12411246

deps/v8/src/isolate.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,6 +2455,7 @@ void Isolate::Deinit() {
24552455
}
24562456

24572457
heap_.mark_compact_collector()->EnsureSweepingCompleted();
2458+
heap_.memory_allocator()->unmapper()->WaitUntilCompleted();
24582459

24592460
DumpAndResetStats();
24602461

deps/v8/test/cctest/heap/test-spaces.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ TEST(NewSpace) {
370370
}
371371

372372
new_space.TearDown();
373+
memory_allocator->unmapper()->WaitUntilCompleted();
373374
memory_allocator->TearDown();
374375
delete memory_allocator;
375376
}

0 commit comments

Comments
 (0)