Debian Codegraph @

perfprofd/scripts/perf_proto_stack.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2017 The Android Open Source Project
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Super simplistic printer of a perfprofd output proto. Illustrates
18 # how to parse and traverse a perfprofd output proto in Python.
19
20 # This relies on libunwindstack's unwind_symbol. Build with
21 # mmma system/core/libunwindstack
22
23 import argparse
24 from datetime import datetime
25 import itertools
26 import json
27 import logging
28 from multiprocessing.dummy import Pool as ThreadPool
29 import os.path
30 from sorted_collection import SortedCollection
31 import subprocess
32 from threading import Timer
33
34 # Generate with:
35 # aprotoc -I=external/perf_data_converter/src/quipper \
36 # --python_out=system/extras/perfprofd/scripts \
37 # external/perf_data_converter/src/quipper/perf_data.proto
38 # aprotoc -I=external/perf_data_converter/src/quipper -I=system/extras/perfprofd \
39 # --python_out=system/extras/perfprofd/scripts \
40 # system/extras/perfprofd/perfprofd_record.proto
41 import perfprofd_record_pb2
42 import perf_data_pb2
43
44 # Make sure that symbol is on the PYTHONPATH, e.g., run as
45 # PYTHONPATH=$PYTHONPATH:$ANDROID_BUILD_TOP/development/scripts python ...
46 import symbol
47 from symbol import SymbolInformation
48
49 logging.basicConfig(format='%(message)s')
50
51 # This is wrong. But then the symbol module is a bad quagmire.
52 # TODO: Check build IDs.
53 symbol.SetAbi(["ABI: 'arm64'"])
54
55
56 class MmapState(object):
57
58 def __init__(self):
59 self._list = SortedCollection((), lambda x: x[0])
60
61 def add_map(self, start, length, pgoff, name):
62 map_tuple = (start, length, pgoff, name)
63 self._list.insert(map_tuple)
64
65 def find(self, addr):
66 try:
67 map_tuple = self._list.find_le(addr)
68 if addr < map_tuple[0] + map_tuple[1]:
69 return map_tuple
70 return None
71 except ValueError:
72 return None
73
74 def copy(self):
75 ret = MmapState()
76 ret._list = self._list.copy()
77 return ret
78
79 def __str__(self):
80 return 'MmapState: ' + self._list.__str__()
81
82 def __repr__(self):
83 return self.__str__()
84
85
86 class SymbolMap(object):
87 def __init__(self, min_v):
88 self._list = SortedCollection((), lambda x: x[0])
89 self._min_vaddr = min_v
90
91 def add_symbol(self, start, length, name):
92 tuple = (start, length, name)
93 self._list.insert(tuple)
94
95 def find(self, addr):
96 try:
97 tuple = self._list.find_le(addr)
98 if addr < tuple[0] + tuple[1]:
99 return tuple[2]
100 return None
101 except ValueError:
102 return None
103
104 def copy(self):
105 ret = SymbolMap()
106 ret._list = self._list.copy()
107 return ret
108
109 def __str__(self):
110 return "SymbolMap: " + self._list.__str__()
111
112 def __repr__(self):
113 return self.__str__()
114
115
116 def intern_uni(u):
117 return intern(u.encode('ascii', 'replace'))
118
119
120 def collect_tid_names(perf_data):
121 tid_name_map = {}
122 for event in perf_data.events:
123 if event.HasField('comm_event'):
124 tid_name_map[event.comm_event.tid] = intern_uni(event.comm_event.comm)
125 return tid_name_map
126
127
128 def create_symbol_maps(profile):
129 symbol_maps = {}
130
131 for si in profile.Extensions[perfprofd_record_pb2.symbol_info]:
132 map = SymbolMap(si.min_vaddr)
133 symbol_maps[si.filename] = map
134 for sym in si.symbols:
135 map.add_symbol(sym.addr, sym.size, intern_uni(sym.name))
136 return symbol_maps
137
138
139 def update_mmap_states(event, state_map):
140 if event.HasField('mmap_event'):
141 mmap_event = event.mmap_event
142 # Skip kernel stuff.
143 if mmap_event.tid == 0:
144 return
145 # Create new map, if necessary.
146 if mmap_event.pid not in state_map:
147 state_map[mmap_event.pid] = MmapState()
148 state_map[mmap_event.pid].add_map(mmap_event.start, mmap_event.len, mmap_event.pgoff,
149 intern_uni(mmap_event.filename))
150 elif event.HasField('fork_event'):
151 fork_event = event.fork_event
152 # Skip threads
153 if fork_event.pid == fork_event.ppid:
154 return
155
156 if fork_event.ppid not in state_map:
157 logging.warn("fork from %d without map", fork_event.ppid)
158 return
159 state_map[fork_event.pid] = state_map[fork_event.ppid].copy()
160
161
162 skip_dso = set()
163 vaddr = {}
164
165
166 def find_vaddr(vaddr_map, filename):
167 if filename in vaddr_map:
168 return vaddr_map[filename]
169
170 path = "%s/%s" % (symbol.SYMBOLS_DIR, filename)
171 if not os.path.isfile(path):
172 logging.warn('Cannot find %s for min_vaddr', filename)
173 vaddr_map[filename] = 0
174 return 0
175
176 try:
177 # Use "-W" to have single-line format.
178 res = subprocess.check_output(['readelf', '-lW', path])
179 lines = res.split("\n")
180 reading_headers = False
181 min_vaddr = None
182
183 def min_fn(x, y):
184 return y if x is None else min(x, y)
185 # Using counting loop for access to next line.
186 for i in range(0, len(lines) - 1):
187 line = lines[i].strip()
188 if reading_headers:
189 if line == "":
190 # Block is done, won't find anything else.
191 break
192 if line.startswith("LOAD"):
193 # Look at the current line to distinguish 32-bit from 64-bit
194 line_split = line.split()
195 if len(line_split) >= 8:
196 if " R E " in line:
197 # Found something expected. So parse VirtAddr.
198 try:
199 min_vaddr = min_fn(min_vaddr, int(line_split[2], 0))
200 except ValueError:
201 pass
202 else:
203 logging.warn('Could not parse readelf line %s', line)
204 else:
205 if line.strip() == "Program Headers:":
206 reading_headers = True
207
208 if min_vaddr is None:
209 min_vaddr = 0
210 logging.debug("min_vaddr for %s is %d", filename, min_vaddr)
211 vaddr_map[filename] = min_vaddr
212 except subprocess.CalledProcessError:
213 logging.warn('Error finding min_vaddr for %s', filename)
214
215 vaddr_map[filename] = 0
216 return vaddr_map[filename]
217
218
219 unwind_symbols_cache = {}
220 unwind_symbols_warn_missing_cache = set()
221
222
223 def run_unwind_symbols(filename, offset_hex):
224 path = "%s/%s" % (symbol.SYMBOLS_DIR, filename)
225 if not os.path.isfile(path):
226 if path not in unwind_symbols_warn_missing_cache:
227 logging.warn('Cannot find %s for unwind_symbols', filename)
228 unwind_symbols_warn_missing_cache.add(path)
229 return None
230
231 if (path, offset_hex) in unwind_symbols_cache:
232 pair = unwind_symbols_cache[(path, offset_hex)]
233 if pair is None:
234 return None
235 return [(pair[0], pair[1], filename)]
236
237 try:
238 res = subprocess.check_output(['unwind_symbols', path, offset_hex])
239 lines = res.split("\n")
240 for line in lines:
241 if line.startswith('<0x'):
242 parts = line.split(' ', 1)
243 if len(parts) == 2:
244 # Get offset, too.
245 offset = 0
246 plus_index = parts[0].find('>+')
247 if plus_index > 0:
248 offset_str = parts[0][plus_index + 2:-1]
249 try:
250 offset = int(offset_str)
251 except ValueError:
252 logging.warn('error parsing offset from %s', parts[0])
253
254 # TODO C++ demangling necessary.
255 logging.debug('unwind_symbols: %s %s -> %s +%d', filename, offset_hex, parts[1],
256 offset)
257 sym = intern(parts[1])
258 unwind_symbols_cache[(path, offset_hex)] = (sym, offset)
259 return [(sym, offset, filename)]
260 except subprocess.CalledProcessError:
261 logging.warn('Failed running unwind_symbols for %s', filename)
262 unwind_symbols_cache[(path, offset_hex)] = None
263 return None
264
265
266 def decode_with_symbol_lib(name, addr_rel_hex):
267 info = SymbolInformation(name, addr_rel_hex)
268 # As-is, only info[0] (inner-most inlined function) is recognized.
269 (source_symbol, source_location, object_symbol_with_offset) = info[0]
270
271 def parse_symbol_lib_output(s):
272 i = s.rfind('+')
273 if i > 0:
274 try:
275 off = int(s[i + 1:])
276 return (s[0:i], off)
277 except ValueError:
278 pass
279 return (s, 0)
280
281 ret = []
282
283 if object_symbol_with_offset is not None:
284 pair = parse_symbol_lib_output(object_symbol_with_offset)
285 ret.append((intern(pair[0]), pair[1], name))
286
287 if source_symbol is not None:
288 iterinfo = iter(info)
289 next(iterinfo)
290 for (sym_inlined, loc_inlined, _) in iterinfo:
291 # TODO: Figure out what's going on here:
292 if sym_inlined is not None:
293 pair = parse_symbol_lib_output(sym_inlined)
294 ret.insert(0, (intern(pair[0]), pair[1], name))
295 if len(ret) > 0:
296 return ret
297 return None
298
299
300 def decode_addr(addr, mmap_state, device_symbols):
301 """Try to decode the given address against the current mmap table and device symbols.
302
303 First, look up the address in the mmap state. If none is found, use a simple address
304 heuristic to guess kernel frames on 64-bit devices.
305
306 Next, check on-device symbolization for a hit.
307
308 Last, try to symbolize against host information. First try the symbol module. However,
309 as it is based on addr2line, it will not work for pure-gnu_debugdata DSOs (e.g., ART
310 preopt artifacts). For that case, use libunwindstack's unwind_symbols.
311 """
312
313 map = mmap_state.find(addr)
314 if map is None:
315 # If it looks large enough, assume it's from
316 # the kernel.
317 if addr > 18000000000000000000:
318 return [("[kernel]", 0, "[kernel]")]
319 return [("%d (no mapped segment)" % addr, 0, None)]
320 name = map[3]
321 logging.debug('%d is %s (%d +%d)', addr, name, map[0], map[1])
322
323 # Once relocation packer is off, it would be:
324 # offset = addr - map.start + map.pgoff
325 # Right now it is
326 # offset = addr - map.start (+ min_vaddr)
327 # Note that on-device symbolization doesn't include min_vaddr but
328 # does include pgoff.
329 offset = addr - map[0]
330
331 if name in device_symbols:
332 offset = offset + map[2]
333 symbol = device_symbols[name].find(offset)
334 if symbol is None:
335 return [("%s (missing on-device symbol)" % (name), offset, name)]
336 else:
337 # TODO: Should we change the format?
338 return [(symbol, 0, name)]
339 offset = offset + find_vaddr(vaddr, name)
340 if (name, offset) in skip_dso:
341 # We already failed, skip symbol finding.
342 return [(name, offset, name)]
343 else:
344 addr_rel_hex = intern("%x" % offset)
345 ret = decode_with_symbol_lib(name, addr_rel_hex)
346 if ret is not None and len(ret) != 0:
347 # Addr2line may report oatexec+xyz. Let unwind_symbols take care of that.
348 if len(ret) != 1 or ret[0][0] != 'oatexec':
349 logging.debug('Got result from symbol module: %s', str(ret))
350 return ret
351 # Try unwind_symbols
352 ret = run_unwind_symbols(name, addr_rel_hex)
353 if ret is not None and len(ret) != 0:
354 return ret
355 logging.warn("Failed to find symbol for %s +%d (%d)", name, offset, addr)
356 # Remember the fail.
357 skip_dso.add((name, offset))
358 return [(name, offset, name)]
359
360
361 def print_sample(sample, tid_name_map):
362 if sample[0] in tid_name_map:
363 pid_name = "%s (%d)" % (tid_name_map[sample[0]], sample[0])
364 elif sample[0] == 0:
365 pid_name = "kernel (0)"
366 else:
367 pid_name = "unknown (%d)" % (sample[0])
368
369 if sample[1] in tid_name_map:
370 tid_name = "%s (%d)" % (tid_name_map[sample[1]], sample[1])
371 elif sample[1] == 0:
372 tid_name = "kernel (0)"
373 else:
374 tid_name = "unknown (%d)" % (sample[1])
375 print " %s - %s:" % (pid_name, tid_name)
376
377 for sym in sample[2]:
378 print " %s +%d (%s)" % (sym[0], sym[1], sym[2])
379
380
381 def print_samples(samples, tid_name_map):
382 for sample in samples:
383 print_sample(sample, tid_name_map)
384
385
386 def symbolize_events(perf_data, device_symbols, tid_name_map, printSamples=False,
387 removeKernelTop=False):
388 samples = []
389 mmap_states = {}
390 for event in perf_data.events:
391 update_mmap_states(event, mmap_states)
392 if event.HasField('sample_event'):
393 sample_ev = event.sample_event
394 # Handle sample.
395 new_sample = None
396 if sample_ev.pid in mmap_states:
397 mmap_state = mmap_states[sample_ev.pid]
398 ip_sym = decode_addr(sample_ev.ip, mmap_state, device_symbols)
399 stack = ip_sym
400 for cc_ip in sample_ev.callchain:
401 cc_sym = decode_addr(cc_ip, mmap_state, device_symbols)
402 stack.extend(cc_sym)
403 if removeKernelTop:
404 while len(stack) > 1 and stack[0][0] == "[kernel]":
405 stack.pop(0)
406 new_sample = (sample_ev.pid, sample_ev.tid, stack)
407 else:
408 # Handle kernel symbols specially.
409 if sample_ev.pid == 0:
410 samples.append((0, sample_ev.tid, [("[kernel]", 0, "[kernel]")]))
411 elif sample_ev.pid in tid_name_map:
412 samples.append((sample_ev.pid, sample_ev.tid, [(tid_name_map[sample_ev.pid], 0,
413 None)]))
414 else:
415 samples.append((sample_ev.pid, sample_ev.tid, [("[unknown]", 0, None)]))
416 if new_sample is not None:
417 samples.append(new_sample)
418 if printSamples:
419 print_sample(new_sample, tid_name_map)
420 return samples
421
422
423 def count_key_reduce_function(x, y, key_fn):
424 key = key_fn(y)
425 if key not in x:
426 x[key] = 0
427 x[key] += 1
428 return x
429
430
431 def print_histogram(samples, reduce_key_fn, label_key_fn, size):
432 # Create a sorted list of top samples.
433 sorted_count_list = sorted(
434 reduce(lambda x, y: count_key_reduce_function(x, y, reduce_key_fn), samples, {}).
435 iteritems(),
436 cmp=lambda x, y: cmp(x[1], y[1]),
437 reverse=True)
438 sorted_count_topX = list(itertools.islice(sorted_count_list, size))
439
440 # Print top-size samples.
441 print 'Histogram top-%d:' % (size)
442 for i in xrange(0, len(sorted_count_topX)):
443 print ' %d: %s (%s)' % (i + 1, label_key_fn(sorted_count_topX[i][0]),
444 sorted_count_topX[i][1])
445
446
447 def get_name(pid, tid_name_map):
448 if pid in tid_name_map:
449 return tid_name_map[pid]
450 if pid == 0:
451 return "[kernel]"
452 return "[unknown]"
453
454
455 def create_cmd(args, f):
456 ret = ['python', '-u', 'system/extras/perfprofd/scripts/perf_proto_stack.py']
457 if args.syms is not None:
458 ret.extend(['--syms', args.syms[0]])
459 if args.print_samples is not None:
460 ret.append('--print-samples')
461 if args.skip_kernel_syms is not None:
462 ret.append('--skip-kernel-syms')
463 if args.print_pid_histogram is not None:
464 ret.append('--print-pid-histogram')
465 if args.print_sym_histogram is not None:
466 ret.append('--print-sym-histogram')
467 if args.print_dso_histogram is not None:
468
469 ret.append('--print-dso-histogram')
470 ret.extend(['--json-out', '%s.json' % (f)])
471 ret.append(f)
472 return ret
473
474
475 def run_cmd(x):
476
477 args = x[0]
478 f = x[1]
479 cmd = create_cmd(args, f)
480 logging.warn('Running on %s', f)
481 success = False
482 logging.debug('%r', cmd)
483 err_out = open('%s.err' % (f), 'w')
484
485 def kill(process):
486 process.kill()
487 start = datetime.now()
488
489 p = subprocess.Popen(cmd, stderr=err_out)
490 kill_timer = Timer(3600, kill, [p])
491 try:
492 kill_timer.start()
493 p.communicate()
494 success = True
495
496 finally:
497 kill_timer.cancel()
498 err_out.close()
499 end = datetime.now()
500 logging.warn('Ended %s (%s)', f, str(end - start))
501 return '%s: %r' % (f, success)
502
503
504 def parallel_runner(args):
505 pool = ThreadPool(args.parallel)
506 map_args = map(lambda f: (args, f), args.file)
507
508 result = pool.map(run_cmd, map_args)
509 pool.close()
510 pool.join()
511 print result
512
513
514 def run(args):
515 if args.syms is not None:
516 symbol.SYMBOLS_DIR = args.syms[0]
517 print_symbols = args.print_samples is not None
518 skip_kernel_syms = args.skip_kernel_syms is not None
519
520 # TODO: accept argument for parsing.
521 file = open(args.file[0], 'rb')
522 data = file.read()
523
524 file.close()
525
526 profile = perf_data_pb2.PerfDataProto()
527 # PerfprofdRecord()
528 profile.ParseFromString(data)
529
530 perf_data = profile
531
532 print "Stats: ", perf_data.stats
533
534 tid_name_map = collect_tid_names(perf_data)
535 symbol_maps = create_symbol_maps(profile)
536
537 samples = symbolize_events(perf_data, symbol_maps, tid_name_map, printSamples=print_symbols,
538 removeKernelTop=skip_kernel_syms)
539
540 if args.print_pid_histogram is not None:
541 print_histogram(samples, lambda x: x[0], lambda x: get_name(x, tid_name_map), 25)
542 if args.print_sym_histogram is not None:
543 print_histogram(samples, lambda x: x[2][0][0], lambda x: x, 100)
544 if args.print_dso_histogram is not None:
545 print_histogram(samples, lambda x: x[2][0][2], lambda x: x, 25)
546
547 if args.json_out is not None:
548 json_file = open(args.json_out[0], 'w')
549 json_data = {'samples': samples, 'names': tid_name_map}
550 json.dump(json_data, json_file)
551 json_file.close()
552
553
554 if __name__ == "__main__":
555 parser = argparse.ArgumentParser(description='Process a perfprofd record.')
556
557 parser.add_argument('file', help='proto file to parse', metavar='file', nargs='+')
558 parser.add_argument('--syms', help='directory for symbols', nargs=1)
559 parser.add_argument('--json-out', help='output file for JSON', nargs=1)
560 parser.add_argument('--print-samples', help='print samples', action='store_const', const=True)
561 parser.add_argument('--skip-kernel-syms', help='skip kernel symbols at the top of stack',
562 action='store_const', const=True)
563 parser.add_argument('--print-pid-histogram', help='print a top-25 histogram of processes',
564 action='store_const', const=True)
565 parser.add_argument('--print-sym-histogram', help='print a top-100 histogram of symbols',
566 action='store_const', const=True)
567 parser.add_argument('--print-dso-histogram', help='print a top-25 histogram of maps',
568 action='store_const', const=True)
569 parser.add_argument('--parallel', help='run parallel jobs', type=int)
570
571 args = parser.parse_args()
572 if args is not None:
573 if args.parallel is not None:
574 parallel_runner(args)
575 else:
576 run(args)