jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1 | import os, sys |
| 2 | import common |
| 3 | |
| 4 | from autotest_lib.client.common_lib import error, utils, packages |
| 5 | |
| 6 | |
| 7 | class ProfilerNotPresentError(error.JobError): |
| 8 | def __init__(self, name, *args, **dargs): |
| 9 | msg = "%s not present" % name |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 10 | error.JobError.__init__(self, msg, *args, **dargs) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 11 | |
| 12 | |
| 13 | class profiler_manager(object): |
| 14 | def __init__(self, job): |
| 15 | self.job = job |
| 16 | self.list = [] |
| 17 | self.tmpdir = job.tmpdir |
mbligh | d543369 | 2009-02-26 00:19:55 +0000 | [diff] [blame] | 18 | self.profile_run_only = False |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 19 | self.active_flag = False |
mbligh | f58865f | 2009-05-13 21:32:42 +0000 | [diff] [blame] | 20 | self.created_dirs = [] |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 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 | |
mbligh | d543369 | 2009-02-26 00:19:55 +0000 | [diff] [blame] | 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 | |
mbligh | 1b0faf9 | 2009-12-19 05:26:13 +0000 | [diff] [blame] | 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 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 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) |
mbligh | aad5c50 | 2009-05-26 19:29:24 +0000 | [diff] [blame] | 96 | |
| 97 | if getattr(test, 'iteration', None): |
mbligh | f58865f | 2009-05-13 21:32:42 +0000 | [diff] [blame] | 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) |
mbligh | f58865f | 2009-05-13 21:32:42 +0000 | [diff] [blame] | 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) |