def _init_additional_local_variables(self): self.settings = SimSettings.SimSettings() # set random seed if self.settings.exec_randomSeed == 'random': self.random_seed = random.randint(0, sys.maxint) elif self.settings.exec_randomSeed == 'context': # with context for exec_randomSeed, an MD5 value of # 'startTime-hostname-run_id' is used for a random seed startTime = SimConfig.SimConfig.get_startTime() if startTime is None: startTime = time.time() context = (platform.uname()[1], str(startTime), str(self.run_id)) md5 = hashlib.md5() md5.update('-'.join(context)) self.random_seed = int(md5.hexdigest(), 16) % sys.maxint else: assert isinstance(self.settings.exec_randomSeed, int) self.random_seed = self.settings.exec_randomSeed # apply the random seed; log the seed after self.log is initialized random.seed(a=self.random_seed) if self.settings.motes_eui64: eui64_table = self.settings.motes_eui64[:] if len(eui64_table) < self.settings.exec_numMotes: eui64_table.extend( [None] * (self.settings.exec_numMotes - len(eui64_table))) else: eui64_table = [None] * self.settings.exec_numMotes self.motes = [ Mote.Mote.Mote(id, eui64) for id, eui64 in zip( range(self.settings.exec_numMotes), eui64_table) ] eui64_list = set([mote.get_mac_addr() for mote in self.motes]) if len(eui64_list) != len(self.motes): assert len(eui64_list) < len(self.motes) raise ValueError('given motes_eui64 causes dulicates') self.connectivity = Connectivity.Connectivity() self.log = SimLog.SimLog().log SimLog.SimLog().set_simengine(self) # log the random seed self.log(SimLog.LOG_SIMULATOR_RANDOM_SEED, {'value': self.random_seed}) # flush buffered logs, which are supposed to be 'config' and # 'random_seed' lines, right now. This could help, for instance, when a # simulation is stuck by an infinite loop without writing these # 'config' and 'random_seed' to a log file. SimLog.SimLog().flush() # select dagRoot self.motes[self.DAGROOT_ID].setDagRoot() # boot all motes for i in range(len(self.motes)): self.motes[i].boot()
def _init_additional_local_variables(self): self.settings = SimSettings.SimSettings() # set random seed if self.settings.exec_randomSeed == 'random': self.random_seed = random.randint(0, sys.maxint) elif self.settings.exec_randomSeed == 'context': # with context for exec_randomSeed, an MD5 value of # 'startTime-hostname-run_id' is used for a random seed startTime = SimConfig.SimConfig.get_startTime() if startTime is None: startTime = time.time() context = (platform.uname()[1], str(startTime), str(self.run_id)) md5 = hashlib.md5() md5.update('-'.join(context)) self.random_seed = int(md5.hexdigest(), 16) % sys.maxint else: assert isinstance(self.settings.exec_randomSeed, int) self.random_seed = self.settings.exec_randomSeed # apply the random seed; log the seed after self.log is initialized random.seed(a=self.random_seed) self.motes = [ Mote.Mote.Mote(m) for m in range(self.settings.exec_numMotes) ] self.connectivity = Connectivity.Connectivity() self.log = SimLog.SimLog().log SimLog.SimLog().set_simengine(self) # log the random seed self.log(SimLog.LOG_SIMULATOR_RANDOM_SEED, {'value': self.random_seed}) # log the mote EUI addresses and coordinates if using random connectivity motes = {} for mote in self.motes: motes[mote.id] = { 'mote_id': mote.id, 'mac': mote.get_mac_addr(), } if hasattr(self.connectivity, 'coordinates'): motes[mote.id]['coordinates'] = self.connectivity.coordinates[ mote.id] self.log(SimLog.LOG_MOTES, {'motes': motes}) # flush buffered logs, which are supposed to be 'config' and # 'random_seed' lines, right now. This could help, for instance, when a # simulation is stuck by an infinite loop without writing these # 'config' and 'random_seed' to a log file. SimLog.SimLog().flush() # select dagRoot self.motes[self.DAGROOT_ID].setDagRoot() # boot all motes for i in range(len(self.motes)): self.motes[i].boot()
def _init_additional_local_variables(self): self.settings = SimSettings.SimSettings() # set random seed if self.settings.exec_randomSeed == 'random': self.random_seed = random.randint(0, sys.maxint) elif self.settings.exec_randomSeed == 'context': # with context for exec_randomSeed, an MD5 value of # 'startTime-hostname-run_id' is used for a random seed startTime = SimConfig.SimConfig.get_startTime() if startTime is None: startTime = time.time() context = (platform.uname()[1], str(startTime), str(self.run_id)) md5 = hashlib.md5() md5.update('-'.join(context)) self.random_seed = int(md5.hexdigest(), 16) % sys.maxint else: assert isinstance(self.settings.exec_randomSeed, int) self.random_seed = self.settings.exec_randomSeed # apply the random seed; log the seed after self.log is initialized random.seed(a=self.random_seed) self.motes = [Mote.Mote.Mote(m) for m in range(self.settings.exec_numMotes)] self.connectivity = Connectivity.Connectivity() self.log = SimLog.SimLog().log SimLog.SimLog().set_simengine(self) # log the random seed self.log( SimLog.LOG_SIMULATOR_RANDOM_SEED, { 'value': self.random_seed } ) # select dagRoot self.motes[self.DAGROOT_ID].setDagRoot() # boot all motes for i in range(len(self.motes)): self.motes[i].boot()
def run(self): """ loop through events """ try: # additional routine self._routine_thread_started() # consume events until self.goOn is False while self.goOn: with self.dataLock: # abort simulation when no more events if not self.events: break # make sure we are in the future (a, b, cb, c) = self.events[0] if c[1] != '_actionPauseSim': assert self.events[0][0] >= self.asn # update the current ASN self.asn = self.events[0][0] # find callbacks for this ASN cbs = [] while True: if (not self.events) or (self.events[0][0] != self.asn): break (_, _, cb, _) = self.events.pop(0) cbs += [cb] # call the callbacks (outside the dataLock) for cb in cbs: cb() except Exception as e: # thread crashed # record the exception self.exc = e # additional routine self._routine_thread_crashed() # print output = [] output += [''] output += ['=============================='] output += [''] output += ['CRASH in {0}!'.format(self.name)] output += [''] output += [traceback.format_exc()] output += ['=============================='] output += [''] output += ['The following settings are used:'] output += [''] for k, v in SimSettings.SimSettings().__dict__.iteritems(): if ( (k == 'exec_randomSeed') and (v in ['random', 'context']) ): # put the random seed value in output # exec_randomSeed: random v = '{0} ({1})'.format(v, self.random_seed) output += ['{0}: {1}'.format(str(k), str(v))] output += [''] output += ['=============================='] output += [''] output = '\n'.join(output) sys.stderr.write(output) # flush all the buffered log data SimLog.SimLog().flush() else: # thread ended (gracefully) # no exception self.exc = None # additional routine self._routine_thread_ended() finally: # destroy this singleton cls = type(self) cls._instance = None cls._init = False
def run(self): """ loop through events """ try: # additional routine self._routine_thread_started() # consume events until self.goOn is False while self.goOn: with self.dataLock: # abort simulation when no more events if not self.events: break # make sure we are in the future (a, b, cb, c) = self.events[0] if c[1] != '_actionPauseSim': assert self.events[0][0] >= self.asn # update the current ASN self.asn = self.events[0][0] # find callbacks for this ASN cbs = [] while True: if (not self.events) or (self.events[0][0] != self.asn): break (_, _, cb, _) = self.events.pop(0) cbs += [cb] # call the callbacks (outside the dataLock) for cb in cbs: cb() except Exception as e: # thread crashed # record the exception self.exc = e # additional routine self._routine_thread_crashed() # print output = [] output += [''] output += ['=============================='] output += [''] output += ['CRASH in {0}!'.format(self.name)] output += [''] output += [traceback.format_exc()] output += ['=============================='] output += [''] output += ['The current ASN is {0}'.format(self.asn)] output += ['The log file is {0}'.format( self.settings.getOutputFile() )] output += [''] output += ['=============================='] output += ['config.json to reproduce:'] output += [''] output += [''] output = '\n'.join(output) output += json.dumps( SimConfig.SimConfig.generate_config( settings_dict = self.settings.__dict__, random_seed = self.random_seed ), indent = 4 ) output += '\n\n==============================\n' sys.stderr.write(output) # flush all the buffered log data SimLog.SimLog().flush() ParentLogs.ParentLogs().flush() else: # thread ended (gracefully) # no exception self.exc = None # additional routine self._routine_thread_ended() finally: # destroy this singleton cls = type(self) cls._instance = None cls._init = False
def run(self): """ loop through events """ try: # additional routine self._routine_thread_started() # consume events until self.goOn is False while self.goOn: with self.dataLock: # abort simulation when no more events if not self.events: break # update the current ASN self.asn += 1 if self.asn not in self.events: continue intraSlotOrderKeys = self.events[self.asn].keys() intraSlotOrderKeys.sort() cbs = [] for intraSlotOrder in intraSlotOrderKeys: for uniqueTag, cb in self.events[ self.asn][intraSlotOrder].items(): cbs += [cb] del self.uniqueTagSchedule[uniqueTag] del self.events[self.asn] # call the callbacks (outside the dataLock) for cb in cbs: cb() except Exception as e: # thread crashed # record the exception self.exc = e # additional routine self._routine_thread_crashed() # print output = [] output += [''] output += ['=============================='] output += [''] output += ['CRASH in {0}!'.format(self.name)] output += [''] output += [traceback.format_exc()] output += ['=============================='] output += [''] output += ['The current ASN is {0}'.format(self.asn)] output += [ 'The log file is {0}'.format(self.settings.getOutputFile()) ] output += [''] output += ['=============================='] output += ['config.json to reproduce:'] output += [''] output += [''] output = '\n'.join(output) output += json.dumps(SimConfig.SimConfig.generate_config( settings_dict=self.settings.__dict__, random_seed=self.random_seed), indent=4) output += '\n\n==============================\n' sys.stderr.write(output) # flush all the buffered log data SimLog.SimLog().flush() else: # thread ended (gracefully) # no exception self.exc = None # additional routine self._routine_thread_ended() finally: # destroy this singleton cls = type(self) cls._instance = None cls._init = False