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