Commit 62f109f0 authored by Deathcrow's avatar Deathcrow
Browse files

- added legacy option

- code restructuring
parent a26951ca
#!/usr/bin/env python3 #!/usr/bin/env python3
# vim: expandtab shiftwidth=4 softtabstop=4
#iperf -V -Z cubic -t 30 -c fdb2:f689:4248:2bc8::3 -p 12345 #iperf -V -Z cubic -t 30 -c fdb2:f689:4248:2bc8::3 -p 12345
import argparse import argparse
...@@ -57,28 +60,53 @@ def process_raw_tcpprobe_file(tcpprobe_file, sender): ...@@ -57,28 +60,53 @@ def process_raw_tcpprobe_file(tcpprobe_file, sender):
for key, i in enumerate(sender): for key, i in enumerate(sender):
if line_split[1] == r"["+i['src']+r"]:"+i['src_port']: if line_split[1] == r"["+i['src']+r"]:"+i['src_port']:
# and line_split[2][:-5] == r"["+i['dst']+r"]:": # and line_split[2][:-5] == r"["+i['dst']+r"]:":
tcpprobe_sender_file = open(args.tmp_folder+"/tcpprobe_"+i['congestion']+"_"+str(key), 'a') i['tcpprobe_file'].write(line)
tcpprobe_sender_file.write(line) # i['tcpprobe_file'].close()
tcpprobe_sender_file.close() # if not i.get('tcpprobe_time'):
if not i.get('tcpprobe_time'): # i['tcpprobe_time'] = float(line_split[0])
i['tcpprobe_time'] = float(line_split[0]) # i['tcpprobe_seq'] = int(line_split[4], 0)
i['tcpprobe_seq'] = int(line_split[4], 0) # if float(line_split[0]) - i['tcpprobe_time'] > bandwith_interval:
if float(line_split[0]) - i['tcpprobe_time'] > bandwith_interval: # bandwidth_file = open(args.tmp_folder+"/bw_"+i['congestion']+"_"+str(key), 'a')
bandwith_file = open(args.tmp_folder+"/bw_"+i['congestion']+"_"+str(key), 'a')
# write Mbps to file # write Mbps to file
bandwith_file.write(" ".join(line_split[:3]+[str(abs(int(line_split[4], 0) - i['tcpprobe_seq']) / bandwith_interval * 8 / 1000 / 1000 )] )+"\n" ) # bandwidth_file.write(" ".join(line_split[:3]+[str(abs(int(line_split[4], 0) - i['tcpprobe_seq']) / bandwith_interval * 8 / 1000 / 1000 )] )+"\n" )
bandwith_file.close() # bandwidth_file.close()
i['tcpprobe_time'] = float(line_split[0]) # i['tcpprobe_time'] = float(line_split[0])
i['tcpprobe_seq'] = int(line_split[4], 0) # i['tcpprobe_seq'] = int(line_split[4], 0)
tcpprobe_file.close()
def process_proc_tcpprobe(sender): #sampling
tcpprobe_proc = open("/proc/net/tcpprobe", "r")
unprocessed_senders = sender.copy()
# collect line from tcpprobe and write to file if it belongs to a
# unprocessed (one that didn't have a line written yet) sender
while len(unprocessed_senders) > 0:
line = tcpprobe_proc.readline()
time_dif = time.perf_counter() - start_time
line_split = line.split(" ")
# try to match current tcpprobe line against all unprocessed senders
for key, i in enumerate(unprocessed_senders):
if (line_split[1] == r"["+i['src']+r"]:"+i['src_port'] and line_split[2] == r"["+i['dst']+r"]:"+i['port']):
# write time + current tcpprobe line to file for this sender
unprocessed_senders.pop(key)['tcpprobe_file'].write(str(time_dif)+" "+" ".join(line_split[1:]))
break
else: # no break occured
if line == "": # this shouldn't happen. Try to continue anyway
print("CRITICAL ERROR!")
unprocessed_senders={}
tcpprobe_proc.close()
def signal_handler(signum, frame): def signal_handler(signum, frame):
clean_up() clean_up()
raise SystemExit raise SystemExit
def clean_up(): def clean_up():
# Clean up # Clean up
# tcpprobe.terminate() if args.legacy:
tcpprobe.terminate()
if args.tcpdump == "1": if args.tcpdump == "1":
tcpdump.terminate() tcpdump.terminate()
# cpunetlog.terminate() # cpunetlog.terminate()
...@@ -116,6 +144,10 @@ parser.add_argument('-D', '--tcpdump', default="1", \ ...@@ -116,6 +144,10 @@ parser.add_argument('-D', '--tcpdump', default="1", \
help="Enable/Disable tcpdump (Default: enabled") help="Enable/Disable tcpdump (Default: enabled")
parser.add_argument('-S', '--samplerate', default="0.1", \ parser.add_argument('-S', '--samplerate', default="0.1", \
help="Samplerate for measurements") help="Samplerate for measurements")
parser.add_argument('--legacy', action='store_true', \
help="Record entire tcp_probe output instead of samples by samplerate. \
Can be less CPU intensive for certain samplerates. \
Consider using full=0 for tcp_probe module")
args = parser.parse_args() args = parser.parse_args()
...@@ -161,8 +193,9 @@ else: ...@@ -161,8 +193,9 @@ else:
print("Utility not supported") print("Utility not supported")
raise SystemExit raise SystemExit
tcpprobe_file = tempfile.TemporaryFile('w+') if args.legacy:
#tcpprobe = subprocess.Popen(["cat", "/proc/net/tcpprobe"],stdout=tcpprobe_file) tcpprobe_file = tempfile.TemporaryFile('w+')
tcpprobe = subprocess.Popen(["cat", "/proc/net/tcpprobe"],stdout=tcpprobe_file)
if args.tcpdump == "1": if args.tcpdump == "1":
tcpdump = subprocess.Popen(["tcpdump", "-iany", "-w"+args.tmp_folder+"/tcpdump_"+str(mypid), "-s100"]) tcpdump = subprocess.Popen(["tcpdump", "-iany", "-w"+args.tmp_folder+"/tcpdump_"+str(mypid), "-s100"])
...@@ -230,43 +263,29 @@ if args.utility != "netperf": ...@@ -230,43 +263,29 @@ if args.utility != "netperf":
# ss_file.close() # ss_file.close()
# time.sleep(0.1) # time.sleep(0.1)
if not args.legacy:
while time.perf_counter() - start_time < float(args.time):
while_start = time.perf_counter() # remember beginning of this loop
process_proc_tcpprobe(sender) #heavy lifting here
try:
# sleep samplerate minus time spent in loop
time.sleep(float(args.samplerate) - (time.perf_counter() - while_start))
except ValueError: # negative sleep
pass
while time.perf_counter() - start_time < float(args.time): else:
time.sleep(float(args.time))
while_start = time.perf_counter() # remember beginning of this loop
tcpprobe_proc = open("/proc/net/tcpprobe", "r")
unprocessed_senders = sender.copy()
# collect line from tcpprobe and write to file if it belongs to a
# unprocessed (one that didn't have a line written yet) sender
while len(unprocessed_senders) > 0:
line = tcpprobe_proc.readline()
time_dif = time.perf_counter() - start_time
line_split = line.split(" ")
# try to match current tcpprobe line against all unprocessed senders
for key, i in enumerate(unprocessed_senders):
if (line_split[1] == r"["+i['src']+r"]:"+i['src_port'] and line_split[2] == r"["+i['dst']+r"]:"+i['port']):
# write time + current tcpprobe line to file for this sender
unprocessed_senders.pop(key)['tcpprobe_file'].write(str(time_dif)+" "+" ".join(line_split[1:]))
break
else: # no break occured
if line == "":
unprocessed_senders={} # fly, you fools!
tcpprobe_proc.close()
#print(abs(time.perf_counter() - while_start))
try:
# sleep samplerate minus time spent in loop
time.sleep(float(args.samplerate) - (time.perf_counter() - while_start))
except ValueError: # negative sleep
pass
for i in sender: for i in sender:
i['utility'].wait() i['utility'].wait()
clean_up() clean_up()
if args.legacy:
process_raw_tcpprobe_file(tcpprobe_file, sender)
#calculate bandwidth for each sender: #calculate bandwidth for each sender:
for key, i in enumerate(sender): for key, i in enumerate(sender):
last_timestamp = 0 last_timestamp = 0
...@@ -278,16 +297,20 @@ for key, i in enumerate(sender): ...@@ -278,16 +297,20 @@ for key, i in enumerate(sender):
last_timestamp = float(line_split[0]) last_timestamp = float(line_split[0])
last_seq = int(line_split[4], 0) #convert from hex-string last_seq = int(line_split[4], 0) #convert from hex-string
else: else:
bandwith_file = open(args.tmp_folder+"/bw_"+i['congestion']+"_"+str(key), 'a') if abs(float(line_split[0]) - last_timestamp) >= float(args.samplerate):
# print bw for current time interval, convert to Mbps bandwidth_file = open(args.tmp_folder+"/bw_"+i['congestion']+"_"+str(key), 'a')
# pay attention to seq number wrap-around # print bw for current time interval, convert to Mbps
bw = (int(line_split[4], 0) - last_seq) if (int(line_split[4], 0) - last_seq) >= 0 else (int("0xffffffff",0) - last_seq + int(line_split[4], 0)) # pay attention to seq number wrap-around
bw = bw / abs(float(line_split[0]) - last_timestamp) # byte/s bw = (int(line_split[4], 0) - last_seq) if (int(line_split[4], 0) - last_seq) >= 0 \
bw = bw * 8 / 1000 / 1000 # Mbps else (int("0xffffffff",0) - last_seq + int(line_split[4], 0))
bandwith_file.write(" ".join(line_split[:3]+[str(bw)] )+"\n" ) bw = bw / abs(float(line_split[0]) - last_timestamp) # byte/s
bandwith_file.close() bw = bw * 8 / 1000 / 1000 # Mbps
last_timestamp = float(line_split[0]) bandwidth_file.write(" ".join(line_split[:3]+[str(bw)] )+"\n" )
last_seq = int(line_split[4], 0) bandwidth_file.close()
last_timestamp = float(line_split[0])
last_seq = int(line_split[4], 0)
else:
pass
for i in sender: for i in sender:
i['tcpprobe_file'].close() i['tcpprobe_file'].close()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment