updated usage of time.time to time.monotonic or time.perf_counter due to time.time possibly rolling backwards

This commit is contained in:
Blake Harnden 2019-12-06 09:42:41 -08:00
parent b9bbf397c9
commit 45a23a6c14
11 changed files with 29 additions and 29 deletions

View file

@ -411,7 +411,7 @@ class CoreClient:
else:
emane_config = None
start = time.time()
start = time.perf_counter()
response = self.client.start_session(
self.session_id,
nodes,
@ -425,7 +425,7 @@ class CoreClient:
service_configs,
file_configs,
)
process_time = time.time() - start
process_time = time.perf_counter() - start
logging.debug("start session(%s), result: %s", self.session_id, response.result)
self.app.statusbar.start_session_callback(process_time)
@ -439,9 +439,9 @@ class CoreClient:
def stop_session(self, session_id=None):
if not session_id:
session_id = self.session_id
start = time.time()
start = time.perf_counter()
response = self.client.stop_session(session_id)
process_time = time.time() - start
process_time = time.perf_counter() - start
self.app.statusbar.stop_session_callback(process_time)
logging.debug("stopped session(%s), result: %s", session_id, response.result)

View file

@ -33,10 +33,10 @@ class MenuAction:
def cleanup_old_session(self, quitapp=False):
logging.info("cleaning up old session")
start = time.time()
start = time.perf_counter()
self.app.core.stop_session()
self.app.core.delete_session()
process_time = time.time() - start
process_time = time.perf_counter() - start
self.app.statusbar.stop_session_callback(process_time)
if quitapp:
self.app.quit()

View file

@ -660,7 +660,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
last_check = None
last_stats = None
while self._is_running(context):
now = time.time()
now = time.monotonic()
stats = get_net_stats()
# calculate average

View file

@ -1710,7 +1710,7 @@ class CoreHandler(socketserver.BaseRequestHandler):
event_type=event_type,
name=name,
data=fail_data + ";" + unknown_data,
time=str(time.time()),
time=str(time.monotonic()),
)
self.session.broadcast_event(event_data)

View file

@ -103,7 +103,7 @@ class Session:
# TODO: should the default state be definition?
self.state = EventTypes.NONE.value
self._state_time = time.time()
self._state_time = time.monotonic()
self._state_file = os.path.join(self.session_dir, "state")
# hooks handlers
@ -1030,7 +1030,7 @@ class Session:
return
self.state = state_value
self._state_time = time.time()
self._state_time = time.monotonic()
logging.info("changing session(%s) to state %s", self.id, state_name)
self.write_state(state_value)
@ -1038,7 +1038,7 @@ class Session:
self.run_state_hooks(state_value)
if send_event:
event_data = EventData(event_type=state_value, time=str(time.time()))
event_data = EventData(event_type=state_value, time=str(time.monotonic()))
self.broadcast_event(event_data)
def write_state(self, state):
@ -1821,7 +1821,7 @@ class Session:
if not in runtime.
"""
if self.state == EventTypes.RUNTIME_STATE.value:
return time.time() - self._state_time
return time.monotonic() - self._state_time
else:
return 0.0

View file

@ -145,7 +145,7 @@ class EventLoop:
with self.lock:
if not self.running or not self.queue:
break
now = time.time()
now = time.monotonic()
if self.queue[0].time > now:
schedule = True
break
@ -170,7 +170,7 @@ class EventLoop:
raise ValueError("scheduling event while not running")
if not self.queue:
return
delay = self.queue[0].time - time.time()
delay = self.queue[0].time - time.monotonic()
if self.timer:
raise ValueError("timer was already set")
self.timer = Timer(delay, self.__run_events)
@ -187,7 +187,7 @@ class EventLoop:
if self.running:
return
self.running = True
self.start = time.time()
self.start = time.monotonic()
for event in self.queue:
event.time += self.start
self.__schedule_event()
@ -225,7 +225,7 @@ class EventLoop:
self.eventnum += 1
evtime = float(delaysec)
if self.running:
evtime += time.time()
evtime += time.monotonic()
event = Event(eventnum, evtime, func, *args, **kwds)
if self.queue:

View file

@ -174,7 +174,7 @@ class MobilityManager(ModelManager):
event_type=event_type,
name=f"mobility:{model.name}",
data=data,
time=str(time.time()),
time=str(time.monotonic()),
)
self.session.broadcast_event(event_data)
@ -612,7 +612,7 @@ class WayPointMobility(WirelessModel):
if self.state != self.STATE_RUNNING:
return
t = self.lasttime
self.lasttime = time.time()
self.lasttime = time.monotonic()
now = self.lasttime - self.timezero
dt = self.lasttime - t
@ -664,7 +664,7 @@ class WayPointMobility(WirelessModel):
:return: nothing
"""
logging.info("running mobility scenario")
self.timezero = time.time()
self.timezero = time.monotonic()
self.lasttime = self.timezero - (0.001 * self.refresh_ms)
self.movenodesinitial()
self.runround()
@ -844,7 +844,7 @@ class WayPointMobility(WirelessModel):
self.lasttime = 0
self.run()
elif laststate == self.STATE_PAUSED:
now = time.time()
now = time.monotonic()
self.timezero += now - self.lasttime
self.lasttime = now - (0.001 * self.refresh_ms)
self.runround()
@ -871,7 +871,7 @@ class WayPointMobility(WirelessModel):
:return: nothing
"""
self.state = self.STATE_PAUSED
self.lasttime = time.time()
self.lasttime = time.monotonic()
class Ns2ScriptedMobility(WayPointMobility):

View file

@ -57,7 +57,7 @@ class EbtablesQueue:
:return: nothing
"""
with self.updatelock:
self.last_update_time[wlan] = time.time()
self.last_update_time[wlan] = time.monotonic()
if self.doupdateloop:
return
@ -108,9 +108,9 @@ class EbtablesQueue:
:rtype: float
"""
try:
elapsed = time.time() - self.last_update_time[wlan]
elapsed = time.monotonic() - self.last_update_time[wlan]
except KeyError:
self.last_update_time[wlan] = time.time()
self.last_update_time[wlan] = time.monotonic()
elapsed = 0.0
return elapsed
@ -122,7 +122,7 @@ class EbtablesQueue:
:param wlan: wlan entity
:return: nothing
"""
self.last_update_time[wlan] = time.time()
self.last_update_time[wlan] = time.monotonic()
self.updates.remove(wlan)
def updateloop(self):

View file

@ -538,13 +538,13 @@ class CoreServices:
time.sleep(service.validation_timer)
# non-blocking, attempt to validate periodically, up to validation_timer time
elif service.validation_mode == ServiceMode.NON_BLOCKING:
start = time.time()
start = time.monotonic()
while True:
status = self.validate_service(node, service)
if not status:
break
if time.time() - start > service.validation_timer:
if time.monotonic() - start > service.validation_timer:
break
time.sleep(service.validation_period)

View file

@ -1042,7 +1042,7 @@ class TestGrpc:
client.events(session.id, handle_event)
time.sleep(0.1)
event = EventData(
event_type=EventTypes.RUNTIME_STATE.value, time=str(time.time())
event_type=EventTypes.RUNTIME_STATE.value, time=str(time.monotonic())
)
session.broadcast_event(event)

View file

@ -523,7 +523,7 @@ class TestGui:
MessageFlags.ADD.value,
[
(EventTlvs.TYPE, EventTypes.SCHEDULED.value),
(EventTlvs.TIME, str(time.time() + 100)),
(EventTlvs.TIME, str(time.monotonic() + 100)),
(EventTlvs.NODE, node.id),
(EventTlvs.NAME, "event"),
(EventTlvs.DATA, "data"),