Skip to content

Instantly share code, notes, and snippets.

@BafS
Created December 6, 2023 17:07
Show Gist options
  • Save BafS/b677f0f292a00c3aae75d20df55e4f1a to your computer and use it in GitHub Desktop.
Save BafS/b677f0f292a00c3aae75d20df55e4f1a to your computer and use it in GitHub Desktop.
Convert SPX format to trace event
<?php
// Usage: php convert-spx-to-trace.php input.dms > foo-trace.json
[, $file] = $argv;
$data = file($file);
$functions = [];
$events = [];
$type = null;
foreach ($data as $line) {
$line = rtrim($line);
if ($line === '[events]') {
$type = 'event';
continue;
}
if ($line === '[functions]') {
$type = 'function';
continue;
}
if ($type === 'event') {
$events[] = explode(' ', $line);
}
if ($type === 'function') {
$functions[] = $line;
}
}
$info = [];
foreach ($events as $eventInfo) {
$fnIndex = (int) $eventInfo[0];
$eventType = (int) $eventInfo[1];
$m1 = (float) $eventInfo[2];
// To have the right time unit
$m1 = $m1 / 1000;
$info[] = [
'ph' => $eventType === 1 ? 'B' : 'E',
// We assume that the first metrics is the wall time inc.
'ts' => $m1,
'name' => $functions[$fnIndex],
];
}
$trace = [
'otherData' => [
'buildTs' => time(),
],
'displayTimeUnit' => 'ns',
'traceEvents' => $info,
];
echo json_encode($trace, flags: JSON_THROW_ON_ERROR);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment