CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
prof2calltree.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 '''
4 The output from the profiler has names that end in 'names' and 'paths'.
5 This script expects you run c++filt on the 'names' file and save
6 it will the same prefix, but with 'nice_names' on the end.
7 This will be improved on the release of simple profiler.
8 
9 The output file from this script has the same prefix as the input
10 files, but ends in 'calltree'. This file can be fed directly into
11 kcachegrind.
12 '''
13 
14 import sys
15 import csv
16 
17 if len(sys.argv) < 3:
18  print "usage: ",sys.argv[0]," prefix_of_run_data tick_cutoff n|o"
19  sys.exit(1)
20 
21 prefix = sys.argv[1]
22 cutoff = int(sys.argv[2])
23 newold_flag = 'o'
24 
25 if len(sys.argv) > 3:
26  newold_flag = sys.argv[3]
27 
28 outfile = open(prefix+"calltree","w")
29 pathfile = open(prefix+"paths","r")
30 
31 names = {}
32 edges = {}
33 
34 def tostring(l):
35  x=""
36  for i in l: x += "%d "%i
37  return x
38 
39 class Node:
40  def __init__(me,fid,name):
41  me.fid = int(fid)
42  me.name = name
43  me.ticks_as_parent = 0
44  me.ticks_as_child = 0
45  me.ticks_as_parent_recur = 0
46  me.calls = 0
47  me.children = {} # set() - want to use set, but not until 2.4
48 
49  def parentTicks(me,count):
50  me.ticks_as_parent += int(count)
51 
52  def childTicks(me,count):
53  c = int(count)
54  me.ticks_as_parent += c
55  me.ticks_as_child += c
56 
57  def recurTicks(me,count):
58  me.ticks_as_parent_recur += int(count)
59 
60  def addChild(me,child_id):
61  me.children[child_id]=0 # .add(child_id) -cannot use set functions yet
62 
63  def __str__(me):
64  return "(%d,%s,%d,%d,%s)"%(me.fid,me.name,me.ticks_as_parent,me.ticks_as_child,tostring(me.children.keys()))
65 
66 class EdgeCount:
67  def __init__(me):
68  me.count = 0
69 
70  def addTicks(me,count):
71  me.count += count
72 
73  def __str__(me):
74  return "%d"%me.count
75 
76 if newold_flag == 'o':
77  namefile = open(prefix+"nice_names","r")
78  for entry in namefile:
79  front=entry.split(' ',2)
80  back=front[2].rsplit(' ',16)
81  name=back[0]
82  name=name.replace(' ','-')
83  #print name
84  #names[front[0]]=(front[0],name,back[1],back[2],back[3],back[4],back[5])
85  names[int(front[0])] = Node(front[0],name)
86 else:
87  namefile = csv.reader(open(prefix+"nice_names","rb"),delimiter='\t')
88  for row in namefile:
89  names[int(row[0])] = Node(row[0],row[-1])
90 
91 
92 print >>outfile,"events: ticks"
93 
94 for entry in names.values():
95  print >>outfile,"fn=(%s) %s"%(entry.fid,entry.name)
96  # print entry[1]
97 
98 print >>outfile
99 
100 # all the 0 values below are line numbers (none in my case)
101 #------------------------------
102 # fn=func <- parent function
103 # 0 100 <- cost, accumulated into "incl" and "self" for func
104 # cfn=func2 <- child function
105 # calls=2 0 <- times called from func
106 # (accumulated into "called" for func2)
107 # 0 350 <- cost involved in call
108 # (accumulated into "incl" for func2)
109 
110 # "incl" for a function appears in the graphed box, it is the sum over
111 # all incoming wires
112 # A wire contains cost value from cfn entry
113 
114 for pe in pathfile:
115  all = pe.split()
116  l = len(all)
117  #entry = names[all[-1:]]
118  #print all
119 
120  ticks = int(all[1])
121  if ticks < cutoff: continue
122  last = int(all[l-1])
123  names[last].childTicks(ticks)
124 
125  for i in range(2,l-1):
126  edge = (int(all[i]), int(all[i+1]))
127  node = names[edge[0]]
128  node.recurTicks(ticks)
129  if edge[0]!=edge[1]:
130  node.parentTicks(ticks)
131  node.addChild(edge[1])
132  edges.setdefault(edge,EdgeCount()).addTicks(ticks)
133 
134 #for x in names.values():
135 # print x
136 
137 for node in names.values():
138  cost=0
139  #if len(node.children) == 0:
140  cost = node.ticks_as_child
141  print >>outfile,"fn=(%d)"%node.fid
142  print >>outfile,"0 %d"%cost
143  #print >>outfile,"\n\n"
144 
145  for child in node.children.keys():
146  count = edges[(node.fid,child)].count
147  print >>outfile,"cfn=(%d)"%child
148  print >>outfile,"calls=%d 0"%count
149  print >>outfile,"0 %d"%count
150 
151  print >>outfile,"\n\n"
152