| 1 | #!/usr/bin/python3
|
|---|
| 2 | import numpy as np
|
|---|
| 3 | import re
|
|---|
| 4 | import sys, getopt
|
|---|
| 5 | import fileinput
|
|---|
| 6 | import argparse
|
|---|
| 7 |
|
|---|
| 8 | #--------------------------------------------------------------------------------
|
|---|
| 9 | # Parse arguments
|
|---|
| 10 | parser = argparse.ArgumentParser(description='Produce a graph representing CPU activity over time.')
|
|---|
| 11 | parser.add_argument('-i', '--input' , dest='infile' , type=argparse.FileType('r'), default=sys.stdin, help='input file containing processor activity; if none specified then will use stdin.')
|
|---|
| 12 | parser.add_argument('-o', '--output', dest='outfile', type=argparse.FileType('w'), default=None, help='output file with any image format extension such as .png or .svg; if none specified then plt.show() will be used')
|
|---|
| 13 | args = parser.parse_args()
|
|---|
| 14 |
|
|---|
| 15 | #--------------------------------------------------------------------------------
|
|---|
| 16 | # Process data
|
|---|
| 17 | class Proc:
|
|---|
| 18 | def __init__(self, id, name, address):
|
|---|
| 19 | self.name = name
|
|---|
| 20 | self.address = address
|
|---|
| 21 | self.id = id
|
|---|
| 22 |
|
|---|
| 23 | class Point:
|
|---|
| 24 | def __init__(self, time, on):
|
|---|
| 25 | self.time = time
|
|---|
| 26 | self.on = on
|
|---|
| 27 |
|
|---|
| 28 | processors = {}
|
|---|
| 29 | data = {}
|
|---|
| 30 |
|
|---|
| 31 | #--------------------------------------------------------------------------------
|
|---|
| 32 | # Parse data
|
|---|
| 33 | for line in args.infile:
|
|---|
| 34 | match = re.match("Processor : ([0-9]+) - (.*) \((0x[0-9a-f]+)\)", line)
|
|---|
| 35 | if match :
|
|---|
| 36 | id = int(match.group(1))
|
|---|
| 37 | processors[id] = Proc(id, match.group(2), match.group(3))
|
|---|
| 38 | continue
|
|---|
| 39 |
|
|---|
| 40 | match = re.match("PH:([0-9]+) - ([0-9]+) ([0-9]+)", line)
|
|---|
| 41 | if match :
|
|---|
| 42 | id = int(match.group(1))
|
|---|
| 43 | if not id in data:
|
|---|
| 44 | data[id] = []
|
|---|
| 45 | data[id].append(Point(int(match.group(2)), int(match.group(3))))
|
|---|
| 46 | continue
|
|---|
| 47 |
|
|---|
| 48 | print("WARNING : line '%s' filterred not matched" % line, file=sys.stderr)
|
|---|
| 49 |
|
|---|
| 50 | #--------------------------------------------------------------------------------
|
|---|
| 51 | # Check data
|
|---|
| 52 | if not data:
|
|---|
| 53 | print("ERROR : no data extracted from '%s'" % args.infile, file=sys.stderr)
|
|---|
| 54 |
|
|---|
| 55 | for d in data:
|
|---|
| 56 | if not d in processors:
|
|---|
| 57 | print("WARNING : unkown processor '%s'" % d, file=sys.stderr)
|
|---|
| 58 |
|
|---|
| 59 | prevT = data[d][0].time
|
|---|
| 60 | prevO = data[d][0].on
|
|---|
| 61 | for p in data[d][1:]:
|
|---|
| 62 | if prevT > p.time:
|
|---|
| 63 | print("WARNING : Time is inconsistant for Proc '%s'" % d, file=sys.stderr)
|
|---|
| 64 |
|
|---|
| 65 | if prevO == p.on:
|
|---|
| 66 | print("WARNING : State is inconsistant for Proc '%s' at %s-%s" % (d, prevT, p.time), file=sys.stderr)
|
|---|
| 67 |
|
|---|
| 68 | prevT = p.time
|
|---|
| 69 | prevO = p.on
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | #--------------------------------------------------------------------------------
|
|---|
| 73 | # Convert data to series
|
|---|
| 74 | offset = min(data)
|
|---|
| 75 | series = dict()
|
|---|
| 76 | for d in data:
|
|---|
| 77 | series[d] = ([], [], [])
|
|---|
| 78 | for p in data[d]:
|
|---|
| 79 | series[d][0].append( p.time )
|
|---|
| 80 | series[d][1].append(p.on + d - offset)
|
|---|
| 81 | series[d][2].append(d - offset)
|
|---|
| 82 |
|
|---|
| 83 | #--------------------------------------------------------------------------------
|
|---|
| 84 | # setup matplotlib
|
|---|
| 85 | import matplotlib as mpl
|
|---|
| 86 | mpl.use('Agg')
|
|---|
| 87 | import matplotlib.pyplot as plt
|
|---|
| 88 | import matplotlib.ticker as ticker
|
|---|
| 89 | plt.style.use('dark_background')
|
|---|
| 90 |
|
|---|
| 91 | #--------------------------------------------------------------------------------
|
|---|
| 92 | # setup plot
|
|---|
| 93 | dots_per_inch = 2000
|
|---|
| 94 | height_inches = 5
|
|---|
| 95 | width_inches = 12
|
|---|
| 96 | fig = plt.figure(figsize=(width_inches, height_inches), dpi=dots_per_inch)
|
|---|
| 97 | print('number of series={}'.format(len(series)))
|
|---|
| 98 | for s, xy in series.items():
|
|---|
| 99 | if s in processors:
|
|---|
| 100 | name = '"{}" ({})'.format(processors[s].name, processors[s].address)
|
|---|
| 101 | else:
|
|---|
| 102 | name = s
|
|---|
| 103 | print( ' plotting series {} with {} points'.format(name, len(xy[0])) )
|
|---|
| 104 | plt.fill_between(xy[0], xy[1], xy[2], step="post", alpha=0.4)
|
|---|
| 105 | plt.step(xy[0], xy[1], where='post')
|
|---|
| 106 |
|
|---|
| 107 | #--------------------------------------------------------------------------------
|
|---|
| 108 | # y axis major, minor
|
|---|
| 109 | ax = plt.gca()
|
|---|
| 110 | ax.grid(which='major', axis='y', linestyle='-', color='lightgray')
|
|---|
| 111 | ax.yaxis.set_minor_locator(ticker.AutoMinorLocator())
|
|---|
| 112 | ax.grid(which='minor', axis='y', linestyle='dotted', color='gray')
|
|---|
| 113 |
|
|---|
| 114 | #--------------------------------------------------------------------------------
|
|---|
| 115 | # x axis major, minor
|
|---|
| 116 | ax.grid(which='major', axis='x', linestyle='-', color='lightgray')
|
|---|
| 117 | ax.xaxis.set_minor_locator(ticker.AutoMinorLocator())
|
|---|
| 118 | ax.grid(which='minor', axis='x', linestyle='dotted', color='gray')
|
|---|
| 119 |
|
|---|
| 120 | #--------------------------------------------------------------------------------
|
|---|
| 121 | # do the plot
|
|---|
| 122 | plt.tight_layout()
|
|---|
| 123 | if not args.outfile:
|
|---|
| 124 | plt.show()
|
|---|
| 125 | else:
|
|---|
| 126 | print("saving figure image %s\n" % args.outfile.name)
|
|---|
| 127 | plt.savefig(args.outfile.name)
|
|---|