CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
cmsSimplememchecker_parser.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 
4 def manipulate_log(outdir,logfile_name,startevt):
5 
6  import time
7  import sys
8  import ROOT
9 
10  os.system('pwd')
11 
12  # the fundamental structure: the key is the evt number the value is a list containing
13  # VSIZE deltaVSIZE RSS deltaRSS
14  data=[]
15  values_set=('vsize','delta_vsize','rss','delta_rss')
16 
17  # open file and read it and fill the structure!
18  logfile=open(logfile_name,'r')
19  logfile_lines=logfile.readlines()
20  logfile.close()
21 
22  # we get the info we need!
23  i=0
24  while i < len(logfile_lines):
25  line=logfile_lines[i]
26  if '%MSG-w MemoryCheck:' in line:
27  line=line[:-1] #no \n!
28  line_content_list=line.split(' ')
29  event_number=int(line_content_list[-1])
30  if event_number<startevt:
31  i+=1
32  continue
33  i+=1 # we inspect the following line
34  line=logfile_lines[i]
35  line=line[:-1] #no \n!
36  line_content_list=line.split(' ')
37  vsize=float(line_content_list[4])
38  delta_vsize=float(line_content_list[5])
39  rss=float(line_content_list[7])
40  delta_rss=float(line_content_list[8])
41 
42  data.append((event_number,{'vsize':vsize,
43  'delta_vsize':delta_vsize,
44  'rss':rss,
45  'delta_rss':delta_rss}))
46  i+=1
47 
48  # skim the second entry when the event number is the same BUG!!!!!!!
49  # i take elements in couples!
50  new_data=[]
51  if len(data)>2:
52  if data[0][0]==data[1][0]:
53  print 'Two modules seem to have some output.\nCollapsing ...'
54  i=0
55  while True:
56  dataline1=data[i]
57  i+=1
58  dataline2=data[i]
59  new_eventnumber=dataline1[0]
60  new_vsize=dataline2[1]['vsize']
61  new_delta_vsize=dataline1[1]['delta_vsize']+dataline2[1]['delta_vsize']
62  new_rss=dataline2[1]['rss']
63  new_delta_rss=dataline1[1]['delta_rss']+dataline2[1]['delta_rss']
64 
65  new_data.append((new_eventnumber,{'vsize':new_vsize,
66  'delta_vsize':new_delta_vsize,
67  'rss':new_rss,
68  'delta_rss':new_delta_rss}))
69  i+=1
70  if i==len(data): break
71 
72  data=new_data
73  print 'Collapsing: Done!'
74 
75  npoints=len(data)
76 
77  print '%s values read and stored ...' %npoints
78 
79 
80  # The Graphs
81  __argv=sys.argv # trick for a strange behaviour of the TApp..
82  sys.argv=sys.argv[:1]
83  ROOT.gROOT.SetStyle("Plain") # style paranoia
84  sys.argv=__argv
85 
86  #Cannot use this option when the logfile includes
87  #a large number of events... PyRoot seg-faults.
88  #Set ROOT in batch mode to avoid canvases popping up!
89  ROOT.gROOT.SetBatch(1)
90 
91  # Save in file
92  rootfilename='%s/graphs.root' %outdir
93  myfile=ROOT.TFile(rootfilename,'RECREATE')
94 
95  # dictionary of graphs!
96  graph_dict={}
97  for value in values_set:
98  #graph_dict[value]
99  graph=ROOT.TGraph(npoints)
100  graph.SetMarkerStyle(8)
101  graph.SetMarkerSize(.7)
102  graph.SetMarkerColor(1)
103  graph.SetLineWidth(3)
104  graph.SetLineColor(2)
105  graph.SetTitle(value)
106  graph.SetName('%s_graph' %value)
107 
108 
109  #fill the graphs
110  point_counter=0
111  for event_number,vals_dict in data:
112  graph.SetPoint(point_counter,
113  event_number,
114  vals_dict[value])
115  point_counter+=1
116 
117  graph.GetXaxis().SetTitle("Event")
118  last_event=data[-1][0]
119  graph.GetXaxis().SetRangeUser(0,last_event+1)
120  graph.GetYaxis().SetTitleOffset(1.3)
121  graph.GetYaxis().SetTitle("MB")
122 
123 
124 
125  #print the graphs as files :)
126  mycanvas=ROOT.TCanvas('%s_canvas' %value)
127  mycanvas.cd()
128  graph.Draw("ALP")
129 
130  mycanvas.Print("%s/%s_graph.gif"%(outdir,value),"gif")
131 
132  # write it on file
133  graph.Write()
134  mycanvas.Write()
135 
136  myfile.Close()
137 
138  os.system('pwd')
139 
140  # The html page!------------------------------------------------------------------------------
141 
142  titlestring='<b>Report executed with release %s on %s.</b>\n<br>\n<hr>\n'\
143  %(os.environ['CMSSW_VERSION'],time.asctime())
144  #Introducing this if to catch the cmsRelvalreport.py use case of "reuse" of TimingReport
145  #profile when doing the SimpleMemReport... otherwise the filename for the html
146  #would be misleadingly TimingReport...
147  if len(logfile_name)>16 and 'TimingReport.log' in logfile_name[-16:]:
148  file_name=logfile_name[:-16]+"_SimpleMemReport"
149  else:
150  file_name=logfile_name[:-4]+"_SimpleMemReport"
151  html_file_name='%s/%s.html' %(outdir,file_name)
152  html_file=open(html_file_name,'w')
153  html_file.write('<html>\n<body>\n'+\
154  titlestring)
155  html_file.write('<table>\n'+\
156  '<tr><td><img src=vsize_graph.gif></img></td>'+\
157  '<td><img src=rss_graph.gif></img></td></tr>'+\
158  '<tr><td><img src=delta_vsize_graph.gif></img></td>'+\
159  '<td><img src=delta_rss_graph.gif></img></td></tr>' +\
160  '</table>\n')
161 
162  html_file.write('\n</body>\n</html>')
163  html_file.close()
164 
165 
166 #################################################################################################
167 
168 if __name__ == '__main__':
169 
170  import optparse
171  import os
172 
173  # Here we define an option parser to handle commandline options..
174  usage='simplememchecker_parser.py <options>'
175  parser = optparse.OptionParser(usage)
176  parser.add_option('-i', '--in_ profile',
177  help='The profile to manipulate' ,
178  default='',
179  dest='profile')
180 
181  parser.add_option('-o', '--outdir',
182  help='The directory of the output' ,
183  default='',
184  dest='outdir')
185 
186  parser.add_option('-n',
187  help='The event number from which we start. Default is 1.' ,
188  default='1',
189  dest='startevt')
190 
191  (options,args) = parser.parse_args()
192 
193  # Now some fault control..If an error is found we raise an exception
194  if options.profile=='' or\
195  options.outdir=='':
196  raise('Please select a profile and an output dir!')
197 
198  if not os.path.exists(options.profile) or\
199  not os.path.exists(options.outdir):
200  raise ('Outdir or input profile not present!')
201 
202  try:
203  startevt=int(options.startevt)
204  except ValueError:
205  print 'Problems in convertng starting event value!'
206 
207 
208  #launch the function!
209  manipulate_log(options.outdir,options.profile,startevt)
210 
211