-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjitterplot
executable file
·160 lines (129 loc) · 4.57 KB
/
jitterplot
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
import os
import sys
import json
import argparse
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_cdf(filename, outfilename):
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]
bins = [int(i) for i in data['histogram'].keys()]
values = [int(i) for i in data['histogram'].values()]
cumulative = np.cumsum(values) / np.sum(values)
# show 99.9 and 99.99 percentiles in legend
p3_idx = list(map(lambda i: i > 0.999, cumulative)).index(True)
p4_idx = list(map(lambda i: i > 0.9999, cumulative)).index(True)
p3 = bins[p3_idx]
p4 = bins[p4_idx]
pmax = bins[-1]
ax.step(bins, cumulative,
label='cpu{}: p99.9={}, p99.99={}, max={}'
.format(cid, p3, p4, pmax))
cpu_id = cpu_id + 1
L = ax.legend()
plt.grid(color='lightgrey', linestyle='-', linewidth=1, which='both')
plt.yticks(ticks=np.arange(0, 1.1, 0.1))
plt.xlabel('jitter [us]')
plt.ylabel('probability')
plt.setp(L.texts, family='monospace')
if outfilename is not None:
plt.savefig(outfilename)
plt.show()
def plot_histogram(filename, outfilename):
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()}
lbl = '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=lbl)
cpu_id = cpu_id + 1
L = ax.legend()
plt.setp(L.texts, family='monospace')
plt.xlabel('jitter [us]')
plt.ylabel('frequency')
if outfilename is not None:
plt.savefig(outfilename)
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, outfilename):
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)
if outfilename is not None:
plt.savefig(outfilename)
plt.show()
def main():
ap = argparse.ArgumentParser(
description='Plot statistics collected with jitterdebugger')
ap.add_argument('--version', action='version',
version='%(prog)s ' + __version__)
ap.add_argument('--output', help='output file name to save figure',
default=None, action='store', type=str)
sap = ap.add_subparsers(dest='cmd')
hrs = sap.add_parser('hist', help='Print historgram')
hrs.add_argument('HIST_FILE')
crs = sap.add_parser('cdf', help='Plot cumulative densitiy function')
crs.add_argument('CDF_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, args.output)
elif args.cmd == 'cdf':
fname = args.CDF_FILE
if os.path.isdir(fname):
fname = fname + '/results.json'
plot_cdf(fname, args.output)
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, args.output)
if __name__ == '__main__':
main()