fio2gnuplot: Fixing typo when inserting fake data
[fio.git] / tools / plot / fio2gnuplot.py
index c1a5cfb0a1d94ed6a5016abb51af95a571a14bd1..4be011d2349fab151fd68581f6410fc53d30108b 100755 (executable)
@@ -25,6 +25,7 @@ import sys
 import getopt
 import re
 import math
+import shutil
 
 def find_file(path, pattern):
        fio_data_file=[]
@@ -37,39 +38,95 @@ def find_file(path, pattern):
 
        return fio_data_file
 
-def generate_gnuplot_script(fio_data_file,title,gnuplot_output_filename,mode,disk_perf):
-       f=open("mygraph",'w')
-        f.write("call \'graph3D.gpm\' \'%s' \'%s\' \'\' \'%s\' \'%s\'\n" % (title,gnuplot_output_filename,gnuplot_output_filename,mode))
+def generate_gnuplot_script(fio_data_file,title,gnuplot_output_filename,gnuplot_output_dir,mode,disk_perf,gpm_dir):
+       print "Generating rendering scripts"
+       filename=gnuplot_output_dir+'mygraph'
+       f=open(filename,'w')
+
+       # Plotting 3D or comparing graphs doesn't have a meaning unless if there is at least 2 traces
+       if len(fio_data_file) > 1:
+               f.write("call \'%s/graph3D.gpm\' \'%s' \'%s\' \'\' \'%s\' \'%s\'\n" % (gpm_dir,title,gnuplot_output_filename,gnuplot_output_filename,mode))
+
+               # Setting up the compare files that will be plot later
+               compare=open(gnuplot_output_dir + 'compare.gnuplot','w')
+               compare.write('''
+set title '%s'
+set terminal png size 1280,1024
+set ytics axis out auto
+set key top left reverse
+set xlabel "Time (Seconds)"
+set ylabel '%s'
+set xrange [0:]
+set yrange [0:]
+'''% (title,mode))
+               compare.close()
+               #Copying the common file for all kind of graph (raw/smooth/trend)
+               compare_raw_filename="compare-%s-2Draw" % (gnuplot_output_filename)
+               compare_smooth_filename="compare-%s-2Dsmooth" % (gnuplot_output_filename)
+               compare_trend_filename="compare-%s-2Dtrend" % (gnuplot_output_filename)
+               shutil.copy(gnuplot_output_dir+'compare.gnuplot',gnuplot_output_dir+compare_raw_filename+".gnuplot")
+               shutil.copy(gnuplot_output_dir+'compare.gnuplot',gnuplot_output_dir+compare_smooth_filename+".gnuplot")
+               shutil.copy(gnuplot_output_dir+'compare.gnuplot',gnuplot_output_dir+compare_trend_filename+".gnuplot")
+
+               #Setting up a different output filename for each kind of graph
+               compare_raw=open(gnuplot_output_dir+compare_raw_filename + ".gnuplot",'a')
+               compare_raw.write("set output '%s.png'\n" % compare_raw_filename)
+               compare_smooth=open(gnuplot_output_dir+compare_smooth_filename+".gnuplot",'a')
+               compare_smooth.write("set output '%s.png'\n" % compare_smooth_filename)
+               compare_trend=open(gnuplot_output_dir+compare_trend_filename+".gnuplot",'a')
+               compare_trend.write("set output '%s.png'\n" % compare_trend_filename)
 
         pos=0
         # Let's create a temporary file for each selected fio file
         for file in fio_data_file:
                 tmp_filename = "gnuplot_temp_file.%d" % pos
-                png_file=file.replace('.log','')
+
+               # Plotting comparing graphs doesn't have a meaning unless if there is at least 2 traces
+               if len(fio_data_file) > 1:
+                       # Adding the plot instruction for each kind of comparing graphs
+                       if pos ==0 :
+                               compare_raw.write("plot '%s' using 2:3 with linespoints title '%s'" % (tmp_filename,fio_data_file[pos]))
+                               compare_smooth.write("plot '%s' using 2:3 smooth csplines title '%s'" % (tmp_filename,fio_data_file[pos]))
+                               compare_trend.write("plot '%s' using 2:3 smooth bezier title '%s'" % (tmp_filename,fio_data_file[pos]))
+                       else:
+                               compare_raw.write(",\\\n'%s' using 2:3 with linespoints title '%s'" % (tmp_filename,fio_data_file[pos]))
+                               compare_smooth.write(",\\\n'%s' using 2:3 smooth csplines title '%s'" % (tmp_filename,fio_data_file[pos]))
+                               compare_trend.write(",\\\n'%s' using 2:3 smooth bezier title '%s'" % (tmp_filename,fio_data_file[pos]))
+
+               png_file=file.replace('.log','')
                 raw_filename = "%s-2Draw" % (png_file)
                 smooth_filename = "%s-2Dsmooth" % (png_file)
                 trend_filename = "%s-2Dtrend" % (png_file)
                 avg  = average(disk_perf[pos])
-                f.write("call \'graph2D.gpm\' \'%s' \'%s\' \'\' \'%s\' \'%s\' \'%s\' \'%s\' \'%f\'\n" % (title,tmp_filename,raw_filename,mode,smooth_filename,trend_filename,avg))
+                f.write("call \'%s/graph2D.gpm\' \'%s' \'%s\' \'%s\' \'%s\' \'%s\' \'%s\' \'%s\' \'%f\'\n" % (gpm_dir,title,tmp_filename,fio_data_file[pos],raw_filename,mode,smooth_filename,trend_filename,avg))
                 pos = pos +1
 
+       # Plotting comparing graphs doesn't have a meaning unless if there is at least 2 traces
+       if len(fio_data_file) > 1:
+               os.remove(gnuplot_output_dir+"compare.gnuplot")
+               compare_raw.close()
+               compare_smooth.close()
+               compare_trend.close()
        f.close()
 
-def generate_gnuplot_math_script(title,gnuplot_output_filename,mode,average):
-       f=open("mymath",'a')
-        f.write("call \'math.gpm\' \'%s' \'%s\' \'\' \'%s\' \'%s\' %s\n" % (title,gnuplot_output_filename,gnuplot_output_filename,mode,average))
+def generate_gnuplot_math_script(title,gnuplot_output_filename,mode,average,gnuplot_output_dir,gpm_dir):
+       filename=gnuplot_output_dir+'mymath';
+       f=open(filename,'a')
+        f.write("call \'%s/math.gpm\' \'%s' \'%s\' \'\' \'%s\' \'%s\' %s\n" % (gpm_dir,title,gnuplot_output_filename,gnuplot_output_filename,mode,average))
        f.close()
 
-def compute_aggregated_file(fio_data_file, gnuplot_output_filename):
+def compute_aggregated_file(fio_data_file, gnuplot_output_filename, gnuplot_output_dir):
+       print "Processing data file 2/2"
        temp_files=[]
        pos=0
+
        # Let's create a temporary file for each selected fio file
        for file in fio_data_file:
-               tmp_filename = "gnuplot_temp_file.%d" % pos
+               tmp_filename = "%sgnuplot_temp_file.%d" % (gnuplot_output_dir, pos)
                temp_files.append(open(tmp_filename,'r'))
                pos = pos +1
 
-       f = open(gnuplot_output_filename, "w")
+       f = open(gnuplot_output_dir+gnuplot_output_filename, "w")
        index=0
        # Let's add some information
        for tempfile in temp_files:
@@ -82,25 +139,35 @@ def compute_aggregated_file(fio_data_file, gnuplot_output_filename):
 
 def average(s): return sum(s) * 1.0 / len(s)
 
-def compute_temp_file(fio_data_file,disk_perf):
+def compute_temp_file(fio_data_file,disk_perf,gnuplot_output_dir):
+       print "Processing data file 1/2"
        files=[]
        temp_outfile=[]
        blk_size=0
        for file in fio_data_file:
                files.append(open(file))
                pos = len(files) - 1
-               tmp_filename = "gnuplot_temp_file.%d" % pos
-               temp_outfile.append(open(tmp_filename,'w'))
+               tmp_filename = "%sgnuplot_temp_file.%d" % (gnuplot_output_dir,pos)
+               gnuplot_file=open(tmp_filename,'w')
+               temp_outfile.append(gnuplot_file)
+               gnuplot_file.write("#Temporary file based on file %s\n" % file)
                disk_perf.append([])
 
        shall_break = False
        while True:
                current_line=[]
+               nb_empty_files=0
+               nb_files=len(files)
                for file in files:
                        s=file.readline().replace(',',' ').split()
                        if not s:
+                               nb_empty_files+=1
+                               s="-1, 0, 0, 0".replace(',',' ').split()
+
+                       if (nb_empty_files == nb_files):
                                shall_break=True
                                break;
+
                        current_line.append(s);
 
                if shall_break == True:
@@ -116,14 +183,17 @@ def compute_temp_file(fio_data_file,disk_perf):
 
                        # We ignore the first 500msec as it doesn't seems to be part of the real benchmark
                        # Time < 500 usually reports BW=0 breaking the min computing
-                       if ((int(time)) > 500):
+                       if (((int(time)) > 500) or (int(time)==-1)):
                                disk_perf[index].append(int(perf))
-                               perfs.append(perf)
+                               perfs.append("%s %s"% (time, perf))
                                index = index + 1
 
                # If we reach this point, it means that all the traces are coherent
                for p in enumerate(perfs):
-                       temp_outfile[p[0]].write("%s %.2f %s\n" % (p[0], float(float(time)/1000), p[1]))
+                       perf_time,perf = p[1].split()
+                       if (perf_time != "-1"):
+                               temp_outfile[p[0]].write("%s %.2f %s\n" % (p[0], float(float(perf_time)/1000), perf))
+
 
        for file in files:
                file.close()
@@ -131,14 +201,15 @@ def compute_temp_file(fio_data_file,disk_perf):
                 file.close()
        return blk_size
 
-def compute_math(fio_data_file, title,gnuplot_output_filename,mode,disk_perf):
+def compute_math(fio_data_file, title,gnuplot_output_filename,gnuplot_output_dir,mode,disk_perf,gpm_dir):
+       print "Computing Maths"
        global_min=[]
        global_max=[]
-       average_file=open(gnuplot_output_filename+'.average', 'w')
-       min_file=open(gnuplot_output_filename+'.min', 'w')
-       max_file=open(gnuplot_output_filename+'.max', 'w')
-       stddev_file=open(gnuplot_output_filename+'.stddev', 'w')
-       global_file=open(gnuplot_output_filename+'.global','w')
+       average_file=open(gnuplot_output_dir+gnuplot_output_filename+'.average', 'w')
+       min_file=open(gnuplot_output_dir+gnuplot_output_filename+'.min', 'w')
+       max_file=open(gnuplot_output_dir+gnuplot_output_filename+'.max', 'w')
+       stddev_file=open(gnuplot_output_dir+gnuplot_output_filename+'.stddev', 'w')
+       global_file=open(gnuplot_output_dir+gnuplot_output_filename+'.global','w')
 
        min_file.write('DiskName %s\n' % mode)
        max_file.write('DiskName %s\n'% mode)
@@ -182,14 +253,14 @@ def compute_math(fio_data_file, title,gnuplot_output_filename,mode,disk_perf):
        stddev_file.close()
        global_file.close()
        try:
-               os.remove('mymath')
+               os.remove(gnuplot_output_dir+'mymath')
        except:
                True
 
-       generate_gnuplot_math_script("Average values of "+title,gnuplot_output_filename+'.average',mode,int(avg))
-       generate_gnuplot_math_script("Min values of "+title,gnuplot_output_filename+'.min',mode,average(global_min))
-       generate_gnuplot_math_script("Max values of "+title,gnuplot_output_filename+'.max',mode,average(global_max))
-       generate_gnuplot_math_script("Standard Deviation of "+title,gnuplot_output_filename+'.stddev',mode,int(standard_deviation))
+       generate_gnuplot_math_script("Average values of "+title,gnuplot_output_filename+'.average',mode,int(avg),gnuplot_output_dir,gpm_dir)
+       generate_gnuplot_math_script("Min values of "+title,gnuplot_output_filename+'.min',mode,average(global_min),gnuplot_output_dir,gpm_dir)
+       generate_gnuplot_math_script("Max values of "+title,gnuplot_output_filename+'.max',mode,average(global_max),gnuplot_output_dir,gpm_dir)
+       generate_gnuplot_math_script("Standard Deviation of "+title,gnuplot_output_filename+'.stddev',mode,int(standard_deviation),gnuplot_output_dir,gpm_dir)
 
 def parse_global_files(fio_data_file, global_search):
        max_result=0
@@ -236,17 +307,28 @@ def parse_global_files(fio_data_file, global_search):
        else:
                print "Global search %s is not yet implemented\n" % global_search
 
-def render_gnuplot():
-       print "Running gnuplot Rendering\n"
+def render_gnuplot(fio_data_file, gnuplot_output_dir):
+       print "Running gnuplot Rendering"
        try:
-               os.system("gnuplot mymath")
-               os.system("gnuplot mygraph")
+               # Let's render all the compared files if some
+               if len(fio_data_file) > 1:
+                       print " |-> Rendering comparing traces"
+                       os.system("cd %s; for i in *.gnuplot; do gnuplot $i; done" % gnuplot_output_dir)
+               print " |-> Rendering math traces"
+               os.system("cd %s; gnuplot mymath" % gnuplot_output_dir)
+               print " |-> Rendering 2D & 3D traces"
+               os.system("cd %s; gnuplot mygraph" % gnuplot_output_dir)
+
+               name_of_directory="the current"
+               if gnuplot_output_dir != "./":
+                       name_of_directory=gnuplot_output_dir
+               print "\nRendering traces are available in %s directory" % name_of_directory
        except:
                print "Could not run gnuplot on mymath or mygraph !\n"
                sys.exit(1);
 
 def print_help():
-    print 'fio2gnuplot.py -ghbio -t <title> -o <outputfile> -p <pattern>'
+    print 'fio2gnuplot.py -ghbiod -t <title> -o <outputfile> -p <pattern> -G <type>'
     print
     print '-h --help                           : Print this help'
     print '-p <pattern> or --pattern <pattern> : A pattern in regexp to select fio input files'
@@ -255,6 +337,7 @@ def print_help():
     print '-g           or --gnuplot           : Render gnuplot traces before exiting'
     print '-o           or --outputfile <file> : The basename for gnuplot traces'
     print '                                       - Basename is set with the pattern if defined'
+    print '-d           or --outputdir <dir>   : The directory where gnuplot shall render files'
     print '-t           or --title <title>     : The title of the gnuplot traces'
     print '                                       - Title is set with the block size detected in fio traces'
     print '-G          or --Global <type>     : Search for <type> in .global files match by a pattern'
@@ -267,13 +350,21 @@ def main(argv):
     pattern_set_by_user=False
     title='No title'
     gnuplot_output_filename='result'
+    gnuplot_output_dir='./'
+    gpm_dir="/usr/share/fio/"
     disk_perf=[]
     run_gnuplot=False
     parse_global=False
     global_search=''
 
+    if not os.path.isfile(gpm_dir+'math.gpm'):
+           gpm_dir="/usr/local/share/fio/"
+           if not os.path.isfile(gpm_dir+'math.gpm'):
+                   print "Looks like fio didn't got installed properly as no gpm files found in '/usr/share/fio' or '/usr/local/share/fio'\n"
+                   sys.exit(3)
+
     try:
-           opts, args = getopt.getopt(argv[1:],"ghbio:t:p:G:")
+           opts, args = getopt.getopt(argv[1:],"ghbio:d:t:p:G:")
     except getopt.GetoptError:
         print_help()
          sys.exit(2)
@@ -289,6 +380,12 @@ def main(argv):
         pattern=pattern.replace('\\','')
       elif opt in ("-o", "--outputfile"):
          gnuplot_output_filename=arg
+      elif opt in ("-d", "--outputdir"):
+         gnuplot_output_dir=arg
+        if not gnuplot_output_dir.endswith('/'):
+               gnuplot_output_dir=gnuplot_output_dir+'/'
+        if not os.path.exists(gnuplot_output_dir):
+               os.makedirs(gnuplot_output_dir)
       elif opt in ("-t", "--title"):
          title=arg
       elif opt in ("-g", "--gnuplot"):
@@ -309,10 +406,12 @@ def main(argv):
     if len(fio_data_file) == 0:
            print "No log file found with pattern %s!" % pattern
            sys.exit(1)
+    else:
+           print "%d files Selected with pattern '%s'" % (len(fio_data_file), pattern)
 
     fio_data_file=sorted(fio_data_file, key=str.lower)
     for file in fio_data_file:
-       print 'Selected %s' % file
+       print ' |-> %s' % file
        if "_bw.log" in file :
                mode="Bandwidth (KB/sec)"
        if "_iops.log" in file :
@@ -323,6 +422,7 @@ def main(argv):
            if "IO" in mode:
                    title='IO benchmark with %d fio results' % len(fio_data_file)
 
+    print
     #We need to adjust the output filename regarding the pattern required by the user
     if (pattern_set_by_user == True):
            gnuplot_output_filename=pattern
@@ -339,14 +439,14 @@ def main(argv):
     if parse_global==True:
        parse_global_files(fio_data_file, global_search)
     else:
-       blk_size=compute_temp_file(fio_data_file,disk_perf)
+       blk_size=compute_temp_file(fio_data_file,disk_perf,gnuplot_output_dir)
        title="%s @ Blocksize = %dK" % (title,blk_size/1024)
-       compute_aggregated_file(fio_data_file, gnuplot_output_filename)
-       compute_math(fio_data_file,title,gnuplot_output_filename,mode,disk_perf)
-       generate_gnuplot_script(fio_data_file,title,gnuplot_output_filename,mode,disk_perf)
+       compute_aggregated_file(fio_data_file, gnuplot_output_filename, gnuplot_output_dir)
+       compute_math(fio_data_file,title,gnuplot_output_filename,gnuplot_output_dir,mode,disk_perf,gpm_dir)
+       generate_gnuplot_script(fio_data_file,title,gnuplot_output_filename,gnuplot_output_dir,mode,disk_perf,gpm_dir)
 
        if (run_gnuplot==True):
-               render_gnuplot()
+               render_gnuplot(fio_data_file, gnuplot_output_dir)
 
     # Cleaning temporary files
     try: