OLD | NEW |
1 """ | 1 """ |
2 perf is a tool included in the linux kernel tree that | 2 perf is a tool included in the linux kernel tree that |
3 supports functionality similar to oprofile and more. | 3 supports functionality similar to oprofile and more. |
4 | 4 |
5 @see: http://lwn.net/Articles/310260/ | 5 @see: http://lwn.net/Articles/310260/ |
6 """ | 6 """ |
7 | 7 |
8 import time, os, stat, subprocess, signal | 8 import time, os, stat, subprocess, signal |
9 import logging | 9 import logging |
10 from autotest_lib.client.bin import profiler, os_dep, utils | 10 from autotest_lib.client.bin import profiler, os_dep, utils |
11 | 11 |
12 | 12 |
13 class perf(profiler.profiler): | 13 class perf(profiler.profiler): |
14 version = 1 | 14 version = 1 |
15 | 15 |
16 def initialize(self, events=["cycles","instructions"]): | 16 def initialize(self, events=["cycles","instructions"], trace=False): |
17 if type(events) == str: | 17 if type(events) == str: |
18 self.events = [events] | 18 self.events = [events] |
19 else: | 19 else: |
20 self.events = events | 20 self.events = events |
| 21 self.trace = trace |
21 self.perf_bin = os_dep.command('perf') | 22 self.perf_bin = os_dep.command('perf') |
22 perf_help = utils.run('%s report help' % self.perf_bin, | 23 perf_help = utils.run('%s report help' % self.perf_bin, |
23 ignore_status=True).stderr | 24 ignore_status=True).stderr |
24 self.sort_keys = None | 25 self.sort_keys = None |
25 for line in perf_help.split('\n'): | 26 for line in perf_help.split('\n'): |
26 a = "sort by key(s):" | 27 a = "sort by key(s):" |
27 if a in line: | 28 if a in line: |
28 line = line.strip(a) | 29 line = line.replace(a, "") |
29 self.sort_keys = [k.rstrip(",") for k in line.split() if | 30 self.sort_keys = [k.rstrip(",") for k in line.split() if |
30 k.rstrip(",") != 'dso'] | 31 k.rstrip(",") != 'dso'] |
31 if not self.sort_keys: | 32 if not self.sort_keys: |
32 self.sort_keys = ['comm', 'cpu'] | 33 self.sort_keys = ['comm', 'cpu'] |
33 | 34 |
34 | 35 |
35 def start(self, test): | 36 def start(self, test): |
36 self.logfile = os.path.join(test.profdir, "perf") | 37 self.logfile = os.path.join(test.profdir, "perf") |
37 cmd = ("%s record -a -o %s" % | 38 cmd = ("exec %s record -a -o %s" % |
38 (self.perf_bin, self.logfile)) | 39 (self.perf_bin, self.logfile)) |
| 40 if "parent" in self.sort_keys: |
| 41 cmd += " -g" |
| 42 if self.trace: |
| 43 cmd += " -R" |
39 for event in self.events: | 44 for event in self.events: |
40 cmd += " -e %s" % event | 45 cmd += " -e %s" % event |
41 self._process = subprocess.Popen(cmd, shell=True, | 46 self._process = subprocess.Popen(cmd, shell=True, |
42 stderr=subprocess.STDOUT) | 47 stderr=subprocess.STDOUT) |
43 | 48 |
44 | 49 |
45 def stop(self, test): | 50 def stop(self, test): |
46 os.kill(self._process.pid, signal.SIGINT) | 51 os.kill(self._process.pid, signal.SIGINT) |
47 self._process.wait() | 52 self._process.wait() |
48 | 53 |
49 | 54 |
50 def report(self, test): | 55 def report(self, test): |
51 for key in self.sort_keys: | 56 for key in self.sort_keys: |
52 reportfile = os.path.join(test.profdir, '%s.comm' % key) | 57 reportfile = os.path.join(test.profdir, '%s.comm' % key) |
53 cmd = ("%s report -i %s --sort %s,dso" % (self.perf_bin, | 58 cmd = ("%s report -i %s --sort %s,dso" % (self.perf_bin, |
54 self.logfile, | 59 self.logfile, |
55 key)) | 60 key)) |
56 outfile = open(reportfile, 'w') | 61 outfile = open(reportfile, 'w') |
57 p = subprocess.Popen(cmd, shell=True, stdout=outfile, | 62 p = subprocess.Popen(cmd, shell=True, stdout=outfile, |
58 stderr=subprocess.STDOUT) | 63 stderr=subprocess.STDOUT) |
59 p.wait() | 64 p.wait() |
| 65 |
| 66 if self.trace: |
| 67 tracefile = os.path.join(test.profdir, 'trace') |
| 68 cmd = ("%s trace -i %s" % (self.perf_bin, self.logfile,)) |
| 69 |
| 70 outfile = open(tracefile, 'w') |
| 71 p = subprocess.Popen(cmd, shell=True, stdout=outfile, |
| 72 stderr=subprocess.STDOUT) |
| 73 p.wait() |
| 74 |
60 # The raw detailed perf output is HUGE. We cannot store it by default. | 75 # The raw detailed perf output is HUGE. We cannot store it by default. |
61 perf_log_size = os.stat(self.logfile)[stat.ST_SIZE] | 76 perf_log_size = os.stat(self.logfile)[stat.ST_SIZE] |
62 logging.info('Removing %s after generating reports (saving %s bytes).', | 77 logging.info('Removing %s after generating reports (saving %s bytes).', |
63 self.logfile, perf_log_size) | 78 self.logfile, perf_log_size) |
64 os.unlink(self.logfile) | 79 os.unlink(self.logfile) |
OLD | NEW |