blob: b3a257c13e5519e88d84d124732bbb7e050e760c [file] [log] [blame]
The Android Open Source Project10e23ee2009-03-03 19:30:30 -08001#!/usr/bin/python2.4 -E
2
3import os
4import re
5import sys
6
7def PrintUsage():
Jing Yudf3ced82009-11-09 16:49:37 -08008 print "Usage:" + sys.argv[0] + " [-s serial_number] [-r] dir"
9 print " serial_number: the device being profiled"
Ben Chengd6eeae32009-10-07 12:28:49 -070010 print " -r : reuse the directory if it already exists"
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080011 print " dir: directory on the host to store profile results"
12
Jeff Brownb415fab2011-01-11 12:38:32 -080013if (len(sys.argv) <= 1 or len(sys.argv) > 5):
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080014 PrintUsage()
15 sys.exit(1)
16
Jeff Brownb415fab2011-01-11 12:38:32 -080017# find prebuilt event data
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080018try:
19 oprofile_event_dir = os.environ['OPROFILE_EVENTS_DIR']
20except:
Jeff Brownb415fab2011-01-11 12:38:32 -080021 # TODO: We can remove this in the future since we pull the abi data
22 # off the device so we don't need it to be in the prebuilts directory.
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080023 print "OPROFILE_EVENTS_DIR not set. Run \". envsetup.sh\" first"
24 sys.exit(1)
25
Jeff Brownb415fab2011-01-11 12:38:32 -080026# find binaries
27try:
28 oprofile_bin_dir = os.environ['OPROFILE_BIN_DIR']
29except:
30 # TODO: This assumes that the bin dir is prebuilt.
31 # When the host libbfd dependency is resolved and we can compile oprofile
32 # on the host, we should use those binaries instead.
33 oprofile_bin_dir = oprofile_event_dir + '/bin'
34
Jing Yudf3ced82009-11-09 16:49:37 -080035argv_next = 1
36if sys.argv[1] == "-s":
37 if len(sys.argv) < 4:
38 PrintUsage()
39 sys.exit(1)
40 device = " -s %s" % sys.argv[2]
41 argv_next = argv_next + 2
42else:
43 device = ""
44
45if sys.argv[argv_next] == "-r" :
Ben Chengd6eeae32009-10-07 12:28:49 -070046 replace_dir = 1
Jing Yudf3ced82009-11-09 16:49:37 -080047 output_dir = sys.argv[argv_next+1]
Ben Chengd6eeae32009-10-07 12:28:49 -070048else:
49 replace_dir = 0
Jing Yudf3ced82009-11-09 16:49:37 -080050 output_dir = sys.argv[argv_next]
Ben Chengd6eeae32009-10-07 12:28:49 -070051
52if (os.path.exists(output_dir) and (replace_dir == 1)):
53 os.system("rm -fr " + output_dir)
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080054
55try:
56 os.makedirs(output_dir)
57except:
58 if os.path.exists(output_dir):
59 print "Directory already exists:", output_dir
Ben Chengd6eeae32009-10-07 12:28:49 -070060 print "Try \"" + sys.argv[0] + " -r " + output_dir + "\""
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080061 else:
62 print "Cannot create", output_dir
63 sys.exit(1)
64
65# get the samples off the phone
Jing Yudf3ced82009-11-09 16:49:37 -080066result = os.system("adb%s pull /data/oprofile/samples %s/raw_samples "
67 "> /dev/null 2>&1" % (device, output_dir))
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080068if result != 0:
Jing Yudf3ced82009-11-09 16:49:37 -080069 print "adb%s pull failure, exiting" % device
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080070 sys.exit(1)
71
Jeff Brownb415fab2011-01-11 12:38:32 -080072# get the ABI information off the phone
73result = os.system("adb%s pull /data/oprofile/abi %s/abi "
74 "> /dev/null 2>&1" % (device, output_dir))
75if result != 0:
76 print "adb%s pull failure, exiting" % device
77 sys.exit(1)
78
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080079# enter the destination directory
80os.chdir(output_dir)
Ben Chengafec5b92010-02-22 15:33:08 -080081
82# We need to replace the " (deleted)" part in the directory names if
83# the region is allocated through ashmem. The post-processing tool doesn't like
84# space and parentheses.
85# Rename each individual directory from the longest first
86# For example, first rename
87# raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache (deleted)/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache (deleted)
88# to
89# raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache (deleted)/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache
90# then to
91# raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache
92deleted_pattern = re.compile(" \(deleted\)$");
93stream = os.popen("find raw_samples -type d -name \*\ \(deleted\)\* | sort -r")
94for line in stream:
95 line = line.rstrip()
96 new_dir = deleted_pattern.sub("", line)
97 cmd = "mv " + "\"" + line + "\" \"" + new_dir + "\""
98 os.system(cmd)
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080099
100# now all the sample files are on the host, we need to invoke opimport one at a
101# time to convert the content from the ARM abi to x86 ABI
102
103# break the full filename into:
104# 1: leading dir: "raw_samples"
105# 2: intermediate dirs: "/blah/blah/blah"
106# 3: filename: e.g. "CPU_CYCLES.150000.0.all.all.all"
107pattern = re.compile("(^raw_samples)(.*)/(.*)$")
Ben Chengafec5b92010-02-22 15:33:08 -0800108
109stream = os.popen("find raw_samples -type f -name \*all")
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800110for line in stream:
111 match = pattern.search(line)
112 leading_dir = match.group(1)
113 middle_part = match.group(2)
114 file_name = match.group(3)
115
116 dir = "samples" + middle_part
117
118 # if multiple events are collected the directory could have been setup
119 if not os.path.exists(dir):
120 os.makedirs(dir)
121
Jeff Brownb415fab2011-01-11 12:38:32 -0800122 cmd = oprofile_bin_dir + "/opimport -a abi " \
123 + " -o samples" + middle_part + "/" + file_name + " " + line
The Android Open Source Project10e23ee2009-03-03 19:30:30 -0800124 os.system(cmd)
125
126stream.close()
127
128# short summary of profiling results
Jeff Brownb415fab2011-01-11 12:38:32 -0800129os.system(oprofile_bin_dir + "/opreport --session-dir=.")