| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from metrics import Metric | 5 from metrics import Metric |
| 6 from telemetry.value import scalar | 6 from telemetry.value import scalar |
| 7 | 7 |
| 8 | 8 |
| 9 class CpuMetric(Metric): | 9 class CpuMetric(Metric): |
| 10 """Calulates CPU load over a span of time.""" | 10 """Calulates CPU load over a span of time.""" |
| 11 | 11 |
| 12 def __init__(self, browser): | 12 def __init__(self, browser): |
| 13 super(CpuMetric, self).__init__() | 13 super(CpuMetric, self).__init__() |
| 14 self._results = None | |
| 15 self._browser = browser | 14 self._browser = browser |
| 16 self._start_cpu = None | 15 self._start_cpu = None |
| 16 self._stop_cpu = None |
| 17 | 17 |
| 18 def DidStartBrowser(self, browser): | 18 def DidStartBrowser(self, browser): |
| 19 # Save the browser object so that cpu_stats can be accessed later. | 19 # Save the browser object so that cpu_stats can be accessed later. |
| 20 self._browser = browser | 20 self._browser = browser |
| 21 | 21 |
| 22 def Start(self, page, tab): | 22 def Start(self, page, tab): |
| 23 self._start_cpu = self._browser.cpu_stats | 23 self._start_cpu = self._browser.cpu_stats |
| 24 | 24 |
| 25 def Stop(self, page, tab): | 25 def Stop(self, page, tab): |
| 26 assert self._start_cpu, 'Must call Start() first' | 26 assert self._start_cpu, 'Must call Start() first' |
| 27 self._results = _SubtractCpuStats(self._browser.cpu_stats, self._start_cpu) | 27 self._stop_cpu = self._browser.cpu_stats |
| 28 | 28 |
| 29 # Optional argument trace_name is not in base class Metric. | 29 # Optional argument trace_name is not in base class Metric. |
| 30 # pylint: disable=W0221 | 30 # pylint: disable=W0221 |
| 31 def AddResults(self, tab, results, trace_name='cpu_utilization'): | 31 def AddResults(self, tab, results, trace_name='cpu_utilization'): |
| 32 assert self._results, 'Must call Stop() first' | 32 assert self._stop_cpu, 'Must call Stop() first' |
| 33 cpu_stats = _SubtractCpuStats(self._stop_cpu, self._start_cpu) |
| 33 # Add a result for each process type. | 34 # Add a result for each process type. |
| 34 for process_type in self._results: | 35 for process_type in cpu_stats: |
| 35 trace_name_for_process = '%s_%s' % (trace_name, process_type.lower()) | 36 trace_name_for_process = '%s_%s' % (trace_name, process_type.lower()) |
| 36 cpu_percent = 100 * self._results[process_type] | 37 cpu_percent = 100 * cpu_stats[process_type] |
| 37 results.AddValue(scalar.ScalarValue( | 38 results.AddValue(scalar.ScalarValue( |
| 38 results.current_page, 'cpu_utilization.%s' % trace_name_for_process, | 39 results.current_page, 'cpu_utilization.%s' % trace_name_for_process, |
| 39 '%', cpu_percent, important=False)) | 40 '%', cpu_percent, important=False)) |
| 40 | 41 |
| 41 | 42 |
| 42 def _SubtractCpuStats(cpu_stats, start_cpu_stats): | 43 def _SubtractCpuStats(cpu_stats, start_cpu_stats): |
| 43 """Computes average cpu usage over a time period for different process types. | 44 """Computes average cpu usage over a time period for different process types. |
| 44 | 45 |
| 45 Each of the two cpu_stats arguments is a dict with the following format: | 46 Each of the two cpu_stats arguments is a dict with the following format: |
| 46 {'Browser': {'CpuProcessTime': ..., 'TotalTime': ...}, | 47 {'Browser': {'CpuProcessTime': ..., 'TotalTime': ...}, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 69 # Linux kernel starts with a value close to an overflow, so correction is | 70 # Linux kernel starts with a value close to an overflow, so correction is |
| 70 # necessary. | 71 # necessary. |
| 71 if total_time < 0: | 72 if total_time < 0: |
| 72 total_time += 2**32 | 73 total_time += 2**32 |
| 73 # Assert that the arguments were given in the correct order. | 74 # Assert that the arguments were given in the correct order. |
| 74 assert total_time > 0 and total_time < 2**31, ( | 75 assert total_time > 0 and total_time < 2**31, ( |
| 75 'Expected total_time > 0, was: %d' % total_time) | 76 'Expected total_time > 0, was: %d' % total_time) |
| 76 cpu_usage[process_type] = float(cpu_process_time) / total_time | 77 cpu_usage[process_type] = float(cpu_process_time) / total_time |
| 77 return cpu_usage | 78 return cpu_usage |
| 78 | 79 |
| OLD | NEW |