1#!/usr/bin/python2.7 2# Copyright 2015 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import subprocess 7import numpy 8 9class Error(Exception): 10 """Module error class.""" 11 12def GatherPerfStats(program, events): 13 """Run perf stat with the given events and given program. 14 15 @param program: path to benchmark binary. It should take one argument 16 (number of loop iterations) and produce no output. 17 @param events: value to pass to '-e' arg of perf stat. 18 @returns: List of dicts. 19 """ 20 facts = [] 21 for i in xrange(0, 10): 22 loops = (i + 1) * 10 23 out = subprocess.check_output( 24 ('perf', 'stat', '-x', ',', 25 '-e', events, 26 program, '%d' % loops), 27 stderr=subprocess.STDOUT) 28 unsupported_events = [] 29 f = {} 30 for line in out.splitlines(): 31 fields = line.split(',') 32 count, unit, event = None, None, None 33 if len(fields) == 2: 34 count, event = fields 35 elif len(fields) == 3: 36 count, unit, event = fields 37 else: 38 raise Error('Unable to parse perf stat output') 39 if count == '<not supported>': 40 unsupported_events.append(event) 41 else: 42 f[event] = int(count) 43 if unsupported_events: 44 raise Error('These events are not supported: %s' 45 % unsupported_events) 46 facts.append(f) 47 return facts 48