blob: 81768d725b1afd863afae15b4c1748b56110b2cf [file] [log] [blame]
jadmanski043e1132008-11-19 17:10:32 +00001import 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
mblighbccad482009-08-24 22:08:31 +000010 error.JobError.__init__(self, msg, *args, **dargs)
jadmanski043e1132008-11-19 17:10:32 +000011
12
13class profiler_manager(object):
14 def __init__(self, job):
15 self.job = job
16 self.list = []
17 self.tmpdir = job.tmpdir
mblighd5433692009-02-26 00:19:55 +000018 self.profile_run_only = False
jadmanski043e1132008-11-19 17:10:32 +000019 self.active_flag = False
mblighf58865f2009-05-13 21:32:42 +000020 self.created_dirs = []
jadmanski043e1132008-11-19 17:10:32 +000021
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
mblighd5433692009-02-26 00:19:55 +000051 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
mbligh1b0faf92009-12-19 05:26:13 +000063 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
jadmanski043e1132008-11-19 17:10:32 +000072 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)
mblighaad5c502009-05-26 19:29:24 +000096
97 if getattr(test, 'iteration', None):
mblighf58865f2009-05-13 21:32:42 +000098 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)
mblighf58865f2009-05-13 21:32:42 +0000102 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)