1"""
2Autotest profiler for blktrace
3blktrace - generate traces of the i/o traffic on block devices
4"""
5import os
6from autotest_lib.client.common_lib import error
7from autotest_lib.client.bin import profiler, utils
8
9
10class blktrace(profiler.profiler):
11    version = 2
12
13    def initialize(self, **dargs):
14        self.mountpoint = '/sys/kernel/debug'
15        self.blktrace = os.path.join(self.srcdir, 'blktrace')
16        self.blkparse = os.path.join(self.srcdir, 'blkparse')
17        self.blktrace_job = None
18
19        self.job.require_gcc()
20        self.job.setup_dep(['libaio'])
21        ldflags = '-L ' + self.job.autodir + '/deps/libaio/lib'
22        cflags = '-I ' + self.job.autodir + '/deps/libaio/include'
23        self.gcc_flags = ldflags + ' ' + cflags
24        self.device = dargs.get('device', None)
25
26
27    def setup(self, tarball='blktrace.tar.bz2', **dargs):
28        # v1.0.1, pulled from git, 2009/06/10
29        # commit 1e09f6e9012826fca69fa07222b7bc53c3e629ee
30        self.tarball = utils.unmap_url(self.bindir, tarball, self.tmpdir)
31        utils.extract_tarball_to_dir(self.tarball, self.srcdir)
32        os.chdir(self.srcdir)
33        utils.make('"CFLAGS=' + self.gcc_flags + '"')
34
35
36    def get_device(self, test):
37        if getattr(test, 'device', None):
38            device = test.device
39        else:
40            if self.device:
41                device=self.device
42            else:
43                raise error.TestWarn('No device specified for blktrace')
44        return device
45
46
47    def start(self, test):
48        result = utils.system("mount | grep '%s'" % self.mountpoint,
49                              ignore_status=True)
50        if result:
51            utils.system('mount -t debugfs debugfs /sys/kernel/debug')
52        device = self.get_device(test)
53        self.blktrace_job = utils.BgJob('%s /dev/%s' % (self.blktrace, device))
54
55
56    def stop(self, test):
57        if self.blktrace_job is not None:
58            utils.nuke_subprocess(self.blktrace_job.sp)
59        self.blktrace_job = None
60
61
62    def report(self, test):
63        output_file = os.path.join(test.profdir, 'blktrace')
64        if getattr(test, 'profile_tag', None):
65            output_file += '.' + test.profile_tag
66        device = self.get_device(test)
67        utils.system('%s %s > %s' % (self.blkparse, device, output_file))
68