1import os
2from autotest_lib.client.bin import utils
3
4import base_tracer
5try:
6    from site_tracers import *
7except ImportError:
8    pass
9
10
11off = base_tracer.Tracer
12
13
14class ftrace(base_tracer.Tracer):
15
16    mountpoint = '/sys/kernel/debug'
17    tracing_dir = os.path.join(mountpoint, 'tracing')
18
19    def warmup(self, buffer_size_kb):
20        if not os.path.exists(self.tracing_dir):
21            utils.system('mount -t debugfs debugfs %s' % self.mountpoint)
22
23        # ensure clean state:
24        self.trace_config('tracing_enabled', '0')
25        self.trace_config('current_tracer', 'nop')
26        self.trace_config('events/enable', '0')
27        self.trace_config('trace', '')
28        # set ring buffer size:
29        self.trace_config('buffer_size_kb', str(buffer_size_kb))
30        # enable tracepoints:
31        self.trace_config('events/syscalls/sys_enter_getuid/enable', '1')
32
33    def cleanup(self):
34        # reset ring buffer size:
35        self.trace_config('buffer_size_kb', '1408')
36        # disable tracepoints:
37        self.trace_config('events/enable', '0')
38
39    def start_tracing(self):
40        self.trace_config('tracing_enabled', '1')
41
42    def stop_tracing(self):
43        self.trace_config('tracing_enabled', '0')
44
45    def reset_tracing(self):
46        self.trace_config('trace', '')
47
48    def gather_stats(self, results):
49        per_cpu = os.path.join(self.tracing_dir, 'per_cpu')
50        for cpu in os.listdir(per_cpu):
51            cpu_stats = os.path.join(per_cpu, cpu, 'stats')
52            for line in utils.read_file(cpu_stats).splitlines():
53                key, val = line.split(': ')
54                key = key.replace(' ', '_')
55                val = int(val)
56                cpu_key = '%s_%s' % (cpu, key)
57                total_key = 'total_' + key
58                results[cpu_key] = val
59                results[total_key] = (results.get(total_key, 0) +
60                                      results[cpu_key])
61