The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 1 | #!/usr/bin/python2.4 -E |
| 2 | |
| 3 | import os |
| 4 | import re |
| 5 | import sys |
| 6 | |
| 7 | def PrintUsage(): |
Jing Yu | df3ced8 | 2009-11-09 16:49:37 -0800 | [diff] [blame] | 8 | print "Usage:" + sys.argv[0] + " [-s serial_number] [-r] dir" |
| 9 | print " serial_number: the device being profiled" |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 10 | print " -r : reuse the directory if it already exists" |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 11 | print " dir: directory on the host to store profile results" |
| 12 | |
Jeff Brown | b415fab | 2011-01-11 12:38:32 -0800 | [diff] [blame] | 13 | if (len(sys.argv) <= 1 or len(sys.argv) > 5): |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 14 | PrintUsage() |
| 15 | sys.exit(1) |
| 16 | |
Jeff Brown | b415fab | 2011-01-11 12:38:32 -0800 | [diff] [blame] | 17 | # find binaries |
| 18 | try: |
| 19 | oprofile_bin_dir = os.environ['OPROFILE_BIN_DIR'] |
| 20 | except: |
Jeff Brown | c3efb23 | 2011-03-17 16:08:14 -0700 | [diff] [blame] | 21 | try: |
| 22 | android_host_out = os.environ['ANDROID_HOST_OUT'] |
| 23 | except: |
| 24 | print "Either OPROFILE_BIN_DIR or ANDROID_HOST_OUT must be set. Run \". envsetup.sh\" first" |
| 25 | sys.exit(1) |
| 26 | oprofile_bin_dir = android_host_out + '/bin' |
Jeff Brown | b415fab | 2011-01-11 12:38:32 -0800 | [diff] [blame] | 27 | |
Jing Yu | df3ced8 | 2009-11-09 16:49:37 -0800 | [diff] [blame] | 28 | argv_next = 1 |
| 29 | if sys.argv[1] == "-s": |
| 30 | if len(sys.argv) < 4: |
| 31 | PrintUsage() |
| 32 | sys.exit(1) |
| 33 | device = " -s %s" % sys.argv[2] |
| 34 | argv_next = argv_next + 2 |
| 35 | else: |
| 36 | device = "" |
| 37 | |
| 38 | if sys.argv[argv_next] == "-r" : |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 39 | replace_dir = 1 |
Jing Yu | df3ced8 | 2009-11-09 16:49:37 -0800 | [diff] [blame] | 40 | output_dir = sys.argv[argv_next+1] |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 41 | else: |
| 42 | replace_dir = 0 |
Jing Yu | df3ced8 | 2009-11-09 16:49:37 -0800 | [diff] [blame] | 43 | output_dir = sys.argv[argv_next] |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 44 | |
| 45 | if (os.path.exists(output_dir) and (replace_dir == 1)): |
| 46 | os.system("rm -fr " + output_dir) |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 47 | |
| 48 | try: |
| 49 | os.makedirs(output_dir) |
| 50 | except: |
| 51 | if os.path.exists(output_dir): |
| 52 | print "Directory already exists:", output_dir |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 53 | print "Try \"" + sys.argv[0] + " -r " + output_dir + "\"" |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 54 | else: |
| 55 | print "Cannot create", output_dir |
| 56 | sys.exit(1) |
| 57 | |
| 58 | # get the samples off the phone |
Jing Yu | df3ced8 | 2009-11-09 16:49:37 -0800 | [diff] [blame] | 59 | result = os.system("adb%s pull /data/oprofile/samples %s/raw_samples " |
| 60 | "> /dev/null 2>&1" % (device, output_dir)) |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 61 | if result != 0: |
Jing Yu | df3ced8 | 2009-11-09 16:49:37 -0800 | [diff] [blame] | 62 | print "adb%s pull failure, exiting" % device |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 63 | sys.exit(1) |
| 64 | |
Jeff Brown | b415fab | 2011-01-11 12:38:32 -0800 | [diff] [blame] | 65 | # get the ABI information off the phone |
| 66 | result = os.system("adb%s pull /data/oprofile/abi %s/abi " |
| 67 | "> /dev/null 2>&1" % (device, output_dir)) |
| 68 | if result != 0: |
| 69 | print "adb%s pull failure, exiting" % device |
| 70 | sys.exit(1) |
| 71 | |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 72 | # enter the destination directory |
| 73 | os.chdir(output_dir) |
Ben Cheng | afec5b9 | 2010-02-22 15:33:08 -0800 | [diff] [blame] | 74 | |
| 75 | # We need to replace the " (deleted)" part in the directory names if |
| 76 | # the region is allocated through ashmem. The post-processing tool doesn't like |
| 77 | # space and parentheses. |
| 78 | # Rename each individual directory from the longest first |
| 79 | # For example, first rename |
| 80 | # raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache (deleted)/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache (deleted) |
| 81 | # to |
| 82 | # raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache (deleted)/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache |
| 83 | # then to |
| 84 | # raw_samples/current/{root}/dev/ashmem/dalvik-jit-code-cache/{dep}/{root}/dev/ashmem/dalvik-jit-code-cache |
| 85 | deleted_pattern = re.compile(" \(deleted\)$"); |
| 86 | stream = os.popen("find raw_samples -type d -name \*\ \(deleted\)\* | sort -r") |
| 87 | for line in stream: |
| 88 | line = line.rstrip() |
| 89 | new_dir = deleted_pattern.sub("", line) |
| 90 | cmd = "mv " + "\"" + line + "\" \"" + new_dir + "\"" |
| 91 | os.system(cmd) |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 92 | |
| 93 | # now all the sample files are on the host, we need to invoke opimport one at a |
| 94 | # time to convert the content from the ARM abi to x86 ABI |
| 95 | |
| 96 | # break the full filename into: |
| 97 | # 1: leading dir: "raw_samples" |
| 98 | # 2: intermediate dirs: "/blah/blah/blah" |
| 99 | # 3: filename: e.g. "CPU_CYCLES.150000.0.all.all.all" |
| 100 | pattern = re.compile("(^raw_samples)(.*)/(.*)$") |
Ben Cheng | afec5b9 | 2010-02-22 15:33:08 -0800 | [diff] [blame] | 101 | |
| 102 | stream = os.popen("find raw_samples -type f -name \*all") |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 103 | for line in stream: |
| 104 | match = pattern.search(line) |
| 105 | leading_dir = match.group(1) |
| 106 | middle_part = match.group(2) |
| 107 | file_name = match.group(3) |
| 108 | |
| 109 | dir = "samples" + middle_part |
| 110 | |
| 111 | # if multiple events are collected the directory could have been setup |
| 112 | if not os.path.exists(dir): |
| 113 | os.makedirs(dir) |
| 114 | |
Jeff Brown | b415fab | 2011-01-11 12:38:32 -0800 | [diff] [blame] | 115 | cmd = oprofile_bin_dir + "/opimport -a abi " \ |
| 116 | + " -o samples" + middle_part + "/" + file_name + " " + line |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 117 | os.system(cmd) |
| 118 | |
| 119 | stream.close() |
| 120 | |
| 121 | # short summary of profiling results |
Jeff Brown | b415fab | 2011-01-11 12:38:32 -0800 | [diff] [blame] | 122 | os.system(oprofile_bin_dir + "/opreport --session-dir=.") |