Commit 2c3fae96 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm] Allow to restart background tasks during streaming compilation

In a certain scenario streaming compilation got stuck and did never
finish. This CL fixes this issue.

Scenario:
* Streaming compilation starts
* The compilation tasks execute all compiation units in the working
  queue and set the finished_ flag to true.
* New data arrives over streaming
* The compilation tasks compile so fast that the executed_units_ queue
  gets full. The compilation tasks stop executing and wait for the
  finisher task to restart them.
* The finisher task does not restart the compilation tasks because the
  finished_ flag is set.

With this CL I remove the finished flag and instead look at the size
of the working queue directly.

In addition I added a test which does not actually reproduce this
scenario but seems good to have anyways.

R=mtrofin@chromium.org

Change-Id: I44560c43e51be13c4461208368e21137b115656c
Reviewed-on: https://chromium-review.googlesource.com/824523Reviewed-by: 's avatarMircea Trofin <mtrofin@chromium.org>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50090}
parent 599324d0
......@@ -166,8 +166,13 @@ class ModuleCompiler {
bool CanAcceptWork() const { return executed_units_.CanAcceptWork(); }
bool ShouldIncreaseWorkload() const {
return executed_units_.ShouldIncreaseWorkload();
bool ShouldIncreaseWorkload() {
if (executed_units_.ShouldIncreaseWorkload()) {
// Check if it actually makes sense to increase the workload.
base::LockGuard<base::Mutex> guard(&compilation_units_mutex_);
return !compilation_units_.empty();
}
return false;
}
size_t InitializeCompilationUnits(const std::vector<WasmFunction>& functions,
......@@ -3961,7 +3966,6 @@ class AsyncCompileJob::ExecuteAndFinishCompilationUnits : public CompileStep {
DisallowHeapAllocation no_allocation;
if (!job_->compiler_->FetchAndExecuteCompilationUnit(
StartFinishCompilationUnit)) {
finished_ = true;
break;
}
}
......@@ -3986,7 +3990,7 @@ class AsyncCompileJob::ExecuteAndFinishCompilationUnits : public CompileStep {
double deadline = MonotonicallyIncreasingTimeInMs() + 1.0;
while (true) {
if (!finished_ && job_->compiler_->ShouldIncreaseWorkload()) {
if (job_->compiler_->ShouldIncreaseWorkload()) {
job_->RestartBackgroundTasks();
}
......@@ -4042,7 +4046,6 @@ class AsyncCompileJob::ExecuteAndFinishCompilationUnits : public CompileStep {
private:
std::atomic<bool> failed_{false};
std::atomic<bool> finished_{false};
};
//==========================================================================
......
......@@ -874,6 +874,42 @@ STREAM_TEST(TestModuleWithZeroFunctions) {
CHECK(tester.IsPromiseFulfilled());
}
STREAM_TEST(TestModuleWithMultipleFunctions) {
StreamTester tester;
uint8_t code[] = {
U32V_1(4), // body size
U32V_1(0), // locals count
kExprGetLocal, 0, kExprEnd // body
};
const uint8_t bytes[] = {
WASM_MODULE_HEADER, // module header
kTypeSectionCode, // section code
U32V_1(1 + SIZEOF_SIG_ENTRY_x_x), // section size
U32V_1(1), // type count
SIG_ENTRY_x_x(kLocalI32, kLocalI32), // signature entry
kFunctionSectionCode, // section code
U32V_1(1 + 3), // section size
U32V_1(3), // functions count
0, // signature index
0, // signature index
0, // signature index
kCodeSectionCode, // section code
U32V_1(1 + arraysize(code) * 3), // section size
U32V_1(3), // functions count
};
tester.OnBytesReceived(bytes, arraysize(bytes));
tester.OnBytesReceived(code, arraysize(code));
tester.OnBytesReceived(code, arraysize(code));
tester.RunCompilerTasks();
tester.OnBytesReceived(code, arraysize(code));
tester.FinishStream();
tester.RunCompilerTasks();
CHECK(tester.IsPromiseFulfilled());
}
// Test that all bytes arrive before doing any compilation. FinishStream is
// called immediately.
STREAM_TEST(TestModuleWithImportedFunction) {
......
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