1import os, sys 2import common 3 4from autotest_lib.client.common_lib import error, utils, packages 5 6 7class ProfilerNotPresentError(error.JobError): 8 def __init__(self, name, *args, **dargs): 9 msg = "%s not present" % name 10 error.JobError.__init__(self, msg, *args, **dargs) 11 12 13class profiler_manager(object): 14 def __init__(self, job): 15 self.job = job 16 self.list = [] 17 self.tmpdir = job.tmpdir 18 self.profile_run_only = False 19 self.active_flag = False 20 self.created_dirs = [] 21 22 23 def load_profiler(self, profiler, args, dargs): 24 """ Given a name and args, loads a profiler, initializes it 25 with the required arguments, and returns an instance of it. Raises 26 a ProfilerNotPresentError if the module isn't found. """ 27 raise NotImplementedError("load_profiler not implemented") 28 29 30 def add(self, profiler, *args, **dargs): 31 """ Add a profiler """ 32 new_profiler = self.load_profiler(profiler, args, dargs) 33 self.list.append(new_profiler) 34 35 36 def delete(self, profiler): 37 """ Remove a profiler """ 38 self.list = [p for p in self.list if p.name != profiler] 39 40 41 def current_profilers(self): 42 """ Returns a set of the currently enabled profilers """ 43 return set(p.name for p in self.list) 44 45 46 def present(self): 47 """ Indicates if any profilers are enabled """ 48 return len(self.list) > 0 49 50 51 def only(self): 52 """ Returns True if job is supposed to be run only with profiling 53 turned on, False otherwise """ 54 return self.profile_run_only 55 56 57 def set_only(self, value): 58 """ Changes the flag which determines whether or not the job is to be 59 run without profilers at all """ 60 self.profile_run_only = value 61 62 63 def before_start(self, test): 64 """ 65 Override to do any setup needed before actually starting the profilers 66 (this function is called before calling test.before_run_once() and 67 profilers.start() in a profiled run). 68 """ 69 pass 70 71 72 def start(self, test): 73 """ Start all enabled profilers """ 74 for p in self.list: 75 p.start(test) 76 self.active_flag = True 77 78 79 def stop(self, test): 80 """ Stop all enabled profilers """ 81 for p in self.list: 82 p.stop(test) 83 self.active_flag = False 84 85 86 def active(self): 87 """ Returns True if profilers are present and started, False 88 otherwise """ 89 return self.present() and self.active_flag 90 91 92 def report(self, test): 93 """ Report on all enabled profilers """ 94 for p in self.list: 95 p.report(test) 96 97 if getattr(test, 'iteration', None): 98 name = 'iteration.%s' % test.iteration 99 iter_path = os.path.join(test.profdir, name) 100 os.system('mkdir -p %s' % iter_path) 101 self.created_dirs.append(name) 102 for file in os.listdir(test.profdir): 103 if file in self.created_dirs: 104 continue 105 file_path = os.path.join(test.profdir, file) 106 iter_path_file = os.path.join(iter_path, file) 107 os.rename(file_path, iter_path_file) 108