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