Add long running gmail memory benchmark for background tab.
[chromium-blink-merge.git] / tools / perf / metrics / cpu.py
blob66424d4ae8dcf84fd5e3a92db437d7556258e90b
1 # Copyright 2013 The Chromium 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.
5 from telemetry.value import scalar
7 from metrics import Metric
10 class CpuMetric(Metric):
11 """Calulates CPU load over a span of time."""
13 def __init__(self, browser):
14 super(CpuMetric, self).__init__()
15 self._browser = browser
16 self._start_cpu = None
17 self._stop_cpu = None
19 def DidStartBrowser(self, browser):
20 # Save the browser object so that cpu_stats can be accessed later.
21 self._browser = browser
23 def Start(self, page, tab):
24 self._start_cpu = self._browser.cpu_stats
26 def Stop(self, page, tab):
27 assert self._start_cpu, 'Must call Start() first'
28 self._stop_cpu = self._browser.cpu_stats
30 # Optional argument trace_name is not in base class Metric.
31 # pylint: disable=W0221
32 def AddResults(self, tab, results, trace_name='cpu_utilization'):
33 assert self._stop_cpu, 'Must call Stop() first'
34 cpu_stats = _SubtractCpuStats(self._stop_cpu, self._start_cpu)
36 # FIXME: Renderer process CPU times are impossible to compare correctly.
37 # http://crbug.com/419786#c11
38 if 'Renderer' in cpu_stats:
39 del cpu_stats['Renderer']
41 # Add a result for each process type.
42 for process_type in cpu_stats:
43 trace_name_for_process = '%s_%s' % (trace_name, process_type.lower())
44 cpu_percent = 100 * cpu_stats[process_type]
45 results.AddValue(scalar.ScalarValue(
46 results.current_page, 'cpu_utilization.%s' % trace_name_for_process,
47 '%', cpu_percent, important=False))
50 def _SubtractCpuStats(cpu_stats, start_cpu_stats):
51 """Computes average cpu usage over a time period for different process types.
53 Each of the two cpu_stats arguments is a dict with the following format:
54 {'Browser': {'CpuProcessTime': ..., 'TotalTime': ...},
55 'Renderer': {'CpuProcessTime': ..., 'TotalTime': ...}
56 'Gpu': {'CpuProcessTime': ..., 'TotalTime': ...}}
58 The 'CpuProcessTime' fields represent the number of seconds of CPU time
59 spent in each process, and total time is the number of real seconds
60 that have passed (this may be a Unix timestamp).
62 Returns:
63 A dict of process type names (Browser, Renderer, etc.) to ratios of cpu
64 time used to total time elapsed.
65 """
66 cpu_usage = {}
67 for process_type in cpu_stats:
68 assert process_type in start_cpu_stats, 'Mismatching process types'
69 # Skip any process_types that are empty.
70 if (not cpu_stats[process_type]) or (not start_cpu_stats[process_type]):
71 continue
72 cpu_process_time = (cpu_stats[process_type]['CpuProcessTime'] -
73 start_cpu_stats[process_type]['CpuProcessTime'])
74 total_time = (cpu_stats[process_type]['TotalTime'] -
75 start_cpu_stats[process_type]['TotalTime'])
76 # Fix overflow for 32-bit jiffie counter, 64-bit counter will not overflow.
77 # Linux kernel starts with a value close to an overflow, so correction is
78 # necessary.
79 if total_time < 0:
80 total_time += 2**32
81 # Assert that the arguments were given in the correct order.
82 assert total_time > 0 and total_time < 2**31, (
83 'Expected total_time > 0, was: %d' % total_time)
84 cpu_usage[process_type] = float(cpu_process_time) / total_time
85 return cpu_usage