1""" 2Sets up a subprocess to run any generic command in the background every 3few seconds (by default the interval is 60 secs) 4""" 5 6import time, os, subprocess 7from autotest_lib.client.bin import profiler 8from autotest_lib.client.common_lib import utils, error 9 10class cmdprofile(profiler.profiler): 11 version = 2 12 supports_reboot = True 13 14 15 def initialize(self, cmds=['ps'], interval=60, outputfile='cmdprofile', 16 outputfiles=None): 17 18 # do some basic sanity checking on the parameters 19 if not outputfiles and not outputfile: 20 raise error.TestError( 21 'cmdprofile cannot run if neither outputfile nor outputfile ' 22 'is specified') 23 elif outputfiles and len(outputfiles) != len(cmds): 24 raise error.TestError( 25 'cmdprofile paramter outputfiles has length %d and cmds has ' 26 'length %d, but both lists must have the same length' % 27 (len(outputfiles), len(cmds))) 28 29 self.interval = interval 30 self.cmds = cmds 31 if outputfiles: 32 # outputfiles overrides outputfile 33 self.outputfiles = outputfiles 34 else: 35 self.outputfiles = [outputfile] * len(cmds) 36 37 38 def start(self, test): 39 self.pid = os.fork() 40 if self.pid: # parent 41 return 42 else: # child 43 while True: 44 for cmd, outputfile in zip(self.cmds, self.outputfiles): 45 logfile = open(os.path.join(test.profdir, outputfile), 'a') 46 utils.run(cmd, stdout_tee=logfile, stderr_tee=logfile) 47 logfile.write('\n') 48 logfile.close() 49 time.sleep(self.interval) 50 51 52 def stop(self, test): 53 utils.nuke_pid(self.pid) 54