v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
collection-barrier.cc
Go to the documentation of this file.
1// Copyright 2020 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <memory>
8
11#include "src/common/globals.h"
13#include "src/handles/handles.h"
14#include "src/heap/heap-inl.h"
15#include "src/heap/heap.h"
18
19namespace v8 {
20namespace internal {
21
23 Heap* heap, std::shared_ptr<v8::TaskRunner> foreground_task_runner)
24 : heap_(heap), foreground_task_runner_(foreground_task_runner) {}
25
29
32 if (shutdown_requested_) return false;
33 bool was_already_requested = collection_requested_.exchange(true);
34
35 if (!was_already_requested) {
37 timer_.Start();
38 }
39
40 return true;
41}
42
44 public:
47
50 delete;
52 const BackgroundCollectionInterruptTask&) = delete;
53
54 private:
55 // v8::internal::CancelableTask overrides.
56 void RunInternal() override {
57 // Set the current isolate such that trusted pointer tables etc are
58 // available and the cage base is set correctly for multi-cage mode.
59 SetCurrentIsolateScope isolate_scope(heap_->isolate());
61 }
62
64};
65
72
81
90
92 bool first_thread;
93
94 {
95 // Update flag before parking this thread, this guarantees that the flag is
96 // set before the next GC.
98 if (shutdown_requested_) return false;
99
100 // Collection was cancelled by the main thread.
101 if (!collection_requested_.load()) return false;
102
103 first_thread = !block_for_collection_;
106 }
107
108 // The first thread needs to activate the stack guard and post the task.
109 if (first_thread) {
110 Isolate* isolate = heap_->isolate();
111 ExecutionAccess access(isolate);
112 isolate->stack_guard()->RequestGC();
113
115 std::make_unique<BackgroundCollectionInterruptTask>(heap_));
116 }
117
118 bool collection_performed = false;
119 local_heap->ExecuteWhileParked([this, &collection_performed]() {
120 base::MutexGuard guard(&mutex_);
121
122 while (block_for_collection_) {
124 collection_performed = false;
125 return;
126 }
128 }
129
130 // Collection may have been cancelled while blocking for it.
131 collection_performed = collection_performed_;
132 });
133
134 return collection_performed;
135}
136
138 if (collection_requested_.load()) {
139 base::MutexGuard guard(&mutex_);
140 // The first thread that requests the GC, starts the timer first and *then*
141 // parks itself. Since we are in a safepoint here, the timer is always
142 // initialized here already.
146 "V8.GC.TimeToCollectionOnBackground",
147 TRACE_EVENT_SCOPE_THREAD, "duration",
148 delta.InMillisecondsF());
149 heap_->isolate()
150 ->counters()
151 ->gc_time_to_collection_on_background()
152 ->AddTimedSample(delta);
153 timer_.Stop();
154 }
155}
156
157} // namespace internal
158} // namespace v8
void PostTask(std::unique_ptr< Task > task, const SourceLocation &location=SourceLocation::Current())
Definition v8-platform.h:82
TimeDelta Elapsed() const
double InMillisecondsF() const
Definition time.cc:226
BackgroundCollectionInterruptTask & operator=(const BackgroundCollectionInterruptTask &)=delete
BackgroundCollectionInterruptTask(const BackgroundCollectionInterruptTask &)=delete
std::shared_ptr< v8::TaskRunner > foreground_task_runner_
CollectionBarrier(Heap *heap, std::shared_ptr< v8::TaskRunner > foreground_task_runner)
base::ConditionVariable cv_wakeup_
bool AwaitCollectionBackground(LocalHeap *local_heap)
std::atomic< bool > collection_requested_
void CheckCollectionRequested()
Definition heap.cc:2113
Isolate * isolate() const
Definition heap-inl.h:61
Counters * counters()
Definition isolate.h:1180
V8_INLINE void ExecuteWhileParked(Callback callback)
#define CHECK(condition)
Definition logging.h:124
#define DCHECK(condition)
Definition logging.h:482
#define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val)
#define TRACE_EVENT_SCOPE_THREAD
#define TRACE_DISABLED_BY_DEFAULT(name)
Heap * heap_