mbligh | 4a3e697 | 2008-01-16 17:55:05 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2007 Google Inc. All Rights Reserved. |
| 3 | |
| 4 | """Runs profilers on a machine when no autotest job is running. |
| 5 | |
| 6 | This is used to profile a task when the task is running on a machine that is not |
| 7 | running through autotest. |
| 8 | """ |
| 9 | |
| 10 | __author__ = 'cranger@google.com (Colby Ranger)' |
| 11 | |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 12 | import platform |
mbligh | fa29a2a | 2008-05-16 22:48:09 +0000 | [diff] [blame] | 13 | import common |
mbligh | ccb9e18 | 2008-04-17 15:42:10 +0000 | [diff] [blame] | 14 | from autotest_lib.client.common_lib import barrier |
mbligh | 4a3e697 | 2008-01-16 17:55:05 +0000 | [diff] [blame] | 15 | |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 16 | # 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 Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 20 | _PROF_MASTER = platform.node() |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 21 | _PORT = 11920 |
| 22 | |
mbligh | a8ba704 | 2009-12-19 05:29:13 +0000 | [diff] [blame] | 23 | |
| 24 | def _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 | |
mbligh | 4a3e697 | 2008-01-16 17:55:05 +0000 | [diff] [blame] | 30 | |
| 31 | def generate_test(machines, hostname, profilers, timeout_start, timeout_stop, |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 32 | timeout_sync=180): |
mbligh | a8ba704 | 2009-12-19 05:29:13 +0000 | [diff] [blame] | 33 | """ |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 34 | Generate a control file that enables profilers and starts profiler_sync. |
mbligh | a8ba704 | 2009-12-19 05:29:13 +0000 | [diff] [blame] | 35 | |
| 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 |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 44 | @param timeout_start: how many seconds to wait in profiler_sync for the |
mbligh | a8ba704 | 2009-12-19 05:29:13 +0000 | [diff] [blame] | 45 | profilers to start (None means no timeout) |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 46 | @param timeout_stop: how many seconds to wait in profiler_sync for the |
mbligh | a8ba704 | 2009-12-19 05:29:13 +0000 | [diff] [blame] | 47 | profilers to stop (None means no timeout) |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 48 | @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 |
mbligh | a8ba704 | 2009-12-19 05:29:13 +0000 | [diff] [blame] | 50 | timeout) |
| 51 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 52 | control_file = [] |
| 53 | for profiler in profilers: |
| 54 | control_file.append("job.profilers.add(%s)" |
mbligh | a8ba704 | 2009-12-19 05:29:13 +0000 | [diff] [blame] | 55 | % _encode_args(*profiler)) |
mbligh | 4a3e697 | 2008-01-16 17:55:05 +0000 | [diff] [blame] | 56 | |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 57 | profiler_sync_call = (_RUNTEST_PATTERN % |
| 58 | (timeout_sync, timeout_start, timeout_stop, |
| 59 | hostname, _PROF_MASTER, machines)) |
| 60 | control_file.append(profiler_sync_call) |
mbligh | 4a3e697 | 2008-01-16 17:55:05 +0000 | [diff] [blame] | 61 | |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 62 | for profiler in reversed(profilers): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 63 | control_file.append("job.profilers.delete('%s')" % profiler[0]) |
mbligh | 4a3e697 | 2008-01-16 17:55:05 +0000 | [diff] [blame] | 64 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 65 | return "\n".join(control_file) |
mbligh | 4a3e697 | 2008-01-16 17:55:05 +0000 | [diff] [blame] | 66 | |
| 67 | |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 68 | def 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) |
mbligh | 4a3e697 | 2008-01-16 17:55:05 +0000 | [diff] [blame] | 72 | |
| 73 | |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 74 | def start_profilers(machines, timeout=120): |
| 75 | sb = barrier.barrier(_PROF_MASTER, "start_profilers", |
| 76 | timeout, port=_PORT) |
| 77 | sb.rendezvous_servers(_PROF_MASTER, *machines) |
mbligh | 4a3e697 | 2008-01-16 17:55:05 +0000 | [diff] [blame] | 78 | |
| 79 | |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 80 | def stop_profilers(machines, timeout=120): |
| 81 | sb = barrier.barrier(_PROF_MASTER, "stop_profilers", |
| 82 | timeout, port=_PORT) |
| 83 | sb.rendezvous_servers(_PROF_MASTER, *machines) |
mbligh | a8ba704 | 2009-12-19 05:29:13 +0000 | [diff] [blame] | 84 | |
| 85 | |
mbligh | 9e44a45 | 2010-04-08 18:20:25 +0000 | [diff] [blame] | 86 | def finish_profilers(machines, timeout=120): |
| 87 | sb = barrier.barrier(_PROF_MASTER, "finish_profilers", |
| 88 | timeout, port=_PORT) |
| 89 | sb.rendezvous_servers(_PROF_MASTER, *machines) |