Re-land: C++ readability review
[chromium-blink-merge.git] / tools / perf / measurements / v8_gc_times.py
blob18546fbaee3aee5a6bac89a467f78f9d116c28b6
1 # Copyright 2015 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.core.platform import tracing_category_filter
6 from telemetry.core.platform import tracing_options
7 from telemetry.page import page_test
8 from telemetry.timeline.model import TimelineModel
9 from telemetry.util import statistics
10 from telemetry.value import scalar
13 class V8GCTimes(page_test.PageTest):
15 _TIME_OUT_IN_SECONDS = 60
16 _CATEGORIES = ['blink.console',
17 'renderer.scheduler',
18 'toplevel',
19 'v8',
20 'webkit.console']
21 _RENDERER_MAIN_THREAD = 'CrRendererMain'
22 _IDLE_TASK_PARENT = 'SingleThreadIdleTaskRunner::RunTask'
24 def __init__(self):
25 super(V8GCTimes, self).__init__()
26 self._v8_event_stats = [
27 V8EventStat('V8.GCIncrementalMarking',
28 'v8_gc_incremental_marking',
29 'incremental marking steps'),
30 V8EventStat('V8.GCScavenger',
31 'v8_gc_scavenger',
32 'scavenges'),
33 V8EventStat('V8.GCCompactor',
34 'v8_gc_mark_compactor',
35 'mark-sweep-compactor')]
36 self._renderer_process = None
38 def WillNavigateToPage(self, page, tab):
39 category_filter = tracing_category_filter.TracingCategoryFilter()
41 for category in self._CATEGORIES:
42 category_filter.AddIncludedCategory(category)
44 options = tracing_options.TracingOptions()
45 options.enable_chrome_trace = True
47 tab.browser.platform.tracing_controller.Start(
48 options, category_filter, self._TIME_OUT_IN_SECONDS)
50 def DidRunActions(self, page, tab):
51 trace_data = tab.browser.platform.tracing_controller.Stop()
52 timeline_model = TimelineModel(trace_data)
54 self._renderer_process = timeline_model.GetRendererProcessFromTabId(tab.id)
56 def ValidateAndMeasurePage(self, page, tab, results):
57 self._AddV8MetricsToResults(self._renderer_process, results)
59 def _AddV8MetricsToResults(self, process, results):
60 if process is None:
61 return
63 for thread in process.threads.values():
64 if thread.name != self._RENDERER_MAIN_THREAD:
65 continue
67 self._AddV8EventStatsToResults(thread, results)
68 self._AddCpuTimeStatsToResults(thread, results)
70 def _AddV8EventStatsToResults(self, thread, results):
71 # Find all V8 GC events in the trace.
72 for event in thread.IterAllSlices():
73 event_stat = _FindV8EventStatForEvent(self._v8_event_stats, event.name)
74 if not event_stat:
75 continue
77 event_stat.thread_duration += event.thread_duration
79 parent_idle_task = _ParentIdleTask(event)
80 if parent_idle_task:
81 allotted_idle_time = parent_idle_task.args['allotted_time_ms']
82 idle_task_wall_overrun = 0
83 if event.duration > allotted_idle_time:
84 idle_task_wall_overrun = event.duration - allotted_idle_time
85 # Don't count time over the deadline as being inside idle time.
86 # Since the deadline should be relative to wall clock we compare
87 # allotted_time_ms with wall duration instead of thread duration, and
88 # then assume the thread duration was inside idle for the same
89 # percentage of time.
90 inside_idle = event.thread_duration * statistics.DivideIfPossibleOrZero(
91 event.duration - idle_task_wall_overrun, event.duration)
92 event_stat.thread_duration_inside_idle += inside_idle
93 event_stat.idle_task_overrun_duration += idle_task_wall_overrun
95 for v8_event_stat in self._v8_event_stats:
96 results.AddValue(scalar.ScalarValue(
97 results.current_page, v8_event_stat.result_name, 'ms',
98 v8_event_stat.thread_duration,
99 description=('Total thread duration spent in %s' %
100 v8_event_stat.result_description)))
101 results.AddValue(scalar.ScalarValue(results.current_page,
102 '%s_outside_idle' % v8_event_stat.result_name, 'ms',
103 v8_event_stat.thread_duration_outside_idle,
104 description=(
105 'Total thread duration spent in %s outside of idle tasks' %
106 v8_event_stat.result_description)))
107 results.AddValue(scalar.ScalarValue(results.current_page,
108 '%s_idle_deadline_overrun' % v8_event_stat.result_name, 'ms',
109 v8_event_stat.idle_task_overrun_duration,
110 description=('Total idle task deadline overrun for %s idle tasks'
111 % v8_event_stat.result_description)))
112 results.AddValue(scalar.ScalarValue(results.current_page,
113 '%s_percentage_idle' % v8_event_stat.result_name, 'idle%',
114 v8_event_stat.percentage_thread_duration_during_idle,
115 description=('Percentage of %s spent in idle time' %
116 v8_event_stat.result_description)))
118 # Add total metrics.
119 gc_total = sum(x.thread_duration for x in self._v8_event_stats)
120 gc_total_outside_idle = sum(
121 x.thread_duration_outside_idle for x in self._v8_event_stats)
122 gc_total_idle_deadline_overrun = sum(
123 x.idle_task_overrun_duration for x in self._v8_event_stats)
124 gc_total_percentage_idle = statistics.DivideIfPossibleOrZero(
125 100 * (gc_total - gc_total_outside_idle), gc_total)
127 results.AddValue(scalar.ScalarValue(results.current_page,
128 'v8_gc_total', 'ms', gc_total,
129 description='Total thread duration of all garbage collection events'))
130 results.AddValue(scalar.ScalarValue(results.current_page,
131 'v8_gc_total_outside_idle', 'ms', gc_total_outside_idle,
132 description=(
133 'Total thread duration of all garbage collection events outside of '
134 'idle tasks')))
135 results.AddValue(scalar.ScalarValue(results.current_page,
136 'v8_gc_total_idle_deadline_overrun', 'ms',
137 gc_total_idle_deadline_overrun,
138 description=(
139 'Total idle task deadline overrun for all idle tasks garbage '
140 'collection events')))
141 results.AddValue(scalar.ScalarValue(results.current_page,
142 'v8_gc_total_percentage_idle', 'idle%', gc_total_percentage_idle,
143 description=(
144 'Percentage of the thread duration of all garbage collection '
145 'events spent inside of idle tasks')))
147 def _AddCpuTimeStatsToResults(self, thread, results):
148 if thread.toplevel_slices:
149 start_time = min(s.start for s in thread.toplevel_slices)
150 end_time = max(s.end for s in thread.toplevel_slices)
151 duration = end_time - start_time
152 cpu_time = sum(s.thread_duration for s in thread.toplevel_slices)
153 else:
154 duration = cpu_time = 0
156 results.AddValue(scalar.ScalarValue(
157 results.current_page, 'duration', 'ms', duration))
158 results.AddValue(scalar.ScalarValue(
159 results.current_page, 'cpu_time', 'ms', cpu_time))
162 def _FindV8EventStatForEvent(v8_event_stats_list, event_name):
163 for v8_event_stat in v8_event_stats_list:
164 if v8_event_stat.src_event_name == event_name:
165 return v8_event_stat
166 return None
169 def _ParentIdleTask(event):
170 parent = event.parent_slice
171 while parent:
172 if parent.name == V8GCTimes._IDLE_TASK_PARENT:
173 return parent
174 parent = parent.parent_slice
175 return None
178 class V8EventStat(object):
180 def __init__(self, src_event_name, result_name, result_description):
181 self.src_event_name = src_event_name
182 self.result_name = result_name
183 self.result_description = result_description
184 self.thread_duration = 0.0
185 self.thread_duration_inside_idle = 0.0
186 self.idle_task_overrun_duration = 0.0
188 @property
189 def thread_duration_outside_idle(self):
190 return self.thread_duration - self.thread_duration_inside_idle
192 @property
193 def percentage_thread_duration_during_idle(self):
194 return statistics.DivideIfPossibleOrZero(
195 100 * self.thread_duration_inside_idle, self.thread_duration)