Commit c8e11dc8 authored by Ben Gamari's avatar Ben Gamari 🐢

sample-proc: Add ability to spawn new process

parent 768497fc
......@@ -10,6 +10,7 @@ Typically used with the VmRSS and RssAnon metrics.
from pathlib import Path
import sys
import time
import subprocess
from typing import List, Optional
def read_proc(pid: int) -> dict:
......@@ -49,17 +50,26 @@ def plot_it(results: "numpy.ndarray",
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--pid', required=True, type=int, help='process id')
parser.add_argument('-m', '--metric', action='append', type=str, help='metric names')
parser.add_argument('-s', '--period', type=float, help='sampling period (seconds)', default=0.1)
parser.add_argument('-o', '--output', type=argparse.FileType('w'), help='output file', default=sys.stdout)
parser.add_argument('-P', '--plot', nargs='?', help='plot it', default=False, const=True)
parser.add_argument('-p', '--pid', type=int, help='process id')
parser.add_argument('cmdline', type=str, nargs='*', help='command-line to run')
args = parser.parse_args()
metrics = args.metric
period = args.period
pid =
output = args.output
if is not None and args.cmdline is not None:
raise ValueError("Both pid and command-line given")
elif is not None:
pid =
elif args.cmdline == []:
raise ValueError("Expected either pid or (non-empty) command-line")
subp = subprocess.Popen(args.cmdline)
pid =
if args.plot and output == sys.stdout:
print("Must output to file in order to plot", file=sys.stderr)
......@@ -68,13 +78,14 @@ def main():
while True:
t = time.time()
result = read_proc(
result = read_proc(pid)
values = [result[metric].split()[0] for metric in metrics]
output.write('\t'.join([str(t)] + values) + '\n')
except Exception as e:
print(f"Failed with {e}", file=sys.stderr)
import traceback
if args.plot is not False:
if args.plot is True:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment