blob: e61d3e55d7e45dc8d32dd24546b576cd030a1fd3 [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
Jing Yudf3ced82009-11-09 16:49:37 -080013if (len(sys.argv) > 5):
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080014 PrintUsage()
15 sys.exit(1)
16
Ben Chengd6eeae32009-10-07 12:28:49 -070017# identify 32-bit vs 64-bit platform
18stream = os.popen("uname -m")
19arch_name = stream.readline().rstrip("\n");
20stream.close()
21
22# default path is prebuilt/linux-x86/oprofile
23# for 64-bit OS, use prebuilt/linux-x86_64/oprofile instead
24if arch_name == "x86_64":
25 arch_path = "/../../linux-x86_64/oprofile"
26else:
27 arch_path = ""
28
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080029try:
30 oprofile_event_dir = os.environ['OPROFILE_EVENTS_DIR']
31except:
32 print "OPROFILE_EVENTS_DIR not set. Run \". envsetup.sh\" first"
33 sys.exit(1)
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
72# enter the destination directory
73os.chdir(output_dir)
74stream = os.popen("find raw_samples -type f -name \*all")
75
76# now all the sample files are on the host, we need to invoke opimport one at a
77# time to convert the content from the ARM abi to x86 ABI
78
79# break the full filename into:
80# 1: leading dir: "raw_samples"
81# 2: intermediate dirs: "/blah/blah/blah"
82# 3: filename: e.g. "CPU_CYCLES.150000.0.all.all.all"
83pattern = re.compile("(^raw_samples)(.*)/(.*)$")
84for line in stream:
85 match = pattern.search(line)
86 leading_dir = match.group(1)
87 middle_part = match.group(2)
88 file_name = match.group(3)
89
90 dir = "samples" + middle_part
91
92 # if multiple events are collected the directory could have been setup
93 if not os.path.exists(dir):
94 os.makedirs(dir)
95
Ben Chengd6eeae32009-10-07 12:28:49 -070096 cmd = oprofile_event_dir + arch_path + "/bin/opimport -a " + \
97 oprofile_event_dir + \
The Android Open Source Project10e23ee2009-03-03 19:30:30 -080098 "/abi/arm_abi -o samples" + middle_part + "/" + file_name + " " + line
99 os.system(cmd)
100
101stream.close()
102
103# short summary of profiling results
Ben Chengd6eeae32009-10-07 12:28:49 -0700104os.system(oprofile_event_dir + arch_path + "/bin/opreport --session-dir=.")