| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 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 | |
| 11 | |
| 12 | |
| 13 class V8GCTimes(page_test.PageTest): | |
| 14 | |
| 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' | |
| 23 | |
| 24 def __init__(self): | |
| 25 super(V8GCTimes, self).__init__('RunPageInteractions') | |
| 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 | |
| 37 | |
| 38 def WillNavigateToPage(self, page, tab): | |
| 39 category_filter = tracing_category_filter.TracingCategoryFilter() | |
| 40 | |
| 41 for category in self._CATEGORIES: | |
| 42 category_filter.AddIncludedCategory(category) | |
| 43 | |
| 44 options = tracing_options.TracingOptions() | |
| 45 options.enable_chrome_trace = True | |
| 46 | |
| 47 tab.browser.platform.tracing_controller.Start( | |
| 48 options, category_filter, self._TIME_OUT_IN_SECONDS) | |
| 49 | |
| 50 def DidRunActions(self, page, tab): | |
| 51 trace_data = tab.browser.platform.tracing_controller.Stop() | |
| 52 timeline_model = TimelineModel(trace_data) | |
| 53 | |
| 54 self._renderer_process = timeline_model.GetRendererProcessFromTabId(tab.id) | |
| 55 | |
| 56 def ValidateAndMeasurePage(self, page, tab, results): | |
| 57 self._AddV8MetricsToResults(self._renderer_process, results) | |
| 58 | |
| 59 def _AddV8MetricsToResults(self, process, results): | |
| 60 if process is None: | |
| 61 return | |
| 62 | |
| 63 for thread in process.threads.values(): | |
| 64 if thread.name != self._RENDERER_MAIN_THREAD: | |
| 65 continue | |
| 66 | |
| 67 self._AddV8EventStatsToResults(thread, results) | |
| 68 self._AddCpuTimeStatsToResults(thread, results) | |
| 69 | |
| 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 | |
| 76 | |
| 77 event_stat.thread_duration += event.thread_duration | |
| 78 | |
| 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 | |
| 94 | |
| 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 ('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 ('Total thread duration spent in %s outside of idle tasks' % | |
| 105 v8_event_stat.result_description))) | |
| 106 results.AddValue(scalar.ScalarValue(results.current_page, | |
| 107 '%s_idle_deadline_overrun' % v8_event_stat.result_name, 'ms', | |
| 108 v8_event_stat.idle_task_overrun_duration, | |
| 109 ('Total idle task deadline overrun for %s idle tasks' | |
| 110 % v8_event_stat.result_description))) | |
| 111 results.AddValue(scalar.ScalarValue(results.current_page, | |
| 112 '%s_percentage_idle' % v8_event_stat.result_name, '%', | |
| 113 v8_event_stat.percentage_thread_duration_during_idle, | |
| 114 ('Percentage of %s spent in idle time' % | |
| 115 v8_event_stat.result_description))) | |
| 116 | |
| 117 # Add total metrics. | |
| 118 gc_total = sum(x.thread_duration for x in self._v8_event_stats) | |
| 119 gc_total_outside_idle = sum( | |
| 120 x.thread_duration_outside_idle for x in self._v8_event_stats) | |
| 121 gc_total_idle_deadline_overrun = sum( | |
| 122 x.idle_task_overrun_duration for x in self._v8_event_stats) | |
| 123 gc_total_percentage_idle = statistics.DivideIfPossibleOrZero( | |
| 124 100 * (gc_total - gc_total_outside_idle), gc_total) | |
| 125 | |
| 126 results.AddValue(scalar.ScalarValue(results.current_page, | |
| 127 'v8_gc_total', 'ms', gc_total, | |
| 128 'Total thread duration of all garbage collection events')) | |
| 129 results.AddValue(scalar.ScalarValue(results.current_page, | |
| 130 'v8_gc_total_outside_idle', 'ms', gc_total_outside_idle, | |
| 131 'Total thread duration of all garbage collection events outside of ' | |
| 132 'idle tasks')) | |
| 133 results.AddValue(scalar.ScalarValue(results.current_page, | |
| 134 'v8_gc_total_idle_deadline_overrun', 'ms', | |
| 135 gc_total_idle_deadline_overrun, | |
| 136 'Total idle task deadline overrun for all idle tasks garbage ' | |
| 137 'collection events')) | |
| 138 results.AddValue(scalar.ScalarValue(results.current_page, | |
| 139 'v8_gc_total_percentage_idle', '%', gc_total_percentage_idle, | |
| 140 'Percentage of the thread duration of all garbage collection events ' | |
| 141 'spent inside of idle tasks')) | |
| 142 | |
| 143 def _AddCpuTimeStatsToResults(self, thread, results): | |
| 144 if thread.toplevel_slices: | |
| 145 start_time = min(s.start for s in thread.toplevel_slices) | |
| 146 end_time = max(s.end for s in thread.toplevel_slices) | |
| 147 duration = end_time - start_time | |
| 148 cpu_time = sum(s.thread_duration for s in thread.toplevel_slices) | |
| 149 else: | |
| 150 duration = cpu_time = 0 | |
| 151 | |
| 152 results.AddValue(scalar.ScalarValue( | |
| 153 results.current_page, 'duration', 'ms', duration)) | |
| 154 results.AddValue(scalar.ScalarValue( | |
| 155 results.current_page, 'cpu_time', 'ms', cpu_time)) | |
| 156 | |
| 157 | |
| 158 def _FindV8EventStatForEvent(v8_event_stats_list, event_name): | |
| 159 for v8_event_stat in v8_event_stats_list: | |
| 160 if v8_event_stat.src_event_name == event_name: | |
| 161 return v8_event_stat | |
| 162 return None | |
| 163 | |
| 164 | |
| 165 def _ParentIdleTask(event): | |
| 166 parent = event.parent_slice | |
| 167 while parent: | |
| 168 if parent.name == V8GCTimes._IDLE_TASK_PARENT: | |
| 169 return parent | |
| 170 parent = parent.parent_slice | |
| 171 return None | |
| 172 | |
| 173 | |
| 174 class V8EventStat(object): | |
| 175 | |
| 176 def __init__(self, src_event_name, result_name, result_description): | |
| 177 self.src_event_name = src_event_name | |
| 178 self.result_name = result_name | |
| 179 self.result_description = result_description | |
| 180 self.thread_duration = 0.0 | |
| 181 self.thread_duration_inside_idle = 0.0 | |
| 182 self.idle_task_overrun_duration = 0.0 | |
| 183 | |
| 184 @property | |
| 185 def thread_duration_outside_idle(self): | |
| 186 return self.thread_duration - self.thread_duration_inside_idle | |
| 187 | |
| 188 @property | |
| 189 def percentage_thread_duration_during_idle(self): | |
| 190 return statistics.DivideIfPossibleOrZero( | |
| 191 100 * self.thread_duration_inside_idle, self.thread_duration) | |
| OLD | NEW |