Beispiel #1
0
def demo_15_db_include_pat(config, factors=None):
    dir1 = os.getcwd()
    dst = '.resource'
    create_dir(dst)
    # copy db file created in demo_13
    fdb = 'sim_13_db.sqlite'
    src = dir1 + '/' + config['sim.workspace'] + '/' + fdb
    dir2 = dir1 + '/' + dst
    dst = dir2 + '/' + fdb
    dst = copy_file(src, dst)
    assert os.path.exists(dst)
    db_dir, db_file = os.path.split(dst)

    config['sim.result.file'] = 'result_15_db_include_pat.yaml'
    config['sim.db.file'] = db_file
    config['sim.db.enable'] = True
    config['sim.db.include_pat'] = [db_dir]
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)

    # TODO: verify sim updated db file timestamp

    # assert test used in demo_13
    db = sqlite3.connect(dst)
    assert db.execute('SELECT COUNT() FROM trace').fetchone()[0] == 15
Beispiel #2
0
def test_simulate_factors_overwrite(config):
    config['sim.workspace.overwrite'] = True
    factors = [(['sim.seed'], [[1], [2], [3]])]
    results = simulate_factors(config, factors, TopTest)
    assert os.path.isdir(config['sim.workspace'])
    assert len(results) == 3
    for result in results:
        assert result['sim.exception'] is None
        assert os.path.exists(
            os.path.join(
                result['config']['meta.sim.workspace'],
                result['config']['sim.result.file'],
            ))

    with open(os.path.join(config['sim.workspace'], 'cookie.txt'), 'w') as f:
        f.write('hi')

    factors = [(['sim.seed'], [[1], [2]])]
    results = simulate_factors(config, factors, TopTest)
    assert len(results) == 2
    assert os.path.isdir(config['sim.workspace'])
    for result in results:
        assert result['sim.exception'] is None
        assert os.path.exists(
            os.path.join(
                result['config']['meta.sim.workspace'],
                result['config']['sim.result.file'],
            ))

    assert not os.path.exists(
        os.path.join(config['sim.workspace'], 'cookie.txt'))
    assert set(os.listdir(config['sim.workspace'])) == set(['0', '1'])
Beispiel #3
0
def demo_14_db_persist(config, factors=None):
    config['sim.result.file'] = 'result_14_db_persist.yaml'
    config['sim.db.file'] = 'sim_14_db_persist.sqlite'
    config['sim.db.enable'] = True
    config['sim.db.persist'] = False
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)
    db_path = os.path.join(config['sim.workspace'], config['sim.db.file'])
    assert not os.path.exists(db_path)
Beispiel #4
0
def demo_13_db(config, factors=None):
    config['sim.result.file'] = 'result_13_db.yaml'
    config['sim.db.file'] = 'sim_13_db.sqlite'
    config['sim.db.enable'] = True
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)
    db_path = os.path.join(config['sim.workspace'], config['sim.db.file'])
    assert os.path.exists(db_path)
    db = sqlite3.connect(db_path)
    assert db.execute('SELECT COUNT() FROM trace').fetchone()[0] == 15
Beispiel #5
0
def demo_12_vcd_persist(config, factors=None):
    config['sim.result.file'] = 'result_12_vcd_persist.yaml'
    config['sim.vcd.dump_file'] = 'sim_12_vcd_persist.vcd'
    config['sim.vcd.enable'] = True
    config['sim.vcd.persist'] = False
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)
    dump_path = os.path.join(config['sim.workspace'],
                             config['sim.vcd.dump_file'])
    assert not os.path.exists(dump_path)
Beispiel #6
0
def demo_03_log(config, factors=None):
    config['sim.result.file'] = 'result_03_log.yaml'
    config['sim.log.file'] = 'sim_03_log.log'
    config['test.raise'] = False
    config['sim.log.enable'] = True
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)
    log_path = os.path.join(config['sim.workspace'], config['sim.log.file'])
    assert os.path.exists(log_path)
    last_line = open(log_path).readlines()[-1]
    assert last_line == 'INFO    9.000 us: top.container: 1\n'
Beispiel #7
0
def demo_11_vcd_timescale(config, factors=None):
    config['sim.result.file'] = 'result_11_vcd_timescale.yaml'
    config['sim.vcd.dump_file'] = 'sim_11_vcd_timescale.vcd'
    config['sim.vcd.enable'] = True
    config['sim.vcd.timescale'] = '10 s'
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)
    dump_path = os.path.join(config['sim.workspace'],
                             config['sim.vcd.dump_file'])
    with open(dump_path) as dump:
        vcd_str = dump.read()
        assert '$timescale 10 s' in vcd_str
Beispiel #8
0
def demo_05_log_persist(config, factors=None):
    config['sim.result.file'] = 'result_05_log_persist.yaml'
    config['sim.log.file'] = 'sim_05_log_persist.log'
    config['sim.log.enable'] = True
    config['sim.log.persist'] = False
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)
    log_path = os.path.join(config['sim.workspace'], config['sim.log.file'])
    assert not os.path.exists(log_path)

    config['sim.log.file'] = ''
    simulate(config, Top)
Beispiel #9
0
def demo_08_vcd_stop(config, factors=None):
    config['sim.result.file'] = 'result_08_vcd_stop.yaml'
    config['sim.vcd.dump_file'] = 'sim_08_vcd_stop.vcd'
    config['sim.vcd.enable'] = True
    config['sim.vcd.stop_time'] = '5 us'
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)
    dump_path = os.path.join(config['sim.workspace'],
                             config['sim.vcd.dump_file'])
    with open(dump_path) as dump:
        vcd_str = dump.read()
        assert 'dumpoff' in vcd_str
        assert '#6' not in vcd_str
Beispiel #10
0
def demo_06_vcd(config, factors=None):
    config['sim.result.file'] = 'result_06_vcd.yaml'
    config['sim.vcd.dump_file'] = 'sim_06_vcd.vcd'
    config['sim.vcd.enable'] = True
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)
    dump_path = os.path.join(config['sim.workspace'],
                             config['sim.vcd.dump_file'])
    assert os.path.exists(dump_path)
    with open(dump_path) as dump:
        vcd_str = dump.read()
        for t in range(1, 11):
            assert '#{}\n'.format(t) in vcd_str
Beispiel #11
0
def demo_01_defaults(config, factors=None):
    config['sim.result.file'] = 'result_01_defaults.yaml'
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)
    workspace = config['sim.workspace']
    assert os.path.isdir(workspace)
    assert os.path.exists(os.path.join(workspace, config['sim.result.file']))
    for filename_key in [
            'sim.log.file', 'sim.vcd.dump_file', 'sim.vcd.gtkw_file',
            'sim.db.file'
    ]:
        assert not os.path.exists(os.path.join(workspace,
                                               config[filename_key]))
Beispiel #12
0
def demo_04_log_stderr(config, factors=None):
    config['sim.result.file'] = 'result_04_log_stderr.yaml'
    config['sim.log.enable'] = True
    config['sim.log.file'] = ''

    old_stdout = sys.stdout
    old_stderr = sys.stderr
    redirected_output = sys.stdout = StringIO()
    redirected_error = sys.stderr = StringIO()

    ns_globals = {}
    ns_locals = {}
    out, err, exc = None, None, None

    try:
        if factors:
            exec(simulate_factors(config, factors, Top), ns_globals, ns_locals)
        else:
            exec(simulate(config, Top), ns_globals, ns_locals)
    except:
        import traceback
        exc = traceback.format_exc()

    out = redirected_output.getvalue()
    err = redirected_error.getvalue()

    assert out == ''
    assert err.endswith('INFO    9.000 us: top.container: 1\n')

    # reset outputs to the original values
    sys.stdout = old_stdout
    sys.stderr = old_stderr
Beispiel #13
0
def test_simulate_factors(config):
    factors = [(['sim.seed'], [[1], [2], [3]])]
    results = simulate_factors(config, factors, TopTest)
    assert len(results) == 3
    for result in results:
        assert result['sim.exception'] is None
        assert os.path.exists(
            os.path.join(result['config']['meta.sim.workspace'],
                         result['config']['sim.result.file']))
Beispiel #14
0
def demo_02_exception(config, factors=None):
    config['sim.result.file'] = 'result_02_exception.yaml'
    config['sim.log.file'] = 'sim_02_exception.log'
    config['sim.log.enable'] = True
    config['test.raise'] = True
    try:
        if factors:
            simulate_factors(config, factors, Top)
        else:
            simulate(config, Top)
    except:
        log_path = os.path.join(config['sim.workspace'],
                                config['sim.log.file'])
        assert os.path.exists(log_path)
        with open(log_path) as f:
            log = f.read()
        assert 'ERROR' in log
    finally:
        return
Beispiel #15
0
def demo_10_vcd_stop_then_start(config, factors=None):
    config['sim.result.file'] = 'result_10_vcd_stop_then_start.yaml'
    config['sim.vcd.dump_file'] = 'sim_10_vcd_stop_then_start.vcd'
    config['sim.vcd.enable'] = True
    config['sim.vcd.start_time'] = '6 us'
    config['sim.vcd.stop_time'] = '4 us'
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)
    dump_path = os.path.join(config['sim.workspace'],
                             config['sim.vcd.dump_file'])
    with open(dump_path) as dump:
        vcd_str = dump.read()
        assert 'dumpon' in vcd_str
        assert 'dumpoff' in vcd_str
        assert '#1\n' in vcd_str
        assert '#5' not in vcd_str
        assert '#9' in vcd_str
Beispiel #16
0
def test_simulate_factors_progress_tty(config, capsys):
    config['sim.progress.enable'] = True
    config['sim.duration'] = '10 us'
    factors = [(['sim.seed'], [[1], [2], [3]])]
    with capsys.disabled():
        results = simulate_factors(config, factors, TopTest)
    assert len(results) == 3
    for result in results:
        assert result['sim.exception'] is None
        assert os.path.exists(
            os.path.join(result['config']['meta.sim.workspace'],
                         result['config']['sim.result.file']))
Beispiel #17
0
def test_many_progress_enabled(config, max_width):
    config['sim.progress.enable'] = True
    config['sim.progress.max_width'] = max_width
    factors = [(['sim.seed'], [[1], [2], [3]])]
    results = simulate_factors(config, factors, TopTest)
    for result in results:
        assert result['sim.exception'] is None
        assert result['sim.now'] == 1
        assert result['sim.time'] == 1e-6
        assert result['sim.runtime'] > 0
        assert os.path.exists(
            os.path.join(result['config']['meta.sim.workspace'],
                         result['config']['sim.result.file']))
Beispiel #18
0
def test_simulate_factors_progress(config, capfd):
    config['sim.progress.enable'] = True
    config['sim.duration'] = '10 us'
    factors = [(['sim.seed'], [[1], [2], [3]])]
    results = simulate_factors(config, factors, TopTest)
    assert len(results) == 3
    for result in results:
        assert result['sim.exception'] is None
        assert os.path.exists(
            os.path.join(result['config']['meta.sim.workspace'],
                         result['config']['sim.result.file']))
    out, err = capfd.readouterr()
    assert out == ''
    assert '3 of 3 simulations' in err
Beispiel #19
0
def test_simulate_factors_only_factor(config):
    FACTOR_NUM = 2

    def single_factor_filter_fn(cfg):
        return cfg['meta.sim.index'] == FACTOR_NUM

    factors = [(['sim.seed'], [[1], [2], [3]])]
    results = simulate_factors(config,
                               factors,
                               TopTest,
                               config_filter=single_factor_filter_fn)
    assert len(results) == 1
    for result in results:
        assert result['sim.exception'] is None
        assert result['config']['meta.sim.workspace'] == os.path.join(
            config['sim.workspace'], str(FACTOR_NUM))
        assert os.path.exists(
            os.path.join(result['config']['meta.sim.workspace'],
                         result['config']['sim.result.file']))
Beispiel #20
0
def test_many_progress_no_colorama(config, capsys, no_colorama):
    config['sim.progress.enable'] = True
    factors = [(['sim.seed'], [[1], [2], [3]])]
    with capsys.disabled():
        simulate_factors(config, factors, TopTest)
Beispiel #21
0
def test_many_progress_no_pbar(config, capsys, no_progressbar):
    config['sim.progress.enable'] = True
    config['sim.duration'] = '10 us'
    factors = [(['sim.seed'], [[1], [2], [3]])]
    with capsys.disabled():
        simulate_factors(config, factors, TopTest)
Beispiel #22
0
def main(argv=None):
    """Command line interface for running grocery simulations.

    This function handles command line parsing, configuration setup, and
    launching simulations.

    """
    parser = argparse.ArgumentParser()
    version = get_version()
    parser.add_argument(
        '--version', '-V', action='version', version=version,
        help='Show version and exit.')
    parser.add_argument(
        '--named', '-n', metavar='GROUP', dest='named_configs',
        action='append', default=[],
        help='Use named configuration %(metavar)s.')
    parser.add_argument(
        '--set', '-s', nargs=2, metavar=('KEY', 'VALUE'),
        action='append', default=[], dest='config_overrides',
        help='Override configuration KEY with VALUE expression.')
    parser.add_argument(
        '--factor', '-f', nargs=2, metavar=('KEYS', 'VALUES'),
        action='append', default=[], dest='factors',
        help='Add factorial KEYS with VALUES list of value expressions.')
    parser.add_argument(
        '--config', '-c', metavar='YAML', type=argparse.FileType('r'),
        action='append', default=[], dest='config_files',
        help='Read configuration from YAML file.')
    parser.add_argument(
        '--print-config', action='store_true',
        help='Print configuration and exit.')
    parser.add_argument(
        '--print-named', action='store_true',
        help='Print named configuration groups and exit.')

    extra_argv = shlex.split(os.environ.get('SB_EXTRA', ''))

    if argv is None:
        argv = sys.argv[1:]

    args = parser.parse_args(extra_argv + argv)

    if len(args.config_files) > 1 and args.factors:
        parser.error('argument --factor/-f: not allowed with multiple '
                     '--config/-c arguments')

    if args.print_named:
        print_named(named, sys.stdout)
        parser.exit()

    configs = []

    try:
        if args.config_files:
            named_overrides = named.resolve(*args.named_configs)
            for config_file in args.config_files:
                config = named.resolve('default')
                apply_user_config(config, parse_config_file(config_file))
                config.update(named_overrides)
                apply_user_overrides(config, args.config_overrides)
                configs.append(config)
        else:
            config = named.resolve('default', *args.named_configs)
            apply_user_overrides(config, args.config_overrides)
            configs.append(config)

        factors = parse_user_factors(configs[0], args.factors)
    except ConfigError as e:
        parser.error(str(e))

    if args.print_config:
        yaml.safe_dump_all(configs, stream=sys.stdout)
        parser.exit()

    try:
        if len(configs) == 1:
            config = configs[0]
            if factors:
                results = simulate_factors(config, factors, Top, Environment)
                return check_errors(results)
            else:
                simulate(config, Top, Environment)
        else:
            results = simulate_many(configs, Top, Environment)
            return check_errors(results)
    except KeyboardInterrupt:
        print("\nInterrupted by user", file=sys.stderr)
        return 1
Beispiel #23
0
    }

    parser = ArgumentParser()
    parser.add_argument(
        '--set',
        '-s',
        nargs=2,
        metavar=('KEY', 'VALUE'),
        action='append',
        default=[],
        dest='config_overrides',
        help='Override config KEY with VALUE expression',
    )
    parser.add_argument(
        '--factor',
        '-f',
        nargs=2,
        metavar=('KEYS', 'VALUES'),
        action='append',
        default=[],
        dest='factors',
        help='Add multi-factor VALUES for KEY(S)',
    )
    args = parser.parse_args()
    apply_user_overrides(config, args.config_overrides)
    factors = parse_user_factors(config, args.factors)
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)
Beispiel #24
0
        'sim.duration': '7200 s',
        'sim.gtkw.file': 'sim.gtkw',
        'sim.gtkw.live': False,
        'sim.log.enable': True,
        'sim.progress.enable': False,
        'sim.result.file': 'result.json',
        'sim.seed': 1234,
        'sim.timescale': 's',
        'sim.vcd.dump_file': 'sim.vcd',
        'sim.vcd.enable': True,
        'sim.vcd.persist': False,
        'sim.workspace': 'workspace',
    }

    parser = ArgumentParser()
    parser.add_argument(
        '--set', '-s', nargs=2, metavar=('KEY', 'VALUE'),
        action='append', default=[], dest='config_overrides',
        help='Override config KEY with VALUE expression')
    parser.add_argument(
        '--factor', '-f', nargs=2, metavar=('KEYS', 'VALUES'),
        action='append', default=[], dest='factors',
        help='Add multi-factor VALUES for KEY(S)')
    args = parser.parse_args()
    apply_user_overrides(config, args.config_overrides)
    factors = parse_user_factors(config, args.factors)
    if factors:
        simulate_factors(config, factors, Top)
    else:
        simulate(config, Top)