Skip to content

Commit d85283b

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 <[email protected]> Reviewed-by: Ulan Degenbaev <[email protected]> Cr-Commit-Position: refs/heads/master@{#47126} Backport-PR-URL: #15393 PR-URL: #14001 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 19a5021 commit d85283b

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
@@ -163,6 +163,7 @@ Heap::Heap()
163163
heap_iterator_depth_(0),
164164
local_embedder_heap_tracer_(nullptr),
165165
fast_promotion_mode_(false),
166+
use_tasks_(true),
166167
force_oom_(false),
167168
delay_sweeper_tasks_for_testing_(false),
168169
pending_layout_change_object_(nullptr) {
@@ -5850,6 +5851,7 @@ void Heap::RegisterExternallyReferencedObject(Object** object) {
58505851
}
58515852

58525853
void Heap::TearDown() {
5854+
use_tasks_ = false;
58535855
#ifdef VERIFY_HEAP
58545856
if (FLAG_verify_heap) {
58555857
Verify();

deps/v8/src/heap/heap.h

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

960+
bool use_tasks() const { return use_tasks_; }
961+
960962
// ===========================================================================
961963
// Getters for spaces. =======================================================
962964
// ===========================================================================
@@ -2371,6 +2373,8 @@ class Heap {
23712373

23722374
bool fast_promotion_mode_;
23732375

2376+
bool use_tasks_;
2377+
23742378
// Used for testing purposes.
23752379
bool force_oom_;
23762380
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
@@ -302,7 +302,7 @@ MemoryAllocator::MemoryAllocator(Isolate* isolate)
302302
size_executable_(0),
303303
lowest_ever_allocated_(reinterpret_cast<void*>(-1)),
304304
highest_ever_allocated_(reinterpret_cast<void*>(0)),
305-
unmapper_(this) {}
305+
unmapper_(isolate->heap(), this) {}
306306

307307
bool MemoryAllocator::SetUp(size_t capacity, size_t code_range_size) {
308308
capacity_ = RoundUp(capacity, Page::kPageSize);
@@ -334,40 +334,46 @@ void MemoryAllocator::TearDown() {
334334
code_range_ = nullptr;
335335
}
336336

337-
class MemoryAllocator::Unmapper::UnmapFreeMemoryTask : public v8::Task {
337+
class MemoryAllocator::Unmapper::UnmapFreeMemoryTask : public CancelableTask {
338338
public:
339-
explicit UnmapFreeMemoryTask(Unmapper* unmapper) : unmapper_(unmapper) {}
339+
explicit UnmapFreeMemoryTask(Isolate* isolate, Unmapper* unmapper)
340+
: CancelableTask(isolate), unmapper_(unmapper) {}
340341

341342
private:
342-
// v8::Task overrides.
343-
void Run() override {
343+
void RunInternal() override {
344344
unmapper_->PerformFreeMemoryOnQueuedChunks<FreeMode::kUncommitPooled>();
345345
unmapper_->pending_unmapping_tasks_semaphore_.Signal();
346346
}
347347

348-
Unmapper* unmapper_;
348+
Unmapper* const unmapper_;
349349
DISALLOW_COPY_AND_ASSIGN(UnmapFreeMemoryTask);
350350
};
351351

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

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

373379
template <MemoryAllocator::Unmapper::FreeMode mode>
@@ -394,7 +400,7 @@ void MemoryAllocator::Unmapper::PerformFreeMemoryOnQueuedChunks() {
394400
}
395401

396402
void MemoryAllocator::Unmapper::TearDown() {
397-
WaitUntilCompleted();
403+
CHECK_EQ(0, concurrent_unmapping_tasks_active_);
398404
ReconsiderDelayedChunks();
399405
CHECK(delayed_regular_chunks_.empty());
400406
PerformFreeMemoryOnQueuedChunks<FreeMode::kReleasePooled>();

deps/v8/src/heap/spaces.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "src/base/hashmap.h"
1717
#include "src/base/iterator.h"
1818
#include "src/base/platform/mutex.h"
19+
#include "src/cancelable-task.h"
1920
#include "src/flags.h"
2021
#include "src/globals.h"
2122
#include "src/heap/heap.h"
@@ -1184,8 +1185,9 @@ class V8_EXPORT_PRIVATE MemoryAllocator {
11841185
public:
11851186
class UnmapFreeMemoryTask;
11861187

1187-
explicit Unmapper(MemoryAllocator* allocator)
1188-
: allocator_(allocator),
1188+
Unmapper(Heap* heap, MemoryAllocator* allocator)
1189+
: heap_(heap),
1190+
allocator_(allocator),
11891191
pending_unmapping_tasks_semaphore_(0),
11901192
concurrent_unmapping_tasks_active_(0) {
11911193
chunks_[kRegular].reserve(kReservedQueueingSlots);
@@ -1219,13 +1221,14 @@ class V8_EXPORT_PRIVATE MemoryAllocator {
12191221
}
12201222

12211223
void FreeQueuedChunks();
1222-
bool WaitUntilCompleted();
1224+
void WaitUntilCompleted();
12231225
void TearDown();
12241226

12251227
bool has_delayed_chunks() { return delayed_regular_chunks_.size() > 0; }
12261228

12271229
private:
12281230
static const int kReservedQueueingSlots = 64;
1231+
static const int kMaxUnmapperTasks = 24;
12291232

12301233
enum ChunkQueueType {
12311234
kRegular, // Pages of kPageSize that do not live in a CodeRange and
@@ -1264,13 +1267,15 @@ class V8_EXPORT_PRIVATE MemoryAllocator {
12641267
template <FreeMode mode>
12651268
void PerformFreeMemoryOnQueuedChunks();
12661269

1270+
Heap* const heap_;
1271+
MemoryAllocator* const allocator_;
12671272
base::Mutex mutex_;
1268-
MemoryAllocator* allocator_;
12691273
std::vector<MemoryChunk*> chunks_[kNumberOfChunkQueues];
12701274
// Delayed chunks cannot be processed in the current unmapping cycle because
12711275
// of dependencies such as an active sweeper.
12721276
// See MemoryAllocator::CanFreeMemoryChunk.
12731277
std::list<MemoryChunk*> delayed_regular_chunks_;
1278+
CancelableTaskManager::Id task_ids_[kMaxUnmapperTasks];
12741279
base::Semaphore pending_unmapping_tasks_semaphore_;
12751280
intptr_t concurrent_unmapping_tasks_active_;
12761281

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
wasm_compilation_manager_->TearDown();
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)