graphstats: Add support for generating a graph of mcu frequency

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-12-29 15:27:51 -05:00
parent f10bd5726d
commit cf4c31cb88
1 changed files with 53 additions and 4 deletions

View File

@ -8,6 +8,7 @@ import optparse, datetime
import matplotlib import matplotlib
matplotlib.use('Agg') matplotlib.use('Agg')
import matplotlib.pyplot, matplotlib.dates, matplotlib.font_manager import matplotlib.pyplot, matplotlib.dates, matplotlib.font_manager
import matplotlib.ticker
MAXBANDWIDTH=25000. MAXBANDWIDTH=25000.
MAXBUFFER=2. MAXBUFFER=2.
@ -23,8 +24,16 @@ def parse_log(logname):
#if parts and parts[0] == 'INFO:root:shutdown:': #if parts and parts[0] == 'INFO:root:shutdown:':
# break # break
continue continue
keyparts = dict(p.split('=', 1) prefix = ""
for p in parts[2:] if not p.endswith(':')) keyparts = {}
for p in parts[2:]:
if p.endswith(':'):
prefix = p
if prefix == 'mcu:':
prefix = ''
continue
name, val = p.split('=', 1)
keyparts[prefix + name] = val
if keyparts.get('bytes_write', '0') == '0': if keyparts.get('bytes_write', '0') == '0':
continue continue
keyparts['#sampletime'] = float(parts[1][:-1]) keyparts['#sampletime'] = float(parts[1][:-1])
@ -107,11 +116,48 @@ def plot_mcu(data, maxbw, outname, graph_awake=False):
ax1.grid(True) ax1.grid(True)
fig.savefig(outname) fig.savefig(outname)
def plot_frequency(data, outname):
all_keys = {}
for d in data:
all_keys.update(d)
graph_keys = {}
for key in all_keys:
if ((key in ("freq", "adj")
or key.endswith(":freq") or key.endswith(":adj"))
and key not in graph_keys):
graph_keys[key] = ([], [])
basetime = lasttime = data[0]['#sampletime']
for d in data:
st = datetime.datetime.utcfromtimestamp(d['#sampletime'])
for key, (times, values) in graph_keys.items():
val = d.get(key)
if val not in (None, '0', '1'):
times.append(st)
values.append(float(val))
# Build plot
fig, ax1 = matplotlib.pyplot.subplots()
ax1.set_title("MCU frequency")
ax1.set_xlabel('Time')
ax1.set_ylabel('Frequency')
for key in sorted(graph_keys):
times, values = graph_keys[key]
ax1.plot_date(times, values, '.', label=key)
fontP = matplotlib.font_manager.FontProperties()
fontP.set_size('x-small')
ax1.legend(loc='best', prop=fontP)
ax1.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%H:%M'))
ax1.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('%d'))
ax1.grid(True)
fig.savefig(outname)
def main(): def main():
usage = "%prog [options] <logfile> <outname>" usage = "%prog [options] <logfile> <outname>"
opts = optparse.OptionParser(usage) opts = optparse.OptionParser(usage)
opts.add_option("-a", "--awake", action="store_true" opts.add_option("-a", "--awake", action="store_true",
, help="graph mcu awake time") help="graph mcu awake time")
opts.add_option("-f", "--frequency", action="store_true",
help="graph mcu frequency")
options, args = opts.parse_args() options, args = opts.parse_args()
if len(args) != 2: if len(args) != 2:
opts.error("Incorrect number of arguments") opts.error("Incorrect number of arguments")
@ -119,6 +165,9 @@ def main():
data = parse_log(logname) data = parse_log(logname)
if not data: if not data:
return return
if options.frequency:
plot_frequency(data, outname)
return
plot_mcu(data, MAXBANDWIDTH, outname, graph_awake=options.awake) plot_mcu(data, MAXBANDWIDTH, outname, graph_awake=options.awake)
if __name__ == '__main__': if __name__ == '__main__':