| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import os |
| 16 |
import sys |
| 17 |
import json |
| 18 |
import subprocess |
| 19 |
from collections import defaultdict |
| 20 |
|
| 21 |
sys.path.append(os.environ["PERF_EXEC_PATH"] + "/scripts/python/Perf-Trace-Util/lib/Perf/Trace") |
| 22 |
|
| 23 |
from perf_trace_context import * |
| 24 |
|
| 25 |
try: |
| 26 |
from subprocess import DEVNULL |
| 27 |
except ImportError: |
| 28 |
import os |
| 29 |
DEVNULL = open(os.devnull, 'wb') |
| 30 |
|
| 31 |
class FileInfo: |
| 32 |
def __init__(self, file, line): |
| 33 |
self.file = file |
| 34 |
self.line = line |
| 35 |
|
| 36 |
class InternMap: |
| 37 |
def __init__(self): |
| 38 |
self.map = dict() |
| 39 |
self.map[""] = 0; |
| 40 |
|
| 41 |
def add(self, string): |
| 42 |
nextIndex = len(self.map) |
| 43 |
index = self.map.get(string, nextIndex) |
| 44 |
if index == nextIndex: |
| 45 |
print("s %s" % string) |
| 46 |
self.map[string] = nextIndex |
| 47 |
return nextIndex |
| 48 |
return index |
| 49 |
|
| 50 |
strings = InternMap() |
| 51 |
|
| 52 |
def addr2line(dsoName, address): |
| 53 |
try: |
| 54 |
output = subprocess.check_output(["addr2line", "-e", dsoName, hex(address)], stderr=DEVNULL).split(':') |
| 55 |
file = output[0] |
| 56 |
if not file or file == "??": |
| 57 |
raise "error" |
| 58 |
line = int(output[1]) |
| 59 |
return FileInfo(file, line) |
| 60 |
except: |
| 61 |
return FileInfo("???", 0) |
| 62 |
|
| 63 |
nextIpIndex = 1 |
| 64 |
class InstructionPointerMap: |
| 65 |
def __init__(self): |
| 66 |
self.map = dict() |
| 67 |
|
| 68 |
def add(self, ip, dsoName, name, sym): |
| 69 |
ipEntry = self.map.get(ip, None) |
| 70 |
if ipEntry == None: |
| 71 |
global nextIpIndex |
| 72 |
ipEntry = nextIpIndex |
| 73 |
nextIpIndex += 1 |
| 74 |
fileInfo = addr2line(dsoName, ip) |
| 75 |
print("i %x %x %x %x %d" % (ip, strings.add(dsoName), strings.add(name), strings.add(fileInfo.file), fileInfo.line)) |
| 76 |
self.map[ip] = ipEntry |
| 77 |
return ipEntry |
| 78 |
ipMap = InstructionPointerMap() |
| 79 |
|
| 80 |
nextTraceIndex = 1 |
| 81 |
class TraceEntry: |
| 82 |
def __init__(self, traceIndex): |
| 83 |
self.index = traceIndex |
| 84 |
self.children = dict() |
| 85 |
|
| 86 |
def add(self, ipIndex): |
| 87 |
child = self.children.get(ipIndex, None) |
| 88 |
if child == None: |
| 89 |
global nextTraceIndex |
| 90 |
child = TraceEntry(nextTraceIndex) |
| 91 |
nextTraceIndex += 1 |
| 92 |
print("t %x %x" % (ipIndex, self.index)) |
| 93 |
self.children[ipIndex] = child |
| 94 |
return child |
| 95 |
|
| 96 |
traceRoot = TraceEntry(0) |
| 97 |
|
| 98 |
nextSampleIndex = 0 |
| 99 |
class SampleMap: |
| 100 |
def __init__(self): |
| 101 |
self.map = dict() |
| 102 |
|
| 103 |
def add(self, traceIndex): |
| 104 |
sampleIndex = self.map.get(traceIndex, None) |
| 105 |
if sampleIndex == None: |
| 106 |
global nextSampleIndex |
| 107 |
sampleIndex = nextSampleIndex |
| 108 |
nextSampleIndex += 1 |
| 109 |
print("a 1 %x" % (traceIndex)) |
| 110 |
self.map[traceIndex] = sampleIndex |
| 111 |
return sampleIndex |
| 112 |
samples = SampleMap() |
| 113 |
|
| 114 |
|
| 115 |
def trace_begin(): |
| 116 |
print("v 10000") |
| 117 |
print("# perf.data converted using perf-heaptrack.py") |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
startTime = 0 |
| 123 |
lastTime = 0 |
| 124 |
finalTime = 0 |
| 125 |
|
| 126 |
def trace_end(): |
| 127 |
print("c %x" % (finalTime + 1)) |
| 128 |
|
| 129 |
def process_event(event): |
| 130 |
global startTime, lastTime, finalTime |
| 131 |
if startTime == 0: |
| 132 |
startTime = event["sample"]["time"] |
| 133 |
elapsed = (event["sample"]["time"] - startTime) / 10000000 |
| 134 |
if (lastTime + 1) <= elapsed: |
| 135 |
print("c %x" % elapsed) |
| 136 |
lastTime = elapsed |
| 137 |
finalTime = elapsed |
| 138 |
|
| 139 |
global ipMap, traceRoot, samples |
| 140 |
trace = traceRoot |
| 141 |
if not event["callchain"]: |
| 142 |
dsoName = event.get("dso", "???") |
| 143 |
name = event.get("symbol", "???") |
| 144 |
ipIndex = ipMap.add(event["sample"]["ip"], dsoName, name, None) |
| 145 |
traceIndex = trace.add(ipIndex).index |
| 146 |
else: |
| 147 |
|
| 148 |
for item in reversed(event["callchain"]): |
| 149 |
dsoName = item.get("dso", "???") |
| 150 |
name = "???" |
| 151 |
if "sym" in item: |
| 152 |
name = item["sym"]["name"] |
| 153 |
ipIndex = ipMap.add(item["ip"], dsoName, name, item.get("sym", None)) |
| 154 |
trace = trace.add(ipIndex) |
| 155 |
traceIndex = trace.index |
| 156 |
sampleIndex = samples.add(traceIndex) |
| 157 |
print("+ %x" % sampleIndex) |