Chromium Code Reviews| 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 | 6 |
| 7 class CpuMetric(Metric): | 7 class CpuMetric(Metric): |
| 8 """Calulates CPU load over a span of time.""" | 8 """Calulates CPU load over a span of time.""" |
| 9 | 9 |
| 10 def __init__(self, browser): | 10 def __init__(self, browser): |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 def Stop(self, page, tab): | 23 def Stop(self, page, tab): |
| 24 assert self._start_cpu, 'Must call Start() first' | 24 assert self._start_cpu, 'Must call Start() first' |
| 25 self._results = _SubtractCpuStats(self._browser.cpu_stats, self._start_cpu) | 25 self._results = _SubtractCpuStats(self._browser.cpu_stats, self._start_cpu) |
| 26 | 26 |
| 27 # Optional argument trace_name is not in base class Metric. | 27 # Optional argument trace_name is not in base class Metric. |
| 28 # pylint: disable=W0221 | 28 # pylint: disable=W0221 |
| 29 def AddResults(self, tab, results, trace_name='cpu_utilization'): | 29 def AddResults(self, tab, results, trace_name='cpu_utilization'): |
| 30 assert self._results, 'Must call Stop() first' | 30 assert self._results, 'Must call Stop() first' |
| 31 # Add a result for each process type. | 31 # Add a result for each process type. |
| 32 for process_type in self._results: | 32 for process_type in self._results: |
| 33 if process_type == 'Global': | |
| 34 continue | |
| 33 trace_name_for_process = '%s_%s' % (trace_name, process_type.lower()) | 35 trace_name_for_process = '%s_%s' % (trace_name, process_type.lower()) |
| 34 cpu_percent = 100 * self._results[process_type] | 36 cpu_percent = 100 * self._results[process_type] |
| 35 results.Add(trace_name_for_process, '%', cpu_percent, | 37 results.Add(trace_name_for_process, '%', cpu_percent, |
| 36 chart_name='cpu_utilization', data_type='unimportant') | 38 chart_name='cpu_utilization', data_type='unimportant') |
| 39 # Add a result for Global CPU stats. | |
|
qyearsley
2014/04/22 17:00:51
Does "Global" mean all process types combined?
bulach
2014/04/22 17:12:30
sort of. :)
it means system-wide utilization, not
qyearsley
2014/04/22 17:25:52
Alright -- not sure if it would be clearer if you
bulach
2014/04/22 17:52:48
Done.
| |
| 40 time_spent = 0.0 | |
|
qyearsley
2014/04/22 17:00:51
Is this in seconds or milliseconds?
bulach
2014/04/22 17:12:30
I suppose the unit is 10mS:
https://www.kernel.org
| |
| 41 frequency_sum = 0.0 | |
| 42 for k, v in self._results['Global']['GlobalCpuFrequencyStats'].iteritems(): | |
| 43 frequency_sum += (k * v) | |
|
qyearsley
2014/04/22 17:00:51
So what does frequency_sum represent? What are k a
bulach
2014/04/22 17:12:30
k and v are the key-values, happy to expand to fre
qyearsley
2014/04/22 17:25:52
I think it would be a bit clearer if it were frequ
bulach
2014/04/22 17:52:48
Replaced k, v with frequency, time_in_state.
I cou
| |
| 44 time_spent += v | |
| 45 results.Add('average_frequency_hz', 'Hz', | |
| 46 int(frequency_sum / time_spent), | |
| 47 chart_name='cpu_frequency', | |
| 48 data_type='unimportant') | |
|
pasko
2014/04/22 14:06:30
what does data_type mean? is it documented anywher
qyearsley
2014/04/22 17:00:51
The data_type parameter specifies two things -- wh
| |
| 37 | 49 |
| 38 | 50 |
| 39 def _SubtractCpuStats(cpu_stats, start_cpu_stats): | 51 def _SubtractCpuStats(cpu_stats, start_cpu_stats): |
| 40 """Computes average cpu usage over a time period for different process types. | 52 """Computes average cpu usage over a time period for different process types. |
| 41 | 53 |
| 42 Each of the two cpu_stats arguments is a dict with the following format: | 54 Each of the two cpu_stats arguments is a dict with the following format: |
| 43 {'Browser': {'CpuProcessTime': ..., 'TotalTime': ...}, | 55 {'Browser': {'CpuProcessTime': ..., 'TotalTime': ...}, |
| 44 'Renderer': {'CpuProcessTime': ..., 'TotalTime': ...} | 56 'Renderer': {'CpuProcessTime': ..., 'TotalTime': ...} |
| 45 'Gpu': {'CpuProcessTime': ..., 'TotalTime': ...}} | 57 'Gpu': {'CpuProcessTime': ..., 'TotalTime': ...}} |
| 46 | 58 |
| 47 The 'CpuProcessTime' fields represent the number of seconds of CPU time | 59 The 'CpuProcessTime' fields represent the number of seconds of CPU time |
| 48 spent in each process, and total time is the number of real seconds | 60 spent in each process, and total time is the number of real seconds |
| 49 that have passed (this may be a Unix timestamp). | 61 that have passed (this may be a Unix timestamp). |
| 50 | 62 |
| 51 Returns: | 63 Returns: |
| 52 A dict of process type names (Browser, Renderer, etc.) to ratios of cpu | 64 A dict of process type names (Browser, Renderer, etc.) to ratios of cpu |
| 53 time used to total time elapsed. | 65 time used to total time elapsed. |
| 54 """ | 66 """ |
| 55 cpu_usage = {} | 67 cpu_usage = {} |
| 56 for process_type in cpu_stats: | 68 for process_type in cpu_stats: |
| 57 assert process_type in start_cpu_stats, 'Mismatching process types' | 69 assert process_type in start_cpu_stats, 'Mismatching process types' |
| 58 # Skip any process_types that are empty. | 70 # Skip any process_types that are empty. |
| 59 if (not cpu_stats[process_type]) or (not start_cpu_stats[process_type]): | 71 if ((not cpu_stats[process_type]) or |
| 72 (not start_cpu_stats[process_type]) or | |
| 73 process_type == 'Global'): | |
| 60 continue | 74 continue |
| 61 cpu_process_time = (cpu_stats[process_type]['CpuProcessTime'] - | 75 cpu_process_time = (cpu_stats[process_type]['CpuProcessTime'] - |
| 62 start_cpu_stats[process_type]['CpuProcessTime']) | 76 start_cpu_stats[process_type]['CpuProcessTime']) |
| 63 total_time = (cpu_stats[process_type]['TotalTime'] - | 77 total_time = (cpu_stats[process_type]['TotalTime'] - |
| 64 start_cpu_stats[process_type]['TotalTime']) | 78 start_cpu_stats[process_type]['TotalTime']) |
| 65 assert total_time > 0, 'Expected total_time > 0, was: %d' % total_time | 79 assert total_time > 0, 'Expected total_time > 0, was: %d' % total_time |
| 66 cpu_usage[process_type] = float(cpu_process_time) / total_time | 80 cpu_usage[process_type] = float(cpu_process_time) / total_time |
| 81 | |
| 82 if 'Global' not in cpu_stats: | |
| 83 return cpu_usage | |
| 84 | |
| 85 frequency_stats = cpu_stats['Global']['GlobalCpuFrequencyStats'] | |
| 86 start_frequency_stats = start_cpu_stats['Global']['GlobalCpuFrequencyStats'] | |
| 87 total_frequency_stats = {} | |
| 88 for k in frequency_stats.iterkeys(): | |
| 89 total_frequency_stats[k] = frequency_stats[k] - start_frequency_stats[k] | |
| 90 | |
| 91 cpu_usage['Global'] = {} | |
| 92 cpu_usage['Global']['GlobalCpuFrequencyStats'] = total_frequency_stats | |
| 67 return cpu_usage | 93 return cpu_usage |
| 68 | |
| OLD | NEW |