aboutsummaryrefslogtreecommitdiff
path: root/oprofile_android
blob: 3373b0cc778647c4095bdc7aacc77f788877d4b7 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python2.6
#
# Copyright (C) 2011 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Remotely controls an OProfile session on an Android device.
#

import os
import sys
import subprocess
import getopt
import re


class Adb:
  def __init__(self, serial_number):
    self._base_args = ['adb']
    if serial_number != None:
      self._base_args.append('-s')
      self._base_args.append(serial_number)

  def shell(self, command_args, echo=True):
    print 'adb: %s' % (' '.join(command_args))
    popen = subprocess.Popen(self._base_args + ['shell'] + command_args,
      stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    output = ''
    while True:
      stdout, stderr = popen.communicate()
      if echo:
        print stdout
        print stderr
      output += stdout
      output += stderr
      rc = popen.poll()
      if rc is not None:
        break
    print 'exit code: %d' % rc
    return rc, output


class Tool:
  def __init__(self, argv):
    self.argv = argv
    self.verbose = False

  def usage(self):
    print "Usage:" + self.argv[0]
    print "    -h, --help          : show this help text"
    print "    -s, --serial=number : the serial number of the device being profiled"
    print "    -v, --verbose       : show verbose output"
    print "    --setup             : setup profiler"
    print "    --shutdown          : shutdown profiler"
    print "    --start             : start profiling"
    print "    --stop              : stop profiling"
    print "    --status            : show profiler status"
    print

  def main(self):
    try:
      opts, args = getopt.getopt(self.argv[1:],
        "hs:v",
        ["help", "serial=", "setup", "start", "stop", "status", "shutdown", "verbose"])
    except getopt.GetoptError, e:
      self.usage()
      print str(e)
      return 1

    serial_number = None
    command = None
    for o, a in opts:
      if o in ('-h', '--help'):
        self.usage()
        return 0
      elif o in ('-s', '--serial'):
        serial_number = a
      elif o in ('-v', '--verbose'):
        self.verbose = True
      elif o in ('--setup', '--start', '--stop', '--status', '--shutdown'):
        command = o[2:]
      else:
        assert False, 'unhandled option' + o

    self.adb = Adb(serial_number)

    if command == 'setup':
      rc = self.setup()
    elif command == 'shutdown':
      rc = self.shutdown()
    elif command == 'start':
      rc = self.start()
    elif command == 'stop':
      rc = self.stop()
    elif command == 'status':
      rc = self.status()
    else:
      self.usage()
      print 'A command must be specified.'
      rc = 1
    return rc

  def setup(self):
    rc, output = self.adb.shell(['cat', '/proc/kallsyms'], echo=False)
    if rc != 0:
      print 'Failed to determine kernel VMA range.'
      return rc
    vma_start = re.search('([0-9a-fA-F]{8}) T _text', output).group(1)
    vma_end = re.search('([0-9a-fA-F]{8}) A _etext', output).group(1)

    rc, output = self.adb.shell(['/system/xbin/opcontrol'] + self.opcontrol_verbose() + [
      '--reset',
      '--kernel-range=' + vma_start + '-' + vma_end,
      #'--event=CPU_CYCLES:100000',
      '--timer',
      '--setup',
      '--status', '--verbose-log=all'])

  def shutdown(self):
    rc, output = self.adb.shell(['/system/xbin/opcontrol'] + self.opcontrol_verbose() + [
      '--shutdown'])
    if rc != 0:
      print 'Failed to shutdown.'
      return rc
    return rc

  def start(self):
    rc, output = self.adb.shell(['/system/xbin/opcontrol'] + self.opcontrol_verbose() + [
      '--start', '--status'])
    return rc

  def stop(self):
    rc, output = self.adb.shell(['/system/xbin/opcontrol'] + self.opcontrol_verbose() + [
      '--stop', '--status'])
    return rc

  def status(self):
    rc, output = self.adb.shell(['/system/xbin/opcontrol'] + self.opcontrol_verbose() + [
      '--status'])
    return rc

  def opcontrol_verbose(self):
    if self.verbose:
      return ['--verbose']
    else:
      return []

# Main entry point
tool = Tool(sys.argv)
rc = tool.main()
sys.exit(rc)