def __init__(self, browser):
super(CpuMetric, self).__init__()
- self._results = None
self._browser = browser
self._start_cpu = None
+ self._stop_cpu = None
def DidStartBrowser(self, browser):
# Save the browser object so that cpu_stats can be accessed later.
def Stop(self, page, tab):
assert self._start_cpu, 'Must call Start() first'
- self._results = _SubtractCpuStats(self._browser.cpu_stats, self._start_cpu)
+ self._stop_cpu = self._browser.cpu_stats
# Optional argument trace_name is not in base class Metric.
# pylint: disable=W0221
def AddResults(self, tab, results, trace_name='cpu_utilization'):
- assert self._results, 'Must call Stop() first'
+ assert self._stop_cpu, 'Must call Stop() first'
+ cpu_stats = _SubtractCpuStats(self._stop_cpu, self._start_cpu)
# Add a result for each process type.
- for process_type in self._results:
+ for process_type in cpu_stats:
trace_name_for_process = '%s_%s' % (trace_name, process_type.lower())
- cpu_percent = 100 * self._results[process_type]
+ cpu_percent = 100 * cpu_stats[process_type]
results.AddValue(scalar.ScalarValue(
results.current_page, 'cpu_utilization.%s' % trace_name_for_process,
'%', cpu_percent, important=False))
start_cpu_stats[process_type]['CpuProcessTime'])
total_time = (cpu_stats[process_type]['TotalTime'] -
start_cpu_stats[process_type]['TotalTime'])
- assert total_time > 0, 'Expected total_time > 0, was: %d' % total_time
+ # Fix overflow for 32-bit jiffie counter, 64-bit counter will not overflow.
+ # Linux kernel starts with a value close to an overflow, so correction is
+ # necessary.
+ if total_time < 0:
+ total_time += 2**32
+ # Assert that the arguments were given in the correct order.
+ assert total_time > 0 and total_time < 2**31, (
+ 'Expected total_time > 0, was: %d' % total_time)
cpu_usage[process_type] = float(cpu_process_time) / total_time
return cpu_usage