forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from mooori/inst-helper
feat(bench): add `InstructionTracer.js` helper
- Loading branch information
Showing
6 changed files
with
225 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const fs = require("fs"); | ||
|
||
const dataDump = require("./data-dump"); | ||
|
||
/** | ||
* Handles the generation of traces of instructions executed at runtime. | ||
*/ | ||
class InstructionTracer { | ||
constructor() { | ||
// Contains executed instructions in order of execution. | ||
this.rawTrace = []; | ||
} | ||
|
||
setup() { | ||
// `zkevm-proverjs` requires a `setup` function on helper objects. | ||
} | ||
|
||
/** | ||
* @param {Object} ctx - The context passed by `zkevm-proverjs` to helper calls | ||
* @param {Object} tag - Helper call specific information passed by `zkevm-proverjs` | ||
* @param {Object} tag.params[0] - expects the identifier as first parameter of | ||
* `$${instrumentInst(...)}` | ||
* | ||
* @example | ||
* // To trace the execution of an `AluRRR` instruction, call | ||
* // $${instrumentInst(AluRRR)} | ||
*/ | ||
eval_traceInstruction(ctx, tag) { | ||
const instruction = tag.params[0].varName; | ||
this.rawTrace.push(instruction); | ||
} | ||
|
||
/** | ||
* Writes the raw trace to `path`. | ||
* | ||
* @param {string} path | ||
*/ | ||
writeRawTrace(path) { | ||
if (typeof path !== "string") { | ||
// Writing to a descriptor will append instead of replace content, | ||
// which might result in invalid traces, see | ||
// https://nodejs.org/api/fs.html#using-fswritefile-with-file-descriptors | ||
throw new Error("provide a file name (not descriptor) to write to"); | ||
} | ||
// Writing in chunks of 1000 instructions to limit memory usage, | ||
// as raw traces might grow big. | ||
dataDump.writeToFileInChunks(this.rawTrace, path, 1000); | ||
} | ||
} | ||
|
||
module.exports = InstructionTracer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const fs = require('fs'); | ||
|
||
/** | ||
* Writes `lines` to a file in chunks to limit memory usage. | ||
* | ||
* @param {string[]} lines - each string in the array is written on its own line | ||
* @param {string} filePath | ||
* @param {number} chunkSize | ||
*/ | ||
function writeToFileInChunks(lines, filePath, chunkSize) { | ||
// If the file already exists, clear it by writing empty string. | ||
if (fs.existsSync(filePath)) { | ||
fs.writeFileSync(filePath, "", "utf-8"); | ||
} | ||
|
||
// Append chunks to the file (will be created if it doesn't exist). | ||
for (let i = 0; i < lines.length; i += chunkSize) { | ||
let linesToWrite = lines.slice(i, i + chunkSize).join("\n") + "\n"; | ||
fs.appendFileSync(filePath, linesToWrite, "utf-8"); | ||
} | ||
} | ||
|
||
module.exports = { | ||
writeToFileInChunks | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters