1*9c5db199SXin Li# Lint as: python2, python3 2*9c5db199SXin Li# 3*9c5db199SXin Li# Copyright 2007 Google Inc. All Rights Reserved. 4*9c5db199SXin Li 5*9c5db199SXin Li"""Runs profilers on a machine when no autotest job is running. 6*9c5db199SXin Li 7*9c5db199SXin LiThis is used to profile a task when the task is running on a machine that is not 8*9c5db199SXin Lirunning through autotest. 9*9c5db199SXin Li""" 10*9c5db199SXin Li 11*9c5db199SXin Lifrom __future__ import absolute_import 12*9c5db199SXin Lifrom __future__ import division 13*9c5db199SXin Lifrom __future__ import print_function 14*9c5db199SXin Li 15*9c5db199SXin Li__author__ = '[email protected] (Colby Ranger)' 16*9c5db199SXin Li 17*9c5db199SXin Liimport platform 18*9c5db199SXin Liimport common 19*9c5db199SXin Lifrom autotest_lib.client.common_lib import barrier 20*9c5db199SXin Liimport six 21*9c5db199SXin Li 22*9c5db199SXin Li# Client control file snippet used to synchronize profiler start & stop. 23*9c5db199SXin Li_RUNTEST_PATTERN = ("job.run_test('profiler_sync', timeout_sync=%r,\n" 24*9c5db199SXin Li " timeout_start=%r, timeout_stop=%r,\n" 25*9c5db199SXin Li " hostid='%s', mainid='%s', all_ids=%r)") 26*9c5db199SXin Li_PROF_MAIN = platform.node() 27*9c5db199SXin Li_PORT = 11920 28*9c5db199SXin Li 29*9c5db199SXin Li 30*9c5db199SXin Lidef _encode_args(profiler, args, dargs): 31*9c5db199SXin Li parts = [repr(profiler)] 32*9c5db199SXin Li parts += [repr(arg) for arg in args] 33*9c5db199SXin Li parts += ["%s=%r" % darg for darg in six.iteritems(dargs)] 34*9c5db199SXin Li return ", ".join(parts) 35*9c5db199SXin Li 36*9c5db199SXin Li 37*9c5db199SXin Lidef generate_test(machines, hostname, profilers, timeout_start, timeout_stop, 38*9c5db199SXin Li timeout_sync=180): 39*9c5db199SXin Li """ 40*9c5db199SXin Li Generate a control file that enables profilers and starts profiler_sync. 41*9c5db199SXin Li 42*9c5db199SXin Li @param machines: sequence of all the hostnames involved in the barrier 43*9c5db199SXin Li synchronization 44*9c5db199SXin Li @param hostname: hostname of the machine running the generated control file 45*9c5db199SXin Li @param profilers: a sequence of 3 items tuples where the first item is a 46*9c5db199SXin Li string (the profiler name), second argument is a tuple with the 47*9c5db199SXin Li non keyword arguments to give to the profiler when being added 48*9c5db199SXin Li with "job.profilers.add()" in the control file, third item is 49*9c5db199SXin Li a dictionary of the keyword arguments to give it 50*9c5db199SXin Li @param timeout_start: how many seconds to wait in profiler_sync for the 51*9c5db199SXin Li profilers to start (None means no timeout) 52*9c5db199SXin Li @param timeout_stop: how many seconds to wait in profiler_sync for the 53*9c5db199SXin Li profilers to stop (None means no timeout) 54*9c5db199SXin Li @param timeout_sync: how many seconds to wait in profiler_sync for other 55*9c5db199SXin Li machines to reach the start of the profiler_sync (None means no 56*9c5db199SXin Li timeout) 57*9c5db199SXin Li """ 58*9c5db199SXin Li control_file = [] 59*9c5db199SXin Li for profiler in profilers: 60*9c5db199SXin Li control_file.append("job.profilers.add(%s)" 61*9c5db199SXin Li % _encode_args(*profiler)) 62*9c5db199SXin Li 63*9c5db199SXin Li profiler_sync_call = (_RUNTEST_PATTERN % 64*9c5db199SXin Li (timeout_sync, timeout_start, timeout_stop, 65*9c5db199SXin Li hostname, _PROF_MAIN, machines)) 66*9c5db199SXin Li control_file.append(profiler_sync_call) 67*9c5db199SXin Li 68*9c5db199SXin Li for profiler in reversed(profilers): 69*9c5db199SXin Li control_file.append("job.profilers.delete('%s')" % profiler[0]) 70*9c5db199SXin Li 71*9c5db199SXin Li return "\n".join(control_file) 72*9c5db199SXin Li 73*9c5db199SXin Li 74*9c5db199SXin Lidef wait_for_profilers(machines, timeout=300): 75*9c5db199SXin Li sb = barrier.barrier(_PROF_MAIN, "sync_profilers", 76*9c5db199SXin Li timeout, port=_PORT) 77*9c5db199SXin Li sb.rendezvous_servers(_PROF_MAIN, *machines) 78*9c5db199SXin Li 79*9c5db199SXin Li 80*9c5db199SXin Lidef start_profilers(machines, timeout=120): 81*9c5db199SXin Li sb = barrier.barrier(_PROF_MAIN, "start_profilers", 82*9c5db199SXin Li timeout, port=_PORT) 83*9c5db199SXin Li sb.rendezvous_servers(_PROF_MAIN, *machines) 84*9c5db199SXin Li 85*9c5db199SXin Li 86*9c5db199SXin Lidef stop_profilers(machines, timeout=120): 87*9c5db199SXin Li sb = barrier.barrier(_PROF_MAIN, "stop_profilers", 88*9c5db199SXin Li timeout, port=_PORT) 89*9c5db199SXin Li sb.rendezvous_servers(_PROF_MAIN, *machines) 90*9c5db199SXin Li 91*9c5db199SXin Li 92*9c5db199SXin Lidef finish_profilers(machines, timeout=120): 93*9c5db199SXin Li sb = barrier.barrier(_PROF_MAIN, "finish_profilers", 94*9c5db199SXin Li timeout, port=_PORT) 95*9c5db199SXin Li sb.rendezvous_servers(_PROF_MAIN, *machines) 96