1*7330f729Sjoerg#!/usr/bin/env python 2*7330f729Sjoerg 3*7330f729Sjoergfrom __future__ import print_function 4*7330f729Sjoerg 5*7330f729Sjoergclass TimingScriptGenerator: 6*7330f729Sjoerg """Used to generate a bash script which will invoke the toy and time it""" 7*7330f729Sjoerg def __init__(self, scriptname, outputname): 8*7330f729Sjoerg self.shfile = open(scriptname, 'w') 9*7330f729Sjoerg self.timeFile = outputname 10*7330f729Sjoerg self.shfile.write("echo \"\" > %s\n" % self.timeFile) 11*7330f729Sjoerg 12*7330f729Sjoerg def writeTimingCall(self, irname, callname): 13*7330f729Sjoerg """Echo some comments and invoke both versions of toy""" 14*7330f729Sjoerg rootname = irname 15*7330f729Sjoerg if '.' in irname: 16*7330f729Sjoerg rootname = irname[:irname.rfind('.')] 17*7330f729Sjoerg self.shfile.write("echo \"%s: Calls %s\" >> %s\n" % (callname, irname, self.timeFile)) 18*7330f729Sjoerg self.shfile.write("echo \"\" >> %s\n" % self.timeFile) 19*7330f729Sjoerg self.shfile.write("echo \"With MCJIT\" >> %s\n" % self.timeFile) 20*7330f729Sjoerg self.shfile.write("/usr/bin/time -f \"Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb\"") 21*7330f729Sjoerg self.shfile.write(" -o %s -a " % self.timeFile) 22*7330f729Sjoerg self.shfile.write("./toy -suppress-prompts -use-mcjit=true -enable-lazy-compilation=true -use-object-cache -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n" % (irname, callname, rootname, rootname)) 23*7330f729Sjoerg self.shfile.write("echo \"\" >> %s\n" % self.timeFile) 24*7330f729Sjoerg self.shfile.write("echo \"With MCJIT again\" >> %s\n" % self.timeFile) 25*7330f729Sjoerg self.shfile.write("/usr/bin/time -f \"Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb\"") 26*7330f729Sjoerg self.shfile.write(" -o %s -a " % self.timeFile) 27*7330f729Sjoerg self.shfile.write("./toy -suppress-prompts -use-mcjit=true -enable-lazy-compilation=true -use-object-cache -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n" % (irname, callname, rootname, rootname)) 28*7330f729Sjoerg self.shfile.write("echo \"\" >> %s\n" % self.timeFile) 29*7330f729Sjoerg self.shfile.write("echo \"With JIT\" >> %s\n" % self.timeFile) 30*7330f729Sjoerg self.shfile.write("/usr/bin/time -f \"Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb\"") 31*7330f729Sjoerg self.shfile.write(" -o %s -a " % self.timeFile) 32*7330f729Sjoerg self.shfile.write("./toy -suppress-prompts -use-mcjit=false -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n" % (irname, callname, rootname, rootname)) 33*7330f729Sjoerg self.shfile.write("echo \"\" >> %s\n" % self.timeFile) 34*7330f729Sjoerg self.shfile.write("echo \"\" >> %s\n" % self.timeFile) 35*7330f729Sjoerg 36*7330f729Sjoergclass LibScriptGenerator: 37*7330f729Sjoerg """Used to generate a bash script which will invoke the toy and time it""" 38*7330f729Sjoerg def __init__(self, filename): 39*7330f729Sjoerg self.shfile = open(filename, 'w') 40*7330f729Sjoerg 41*7330f729Sjoerg def writeLibGenCall(self, libname, irname): 42*7330f729Sjoerg self.shfile.write("./toy -suppress-prompts -use-mcjit=false -dump-modules < %s 2> %s\n" % (libname, irname)) 43*7330f729Sjoerg 44*7330f729Sjoergdef splitScript(inputname, libGenScript, timingScript): 45*7330f729Sjoerg rootname = inputname[:-2] 46*7330f729Sjoerg libname = rootname + "-lib.k" 47*7330f729Sjoerg irname = rootname + "-lib.ir" 48*7330f729Sjoerg callname = rootname + "-call.k" 49*7330f729Sjoerg infile = open(inputname, "r") 50*7330f729Sjoerg libfile = open(libname, "w") 51*7330f729Sjoerg callfile = open(callname, "w") 52*7330f729Sjoerg print("Splitting %s into %s and %s" % (inputname, callname, libname)) 53*7330f729Sjoerg for line in infile: 54*7330f729Sjoerg if not line.startswith("#"): 55*7330f729Sjoerg if line.startswith("print"): 56*7330f729Sjoerg callfile.write(line) 57*7330f729Sjoerg else: 58*7330f729Sjoerg libfile.write(line) 59*7330f729Sjoerg libGenScript.writeLibGenCall(libname, irname) 60*7330f729Sjoerg timingScript.writeTimingCall(irname, callname) 61*7330f729Sjoerg 62*7330f729Sjoerg# Execution begins here 63*7330f729SjoerglibGenScript = LibScriptGenerator("make-libs.sh") 64*7330f729SjoergtimingScript = TimingScriptGenerator("time-lib.sh", "lib-timing.txt") 65*7330f729Sjoerg 66*7330f729Sjoergscript_list = ["test-5000-3-50-50.k", "test-5000-10-100-10.k", "test-5000-10-5-10.k", "test-5000-10-1-0.k", 67*7330f729Sjoerg "test-1000-3-10-50.k", "test-1000-10-100-10.k", "test-1000-10-5-10.k", "test-1000-10-1-0.k", 68*7330f729Sjoerg "test-200-3-2-50.k", "test-200-10-40-10.k", "test-200-10-2-10.k", "test-200-10-1-0.k"] 69*7330f729Sjoerg 70*7330f729Sjoergfor script in script_list: 71*7330f729Sjoerg splitScript(script, libGenScript, timingScript) 72*7330f729Sjoergprint("All done!") 73