CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/FWCore/Services/bin/profilereader.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 """module to read profiling information generated by the SimpleProfiling Service
00003 """
00004 import os
00005 class FunctionInfo(object):
00006     """Holds the profiling information about one function
00007     """
00008     def __init__(self,attrList):
00009         self.address =attrList[0] 
00010         self.name =os.popen("c++filt  "+attrList[-1]).read().strip()
00011         self.leafCount = int(attrList[1])
00012         self.countOfFunctPlusChildWithRecursion = int(attrList[2])
00013         self.countOfFunctPlusChild = int(attrList[3])
00014         self.fractionInFunctionOnly = float(attrList[4])
00015         self.fractionInPath = float(attrList[5])
00016         self.__callerTemp = dict()
00017         self.__calleeTemp = dict()
00018     def addCaller(self,caller,weight):
00019         #print caller.name, weight
00020         self.__callerTemp.setdefault(caller.address,[0,caller])[0]+=weight
00021     def addCallee(self,callee,weight):
00022         self.__calleeTemp.setdefault(callee.address,[0,callee])[0]+=weight
00023     def normalize(self):
00024         self.callerList = list()
00025         self.calleeList = list()
00026         for caller in self.__callerTemp.keys():
00027             (count,_caller) = self.__callerTemp[caller]
00028             self.callerList.append((float(count)/self.countOfFunctPlusChildWithRecursion,_caller))
00029         for callee in self.__calleeTemp.keys():
00030             (count,_callee) = self.__calleeTemp[callee]
00031             self.calleeList.append((float(count)/self.countOfFunctPlusChildWithRecursion,_callee))
00032         self.callerList.sort()
00033         self.callerList.reverse()
00034         self.calleeList.sort()
00035         self.calleeList.reverse()
00036 
00037 class Path(object):
00038     def __init__(self,attrList):
00039         self.count = int(attrList[0])
00040         self.functionIds = [int(x) for x in attrList[1:] if x != '\n']
00041 
00042 class ProfileData(object):
00043     def __init__(self,filesToUseInfo,feedBackStream=None):
00044         #try to determine the filePrefix from the value given
00045         # since caller may have given us just one of the files
00046         # instead of the prefix to the files
00047         (dir,base) = os.path.split(filesToUseInfo)
00048         uniqueID = base.split('_')[1]
00049         filePrefix = os.path.join(dir,"profdata_"+uniqueID+"_")
00050         if feedBackStream:
00051             feedBackStream.write('reading file: '+filePrefix+'names')
00052         nameFile = file(filePrefix+'names','r')
00053         self.idToFunctionInfo = dict()
00054         self.functionNameToId = dict()
00055         feedbackIndex = 0
00056         for line in nameFile:
00057             if feedBackStream and (0 == feedbackIndex % 100):
00058                 feedBackStream.write('.')
00059             feedbackIndex +=1
00060             infoList = line.split('\t')
00061             self.idToFunctionInfo.setdefault( int(infoList[0]), FunctionInfo(infoList[1:]))
00062             self.functionNameToId.setdefault(self.idToFunctionInfo[int(infoList[0])].name, int(infoList[0]))
00063         if feedBackStream:
00064             feedBackStream.write('\nreading file: '+filePrefix+'paths')
00065         pathFile = file(filePrefix+'paths','r')
00066         self.paths = list()
00067         feedbackIndex = 0
00068         for line in pathFile:
00069             if feedBackStream and (0 == feedbackIndex % 100):
00070                 feedBackStream.write('.')
00071             feedbackIndex +=1
00072             line.strip()
00073             path = Path( line.split(' ')[1:])
00074             self.paths.append(path)
00075             caller = None
00076             for funcId in path.functionIds:
00077                 func = self.idToFunctionInfo[funcId]
00078                 if caller:
00079                     func.addCaller(caller,path.count)
00080                     caller.addCallee(func,path.count)
00081                 caller = func
00082         feedbackIndex = 0
00083         if feedBackStream:
00084             feedBackStream.write('\nprocessing data:')
00085         for x in self.idToFunctionInfo.items():
00086             if feedBackStream and (0 == feedbackIndex % 100):
00087                 feedBackStream.write('.')
00088             feedbackIndex +=1
00089             x[1].normalize()
00090 
00091 if __name__ == '__main__':
00092     import sys
00093     profile = ProfileData(sys.argv[1])
00094     print profile.idToFunctionInfo
00095