1 # Copyright 2014 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 metrics
import memory
6 from metrics
import Metric
7 from telemetry
.value
import scalar
10 class SystemMemoryMetric(Metric
):
11 """SystemMemoryMetric gathers system memory statistic.
13 This metric collects system memory stats per test. It reports the difference
14 (delta) in system memory starts from the start of the test to the end of it.
17 def __init__(self
, browser
):
18 super(SystemMemoryMetric
, self
).__init
__()
19 self
._browser
= browser
20 self
._memory
_stats
_start
= None
21 self
._memory
_stats
_end
= None
23 def Start(self
, page
, tab
):
24 """Start the per-page preparation for this metric.
26 Records the system memory stats at this point.
28 self
._memory
_stats
_start
= self
._browser
.memory_stats
30 def Stop(self
, page
, tab
):
31 """Prepare the results for this page.
33 The results are the differences between the current system memory stats
34 and the values when Start() was called.
36 assert self
._memory
_stats
_start
, 'Must call Start() first'
37 self
._memory
_stats
_end
= self
._browser
.memory_stats
39 # |trace_name| and |exclude_metrics| args are not in base class Metric.
40 # pylint: disable=W0221
41 def AddResults(self
, tab
, results
, trace_name
=None, exclude_metrics
=None):
42 """Add results for this page to the results object.
44 Reports the delta in memory stats between the start stats and the end stats
45 (as *_delta metrics). It reports end memory stats in case no matching start
49 trace_name: Trace name to identify the summary results for current page.
50 exclude_metrics: List of memory metrics to exclude from results,
51 e.g. VM, VMPeak, etc. See AddResultsForProcesses().
53 assert self
._memory
_stats
_end
, 'Must call Stop() first'
54 memory_stats
= _SubtractMemoryStats(self
._memory
_stats
_end
,
55 self
._memory
_stats
_start
)
56 if not memory_stats
['Browser']:
58 exclude_metrics
= exclude_metrics
or {}
59 memory
.AddResultsForProcesses(results
, memory_stats
,
60 metric_trace_name
=trace_name
, chart_trace_name
='delta',
61 exclude_metrics
=exclude_metrics
)
63 if 'SystemCommitCharge' not in exclude_metrics
:
64 results
.AddValue(scalar
.ScalarValue(
66 'commit_charge_delta.%s' % (trace_name
or 'commit_charge'), 'kb',
67 memory_stats
['SystemCommitCharge'], important
=False))
69 if 'ProcessCount' not in exclude_metrics
:
70 results
.AddValue(scalar
.ScalarValue(
72 'processes_delta.%s' % (trace_name
or 'processes'), 'count',
73 memory_stats
['ProcessCount'], important
=False))
76 def _SubtractMemoryStats(end_memory_stats
, start_memory_stats
):
77 """Computes the difference in memory usage stats.
79 Each of the two stats arguments is a dict with the following format:
80 {'Browser': {metric: value, ...},
81 'Renderer': {metric: value, ...},
82 'Gpu': {metric: value, ...},
83 'ProcessCount': value,
86 The metrics can be VM, WorkingSetSize, ProportionalSetSize, etc depending on
89 NOTE: The only metrics that are not subtracted from original are the *Peak*
93 A dict of process type names (Browser, Renderer, etc.) to memory usage
94 metrics between the end collected stats and the start collected stats.
97 end_memory_stats
= end_memory_stats
or {}
98 start_memory_stats
= start_memory_stats
or {}
100 for process_type
in end_memory_stats
:
101 memory_stats
[process_type
] = {}
102 end_process_memory
= end_memory_stats
[process_type
]
103 if not end_process_memory
:
106 # If a process has end stats without start stats then report the end stats.
107 # For example, a GPU process that started just after media playback.
108 if (process_type
not in start_memory_stats
or
109 not start_memory_stats
[process_type
]):
110 memory_stats
[process_type
] = end_process_memory
113 if not isinstance(end_process_memory
, dict):
114 start_value
= start_memory_stats
[process_type
] or 0
115 memory_stats
[process_type
] = end_process_memory
- start_value
117 for metric
in end_process_memory
:
118 end_value
= end_process_memory
[metric
]
119 start_value
= start_memory_stats
[process_type
][metric
] or 0
121 memory_stats
[process_type
][metric
] = end_value
123 memory_stats
[process_type
][metric
] = end_value
- start_value