set_collector.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python
__author__ = 'hAsh'
#Expected args: <pin_run_script> <analysed_app_path> <in_path> <out_path> <idle_timeout>
import threading
import time
import os
import sys
WAIT_UNIT = 1
KILL_CMD_WIN = "taskkill /F /PID"
KILL_CMD_LIN = "kill -9"
KILL_CMD = KILL_CMD_WIN
ARG_NUM = 6
SET_LIST_DELIMITER = ";"
set_list_in_name = "SetList.in"
set_list_out_name = "CoverageList.out"
pin_run_script = "" # pin.exe -t C:\pin\source\tools\MyPinTool\x64\Debug\MyPinTool.dll -o %3 -- %1 %2
app_path = ""
in_path = ""
out_path = ""
timeout = WAIT_UNIT * 5 #default value can be changed by param
min_set_app = "\"D:\\min_set.exe\""
###############################################################
def wait_and_kill(pid_path, out_file, timeout):
the_pid = 0
pid_wait = WAIT_UNIT
while True :
try:
time.sleep(pid_wait)
pid_file = open(pid_path, 'r')
the_pid = int(pid_file.readline())
pid_file.close()
break
except IOError:
if (pid_wait == timeout) :
print "[ERROR] Could not open pid file, the script exits!"
exit(-1)
pid_wait = timeout
print "Waiting for PID..."
prev_file_size = 0
while True :
time.sleep(timeout)
file_size = os.stat(out_file).st_size
if ( prev_file_size == file_size ) :
print str(file_size)
break
else :
prev_file_size = file_size
os.system(KILL_CMD + " " + str(the_pid))
###############################################################
def main():
argc = sys.argv.__len__()
if (argc < ARG_NUM):
print "Expected args: <pin_run_script> <app_path> <in_path> <out_path> <timeout>"
exit (2)
pin_run_script = sys.argv[1]
app_path = sys.argv[2]
in_path = sys.argv[3]
out_path = sys.argv[4]
timeout = int(sys.argv[5])
print 'PIN run script is ', pin_run_script
print 'App path is ', app_path
print 'Input dir is ', in_path
print 'Output dir is ', out_path
print 'Timeout is ', str(timeout)
#
set_list_path = os.path.join(out_path, set_list_in_name)
set_covered_path = os.path.join(out_path, set_list_out_name)
set_list_file = open(set_list_path, "w")
for dirname, dirnames, filenames in os.walk(in_path):
# path to all subdirectories
for subdirname in dirnames:
print os.path.join(dirname, subdirname)
# path to all filenames
for filename in filenames:
param = os.path.join(dirname, filename)
out_file = os.path.join(out_path, filename) + ".txt"
pid_path = out_file + ".pid"
threading.Thread(target=wait_and_kill, args=[pid_path, out_file, timeout]).start()
set_list_file.write(out_file + SET_LIST_DELIMITER + param + "\n") #<fileName>;<setName>
set_list_file.flush()
os.system(pin_run_script + " \"" + app_path + "\" \"" + param + "\" \"" + out_file + "\"")
set_list_file.close()
command = "\"" + min_set_app + " \""+ set_list_path + "\" \"" + set_covered_path + "\" \""
print command
os.system(command)
###############################################################
if __name__ == "__main__":
sys.exit(main())