1ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch# Copyright 2013 The Chromium Authors. All rights reserved.
2ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch# Use of this source code is governed by a BSD-style license that can be
3ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch# found in the LICENSE file.
4ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
5ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochimport codecs
6ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
7ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochfrom telemetry.core.platform import profiler
86e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)from telemetry.core.platform import tracing_options
9ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
10ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
11ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochclass TraceProfiler(profiler.Profiler):
12ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def __init__(self, browser_backend, platform_backend, output_path, state,
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)               categories=None):
15ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    super(TraceProfiler, self).__init__(
164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        browser_backend, platform_backend, output_path, state)
17ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    assert self._browser_backend.supports_tracing
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    # We always want flow events when tracing via telemetry.
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    categories_with_flow = 'disabled-by-default-toplevel.flow'
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if categories:
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      categories_with_flow = ',%s' % categories
226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    options = tracing_options.TracingOptions()
236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    options.enable_chrome_trace = True
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    self._browser_backend.StartTracing(
256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        options, categories_with_flow, timeout=10)
26ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
27ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  @classmethod
28ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  def name(cls):
29ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return 'trace'
30ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
31ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  @classmethod
3258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  def is_supported(cls, browser_type):
33ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return True
34ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
35ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  def CollectProfile(self):
36ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    print 'Processing trace...'
37ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
38d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    trace_result = self._browser_backend.StopTracing()
39ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
40ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    trace_file = '%s.json' % self._output_path
41ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
42ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    with codecs.open(trace_file, 'w', encoding='utf-8') as f:
43ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch      trace_result.Serialize(f)
44ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
45ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    print 'Trace saved as %s' % trace_file
46ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    print 'To view, open in chrome://tracing'
47558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
48558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    return [trace_file]
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class TraceDetailedProfiler(TraceProfiler):
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def __init__(self, browser_backend, platform_backend, output_path, state):
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    super(TraceDetailedProfiler, self).__init__(
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        browser_backend, platform_backend, output_path, state,
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        categories='disabled-by-default-cc.debug*')
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  @classmethod
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def name(cls):
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return 'trace-detailed'
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class TraceAllProfiler(TraceProfiler):
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def __init__(self, browser_backend, platform_backend, output_path, state):
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    super(TraceAllProfiler, self).__init__(
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        browser_backend, platform_backend, output_path, state,
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        categories='disabled-by-default-*')
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  @classmethod
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  def name(cls):
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return 'trace-all'
73