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
.page
import page_test
6 from telemetry
.timeline
.model
import TimelineModel
7 from telemetry
.timeline
import tracing_category_filter
8 from telemetry
.timeline
import tracing_options
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',
21 _RENDERER_MAIN_THREAD
= 'CrRendererMain'
22 _IDLE_TASK_PARENT
= 'SingleThreadIdleTaskRunner::RunTask'
25 super(V8GCTimes
, self
).__init
__()
27 def WillNavigateToPage(self
, page
, tab
):
28 category_filter
= tracing_category_filter
.TracingCategoryFilter()
30 for category
in self
._CATEGORIES
:
31 category_filter
.AddIncludedCategory(category
)
33 options
= tracing_options
.TracingOptions()
34 options
.enable_chrome_trace
= True
36 tab
.browser
.platform
.tracing_controller
.Start(
37 options
, category_filter
, self
._TIME
_OUT
_IN
_SECONDS
)
39 def ValidateAndMeasurePage(self
, page
, tab
, results
):
40 trace_data
= tab
.browser
.platform
.tracing_controller
.Stop()
41 timeline_model
= TimelineModel(trace_data
)
42 renderer_process
= timeline_model
.GetRendererProcessFromTabId(tab
.id)
43 self
._AddV
8MetricsToResults
(renderer_process
, results
)
45 def DidRunPage(self
, platform
):
46 if platform
.tracing_controller
.is_tracing_running
:
47 platform
.tracing_controller
.Stop()
49 def _AddV8MetricsToResults(self
, process
, results
):
53 for thread
in process
.threads
.values():
54 if thread
.name
!= self
._RENDERER
_MAIN
_THREAD
:
57 self
._AddV
8EventStatsToResults
(thread
, results
)
58 self
._AddCpuTimeStatsToResults
(thread
, results
)
60 def _AddV8EventStatsToResults(self
, thread
, results
):
62 V8EventStat('V8.GCIncrementalMarking',
63 'v8_gc_incremental_marking',
64 'incremental marking steps'),
65 V8EventStat('V8.GCScavenger',
68 V8EventStat('V8.GCCompactor',
69 'v8_gc_mark_compactor',
70 'mark-sweep-compactor')]
71 # Find all V8 GC events in the trace.
72 for event
in thread
.IterAllSlices():
73 event_stat
= _FindV8EventStatForEvent(v8_event_stats
, event
.name
)
77 event_stat
.thread_duration
+= event
.thread_duration
79 parent_idle_task
= _ParentIdleTask(event
)
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
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 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
,
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
)))
119 gc_total
= sum(x
.thread_duration
for x
in v8_event_stats
)
120 gc_total_outside_idle
= sum(
121 x
.thread_duration_outside_idle
for x
in v8_event_stats
)
122 gc_total_idle_deadline_overrun
= sum(
123 x
.idle_task_overrun_duration
for x
in 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
,
133 'Total thread duration of all garbage collection events outside of '
135 results
.AddValue(scalar
.ScalarValue(results
.current_page
,
136 'v8_gc_total_idle_deadline_overrun', 'ms',
137 gc_total_idle_deadline_overrun
,
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
,
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
)
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
:
169 def _ParentIdleTask(event
):
170 parent
= event
.parent_slice
172 # pylint: disable=protected-access
173 if parent
.name
== V8GCTimes
._IDLE
_TASK
_PARENT
:
175 parent
= parent
.parent_slice
179 class V8EventStat(object):
181 def __init__(self
, src_event_name
, result_name
, result_description
):
182 self
.src_event_name
= src_event_name
183 self
.result_name
= result_name
184 self
.result_description
= result_description
185 self
.thread_duration
= 0.0
186 self
.thread_duration_inside_idle
= 0.0
187 self
.idle_task_overrun_duration
= 0.0
190 def thread_duration_outside_idle(self
):
191 return self
.thread_duration
- self
.thread_duration_inside_idle
194 def percentage_thread_duration_during_idle(self
):
195 return statistics
.DivideIfPossibleOrZero(
196 100 * self
.thread_duration_inside_idle
, self
.thread_duration
)