1# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6import os, signal, subprocess
7from autotest_lib.client.bin import profiler, os_dep
8from autotest_lib.client.common_lib import error
9
10
11class custom_perf(profiler.profiler):
12    """
13    This is a profiler class for the perf profiler in ChromeOS. It differs from
14    cros_perf in that you can completely customize what arguments you send in to
15    perf.
16    """
17    version = 1
18
19    def initialize(self, perf_options=''):
20        # The two supported options for profile_type are 'record' and 'stat'.
21        self.perf_options = perf_options
22        self.perf_bin = os_dep.command('perf')
23
24
25    def start(self, test):
26        outfile = os.path.join(test.profdir, 'perf.out')
27
28        cmd = ('cd %s; exec %s %s > %s 2>&1' %
29               (test.profdir, self.perf_bin, self.perf_options, outfile))
30
31        self._process = subprocess.Popen(cmd, shell=True,
32                                         stderr=subprocess.STDOUT)
33
34
35    def stop(self, test):
36        ret_code = self._process.poll()
37        if ret_code is not None:
38            raise error.AutotestError('perf terminated early with return code: '
39                                      '%d. Please check your logs.' % ret_code)
40
41        os.killpg(os.getpgid(self._process.pid), signal.SIGINT)
42        self._process.wait()
43