1# 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 12import platform 13import common 14from autotest_lib.client.common_lib import barrier 15 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() 21_PORT = 11920 22 23 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 30 31def generate_test(machines, hostname, profilers, timeout_start, timeout_stop, 32 timeout_sync=180): 33 """ 34 Generate a control file that enables profilers and starts profiler_sync. 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 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 50 timeout) 51 """ 52 control_file = [] 53 for profiler in profilers: 54 control_file.append("job.profilers.add(%s)" 55 % _encode_args(*profiler)) 56 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) 61 62 for profiler in reversed(profilers): 63 control_file.append("job.profilers.delete('%s')" % profiler[0]) 64 65 return "\n".join(control_file) 66 67 68def 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) 72 73 74def start_profilers(machines, timeout=120): 75 sb = barrier.barrier(_PROF_MASTER, "start_profilers", 76 timeout, port=_PORT) 77 sb.rendezvous_servers(_PROF_MASTER, *machines) 78 79 80def stop_profilers(machines, timeout=120): 81 sb = barrier.barrier(_PROF_MASTER, "stop_profilers", 82 timeout, port=_PORT) 83 sb.rendezvous_servers(_PROF_MASTER, *machines) 84 85 86def finish_profilers(machines, timeout=120): 87 sb = barrier.barrier(_PROF_MASTER, "finish_profilers", 88 timeout, port=_PORT) 89 sb.rendezvous_servers(_PROF_MASTER, *machines) 90