-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
executable file
·419 lines (386 loc) · 11.8 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env node
const yargs = require("yargs");
const { spawn } = require("child_process");
const fs = require("fs");
const readline = require("readline");
const tempy = require("tempy");
const path = require("path");
const prettyMS = require("pretty-ms");
const moment = require("moment");
process.on("unhandledRejection", err => {
throw err;
});
const MIN_PERIOD = 1000000;
yargs
.command(
"list",
"List available presets.",
yargs => yargs.check(validateExtraPositionalArgs({ max: 1 })),
list
)
.command(
"collect <executable> [args..]",
"Collect performance data on an executable.",
yargs =>
yargs
.positional("executable", {
description: "Executable file to profile.",
type: "string"
})
.positional("args", {
description: "Arguments to be passed to the executable.",
type: "string"
})
.option("presets", {
alias: "p",
description:
"Sensible performance metrics. Use `list` command to see available presets.",
type: "array",
default: ["all"]
})
.option("events", {
alias: "e",
description: "A list of events to count.",
type: "array",
default: []
})
.option("in", {
alias: "i",
description: "The file to pipe into the stdin of <executable>.",
type: "string"
})
.option("out", {
description: "The file to pipe the stdout of <executable> into.",
default: "alex_$executable_out_$timestamp.log"
})
.option("err", {
description: "The file to pipe the stderr of <executable> into.",
default: "alex_$executable_err_$timestamp.log"
})
.option("result", {
description: "The file to pipe the performance results into.",
default: "alex_$executable_result_$timestamp.bin"
})
.option("visualize", {
description: "Where to visualize the results.",
choices: ["window", "no", "ask"],
default: "ask"
})
.option("show-timer", {
description: "Show a timer indicating the time spent collecting.",
type: "boolean",
default: true
})
.option("period", {
description: `The period in CPU cycles. Must be at least ${MIN_PERIOD}`,
type: "number",
default: 10000000
})
.check(argv => {
if (argv.period < MIN_PERIOD) {
throw new Error(
`Invalid period: ${argv.period}. Must be at least ${MIN_PERIOD}.`
);
} else {
return true;
}
})
.option("wattsup-device", {
description:
"Use `dmesg` after plugging in the device to see what the USB " +
"serial port is detected at.",
default: "ttyUSB0"
})
.example(
"$0 collect --in my-input-file.in -p cache -- ./my-program --arg1 arg2"
),
argv => {
const executableName = argv.executable
.split("/")
.filter(Boolean)
.reverse()[0]
.toLowerCase();
const timestampString = moment().format("YYYY-MM-DD" + "T" + "HH-mm-ss");
const normalizeFileName = fileName =>
fileName
? path.resolve(
process.cwd(),
fileName
.replace("$executable", executableName)
.replace("$timestamp", timestampString)
)
: undefined;
collect({
...argv,
inFile: argv.in ? path.resolve(process.cwd(), argv.in) : undefined,
outFile: normalizeFileName(argv.out),
errFile: normalizeFileName(argv.err),
resultOption: normalizeFileName(argv.result),
presets: argv.presets.filter(Boolean),
events: argv.events.filter(Boolean),
visualizeOption: argv.visualize,
// Manually parse this out, since positional args can't handle "--xxx" args
executableArgs: process.argv.includes("--")
? process.argv.slice(process.argv.indexOf("--") + 2)
: argv.args
});
}
)
.command(
"visualize <file>",
"Visualize performance data from a file.",
yargs =>
yargs
.positional("file", {
description: "File to read result data from.",
type: "string"
})
.option("heap-size", {
description:
"The maximum size of the JS heap in MB. Increase if " +
"visualization freezes while loading large data.",
type: "number",
default: 4096
})
.check(validateExtraPositionalArgs({ max: 1 })),
argv => {
visualize(argv.file, argv.heapSize);
}
)
.command("*", false, yargs =>
yargs.check(argv => {
throw new Error(`Unknown command: ${argv._[0]}`);
})
)
.demandCommand(1, "Must specify a command.")
.check((argv, aliases) => {
const validKeys = new Set([
"$0",
"_",
...Object.keys(aliases),
...Object.keys(aliases)
.map(key => aliases[key])
.reduce((a, b) => [...a, ...b])
]);
const invalidKeys = Object.keys(argv).filter(key => !validKeys.has(key));
if (invalidKeys.length > 0) {
const firstInvalidArg =
(invalidKeys[0].length === 1 ? "-" : "--") + invalidKeys[0];
throw new Error(`Unknown argument: ${firstInvalidArg}`);
}
return true;
})
.help()
.parse();
function validateExtraPositionalArgs({ max }) {
return argv => {
if (argv._.length > max) {
throw new Error(`Unknown argument: ${argv._[1]}`);
}
return true;
};
}
function getAllPresetInfo() {
return new Promise((resolve, reject) => {
let output = "";
spawn(path.join(__dirname, "./collector/build/list-presets"))
.on("error", reject)
.stdout.on("data", chunk => {
output += chunk;
})
.on("end", () => {
try {
resolve(
JSON.parse(output).sort((a, b) => a.name.localeCompare(b.name))
);
} catch (err) {
reject(err);
}
})
.on("error", reject);
});
}
async function list() {
const presets = await getAllPresetInfo();
const maxNameLength = Math.max(...presets.map(preset => preset.name.length));
const presetToString = preset =>
` ${preset.name.padEnd(maxNameLength)} ${preset.description || ""}`;
console.info("Available Presets:");
console.info(
presetToString({
name: "all",
description: "Shortcut for all available presets."
})
);
console.info(
presets
.filter(preset => preset.isAvailable)
.map(presetToString)
.join("\n")
);
console.info("");
console.info("Unavailable Presets:");
console.info(
presets
.filter(preset => !preset.isAvailable)
.map(presetToString)
.join("\n")
);
}
async function collect({
presets,
events,
resultOption,
executable,
executableArgs,
period,
inFile,
outFile,
errFile,
visualizeOption,
showTimer,
wattsupDevice
}) {
const resultFile = resultOption || tempy.file({ extension: "bin" });
const allPresetInfo = await getAllPresetInfo();
const presetsSet = new Set([
...presets.filter(preset => preset !== "all"),
...(presets.includes("all")
? allPresetInfo.filter(info => info.isAvailable).map(info => info.name)
: [])
]);
for (const preset of presetsSet) {
const presetInfo = allPresetInfo.find(info => info.name === preset);
if (!presetInfo) {
console.error(`Invalid preset: ${preset}`);
console.error("Try `alex list` to see a list of available presets.");
process.exit(1);
} else if (!presetInfo.isAvailable) {
console.error(`Preset unavailable: ${preset}`);
console.error("This is most likely due to a lack of hardware support.");
console.error("Try `alex list` to see a list of available presets.");
process.exit(1);
}
}
let startTime = Date.now();
let progressInterval;
process.on("SIGUSR2", () => {
console.info("Collecting performance data...");
if (showTimer) {
const MS_PER_SEC = 1000;
startTime = Date.now();
progressInterval = setInterval(() => {
// Clear previous progress message
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
const time = prettyMS(Math.max(Date.now() - startTime, 0), {
verbose: true,
secDecimalDigits: 0
});
process.stdout.write(`It's been ${time}. Still going...`);
}, 1 * MS_PER_SEC);
}
});
console.info(
"$ " +
[executable, ...executableArgs].join(" ") +
(inFile ? ` < ${inFile}` : "")
);
console.info("Waiting for collection to start...");
const collector = spawn(executable, executableArgs, {
env: {
...process.env,
COLLECTOR_PERIOD: period,
COLLECTOR_PRESETS: [...presetsSet].join(","),
COLLECTOR_EVENTS: events.join(","),
COLLECTOR_RESULT_FILE: resultFile,
COLLECTOR_WATTSUP_DEVICE: wattsupDevice,
COLLECTOR_NOTIFY_START: "yes",
COLLECTOR_INPUT: inFile ? inFile : "",
LD_PRELOAD: path.join(__dirname, "./collector/build/collector.so")
}
});
collector.on("error", err => {
console.error(`Couldn't start collection: ${err.message}`);
process.exit(1);
});
// Pipe through inputs and outputs
if (inFile) {
fs.createReadStream(inFile)
.pipe(collector.stdin)
.on("error", err =>
console.error(`Problem connecting to program stdin: ${err.message}`)
);
} else {
process.stdin.pipe(collector.stdin);
}
if (outFile) {
collector.stdout
.pipe(fs.createWriteStream(outFile))
.on("error", err =>
console.error(`Problem connecting to program stdout: ${err.message}`)
);
} else {
collector.stdout.pipe(process.stdout);
}
if (errFile) {
collector.stderr
.pipe(fs.createWriteStream(errFile))
.on("error", err =>
console.error(`Problem connecting to program stderr: ${err.message}`)
);
} else {
collector.stderr.pipe(process.stderr);
}
collector.on("exit", async code => {
clearInterval(progressInterval);
// Clear out progress message
readline.clearLine(process.stdout, 0);
readline.cursorTo(process.stdout, 0);
const timeSpent = prettyMS(Date.now() - startTime, { verbose: true });
console.info(`Finished after collecting for ${timeSpent}.`);
const errorCodes = {
1: "Internal error.",
2: "There was a problem with the result file.",
3: "There was a problem with the executable file.",
4: "There was a problem with the debug symbols file.",
5: "There was a problem accessing an environment variable.",
6: "There was a problem opening the event name.",
7: "Invalid parameter(s) for collector"
};
if (code in errorCodes) {
console.error(errorCodes[code]);
console.error(`Check ${errFile || "error logs"} for details`);
} else {
if (resultOption) {
console.info(`Results saved to ${resultFile}`);
}
if (visualizeOption === "window") {
visualize(resultFile);
} else if (visualizeOption === "ask") {
const readlineInterface = readline.createInterface(
process.stdin,
process.stdout
);
readlineInterface.question(
"Would you like to see a visualization of the results ([yes]/no)? ",
answer => {
if (answer !== "no") {
visualize(resultFile);
}
readlineInterface.close();
}
);
} else if (visualizeOption === "no") {
process.exit(0);
}
}
});
}
function visualize(resultFile, heapSize) {
spawn(
path.join(__dirname, "./node_modules/.bin/electron"),
[path.join(__dirname, "./visualizer"), resultFile, heapSize],
{ stdio: ["ignore", "inherit", "ignore"] }
);
}