4 from autotest_lib
.client
.common_lib
import error
, utils
, packages
7 class ProfilerNotPresentError(error
.JobError
):
8 def __init__(self
, name
, *args
, **dargs
):
9 msg
= "%s not present" % name
10 error
.JobError
.__init
__(self
, msg
, *args
, **dargs
)
13 class profiler_manager(object):
14 def __init__(self
, job
):
17 self
.tmpdir
= job
.tmpdir
18 self
.profile_run_only
= False
19 self
.active_flag
= False
20 self
.created_dirs
= []
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")
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
)
36 def delete(self
, profiler
):
37 """ Remove a profiler """
38 self
.list = [p
for p
in self
.list if p
.name
!= profiler
]
41 def current_profilers(self
):
42 """ Returns a set of the currently enabled profilers """
43 return set(p
.name
for p
in self
.list)
47 """ Indicates if any profilers are enabled """
48 return len(self
.list) > 0
52 """ Returns True if job is supposed to be run only with profiling
53 turned on, False otherwise """
54 return self
.profile_run_only
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
63 def before_start(self
, test
):
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).
72 def start(self
, test
):
73 """ Start all enabled profilers """
76 self
.active_flag
= True
80 """ Stop all enabled profilers """
83 self
.active_flag
= False
87 """ Returns True if profilers are present and started, False
89 return self
.present() and self
.active_flag
92 def report(self
, test
):
93 """ Report on all enabled profilers """
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
:
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
)