import time

ssh_hosts = [hosts.create_host(m) for m in machines]
at_hosts = [autotest.Autotest(h) for h in ssh_hosts]


def add_profilers(at, profilers, timeout_sync, timeout_start, timeout_stop,
                        machines, name):
    control_file = []
    for profiler in profilers:
        control_file.append("job.profilers.add(%s)"
                                % str(profiler)[1:-1])

    control_file.append(("job.run_test('profiler_sync', timeout_sync=%d, "
                         "timeout_start=%d, timeout_stop=%d, "
                         "hostid='%s', masterid='%s', all_ids=%s)")
                        % (timeout_sync, timeout_start, timeout_stop,
                           at.host.hostname, "PROF_MASTER", str(machines)))

    for profiler in profilers:
        control_file.append("job.profilers.delete('%s')" % profiler[0])

    params = ["\n".join(control_file), "profile-" + profiler[0], at.host]
    return subcommand(at.run, params, name)


def wait_for_profilers(machines, timeout = 180):
    # wait until the profilers have started
    sync_bar = barrier("PROF_MASTER", "sync_profilers",
            timeout, port=11920)
    sync_bar.rendezvous_servers("PROF_MASTER", *machines)


def start_profilers(machines, timeout = 180):
    # wait until the profilers have started
    start_bar = barrier("PROF_MASTER", "start_profilers",
            timeout, port=11920)
    start_bar.rendezvous_servers("PROF_MASTER", *machines)


def stop_profilers(machines, timeout = 120):
    stop_bar = barrier("PROF_MASTER", "stop_profilers", timeout, port=11920)
    stop_bar.rendezvous_servers("PROF_MASTER", *machines)


def server_sleep_test(seconds):
    wait_for_profilers(machines)
    start_profilers(machines)
    for i in range(seconds):
        print "%d of %d" % (i, seconds)
        time.sleep(1)
    stop_profilers(machines)


def main():
    timeout_sync = 180
    timeout_start = 60
    timeout_stop = 60
    profilers = [["vmstat"], ["iostat"]]

    tests = [subcommand(server_sleep_test, [20], "server_sleep_test")]
    for at in at_hosts:
        name = "profiled-%s" % at.host.hostname
        tests.append(add_profilers(at, profilers, timeout_sync,
                        timeout_start, timeout_stop, machines, name))
    parallel(tests)


main()