aboutsummaryrefslogtreecommitdiff
path: root/opimport_pull
blob: ed820f0fa41973ee8d06157a9095077999d71dff (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
#!/usr/bin/python2.4 -E

import os
import re
import sys

def PrintUsage():
    print "Usage:" + sys.argv[0] + " [-s serial_number] [-r] dir"
    print "    serial_number: the device being profiled"
    print "    -r : reuse the directory if it already exists"
    print "    dir: directory on the host to store profile results"

if (len(sys.argv) > 5):
    PrintUsage()
    sys.exit(1)

# identify 32-bit vs 64-bit platform
stream = os.popen("uname -m")
arch_name = stream.readline().rstrip("\n");
stream.close()

# default path is prebuilt/linux-x86/oprofile
# for 64-bit OS, use prebuilt/linux-x86_64/oprofile instead
if arch_name == "x86_64":
    arch_path = "/../../linux-x86_64/oprofile"
else:
    arch_path = ""

try:
    oprofile_event_dir = os.environ['OPROFILE_EVENTS_DIR']
except:
    print "OPROFILE_EVENTS_DIR not set. Run \". envsetup.sh\" first"
    sys.exit(1)

argv_next = 1
if sys.argv[1] == "-s":
  if len(sys.argv) < 4:
    PrintUsage()
    sys.exit(1)
  device = " -s %s" % sys.argv[2]
  argv_next = argv_next + 2
else:
  device = ""

if sys.argv[argv_next] == "-r" :
    replace_dir = 1
    output_dir = sys.argv[argv_next+1]
else:
    replace_dir = 0
    output_dir = sys.argv[argv_next]

if (os.path.exists(output_dir) and (replace_dir == 1)):
    os.system("rm -fr " + output_dir)

try:
    os.makedirs(output_dir)
except:
    if os.path.exists(output_dir):
        print "Directory already exists:", output_dir
        print "Try \"" + sys.argv[0] + " -r " + output_dir + "\""
    else:
        print "Cannot create", output_dir
    sys.exit(1)

# get the samples off the phone
result = os.system("adb%s pull /data/oprofile/samples %s/raw_samples "
                   "> /dev/null 2>&1" % (device, output_dir))
if result != 0:
    print "adb%s pull failure, exiting" % device
    sys.exit(1)

# enter the destination directory
os.chdir(output_dir)

# We need to replace the " (deleted)" part in the directory names if
# the region is allocated through ashmem. The post-processing tool doesn't like
# space and parentheses.
# Rename each individual directory from the longest first
# For example, first rename
# raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache (deleted)/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache (deleted)
# to
# raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache (deleted)/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache
# then to
# raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache
deleted_pattern = re.compile(" \(deleted\)$");
stream = os.popen("find raw_samples -type d -name \*\ \(deleted\)\* | sort -r")
for line in stream:
    line = line.rstrip()
    new_dir = deleted_pattern.sub("", line)
    cmd = "mv " + "\"" + line + "\" \"" + new_dir + "\""
    os.system(cmd)

# now all the sample files are on the host, we need to invoke opimport one at a
# time to convert the content from the ARM abi to x86 ABI

# break the full filename into:
# 1: leading dir: "raw_samples"
# 2: intermediate dirs: "/blah/blah/blah"
# 3: filename: e.g. "CPU_CYCLES.150000.0.all.all.all"
pattern = re.compile("(^raw_samples)(.*)/(.*)$")

stream = os.popen("find raw_samples -type f -name \*all")
for line in stream:
    match = pattern.search(line)
    leading_dir = match.group(1)
    middle_part = match.group(2)
    file_name = match.group(3)

    dir = "samples" + middle_part

    # if multiple events are collected the directory could have been setup
    if not os.path.exists(dir):
        os.makedirs(dir)

    cmd = oprofile_event_dir + arch_path + "/bin/opimport -a " + \
          oprofile_event_dir + \
          "/abi/arm_abi -o samples" + middle_part + "/" + file_name + " " + line
    os.system(cmd)

stream.close()

# short summary of profiling results
os.system(oprofile_event_dir + arch_path + "/bin/opreport --session-dir=.")