summaryrefslogtreecommitdiff
path: root/systrace/catapult/systrace/profile_chrome/ddms_tracing_agent.py
blob: 9d041b91a8d8c4286893d51e4d0f386b3ebbd302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import optparse
import os
import py_utils
import re

from profile_chrome import util
from systrace import trace_result
from systrace import tracing_agents


_DDMS_SAMPLING_FREQUENCY_US = 100


class DdmsAgent(tracing_agents.TracingAgent):
  def __init__(self, device, package_info):
    tracing_agents.TracingAgent.__init__(self)
    self._device = device
    self._package = package_info.package
    self._output_file = None
    self._supports_sampling = self._SupportsSampling()

  def __repr__(self):
    return 'ddms profile'

  def _SupportsSampling(self):
    for line in self._device.RunShellCommand(
        ['am', '--help'], check_return=True):
      if re.match(r'.*am profile start.*--sampling', line):
        return True
    return False

  @py_utils.Timeout(tracing_agents.START_STOP_TIMEOUT)
  def StartAgentTracing(self, config, timeout=None):
    self._output_file = (
        '/data/local/tmp/ddms-profile-%s' % util.GetTraceTimestamp())
    cmd = ['am', 'profile', 'start']
    if self._supports_sampling:
      cmd.extend(['--sampling', str(_DDMS_SAMPLING_FREQUENCY_US)])
    cmd.extend([self._package, self._output_file])
    self._device.RunShellCommand(cmd, check_return=True)
    return True

  @py_utils.Timeout(tracing_agents.START_STOP_TIMEOUT)
  def StopAgentTracing(self, timeout=None):
    self._device.RunShellCommand(
        ['am', 'profile', 'stop', self._package], check_return=True)
    return True

  @py_utils.Timeout(tracing_agents.GET_RESULTS_TIMEOUT)
  def GetResults(self, timeout=None):
    with open(self._PullTrace(), 'r') as f:
      trace_data = f.read()
    return trace_result.TraceResult('ddms', trace_data)

  def _PullTrace(self):
    if not self._output_file:
      return None

    host_file = os.path.join(
        os.path.curdir, os.path.basename(self._output_file))
    self._device.PullFile(self._output_file, host_file)
    return host_file

  def SupportsExplicitClockSync(self):
    return False

  def RecordClockSyncMarker(self, sync_id, did_record_sync_marker_callback):
    # pylint: disable=unused-argument
    assert self.SupportsExplicitClockSync(), ('Clock sync marker cannot be '
        'recorded since explicit clock sync is not supported.')


class DdmsConfig(tracing_agents.TracingConfig):
  def __init__(self, device, package_info, ddms):
    tracing_agents.TracingConfig.__init__(self)
    self.device = device
    self.package_info = package_info
    self.ddms = ddms


def try_create_agent(config):
  if config.ddms:
    return DdmsAgent(config.device, config.package_info)
  return None

def add_options(parser):
  options = optparse.OptionGroup(parser, 'Java tracing')
  options.add_option('--ddms', help='Trace Java execution using DDMS '
                     'sampling.', action='store_true')
  return options

def get_config(options):
  return DdmsConfig(options.device, options.package_info, options.ddms)