Esempio n. 1
0
def test_two_branch_cascading_schedule_installation_4(motes):
    # even tree with random pick
    params = {
        'withJoin': False,
        'topology': 'twoBranch',
        'cascadingScheduling': True,
        'schedulingMode': 'random-pick'
    }

    settings = SimSettings.SimSettings(**params)
    engine = SimEngine.SimEngine()
    motes1 = engine.motes
    engine.destroy()
    settings.destroy()

    settings = SimSettings.SimSettings(**params)
    engine = SimEngine.SimEngine()
    motes2 = engine.motes
    engine.destroy()
    settings.destroy()

    assert len(motes1) == len(motes2)
    prev_ret = True
    for i, v in enumerate(motes1):
        assert len(motes1[i].schedule) == len(motes2[i].schedule)
        for j, cell in enumerate(motes1[i].schedule):
            if j not in motes1[i].schedule:
                continue

            try:
                cell1 = motes1[i].schedule[j]
                cell2 = motes2[i].schedule[j]
            except KeyError:
                ret = False
                break

            if type(cell1['neighbor']) is list:
                ret = (cell1['ch'] == cell2['ch']
                       and cell1['dir'] == Mote.DIR_TXRX_SHARED
                       and cell1['dir'] == cell2['dir']
                       and (sorted(map(lambda x: x.id, cell1['neighbor']))
                            == sorted(map(lambda x: x.id, cell2['neighbor']))))
            else:
                ret = (cell1['ch'] == cell2['ch']
                       and cell1['dir'] == cell2['dir']
                       and cell1['neighbor'].id == cell2['neighbor'].id)

            ret = prev_ret and ret

        if ret is False:
            break

    # all schedules must not be the same
    assert ret is False
Esempio n. 2
0
def runSimsSequentially(params):

    (cpuID, numRuns, options, verbose) = params

    # record simulation start time
    simStartTime = time.time()

    # compute all the simulation parameter combinations
    combinationKeys = sorted(
        [k for (k, v) in options.items() if type(v) == list])
    simParams = []
    for p in itertools.product(*[options[k] for k in combinationKeys]):
        simParam = {}
        for (k, v) in zip(combinationKeys, p):
            simParam[k] = v
        for (k, v) in options.items():
            if k not in simParam:
                simParam[k] = v
        simParams += [simParam]

    # run a simulation for each set of simParams
    for (simParamNum, simParam) in enumerate(simParams):

        # record run start time
        runStartTime = time.time()

        # run the simulation runs
        for runNum in xrange(numRuns):

            # print
            output = 'parameters {0}/{1}, run {2}/{3}'.format(
                simParamNum + 1, len(simParams), runNum + 1, numRuns)
            printOrLog(cpuID, output, verbose)

            # create singletons
            settings = SimSettings.SimSettings(cpuID=cpuID,
                                               runNum=runNum,
                                               **simParam)
            settings.setStartTime(runStartTime)
            settings.setCombinationKeys(combinationKeys)
            simengine = SimEngine.SimEngine(cpuID=cpuID, runNum=runNum)
            simstats = SimStats.SimStats(cpuID=cpuID,
                                         runNum=runNum,
                                         verbose=verbose)

            # start simulation run
            simengine.start()

            # wait for simulation run to end
            simengine.join()

            # destroy singletons
            simstats.destroy()
            simengine.destroy()
            settings.destroy()

        # print
        output = 'simulation ended after {0:.0f}s.'.format(time.time() -
                                                           simStartTime)
        printOrLog(cpuID, output, verbose)
Esempio n. 3
0
def test_event_order_at_same_asn():
    # if we have multiple events scheduled at the same ASN with the
    # same intraSlotOrder, their callbacks should be executed in order
    # of insertion (FIFO)

    INTRA_SLOT_ORDER = d.INTRASLOTORDER_STACKTASKS
    NUM_TEST_RUNS = 1000
    result = []

    def _callback_1():
        result.append(1)

    def _callback_2():
        result.append(2)

    def _callback_3():
        result.append(3)

    for _ in range(NUM_TEST_RUNS):
        result = []
        engine = SimEngine.DiscreteEventEngine()

        # schedule events at ASN 1
        engine.scheduleAtAsn(1, _callback_1, 'first_event', INTRA_SLOT_ORDER)
        engine.scheduleAtAsn(1, _callback_2, 'second_event', INTRA_SLOT_ORDER)
        engine.scheduleAtAsn(1, _callback_3, 'third_event', INTRA_SLOT_ORDER)

        engine.start()
        engine.join()

        assert result == [1, 2, 3]
Esempio n. 4
0
    def create_motes(num_motes, **kwargs):

        # Mote needs SimEngine has already instantiated when its constructor is
        # called
        params = {'numMotes': num_motes, 'topology': 'linear'}
        params['linearTopologyStaticScheduling'] = True

        # there are prerequisite parameters for Mote.Mote()
        params['pkPeriod'] = 0
        params['minRssi'] = 0
        params['withJoin'] = False
        params['slotframeLength'] = 101
        params['slotDuration'] = 0.010
        params['bayesianBroadcast'] = False

        # disable interference model
        params['noInterference'] = True

        # override by kwargs is any specified
        if kwargs:
            params.update(kwargs)

        settings = SimSettings.SimSettings(**params)
        engine = SimEngine.SimEngine()

        def fin():

            engine.destroy()
            settings.destroy()

        request.addfinalizer(fin)

        return engine.motes
Esempio n. 5
0
def main():

    # instantiate a SimEngine object
    simengine = SimEngine.SimEngine(loghandler)
    simengine.start()

    # instantiate the CLI interface
    cliHandler = SimCli.SimCli(simengine)
    cliHandler.start()
Esempio n. 6
0
    def create_sim_engine(
            diff_config                                = {},
            force_initial_routing_and_scheduling_state = False,
            run_id                                     = None
        ):
        
        engine = None
        sim_log = None
        sim_settings = None

        # add a finalizer
        def fin():
            if engine:
                engine.connectivity.destroy()
                engine.destroy()
            if sim_log:
                sim_log.destroy()
            if sim_settings:
                sim_settings.destroy()
        request.addfinalizer(fin)
        
        # get default configuration
        sim_config = SimConfig.SimConfig(u.CONFIG_FILE_PATH)
        config = sim_config.settings['regular']
        assert 'exec_numMotes' not in config
        config['exec_numMotes'] = sim_config.settings['combination']['exec_numMotes'][0]

        # update default configuration with parameters
        for (k,v) in list(diff_config.items()):
            assert k in config
        config.update(**diff_config)

        # create sim settings
        sim_settings = SimSettings.SimSettings(**config)
        sim_settings.setLogDirectory(
            '{0}-{1:03d}'.format(
                time.strftime('%Y%m%d-%H%M%S'),
                int(round(time.time() * 1000))%1000
            )
        )
        sim_settings.setCombinationKeys([])

        # create sim log
        sim_log = SimEngine.SimLog.SimLog()
        sim_log.set_log_filters('all') # do not log

        # create sim engine
        engine = SimEngine.SimEngine(run_id=run_id)
        
        # force initial routing and schedule, if appropriate
        if force_initial_routing_and_scheduling_state:
            set_initial_routing_and_scheduling_state(engine)

        return engine
Esempio n. 7
0
def runSims(options):

    # compute all the simulation parameter combinations
    combinationKeys = sorted(
        [k for (k, v) in options.items() if type(v) == list])
    print options.items()
    simParams = []
    for p in itertools.product(*[options[k] for k in combinationKeys]):
        simParam = {}
        for (k, v) in zip(combinationKeys, p):
            simParam[k] = v
        for (k, v) in options.items():
            if k not in simParam:
                simParam[k] = v
        simParams += [simParam]

    # run a simulation for each set of simParams
    for (simParamNum, simParam) in enumerate(simParams):

        # print
        if simParam['processID'] == None:
            print('parameters {0}/{1}'.format(simParamNum + 1, len(simParams)))
        else:
            print('parameters {0}/{1}, processID {2}'.format(
                simParamNum + 1, len(simParams), simParam['processID']))

        # record run start time
        runStartTime = time.time()

        # run the simulation runs
        for runNum in xrange(simParam['numRuns']):

            # print
            if simParam['processID'] == None:
                print('   run {0}/{1}'.format(runNum + 1, simParam['numRuns']))

            # create singletons
            settings = SimSettings.SimSettings(**simParam)
            settings.setStartTime(runStartTime)
            settings.setCombinationKeys(combinationKeys)
            simengine = SimEngine.SimEngine(runNum)
            simstats = SimStats.SimStats(runNum, simParam['numRuns'])
            #passing the number of runs to enable the display  of them while the code is runnig

            # start simulation run
            simengine.start()

            # wait for simulation run to end
            simengine.join()

            # destroy singletons
            simstats.destroy()
            simengine.destroy()
            settings.destroy()
Esempio n. 8
0
def test_two_branch_cascading_schedule_installation(motes):
    # even tree *without* random pick
    params = {
        'withJoin': False,
        'topology': 'twoBranch',
        'cascadingScheduling': True
    }

    settings = SimSettings.SimSettings(**params)
    engine = SimEngine.SimEngine()
    motes1 = engine.motes
    engine.destroy()
    settings.destroy()

    settings = SimSettings.SimSettings(**params)
    engine = SimEngine.SimEngine()
    motes2 = engine.motes
    engine.destroy()
    settings.destroy()

    assert len(motes1) == len(motes2)
    for i, v in enumerate(motes1):
        assert len(motes1[i].schedule) == len(motes2[i].schedule)
        for j, cell in enumerate(motes1[i].schedule):
            if j not in motes1[i].schedule:
                continue

            cell1 = motes1[i].schedule[j]
            cell2 = motes2[i].schedule[j]

            if type(cell1['neighbor']) is list:
                ret = (cell1['ch'] == cell2['ch']
                       and cell1['dir'] == Mote.DIR_TXRX_SHARED
                       and cell1['dir'] == cell2['dir']
                       and (sorted(map(lambda x: x.id, cell1['neighbor']))
                            == sorted(map(lambda x: x.id, cell2['neighbor']))))
            else:
                ret = (cell1['ch'] == cell2['ch']
                       and cell1['dir'] == cell2['dir']
                       and cell1['neighbor'].id == cell2['neighbor'].id)
            assert ret is True
Esempio n. 9
0
def test_event_execution_order(repeat4times):

    # create engine
    engine = SimEngine.DiscreteEventEngine()
    engine.scheduleAtAsn(
        asn=10,
        cb=engine._actionEndSim,
        uniqueTag=('engine', '_actionEndSim'),
        intraSlotOrder=3,
    )
    stateoftest = StateOfTest()

    # schedule events (out of order)
    engine.scheduleAtAsn(
        asn=1,
        cb=stateoftest._cb_asn_1_1,
        uniqueTag=('stateoftest', '_cb_asn_1_1'),
        intraSlotOrder=1,
    )
    engine.scheduleAtAsn(
        asn=2,
        cb=stateoftest._cb_asn_2_0,
        uniqueTag=('stateoftest', '_cb_asn_2_0'),
        intraSlotOrder=0,
    )
    engine.scheduleAtAsn(
        asn=1,
        cb=stateoftest._cb_asn_1_2,
        uniqueTag=('stateoftest', '_cb_asn_1_2'),
        intraSlotOrder=2,
    )

    # run engine, run until done
    assert not engine.is_alive()
    engine.start()
    engine.join()
    assert not engine.is_alive()

    # verify we got the right events
    assert stateoftest.events == ['1.1', '1.2', '2.0']
Esempio n. 10
0
def runSimCombinations(params):
    """
    Runs simulations for all combinations of simulation settings.
    This function may run independently on different CPUs.
    """

    cpuID = params['cpuID']
    pid = params['pid']
    numRuns = params['numRuns']
    first_run = params['first_run']
    verbose = params['verbose']
    config_data = params['config_data']

    simconfig = SimConfig.SimConfig(configdata=config_data)

    # record simulation start time
    simStartTime = time.time()

    # compute all the simulation parameter combinations
    combinationKeys = simconfig.settings.combination.keys()
    simParams = []
    for p in itertools.product(
            *[simconfig.settings.combination[k] for k in combinationKeys]):
        simParam = {}
        for (k, v) in zip(combinationKeys, p):
            simParam[k] = v
        for (k, v) in simconfig.settings.regular.items():
            if k not in simParam:
                simParam[k] = v
        simParams += [simParam]

    # run a simulation for each set of simParams
    for (simParamNum, simParam) in enumerate(simParams):

        # run the simulation runs
        for run_id in range(first_run, first_run + numRuns):

            # printOrLog
            output = 'parameters {0}/{1}, run {2}/{3}'.format(
                simParamNum + 1, len(simParams), run_id + 1 - first_run,
                numRuns)
            printOrLog(cpuID, pid, output, verbose)

            # create singletons
            settings = SimSettings.SimSettings(cpuID=cpuID,
                                               run_id=run_id,
                                               **simParam)
            settings.setLogDirectory(simconfig.get_log_directory_name())
            settings.setCombinationKeys(combinationKeys)
            simlog = SimLog.SimLog()
            simlog.set_log_filters(simconfig.logging)

            parentlog = ParentLogs.ParentLogs()
            parentlog.set_log_filters(simconfig.logging)
            simengine = SimEngine.SimEngine(run_id=run_id, verbose=verbose)

            # start simulation run
            simengine.start()

            # wait for simulation run to end
            simengine.join()

            # destroy singletons
            simlog.destroy()
            #parentlog.destroy()
            simengine.destroy()
            Connectivity.Connectivity().destroy()
            settings.destroy()  # destroy last, Connectivity needs it

        # printOrLog
        output = 'simulation ended after {0:.0f}s ({1} runs).'.format(
            time.time() - simStartTime, numRuns * len(simParams))
        printOrLog(cpuID, pid, output, verbose)
Esempio n. 11
0
 def engine(self):
     return SimEngine.SimEngine(failIfNotInit=True)
Esempio n. 12
0
def test_create_start_destroy_engine(repeat4times):
    engine = SimEngine.DiscreteEventEngine()
    engine.start()
    engine.join()
Esempio n. 13
0
def start(settings, log_notification_filter='all', stderr_redirect=True):
    global _sim_engine
    global _elapsed_minutes

    sim_settings = None
    sim_log = None
    ret_val = {}

    if _sim_engine is not None:
        return {
            'status': RETURN_STATUS_FAILURE,
            'message': 'SimEngine has been started already',
            'trace': None
        }

    try:
        sim_settings = SimSettings.SimSettings(
            cpuID=0, run_id=0, log_root_dir=backend.SIM_DATA_PATH, **settings)
        start_time = time.time()
        sim_settings.setLogDirectory('{0}-{1:03d}'.format(
            time.strftime("%Y%m%d-%H%M%S", time.localtime(start_time)),
            int(round(start_time * 1000)) % 1000))
        sim_settings.setCombinationKeys(DUMMY_COMBINATION_KEYS)

        sim_log = SimLog.SimLog()
        sim_log.set_log_filters(SIM_LOG_FILTERS)
        _overwrite_sim_log_log(log_notification_filter)

        _save_config_json(sim_settings,
                          saving_settings={
                              'combination': {},
                              'regular': settings.copy()
                          })

        crash_report_path = os.path.join(sim_settings.logRootDirectoryPath,
                                         sim_settings.logDirectory,
                                         'crash_report.log')
        if stderr_redirect is True:
            _redirect_stderr(redirect_to=open(crash_report_path, 'w'))

        _sim_engine = SimEngine.SimEngine()
        _elapsed_minutes = 0
        _overwrite_sim_engine_actionEndSlotframe()

        # start and wait until the simulation ends
        _sim_engine.start()
        _sim_engine.join()
    except Exception as e:
        ret_val['status'] = RETURN_STATUS_FAILURE
        ret_val['message'] = str(e)
        ret_val['trace'] = traceback.format_exc()
    else:
        if _sim_engine.getAsn() == (sim_settings.exec_numSlotframesPerRun *
                                    sim_settings.tsch_slotframeLength):
            ret_val['status'] = RETURN_STATUS_SUCCESS
            # rename .dat file and remove the subdir
            dat_file_path = sim_settings.getOutputFile()
            subdir_path = os.path.dirname(dat_file_path)
            new_file_name = subdir_path + '.dat'
            os.rename(dat_file_path, new_file_name)
            os.rmdir(subdir_path)
        else:
            # simulation is aborted
            ret_val['status'] = RETURN_STATUS_ABORTED
    finally:
        # housekeeping for crash_report and stderr
        if stderr_redirect is True:
            crash_report = _restore_stderr()
            crash_report.close()
            if os.stat(crash_report.name).st_size == 0:
                os.remove(crash_report.name)
            else:
                ret_val['crash_report_path'] = crash_report.name

        # cleanup
        if _sim_engine is None:
            if sim_settings is not None:
                sim_settings.destroy()
            if sim_log is not None:
                sim_log.destroy()
        else:
            _destroy_sim()

    return ret_val