Commit cc0f0e65 authored by Peter Jansweijer's avatar Peter Jansweijer

renewed timeout function (this one can pass return values)

added timeout on reading status line
parent 2c4eae27
......@@ -57,6 +57,7 @@ import lib.wrt_sfppt015sc as tunable
###############################################
# Found a neat python timout function on:
# https://stackoverflow.com/questions/366682/how-to-limit-execution-time-of-a-function-call-in-python
# drawback: function cannot return a value
def run_with_limited_time(func, args, kwargs, time):
"""Runs a function with time limit
......@@ -76,6 +77,32 @@ def run_with_limited_time(func, args, kwargs, time):
return True
###############################################
# Found a neat python timout function on:
# https://stackoverflow.com/questions/492519/timeout-on-a-function-call
# default retuns None when tim eout occurs
def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
import signal
class TimeoutError(Exception):
pass
def handler(signum, frame):
raise TimeoutError()
# set the timeout handler
signal.signal(signal.SIGALRM, handler)
signal.alarm(timeout_duration)
try:
result = func(*args, **kwargs)
except TimeoutError as exc:
result = default
finally:
signal.alarm(0)
return result
###############################################
def wr2wrpc(ser, cmd, prompt="slv=>"):
......@@ -93,6 +120,20 @@ def wr2wrpc(ser, cmd, prompt="slv=>"):
return
###############################################
def sfp_ena(ser_slave):
# bring the link down and up again by dis-/en-abeling the TX laser
# Note that this forces the wr switch to re-sybc and bitslide but
# not the wr devidce (SPEC or CLB) connected to it; Their wr links
# is kept up
wr2wrpc(ser_slave,"sfp ena 0\r","slv=>")
time.sleep (1)
wr2wrpc(ser_slave,"sfp ena 1\r","slv=>")
time.sleep (1)
return()
###############################################
def link_restart(ser_master,ser_slave, master_is_switch = True):
......@@ -116,15 +157,8 @@ def link_restart(ser_master,ser_slave, master_is_switch = True):
wr2wrpc(ser_slave,"ptp start\r","slv=>")
time.sleep (1)
# bring the link down and up again by dis-/en-abeling the TX laser
# Note that this forces the wr switch to re-sybc and bitslide but
# not the wr devidce (SPEC or CLB) connected to it; Their wr links
# is kept up
wr2wrpc(ser_slave,"sfp ena 0\r","slv=>")
time.sleep (1)
wr2wrpc(ser_slave,"sfp ena 1\r","slv=>")
time.sleep (1)
sfp_ena(ser_slave)
return()
......@@ -193,8 +227,19 @@ def wait_for_track_phase(ser_slave):
# print ("### error occured while reading wr status.")
# continue
#return(track_phase)
return()
return(track_phase)
#return()
###############################################
def get_statusline(ser_slave):
stat_lst = []
while len(stat_lst) < 27: # Keep reading until
stat = ser_slave.readline() # Readback valid status line
stat_lst = stat.split(' ') # split on spaces
return(stat_lst)
###############################################
# Main
......@@ -344,7 +389,9 @@ if __name__ == "__main__":
wr2wrpc(ser_tunable,"sfp rd_ch\r",ser_tunable_str)
print(ser_tunable.readline())
for restart in range(restarts):
restart = 0
while restart < restarts:
#for restart in range(restarts):
print("Link restart: ",restart+1)
link_restart(ser_master,ser_slave, master_is_switch)
......@@ -354,8 +401,10 @@ if __name__ == "__main__":
while not track_phase:
# Call "wait_for_track_phase(ser_slave)" with a timeout of 60 seconds
# If no TRACK_PHASE within 60 seconds then try restart link!
track_phase = run_with_limited_time(wait_for_track_phase, (ser_slave, ), {}, 60.0)
if not track_phase:
#track_phase = run_with_limited_time(wait_for_track_phase, (ser_slave, ), {}, 60.0)
#if not track_phase:
track_phase = timeout(wait_for_track_phase, (ser_slave, ), {}, 60)
if track_phase == None:
print ("### Timeout waiting for TRACK_PHASE, try link restart...")
link_restart(ser_master,ser_slave, master_is_switch)
......@@ -364,41 +413,47 @@ if __name__ == "__main__":
meas_number = 0
crtt = []
while meas_number < (crtt_skip + crtt_measurement): # take mean value over cttt_measurement
stat_lst = []
while len(stat_lst) < 27: # Keep reading until
stat = ser_slave.readline() # Readback valid status line
stat_lst = stat.split(' ') # split on spaces
#pdb.set_trace()
#if len(stat_lst) >= 27:
# note:
# [s for s in stat_lst if "crtt" in s]
# returns a list with one item, for example ['crtt:82466168']
# take item [0] from this list, split it over ":" and take
# item [1] from the resulting list
# format for 'temp:' is different. it has an extra space so
# the temp value is the next item op the stat_lst
curr_crtt = int([s for s in stat_lst if "crtt" in s][0].split(":")[1])
curr_drxm = int([s for s in stat_lst if "drxm" in s][0].split(":")[1])
curr_drxs = int([s for s in stat_lst if "drxs" in s][0].split(":")[1])
curr_temp = float(stat_lst[stat_lst.index('temp:')+1])
if meas_number == 0: # first measurment loads bitslide check valiables
check_drxm = curr_drxm
check_drxs = curr_drxs
meas_number = meas_number + 1
crtt.append(curr_crtt)
print(curr_crtt, curr_drxm, curr_drxs)
else: # next measurements compare bitslide check variables
if check_drxm == curr_drxm and check_drxs == curr_drxs: # good measurment
stat_lst = timeout(get_statusline, (ser_slave, ), {}, 60)
if stat_lst == None:
print ("### Timeout waiting status line...")
link_restart(ser_master,ser_slave, master_is_switch)
failed = True
else:
#stat_lst = []
#while len(stat_lst) < 27: # Keep reading until
# stat = ser_slave.readline() # Readback valid status line
# stat_lst = stat.split(' ') # split on spaces
#pdb.set_trace()
#if len(stat_lst) >= 27:
# note:
# [s for s in stat_lst if "crtt" in s]
# returns a list with one item, for example ['crtt:82466168']
# take item [0] from this list, split it over ":" and take
# item [1] from the resulting list
# format for 'temp:' is different. it has an extra space so
# the temp value is the next item op the stat_lst
curr_crtt = int([s for s in stat_lst if "crtt" in s][0].split(":")[1])
curr_drxm = int([s for s in stat_lst if "drxm" in s][0].split(":")[1])
curr_drxs = int([s for s in stat_lst if "drxs" in s][0].split(":")[1])
curr_temp = float(stat_lst[stat_lst.index('temp:')+1])
if meas_number == 0: # first measurment loads bitslide check valiables
check_drxm = curr_drxm
check_drxs = curr_drxs
meas_number = meas_number + 1
crtt.append(curr_crtt)
print(curr_crtt, curr_drxm, curr_drxs)
# print(".",end='') # <= python 3
else:
meas_number = 0 # wrong measurement (link resync?) start all over.
crtt = []
else: # next measurements compare bitslide check variables
if check_drxm == curr_drxm and check_drxs == curr_drxs: # good measurment
meas_number = meas_number + 1
crtt.append(curr_crtt)
print(curr_crtt, curr_drxm, curr_drxs)
# print(".",end='') # <= python 3
else:
meas_number = 0 # wrong measurement (link resync?) start all over.
crtt = []
if not failed:
crtt_mean = numpy.mean(crtt[(crtt_skip + 1):]) # skip first measurements
print("sfp_channel: ", sfp_ch ,"itu_channel: ", ch ,"crtt avarage over "+str(crtt_measurement)+" measurments: "+str(crtt_mean))
......@@ -408,6 +463,8 @@ if __name__ == "__main__":
print ("### exception during crtt_measurements.")
continue
restart = restart + 1
data_file.close()
ser_slave.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