| 1 | #!/usr/bin/python |
| 2 | |
| 3 | # This filter replace all occurences of JIT_PPC_${address} by a |
| 4 | # corresponding function name JIT_PPC_${symbol} as defined by a .map file. |
| 5 | # TODO, add an option to append the block address (JIT_PPC_${symbol}@${addr}) |
| 6 | |
| 7 | # Example 1: guest function profiling (excluding host callees) |
| 8 | # |
| 9 | # $ perf record -t $tid |
| 10 | # $ perf script | sed 's/.*cycles: *[0-9a-f]* *//' | |
| 11 | # python Tools/symbolicate-ppc.py ~/.dolphin-emu/Maps/${map}.map | |
| 12 | # rankor -r | head |
| 13 | # 10.05% JIT_Loop (/tmp/perf-15936.map) |
| 14 | # 3.73% [unknown] (/tmp/perf-15936.map) |
| 15 | # 1.91% VideoBackendHardware::Video_GatherPipeBursted (/opt/dolphin-2015-05-06/bin/dolphin-emu) |
| 16 | # 1.39% JIT_PPC_PSMTXConcat (/tmp/perf-15936.map) |
| 17 | # 1.00% JIT_PPC_zz_051754c_ (/tmp/perf-15936.map) |
| 18 | # 0.90% JIT_PPC_zz_051751c_ (/tmp/perf-15936.map) |
| 19 | # 0.71% JIT_PPC_zz_04339d4_ (/tmp/perf-15936.map) |
| 20 | # 0.59% JIT_PPC_zz_05173e0_ (/tmp/perf-15936.map) |
| 21 | # 0.57% JIT_PPC_zz_044141c_ (/tmp/perf-15936.map) |
| 22 | # 0.54% JIT_PPC_zz_01839cc_ (/tmp/perf-15936.map) |
| 23 | |
| 24 | # Example 2: guest function profiling (including host callees) |
| 25 | # |
| 26 | # $ perf record --call-graph dwarf -t $tid |
| 27 | # $ perf script | stackcollapse-perf.pl | sed 's/^CPU;//' | |
| 28 | # python Tools/symbolicate-ppc.py ~/.dolphin-emu/Maps/${map}.map | |
| 29 | # perl -pe 's/^([^; ]*).*? ([0-9]+?)$/\1 \2/' | stackcollapse-recursive.pl | |
| 30 | # awk '{printf "%s %s\n", $2, $1}' | sort -rn | head |
| 31 | # 5811 JIT_Loop |
| 32 | # 2396 [unknown] |
| 33 | # 577 JIT_PPC_PSMTXConcat |
| 34 | # 464 JIT_PPC___restore_gpr |
| 35 | # 396 JIT_PPC_zz_0517514_ |
| 36 | # 313 JIT_PPC_zz_04339d4_ |
| 37 | # 290 JIT_PPC_zz_05173e0_ |
| 38 | # 285 JIT_PPC_zz_01839cc_ |
| 39 | # 277 JIT_PPC_zz_04335ac_ |
| 40 | # 269 JIT_PPC_zz_0420b58_ |
| 41 | |
| 42 | import re |
| 43 | import sys |
| 44 | |
| 45 | stdin = sys.stdin |
| 46 | stdout = sys.stdout |
| 47 | |
| 48 | class Symbol: |
| 49 | def __init__(self, start, size, name): |
| 50 | self.start = start |
| 51 | self.end = start + size |
| 52 | self.name = name |
| 53 | |
| 54 | # Read a .map file: this is a line-oriented file containing mapping from |
| 55 | # the (PowerPC) memory addresses to function names. |
| 56 | # The format is: "%08x %08x %08x %i %s" (address, size, address, 0, name). |
| 57 | # They should be already be sorted. |
| 58 | def read_map(filename): |
| 59 | reg = re.compile("^([0-9a-f]{8}) ([0-9a-f]{8}) ([0-9a-f]{8}) ([0-9]*) (.*)$") |
| 60 | res = [] |
| 61 | with open(filename, "r") as f: |
| 62 | for line in f: |
| 63 | match = reg.match(line) |
| 64 | if match: |
| 65 | start = int(match.group(1), 16) |
| 66 | size = int(match.group(2), 16) |
| 67 | name = match.group(5) |
| 68 | res.append(Symbol(start, size, name)) |
| 69 | return res |
| 70 | |
| 71 | map = read_map(sys.argv[1]) |
| 72 | |
| 73 | # Do a binary each in the map file in order to find the symbol: |
| 74 | def lookup(address): |
| 75 | i = 0 |
| 76 | j = len(map) |
| 77 | while True: |
| 78 | if j < i: |
| 79 | return "JIT_PPC_[unknown]" |
| 80 | k = round((j + i) // 2) |
| 81 | if address < map[k].start: |
| 82 | j = k - 1 |
| 83 | elif address >= map[k].end: |
| 84 | i = k + 1 |
| 85 | else: |
| 86 | return "JIT_PPC_" + map[k].name |
| 87 | |
| 88 | # Function used to replace given match: |
| 89 | def replace(match): |
| 90 | return lookup(int(match.group(1), 16)) |
| 91 | |
| 92 | # Process stdin and write to stdout: |
| 93 | for line in stdin: |
| 94 | modline = re.sub('JIT_PPC_([0-9a-f]*)', replace, line) |
| 95 | stdout.write(modline) |