2 # Copyright 2007 Google Inc. All Rights Reserved.
4 """Runs profilers on a machine when no autotest job is running.
6 This is used to profile a task when the task is running on a machine that is not
7 running through autotest.
10 __author__
= 'cranger@google.com (Colby Ranger)'
14 from autotest_lib
.client
.common_lib
import barrier
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)")
20 _PROF_MASTER
= platform
.node()
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
)
31 def generate_test(machines
, hostname
, profilers
, timeout_start
, timeout_stop
,
34 Generate a control file that enables profilers and starts profiler_sync.
36 @param machines: sequence of all the hostnames involved in the barrier
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
44 @param timeout_start: how many seconds to wait in profiler_sync for the
45 profilers to start (None means no timeout)
46 @param timeout_stop: how many seconds to wait in profiler_sync for the
47 profilers to stop (None means no timeout)
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
53 for profiler
in profilers
:
54 control_file
.append("job.profilers.add(%s)"
55 % _encode_args(*profiler
))
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
)
62 for profiler
in reversed(profilers
):
63 control_file
.append("job.profilers.delete('%s')" % profiler
[0])
65 return "\n".join(control_file
)
68 def wait_for_profilers(machines
, timeout
=300):
69 sb
= barrier
.barrier(_PROF_MASTER
, "sync_profilers",
71 sb
.rendezvous_servers(_PROF_MASTER
, *machines
)
74 def start_profilers(machines
, timeout
=120):
75 sb
= barrier
.barrier(_PROF_MASTER
, "start_profilers",
77 sb
.rendezvous_servers(_PROF_MASTER
, *machines
)
80 def stop_profilers(machines
, timeout
=120):
81 sb
= barrier
.barrier(_PROF_MASTER
, "stop_profilers",
83 sb
.rendezvous_servers(_PROF_MASTER
, *machines
)
86 def finish_profilers(machines
, timeout
=120):
87 sb
= barrier
.barrier(_PROF_MASTER
, "finish_profilers",
89 sb
.rendezvous_servers(_PROF_MASTER
, *machines
)