blob: 62a11e13c08c1fb32f4712196b7204fe31d71d43 [file] [log] [blame]
mbligh4a3e6972008-01-16 17:55:05 +00001#
2# Copyright 2007 Google Inc. All Rights Reserved.
3
4"""Runs profilers on a machine when no autotest job is running.
5
6This is used to profile a task when the task is running on a machine that is not
7running through autotest.
8"""
9
10__author__ = 'cranger@google.com (Colby Ranger)'
11
mblighfa29a2a2008-05-16 22:48:09 +000012import common
mblighccb9e182008-04-17 15:42:10 +000013from autotest_lib.client.common_lib import barrier
mbligh4a3e6972008-01-16 17:55:05 +000014
mbligh9e44a452010-04-08 18:20:25 +000015# Client control file snippet used to synchronize profiler start & stop.
16_RUNTEST_PATTERN = ("job.run_test('profiler_sync', timeout_sync=%r,\n"
17 " timeout_start=%r, timeout_stop=%r,\n"
18 " hostid='%s', masterid='%s', all_ids=%r)")
19_PROF_MASTER = "PROF_MASTER"
20_PORT = 11920
21
mbligha8ba7042009-12-19 05:29:13 +000022
23def _encode_args(profiler, args, dargs):
24 parts = [repr(profiler)]
25 parts += [repr(arg) for arg in args]
26 parts += ["%s=%r" % darg for darg in dargs.iteritems()]
27 return ", ".join(parts)
28
mbligh4a3e6972008-01-16 17:55:05 +000029
30def generate_test(machines, hostname, profilers, timeout_start, timeout_stop,
mbligh9e44a452010-04-08 18:20:25 +000031 timeout_sync=180):
mbligha8ba7042009-12-19 05:29:13 +000032 """
mbligh9e44a452010-04-08 18:20:25 +000033 Generate a control file that enables profilers and starts profiler_sync.
mbligha8ba7042009-12-19 05:29:13 +000034
35 @param machines: sequence of all the hostnames involved in the barrier
36 synchronization
37 @param hostname: hostname of the machine running the generated control file
38 @param profilers: a sequence of 3 items tuples where the first item is a
39 string (the profiler name), second argument is a tuple with the
40 non keyword arguments to give to the profiler when being added
41 with "job.profilers.add()" in the control file, third item is
42 a dictionary of the keyword arguments to give it
mbligh9e44a452010-04-08 18:20:25 +000043 @param timeout_start: how many seconds to wait in profiler_sync for the
mbligha8ba7042009-12-19 05:29:13 +000044 profilers to start (None means no timeout)
mbligh9e44a452010-04-08 18:20:25 +000045 @param timeout_stop: how many seconds to wait in profiler_sync for the
mbligha8ba7042009-12-19 05:29:13 +000046 profilers to stop (None means no timeout)
mbligh9e44a452010-04-08 18:20:25 +000047 @param timeout_sync: how many seconds to wait in profiler_sync for other
48 machines to reach the start of the profiler_sync (None means no
mbligha8ba7042009-12-19 05:29:13 +000049 timeout)
50 """
jadmanski0afbb632008-06-06 21:10:57 +000051 control_file = []
52 for profiler in profilers:
53 control_file.append("job.profilers.add(%s)"
mbligha8ba7042009-12-19 05:29:13 +000054 % _encode_args(*profiler))
mbligh4a3e6972008-01-16 17:55:05 +000055
mbligh9e44a452010-04-08 18:20:25 +000056 profiler_sync_call = (_RUNTEST_PATTERN %
57 (timeout_sync, timeout_start, timeout_stop,
58 hostname, _PROF_MASTER, machines))
59 control_file.append(profiler_sync_call)
mbligh4a3e6972008-01-16 17:55:05 +000060
mbligh9e44a452010-04-08 18:20:25 +000061 for profiler in reversed(profilers):
jadmanski0afbb632008-06-06 21:10:57 +000062 control_file.append("job.profilers.delete('%s')" % profiler[0])
mbligh4a3e6972008-01-16 17:55:05 +000063
jadmanski0afbb632008-06-06 21:10:57 +000064 return "\n".join(control_file)
mbligh4a3e6972008-01-16 17:55:05 +000065
66
mbligh9e44a452010-04-08 18:20:25 +000067def wait_for_profilers(machines, timeout=300):
68 sb = barrier.barrier(_PROF_MASTER, "sync_profilers",
69 timeout, port=_PORT)
70 sb.rendezvous_servers(_PROF_MASTER, *machines)
mbligh4a3e6972008-01-16 17:55:05 +000071
72
mbligh9e44a452010-04-08 18:20:25 +000073def start_profilers(machines, timeout=120):
74 sb = barrier.barrier(_PROF_MASTER, "start_profilers",
75 timeout, port=_PORT)
76 sb.rendezvous_servers(_PROF_MASTER, *machines)
mbligh4a3e6972008-01-16 17:55:05 +000077
78
mbligh9e44a452010-04-08 18:20:25 +000079def stop_profilers(machines, timeout=120):
80 sb = barrier.barrier(_PROF_MASTER, "stop_profilers",
81 timeout, port=_PORT)
82 sb.rendezvous_servers(_PROF_MASTER, *machines)
mbligha8ba7042009-12-19 05:29:13 +000083
84
mbligh9e44a452010-04-08 18:20:25 +000085def finish_profilers(machines, timeout=120):
86 sb = barrier.barrier(_PROF_MASTER, "finish_profilers",
87 timeout, port=_PORT)
88 sb.rendezvous_servers(_PROF_MASTER, *machines)