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 prebuilt event data |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 18 | try: |
| 19 | oprofile_event_dir = os.environ['OPROFILE_EVENTS_DIR'] |
| 20 | except: |
Jeff Brown | b415fab | 2011-01-11 12:38:32 -0800 | [diff] [blame] | 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. |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 23 | print "OPROFILE_EVENTS_DIR not set. Run \". envsetup.sh\" first" |
| 24 | sys.exit(1) |
| 25 | |
Jeff Brown | b415fab | 2011-01-11 12:38:32 -0800 | [diff] [blame] | 26 | # find binaries |
| 27 | try: |
| 28 | oprofile_bin_dir = os.environ['OPROFILE_BIN_DIR'] |
| 29 | except: |
| 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 | |
Jing Yu | df3ced8 | 2009-11-09 16:49:37 -0800 | [diff] [blame] | 35 | argv_next = 1 |
| 36 | if 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 |
| 42 | else: |
| 43 | device = "" |
| 44 | |
| 45 | if sys.argv[argv_next] == "-r" : |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 46 | replace_dir = 1 |
Jing Yu | df3ced8 | 2009-11-09 16:49:37 -0800 | [diff] [blame] | 47 | output_dir = sys.argv[argv_next+1] |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 48 | else: |
| 49 | replace_dir = 0 |
Jing Yu | df3ced8 | 2009-11-09 16:49:37 -0800 | [diff] [blame] | 50 | output_dir = sys.argv[argv_next] |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 51 | |
| 52 | if (os.path.exists(output_dir) and (replace_dir == 1)): |
| 53 | os.system("rm -fr " + output_dir) |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 54 | |
| 55 | try: |
| 56 | os.makedirs(output_dir) |
| 57 | except: |
| 58 | if os.path.exists(output_dir): |
| 59 | print "Directory already exists:", output_dir |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 60 | print "Try \"" + sys.argv[0] + " -r " + output_dir + "\"" |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 61 | else: |
| 62 | print "Cannot create", output_dir |
| 63 | sys.exit(1) |
| 64 | |
| 65 | # get the samples off the phone |
Jing Yu | df3ced8 | 2009-11-09 16:49:37 -0800 | [diff] [blame] | 66 | result = os.system("adb%s pull /data/oprofile/samples %s/raw_samples " |
| 67 | "> /dev/null 2>&1" % (device, output_dir)) |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 68 | if result != 0: |
Jing Yu | df3ced8 | 2009-11-09 16:49:37 -0800 | [diff] [blame] | 69 | print "adb%s pull failure, exiting" % device |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 70 | sys.exit(1) |
| 71 | |
Jeff Brown | b415fab | 2011-01-11 12:38:32 -0800 | [diff] [blame] | 72 | # get the ABI information off the phone |
| 73 | result = os.system("adb%s pull /data/oprofile/abi %s/abi " |
| 74 | "> /dev/null 2>&1" % (device, output_dir)) |
| 75 | if result != 0: |
| 76 | print "adb%s pull failure, exiting" % device |
| 77 | sys.exit(1) |
| 78 | |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 79 | # enter the destination directory |
| 80 | os.chdir(output_dir) |
Ben Cheng | afec5b9 | 2010-02-22 15:33:08 -0800 | [diff] [blame] | 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 |
| 92 | deleted_pattern = re.compile(" \(deleted\)$"); |
| 93 | stream = os.popen("find raw_samples -type d -name \*\ \(deleted\)\* | sort -r") |
| 94 | for line in stream: |
| 95 | line = line.rstrip() |
| 96 | new_dir = deleted_pattern.sub("", line) |
| 97 | cmd = "mv " + "\"" + line + "\" \"" + new_dir + "\"" |
| 98 | os.system(cmd) |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 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" |
| 107 | pattern = re.compile("(^raw_samples)(.*)/(.*)$") |
Ben Cheng | afec5b9 | 2010-02-22 15:33:08 -0800 | [diff] [blame] | 108 | |
| 109 | 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] | 110 | for 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 | |
Jeff Brown | b415fab | 2011-01-11 12:38:32 -0800 | [diff] [blame] | 122 | cmd = oprofile_bin_dir + "/opimport -a abi " \ |
| 123 | + " -o samples" + middle_part + "/" + file_name + " " + line |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 124 | os.system(cmd) |
| 125 | |
| 126 | stream.close() |
| 127 | |
| 128 | # short summary of profiling results |
Jeff Brown | b415fab | 2011-01-11 12:38:32 -0800 | [diff] [blame] | 129 | os.system(oprofile_bin_dir + "/opreport --session-dir=.") |