• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2013 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
5import os
6
7from telemetry.internal.platform import profiler
8from telemetry.timeline import chrome_trace_category_filter
9from telemetry.timeline import tracing_config
10from tracing.trace_data import trace_data as trace_data_module
11
12
13class TraceProfiler(profiler.Profiler):
14
15  def __init__(self, browser_backend, platform_backend, output_path, state,
16               categories=None):
17    super(TraceProfiler, self).__init__(
18        browser_backend, platform_backend, output_path, state)
19    assert self._browser_backend.supports_tracing
20    # We always want flow events when tracing via telemetry.
21    categories_with_flow = 'disabled-by-default-toplevel.flow'
22    if categories:
23      categories_with_flow += ',%s' % categories
24    config = tracing_config.TracingConfig()
25    config.enable_chrome_trace = True
26    config.chrome_trace_config.SetCategoryFilter(
27        chrome_trace_category_filter.ChromeTraceCategoryFilter(
28            categories_with_flow))
29    self._browser_backend.StartTracing(config, timeout=10)
30
31  @classmethod
32  def name(cls):
33    return 'trace'
34
35  @classmethod
36  def is_supported(cls, browser_type):
37    return True
38
39  def CollectProfile(self):
40    print 'Processing trace...'
41
42    trace_result_builder = trace_data_module.TraceDataBuilder()
43    self._browser_backend.StopTracing()
44    self._browser_backend.CollectTracingData(trace_result_builder)
45    trace_result = trace_result_builder.AsData()
46    try:
47      trace_file = '%s.html' % self._output_path
48      title = os.path.basename(self._output_path)
49      trace_result.Serialize(trace_file, trace_title=title)
50    finally:
51      trace_result.CleanUpAllTraces()
52
53    print 'Trace saved as file:///%s' % os.path.abspath(trace_file)
54
55    return [trace_file]
56
57
58class TraceDetailedProfiler(TraceProfiler):
59
60  def __init__(self, browser_backend, platform_backend, output_path, state):
61    super(TraceDetailedProfiler, self).__init__(
62        browser_backend, platform_backend, output_path, state,
63        categories='disabled-by-default-cc.debug*')
64
65  @classmethod
66  def name(cls):
67    return 'trace-detailed'
68
69
70class TraceAllProfiler(TraceProfiler):
71
72  def __init__(self, browser_backend, platform_backend, output_path, state):
73    super(TraceAllProfiler, self).__init__(
74        browser_backend, platform_backend, output_path, state,
75        categories='disabled-by-default-*')
76
77  @classmethod
78  def name(cls):
79    return 'trace-all'
80