opimport_pull revision b415faba7482dd7ee3335f0f1518333554e3da0d
1#!/usr/bin/python2.4 -E
2
3import os
4import re
5import sys
6
7def PrintUsage():
8    print "Usage:" + sys.argv[0] + " [-s serial_number] [-r] dir"
9    print "    serial_number: the device being profiled"
10    print "    -r : reuse the directory if it already exists"
11    print "    dir: directory on the host to store profile results"
12
13if (len(sys.argv) <= 1 or len(sys.argv) > 5):
14    PrintUsage()
15    sys.exit(1)
16
17# find prebuilt event data
18try:
19    oprofile_event_dir = os.environ['OPROFILE_EVENTS_DIR']
20except:
21    # 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.
23    print "OPROFILE_EVENTS_DIR not set. Run \". envsetup.sh\" first"
24    sys.exit(1)
25
26# 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
35argv_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" :
46    replace_dir = 1
47    output_dir = sys.argv[argv_next+1]
48else:
49    replace_dir = 0
50    output_dir = sys.argv[argv_next]
51
52if (os.path.exists(output_dir) and (replace_dir == 1)):
53    os.system("rm -fr " + output_dir)
54
55try:
56    os.makedirs(output_dir)
57except:
58    if os.path.exists(output_dir):
59        print "Directory already exists:", output_dir
60        print "Try \"" + sys.argv[0] + " -r " + output_dir + "\""
61    else:
62        print "Cannot create", output_dir
63    sys.exit(1)
64
65# get the samples off the phone
66result = os.system("adb%s pull /data/oprofile/samples %s/raw_samples "
67                   "> /dev/null 2>&1" % (device, output_dir))
68if result != 0:
69    print "adb%s pull failure, exiting" % device
70    sys.exit(1)
71
72# 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
79# enter the destination directory
80os.chdir(output_dir)
81
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)
99
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)(.*)/(.*)$")
108
109stream = os.popen("find raw_samples -type f -name \*all")
110for 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
122    cmd = oprofile_bin_dir + "/opimport -a abi " \
123          + " -o samples" + middle_part + "/" + file_name + " " + line
124    os.system(cmd)
125
126stream.close()
127
128# short summary of profiling results
129os.system(oprofile_bin_dir + "/opreport --session-dir=.")
130