blob: 00b107946689f33838503bc54c1803983f06997b [file] [log] [blame]
Craig Tillerbd9f9242015-05-01 16:24:41 -07001#!/usr/bin/env python
2# Copyright 2015, Google Inc.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11# * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15# * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31"""
32Read GRPC basic profiles, analyze the data.
33
34Usage:
35 bins/basicprof/qps_smoke_test > log
36 cat log | tools/profile_analyzer/profile_analyzer.py
37"""
38
39
40import collections
David Garcia Quintas5b2ea292015-05-06 09:57:49 -070041import itertools
Craig Tillerbd9f9242015-05-01 16:24:41 -070042import re
43import sys
44
45# Create a regex to parse output of the C core basic profiler,
46# as defined in src/core/profiling/basic_timers.c.
47_RE_LINE = re.compile(r'GRPC_LAT_PROF ' +
David Garcia Quintas5b2ea292015-05-06 09:57:49 -070048 r'([0-9]+\.[0-9]+) 0x([0-9a-f]+) ([{}.!]) ([0-9]+) ' +
Craig Tillerbd9f9242015-05-01 16:24:41 -070049 r'([^ ]+) ([^ ]+) ([0-9]+)')
50
51Entry = collections.namedtuple(
52 'Entry',
53 ['time', 'thread', 'type', 'tag', 'id', 'file', 'line'])
54
David Garcia Quintas5b2ea292015-05-06 09:57:49 -070055
56class ImportantMark(object):
57 def __init__(self, entry, stack):
58 self._entry = entry
59 self._pre_stack = stack
60 self._post_stack = list()
61 self._n = len(stack) # we'll also compute times to that many closing }s
62
63 @property
64 def entry(self):
65 return self._entry
66
67 def append_post_entry(self, entry):
68 if self._n > 0:
69 self._post_stack.append(entry)
70 self._n -= 1
71
72 def get_deltas(self):
73 pre_and_post_stacks = itertools.chain(self._pre_stack, self._post_stack)
74 return collections.OrderedDict((stack_entry,
75 (self._entry.time - stack_entry.time))
76 for stack_entry in pre_and_post_stacks)
77
Craig Tillerbd9f9242015-05-01 16:24:41 -070078def entries():
79 for line in sys.stdin:
80 m = _RE_LINE.match(line)
81 if not m: continue
82 yield Entry(time=float(m.group(1)),
83 thread=m.group(2),
84 type=m.group(3),
85 tag=int(m.group(4)),
86 id=m.group(5),
87 file=m.group(6),
88 line=m.group(7))
89
90threads = collections.defaultdict(lambda: collections.defaultdict(list))
91times = collections.defaultdict(list)
92
David Garcia Quintas5b2ea292015-05-06 09:57:49 -070093# Indexed by the mark's tag. Items in the value list correspond to the mark in
94# different stack situations.
95important_marks = collections.defaultdict(list)
96
Craig Tillerbd9f9242015-05-01 16:24:41 -070097for entry in entries():
98 thread = threads[entry.thread]
99 if entry.type == '{':
100 thread[entry.tag].append(entry)
David Garcia Quintas5b2ea292015-05-06 09:57:49 -0700101 if entry.type == '!':
102 # Save a snapshot of the current stack inside a new ImportantMark instance.
103 # Get all entries with type '{' from "thread".
104 stack = [e for entries_for_tag in thread.values()
105 for e in entries_for_tag if e.type == '{']
106 important_marks[entry.tag].append(ImportantMark(entry, stack))
Craig Tillerbd9f9242015-05-01 16:24:41 -0700107 elif entry.type == '}':
108 last = thread[entry.tag].pop()
109 times[entry.tag].append(entry.time - last.time)
David Garcia Quintas5b2ea292015-05-06 09:57:49 -0700110 # Update accounting for important marks.
111 for imarks_for_tag in important_marks.itervalues():
112 for imark in imarks_for_tag:
113 imark.append_post_entry(entry)
Craig Tillerbd9f9242015-05-01 16:24:41 -0700114
115def percentile(vals, pct):
116 return sorted(vals)[int(len(vals) * pct / 100.0)]
117
118print 'tag 50%/90%/95%/99% us'
119for tag in sorted(times.keys()):
120 vals = times[tag]
David Garcia Quintas5b2ea292015-05-06 09:57:49 -0700121 print '%d %.2f/%.2f/%.2f/%.2f' % (tag,
Craig Tillerbd9f9242015-05-01 16:24:41 -0700122 percentile(vals, 50),
123 percentile(vals, 90),
124 percentile(vals, 95),
125 percentile(vals, 99))
David Garcia Quintas5b2ea292015-05-06 09:57:49 -0700126
127print
128print 'Important marks:'
129print '================'
130for tag, imark_for_tag in important_marks.iteritems():
131 for imark in imarks_for_tag:
132 deltas = imark.get_deltas()
133 print '{tag} @ {file}:{line}'.format(**imark.entry._asdict())
134 for entry, time_delta_us in deltas.iteritems():
135 format_dict = entry._asdict()
136 format_dict['time_delta_us'] = time_delta_us
137 print '{tag} {type} ({file}:{line}): {time_delta_us:12.3f} us'.format(
138 **format_dict)
139 print