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 = args.pid
output = args.output
if args.pid is not None and args.cmdline is not None:
raise ValueError("Both pid and command-line given")
elif args.pid is not None:
pid = args.pid
elif args.cmdline == []:
raise ValueError("Expected either pid or (non-empty) command-line")
else:
subp = subprocess.Popen(args.cmdline)
pid = subp.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():
try:
while True:
t = time.time()
result = read_proc(args.pid)
result = read_proc(pid)
values = [result[metric].split()[0] for metric in metrics]
output.write('\t'.join([str(t)] + values) + '\n')
output.flush()
time.sleep(period)
except Exception as e:
print(f"Failed with {e}", file=sys.stderr)
import traceback
traceback.print_exc(file=sys.stderr)
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