1# Lint as: python2, python3
2"""
3readprofile - a tool to read kernel profiling information
4
5The readprofile command uses the /proc/profile information to print ascii data
6on standard output. The output is organized in three columns: the first is the
7number of clock ticks, the second is the name of the C function in the kernel
8where those many ticks occurred, and the third is the normalized `load' of the
9procedure, calculated as a ratio between the number of ticks and the length of
10the procedure. The output is filled with blanks to ease readability.
11"""
12from __future__ import absolute_import
13from __future__ import division
14from __future__ import print_function
15
16import os, shutil
17
18from autotest_lib.client.bin import utils, profiler
19from autotest_lib.client.common_lib import error
20
21class readprofile(profiler.profiler):
22    version = 1
23
24# http://www.kernel.org/pub/linux/utils/util-linux/util-linux-2.12r.tar.bz2
25    def setup(self, tarball = 'util-linux-2.12r.tar.bz2'):
26        self.tarball = utils.unmap_url(self.bindir, tarball, self.tmpdir)
27        utils.extract_tarball_to_dir(self.tarball, self.srcdir)
28        os.chdir(self.srcdir)
29
30        utils.configure()
31        os.chdir('sys-utils')
32        utils.make('readprofile')
33
34
35    def initialize(self):
36        self.job.require_gcc()
37
38        try:
39            utils.system('grep -iq " profile=" /proc/cmdline')
40        except error.CmdError:
41            raise error.AutotestError('readprofile not enabled')
42
43        self.cmd = self.srcdir + '/sys-utils/readprofile'
44
45
46    def start(self, test):
47        utils.system(self.cmd + ' -r')
48
49
50    def stop(self, test):
51        # There's no real way to stop readprofile, so we stash the
52        # raw data at this point instead. BAD EXAMPLE TO COPY! ;-)
53        self.rawprofile = test.profdir + '/profile.raw'
54        print("STOP")
55        shutil.copyfile('/proc/profile', self.rawprofile)
56
57
58    def report(self, test):
59        args  = ' -n'
60        args += ' -m ' + utils.get_systemmap()
61        args += ' -p ' + self.rawprofile
62        cmd = self.cmd + ' ' + args
63        txtprofile = test.profdir + '/profile.text'
64        utils.system(cmd + ' | sort -nr > ' + txtprofile)
65        utils.system('bzip2 ' + self.rawprofile)
66