<s> from sts . replay_event import * <EOL> import time <EOL> from collections import Counter <EOL> import operator <EOL> import logging <EOL> log = logging . getLogger ( "<STR_LIT>" ) <EOL> def format_time ( time ) : <EOL> mins = int ( time / <NUM_LIT> ) <EOL> secs = int ( time % <NUM_LIT> ) <EOL> ms = int ( ( time * <NUM_LIT:1000> ) % <NUM_LIT:1000> ) <EOL> return "<STR_LIT>" % ( mins , secs , ms ) <EOL> class EventSchedulerStats ( object ) : <EOL> def __init__ ( self ) : <EOL> self . event2matched = Counter ( ) <EOL> self . msgrecv2matched = Counter ( ) <EOL> self . msgsend2matched = Counter ( ) <EOL> self . event2timeouts = Counter ( ) <EOL> self . msgrecv2timeouts = Counter ( ) <EOL> self . msgsend2timeouts = Counter ( ) <EOL> self . replay_start = None <EOL> self . record_start = None <EOL> def start_replay ( self , event ) : <EOL> self . replay_start = time . time ( ) <EOL> self . record_start = event . time . as_float ( ) <EOL> def time ( self , event ) : <EOL> return format_time ( time . time ( ) - self . replay_start ) + "<STR_LIT:U+0020>" + format_time ( event . time . as_float ( ) - self . record_start ) <EOL> def event_matched ( self , event ) : <EOL> msg . replay_event_success ( self . time ( event ) + "<STR_LIT>" + str ( event ) ) <EOL> self . event2matched [ event . __class__ . __name__ ] += <NUM_LIT:1> <EOL> if event . __class__ . __name__ == "<STR_LIT>" : <EOL> pkt_class = event . get_packet ( ) . __class__ . __name__ <EOL> self . msgrecv2matched [ pkt_class ] += <NUM_LIT:1> <EOL> if event . __class__ . __name__ == "<STR_LIT>" : <EOL> pkt_class = event . get_packet ( ) . __class__ . __name__ <EOL> self . msgsend2matched [ pkt_class ] += <NUM_LIT:1> <EOL> def event_timed_out ( self , event ) : <EOL> msg . replay_event_timeout ( self . time ( event ) + "<STR_LIT>" + str ( event ) ) <EOL> self . event2timeouts [ event . __class__ . __name__ ] += <NUM_LIT:1> <EOL> if event . __class__ . __name__ == "<STR_LIT>" : <EOL> pkt_class = event . get_packet ( ) . __class__ . __name__ <EOL> self . msgrecv2timeouts [ pkt_class ] += <NUM_LIT:1> <EOL> if event . __class__ . __name__ == "<STR_LIT>" : <EOL> pkt_class = event . get_packet ( ) . __class__ . __name__ <EOL> self . msgsend2timeouts [ pkt_class ] += <NUM_LIT:1> <EOL> def sorted_match_counts ( self ) : <EOL> for e , count in sorted ( self . event2matched . items ( ) , <EOL> key = operator . itemgetter ( <NUM_LIT:1> ) ) : <EOL> yield ( e , count ) <EOL> def sorted_timeout_counts ( self ) : <EOL> for e , count in sorted ( self . event2timeouts . items ( ) , <EOL> key = operator . itemgetter ( <NUM_LIT:1> ) ) : <EOL> yield ( e , count , ) <EOL> def get_matches_dict ( self ) : <EOL> d = dict ( self . event2matched ) <EOL> if '<STR_LIT>' in d : <EOL> total = d [ '<STR_LIT>' ] <EOL> d [ '<STR_LIT>' ] = dict ( self . msgrecv2matched ) <EOL> d [ '<STR_LIT>' ] [ '<STR_LIT>' ] = total <EOL> if '<STR_LIT>' in d : <EOL> total = d [ '<STR_LIT>' ] <EOL> d [ '<STR_LIT>' ] = dict ( self . msgsend2matched ) <EOL> d [ '<STR_LIT>' ] [ '<STR_LIT>' ] = total <EOL> return d <EOL> def get_timeouts_dict ( self ) : <EOL> d = dict ( self . event2timeouts ) <EOL> if '<STR_LIT>' in d : <EOL> total = d [ '<STR_LIT>' ] <EOL> d [ '<STR_LIT>' ] = dict ( self . msgrecv2timeouts ) <EOL> d [ '<STR_LIT>' ] [ '<STR_LIT>' ] = total <EOL> if '<STR_LIT>' in d : <EOL> total = d [ '<STR_LIT>' ] <EOL> d [ '<STR_LIT>' ] = dict ( self . msgsend2timeouts ) <EOL> d [ '<STR_LIT>' ] [ '<STR_LIT>' ] = total <EOL> return d <EOL> def __str__ ( self ) : <EOL> total_matched = sum ( self . event2matched . values ( ) ) <EOL> total_timeouts = sum ( self . event2timeouts . values ( ) ) <EOL> s = [ ] <EOL> s . append ( "<STR_LIT>" % ( total_matched , <EOL> total_timeouts ) ) <EOL> s . append ( "<STR_LIT>" ) <EOL> for e , count in self . sorted_match_counts ( ) : <EOL> s . append ( "<STR_LIT>" % ( e , count , ) ) <EOL> if e == "<STR_LIT>" : <EOL> for pkt_class , c in self . msgrecv2matched . iteritems ( ) : <EOL> s . append ( "<STR_LIT>" % ( pkt_class , c ) ) <EOL> if e == "<STR_LIT>" : <EOL> for pkt_class , c in self . msgsend2matched . iteritems ( ) : <EOL> s . append ( "<STR_LIT>" % ( pkt_class , c ) ) <EOL> s . append ( "<STR_LIT>" ) <EOL> for e , count in self . sorted_timeout_counts ( ) : <EOL> s . append ( "<STR_LIT>" % ( e , count , ) ) <EOL> if e == "<STR_LIT>" : <EOL> for pkt_class , c in self . msgrecv2timeouts . iteritems ( ) : <EOL> s . append ( "<STR_LIT>" % ( pkt_class , c ) ) <EOL> if e == "<STR_LIT>" : <EOL> for pkt_class , c in self . msgsend2timeouts . iteritems ( ) : <EOL> s . append ( "<STR_LIT>" % ( pkt_class , c ) ) <EOL> return "<STR_LIT>" . join ( s ) <EOL> class EventSchedulerBase ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _input_logger = None <EOL> def set_input_logger ( self , input_logger ) : <EOL> self . _input_logger = input_logger <EOL> def _log_event ( self , event , ** kws ) : <EOL> if self . _input_logger is not None : <EOL> self . _input_logger . log_input_event ( event , ** kws ) <EOL> class DumbEventScheduler ( EventSchedulerBase ) : <EOL> kwargs = set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def __init__ ( self , simulation , epsilon_seconds = <NUM_LIT:0.0> , sleep_interval_seconds = <NUM_LIT> ) : <EOL> super ( DumbEventScheduler , self ) . __init__ ( ) <EOL> self . simulation = simulation <EOL> self . epsilon_seconds = epsilon_seconds <EOL> self . sleep_interval_seconds = sleep_interval_seconds <EOL> self . last_event = None <EOL> self . stats = EventSchedulerStats ( ) <EOL> def schedule ( self , event ) : <EOL> if self . last_event : <EOL> rec_delta = ( event . time . as_float ( ) - self . last_event . time . as_float ( ) ) <EOL> if rec_delta > <NUM_LIT:0> : <EOL> log . info ( "<STR_LIT>" % ( rec_delta * <NUM_LIT:1000> ) ) <EOL> self . simulation . io_master . sleep ( rec_delta ) <EOL> else : <EOL> self . stats . start_replay ( event ) <EOL> log . debug ( "<STR_LIT>" % <EOL> ( str ( event ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) , self . epsilon_seconds * <NUM_LIT:1000> ) ) <EOL> now = time . time ( ) <EOL> end = now + self . epsilon_seconds <EOL> proceed = False <EOL> while True : <EOL> now = time . time ( ) <EOL> if event . proceed ( self . simulation ) : <EOL> proceed = True <EOL> break <EOL> elif now > end : <EOL> break <EOL> self . simulation . io_master . select ( self . sleep_interval_seconds ) <EOL> if proceed : <EOL> event . timed_out = False <EOL> self . stats . event_matches ( event ) <EOL> else : <EOL> event . timed_out = True <EOL> self . stats . event_timed_out ( event ) <EOL> event . replay_time = SyncTime . now ( ) <EOL> self . _log_event ( event ) <EOL> self . last_event = event <EOL> class EventScheduler ( EventSchedulerBase ) : <EOL> '''<STR_LIT>''' <EOL> kwargs = set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def __init__ ( self , simulation , speedup = <NUM_LIT:1.0> , delay_input_events = True , <EOL> initial_wait = <NUM_LIT:0.5> , epsilon_seconds = <NUM_LIT:0.5> , sleep_interval_seconds = <NUM_LIT> , <EOL> sleep_continuation = None , select_continuation = None , assertion_checking = False ) : <EOL> super ( EventScheduler , self ) . __init__ ( ) <EOL> self . simulation = simulation <EOL> self . speedup = speedup <EOL> self . delay_input_events = delay_input_events <EOL> self . last_real_time = None <EOL> self . last_rec_time = None <EOL> self . initial_wait = initial_wait <EOL> self . epsilon_seconds = epsilon_seconds <EOL> self . sleep_interval_seconds = sleep_interval_seconds <EOL> self . started = False <EOL> self . stats = EventSchedulerStats ( ) <EOL> self . assertion_checking = assertion_checking <EOL> self . sleep_continuation = sleep_continuation <EOL> if sleep_continuation is None : <EOL> self . sleep_continuation = self . simulation . io_master . sleep <EOL> self . select_continuation = select_continuation <EOL> if select_continuation is None : <EOL> self . select_continuation = self . simulation . io_master . select <EOL> def schedule ( self , event ) : <EOL> if not self . started : <EOL> self . stats . start_replay ( event ) <EOL> self . started = True <EOL> if isinstance ( event , InputEvent ) : <EOL> self . inject_input ( event ) <EOL> elif isinstance ( event , InternalEvent ) : <EOL> if event . whitelisted ( ) : <EOL> self . delay_whitelisted_internal_event ( event ) <EOL> else : <EOL> self . wait_for_internal ( event ) <EOL> self . update_event_time ( event ) <EOL> self . _log_event ( event ) <EOL> def inject_input ( self , event ) : <EOL> if self . delay_input_events : <EOL> wait_time_seconds = self . wait_time ( event ) <EOL> if wait_time_seconds > <NUM_LIT> : <EOL> log . debug ( "<STR_LIT>" % <EOL> ( str ( event ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) , ( wait_time_seconds ) * <NUM_LIT:1000> ) ) <EOL> self . sleep_continuation ( wait_time_seconds ) <EOL> log . debug ( "<STR_LIT>" , event ) <EOL> end = event . time . as_float ( ) <EOL> self . _poll_event ( event , end ) <EOL> def delay_whitelisted_internal_event ( self , event ) : <EOL> wait_time_seconds = self . wait_time ( event ) <EOL> log . debug ( "<STR_LIT>" % <EOL> ( repr ( event ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) , wait_time_seconds * <NUM_LIT:1000> ) ) <EOL> self . sleep_continuation ( wait_time_seconds ) <EOL> self . stats . event_matched ( event ) <EOL> self . update_event_time ( event ) <EOL> event . replay_time = SyncTime . now ( ) <EOL> def wait_for_internal ( self , event ) : <EOL> wait_time_seconds = self . wait_time ( event ) <EOL> start = time . time ( ) <EOL> end = start + wait_time_seconds - <NUM_LIT> + self . epsilon_seconds <EOL> if event . timeout_disallowed : <EOL> end = <NUM_LIT> <EOL> log . debug ( "<STR_LIT>" % <EOL> ( repr ( event ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) ) ) <EOL> else : <EOL> log . debug ( "<STR_LIT>" % <EOL> ( repr ( event ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) , self . epsilon_seconds * <NUM_LIT:1000> ) ) <EOL> self . _poll_event ( event , end ) <EOL> def _poll_event ( self , event , end_time ) : <EOL> proceed = False <EOL> while True : <EOL> now = time . time ( ) <EOL> if event . proceed ( self . simulation ) : <EOL> proceed = True <EOL> break <EOL> elif now > end_time : <EOL> break <EOL> self . select_continuation ( self . sleep_interval_seconds ) <EOL> if proceed : <EOL> event . timed_out = False <EOL> self . stats . event_matched ( event ) <EOL> self . update_event_time ( event ) <EOL> else : <EOL> event . timed_out = True <EOL> self . stats . event_timed_out ( event ) <EOL> event . replay_time = SyncTime . now ( ) <EOL> def update_event_time ( self , event ) : <EOL> """<STR_LIT>""" <EOL> self . last_real_time = time . time ( ) <EOL> self . last_rec_time = event . time <EOL> def wait_time ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if not self . last_real_time : <EOL> return self . initial_wait <EOL> rec_delta = ( event . time . as_float ( ) - self . last_rec_time . as_float ( ) ) / self . speedup <EOL> real_delta = time . time ( ) - self . last_real_time <EOL> to_wait = rec_delta - real_delta <EOL> if self . assertion_checking and to_wait > <NUM_LIT> : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( to_wait , str ( event ) ) ) <EOL> if self . assertion_checking and to_wait < - <NUM_LIT> : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( to_wait , str ( event ) ) ) <EOL> return max ( to_wait , <NUM_LIT:0> ) </s>
