#!/usr/bin/python

# this script is for debug m5_2.0b
# Author: Rongrong Lai  (rlai@mtu.edu)
# Date: 12/05/2006

import os, sys, shutil, socket, logging, datetime
from time import localtime, strftime

# benchmarks
benchmarks = [
	'ValStream',
        'ValTracy-128M',
#        'ValTracy-1G',
#        'ValStreamScale',
#        'ValStreamCopy'
]

# page placement policy
policy = [	
				'none',
        'seq',
      	'rnd',
        'bhseq',
 	      'bhrnd',
# 	'hybrid' ,
#        'CHANHOPRD',
#	'CHANHOPSQ',
#        'LUB',
#        'HYBRID_AF8',
#        'HYBRID_AF8_LRA8',
#        'HYBRID',
]

# setup directories
os.chdir('/home/tracy/m5_2.0b')
current_dir = os.getcwd() + '/'
sim_dir = 'simulation/'

# executable file of m5 simulator
m5 = current_dir + 'm5/build/ALPHA_FS/m5.opt'

# m5 simulator config file
# all included files by m5.config.py must be in the same directory
#config = current_dir + sim_dir + 'configs/seq/fs.py'

# output directory contains all simulation outputs
# if invoked with 'clean' parameter, the output directory
# will be cleaned before starting the simulation
out_dir = current_dir + sim_dir + 'result/'
if os.path.exists(out_dir):
	if len(sys.argv) > 1 and sys.argv[1] == 'clean':
		shutil.rmtree(out_dir)
		os.mkdir(out_dir)
else:
	os.mkdir(out_dir)

# log file
logging.basicConfig(level=logging.INFO,
                     format='%(asctime)s %(levelname)s %(message)s',
                     filename=current_dir + sim_dir + socket.gethostbyname(gethostname()) + '.log')


# simulation main loop
# the complete command line to run the simulation
for ben in benchmarks:
	for cf in policy:
        	# m5 simulator output file
        	#out_file = out_dir + ben + '_' + cf + '.out'
        	# m5 simulator statistics file
        	sta_file = out_dir + ben + '_' + cf + '.sta'
        	# dump of stdout
        	std_file = out_dir + ben + '_' + cf + '.std'
       	 	# dump of stderr
        	err_file = out_dir + ben + '_' + cf + '.err'

        	# skip if this benchmark is running or alrady complete
        	if os.access(sta_file, os.F_OK):
                	#logging.info('%s (skipped)', ben)
                	continue

		cmd = m5 + ' -d ' + out_dir +\
                	' --stats-file=' + sta_file +\
        		' ' + current_dir + sim_dir + 'configs/' + cf + '/fs.py' +\
                	' -b ' + ben +\
			' > ' + std_file + ' 2> ' + err_file
	

		# simulation started time
        	logging.info(' %s imulation started', ben)
		start_time = datetime.datetime.now()
		# start the simulation
		os.system(cmd)
		# simulation completed time
		logging.info('simulation completed (%s)',datetime.datetime.now() - start_time)

logging.info('Done')
logging.info('*****************************************')
