2019-02-20 12:04:27 +00:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
|
|
|
let {session, contextGroup, Protocol} = InspectorTest.start(
|
|
|
|
'Test that console profiles contain wasm function names.');
|
|
|
|
|
|
|
|
utils.load('test/mjsunit/wasm/wasm-module-builder.js');
|
|
|
|
|
2020-02-21 14:54:42 +00:00
|
|
|
// Add fibonacci function, calling back and forth between JS and Wasm to also
|
|
|
|
// check for the occurrence of the wrappers.
|
2019-02-20 12:04:27 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
2020-02-21 14:54:42 +00:00
|
|
|
const imp_index = builder.addImport('q', 'f', kSig_i_i);
|
2019-02-20 12:04:27 +00:00
|
|
|
builder.addFunction('fib', kSig_i_i)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 0,
|
2019-02-20 12:04:27 +00:00
|
|
|
kExprI32Const, 2,
|
|
|
|
kExprI32LeS, // i < 2 ?
|
|
|
|
kExprBrIf, 0, // --> return i
|
|
|
|
kExprI32Const, 1, kExprI32Sub, // i - 1
|
2020-02-21 14:54:42 +00:00
|
|
|
kExprCallFunction, imp_index, // imp(i - 1)
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprI32Const, 2, kExprI32Sub, // i - 2
|
2020-02-21 14:54:42 +00:00
|
|
|
kExprCallFunction, imp_index, // imp(i - 2)
|
2019-02-20 12:04:27 +00:00
|
|
|
kExprI32Add
|
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
let module_bytes = builder.toArray();
|
|
|
|
|
|
|
|
function compile(bytes) {
|
|
|
|
let buffer = new ArrayBuffer(bytes.length);
|
|
|
|
let view = new Uint8Array(buffer);
|
|
|
|
for (var i = 0; i < bytes.length; i++) {
|
|
|
|
view[i] = bytes[i] | 0;
|
|
|
|
}
|
|
|
|
let module = new WebAssembly.Module(buffer);
|
2020-02-21 14:54:42 +00:00
|
|
|
let fib = undefined;
|
|
|
|
function imp(i) { return fib(i); }
|
|
|
|
let instance = new WebAssembly.Instance(module, {q: {f: imp}});
|
|
|
|
fib = instance.exports.fib;
|
2019-02-20 12:04:27 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:54:42 +00:00
|
|
|
function checkError(message) {
|
|
|
|
if (!message.error) return;
|
|
|
|
InspectorTest.log('Error: ');
|
|
|
|
InspectorTest.logMessage(message);
|
|
|
|
InspectorTest.completeTest();
|
2019-02-20 12:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(async function test() {
|
|
|
|
Protocol.Profiler.enable();
|
|
|
|
checkError(await Protocol.Profiler.start());
|
2020-02-21 14:54:42 +00:00
|
|
|
let found_good_profile = false;
|
2019-02-20 12:04:27 +00:00
|
|
|
let finished_profiles = 0;
|
|
|
|
Protocol.Profiler.onConsoleProfileFinished(e => {
|
|
|
|
++finished_profiles;
|
2020-02-21 14:54:42 +00:00
|
|
|
let function_names =
|
|
|
|
e.params.profile.nodes.map(n => n.callFrame.functionName);
|
|
|
|
// InspectorTest.log(function_names.join(', '));
|
|
|
|
// Check for at least one full cycle of
|
|
|
|
// fib -> wasm-to-js -> imp -> js-to-wasm -> fib.
|
|
|
|
const expected = ['fib', 'wasm-to-js:i:i', 'imp', 'js-to-wasm:i:i', 'fib'];
|
|
|
|
for (let i = 0; i <= function_names.length - expected.length; ++i) {
|
|
|
|
if (expected.every((val, idx) => val == function_names[i + idx])) {
|
|
|
|
found_good_profile = true;
|
|
|
|
}
|
|
|
|
}
|
2019-02-20 12:04:27 +00:00
|
|
|
});
|
|
|
|
InspectorTest.log('Compiling wasm.');
|
|
|
|
checkError(await Protocol.Runtime.evaluate({
|
|
|
|
expression: 'const instance = (' + compile + ')(' +
|
|
|
|
JSON.stringify(module_bytes) + ');'
|
|
|
|
}));
|
|
|
|
InspectorTest.log(
|
|
|
|
'Running fib with increasing input until it shows up in the profile.');
|
2020-02-21 14:54:42 +00:00
|
|
|
for (let i = 1; !found_good_profile; ++i) {
|
2019-02-20 12:04:27 +00:00
|
|
|
checkError(await Protocol.Runtime.evaluate(
|
|
|
|
{expression: 'console.profile(\'profile\');'}));
|
|
|
|
checkError(await Protocol.Runtime.evaluate(
|
|
|
|
{expression: 'instance.exports.fib(' + i + ');'}));
|
|
|
|
checkError(await Protocol.Runtime.evaluate(
|
|
|
|
{expression: 'console.profileEnd(\'profile\');'}));
|
|
|
|
if (finished_profiles != i) {
|
|
|
|
InspectorTest.log(
|
|
|
|
'Missing consoleProfileFinished message (expected ' + i + ', got ' +
|
|
|
|
finished_profiles + ')');
|
|
|
|
}
|
|
|
|
}
|
2020-02-21 14:54:42 +00:00
|
|
|
InspectorTest.log('Found expected functions in profile.');
|
2019-02-20 12:04:27 +00:00
|
|
|
InspectorTest.completeTest();
|
|
|
|
})().catch(e => InspectorTest.log('caught: ' + e));
|