opimport_pull revision d6eeae3f5dd027a2d4e6b702cc07db4a605848c0
1#!/usr/bin/python2.4 -E
2
3import os
4import re
5import sys
6
7def PrintUsage():
8    print "Usage:" + sys.argv[0] + " [-r] dir"
9    print "    -r : reuse the directory if it already exists"
10    print "    dir: directory on the host to store profile results"
11
12if (len(sys.argv) > 3):
13    PrintUsage()
14    sys.exit(1)
15
16# identify 32-bit vs 64-bit platform
17stream = os.popen("uname -m")
18arch_name = stream.readline().rstrip("\n");
19stream.close()
20
21# default path is prebuilt/linux-x86/oprofile
22# for 64-bit OS, use prebuilt/linux-x86_64/oprofile instead
23if arch_name == "x86_64":
24    arch_path = "/../../linux-x86_64/oprofile"
25else:
26    arch_path = ""
27
28try:
29    oprofile_event_dir = os.environ['OPROFILE_EVENTS_DIR']
30except:
31    print "OPROFILE_EVENTS_DIR not set. Run \". envsetup.sh\" first"
32    sys.exit(1)
33
34if sys.argv[1] == "-r" :
35    replace_dir = 1
36    output_dir = sys.argv[2]
37else:
38    replace_dir = 0
39    output_dir = sys.argv[1]
40
41if (os.path.exists(output_dir) and (replace_dir == 1)):
42    os.system("rm -fr " + output_dir)
43
44try:
45    os.makedirs(output_dir)
46except:
47    if os.path.exists(output_dir):
48        print "Directory already exists:", output_dir
49        print "Try \"" + sys.argv[0] + " -r " + output_dir + "\""
50    else:
51        print "Cannot create", output_dir
52    sys.exit(1)
53
54# get the samples off the phone
55result = os.system("adb pull /data/oprofile/samples " + output_dir + \
56                   "/raw_samples > /dev/null 2>&1")
57if result != 0:
58    print "adb pull failure, exiting"
59    sys.exit(1)
60
61# enter the destination directory
62os.chdir(output_dir)
63stream = os.popen("find raw_samples -type f -name \*all")
64
65# now all the sample files are on the host, we need to invoke opimport one at a
66# time to convert the content from the ARM abi to x86 ABI
67
68# break the full filename into:
69# 1: leading dir: "raw_samples"
70# 2: intermediate dirs: "/blah/blah/blah"
71# 3: filename: e.g. "CPU_CYCLES.150000.0.all.all.all"
72pattern = re.compile("(^raw_samples)(.*)/(.*)$")
73for line in stream:
74    match = pattern.search(line)
75    leading_dir = match.group(1)
76    middle_part = match.group(2)
77    file_name = match.group(3)
78
79    dir = "samples" + middle_part
80
81    # if multiple events are collected the directory could have been setup
82    if not os.path.exists(dir):
83        os.makedirs(dir)
84
85    cmd = oprofile_event_dir + arch_path + "/bin/opimport -a " + \
86          oprofile_event_dir + \
87          "/abi/arm_abi -o samples" + middle_part + "/" + file_name + " " + line
88    os.system(cmd)
89
90stream.close()
91
92# short summary of profiling results
93os.system(oprofile_event_dir + arch_path + "/bin/opreport --session-dir=.")
94