dracut-functions: documentation and restructuring
[platform/upstream/dracut.git] / profile.py
1 #
2 # parse the output of "dracut --profile" and produce profiling information
3 #
4 # Copyright 2011 Harald Hoyer <harald@redhat.com>
5 # Copyright 2011 Red Hat, Inc.  All rights reserved.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 import sys
22 import operator
23 import re
24 loglines = sys.stdin
25
26 logpats  = r'[+]+[ \t]+([^ \t]+)[ \t]+([^ \t:]+):[ ]+.*'
27
28 logpat   = re.compile(logpats)
29
30 groups   = (logpat.match(line) for line in loglines)
31 tuples   = (g.groups() for g in groups if g)
32
33 def gen_times(t):
34     oldx=None
35     for x in t:
36         fx=float(x[0])
37         if oldx:
38             #print fx - float(oldx[0]), x[0], x[1], oldx[0], oldx[1]
39             yield (fx - float(oldx[0]), oldx[1])
40
41         oldx = x
42
43 colnames = ('time','line')
44
45 log      = (dict(zip(colnames,t)) for t in gen_times(tuples))
46
47 if __name__ == '__main__':
48     e={}
49     for x in log:
50         if not x['line'] in e:
51             e[x['line']] = x['time']
52         else:
53             e[x['line']] += x['time']
54
55     sorted_x = sorted(e.iteritems(), key=operator.itemgetter(1), reverse=True)
56     for x in sorted_x:
57         print x[0], x[1]
58