Commit 4bca9dc7 authored by Wiktor Garbacz's avatar Wiktor Garbacz Committed by Commit Bot

[compiler-dispatcher] Use an integer job id.

It enables jobs without a SharedFunctionInfo.

BUG=v8:6093

Change-Id: Icc5f01512c270a55349087d418b6be82ad5c6cb4
Reviewed-on: https://chromium-review.googlesource.com/467148
Commit-Queue: Wiktor Garbacz <wiktorg@google.com>
Reviewed-by: 's avatarRoss McIlroy <rmcilroy@chromium.org>
Reviewed-by: 's avatarJochen Eisinger <jochen@chromium.org>
Reviewed-by: 's avatarMarja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#44402}
parent 96519364
...@@ -63,6 +63,8 @@ class V8_EXPORT_PRIVATE CompilerDispatcherJob { ...@@ -63,6 +63,8 @@ class V8_EXPORT_PRIVATE CompilerDispatcherJob {
Context* context() { return *context_; } Context* context() { return *context_; }
Handle<SharedFunctionInfo> shared() const { return shared_; }
// Returns true if this CompilerDispatcherJob was created for the given // Returns true if this CompilerDispatcherJob was created for the given
// function. // function.
bool IsAssociatedWith(Handle<SharedFunctionInfo> shared) const; bool IsAssociatedWith(Handle<SharedFunctionInfo> shared) const;
......
...@@ -221,6 +221,8 @@ CompilerDispatcher::CompilerDispatcher(Isolate* isolate, Platform* platform, ...@@ -221,6 +221,8 @@ CompilerDispatcher::CompilerDispatcher(Isolate* isolate, Platform* platform,
trace_compiler_dispatcher_(FLAG_trace_compiler_dispatcher), trace_compiler_dispatcher_(FLAG_trace_compiler_dispatcher),
tracer_(new CompilerDispatcherTracer(isolate_)), tracer_(new CompilerDispatcherTracer(isolate_)),
task_manager_(new CancelableTaskManager()), task_manager_(new CancelableTaskManager()),
next_job_id_(0),
shared_to_job_id_(isolate->heap()),
memory_pressure_level_(MemoryPressureLevel::kNone), memory_pressure_level_(MemoryPressureLevel::kNone),
abort_(false), abort_(false),
idle_task_scheduled_(false), idle_task_scheduled_(false),
...@@ -263,6 +265,22 @@ bool CompilerDispatcher::CanEnqueue(Handle<SharedFunctionInfo> function) { ...@@ -263,6 +265,22 @@ bool CompilerDispatcher::CanEnqueue(Handle<SharedFunctionInfo> function) {
return true; return true;
} }
CompilerDispatcher::JobId CompilerDispatcher::Enqueue(
std::unique_ptr<CompilerDispatcherJob> job) {
DCHECK(!IsFinished(job.get()));
bool added;
JobMap::const_iterator it;
std::tie(it, added) =
jobs_.insert(std::make_pair(next_job_id_++, std::move(job)));
DCHECK(added);
if (!it->second->shared().is_null()) {
shared_to_job_id_.Set(it->second->shared(), it->first);
}
ConsiderJobForBackgroundProcessing(it->second.get());
ScheduleIdleTaskIfNeeded();
return it->first;
}
bool CompilerDispatcher::Enqueue(Handle<SharedFunctionInfo> function) { bool CompilerDispatcher::Enqueue(Handle<SharedFunctionInfo> function) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"),
"V8.CompilerDispatcherEnqueue"); "V8.CompilerDispatcherEnqueue");
...@@ -277,10 +295,7 @@ bool CompilerDispatcher::Enqueue(Handle<SharedFunctionInfo> function) { ...@@ -277,10 +295,7 @@ bool CompilerDispatcher::Enqueue(Handle<SharedFunctionInfo> function) {
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
isolate_, tracer_.get(), function, max_stack_size_)); isolate_, tracer_.get(), function, max_stack_size_));
std::pair<int, int> key(Script::cast(function->script())->id(), Enqueue(std::move(job));
function->function_literal_id());
jobs_.insert(std::make_pair(key, std::move(job)));
ScheduleIdleTaskIfNeeded();
return true; return true;
} }
...@@ -321,10 +336,7 @@ bool CompilerDispatcher::Enqueue( ...@@ -321,10 +336,7 @@ bool CompilerDispatcher::Enqueue(
std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob( std::unique_ptr<CompilerDispatcherJob> job(new CompilerDispatcherJob(
isolate_, tracer_.get(), script, function, literal, parse_zone, isolate_, tracer_.get(), script, function, literal, parse_zone,
parse_handles, compile_handles, max_stack_size_)); parse_handles, compile_handles, max_stack_size_));
std::pair<int, int> key(Script::cast(function->script())->id(), Enqueue(std::move(job));
function->function_literal_id());
jobs_.insert(std::make_pair(key, std::move(job)));
ScheduleIdleTaskIfNeeded();
return true; return true;
} }
...@@ -408,6 +420,9 @@ bool CompilerDispatcher::FinishNow(Handle<SharedFunctionInfo> function) { ...@@ -408,6 +420,9 @@ bool CompilerDispatcher::FinishNow(Handle<SharedFunctionInfo> function) {
} }
job->second->ResetOnMainThread(); job->second->ResetOnMainThread();
if (!job->second->shared().is_null()) {
shared_to_job_id_.Delete(job->second->shared());
}
jobs_.erase(job); jobs_.erase(job);
if (jobs_.empty()) { if (jobs_.empty()) {
base::LockGuard<base::Mutex> lock(&mutex_); base::LockGuard<base::Mutex> lock(&mutex_);
...@@ -430,6 +445,7 @@ void CompilerDispatcher::AbortAll(BlockingBehavior blocking) { ...@@ -430,6 +445,7 @@ void CompilerDispatcher::AbortAll(BlockingBehavior blocking) {
it.second->ResetOnMainThread(); it.second->ResetOnMainThread();
} }
jobs_.clear(); jobs_.clear();
shared_to_job_id_.Clear();
{ {
base::LockGuard<base::Mutex> lock(&mutex_); base::LockGuard<base::Mutex> lock(&mutex_);
DCHECK(pending_background_jobs_.empty()); DCHECK(pending_background_jobs_.empty());
...@@ -475,6 +491,9 @@ void CompilerDispatcher::AbortInactiveJobs() { ...@@ -475,6 +491,9 @@ void CompilerDispatcher::AbortInactiveJobs() {
PrintF("\n"); PrintF("\n");
} }
job->second->ResetOnMainThread(); job->second->ResetOnMainThread();
if (!job->second->shared().is_null()) {
shared_to_job_id_.Delete(job->second->shared());
}
jobs_.erase(job); jobs_.erase(job);
} }
if (jobs_.empty()) { if (jobs_.empty()) {
...@@ -516,14 +535,13 @@ void CompilerDispatcher::MemoryPressureNotification( ...@@ -516,14 +535,13 @@ void CompilerDispatcher::MemoryPressureNotification(
CompilerDispatcher::JobMap::const_iterator CompilerDispatcher::GetJobFor( CompilerDispatcher::JobMap::const_iterator CompilerDispatcher::GetJobFor(
Handle<SharedFunctionInfo> shared) const { Handle<SharedFunctionInfo> shared) const {
if (!shared->script()->IsScript()) return jobs_.end(); JobId* job_id_ptr = shared_to_job_id_.Find(shared);
std::pair<int, int> key(Script::cast(shared->script())->id(), JobMap::const_iterator job = jobs_.end();
shared->function_literal_id()); if (job_id_ptr) {
auto range = jobs_.equal_range(key); job = jobs_.find(*job_id_ptr);
for (auto job = range.first; job != range.second; ++job) { DCHECK(job == jobs_.end() || job->second->IsAssociatedWith(shared));
if (job->second->IsAssociatedWith(shared)) return job; }
} return job;
return jobs_.end();
} }
void CompilerDispatcher::ScheduleIdleTaskFromAnyThread() { void CompilerDispatcher::ScheduleIdleTaskFromAnyThread() {
...@@ -697,6 +715,9 @@ void CompilerDispatcher::DoIdleWork(double deadline_in_seconds) { ...@@ -697,6 +715,9 @@ void CompilerDispatcher::DoIdleWork(double deadline_in_seconds) {
tracer_->DumpStatistics(); tracer_->DumpStatistics();
} }
job->second->ResetOnMainThread(); job->second->ResetOnMainThread();
if (!job->second->shared().is_null()) {
shared_to_job_id_.Delete(job->second->shared());
}
job = jobs_.erase(job); job = jobs_.erase(job);
continue; continue;
} else { } else {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_ #ifndef V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_
#define V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_ #define V8_COMPILER_DISPATCHER_COMPILER_DISPATCHER_H_
#include <cstdint>
#include <map> #include <map>
#include <memory> #include <memory>
#include <unordered_set> #include <unordered_set>
...@@ -16,6 +17,7 @@ ...@@ -16,6 +17,7 @@
#include "src/base/platform/mutex.h" #include "src/base/platform/mutex.h"
#include "src/base/platform/semaphore.h" #include "src/base/platform/semaphore.h"
#include "src/globals.h" #include "src/globals.h"
#include "src/identity-map.h"
#include "testing/gtest/include/gtest/gtest_prod.h" #include "testing/gtest/include/gtest/gtest_prod.h"
namespace v8 { namespace v8 {
...@@ -65,6 +67,8 @@ class Handle; ...@@ -65,6 +67,8 @@ class Handle;
// thread. // thread.
class V8_EXPORT_PRIVATE CompilerDispatcher { class V8_EXPORT_PRIVATE CompilerDispatcher {
public: public:
typedef uintptr_t JobId;
enum class BlockingBehavior { kBlock, kDontBlock }; enum class BlockingBehavior { kBlock, kDontBlock };
CompilerDispatcher(Isolate* isolate, Platform* platform, CompilerDispatcher(Isolate* isolate, Platform* platform,
...@@ -117,6 +121,7 @@ class V8_EXPORT_PRIVATE CompilerDispatcher { ...@@ -117,6 +121,7 @@ class V8_EXPORT_PRIVATE CompilerDispatcher {
bool is_isolate_locked); bool is_isolate_locked);
private: private:
FRIEND_TEST(CompilerDispatcherTest, EnqueueJob);
FRIEND_TEST(CompilerDispatcherTest, EnqueueAndStep); FRIEND_TEST(CompilerDispatcherTest, EnqueueAndStep);
FRIEND_TEST(CompilerDispatcherTest, EnqueueAndStepTwice); FRIEND_TEST(CompilerDispatcherTest, EnqueueAndStepTwice);
FRIEND_TEST(CompilerDispatcherTest, EnqueueParsed); FRIEND_TEST(CompilerDispatcherTest, EnqueueParsed);
...@@ -129,9 +134,8 @@ class V8_EXPORT_PRIVATE CompilerDispatcher { ...@@ -129,9 +134,8 @@ class V8_EXPORT_PRIVATE CompilerDispatcher {
FRIEND_TEST(CompilerDispatcherTest, FinishNowDuringAbortAll); FRIEND_TEST(CompilerDispatcherTest, FinishNowDuringAbortAll);
FRIEND_TEST(CompilerDispatcherTest, CompileMultipleOnBackgroundThread); FRIEND_TEST(CompilerDispatcherTest, CompileMultipleOnBackgroundThread);
typedef std::multimap<std::pair<int, int>, typedef std::map<JobId, std::unique_ptr<CompilerDispatcherJob>> JobMap;
std::unique_ptr<CompilerDispatcherJob>> typedef IdentityMap<JobId, FreeStoreAllocationPolicy> SharedToJobIdMap;
JobMap;
class AbortTask; class AbortTask;
class BackgroundTask; class BackgroundTask;
class IdleTask; class IdleTask;
...@@ -147,6 +151,7 @@ class V8_EXPORT_PRIVATE CompilerDispatcher { ...@@ -147,6 +151,7 @@ class V8_EXPORT_PRIVATE CompilerDispatcher {
void ScheduleAbortTask(); void ScheduleAbortTask();
void DoBackgroundWork(); void DoBackgroundWork();
void DoIdleWork(double deadline_in_seconds); void DoIdleWork(double deadline_in_seconds);
JobId Enqueue(std::unique_ptr<CompilerDispatcherJob> job);
Isolate* isolate_; Isolate* isolate_;
Platform* platform_; Platform* platform_;
...@@ -159,10 +164,15 @@ class V8_EXPORT_PRIVATE CompilerDispatcher { ...@@ -159,10 +164,15 @@ class V8_EXPORT_PRIVATE CompilerDispatcher {
std::unique_ptr<CancelableTaskManager> task_manager_; std::unique_ptr<CancelableTaskManager> task_manager_;
// Mapping from (script id, function literal id) to job. We use a multimap, // Id for next job to be added
// as script id is not necessarily unique. JobId next_job_id_;
// Mapping from job_id to job.
JobMap jobs_; JobMap jobs_;
// Mapping from SharedFunctionInfo to corresponding JobId;
SharedToJobIdMap shared_to_job_id_;
base::AtomicValue<v8::MemoryPressureLevel> memory_pressure_level_; base::AtomicValue<v8::MemoryPressureLevel> memory_pressure_level_;
// The following members can be accessed from any thread. Methods need to hold // The following members can be accessed from any thread. Methods need to hold
......
...@@ -112,7 +112,10 @@ class IdentityMap : public IdentityMapBase { ...@@ -112,7 +112,10 @@ class IdentityMap : public IdentityMapBase {
void Set(Object* key, V v) { *(reinterpret_cast<V*>(GetEntry(key))) = v; } void Set(Object* key, V v) { *(reinterpret_cast<V*>(GetEntry(key))) = v; }
V Delete(Handle<Object> key) { return Delete(*key); } V Delete(Handle<Object> key) { return Delete(*key); }
V Delete(Object* key) { return reinterpret_cast<V>(DeleteEntry(key)); } V Delete(Object* key) {
void* val = DeleteEntry(key);
return *reinterpret_cast<V*>(&val);
}
// Removes all elements from the map. // Removes all elements from the map.
void Clear() { IdentityMapBase::Clear(); } void Clear() { IdentityMapBase::Clear(); }
......
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