Commit 51b7d3dc authored by Mythri A's avatar Mythri A Committed by Commit Bot

[turbofan] Dispose compilation job only on main thread

When disposing the compilation jobs, we have to update some fields
on JSFunction. Though the current implementation is safe given we only
dispose the compilation jobs when main thread is blocked, it is still
cleaner and safer to dispose of these jobs only on the main thread.
That also unifies the way we handle flushing the queues when we want
to block waiting for the pending tasks to finish or not

Basically this cl flushes the input queue before waiting for any
pending tasks to finish. This would avoid the special handling on the
background threads that dispose of these jobs when we are in the
flushing mode. This also means we don't need to keep track of
the mode anymore.

Change-Id: Icd3adbe998612159e796b2bc90486d38c420f9e8
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2726502Reviewed-by: 's avatarMichael Stanton <mvstanton@chromium.org>
Reviewed-by: 's avatarRoss McIlroy <rmcilroy@chromium.org>
Commit-Queue: Ross McIlroy <rmcilroy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#73190}
parent 75d7d127
......@@ -31,10 +31,6 @@ void DisposeCompilationJob(OptimizedCompilationJob* job,
if (function->IsInOptimizationQueue()) {
function->ClearOptimizationMarker();
}
// TODO(mvstanton): We can't call EnsureFeedbackVector here due to
// allocation, but we probably shouldn't call set_code either, as this
// sometimes runs on the worker thread!
// JSFunction::EnsureFeedbackVector(function);
}
delete job;
}
......@@ -81,7 +77,7 @@ class OptimizingCompileDispatcher::CompileTask : public CancelableTask {
dispatcher_->recompilation_delay_));
}
dispatcher_->CompileNext(dispatcher_->NextInput(&local_isolate, true),
dispatcher_->CompileNext(dispatcher_->NextInput(&local_isolate),
runtime_call_stats_scope.Get(), &local_isolate);
}
{
......@@ -109,23 +105,13 @@ OptimizingCompileDispatcher::~OptimizingCompileDispatcher() {
}
OptimizedCompilationJob* OptimizingCompileDispatcher::NextInput(
LocalIsolate* local_isolate, bool check_if_flushing) {
LocalIsolate* local_isolate) {
base::MutexGuard access_input_queue_(&input_queue_mutex_);
if (input_queue_length_ == 0) return nullptr;
OptimizedCompilationJob* job = input_queue_[InputQueueIndex(0)];
DCHECK_NOT_NULL(job);
input_queue_shift_ = InputQueueIndex(1);
input_queue_length_--;
if (check_if_flushing) {
if (mode_ == FLUSH) {
UnparkedScope scope(local_isolate->heap());
local_isolate->heap()->AttachPersistentHandles(
job->compilation_info()->DetachPersistentHandles());
DisposeCompilationJob(job, true);
local_isolate->heap()->DetachPersistentHandles();
return nullptr;
}
}
return job;
}
......@@ -163,49 +149,42 @@ void OptimizingCompileDispatcher::FlushOutputQueue(bool restore_function_code) {
}
}
void OptimizingCompileDispatcher::Flush(BlockingBehavior blocking_behavior) {
if (blocking_behavior == BlockingBehavior::kDontBlock) {
if (FLAG_block_concurrent_recompilation) Unblock();
base::MutexGuard access_input_queue_(&input_queue_mutex_);
while (input_queue_length_ > 0) {
OptimizedCompilationJob* job = input_queue_[InputQueueIndex(0)];
DCHECK_NOT_NULL(job);
input_queue_shift_ = InputQueueIndex(1);
input_queue_length_--;
DisposeCompilationJob(job, true);
}
FlushOutputQueue(true);
if (FLAG_trace_concurrent_recompilation) {
PrintF(" ** Flushed concurrent recompilation queues (not blocking).\n");
}
return;
void OptimizingCompileDispatcher::FlushInputQueue() {
base::MutexGuard access_input_queue_(&input_queue_mutex_);
while (input_queue_length_ > 0) {
OptimizedCompilationJob* job = input_queue_[InputQueueIndex(0)];
DCHECK_NOT_NULL(job);
input_queue_shift_ = InputQueueIndex(1);
input_queue_length_--;
DisposeCompilationJob(job, true);
}
mode_ = FLUSH;
}
void OptimizingCompileDispatcher::FlushQueues(
BlockingBehavior blocking_behavior, bool restore_function_code) {
if (FLAG_block_concurrent_recompilation) Unblock();
{
FlushInputQueue();
if (blocking_behavior == BlockingBehavior::kBlock) {
base::MutexGuard lock_guard(&ref_count_mutex_);
while (ref_count_ > 0) ref_count_zero_.Wait(&ref_count_mutex_);
mode_ = COMPILE;
}
FlushOutputQueue(true);
FlushOutputQueue(restore_function_code);
}
void OptimizingCompileDispatcher::Flush(BlockingBehavior blocking_behavior) {
FlushQueues(blocking_behavior, true);
if (FLAG_trace_concurrent_recompilation) {
PrintF(" ** Flushed concurrent recompilation queues.\n");
PrintF(" ** Flushed concurrent recompilation queues. (mode: %s)\n",
(blocking_behavior == BlockingBehavior::kBlock) ? "blocking"
: "non blocking");
}
}
void OptimizingCompileDispatcher::Stop() {
mode_ = FLUSH;
if (FLAG_block_concurrent_recompilation) Unblock();
{
base::MutexGuard lock_guard(&ref_count_mutex_);
while (ref_count_ > 0) ref_count_zero_.Wait(&ref_count_mutex_);
mode_ = COMPILE;
}
FlushQueues(BlockingBehavior::kBlock, false);
// At this point the optimizing compiler thread's event loop has stopped.
// There is no need for a mutex when reading input_queue_length_.
DCHECK_EQ(input_queue_length_, 0);
FlushOutputQueue(false);
}
void OptimizingCompileDispatcher::InstallOptimizedFunctions() {
......
......@@ -30,7 +30,6 @@ class V8_EXPORT_PRIVATE OptimizingCompileDispatcher {
input_queue_capacity_(FLAG_concurrent_recompilation_queue_length),
input_queue_length_(0),
input_queue_shift_(0),
mode_(COMPILE),
blocked_jobs_(0),
ref_count_(0),
recompilation_delay_(FLAG_concurrent_recompilation_delay) {
......@@ -58,11 +57,13 @@ class V8_EXPORT_PRIVATE OptimizingCompileDispatcher {
enum ModeFlag { COMPILE, FLUSH };
void FlushQueues(BlockingBehavior blocking_behavior,
bool restore_function_code);
void FlushInputQueue();
void FlushOutputQueue(bool restore_function_code);
void CompileNext(OptimizedCompilationJob* job, RuntimeCallStats* stats,
LocalIsolate* local_isolate);
OptimizedCompilationJob* NextInput(LocalIsolate* local_isolate,
bool check_if_flushing = false);
OptimizedCompilationJob* NextInput(LocalIsolate* local_isolate);
inline int InputQueueIndex(int i) {
int result = (i + input_queue_shift_) % input_queue_capacity_;
......@@ -86,8 +87,6 @@ class V8_EXPORT_PRIVATE OptimizingCompileDispatcher {
// different threads.
base::Mutex output_queue_mutex_;
std::atomic<ModeFlag> mode_;
int blocked_jobs_;
int ref_count_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment