3 # btt_plot.py: Generate matplotlib plots for BTT generate data files
5 # (C) Copyright 2009 Hewlett-Packard Development Company, L.P.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 btt_plot.py: Generate matplotlib plots for BTT generated data files
26 AQD - Average Queue Depth Running average of queue depths
28 BNOS - Block numbers accessed Markers for each block
30 Q2D - Queue to Issue latencies Running averages
31 D2C - Issue to Complete latencies Running averages
32 Q2C - Queue to Complete latencies Running averages
35 btt_plot_aqd.py equivalent to: btt_plot.py -t aqd <type>=aqd
36 btt_plot_bnos.py equivalent to: btt_plot.py -t bnos <type>=bnos
37 btt_plot_q2d.py equivalent to: btt_plot.py -t q2d <type>=q2d
38 btt_plot_d2c.py equivalent to: btt_plot.py -t d2c <type>=d2c
39 btt_plot_q2c.py equivalent to: btt_plot.py -t q2c <type>=q2c
42 [ -A | --generate-all ] Default: False
43 [ -L | --no-legend ] Default: Legend table produced
44 [ -o <file> | --output=<file> ] Default: <type>.png
45 [ -T <string> | --title=<string> ] Default: Based upon <type>
46 [ -v | --verbose ] Default: False
49 The -A (--generate-all) argument is different: when this is specified,
50 an attempt is made to generate default plots for all 5 types (aqd, bnos,
51 q2d, d2c and q2c). It will find files with the appropriate suffix for
52 each type ('aqd.dat' for example). If such files are found, a plot for
53 that type will be made. The output file name will be the default for
54 each type. The -L (--no-legend) option will be obeyed for all plots,
55 but the -o (--output) and -T (--title) options will be ignored.
58 from __future__ import absolute_import
59 from __future__ import print_function
61 from six.moves import range
62 __author__ = 'Alan D. Brunelle <alan.brunelle@hp.com>'
64 #------------------------------------------------------------------------------
68 import getopt, glob, os, sys
69 import matplotlib.pyplot as plt
71 plot_size = [10.9, 8.4] # inches...
80 types = [ 'aqd', 'q2d', 'd2c', 'q2c', 'live', 'bnos' ]
81 progs = [ 'btt_plot_%s.py' % t for t in types ]
83 get_base = lambda file: file[file.find('_')+1:file.rfind('_')]
85 #------------------------------------------------------------------------------
87 """Generate fatal error message and exit"""
89 print('FATAL: %s' % msg, file=sys.stderr)
92 #------------------------------------------------------------------------------
93 def gen_legends(ax, legends):
94 leg = ax.legend(legends, 'best', shadow=True)
95 frame = leg.get_frame()
96 frame.set_facecolor('0.80')
97 for t in leg.get_texts():
98 t.set_fontsize('xx-small')
100 #----------------------------------------------------------------------
102 """Retrieve data from files provided.
104 Returns a database containing:
105 'min_x', 'max_x' - Minimum and maximum X values found
106 'min_y', 'max_y' - Minimum and maximum Y values found
107 'x', 'y' - X & Y value arrays
108 'ax', 'ay' - Running average over X & Y --
109 if > 10 values provided...
111 #--------------------------------------------------------------
112 def check(mn, mx, v):
113 """Returns new min, max, and float value for those passed in"""
116 if mn == None or v < mn: mn = v
117 if mx == None or v > mx: mx = v
120 #--------------------------------------------------------------
122 """Computes running average for Xs and Ys"""
124 #------------------------------------------------------
126 """Computes average for array of values passed"""
128 return sum(vals) / len(vals)
130 #------------------------------------------------------
139 x_range = (xs[-1] - xs[0]) / 100
140 for idx in range(1, len(ys)):
141 if (xs[idx] - _xs[0]) > x_range:
142 axs.append(_avg(_xs))
143 ays.append(_avg(_ys))
153 axs.append(_avg(_xs))
154 ays.append(_avg(_ys))
158 #--------------------------------------------------------------
162 min_x = max_x = min_y = max_y = None
164 if not os.path.exists(file):
165 fatal('%s not found' % file)
167 print('Processing %s' % file)
171 for line in open(file, 'r'):
172 f = line.rstrip().split(None)
173 if line.find('#') == 0 or len(f) < 2:
175 (min_x, max_x, x) = check(min_x, max_x, f[0])
176 (min_y, max_y, y) = check(min_y, max_y, f[1])
180 db[file] = {'x':xs, 'y':ys}
182 db[file]['ax'], db[file]['ay'] = avg(xs, ys)
184 db[file]['ax'] = db[file]['ay'] = None
192 #----------------------------------------------------------------------
193 def parse_args(args):
194 """Parse command line arguments.
196 Returns list of (data) files that need to be processed -- /unless/
197 the -A (--generate-all) option is passed, in which case superfluous
198 data files are ignored...
201 global add_legend, output_file, title_str, type, verbose
204 prog = args[0][args[0].rfind('/')+1:]
205 if prog == 'btt_plot.py':
207 elif not prog in progs:
208 fatal('%s not a valid command name' % prog)
210 type = prog[prog.rfind('_')+1:prog.rfind('.py')]
213 l_opts = [ 'generate-all', 'type', 'no-legend', 'output', 'title',
217 (opts, args) = getopt.getopt(args[1:], s_opts, l_opts)
218 except getopt.error as msg:
219 print(msg, file=sys.stderr)
223 if o in ('-A', '--generate-all'):
225 elif o in ('-L', '--no-legend'):
227 elif o in ('-o', '--output'):
229 elif o in ('-t', '--type'):
231 fatal('Type %s not supported' % a)
233 elif o in ('-T', '--title'):
235 elif o in ('-v', '--verbose'):
238 if type == None and not generate_all:
239 fatal('Need type of data files to process - (-t <type>)')
243 #------------------------------------------------------------------------------
244 def gen_title(fig, type, title_str):
245 """Sets the title for the figure based upon the type /or/ user title"""
247 if title_str != None:
250 title_str = 'Average Queue Depth'
252 title_str = 'Block Numbers Accessed'
254 title_str = 'Queue (Q) To Issue (D) Average Latencies'
256 title_str = 'Issue (D) To Complete (C) Average Latencies'
258 title_str = 'Queue (Q) To Complete (C) Average Latencies'
260 title = fig.text(.5, .95, title_str, horizontalalignment='center')
261 title.set_fontsize('large')
263 #------------------------------------------------------------------------------
264 def gen_labels(db, ax, type):
265 """Generate X & Y 'axis'"""
267 #----------------------------------------------------------------------
268 def gen_ylabel(ax, type):
269 """Set the Y axis label based upon the type"""
272 str = 'Number of Requests Queued'
279 #----------------------------------------------------------------------
280 xdelta = 0.1 * (db['max_x'] - db['min_x'])
281 ydelta = 0.1 * (db['max_y'] - db['min_y'])
283 ax.set_xlim(db['min_x'] - xdelta, db['max_x'] + xdelta)
284 ax.set_ylim(db['min_y'] - ydelta, db['max_y'] + ydelta)
285 ax.set_xlabel('Runtime (seconds)')
289 #------------------------------------------------------------------------------
290 def generate_output(type, db):
291 """Generate the output plot based upon the type and database"""
293 #----------------------------------------------------------------------
294 def color(idx, style):
295 """Returns a color/symbol type based upon the index passed."""
297 colors = [ 'b', 'g', 'r', 'c', 'm', 'y', 'k' ]
298 l_styles = [ '-', ':', '--', '-.' ]
299 m_styles = [ 'o', '+', '.', ',', 's', 'v', 'x', '<', '>' ]
301 color = colors[idx % len(colors)]
303 style = l_styles[int((idx / len(l_styles)) % len(l_styles))]
304 elif style == 'marker':
305 style = m_styles[int((idx / len(m_styles)) % len(m_styles))]
307 return '%s%s' % (color, style)
309 #----------------------------------------------------------------------
310 global add_legend, output_file, title_str, verbose
312 if output_file != None:
315 ofile = '%s.png' % type
318 print('Generating plot into %s' % ofile)
320 fig = plt.figure(figsize=plot_size)
321 ax = fig.add_subplot(111)
323 gen_title(fig, type, title_str)
324 gen_labels(db, ax, type)
333 for file in six.iterkeys(db):
334 if not file in ['min_x', 'max_x', 'min_y', 'max_y']:
341 ax.plot(dat['x'], dat['y'], color(idx, 'marker'),
343 elif dat['ax'] == None:
344 continue # Don't add legend
346 ax.plot(dat['ax'], dat['ay'], color(idx, 'line'),
349 legends.append(get_base(file))
352 if add_legend and len(legends) > 0:
353 gen_legends(ax, legends)
356 #------------------------------------------------------------------------------
358 """Returns the list of files for the -A option based upon type"""
362 for fn in glob.glob('*c.dat'):
363 for t in [ 'q2q', 'd2d', 'q2c', 'd2c' ]:
369 files = glob.glob('*%s.dat' % type)
372 #------------------------------------------------------------------------------
375 base = get_base(file)
376 title_str = 'Block Numbers Accessed: %s' % base
377 output_file = 'bnos_%s.png' % base
378 generate_output(t, get_data([file]))
380 #------------------------------------------------------------------------------
384 #----------------------------------------------------------------------
385 def get_live_data(fn):
388 for line in open(fn, 'r'):
389 f = line.rstrip().split()
390 if f[0] != '#' and len(f) == 2:
391 xs.append(float(f[0]))
392 ys.append(float(f[1]))
395 #----------------------------------------------------------------------
397 if a[0] == 'sys' and b[0] == 'sys':
399 elif a[0] == 'sys' or a[2][0] < b[2][0]:
401 elif b[0] == 'sys' or a[2][0] > b[2][0]:
406 #----------------------------------------------------------------------
407 def turn_off_ticks(ax):
408 for tick in ax.xaxis.get_major_ticks():
409 tick.tick1On = tick.tick2On = False
410 for tick in ax.yaxis.get_major_ticks():
411 tick.tick1On = tick.tick2On = False
412 for tick in ax.xaxis.get_minor_ticks():
413 tick.tick1On = tick.tick2On = False
414 for tick in ax.yaxis.get_minor_ticks():
415 tick.tick1On = tick.tick2On = False
417 #----------------------------------------------------------------------
418 fig = plt.figure(figsize=plot_size)
419 ax = fig.add_subplot(111)
423 if not os.path.exists(fn):
425 (xs, ys) = get_live_data(fn)
426 db.append([fn[:fn.find('_live.dat')], xs, ys])
430 ax.plot(rec[1], rec[2])
432 gen_title(fig, 'live', 'Active I/O Per Device')
433 ax.set_xlabel('Runtime (seconds)')
434 ax.set_ylabel('Device')
437 ax.set_xlim(-0.1, db[0][1][-1]+1)
438 ax.set_yticks([idx for idx in range(0, len(db))])
439 ax.yaxis.set_ticklabels([rec[0] for rec in db])
442 plt.savefig('live.png')
443 plt.savefig('live.eps')
445 #------------------------------------------------------------------------------
446 if __name__ == '__main__':
447 files = parse_args(sys.argv)
450 output_file = title_str = type = None
460 generate_output(t, get_data(files))
464 fatal('Need data files to process')
466 generate_output(type, get_data(files))