CMS 3D CMS Logo

callgraph.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 from __future__ import print_function
3 import re
4 import yaml
5 
6 topfunc = re.compile(r"::(accumulate|acquire|startingNewLoop|duringLoop|endOfLoop|beginOfJob|endOfJob|produce|analyze|filter|beginLuminosityBlock|beginRun|beginStream|streamBeginRun|streamBeginLuminosityBlock|streamEndRun|streamEndLuminosityBlock|globalBeginRun|globalEndRun|globalBeginLuminosityBlock|globalEndLuminosityBlock|endRun|endLuminosityBlock)\(")
7 
8 baseclass = re.compile(r"edm::(one::|stream::|global::)?(ED(Producer|Filter|Analyzer|(IterateNTimes|NavigateEvents)?Looper)(Base)?|impl::(ExternalWork|Accumulator))")
9 farg = re.compile(r"\(.*?\)")
10 tmpl = re.compile(r'<.*?>')
11 toplevelfuncs = set()
12 epfuncre = re.compile(r"edm::eventsetup::EventSetupRecord::get<.*>\(.*\)")
13 skipfunc = re.compile(r"TGraph::IsA\(.*\)")
14 epfuncs=set()
15 
16 import networkx as nx
17 G=nx.DiGraph()
18 
19 #g = open('module_to_package.txt')
20 #module2package=dict()
21 #for line in g:
22 # fields = line.strip().split(';')
23 # if len(fields) <2:
24 # continue
25 # module2package.setdefault(fields[1], []).append(fields[0])
26 #
27 #i = open('module_to_package.yaml', 'w')
28 #yaml.dump(module2package, i)
29 #i.close()
30 
31 h = open('module_to_package.yaml', 'r')
32 module2package=yaml.load(h, Loader=yaml.FullLoader)
33 
34 with open('function-calls-db.txt') as f:
35  for line in f :
36  fields = line.split("'")
37  if len(fields) < 3:
38  continue
39  if fields[2] == ' calls function ' :
40  if not skipfunc.search(line) :
41  G.add_edge(fields[1],fields[3],kind=fields[2])
42  if epfuncre.search(fields[3]) :
43  epfuncs.add(fields[3])
44  if fields[2] == ' overrides function ' :
45  if baseclass.search(fields[3]) :
46  if topfunc.search(fields[3]) :
47  toplevelfuncs.add(fields[1])
48  G.add_edge(fields[1],fields[3],kind=' overrides function ')
49  else :
50  if not skipfunc.search(line) :
51  G.add_edge(fields[3],fields[1],kind=' calls override function ')
52  if epfuncre.search(fields[1]) : epfuncs.add(fields[1])
53 
54 
55 
56 callstacks=set()
57 for tfunc in toplevelfuncs:
58  for epfunc in epfuncs:
59  if G.has_node(tfunc) and G.has_node(epfunc) and nx.has_path(G,tfunc,epfunc) :
60  path = nx.shortest_path(G,tfunc,epfunc)
61  cs=str("")
62  previous=str("")
63  for p in path :
64  if epfuncre.search(p): break
65  stripped=re.sub(farg,"()",p)
66  if previous != stripped:
67  cs+=' '+stripped+";"
68  previous = stripped
69  callstacks.add(cs)
70  break
71 
72 
73 report=dict()
74 for key in sorted(module2package.keys()):
75  for value in sorted(module2package[key]):
76  vre=re.compile(' %s::.*();' % value)
77  for cs in sorted(callstacks):
78  if vre.search(cs):
79  report.setdefault(key, {}).setdefault(value, []).append(cs)
80 r=open('eventsetuprecord-get.yaml', 'w')
81 yaml.dump(report,r)
str
#define str(s)
Definition: TestProcessor.cc:52
mps_setup.append
append
Definition: mps_setup.py:85