blob: 63245511e6eabc8d12672c17516a4b60ff059c7f [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
Eric Li861b2d52011-02-04 14:50:35 -080012import platform
mblighfa29a2a2008-05-16 22:48:09 +000013import common
mblighccb9e182008-04-17 15:42:10 +000014from autotest_lib.client.common_lib import barrier
mbligh4a3e6972008-01-16 17:55:05 +000015
mbligh9e44a452010-04-08 18:20:25 +000016# Client control file snippet used to synchronize profiler start & stop.
17_RUNTEST_PATTERN = ("job.run_test('profiler_sync', timeout_sync=%r,\n"
18 " timeout_start=%r, timeout_stop=%r,\n"
19 " hostid='%s', masterid='%s', all_ids=%r)")
Eric Li861b2d52011-02-04 14:50:35 -080020_PROF_MASTER = platform.node()
mbligh9e44a452010-04-08 18:20:25 +000021_PORT = 11920
22
mbligha8ba7042009-12-19 05:29:13 +000023
24def _encode_args(profiler, args, dargs):
25 parts = [repr(profiler)]
26 parts += [repr(arg) for arg in args]
27 parts += ["%s=%r" % darg for darg in dargs.iteritems()]
28 return ", ".join(parts)
29
mbligh4a3e6972008-01-16 17:55:05 +000030
31def generate_test(machines, hostname, profilers, timeout_start, timeout_stop,
mbligh9e44a452010-04-08 18:20:25 +000032 timeout_sync=180):
mbligha8ba7042009-12-19 05:29:13 +000033 """
mbligh9e44a452010-04-08 18:20:25 +000034 Generate a control file that enables profilers and starts profiler_sync.
mbligha8ba7042009-12-19 05:29:13 +000035
36 @param machines: sequence of all the hostnames involved in the barrier
37 synchronization
38 @param hostname: hostname of the machine running the generated control file
39 @param profilers: a sequence of 3 items tuples where the first item is a
40 string (the profiler name), second argument is a tuple with the
41 non keyword arguments to give to the profiler when being added
42 with "job.profilers.add()" in the control file, third item is
43 a dictionary of the keyword arguments to give it
mbligh9e44a452010-04-08 18:20:25 +000044 @param timeout_start: how many seconds to wait in profiler_sync for the
mbligha8ba7042009-12-19 05:29:13 +000045 profilers to start (None means no timeout)
mbligh9e44a452010-04-08 18:20:25 +000046 @param timeout_stop: how many seconds to wait in profiler_sync for the
mbligha8ba7042009-12-19 05:29:13 +000047 profilers to stop (None means no timeout)
mbligh9e44a452010-04-08 18:20:25 +000048 @param timeout_sync: how many seconds to wait in profiler_sync for other
49 machines to reach the start of the profiler_sync (None means no
mbligha8ba7042009-12-19 05:29:13 +000050 timeout)
51 """
jadmanski0afbb632008-06-06 21:10:57 +000052 control_file = []
53 for profiler in profilers:
54 control_file.append("job.profilers.add(%s)"
mbligha8ba7042009-12-19 05:29:13 +000055 % _encode_args(*profiler))
mbligh4a3e6972008-01-16 17:55:05 +000056
mbligh9e44a452010-04-08 18:20:25 +000057 profiler_sync_call = (_RUNTEST_PATTERN %
58 (timeout_sync, timeout_start, timeout_stop,
59 hostname, _PROF_MASTER, machines))
60 control_file.append(profiler_sync_call)
mbligh4a3e6972008-01-16 17:55:05 +000061
mbligh9e44a452010-04-08 18:20:25 +000062 for profiler in reversed(profilers):
jadmanski0afbb632008-06-06 21:10:57 +000063 control_file.append("job.profilers.delete('%s')" % profiler[0])
mbligh4a3e6972008-01-16 17:55:05 +000064
jadmanski0afbb632008-06-06 21:10:57 +000065 return "\n".join(control_file)
mbligh4a3e6972008-01-16 17:55:05 +000066
67
mbligh9e44a452010-04-08 18:20:25 +000068def wait_for_profilers(machines, timeout=300):
69 sb = barrier.barrier(_PROF_MASTER, "sync_profilers",
70 timeout, port=_PORT)
71 sb.rendezvous_servers(_PROF_MASTER, *machines)
mbligh4a3e6972008-01-16 17:55:05 +000072
73
mbligh9e44a452010-04-08 18:20:25 +000074def start_profilers(machines, timeout=120):
75 sb = barrier.barrier(_PROF_MASTER, "start_profilers",
76 timeout, port=_PORT)
77 sb.rendezvous_servers(_PROF_MASTER, *machines)
mbligh4a3e6972008-01-16 17:55:05 +000078
79
mbligh9e44a452010-04-08 18:20:25 +000080def stop_profilers(machines, timeout=120):
81 sb = barrier.barrier(_PROF_MASTER, "stop_profilers",
82 timeout, port=_PORT)
83 sb.rendezvous_servers(_PROF_MASTER, *machines)
mbligha8ba7042009-12-19 05:29:13 +000084
85
mbligh9e44a452010-04-08 18:20:25 +000086def finish_profilers(machines, timeout=120):
87 sb = barrier.barrier(_PROF_MASTER, "finish_profilers",
88 timeout, port=_PORT)
89 sb.rendezvous_servers(_PROF_MASTER, *machines)