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(): |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 8 | print "Usage:" + sys.argv[0] + " [-r] dir" |
| 9 | print " -r : reuse the directory if it already exists" |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 10 | print " dir: directory on the host to store profile results" |
| 11 | |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 12 | if (len(sys.argv) > 3): |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 13 | PrintUsage() |
| 14 | sys.exit(1) |
| 15 | |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 16 | # identify 32-bit vs 64-bit platform |
| 17 | stream = os.popen("uname -m") |
| 18 | arch_name = stream.readline().rstrip("\n"); |
| 19 | stream.close() |
| 20 | |
| 21 | # default path is prebuilt/linux-x86/oprofile |
| 22 | # for 64-bit OS, use prebuilt/linux-x86_64/oprofile instead |
| 23 | if arch_name == "x86_64": |
| 24 | arch_path = "/../../linux-x86_64/oprofile" |
| 25 | else: |
| 26 | arch_path = "" |
| 27 | |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 28 | try: |
| 29 | oprofile_event_dir = os.environ['OPROFILE_EVENTS_DIR'] |
| 30 | except: |
| 31 | print "OPROFILE_EVENTS_DIR not set. Run \". envsetup.sh\" first" |
| 32 | sys.exit(1) |
| 33 | |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 34 | if sys.argv[1] == "-r" : |
| 35 | replace_dir = 1 |
| 36 | output_dir = sys.argv[2] |
| 37 | else: |
| 38 | replace_dir = 0 |
| 39 | output_dir = sys.argv[1] |
| 40 | |
| 41 | if (os.path.exists(output_dir) and (replace_dir == 1)): |
| 42 | os.system("rm -fr " + output_dir) |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 43 | |
| 44 | try: |
| 45 | os.makedirs(output_dir) |
| 46 | except: |
| 47 | if os.path.exists(output_dir): |
| 48 | print "Directory already exists:", output_dir |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 49 | print "Try \"" + sys.argv[0] + " -r " + output_dir + "\"" |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 50 | else: |
| 51 | print "Cannot create", output_dir |
| 52 | sys.exit(1) |
| 53 | |
| 54 | # get the samples off the phone |
| 55 | result = os.system("adb pull /data/oprofile/samples " + output_dir + \ |
| 56 | "/raw_samples > /dev/null 2>&1") |
| 57 | if result != 0: |
| 58 | print "adb pull failure, exiting" |
| 59 | sys.exit(1) |
| 60 | |
| 61 | # enter the destination directory |
| 62 | os.chdir(output_dir) |
| 63 | stream = 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" |
| 72 | pattern = re.compile("(^raw_samples)(.*)/(.*)$") |
| 73 | for 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 | |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 85 | cmd = oprofile_event_dir + arch_path + "/bin/opimport -a " + \ |
| 86 | oprofile_event_dir + \ |
The Android Open Source Project | 10e23ee | 2009-03-03 19:30:30 -0800 | [diff] [blame] | 87 | "/abi/arm_abi -o samples" + middle_part + "/" + file_name + " " + line |
| 88 | os.system(cmd) |
| 89 | |
| 90 | stream.close() |
| 91 | |
| 92 | # short summary of profiling results |
Ben Cheng | d6eeae3 | 2009-10-07 12:28:49 -0700 | [diff] [blame] | 93 | os.system(oprofile_event_dir + arch_path + "/bin/opreport --session-dir=.") |