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 codecs
6
7from telemetry.core.platform import profiler
8
9
10class TraceProfiler(profiler.Profiler):
11
12  def __init__(self, browser_backend, platform_backend, output_path, state,
13               categories=None):
14    super(TraceProfiler, self).__init__(
15        browser_backend, platform_backend, output_path, state)
16    assert self._browser_backend.supports_tracing
17    # We always want flow events when tracing via telemetry.
18    categories_with_flow = 'disabled-by-default-toplevel.flow'
19    if categories:
20      categories_with_flow = ',%s' % categories
21    self._browser_backend.StartTracing(categories_with_flow, timeout=10)
22
23  @classmethod
24  def name(cls):
25    return 'trace'
26
27  @classmethod
28  def is_supported(cls, browser_type):
29    return True
30
31  def CollectProfile(self):
32    print 'Processing trace...'
33
34    trace_result = self._browser_backend.StopTracing()
35
36    trace_file = '%s.json' % self._output_path
37
38    with codecs.open(trace_file, 'w', encoding='utf-8') as f:
39      trace_result.Serialize(f)
40
41    print 'Trace saved as %s' % trace_file
42    print 'To view, open in chrome://tracing'
43
44    return [trace_file]
45
46
47class TraceDetailedProfiler(TraceProfiler):
48
49  def __init__(self, browser_backend, platform_backend, output_path, state):
50    super(TraceDetailedProfiler, self).__init__(
51        browser_backend, platform_backend, output_path, state,
52        categories='disabled-by-default-cc.debug*')
53
54  @classmethod
55  def name(cls):
56    return 'trace-detailed'
57
58
59class TraceAllProfiler(TraceProfiler):
60
61  def __init__(self, browser_backend, platform_backend, output_path, state):
62    super(TraceAllProfiler, self).__init__(
63        browser_backend, platform_backend, output_path, state,
64        categories='disabled-by-default-*')
65
66  @classmethod
67  def name(cls):
68    return 'trace-all'
69