2018-10-19 08:28:13 +00:00
|
|
|
// Copyright 2018 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
// Flags: --experimental-wasm-threads
|
|
|
|
|
|
|
|
// This test might time out if the search space for a sequential
|
|
|
|
// interleaving becomes to large. However, it should never fail.
|
|
|
|
// Note that results of this test are flaky by design. While the test is
|
|
|
|
// deterministic with a fixed seed, bugs may introduce non-determinism.
|
|
|
|
|
2019-07-11 09:52:29 +00:00
|
|
|
load('test/mjsunit/wasm/wasm-module-builder.js');
|
2018-10-19 08:28:13 +00:00
|
|
|
|
|
|
|
const kDebug = false;
|
|
|
|
|
|
|
|
const kSequenceLength = 256;
|
|
|
|
const kNumberOfWorker = 4;
|
|
|
|
const kNumberOfSteps = 10000000;
|
|
|
|
|
|
|
|
const kFirstOpcodeWithInput = 3;
|
|
|
|
const kFirstOpcodeWithoutOutput = 3;
|
|
|
|
const kLastOpcodeWithoutOutput = 5;
|
|
|
|
|
|
|
|
const opCodes = [
|
2019-07-11 09:52:29 +00:00
|
|
|
kExprI32AtomicLoad, kExprI32AtomicLoad8U, kExprI32AtomicLoad16U,
|
|
|
|
kExprI32AtomicStore, kExprI32AtomicStore8U, kExprI32AtomicStore16U,
|
|
|
|
kExprI32AtomicAdd, kExprI32AtomicAdd8U, kExprI32AtomicAdd16U,
|
|
|
|
kExprI32AtomicSub, kExprI32AtomicSub8U, kExprI32AtomicSub16U,
|
|
|
|
kExprI32AtomicAnd, kExprI32AtomicAnd8U, kExprI32AtomicAnd16U,
|
|
|
|
kExprI32AtomicOr, kExprI32AtomicOr8U, kExprI32AtomicOr16U,
|
|
|
|
kExprI32AtomicXor, kExprI32AtomicXor8U, kExprI32AtomicXor16U,
|
|
|
|
kExprI32AtomicExchange, kExprI32AtomicExchange8U, kExprI32AtomicExchange16U
|
2018-10-19 08:28:13 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const opCodeNames = [
|
2019-07-11 09:52:29 +00:00
|
|
|
'kExprI32AtomicLoad', 'kExprI32AtomicLoad8U',
|
|
|
|
'kExprI32AtomicLoad16U', 'kExprI32AtomicStore',
|
|
|
|
'kExprI32AtomicStore8U', 'kExprI32AtomicStore16U',
|
|
|
|
'kExprI32AtomicAdd', 'kExprI32AtomicAdd8U',
|
|
|
|
'kExprI32AtomicAdd16U', 'kExprI32AtomicSub',
|
|
|
|
'kExprI32AtomicSub8U', 'kExprI32AtomicSub16U',
|
|
|
|
'kExprI32AtomicAnd', 'kExprI32AtomicAnd8U',
|
|
|
|
'kExprI32AtomicAnd16U', 'kExprI32AtomicOr',
|
|
|
|
'kExprI32AtomicOr8U', 'kExprI32AtomicOr16U',
|
|
|
|
'kExprI32AtomicXor', 'kExprI32AtomicXor8U',
|
|
|
|
'kExprI32AtomicXor16U', 'kExprI32AtomicExchange',
|
|
|
|
'kExprI32AtomicExchange8U', 'kExprI32AtomicExchange16U'
|
2018-10-19 08:28:13 +00:00
|
|
|
];
|
|
|
|
|
2019-07-11 09:52:29 +00:00
|
|
|
let kMaxMemPages = 10;
|
|
|
|
let gSharedMemory =
|
|
|
|
new WebAssembly.Memory({initial: 1, maximum: kMaxMemPages, shared: true});
|
|
|
|
let gSharedMemoryView = new Int32Array(gSharedMemory.buffer);
|
2018-10-19 08:28:13 +00:00
|
|
|
|
2019-07-11 09:52:29 +00:00
|
|
|
let gPrivateMemory =
|
|
|
|
new WebAssembly.Memory({initial: 1, maximum: kMaxMemPages, shared: true});
|
|
|
|
let gPrivateMemoryView = new Int32Array(gPrivateMemory.buffer);
|
2018-10-19 08:28:13 +00:00
|
|
|
|
2019-07-11 09:52:29 +00:00
|
|
|
class Operation {
|
|
|
|
constructor(opcode, input, offset) {
|
|
|
|
this.opcode = opcode != undefined ? opcode : Operation.nextOpcode();
|
|
|
|
this.size = Operation.opcodeToSize(this.opcode);
|
|
|
|
this.input = input != undefined ? input : Operation.inputForSize(this.size);
|
|
|
|
this.offset =
|
|
|
|
offset != undefined ? offset : Operation.offsetForSize(this.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static nextOpcode() {
|
|
|
|
let random = Math.random();
|
|
|
|
return Math.floor(random * opCodes.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static opcodeToSize(opcode) {
|
|
|
|
// Instructions are ordered in 32, 8, 16 bits size
|
|
|
|
return [32, 8, 16][opcode % 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
static opcodeToAlignment(opcode) {
|
|
|
|
// Instructions are ordered in 32, 8, 16 bits size
|
|
|
|
return [2, 0, 1][opcode % 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inputForSize(size) {
|
|
|
|
let random = Math.random();
|
|
|
|
// Avoid 32 bit overflow for integer here :(
|
|
|
|
return Math.floor(random * (1 << (size - 1)) * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static offsetForSize(size) {
|
|
|
|
// Pick an offset in bytes between 0 and 7.
|
|
|
|
let offset = Math.floor(Math.random() * 8);
|
|
|
|
// Make sure the offset matches the required alignment by masking out the
|
|
|
|
// lower bits.
|
|
|
|
let size_in_bytes = size / 8;
|
|
|
|
let mask = ~(size_in_bytes - 1);
|
|
|
|
return offset & mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
get wasmOpcode() {
|
|
|
|
// [opcode, alignment, offset]
|
|
|
|
return [
|
|
|
|
opCodes[this.opcode], Operation.opcodeToAlignment(this.opcode),
|
|
|
|
this.offset
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
get hasInput() {
|
|
|
|
return this.opcode >= kFirstOpcodeWithInput;
|
|
|
|
}
|
|
|
|
|
|
|
|
get hasOutput() {
|
|
|
|
return this.opcode < kFirstOpcodeWithoutOutput ||
|
|
|
|
this.opcode > kLastOpcodeWithoutOutput;
|
|
|
|
}
|
|
|
|
|
|
|
|
truncateResultBits(low, high) {
|
|
|
|
// Shift the lower part. For offsets greater four it drops out of the
|
|
|
|
// visible window.
|
|
|
|
let shiftedL = this.offset >= 4 ? 0 : low >>> (this.offset * 8);
|
|
|
|
// The higher part is zero for offset 0, left shifted for [1..3] and right
|
|
|
|
// shifted for [4..7].
|
|
|
|
let shiftedH = this.offset == 0 ?
|
|
|
|
0 :
|
|
|
|
this.offset >= 4 ? high >>> (this.offset - 4) * 8 :
|
|
|
|
high << ((4 - this.offset) * 8);
|
|
|
|
let value = shiftedL | shiftedH;
|
|
|
|
|
|
|
|
switch (this.size) {
|
|
|
|
case 8:
|
|
|
|
return value & 0xFF;
|
|
|
|
case 16:
|
|
|
|
return value & 0xFFFF;
|
|
|
|
case 32:
|
|
|
|
return value;
|
|
|
|
default:
|
|
|
|
throw 'Unexpected size: ' + this.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static get builder() {
|
|
|
|
if (!Operation.__builder) {
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addImportedMemory('m', 'imported_mem', 0, kMaxMemPages, 'shared');
|
|
|
|
Operation.__builder = builder;
|
|
|
|
}
|
|
|
|
return Operation.__builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
static get exports() {
|
|
|
|
if (!Operation.__instance) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return Operation.__instance.exports;
|
|
|
|
}
|
|
|
|
|
|
|
|
static set instance(instance) {
|
|
|
|
Operation.__instance = instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
compute(state) {
|
|
|
|
let evalFun = Operation.exports[this.key];
|
|
|
|
if (!evalFun) {
|
|
|
|
let builder = Operation.builder;
|
|
|
|
let body = [
|
|
|
|
// Load address of low 32 bits.
|
|
|
|
kExprI32Const, 0,
|
|
|
|
// Load expected value.
|
|
|
|
kExprGetLocal, 0, kExprI32StoreMem, 2, 0,
|
|
|
|
// Load address of high 32 bits.
|
|
|
|
kExprI32Const, 4,
|
|
|
|
// Load expected value.
|
|
|
|
kExprGetLocal, 1, kExprI32StoreMem, 2, 0,
|
|
|
|
// Load address of where our window starts.
|
|
|
|
kExprI32Const, 0,
|
|
|
|
// Load input if there is one.
|
|
|
|
...(this.hasInput ? [kExprGetLocal, 2] : []),
|
|
|
|
// Perform operation.
|
|
|
|
kAtomicPrefix, ...this.wasmOpcode,
|
|
|
|
// Drop output if it had any.
|
|
|
|
...(this.hasOutput ? [kExprDrop] : []),
|
|
|
|
// Load resulting value.
|
|
|
|
kExprI32Const, 0, kExprI32LoadMem, 2, 0,
|
|
|
|
// Return.
|
|
|
|
kExprReturn
|
|
|
|
]
|
|
|
|
builder.addFunction(this.key, kSig_i_iii)
|
|
|
|
.addBody(body)
|
|
|
|
.exportAs(this.key);
|
|
|
|
// Instantiate module, get function exports.
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
Operation.instance =
|
|
|
|
new WebAssembly.Instance(module, {m: {imported_mem: gPrivateMemory}});
|
|
|
|
evalFun = Operation.exports[this.key];
|
|
|
|
}
|
|
|
|
let result = evalFun(state.low, state.high, this.input);
|
|
|
|
let ta = gPrivateMemoryView;
|
|
|
|
if (kDebug) {
|
|
|
|
print(
|
|
|
|
state.high + ':' + state.low + ' ' + this.toString() + ' -> ' +
|
|
|
|
ta[1] + ':' + ta[0]);
|
|
|
|
}
|
|
|
|
if (result != ta[0]) throw '!';
|
|
|
|
return {low: ta[0], high: ta[1]};
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return opCodeNames[this.opcode] + '[+' + this.offset + '] ' + this.input;
|
|
|
|
}
|
|
|
|
|
|
|
|
get key() {
|
|
|
|
return this.opcode + '-' + this.offset;
|
|
|
|
}
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class State {
|
2019-07-11 09:52:29 +00:00
|
|
|
constructor(low, high, indices, count) {
|
|
|
|
this.low = low;
|
|
|
|
this.high = high;
|
|
|
|
this.indices = indices;
|
|
|
|
this.count = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
isFinal() {
|
|
|
|
return (this.count == kNumberOfWorker * kSequenceLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return this.high + ':' + this.low + ' @ ' + this.indices;
|
|
|
|
}
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function makeSequenceOfOperations(size) {
|
2019-07-11 09:52:29 +00:00
|
|
|
let result = new Array(size);
|
|
|
|
for (let i = 0; i < size; i++) {
|
|
|
|
result[i] = new Operation();
|
|
|
|
}
|
|
|
|
return result;
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toSLeb128(val) {
|
2019-07-11 09:52:29 +00:00
|
|
|
let result = [];
|
|
|
|
while (true) {
|
|
|
|
let v = val & 0x7f;
|
|
|
|
val = val >> 7;
|
|
|
|
let msbIsSet = (v & 0x40) || false;
|
|
|
|
if (((val == 0) && !msbIsSet) || ((val == -1) && msbIsSet)) {
|
|
|
|
result.push(v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result.push(v | 0x80);
|
|
|
|
}
|
|
|
|
return result;
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function generateFunctionBodyForSequence(sequence) {
|
2019-07-11 09:52:29 +00:00
|
|
|
// We expect the int32* to perform ops on as arg 0 and
|
|
|
|
// the int32* for our value log as arg1. Argument 2 gives
|
|
|
|
// an int32* we use to count down spinning workers.
|
|
|
|
let body = [];
|
|
|
|
// Initially, we spin until all workers start running.
|
|
|
|
if (!kDebug) {
|
|
|
|
body.push(
|
|
|
|
// Decrement the wait count.
|
|
|
|
kExprGetLocal, 2, kExprI32Const, 1, kAtomicPrefix, kExprI32AtomicSub, 2,
|
|
|
|
0,
|
|
|
|
// Spin until zero.
|
|
|
|
kExprLoop, kWasmStmt, kExprGetLocal, 2, kAtomicPrefix,
|
|
|
|
kExprI32AtomicLoad, 2, 0, kExprI32Const, 0, kExprI32GtU, kExprBrIf, 0,
|
|
|
|
kExprEnd);
|
|
|
|
}
|
|
|
|
for (let operation of sequence) {
|
2018-10-19 08:28:13 +00:00
|
|
|
body.push(
|
2019-07-11 09:52:29 +00:00
|
|
|
// Pre-load address of results sequence pointer for later.
|
2018-10-19 08:28:13 +00:00
|
|
|
kExprGetLocal, 1,
|
2019-07-11 09:52:29 +00:00
|
|
|
// Load address where atomic pointers are stored.
|
|
|
|
kExprGetLocal, 0,
|
|
|
|
// Load the second argument if it had any.
|
|
|
|
...(operation.hasInput ?
|
|
|
|
[kExprI32Const, ...toSLeb128(operation.input)] :
|
|
|
|
[]),
|
|
|
|
// Perform operation
|
|
|
|
kAtomicPrefix, ...operation.wasmOpcode,
|
|
|
|
// Generate fake output in needed.
|
|
|
|
...(operation.hasOutput ? [] : [kExprI32Const, 0]),
|
|
|
|
// Store read intermediate to sequence.
|
|
|
|
kExprI32StoreMem, 2, 0,
|
|
|
|
// Increment result sequence pointer.
|
|
|
|
kExprGetLocal, 1, kExprI32Const, 4, kExprI32Add, kExprSetLocal, 1);
|
|
|
|
}
|
|
|
|
// Return end of sequence index.
|
|
|
|
body.push(kExprGetLocal, 1, kExprReturn);
|
|
|
|
return body;
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSequence(start, end) {
|
2019-07-11 09:52:29 +00:00
|
|
|
return new Int32Array(
|
|
|
|
gSharedMemory.buffer, start,
|
|
|
|
(end - start) / Int32Array.BYTES_PER_ELEMENT);
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function spawnWorkers() {
|
2019-07-11 09:52:29 +00:00
|
|
|
let workers = [];
|
|
|
|
for (let i = 0; i < kNumberOfWorker; i++) {
|
|
|
|
let worker = new Worker(
|
|
|
|
`onmessage = function(msg) {
|
2018-10-19 08:28:13 +00:00
|
|
|
if (msg.module) {
|
|
|
|
let module = msg.module;
|
|
|
|
let mem = msg.mem;
|
|
|
|
this.instance = new WebAssembly.Instance(module, {m: {imported_mem: mem}});
|
|
|
|
postMessage({instantiated: true});
|
|
|
|
} else {
|
|
|
|
let address = msg.address;
|
|
|
|
let sequence = msg.sequence;
|
|
|
|
let index = msg.index;
|
|
|
|
let spin = msg.spin;
|
|
|
|
let result = instance.exports["worker" + index](address, sequence, spin);
|
|
|
|
postMessage({index: index, sequence: sequence, result: result});
|
|
|
|
}
|
2019-07-11 09:52:29 +00:00
|
|
|
}`,
|
|
|
|
{type: 'string'});
|
|
|
|
workers.push(worker);
|
|
|
|
}
|
|
|
|
return workers;
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function instantiateModuleInWorkers(workers) {
|
2019-07-11 09:52:29 +00:00
|
|
|
for (let worker of workers) {
|
|
|
|
worker.postMessage({module: module, mem: gSharedMemory});
|
|
|
|
let msg = worker.getMessage();
|
|
|
|
if (!msg.instantiated) throw 'Worker failed to instantiate';
|
|
|
|
}
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function executeSequenceInWorkers(workers) {
|
2019-07-11 09:52:29 +00:00
|
|
|
for (i = 0; i < workers.length; i++) {
|
|
|
|
let worker = workers[i];
|
|
|
|
worker.postMessage({
|
|
|
|
index: i,
|
|
|
|
address: 0,
|
|
|
|
spin: 16,
|
|
|
|
sequence: 32 + ((kSequenceLength * 4) + 32) * i
|
|
|
|
});
|
|
|
|
// In debug mode, keep execution sequential.
|
|
|
|
if (kDebug) {
|
|
|
|
let msg = worker.getMessage();
|
|
|
|
results[msg.index] = getSequence(msg.sequence, msg.result);
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function selectMatchingWorkers(state) {
|
2019-07-11 09:52:29 +00:00
|
|
|
let matching = [];
|
|
|
|
let indices = state.indices;
|
|
|
|
for (let i = 0; i < indices.length; i++) {
|
|
|
|
let index = indices[i];
|
|
|
|
if (index >= kSequenceLength) continue;
|
|
|
|
// We need to project the expected value to the number of bits this
|
|
|
|
// operation will read at runtime.
|
|
|
|
let expected =
|
|
|
|
sequences[i][index].truncateResultBits(state.low, state.high);
|
|
|
|
let hasOutput = sequences[i][index].hasOutput;
|
|
|
|
if (!hasOutput || (results[i][index] == expected)) {
|
|
|
|
matching.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matching;
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function computeNextState(state, advanceIdx) {
|
2019-07-11 09:52:29 +00:00
|
|
|
let newIndices = state.indices.slice();
|
|
|
|
let sequence = sequences[advanceIdx];
|
|
|
|
let operation = sequence[state.indices[advanceIdx]];
|
|
|
|
newIndices[advanceIdx]++;
|
|
|
|
let {low, high} = operation.compute(state);
|
|
|
|
return new State(low, high, newIndices, state.count + 1);
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function findSequentialOrdering() {
|
2019-07-11 09:52:29 +00:00
|
|
|
let startIndices = new Array(results.length);
|
|
|
|
let steps = 0;
|
|
|
|
startIndices.fill(0);
|
|
|
|
let matchingStates = [new State(0, 0, startIndices, 0)];
|
|
|
|
while (matchingStates.length > 0) {
|
|
|
|
let current = matchingStates.pop();
|
|
|
|
if (kDebug) {
|
|
|
|
print(current);
|
|
|
|
}
|
|
|
|
let matchingResults = selectMatchingWorkers(current);
|
|
|
|
if (matchingResults.length == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (let match of matchingResults) {
|
|
|
|
let newState = computeNextState(current, match);
|
|
|
|
if (newState.isFinal()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
matchingStates.push(newState);
|
|
|
|
}
|
|
|
|
if (steps++ > kNumberOfSteps) {
|
|
|
|
print('Search timed out, aborting...');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We have no options left.
|
|
|
|
return false;
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helpful for debugging failed tests.
|
|
|
|
function loadSequencesFromStrings(inputs) {
|
2019-07-11 09:52:29 +00:00
|
|
|
let reverseOpcodes = {};
|
|
|
|
for (let i = 0; i < opCodeNames.length; i++) {
|
|
|
|
reverseOpcodes[opCodeNames[i]] = i;
|
|
|
|
}
|
|
|
|
let sequences = [];
|
|
|
|
let parseRE = /([a-zA-Z0-9]*)\[\+([0-9])\] ([\-0-9]*)/;
|
|
|
|
for (let input of inputs) {
|
|
|
|
let parts = input.split(',');
|
|
|
|
let sequence = [];
|
|
|
|
for (let part of parts) {
|
|
|
|
let parsed = parseRE.exec(part);
|
|
|
|
sequence.push(
|
|
|
|
new Operation(reverseOpcodes[parsed[1]], parsed[3], parsed[2] | 0));
|
|
|
|
}
|
|
|
|
sequences.push(sequence);
|
|
|
|
}
|
|
|
|
return sequences;
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helpful for debugging failed tests.
|
|
|
|
function loadResultsFromStrings(inputs) {
|
2019-07-11 09:52:29 +00:00
|
|
|
let results = [];
|
|
|
|
for (let input of inputs) {
|
|
|
|
let parts = input.split(',');
|
|
|
|
let result = [];
|
|
|
|
for (let number of parts) {
|
|
|
|
result.push(number | 0);
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
2019-07-11 09:52:29 +00:00
|
|
|
results.push(result);
|
|
|
|
}
|
|
|
|
return results;
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let sequences = [];
|
|
|
|
let results = [];
|
|
|
|
|
|
|
|
let builder = new WasmModuleBuilder();
|
2019-07-11 09:52:29 +00:00
|
|
|
builder.addImportedMemory('m', 'imported_mem', 0, kMaxMemPages, 'shared');
|
2018-10-19 08:28:13 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < kNumberOfWorker; i++) {
|
2019-07-11 09:52:29 +00:00
|
|
|
sequences[i] = makeSequenceOfOperations(kSequenceLength);
|
|
|
|
builder.addFunction('worker' + i, kSig_i_iii)
|
|
|
|
.addBody(generateFunctionBodyForSequence(sequences[i]))
|
|
|
|
.exportAs('worker' + i);
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate module, get function exports.
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
2019-07-11 09:52:29 +00:00
|
|
|
let instance =
|
|
|
|
new WebAssembly.Instance(module, {m: {imported_mem: gSharedMemory}});
|
2018-10-19 08:28:13 +00:00
|
|
|
|
|
|
|
// Spawn off the workers and run the sequences.
|
|
|
|
let workers = spawnWorkers();
|
|
|
|
// Set spin count.
|
2019-07-11 09:52:29 +00:00
|
|
|
gSharedMemoryView[4] = kNumberOfWorker;
|
2018-10-19 08:28:13 +00:00
|
|
|
instantiateModuleInWorkers(workers);
|
|
|
|
executeSequenceInWorkers(workers);
|
|
|
|
|
|
|
|
if (!kDebug) {
|
2019-07-11 09:52:29 +00:00
|
|
|
// Collect results, d8 style.
|
|
|
|
for (let worker of workers) {
|
|
|
|
let msg = worker.getMessage();
|
|
|
|
results[msg.index] = getSequence(msg.sequence, msg.result);
|
|
|
|
}
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Terminate all workers.
|
|
|
|
for (let worker of workers) {
|
2019-07-11 09:52:29 +00:00
|
|
|
worker.terminate();
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In debug mode, print sequences and results.
|
|
|
|
if (kDebug) {
|
2019-07-11 09:52:29 +00:00
|
|
|
for (let result of results) {
|
|
|
|
print(result);
|
|
|
|
}
|
2018-10-19 08:28:13 +00:00
|
|
|
|
2019-07-11 09:52:29 +00:00
|
|
|
for (let sequence of sequences) {
|
|
|
|
print(sequence);
|
|
|
|
}
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to reconstruct a sequential ordering.
|
|
|
|
let passed = findSequentialOrdering();
|
|
|
|
|
|
|
|
if (passed) {
|
2019-07-11 09:52:29 +00:00
|
|
|
print('PASS');
|
2018-10-19 08:28:13 +00:00
|
|
|
} else {
|
2019-07-11 09:52:29 +00:00
|
|
|
for (let i = 0; i < kNumberOfWorker; i++) {
|
|
|
|
print('Worker ' + i);
|
|
|
|
print(sequences[i]);
|
|
|
|
print(results[i]);
|
|
|
|
}
|
|
|
|
print('FAIL');
|
|
|
|
quit(-1);
|
2018-10-19 08:28:13 +00:00
|
|
|
}
|