blob: c4aeb8730ac3c7538e481dbe2216cd2f0ea25e9f [file] [log] [blame]
# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
This is a profiler class for the perf profiler in ChromeOS. It differs from
the existing perf profiler in autotset by directly substituting the options
passed to the initialize function into the "perf" command line. It allows one
to specify which perf command to run and thus what type of profile to collect
(e.g. "perf record" or "perf stat"). It also does not produce a perf report
on the client (where there are no debug symbols) but instead copies
the file back to the server for analysis.
import os, signal, subprocess
import threading, time
import shutil
import logging
from autotest_lib.client.bin import profiler
from autotest_lib.client.common_lib import error
class cros_perf(profiler.profiler):
""" Profiler for running perf """
version = 1
def initialize(self, interval=120, duration=10, profile_type='record'):
"""Initializes the cros perf profiler.
interval (int): How often to start the profiler
duration (int): How long the profiler will run per interval. Set to
None to run for the full duration of the test.
profile_type (str): Profile type to run.
Valid options: record, and stat.
self.interval = interval
self.duration = duration
self.profile_type = profile_type
if self.profile_type not in ['record', 'stat']:
raise error.AutotestError(
'Unknown profile type: %s' % (profile_type))
def start(self, test):
# KASLR makes looking up the symbols from the binary impossible, save
# the running symbols.
shutil.copy('/proc/kallsyms', test.profdir)
self.thread = MonitorThread(
def stop(self, test):
class MonitorThread(threading.Thread):
""" Thread that runs perf at the specified interval and duration """
def __init__(self, interval, duration, profile_type, test):
self.stopped = threading.Event()
self.interval = interval
self.duration = duration
self.profile_type = profile_type
self.test = test
def _get_command(self, timestamp):
file_name = '' % (int(timestamp))
path = os.path.join(self.test.profdir, file_name)
if self.profile_type == 'record':
return ['perf', 'record', '-e', 'cycles', '-g', '--output', path]
elif self.profile_type == 'stat':
return ['perf', 'stat', 'record', '-a', '--output', path]
raise error.AutotestError(
'Unknown profile type: %s' % (self.profile_type))
def run(self):"perf thread starting")
sleep_time = 0
while not self.stopped.wait(sleep_time):
start_time = time.time()
cmd = self._get_command(start_time)"Running perf: %s", cmd)
process = subprocess.Popen(cmd, close_fds=True)"Sleeping for %ss", self.duration)
ret_code = process.poll()
if ret_code is None:"Stopping perf")
'perf terminated early with return code: %d. '
'Please check your logs.', ret_code)
end_time = time.time()
sleep_time = self.interval - (end_time - start_time)
if sleep_time < 0:
sleep_time = 0
def stop(self):
""" Stops the thread """"Stopping perf thread")
self.join()"perf thread stopped")