470 lines
20 KiB
PHP
Executable file
470 lines
20 KiB
PHP
Executable file
#!/usr/bin/env php
|
|
<?php
|
|
/**
|
|
* cryo-inspect — command line front-end for CryoReader (ships with ext-cryo).
|
|
*
|
|
* cryo-inspect info <file.cryo>
|
|
* cryo-inspect top <file.cryo> [--by=retained|class|site|stack|write] [--limit=20]
|
|
* cryo-inspect roots <file.cryo>
|
|
* cryo-inspect path <file.cryo> <node> [--max=5]
|
|
* cryo-inspect node <file.cryo> <node>
|
|
* cryo-inspect dominators <file.cryo> [--limit=20]
|
|
* cryo-inspect orphans <file.cryo>
|
|
* cryo-inspect dupes <file.cryo> [--limit=20]
|
|
* cryo-inspect query <file.cryo> ['class=App\* and retained>1M']
|
|
* [--type= --class= --site= --min_size= --epoch_min= --epoch_max=] [--limit=100]
|
|
* cryo-inspect heuristics <file.cryo> [<older.cryo> ...] (2+ files: growth too)
|
|
* cryo-inspect delta <a.cryo> <b.cryo> [<c.cryo> ...] [--by=site|class|stack]
|
|
* [--explain=<row index|site>] [--timeline]
|
|
* cryo-inspect diff <a.cryo> <b.cryo> [--limit=20]
|
|
* cryo-inspect series <a.cryo> <b.cryo> [...] classes growing at every snapshot
|
|
* cryo-inspect export <file.cryo> [--format=json|dot|csv] [--edges] (csv: nodes, or edges)
|
|
* cryo-inspect serve [--port=9600] <file.cryo> [...] local JSON API (127.0.0.1)
|
|
*
|
|
* Every table command accepts --json.
|
|
*/
|
|
|
|
$args = array_slice($argv, 1);
|
|
$cmd = array_shift($args) ?? 'help';
|
|
|
|
/* `metrics` also works without ext-cryo, through Cryo Studio's pure-PHP reader */
|
|
$studioSeries = __DIR__ . '/../../cryo-studio/src/MetricSeries.php';
|
|
if (!extension_loaded('cryo') && !($cmd === 'metrics' && is_file($studioSeries))) {
|
|
fwrite(STDERR, "cryo-inspect: ext-cryo is not loaded (php -d extension=cryo)\n");
|
|
exit(2);
|
|
}
|
|
$opts = [];
|
|
$files = [];
|
|
foreach ($args as $a) {
|
|
if (preg_match('/^--([a-z_]+)(?:=(.*))?$/s', $a, $m)) {
|
|
$opts[$m[1]] = $m[2] ?? true;
|
|
} else {
|
|
$files[] = $a;
|
|
}
|
|
}
|
|
|
|
const JSON_FLAGS = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION;
|
|
|
|
function table(array $rows, array $cols): void
|
|
{
|
|
$cell = fn($v) => is_bool($v) ? ($v ? 'yes' : '') : (is_array($v) ? implode(' ', $v) : (string) ($v ?? '-'));
|
|
$w = [];
|
|
foreach ($cols as $c) {
|
|
$w[$c] = strlen($c);
|
|
foreach ($rows as $r) {
|
|
$w[$c] = max($w[$c], strlen($cell($r[$c] ?? null)));
|
|
}
|
|
}
|
|
echo rtrim(implode(' ', array_map(fn($c) => str_pad($c, $w[$c]), $cols))), "\n";
|
|
foreach ($rows as $r) {
|
|
echo rtrim(implode(' ', array_map(fn($c) => str_pad($cell($r[$c] ?? null), $w[$c]), $cols))), "\n";
|
|
}
|
|
}
|
|
|
|
/** Prints rows as JSON (--json) or as a table. */
|
|
function output(array $rows, array $cols, array $opts): void
|
|
{
|
|
if (isset($opts['json'])) {
|
|
echo json_encode($rows, JSON_FLAGS), "\n";
|
|
return;
|
|
}
|
|
table($rows, $cols);
|
|
}
|
|
|
|
function need_files(array $files, int $n, string $what): void
|
|
{
|
|
if (count($files) < $n) {
|
|
throw new InvalidArgumentException("expected $what");
|
|
}
|
|
}
|
|
|
|
function node_arg(array $files): int
|
|
{
|
|
if (!isset($files[1]) || !preg_match('/^\d+$/', $files[1])) {
|
|
throw new InvalidArgumentException('expected a node id after the file');
|
|
}
|
|
return (int) $files[1];
|
|
}
|
|
|
|
/** Classes whose instance count never decreases and grows overall (timeline of a delta). */
|
|
function series(array $files): array
|
|
{
|
|
$t = CryoReader::delta($files)->timeline();
|
|
$rows = [];
|
|
foreach ($t['by_class'] as $class => $counts) {
|
|
$mono = true;
|
|
for ($i = 1; $i < count($counts); $i++) {
|
|
if ($counts[$i] < $counts[$i - 1]) {
|
|
$mono = false;
|
|
}
|
|
}
|
|
if ($mono && end($counts) > $counts[0]) {
|
|
$rows[] = ['class' => (string) $class, 'counts' => $counts, 'growth' => end($counts) - $counts[0]];
|
|
}
|
|
}
|
|
usort($rows, fn($a, $b) => $b['growth'] <=> $a['growth']);
|
|
return $rows;
|
|
}
|
|
|
|
/** Whole graph: info, roots, node records, edges. */
|
|
function export_graph(CryoReader $r): array
|
|
{
|
|
$nodes = $edges = [];
|
|
$n = $r->info()['nodes'];
|
|
for ($v = 0; $v < $n; $v++) {
|
|
$node = $r->node($v);
|
|
foreach ($node['out'] as $e) {
|
|
$edges[] = ['from' => $v, 'to' => $e['node'], 'name' => $e['name']];
|
|
}
|
|
unset($node['out'], $node['in'], $node['roots']);
|
|
$nodes[] = $node;
|
|
}
|
|
return ['info' => $r->info(), 'roots' => $r->roots(), 'nodes' => $nodes, 'edges' => $edges];
|
|
}
|
|
|
|
function csv_line(array $cells): string
|
|
{
|
|
return implode(',', array_map(function ($v) {
|
|
$v = is_array($v) ? implode(' <- ', $v) : (string) ($v ?? '');
|
|
return preg_match('/[",\n\r]/', $v) ? '"' . str_replace('"', '""', $v) . '"' : $v;
|
|
}, $cells)) . "\n";
|
|
}
|
|
|
|
/** Local JSON API: GET /info /top /roots /delta /path /node /query /dominators /orphans /dupes /heuristics */
|
|
function serve(array $files, int $port, ?int $max_requests = null): void
|
|
{
|
|
$srv = @stream_socket_server("tcp://127.0.0.1:$port", $errno, $errstr);
|
|
if (!$srv) {
|
|
throw new RuntimeException("cannot listen on 127.0.0.1:$port: $errstr");
|
|
}
|
|
fwrite(STDERR, "cryo-inspect: serving " . count($files) . " snapshot(s) on http://127.0.0.1:$port/\n");
|
|
$readers = [];
|
|
$reader = function (array $q) use ($files, &$readers): CryoReader {
|
|
$f = $q['file'] ?? '0';
|
|
$path = preg_match('/^\d+$/', $f) ? ($files[(int) $f] ?? null) : (in_array($f, $files, true) ? $f : null);
|
|
if ($path === null) {
|
|
throw new InvalidArgumentException('unknown file (give its index or one of the served paths)');
|
|
}
|
|
return $readers[$path] ??= CryoReader::open($path);
|
|
};
|
|
for ($served = 0; $max_requests === null || $served < $max_requests; $served++) {
|
|
$c = @stream_socket_accept($srv, -1);
|
|
if (!$c) {
|
|
continue;
|
|
}
|
|
$line = (string) fgets($c);
|
|
while (($h = fgets($c)) !== false && rtrim($h) !== '') {
|
|
}
|
|
$status = 200;
|
|
try {
|
|
if (!preg_match('#^GET (\S+) HTTP/#', $line, $m)) {
|
|
throw new DomainException('only GET is supported', 405);
|
|
}
|
|
$url = parse_url($m[1]);
|
|
parse_str($url['query'] ?? '', $q);
|
|
$limit = (int) ($q['limit'] ?? 20);
|
|
$body = match ($url['path'] ?? '/') {
|
|
'/', '/files' => ['files' => $files],
|
|
'/info' => $reader($q)->info(),
|
|
'/roots' => $reader($q)->roots(),
|
|
'/top' => $reader($q)->top($q['by'] ?? 'retained', $limit),
|
|
'/path' => $reader($q)->path((int) ($q['node'] ?? -1), (int) ($q['max'] ?? 5)),
|
|
'/node' => $reader($q)->node((int) ($q['id'] ?? $q['node'] ?? -1)),
|
|
'/query' => $reader($q)->query(['expr' => (string) ($q['q'] ?? ''), 'limit' => (int) ($q['limit'] ?? 100)]),
|
|
'/dominators' => $reader($q)->dominators($limit),
|
|
'/orphans' => $reader($q)->orphans(),
|
|
'/dupes' => $reader($q)->dupes($limit),
|
|
'/heuristics' => $reader($q)->heuristics(count($files) >= 2 ? $files : []),
|
|
'/delta' => (function () use ($files, $q) {
|
|
$d = CryoReader::delta($files);
|
|
return match ($q['by'] ?? 'site') { 'class' => $d->byClass(), 'stack' => $d->byStack(),
|
|
'timeline' => $d->timeline(), default => $d->bySite() };
|
|
})(),
|
|
default => throw new DomainException('not found', 404),
|
|
};
|
|
} catch (Throwable $e) {
|
|
$status = $e instanceof DomainException ? $e->getCode() : 400;
|
|
$body = ['error' => $e->getMessage()];
|
|
}
|
|
$json = json_encode($body, JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION | JSON_PARTIAL_OUTPUT_ON_ERROR);
|
|
$text = [200 => 'OK', 400 => 'Bad Request', 404 => 'Not Found', 405 => 'Method Not Allowed'][$status] ?? 'Error';
|
|
fwrite($c, "HTTP/1.1 $status $text\r\nContent-Type: application/json\r\nContent-Length: " . strlen($json)
|
|
. "\r\nConnection: close\r\n\r\n" . $json);
|
|
fclose($c);
|
|
}
|
|
}
|
|
|
|
/** Summary of a .cryom series (array from cryo_metrics_read()). */
|
|
function metrics_summary(array $series): array
|
|
{
|
|
$samples = $series['samples'];
|
|
$n = count($samples);
|
|
$out = [
|
|
'samples' => $n, 'sessions' => count($series['sessions']), 'duration' => 0.0,
|
|
'min' => null, 'max' => null, 'last' => null, 'peak' => null,
|
|
'rss_last' => null, 'objects_last' => null, 'slope_mb_per_hour' => null,
|
|
'marks' => $series['marks'], 'snapshots' => array_column($series['snapshots'], 'file'),
|
|
];
|
|
if ($n === 0) {
|
|
return $out;
|
|
}
|
|
$t0 = $samples[0]['time'];
|
|
$mem = array_column($samples, 'memory');
|
|
$out['duration'] = round($samples[$n - 1]['time'] - $t0, 3);
|
|
$out['min'] = min($mem);
|
|
$out['max'] = max($mem);
|
|
$out['last'] = $mem[$n - 1];
|
|
$out['peak'] = $samples[$n - 1]['memory_peak'];
|
|
$out['rss_last'] = $samples[$n - 1]['rss'];
|
|
$out['objects_last'] = $samples[$n - 1]['objects_alive'];
|
|
if ($n >= 2) {
|
|
$sx = $sy = $sxx = $sxy = 0.0;
|
|
foreach ($samples as $i => $s) {
|
|
$x = $s['time'] - $t0;
|
|
$sx += $x; $sy += $mem[$i]; $sxx += $x * $x; $sxy += $x * $mem[$i];
|
|
}
|
|
$den = $n * $sxx - $sx * $sx;
|
|
if ($den > 0) {
|
|
$out['slope_mb_per_hour'] = round(($n * $sxy - $sx * $sy) / $den * 3600 / 1048576, 3);
|
|
}
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
function mb(int|float|null $b): string
|
|
{
|
|
return $b === null ? '-' : sprintf('%.2f MB', $b / 1048576);
|
|
}
|
|
|
|
try {
|
|
switch ($cmd) {
|
|
case 'metrics':
|
|
if (extension_loaded('cryo')) {
|
|
$series = cryo_metrics_read($files[0] ?? '');
|
|
} else {
|
|
require_once $studioSeries;
|
|
$series = Cryo\Studio\MetricSeries::open($files[0] ?? '')->toArray();
|
|
}
|
|
$sum = metrics_summary($series);
|
|
if (isset($opts['json'])) {
|
|
echo json_encode($sum, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), "\n";
|
|
break;
|
|
}
|
|
printf("%-14s %d (%d session%s)\n", 'samples', $sum['samples'], $sum['sessions'], $sum['sessions'] > 1 ? 's' : '');
|
|
printf("%-14s %.3f s\n", 'duration', $sum['duration']);
|
|
printf("%-14s %s\n", 'memory min', mb($sum['min']));
|
|
printf("%-14s %s\n", 'memory max', mb($sum['max']));
|
|
printf("%-14s %s\n", 'memory last', mb($sum['last']));
|
|
printf("%-14s %s\n", 'memory peak', mb($sum['peak']));
|
|
printf("%-14s %s\n", 'rss last', mb($sum['rss_last']));
|
|
printf("%-14s %s\n", 'objects last', $sum['objects_last'] ?? '-');
|
|
printf("%-14s %s\n", 'slope', $sum['slope_mb_per_hour'] === null ? '-' : sprintf('%+.3f MB/h', $sum['slope_mb_per_hour']));
|
|
$t0 = $series['samples'][0]['time'] ?? 0;
|
|
foreach ($sum['marks'] as $m) {
|
|
printf("%-14s #%d +%.3f s %s\n", 'mark', $m['seq'], $m['time'] - $t0, $m['label']);
|
|
}
|
|
foreach ($sum['snapshots'] as $f) {
|
|
printf("%-14s %s\n", 'snapshot', $f);
|
|
}
|
|
break;
|
|
|
|
case 'info':
|
|
need_files($files, 1, 'a .cryo file');
|
|
$info = CryoReader::open($files[0])->info();
|
|
if (isset($opts['json'])) {
|
|
echo json_encode($info, JSON_FLAGS), "\n";
|
|
break;
|
|
}
|
|
foreach ($info as $k => $v) {
|
|
printf("%-18s %s\n", $k, $v);
|
|
}
|
|
break;
|
|
|
|
case 'top':
|
|
need_files($files, 1, 'a .cryo file');
|
|
$by = $opts['by'] ?? 'retained';
|
|
$rows = CryoReader::open($files[0])->top($by, (int) ($opts['limit'] ?? 20));
|
|
output($rows, $by === 'retained' ? ['class', 'count', 'bytes', 'retained'] : [$by, 'count', 'bytes'], $opts);
|
|
break;
|
|
|
|
case 'roots':
|
|
need_files($files, 1, 'a .cryo file');
|
|
output(CryoReader::open($files[0])->roots(), ['kind', 'name', 'node'], $opts);
|
|
break;
|
|
|
|
case 'path':
|
|
need_files($files, 1, 'a .cryo file');
|
|
$paths = CryoReader::open($files[0])->path(node_arg($files), (int) ($opts['max'] ?? 5));
|
|
if (isset($opts['json'])) {
|
|
echo json_encode($paths, JSON_FLAGS), "\n";
|
|
break;
|
|
}
|
|
if (!$paths) {
|
|
echo "node {$files[1]} is not reachable from any root\n";
|
|
break;
|
|
}
|
|
foreach ($paths as $i => $steps) {
|
|
echo $i ? "\n" : '', 'path ', $i + 1, ":\n";
|
|
table($steps, ['kind', 'name', 'node', 'type', 'class', 'write']);
|
|
}
|
|
break;
|
|
|
|
case 'node':
|
|
need_files($files, 1, 'a .cryo file');
|
|
$n = CryoReader::open($files[0])->node(node_arg($files));
|
|
if ($n === null) {
|
|
throw new InvalidArgumentException("no node {$files[1]}");
|
|
}
|
|
if (isset($opts['json'])) {
|
|
echo json_encode($n, JSON_FLAGS), "\n";
|
|
break;
|
|
}
|
|
foreach (['node', 'type', 'class', 'size', 'site', 'write', 'epoch', 'handle'] as $k) {
|
|
printf("%-8s %s\n", $k, $n[$k] ?? '-');
|
|
}
|
|
foreach ($n['stack'] ?? [] as $i => $frame) {
|
|
printf("%-8s %s\n", $i ? '' : 'stack', $frame);
|
|
}
|
|
foreach ($n['roots'] as $rt) {
|
|
printf("root %s %s\n", $rt['kind'], $rt['name']);
|
|
}
|
|
echo "\nout:\n";
|
|
table($n['out'], ['name', 'node', 'type', 'class']);
|
|
echo "\nin:\n";
|
|
table($n['in'], ['name', 'node', 'type', 'class']);
|
|
break;
|
|
|
|
case 'dominators':
|
|
need_files($files, 1, 'a .cryo file');
|
|
output(CryoReader::open($files[0])->dominators((int) ($opts['limit'] ?? 20)),
|
|
['node', 'type', 'class', 'self', 'retained', 'idom'], $opts);
|
|
break;
|
|
|
|
case 'orphans':
|
|
need_files($files, 1, 'a .cryo file');
|
|
output(CryoReader::open($files[0])->orphans(), ['node', 'class', 'size', 'site', 'write', 'epoch'], $opts);
|
|
break;
|
|
|
|
case 'dupes':
|
|
need_files($files, 1, 'a .cryo file');
|
|
output(CryoReader::open($files[0])->dupes((int) ($opts['limit'] ?? 20)),
|
|
['type', 'hash', 'length', 'count', 'bytes', 'wasted', 'node'], $opts);
|
|
break;
|
|
|
|
case 'query':
|
|
need_files($files, 1, 'a .cryo file');
|
|
$filter = [];
|
|
if (isset($files[1])) {
|
|
$filter['expr'] = implode(' ', array_slice($files, 1));
|
|
}
|
|
foreach (['type', 'class', 'site'] as $k) {
|
|
if (isset($opts[$k])) $filter[$k] = (string) $opts[$k];
|
|
}
|
|
foreach (['min_size', 'epoch_min', 'epoch_max', 'limit'] as $k) {
|
|
if (isset($opts[$k])) $filter[$k] = (int) $opts[$k];
|
|
}
|
|
output(CryoReader::open($files[0])->query($filter),
|
|
['node', 'type', 'class', 'size', 'site', 'write', 'epoch', 'handle'], $opts);
|
|
break;
|
|
|
|
case 'heuristics':
|
|
need_files($files, 1, 'a .cryo file');
|
|
$rows = CryoReader::open($files[0])->heuristics(count($files) >= 2 ? $files : []);
|
|
if (isset($opts['json'])) {
|
|
echo json_encode($rows, JSON_FLAGS), "\n";
|
|
break;
|
|
}
|
|
foreach ($rows as $h) {
|
|
printf("%3d %-12s %s\n", $h['score'], $h['kind'], $h['message']);
|
|
if ($h['path']) {
|
|
echo ' ', implode(' -> ', array_map(fn($s) => $s['name'], $h['path'])), "\n";
|
|
}
|
|
}
|
|
break;
|
|
|
|
case 'delta':
|
|
need_files($files, 2, 'at least 2 .cryo files');
|
|
$by = $opts['by'] ?? 'site';
|
|
$delta = CryoReader::delta($files);
|
|
if (isset($opts['explain'])) {
|
|
$e = $delta->explain(preg_match('/^[0-9]+$/', (string) $opts['explain']) ? (int) $opts['explain'] : (string) $opts['explain']);
|
|
echo json_encode($e, JSON_FLAGS), "\n";
|
|
break;
|
|
}
|
|
if (isset($opts['timeline'])) {
|
|
echo json_encode($delta->timeline(), JSON_FLAGS), "\n";
|
|
break;
|
|
}
|
|
$rows = match ($by) { 'class' => $delta->byClass(), 'stack' => $delta->byStack(), default => $delta->bySite() };
|
|
if (isset($opts['json'])) {
|
|
echo json_encode($rows, JSON_FLAGS), "\n";
|
|
break;
|
|
}
|
|
table(array_map(fn($r) => [
|
|
$by => $r[$by],
|
|
'new' => $r['new_per_interval'],
|
|
'survivors' => $r['survivors'],
|
|
'survival' => round($r['survival'] * 100) . '%',
|
|
'bytes' => $r['bytes_last'],
|
|
'growing' => $r['growing'],
|
|
'hooked_by' => $r['hooked_by'],
|
|
], $rows), [$by, 'new', 'survivors', 'survival', 'bytes', 'growing', 'hooked_by']);
|
|
break;
|
|
|
|
case 'diff':
|
|
need_files($files, 2, 'two .cryo files');
|
|
output(array_slice(cryo_diff($files[0], $files[1]), 0, (int) ($opts['limit'] ?? 20)),
|
|
['class', 'count_a', 'count_b', 'count_delta', 'bytes_a', 'bytes_b', 'bytes_delta'], $opts);
|
|
break;
|
|
|
|
case 'series':
|
|
need_files($files, 2, 'at least 2 .cryo files');
|
|
output(series($files), ['class', 'counts', 'growth'], $opts);
|
|
break;
|
|
|
|
case 'export':
|
|
need_files($files, 1, 'a .cryo file');
|
|
$g = export_graph(CryoReader::open($files[0]));
|
|
$format = $opts['format'] ?? 'json';
|
|
if ($format === 'json') {
|
|
echo json_encode($g, JSON_FLAGS), "\n";
|
|
} elseif ($format === 'csv') {
|
|
if (isset($opts['edges'])) {
|
|
echo csv_line(['from', 'to', 'name']);
|
|
foreach ($g['edges'] as $e) echo csv_line($e);
|
|
} else {
|
|
echo csv_line(['node', 'type', 'class', 'size', 'site', 'write', 'epoch', 'handle']);
|
|
foreach ($g['nodes'] as $n) echo csv_line($n);
|
|
}
|
|
} elseif ($format === 'dot') {
|
|
$q = fn($s) => '"' . addcslashes((string) $s, "\"\\\n") . '"';
|
|
echo "digraph cryo {\n node [shape=box, fontsize=10];\n root [shape=doublecircle, label=\"roots\"];\n";
|
|
foreach ($g['nodes'] as $n) {
|
|
echo " n{$n['node']} [label=", $q(($n['class'] ?? $n['type']) . "\n#{$n['node']} {$n['size']} B"), "];\n";
|
|
}
|
|
foreach ($g['roots'] as $rt) {
|
|
echo " root -> n{$rt['node']} [label=", $q($rt['name']), ", style=dashed];\n";
|
|
}
|
|
foreach ($g['edges'] as $e) {
|
|
echo " n{$e['from']} -> n{$e['to']} [label=", $q($e['name']), "];\n";
|
|
}
|
|
echo "}\n";
|
|
} else {
|
|
throw new InvalidArgumentException('--format must be json, dot or csv');
|
|
}
|
|
break;
|
|
|
|
case 'serve':
|
|
need_files($files, 1, 'at least one .cryo file');
|
|
serve($files, (int) ($opts['port'] ?? 9600), isset($opts['requests']) ? (int) $opts['requests'] : null);
|
|
break;
|
|
|
|
default:
|
|
echo "usage: cryo-inspect info|top|roots|path|node|dominators|orphans|dupes|query|heuristics|delta|diff|series|export|serve|metrics <files...> [options]\n"
|
|
. " see the header of this script for the options of each command\n";
|
|
exit($cmd === 'help' ? 0 : 2);
|
|
}
|
|
} catch (Throwable $e) {
|
|
fwrite(STDERR, 'cryo-inspect: ' . $e->getMessage() . "\n");
|
|
exit(1);
|
|
}
|