# -*- mode: python -*-
#
# Copyright (c) 2011, Stephan Creutz
# Distributed under the MIT License

# 1. requires "dude" (https://bitbucket.org/db7/dude/)
# 2. adapt the following lines to your needs
# 3. run with "dude -f mem_access_time.dude run"
# 4. get the result in table with "dude -f mem_access_time.dude sum"

import subprocess

import dude.summaries
from dude.defaults import *

dude_version = 3
name = "memory access times"
timeout = 500

# binary and execution configuration
binary         = root + '/../src/mem_access_time'
host           = 'user@host'
execute_remote = False
max_runs       = 3

# machine dependent parameters
cacheline_size   = 64
l1_cache_size    = 32 * 1024
l2_cache_size    = 6 * 1024 * 1024
page_size        = 4096
physical_memsize = 8 * 1024 * 1024 * 1024


optspace = {
    'b'  : [1000, 10000, 100000, 1000000000],
    'k'  : list(set([1, 2,
                     cacheline_size / 4,
                     page_size / (4 * 2),
                     page_size / 4,
                     2 * page_size / 4,
                     page_size,
                     l1_cache_size / 4])),
    's'  : [x / 4 for x in [physical_memsize / 8,
                            physical_memsize / 4,
                            physical_memsize / 2,
                            physical_memsize]],
    'run': range(1,max_runs+1)
}

def s_greater_equal_k(optpt):
    return optpt['s'] >= optpt['k']

def s_greater_equal_b(optpt):
    return optpt['s'] >= optpt['b']

def b_greater_equal_k(optpt):
    return optpt['b'] >= optpt['k']

constraints = [s_greater_equal_k, s_greater_equal_b, b_greater_equal_k]

def cmdl_exp(optpt):
    cmd_opts = '-B %d -K %d -S %d' % (optpt['b'], optpt['k'], optpt['s'])
    if execute_remote:
        return 'ssh -oServerAliveInterval=10 %s \'./mem_access_time %s\'' % \
            (host, cmd_opts)
    return binary + cmd_opts

def prepare_global():
    if execute_remote:
        cmd = ['scp', binary, host + ':']
        proc = subprocess.Popen(cmd)
        proc.communicate()
        proc.wait()

def prepare_exp(optpt):
    pass

run_time_sum = dude.summaries.LineSelect(
    name = 'run_time_sum',
    groupby = [],
    header = 'time',
    regex = r'time:',
    split = (lambda line: line.split('time: ')[1])
)

summaries = [run_time_sum]
