使用和添加测试分析器

添加一个分析器和添加一个测试项目类似.每个分析器都有自己的子目录(client/profilers或你可以查看profilers/),一个标准例子:

  • 一个控制文件. profilers/myprofiler/control
  • 分析器控制脚本. profilers/myprofiler.py
  • 分析器的源码(如果不仅仅是控制脚本)

我们来看一下已有的分析器. autotest提供readprofile,尽管这不是个最简单的方式,但是能够看到你想了解的全部信息.readprofile支持编译近内核.

控制文件非常简单,像这样:

job.profilers.add('readprofile')
job.run_test('sleeptest', 1)
job.profilers.delete('readprofile')

只是说明"请再以下测试中使用readprofile".一次测试,如果想添加多个分析器,可以多次调用profilers.add.

还有源码包"util-linux-2.12r.tar.bz2",将会解压到src/.python控制脚本将定义,分析器要做的大部分事情.通过查看readprofile.py,我们可以看到 profiler主类,定义版本.可以看到以下个函数:

  • setup() 它会在第一次用profiler时执行,并且编译源码.
  • intialize() 将在导入profiler时执行
  • start() 启动分析
  • stop() 停止分析
  • report() 利用profiler数据生成报告

接下来 看一下这些方法的细节.

Setup

setup(),在版本变动之前,每次测试只执行一次.它会解压util-linux-2.12r.tar.bz2至src/,并且编译.

#http://www.kernel.org/pub/linux/util-linux/util-linux-2.12r.tar.bz2
def setup(self, tarball = 'util-linux-2.12r.tar.bz2'):
    self.tarball = unmap_url(self.bindir, tarball, self.tmpdir)
    extract_tarball_to_dir(self.tarball, self.srcdir)

注释信息告诉我们去哪得到源码.函数定义的参数,定义了使用什么版本的源码.接下来,使用unmap_url下载源码,并且返回一个源码的保存路径.

os.chdir(self.srcdir)
system('./configure')
os.chdir('sys-utils')
system('make readprofile)

先是切换目录到self.srcdir.切换后,执行./configure,然后执行编译.这里使用的system()(不是os.system(),system()是一个包装后的os.system()).如果返回值 不是0,它将会自动抛出异常.

Initialize

def initialize(self):
    try:
        system('grep -iq" profile = "/proc/cmdline")
    except:
        raise CmdError, 'readprofile not enabled'

self.cmd = self.srcdir + '/sys-utils/readprofile'

它会在我们导入profiler后执行,用来检查 readprofile是否使能.

Start

def start(self, test):
    system(self.cmd + '-r')

启动profiler, 只是执行readprofile -r

Stop

def stop(self, test):
    # There's no real way to stop readprofile, so we stash the 
    # rad data at this point instead. BAD EXAMPLE TO COPY!;-)
    self.rawprofile = test.profdir + '/profile.raw'
    print "STOP"
    shutil.copyfile('/proc/profile', self.rawprofile)

一般的话,只运行readprofile --stop即可.但是如果启用的多个分析器,我们这样做为了区分每个的分析数据,使他们不相互影响.

Report

def report(self, test):
    args = ' -n'
    args += ' -m' + get_systemmap()
    args += ' -p' + self.rawprofile
    cmd = self.cmd + ' ' + args
    txtprofile = test.profdir + '/profile.text'
    system(cmd + ' | sort -nr > ' + txtprofile)
    system('bzip2' + self.rawprofile)

将分析数据转成txt. 我们需要找到kernel的Sytem.map和用户空间产生的结果(a test file).我们将结果保存在test子目录下的profiling/,这个目录在set up时已自动创建.

添加自己的profiler

只需要在profilers下创建一个新目录,然后添加自己的控制文件,源码和控制脚本.最简单的方式是直接复制已有的readprofile.py为mytest.py,并且修改顶部的类名.

Top^

上一篇Autotest:Autotest-add test>> 下一篇Autotest:Autotest-Linux distribution detection>>>

by 李鹏