fio2gnuplot: Don't plot fake data
[fio.git] / tools / plot / fio2gnuplot.py
1 #!/usr/bin/python
2 #
3 #  Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
4 #  Author: Erwan Velu  <erwan@enovance.com>
5 #
6 #  The license below covers all files distributed with fio unless otherwise
7 #  noted in the file itself.
8 #
9 #  This program is free software; you can redistribute it and/or modify
10 #  it under the terms of the GNU General Public License version 2 as
11 #  published by the Free Software Foundation.
12 #
13 #  This program is distributed in the hope that it will be useful,
14 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #  GNU General Public License for more details.
17 #
18 #  You should have received a copy of the GNU General Public License
19 #  along with this program; if not, write to the Free Software
20 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22 import os
23 import fnmatch
24 import sys
25 import getopt
26 import re
27 import math
28
29 def find_file(path, pattern):
30         fio_data_file=[]
31         # For all the local files
32         for file in os.listdir(path):
33             # If the file math the regexp
34             if fnmatch.fnmatch(file, pattern):
35                 # Let's consider this file
36                 fio_data_file.append(file)
37
38         return fio_data_file
39
40 def generate_gnuplot_script(fio_data_file,title,gnuplot_output_filename,mode,disk_perf):
41         f=open("mygraph",'w')
42         if len(fio_data_file) > 1:
43                 f.write("call \'graph3D.gpm\' \'%s' \'%s\' \'\' \'%s\' \'%s\'\n" % (title,gnuplot_output_filename,gnuplot_output_filename,mode))
44
45         pos=0
46         # Let's create a temporary file for each selected fio file
47         for file in fio_data_file:
48                 tmp_filename = "gnuplot_temp_file.%d" % pos
49                 png_file=file.replace('.log','')
50                 raw_filename = "%s-2Draw" % (png_file)
51                 smooth_filename = "%s-2Dsmooth" % (png_file)
52                 trend_filename = "%s-2Dtrend" % (png_file)
53                 avg  = average(disk_perf[pos])
54                 f.write("call \'graph2D.gpm\' \'%s' \'%s\' \'\' \'%s\' \'%s\' \'%s\' \'%s\' \'%f\'\n" % (title,tmp_filename,raw_filename,mode,smooth_filename,trend_filename,avg))
55                 pos = pos +1
56
57         f.close()
58
59 def generate_gnuplot_math_script(title,gnuplot_output_filename,mode,average):
60         f=open("mymath",'a')
61         f.write("call \'math.gpm\' \'%s' \'%s\' \'\' \'%s\' \'%s\' %s\n" % (title,gnuplot_output_filename,gnuplot_output_filename,mode,average))
62         f.close()
63
64 def compute_aggregated_file(fio_data_file, gnuplot_output_filename):
65         temp_files=[]
66         pos=0
67         # Let's create a temporary file for each selected fio file
68         for file in fio_data_file:
69                 tmp_filename = "gnuplot_temp_file.%d" % pos
70                 temp_files.append(open(tmp_filename,'r'))
71                 pos = pos +1
72
73         f = open(gnuplot_output_filename, "w")
74         index=0
75         # Let's add some information
76         for tempfile in temp_files:
77                     f.write("# Disk%d was coming from %s\n" % (index,fio_data_file[index]))
78                     f.write(tempfile.read())
79                     f.write("\n")
80                     tempfile.close()
81                     index = index + 1
82         f.close()
83
84 def average(s): return sum(s) * 1.0 / len(s)
85
86 def compute_temp_file(fio_data_file,disk_perf):
87         files=[]
88         temp_outfile=[]
89         blk_size=0
90         for file in fio_data_file:
91                 files.append(open(file))
92                 pos = len(files) - 1
93                 tmp_filename = "gnuplot_temp_file.%d" % pos
94                 gnuplot_file=open(tmp_filename,'w')
95                 temp_outfile.append(gnuplot_file)
96                 gnuplot_file.write("#Temporary file based on file %s\n" % file)
97                 disk_perf.append([])
98
99         shall_break = False
100         while True:
101                 current_line=[]
102                 nb_empty_files=0
103                 nb_files=len(files)
104                 for file in files:
105                         s=file.readline().replace(',',' ').split()
106                         if not s:
107                                 nb_empty_files+=1
108                                 s="-1, 0, 0, 0'".replace(',',' ').split()
109
110                         if (nb_empty_files == nb_files):
111                                 shall_break=True
112                                 break;
113
114                         current_line.append(s);
115
116                 if shall_break == True:
117                         break
118
119                 last_time = -1
120                 index=0
121                 perfs=[]
122                 for line in current_line:
123                         time, perf, x, block_size = line
124                         if (blk_size == 0):
125                                 blk_size=int(block_size)
126
127                         # We ignore the first 500msec as it doesn't seems to be part of the real benchmark
128                         # Time < 500 usually reports BW=0 breaking the min computing
129                         if (((int(time)) > 500) or (int(time)==-1)):
130                                 disk_perf[index].append(int(perf))
131                                 perfs.append("%s %s"% (time, perf))
132                                 index = index + 1
133
134                 # If we reach this point, it means that all the traces are coherent
135                 for p in enumerate(perfs):
136                         perf_time,perf = p[1].split()
137                         if (perf_time != "-1"):
138                                 temp_outfile[p[0]].write("%s %.2f %s\n" % (p[0], float(float(perf_time)/1000), perf))
139
140
141         for file in files:
142                 file.close()
143         for file in temp_outfile:
144                 file.close()
145         return blk_size
146
147 def compute_math(fio_data_file, title,gnuplot_output_filename,mode,disk_perf):
148         global_min=[]
149         global_max=[]
150         average_file=open(gnuplot_output_filename+'.average', 'w')
151         min_file=open(gnuplot_output_filename+'.min', 'w')
152         max_file=open(gnuplot_output_filename+'.max', 'w')
153         stddev_file=open(gnuplot_output_filename+'.stddev', 'w')
154         global_file=open(gnuplot_output_filename+'.global','w')
155
156         min_file.write('DiskName %s\n' % mode)
157         max_file.write('DiskName %s\n'% mode)
158         average_file.write('DiskName %s\n'% mode)
159         stddev_file.write('DiskName %s\n'% mode )
160         for disk in xrange(len(fio_data_file)):
161 #               print disk_perf[disk]
162                 min_file.write("# Disk%d was coming from %s\n" % (disk,fio_data_file[disk]))
163                 max_file.write("# Disk%d was coming from %s\n" % (disk,fio_data_file[disk]))
164                 average_file.write("# Disk%d was coming from %s\n" % (disk,fio_data_file[disk]))
165                 stddev_file.write("# Disk%d was coming from %s\n" % (disk,fio_data_file[disk]))
166                 avg  = average(disk_perf[disk])
167                 variance = map(lambda x: (x - avg)**2, disk_perf[disk])
168                 standard_deviation = math.sqrt(average(variance))
169 #               print "Disk%d [ min=%.2f max=%.2f avg=%.2f stddev=%.2f \n" % (disk,min(disk_perf[disk]),max(disk_perf[disk]),avg, standard_deviation)
170                 average_file.write('%d %d\n' % (disk, avg))
171                 stddev_file.write('%d %d\n' % (disk, standard_deviation))
172                 local_min=min(disk_perf[disk])
173                 local_max=max(disk_perf[disk])
174                 min_file.write('%d %d\n' % (disk, local_min))
175                 max_file.write('%d %d\n' % (disk, local_max))
176                 global_min.append(int(local_min))
177                 global_max.append(int(local_max))
178
179         global_disk_perf = sum(disk_perf, [])
180         avg  = average(global_disk_perf)
181         variance = map(lambda x: (x - avg)**2, global_disk_perf)
182         standard_deviation = math.sqrt(average(variance))
183
184         global_file.write('min=%.2f\n' % min(global_disk_perf))
185         global_file.write('max=%.2f\n' % max(global_disk_perf))
186         global_file.write('avg=%.2f\n' % avg)
187         global_file.write('stddev=%.2f\n' % standard_deviation)
188         global_file.write('values_count=%d\n' % len(global_disk_perf))
189         global_file.write('disks_count=%d\n' % len(fio_data_file))
190         #print "Global [ min=%.2f max=%.2f avg=%.2f stddev=%.2f \n" % (min(global_disk_perf),max(global_disk_perf),avg, standard_deviation)
191
192         average_file.close()
193         min_file.close()
194         max_file.close()
195         stddev_file.close()
196         global_file.close()
197         try:
198                 os.remove('mymath')
199         except:
200                 True
201
202         generate_gnuplot_math_script("Average values of "+title,gnuplot_output_filename+'.average',mode,int(avg))
203         generate_gnuplot_math_script("Min values of "+title,gnuplot_output_filename+'.min',mode,average(global_min))
204         generate_gnuplot_math_script("Max values of "+title,gnuplot_output_filename+'.max',mode,average(global_max))
205         generate_gnuplot_math_script("Standard Deviation of "+title,gnuplot_output_filename+'.stddev',mode,int(standard_deviation))
206
207 def parse_global_files(fio_data_file, global_search):
208         max_result=0
209         max_file=''
210         for file in fio_data_file:
211                 f=open(file)
212                 disk_count=0
213                 search_value=-1
214
215                 # Let's read the complete file
216                 while True:
217                         try:
218                                 # We do split the name from the value
219                                 name,value=f.readline().split("=")
220                         except:
221                                 f.close()
222                                 break
223                         # If we ended the file
224                         if not name:
225                                 # Let's process what we have
226                                 f.close()
227                                 break
228                         else:
229                                 # disks_count is not global_search item
230                                 # As we need it for some computation, let's save it
231                                 if name=="disks_count":
232                                         disks_count=int(value)
233
234                                 # Let's catch the searched item
235                                 if global_search in name:
236                                         search_value=float(value)
237
238                 # Let's process the avg value by estimated the global bandwidth per file
239                 # We keep the biggest in memory for reporting
240                 if global_search == "avg":
241                         if (disks_count > 0) and (search_value != -1):
242                                 result=disks_count*search_value
243                                 if (result > max_result):
244                                         max_result=result
245                                         max_file=file
246         # Let's print the avg output
247         if global_search == "avg":
248                 print "Biggest aggregated value of %s was %2.f in file %s\n" % (global_search, max_result, max_file)
249         else:
250                 print "Global search %s is not yet implemented\n" % global_search
251
252 def render_gnuplot():
253         print "Running gnuplot Rendering\n"
254         try:
255                 os.system("gnuplot mymath")
256                 os.system("gnuplot mygraph")
257         except:
258                 print "Could not run gnuplot on mymath or mygraph !\n"
259                 sys.exit(1);
260
261 def print_help():
262     print 'fio2gnuplot.py -ghbio -t <title> -o <outputfile> -p <pattern>'
263     print
264     print '-h --help                           : Print this help'
265     print '-p <pattern> or --pattern <pattern> : A pattern in regexp to select fio input files'
266     print '-b           or --bandwidth         : A predefined pattern for selecting *_bw.log files'
267     print '-i           or --iops              : A predefined pattern for selecting *_iops.log files'
268     print '-g           or --gnuplot           : Render gnuplot traces before exiting'
269     print '-o           or --outputfile <file> : The basename for gnuplot traces'
270     print '                                       - Basename is set with the pattern if defined'
271     print '-t           or --title <title>     : The title of the gnuplot traces'
272     print '                                       - Title is set with the block size detected in fio traces'
273     print '-G           or --Global <type>     : Search for <type> in .global files match by a pattern'
274     print '                                       - Available types are : min, max, avg, stddev'
275     print '                                       - The .global extension is added automatically to the pattern'
276
277 def main(argv):
278     mode='unknown'
279     pattern=''
280     pattern_set_by_user=False
281     title='No title'
282     gnuplot_output_filename='result'
283     disk_perf=[]
284     run_gnuplot=False
285     parse_global=False
286     global_search=''
287
288     try:
289             opts, args = getopt.getopt(argv[1:],"ghbio:t:p:G:")
290     except getopt.GetoptError:
291          print_help()
292          sys.exit(2)
293
294     for opt, arg in opts:
295       if opt in ("-b", "--bandwidth"):
296          pattern='*_bw.log'
297       elif opt in ("-i", "--iops"):
298          pattern='*_iops.log'
299       elif opt in ("-p", "--pattern"):
300          pattern_set_by_user=True
301          pattern=arg
302          pattern=pattern.replace('\\','')
303       elif opt in ("-o", "--outputfile"):
304          gnuplot_output_filename=arg
305       elif opt in ("-t", "--title"):
306          title=arg
307       elif opt in ("-g", "--gnuplot"):
308          run_gnuplot=True
309       elif opt in ("-G", "--Global"):
310          parse_global=True
311          global_search=arg
312       elif opt in ("-h", "--help"):
313           print_help()
314           sys.exit(1)
315
316     # Adding .global extension to the file
317     if parse_global==True:
318             if not gnuplot_output_filename.endswith('.global'):
319                 pattern = pattern+'.global'
320
321     fio_data_file=find_file('.',pattern)
322     if len(fio_data_file) == 0:
323             print "No log file found with pattern %s!" % pattern
324             sys.exit(1)
325
326     fio_data_file=sorted(fio_data_file, key=str.lower)
327     for file in fio_data_file:
328         print 'Selected %s' % file
329         if "_bw.log" in file :
330                 mode="Bandwidth (KB/sec)"
331         if "_iops.log" in file :
332                 mode="IO per Seconds (IO/sec)"
333     if (title == 'No title') and (mode != 'unknown'):
334             if "Bandwidth" in mode:
335                     title='Bandwidth benchmark with %d fio results' % len(fio_data_file)
336             if "IO" in mode:
337                     title='IO benchmark with %d fio results' % len(fio_data_file)
338
339     #We need to adjust the output filename regarding the pattern required by the user
340     if (pattern_set_by_user == True):
341             gnuplot_output_filename=pattern
342             # As we do have some regexp in the pattern, let's make this simpliest
343             # We do remove the simpliest parts of the expression to get a clear file name
344             gnuplot_output_filename=gnuplot_output_filename.replace('-*-','-')
345             gnuplot_output_filename=gnuplot_output_filename.replace('*','-')
346             gnuplot_output_filename=gnuplot_output_filename.replace('--','-')
347             gnuplot_output_filename=gnuplot_output_filename.replace('.log','')
348             # Insure that we don't have any starting or trailing dash to the filename
349             gnuplot_output_filename = gnuplot_output_filename[:-1] if gnuplot_output_filename.endswith('-') else gnuplot_output_filename
350             gnuplot_output_filename = gnuplot_output_filename[1:] if gnuplot_output_filename.startswith('-') else gnuplot_output_filename
351
352     if parse_global==True:
353         parse_global_files(fio_data_file, global_search)
354     else:
355         blk_size=compute_temp_file(fio_data_file,disk_perf)
356         title="%s @ Blocksize = %dK" % (title,blk_size/1024)
357         compute_aggregated_file(fio_data_file, gnuplot_output_filename)
358         compute_math(fio_data_file,title,gnuplot_output_filename,mode,disk_perf)
359         generate_gnuplot_script(fio_data_file,title,gnuplot_output_filename,mode,disk_perf)
360
361         if (run_gnuplot==True):
362                 render_gnuplot()
363
364     # Cleaning temporary files
365     try:
366         os.remove('gnuplot_temp_file.*')
367     except:
368         True
369
370 #Main
371 if __name__ == "__main__":
372     sys.exit(main(sys.argv))