-
Notifications
You must be signed in to change notification settings - Fork 1
/
ns2run.py
executable file
·149 lines (128 loc) · 5.45 KB
/
ns2run.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python3
import subprocess
import sys
import time
import threading
import os
import signal
import argparse
import hashlib
from argparse import ArgumentParser
import time
import re
import json
def getParser():
parser = ArgumentParser()
parser.add_argument("--basefile", default="scripts/large-scale.tcl")
parser.add_argument("--dry-run", action="store_true")
parser.add_argument("--print-fct", action="store_true")
parser.add_argument("-n", "--numFlow", type=int, default=10000)
parser.add_argument("--workload", choices=('cachefollower', 'mining', 'search', 'webserver', 'me', 'google', 'facebook'), default='cachefollower')
parser.add_argument("--linkLoad", type=float, default=0.5)
parser.add_argument("-I", "--expID", type=int, default=1000)
parser.add_argument("--deployStep", type=int, choices=range(0, 101), default=100)
parser.add_argument("-c", "--cccType", choices=('xpass', 'gdx', 'flexpass'))
parser.add_argument("--trafficLocality", type=int, choices=range(0, 101), default=0)
parser.add_argument("--deployPerCluster", type=int, choices=(0, 1), default=1)
parser.add_argument("--foregroundFlowRatio", type=float, default=0.0)
parser.add_argument("--enableSharedBuffer", type=int, choices=(0, 1), default=1)
parser.add_argument("--flexpassScheme", type=int, choices=(1, 2, 3, 4), default=2)
parser.add_argument("--flexpassSelDropThresh", type=int, default=150000)
parser.add_argument("--xpassQueueWeight", type=float, default=0.5)
parser.add_argument("--newFlexPassAllocationLogic", type=int, choices=(0, 1), default=1)
parser.add_argument("--tcpInitWindow", type=int, default=5)
parser.add_argument("--flexpassReactiveInitWindow", type=int, default=5)
parser.add_argument("--reorderingMeasureInRc3", type=int, choices=(0, 1), default=0)
parser.add_argument("--staticAllocation", type=int, choices=(0, 1), default=0)
parser.add_argument("--xpassQueueIsolation", type=int, choices=(0, 1), default=0)
parser.add_argument("--strictHighPriority", type=int, choices=(0, 1), default=0)
parser.add_argument("--foregroundFlowSize", type=int, default=8000)
parser.add_argument("--numForegroundFlowPerHost", type=int, default=4)
parser.add_argument("--oracleQueueWeight", type=int, choices=(0, 1), default=0)
parser.add_argument("--trueOracleWeight", type=int, choices=(0, 1), default=0)
return parser
tasks = {}
g_tid = 0
def popenAndCall(onExit, popenArgs):
global g_tid, tasks
g_tid += 1
def runInThread(onExit, tid, popenArgs):
proc = subprocess.Popen(popenArgs)
tasks[tid][1] = proc.pid
ret = proc.wait()
onExit(tid, ret)
return
thread = threading.Thread(target=runInThread, args=(onExit, g_tid, popenArgs))
return thread, g_tid
def finishTask(tid, return_code):
global tasks
tasks[tid][2] = return_code
def sigint_handler(sig, frame):
for _, pid, _ in tasks.values():
os.kill(pid, signal.SIGTERM)
def parseLine(line, options):
if '###!!' not in line:
return line
target = line.split('###!!')[-1].lstrip() # which should still hold \n here
for key, val in vars(options).items():
if val is not None:
target = target.replace('<<<' + key + '>>>', str(val))
if ('<<<' in target) or ('>>>' in target):
target = line
print("Error: Cannot replace the line %s. Value not specified." % (line.strip()), file=sys.stdout)
sys.exit(1)
return target
def main():
parser = getParser()
options, args = parser.parse_known_args()
basefile_content = []
try:
with open(options.basefile, 'r') as f:
basefile_content = f.readlines()
except (OSError, FileNotFoundError):
print("Basefile %s not found." % options.basefile, file=sys.stderr)
sys.exit(1)
for idx, line in enumerate(basefile_content):
parsed = parseLine(line, options)
assert (len(parsed) == 0 or parsed[-1] == '\n')
basefile_content[idx] = parsed
str_config = "".join(basefile_content)
target_path = os.path.join("/tmp", hashlib.sha1(str_config.encode('utf-8')).hexdigest()+'_'+ str(int(time.time()*1000.0)%100000) + ".tmp")
try:
with open(target_path, 'w') as f:
f.write(str_config)
except (OSError, FileNotFoundError):
print("Cannot write configuration to %s." % target_path, file=sys.stderr)
sys.exit(1)
print(target_path, file=sys.stderr)
arg_dict = {}
for a in vars(options):
arg_dict[a] = getattr(options, a)
if options.dry_run:
print(str_config)
print(json.dumps(arg_dict), file=sys.stderr)
return
signal.signal(signal.SIGINT, sigint_handler)
os.system("chmod +x ./ns")
try:
arguments = ['./ns', target_path]
arguments.extend(args)
thr, tid = popenAndCall(finishTask, arguments)
tasks[tid] = [thr, 0, 0]
thr.start()
thr.join()
if options.print_fct:
try:
print("")
print("==========================ARG")
print(json.dumps(arg_dict))
print("==========================FCT")
with open('outputs/fct_%d.out' % options.expID, 'r') as fct_file:
print(fct_file.read(), end="")
except:
pass
sys.exit(tasks[tid][2])
except (OSError, KeyboardInterrupt):
sigint_handler(signal.SIGINT, None)
if __name__ == "__main__":
main()