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
|
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
import os
import sys
import json
import argparse
import subprocess
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
__version__ = '0.3'
# silence SettingWithCopyWarning globally
# https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#evaluation-order-matters
pd.options.mode.chained_assignment = None
def plot_histogram(filename):
with open(filename) as file:
rawdata = json.load(file)
cpu_id = 0
fig, ax = plt.subplots()
while True:
if str(cpu_id) not in rawdata['cpu']:
break
cid = str(cpu_id)
data = rawdata['cpu'][cid]
d = {int(k): int(v) for k, v in data['histogram'].items()}
l = 'cpu{} min{:>3} avg{:>7} max{:>3}'.format(
cid,
rawdata['cpu'][cid]['min'],
rawdata['cpu'][cid]['avg'],
rawdata['cpu'][cid]['max'])
ax.bar(list(d.keys()), list(d.values()), log=True, alpha=0.5, label=l)
cpu_id = cpu_id + 1
L = ax.legend()
plt.setp(L.texts, family='monospace')
plt.show()
def load_samples(filename):
dt = np.dtype([('CPUID', 'u4'),
('Seconds', 'u8'),
('Nanoseconds', 'u8'),
('Value', 'u8')])
data = np.fromfile(filename, dtype=dt)
df = pd.DataFrame(data)
return df
def plot_all_cpus(df):
ids = df["CPUID"].unique()
max_jitter = max(df["Value"])
fig = plt.figure()
axes = fig.subplots(len(ids))
for ax, data in zip(
iter(axes),
(df[df["CPUID"] == id] for id in ids),
):
data["Time"] = data["Seconds"] + data["Nanoseconds"] * 10**-9
ax.plot("Time", "Value", data=data)
ax.set_xlabel("Time [s]")
ax.set_ylabel("Latency [us]")
ax.set_ylim(bottom=0, top=max_jitter)
plt.show()
def main():
ap = argparse.ArgumentParser(description='Plot statistics collected with jitterdebugger')
ap.add_argument('--version', action='version',
version='%(prog)s ' + __version__)
sap = ap.add_subparsers(dest='cmd')
hrs = sap.add_parser('hist', help='Print historgram')
hrs.add_argument('HIST_FILE')
srs = sap.add_parser('samples', help='Plot samples graph')
srs.add_argument('SAMPLE_FILE')
args = ap.parse_args(sys.argv[1:])
if args.cmd == 'hist':
fname = args.HIST_FILE
if os.path.isdir(fname):
fname = fname + '/results.json'
plot_histogram(fname)
elif args.cmd == 'samples':
fname = args.SAMPLE_FILE
if os.path.isdir(fname):
fname = fname + '/samples.raw'
df = load_samples(fname)
plot_all_cpus(df)
if __name__ == '__main__':
main()
|