ef32ee0b156dac047cef5ee0cd71fd60214b35fa
[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                 temp_outfile.append(open(tmp_filename,'w'))
95                 disk_perf.append([])
96
97         shall_break = False
98         while True:
99                 current_line=[]
100                 for file in files:
101                         s=file.readline().replace(',',' ').split()
102                         if not s:
103                                 shall_break=True
104                                 break;
105                         current_line.append(s);
106
107                 if shall_break == True:
108                         break
109
110                 last_time = -1
111                 index=0
112                 perfs=[]
113                 for line in current_line:
114                         time, perf, x, block_size = line
115                         if (blk_size == 0):
116                                 blk_size=int(block_size)
117
118                         # We ignore the first 500msec as it doesn't seems to be part of the real benchmark
119                         # Time < 500 usually reports BW=0 breaking the min computing
120                         if ((int(time)) > 500):
121                                 disk_perf[index].append(int(perf))
122                                 perfs.append(perf)
123                                 index = index + 1
124
125                 # If we reach this point, it means that all the traces are coherent
126                 for p in enumerate(perfs):
127                         temp_outfile[p[0]].write("%s %.2f %s\n" % (p[0], float(float(time)/1000), p[1]))
128
129         for file in files:
130                 file.close()
131         for file in temp_outfile:
132                 file.close()
133         return blk_size
134
135 def compute_math(fio_data_file, title,gnuplot_output_filename,mode,disk_perf):
136         global_min=[]
137         global_max=[]
138         average_file=open(gnuplot_output_filename+'.average', 'w')
139         min_file=open(gnuplot_output_filename+'.min', 'w')
140         max_file=open(gnuplot_output_filename+'.max', 'w')
141         stddev_file=open(gnuplot_output_filename+'.stddev', 'w')
142         global_file=open(gnuplot_output_filename+'.global','w')
143
144         min_file.write('DiskName %s\n' % mode)
145         max_file.write('DiskName %s\n'% mode)
146         average_file.write('DiskName %s\n'% mode)
147         stddev_file.write('DiskName %s\n'% mode )
148         for disk in xrange(len(fio_data_file)):
149 #               print disk_perf[disk]
150                 min_file.write("# Disk%d was coming from %s\n" % (disk,fio_data_file[disk]))
151                 max_file.write("# Disk%d was coming from %s\n" % (disk,fio_data_file[disk]))
152                 average_file.write("# Disk%d was coming from %s\n" % (disk,fio_data_file[disk]))
153                 stddev_file.write("# Disk%d was coming from %s\n" % (disk,fio_data_file[disk]))
154                 avg  = average(disk_perf[disk])
155                 variance = map(lambda x: (x - avg)**2, disk_perf[disk])
156                 standard_deviation = math.sqrt(average(variance))
157 #               print "Disk%d [ min=%.2f max=%.2f avg=%.2f stddev=%.2f \n" % (disk,min(disk_perf[disk]),max(disk_perf[disk]),avg, standard_deviation)
158                 average_file.write('%d %d\n' % (disk, avg))
159                 stddev_file.write('%d %d\n' % (disk, standard_deviation))
160                 local_min=min(disk_perf[disk])
161                 local_max=max(disk_perf[disk])
162                 min_file.write('%d %d\n' % (disk, local_min))
163                 max_file.write('%d %d\n' % (disk, local_max))
164                 global_min.append(int(local_min))
165                 global_max.append(int(local_max))
166
167         global_disk_perf = sum(disk_perf, [])
168         avg  = average(global_disk_perf)
169         variance = map(lambda x: (x - avg)**2, global_disk_perf)
170         standard_deviation = math.sqrt(average(variance))
171
172         global_file.write('min=%.2f\n' % min(global_disk_perf))
173         global_file.write('max=%.2f\n' % max(global_disk_perf))
174         global_file.write('avg=%.2f\n' % avg)
175         global_file.write('stddev=%.2f\n' % standard_deviation)
176         global_file.write('values_count=%d\n' % len(global_disk_perf))
177         global_file.write('disks_count=%d\n' % len(fio_data_file))
178         #print "Global [ min=%.2f max=%.2f avg=%.2f stddev=%.2f \n" % (min(global_disk_perf),max(global_disk_perf),avg, standard_deviation)
179
180         average_file.close()
181         min_file.close()
182         max_file.close()
183         stddev_file.close()
184         global_file.close()
185         try:
186                 os.remove('mymath')
187         except:
188                 True
189
190         generate_gnuplot_math_script("Average values of "+title,gnuplot_output_filename+'.average',mode,int(avg))
191         generate_gnuplot_math_script("Min values of "+title,gnuplot_output_filename+'.min',mode,average(global_min))
192         generate_gnuplot_math_script("Max values of "+title,gnuplot_output_filename+'.max',mode,average(global_max))
193         generate_gnuplot_math_script("Standard Deviation of "+title,gnuplot_output_filename+'.stddev',mode,int(standard_deviation))
194
195 def parse_global_files(fio_data_file, global_search):
196         max_result=0
197         max_file=''
198         for file in fio_data_file:
199                 f=open(file)
200                 disk_count=0
201                 search_value=-1
202
203                 # Let's read the complete file
204                 while True:
205                         try:
206                                 # We do split the name from the value
207                                 name,value=f.readline().split("=")
208                         except:
209                                 f.close()
210                                 break
211                         # If we ended the file
212                         if not name:
213                                 # Let's process what we have
214                                 f.close()
215                                 break
216                         else:
217                                 # disks_count is not global_search item
218                                 # As we need it for some computation, let's save it
219                                 if name=="disks_count":
220                                         disks_count=int(value)
221
222                                 # Let's catch the searched item
223                                 if global_search in name:
224                                         search_value=float(value)
225
226                 # Let's process the avg value by estimated the global bandwidth per file
227                 # We keep the biggest in memory for reporting
228                 if global_search == "avg":
229                         if (disks_count > 0) and (search_value != -1):
230                                 result=disks_count*search_value
231                                 if (result > max_result):
232                                         max_result=result
233                                         max_file=file
234         # Let's print the avg output
235         if global_search == "avg":
236                 print "Biggest aggregated value of %s was %2.f in file %s\n" % (global_search, max_result, max_file)
237         else:
238                 print "Global search %s is not yet implemented\n" % global_search
239
240 def render_gnuplot():
241         print "Running gnuplot Rendering\n"
242         try:
243                 os.system("gnuplot mymath")
244                 os.system("gnuplot mygraph")
245         except:
246                 print "Could not run gnuplot on mymath or mygraph !\n"
247                 sys.exit(1);
248
249 def print_help():
250     print 'fio2gnuplot.py -ghbio -t <title> -o <outputfile> -p <pattern>'
251     print
252     print '-h --help                           : Print this help'
253     print '-p <pattern> or --pattern <pattern> : A pattern in regexp to select fio input files'
254     print '-b           or --bandwidth         : A predefined pattern for selecting *_bw.log files'
255     print '-i           or --iops              : A predefined pattern for selecting *_iops.log files'
256     print '-g           or --gnuplot           : Render gnuplot traces before exiting'
257     print '-o           or --outputfile <file> : The basename for gnuplot traces'
258     print '                                       - Basename is set with the pattern if defined'
259     print '-t           or --title <title>     : The title of the gnuplot traces'
260     print '                                       - Title is set with the block size detected in fio traces'
261     print '-G           or --Global <type>     : Search for <type> in .global files match by a pattern'
262     print '                                       - Available types are : min, max, avg, stddev'
263     print '                                       - The .global extension is added automatically to the pattern'
264
265 def main(argv):
266     mode='unknown'
267     pattern=''
268     pattern_set_by_user=False
269     title='No title'
270     gnuplot_output_filename='result'
271     disk_perf=[]
272     run_gnuplot=False
273     parse_global=False
274     global_search=''
275
276     try:
277             opts, args = getopt.getopt(argv[1:],"ghbio:t:p:G:")
278     except getopt.GetoptError:
279          print_help()
280          sys.exit(2)
281
282     for opt, arg in opts:
283       if opt in ("-b", "--bandwidth"):
284          pattern='*_bw.log'
285       elif opt in ("-i", "--iops"):
286          pattern='*_iops.log'
287       elif opt in ("-p", "--pattern"):
288          pattern_set_by_user=True
289          pattern=arg
290          pattern=pattern.replace('\\','')
291       elif opt in ("-o", "--outputfile"):
292          gnuplot_output_filename=arg
293       elif opt in ("-t", "--title"):
294          title=arg
295       elif opt in ("-g", "--gnuplot"):
296          run_gnuplot=True
297       elif opt in ("-G", "--Global"):
298          parse_global=True
299          global_search=arg
300       elif opt in ("-h", "--help"):
301           print_help()
302           sys.exit(1)
303
304     # Adding .global extension to the file
305     if parse_global==True:
306             if not gnuplot_output_filename.endswith('.global'):
307                 pattern = pattern+'.global'
308
309     fio_data_file=find_file('.',pattern)
310     if len(fio_data_file) == 0:
311             print "No log file found with pattern %s!" % pattern
312             sys.exit(1)
313
314     fio_data_file=sorted(fio_data_file, key=str.lower)
315     for file in fio_data_file:
316         print 'Selected %s' % file
317         if "_bw.log" in file :
318                 mode="Bandwidth (KB/sec)"
319         if "_iops.log" in file :
320                 mode="IO per Seconds (IO/sec)"
321     if (title == 'No title') and (mode != 'unknown'):
322             if "Bandwidth" in mode:
323                     title='Bandwidth benchmark with %d fio results' % len(fio_data_file)
324             if "IO" in mode:
325                     title='IO benchmark with %d fio results' % len(fio_data_file)
326
327     #We need to adjust the output filename regarding the pattern required by the user
328     if (pattern_set_by_user == True):
329             gnuplot_output_filename=pattern
330             # As we do have some regexp in the pattern, let's make this simpliest
331             # We do remove the simpliest parts of the expression to get a clear file name
332             gnuplot_output_filename=gnuplot_output_filename.replace('-*-','-')
333             gnuplot_output_filename=gnuplot_output_filename.replace('*','-')
334             gnuplot_output_filename=gnuplot_output_filename.replace('--','-')
335             gnuplot_output_filename=gnuplot_output_filename.replace('.log','')
336             # Insure that we don't have any starting or trailing dash to the filename
337             gnuplot_output_filename = gnuplot_output_filename[:-1] if gnuplot_output_filename.endswith('-') else gnuplot_output_filename
338             gnuplot_output_filename = gnuplot_output_filename[1:] if gnuplot_output_filename.startswith('-') else gnuplot_output_filename
339
340     if parse_global==True:
341         parse_global_files(fio_data_file, global_search)
342     else:
343         blk_size=compute_temp_file(fio_data_file,disk_perf)
344         title="%s @ Blocksize = %dK" % (title,blk_size/1024)
345         compute_aggregated_file(fio_data_file, gnuplot_output_filename)
346         compute_math(fio_data_file,title,gnuplot_output_filename,mode,disk_perf)
347         generate_gnuplot_script(fio_data_file,title,gnuplot_output_filename,mode,disk_perf)
348
349         if (run_gnuplot==True):
350                 render_gnuplot()
351
352     # Cleaning temporary files
353     try:
354         os.remove('gnuplot_temp_file.*')
355     except:
356         True
357
358 #Main
359 if __name__ == "__main__":
360     sys.exit(main(sys.argv))