| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 import re | 5 import re |
| 6 | 6 |
| 7 from telemetry.timeline import tracing_category_filter | 7 from telemetry.timeline import chrome_trace_category_filter |
| 8 | 8 |
| 9 | 9 |
| 10 RECORD_MODE_PARAM = 'record_mode' | 10 RECORD_MODE_PARAM = 'record_mode' |
| 11 ENABLE_SYSTRACE_PARAM = 'enable_systrace' | |
| 12 | 11 |
| 13 ECHO_TO_CONSOLE = 'trace-to-console' | 12 ECHO_TO_CONSOLE = 'trace-to-console' |
| 14 ENABLE_SYSTRACE = 'enable-systrace' | |
| 15 RECORD_AS_MUCH_AS_POSSIBLE = 'record-as-much-as-possible' | 13 RECORD_AS_MUCH_AS_POSSIBLE = 'record-as-much-as-possible' |
| 16 RECORD_CONTINUOUSLY = 'record-continuously' | 14 RECORD_CONTINUOUSLY = 'record-continuously' |
| 17 RECORD_UNTIL_FULL = 'record-until-full' | 15 RECORD_UNTIL_FULL = 'record-until-full' |
| 18 | 16 |
| 19 # Map telemetry's tracing record_mode to the DevTools API string. | 17 # Map telemetry's tracing record_mode to the DevTools API string. |
| 20 # (The keys happen to be the same as the values.) | 18 # (The keys happen to be the same as the values.) |
| 21 RECORD_MODE_MAP = { | 19 RECORD_MODE_MAP = { |
| 22 RECORD_UNTIL_FULL: 'record-until-full', | 20 RECORD_UNTIL_FULL: 'record-until-full', |
| 23 RECORD_CONTINUOUSLY: 'record-continuously', | 21 RECORD_CONTINUOUSLY: 'record-continuously', |
| 24 RECORD_AS_MUCH_AS_POSSIBLE: 'record-as-much-as-possible', | 22 RECORD_AS_MUCH_AS_POSSIBLE: 'record-as-much-as-possible', |
| (...skipping 27 matching lines...) Expand all Loading... |
| 52 return data | 50 return data |
| 53 | 51 |
| 54 | 52 |
| 55 class ChromeTraceConfig(object): | 53 class ChromeTraceConfig(object): |
| 56 """Stores configuration options specific to the Chrome tracing agent. | 54 """Stores configuration options specific to the Chrome tracing agent. |
| 57 | 55 |
| 58 This produces the trace config JSON string for tracing in Chrome. | 56 This produces the trace config JSON string for tracing in Chrome. |
| 59 | 57 |
| 60 record_mode: can be any mode in RECORD_MODE_MAP. This corresponds to | 58 record_mode: can be any mode in RECORD_MODE_MAP. This corresponds to |
| 61 record modes in chrome. | 59 record modes in chrome. |
| 62 enable_systrace: a boolean that specifies whether to enable systrace. | 60 category_filter: Object that specifies which tracing categories to trace. |
| 63 tracing_category_filter: Object that specifies which tracing | |
| 64 categories to trace. | |
| 65 memory_dump_config: Stores the triggers for memory dumps. | 61 memory_dump_config: Stores the triggers for memory dumps. |
| 66 | |
| 67 """ | 62 """ |
| 68 | 63 |
| 69 def __init__(self): | 64 def __init__(self): |
| 70 self._record_mode = RECORD_AS_MUCH_AS_POSSIBLE | 65 self._record_mode = RECORD_AS_MUCH_AS_POSSIBLE |
| 71 self._enable_systrace = False | 66 self._category_filter = ( |
| 72 self._tracing_category_filter = ( | 67 chrome_trace_category_filter.ChromeTraceCategoryFilter()) |
| 73 tracing_category_filter.TracingCategoryFilter()) | |
| 74 self._memory_dump_config = None | 68 self._memory_dump_config = None |
| 75 | 69 |
| 76 @property | 70 def SetLowOverheadFilter(self): |
| 77 def tracing_category_filter(self): | 71 self._category_filter = ( |
| 78 return self._tracing_category_filter | 72 chrome_trace_category_filter.CreateLowOverheadFilter()) |
| 79 | 73 |
| 80 def SetNoOverheadFilter(self): | 74 def SetDefaultOverheadFilter(self): |
| 81 self._tracing_category_filter = ( | 75 self._category_filter = ( |
| 82 tracing_category_filter.CreateNoOverheadFilter()) | 76 chrome_trace_category_filter.CreateDefaultOverheadFilter()) |
| 83 | |
| 84 def SetMinimalOverheadFilter(self): | |
| 85 self._tracing_category_filter = ( | |
| 86 tracing_category_filter.CreateMinimalOverheadFilter()) | |
| 87 | 77 |
| 88 def SetDebugOverheadFilter(self): | 78 def SetDebugOverheadFilter(self): |
| 89 self._tracing_category_filter = ( | 79 self._category_filter = ( |
| 90 tracing_category_filter.CreateDebugOverheadFilter()) | 80 chrome_trace_category_filter.CreateDebugOverheadFilter()) |
| 91 | 81 |
| 92 def SetTracingCategoryFilter(self, cf): | 82 @property |
| 93 if isinstance(cf, tracing_category_filter.TracingCategoryFilter): | 83 def category_filter(self): |
| 94 self._tracing_category_filter = cf | 84 return self._category_filter |
| 85 |
| 86 def SetCategoryFilter(self, cf): |
| 87 if isinstance(cf, chrome_trace_category_filter.ChromeTraceCategoryFilter): |
| 88 self._category_filter = cf |
| 95 else: | 89 else: |
| 96 raise TypeError( | 90 raise TypeError( |
| 97 'Must pass SetTracingCategoryFilter a TracingCategoryFilter instance') | 91 'Must pass SetCategoryFilter a ChromeTraceCategoryFilter instance') |
| 98 | 92 |
| 99 def SetMemoryDumpConfig(self, dump_config): | 93 def SetMemoryDumpConfig(self, dump_config): |
| 100 if isinstance(dump_config, MemoryDumpConfig): | 94 if isinstance(dump_config, MemoryDumpConfig): |
| 101 self._memory_dump_config = dump_config | 95 self._memory_dump_config = dump_config |
| 102 else: | 96 else: |
| 103 raise TypeError( | 97 raise TypeError( |
| 104 'Must pass SetMemoryDumpConfig a MemoryDumpConfig instance') | 98 'Must pass SetMemoryDumpConfig a MemoryDumpConfig instance') |
| 105 | 99 |
| 106 @property | 100 @property |
| 107 def record_mode(self): | 101 def record_mode(self): |
| 108 return self._record_mode | 102 return self._record_mode |
| 109 | 103 |
| 110 @record_mode.setter | 104 @record_mode.setter |
| 111 def record_mode(self, value): | 105 def record_mode(self, value): |
| 112 assert value in RECORD_MODE_MAP | 106 assert value in RECORD_MODE_MAP |
| 113 self._record_mode = value | 107 self._record_mode = value |
| 114 | 108 |
| 115 @property | |
| 116 def enable_systrace(self): | |
| 117 return self._enable_systrace | |
| 118 | |
| 119 @enable_systrace.setter | |
| 120 def enable_systrace(self, value): | |
| 121 self._enable_systrace = value | |
| 122 | |
| 123 def GetChromeTraceConfigForStartupTracing(self): | 109 def GetChromeTraceConfigForStartupTracing(self): |
| 124 """Map the config to a JSON string for startup tracing. | 110 """Map the config to a JSON string for startup tracing. |
| 125 | 111 |
| 126 All keys in the returned dictionary use underscore-case (e.g. | 112 All keys in the returned dictionary use underscore-case (e.g. |
| 127 'enable_systrace'). In addition, the 'record_mode' value uses hyphen-case | 113 'record_mode'). In addition, the 'record_mode' value uses hyphen-case |
| 128 (e.g. 'record-until-full'). | 114 (e.g. 'record-until-full'). |
| 129 """ | 115 """ |
| 130 result = { | 116 result = { |
| 131 RECORD_MODE_PARAM: RECORD_MODE_MAP[self._record_mode], | 117 RECORD_MODE_PARAM: RECORD_MODE_MAP[self._record_mode] |
| 132 ENABLE_SYSTRACE_PARAM: self._enable_systrace | |
| 133 } | 118 } |
| 134 result.update(self._tracing_category_filter.GetDictForChromeTracing()) | 119 result.update(self._category_filter.GetDictForChromeTracing()) |
| 135 if self._memory_dump_config: | 120 if self._memory_dump_config: |
| 136 result.update(self._memory_dump_config.GetDictForChromeTracing()) | 121 result.update(self._memory_dump_config.GetDictForChromeTracing()) |
| 137 return result | 122 return result |
| 138 | 123 |
| 139 @property | 124 @property |
| 140 def requires_modern_devtools_tracing_start_api(self): | 125 def requires_modern_devtools_tracing_start_api(self): |
| 141 """Returns True iff the config CANNOT be passed via the legacy DevTools API. | 126 """Returns True iff the config CANNOT be passed via the legacy DevTools API. |
| 142 | 127 |
| 143 Legacy DevTools Tracing.start API: | 128 Legacy DevTools Tracing.start API: |
| 144 Available since: the introduction of the Tracing.start request. | 129 Available since: the introduction of the Tracing.start request. |
| 145 Parameters: categories (string), options (string), | 130 Parameters: categories (string), options (string), |
| 146 bufferUsageReportingInterval (number), | 131 bufferUsageReportingInterval (number), |
| 147 transferMode (enum). | 132 transferMode (enum). |
| 148 TraceConfig method: GetChromeTraceCategoriesAndOptionsStringsForDevTools() | 133 TraceConfig method: GetChromeTraceCategoriesAndOptionsStringsForDevTools() |
| 149 | 134 |
| 150 Modern DevTools Tracing.start API: | 135 Modern DevTools Tracing.start API: |
| 151 Available since: Chrome 51.0.2683.0. | 136 Available since: Chrome 51.0.2683.0. |
| 152 Parameters: traceConfig (dict), | 137 Parameters: traceConfig (dict), |
| 153 bufferUsageReportingInterval (number), | 138 bufferUsageReportingInterval (number), |
| 154 transferMode (enum). | 139 transferMode (enum). |
| 155 TraceConfig method: GetChromeTraceConfigDictForDevTools() | 140 TraceConfig method: GetChromeTraceConfigDictForDevTools() |
| 156 """ | 141 """ |
| 157 # Memory dump config cannot be passed via the 'options' string (legacy API) | 142 # Memory dump config cannot be passed via the 'options' string (legacy API) |
| 158 # in the DevTools Tracing.start request. | 143 # in the DevTools Tracing.start request. |
| 159 return bool(self._memory_dump_config) | 144 return bool(self._memory_dump_config) |
| 160 | 145 |
| 161 def GetChromeTraceConfigForDevTools(self): | 146 def GetChromeTraceConfigForDevTools(self): |
| 162 """Map the config to a DevTools API config dictionary. | 147 """Map the config to a DevTools API config dictionary. |
| 163 | 148 |
| 164 All keys in the returned dictionary use camel-case (e.g. 'enableSystrace'). | 149 All keys in the returned dictionary use camel-case (e.g. 'recordMode'). |
| 165 In addition, the 'recordMode' value also uses camel-case (e.g. | 150 In addition, the 'recordMode' value also uses camel-case (e.g. |
| 166 'recordUntilFull'). This is to invert the camel-case -> | 151 'recordUntilFull'). This is to invert the camel-case -> |
| 167 underscore/hyphen-delimited mapping performed in Chromium devtools. | 152 underscore/hyphen-delimited mapping performed in Chromium devtools. |
| 168 """ | 153 """ |
| 169 result = self.GetChromeTraceConfigForStartupTracing() | 154 result = self.GetChromeTraceConfigForStartupTracing() |
| 170 if result[RECORD_MODE_PARAM]: | 155 if result[RECORD_MODE_PARAM]: |
| 171 result[RECORD_MODE_PARAM] = ConvertStringToCamelCase( | 156 result[RECORD_MODE_PARAM] = ConvertStringToCamelCase( |
| 172 result[RECORD_MODE_PARAM]) | 157 result[RECORD_MODE_PARAM]) |
| 173 return ConvertDictKeysToCamelCaseRecursively(result) | 158 return ConvertDictKeysToCamelCaseRecursively(result) |
| 174 | 159 |
| 175 def GetChromeTraceCategoriesAndOptionsForDevTools(self): | 160 def GetChromeTraceCategoriesAndOptionsForDevTools(self): |
| 176 """Map the categories and options to their DevTools API counterparts.""" | 161 """Map the categories and options to their DevTools API counterparts.""" |
| 177 assert not self.requires_modern_devtools_tracing_start_api | 162 assert not self.requires_modern_devtools_tracing_start_api |
| 178 options_parts = [RECORD_MODE_MAP[self._record_mode]] | 163 options_parts = [RECORD_MODE_MAP[self._record_mode]] |
| 179 if self._enable_systrace: | 164 return (self._category_filter.stable_filter_string, |
| 180 options_parts.append(ENABLE_SYSTRACE) | |
| 181 return (self._tracing_category_filter.stable_filter_string, | |
| 182 ','.join(options_parts)) | 165 ','.join(options_parts)) |
| 183 | 166 |
| 184 | 167 |
| 185 class MemoryDumpConfig(object): | 168 class MemoryDumpConfig(object): |
| 186 """Stores the triggers for memory dumps in ChromeTraceConfig.""" | 169 """Stores the triggers for memory dumps in ChromeTraceConfig.""" |
| 187 def __init__(self): | 170 def __init__(self): |
| 188 self._triggers = [] | 171 self._triggers = [] |
| 189 | 172 |
| 190 def AddTrigger(self, mode, periodic_interval_ms): | 173 def AddTrigger(self, mode, periodic_interval_ms): |
| 191 """Adds a new trigger to config. | 174 """Adds a new trigger to config. |
| 192 | 175 |
| 193 Args: | 176 Args: |
| 194 periodic_interval_ms: Dump time period in milliseconds. | 177 periodic_interval_ms: Dump time period in milliseconds. |
| 195 level_of_detail: Memory dump level of detail string. | 178 level_of_detail: Memory dump level of detail string. |
| 196 Valid arguments are "background", "light" and "detailed". | 179 Valid arguments are "background", "light" and "detailed". |
| 197 """ | 180 """ |
| 198 assert mode in ['background', 'light', 'detailed'] | 181 assert mode in ['background', 'light', 'detailed'] |
| 199 assert periodic_interval_ms > 0 | 182 assert periodic_interval_ms > 0 |
| 200 self._triggers.append({'mode': mode, | 183 self._triggers.append({'mode': mode, |
| 201 'periodic_interval_ms': periodic_interval_ms}) | 184 'periodic_interval_ms': periodic_interval_ms}) |
| 202 | 185 |
| 203 def GetDictForChromeTracing(self): | 186 def GetDictForChromeTracing(self): |
| 204 """Returns the dump config as dictionary for chrome tracing.""" | 187 """Returns the dump config as dictionary for chrome tracing.""" |
| 205 # An empty trigger list would mean no periodic memory dumps. | 188 # An empty trigger list would mean no periodic memory dumps. |
| 206 return {'memory_dump_config': {'triggers': self._triggers}} | 189 return {'memory_dump_config': {'triggers': self._triggers}} |
| OLD | NEW |