ATLAS Offline Software
sim_check_batch.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
4 """
5  check db has all needed host information
6 
7 """
8 
9 import os
10 import shutil
11 import datetime as dt
12 from sqlite3 import IntegrityError, OperationalError
13 
14 def getHost(hostname=None):
15  if hostname is None:
16  hostname=os.getenv('HOSTNAME')
17  #hostname='lxbsu2921.cern.ch'
18  #hostname1='lxbsu2921a.cern.ch'
19  hout=os.popen('lshosts '+hostname)
20  res=hout.read()
21  hout.close()
22  res2=[l.split() for l in res.strip().split('\n')]
23  res3=[ l[:8]+[' '.join(l[8:])] for l in res2 ]
24  return res3
25 
27  def __init__(self,fname):
28  self.fname=fname
29  self.cnx=None
30  self.cur=None
31 
32  def openConnection(self):
33  import sqlite3
34  self.cnx = sqlite3.connect(self.fname)
35  self.cur = self.cnx.cursor()
36 
37  def _execute(self,cmd,args=()):
38  print 'SQL:',cmd,args
39  self.cur.execute(cmd,args)
40 
41  def _dumpTable(self,t):
42  #t = (symbol,)
43  #cur.execute('select * from stocks where symbol=?', t)
44  self._execute('select * from %s'%t)
45  header= [ s[0] for s in self.cur.description ]
46  format= ' '.join([ '%25s' for s in self.cur.description])
47  print format%tuple(header)
48  for res in self.cur.fetchall():
49  print format%res
50 
51 
52  def insert(self,tname='t_run',hargs=[],args=[]):
53  "create prepared statement and execute"
54  keys=','.join(hargs)
55  vals=','.join(('?',)*len(args))
56  cmd="insert into %s (%s) values (%s)"%(tname,keys,vals)
57  print cmd,args
58  self.cur.execute(cmd,args)
59  self.rid=self.cur.lastrowid
60  return self.rid
61 
62  def dummyHostInfo(self):
63  hostInfo={'HOST_NAME': 'lxplus443',
64  'RESOURCES': '(intel plus quadcore wan)',
65  'cpuf': '1.9',
66  'maxmem': '48161M',
67  'maxswp': '51207M',
68  'model': 'vi_10_24',
69  'ncpus': '8',
70  'server': 'Yes',
71  'type': 'SLC5_64'}
72  return hostInfo
73 
74  def addRun(self):
75 
76  #AtlasArea=/afs/cern.ch/atlas/software/builds/nightlies/devval/AtlasProduction/rel_2
77  atlasArea=os.getenv('AtlasArea')
78  dummy,branch,proj,rel=atlasArea.rsplit('/',3)
79 
80  #LS_SUBCWD=/afs/cern.ch/atlas/project/RTT/prod/Results/rtt/rel_2/devval/build/x86_64-slc5-gcc43-opt/offline/SimCoreTests/AtlasG4_muons
81  subcwd=os.getenv('LS_SUBCWD')
82  dummy1,cmt,dummy2,package,job=subcwd.rsplit('/',4)
83 
84  rdate,rtime=dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S').split()
85 
86  hostInfo=getHost()
87  hostInfo=dict(zip(*hostInfo))
88 
89  machine=hostInfo['HOST_NAME']
90  KSI2K_fac=float(hostInfo['cpuf'])
91  hargs=['branch', 'rel', 'cmt', 'rdate', 'rtime', 'machine', 'KSI2K_fac', 'package', 'job']
92  args=[locals()[x] for x in hargs]
93  id=self.insert('t_run',hargs,args)
94  self.cnx.commit()
95  return id
96 
97  def addFile(self,fl):
98  "add file entry to db and return id"
99  # check if already registered
100  if fl.dbid is not None:
101  return fl.dbid
102 
103  # check if database
104  cmd='select id from t_files where fl_name=?'
105  self._execute(cmd,(fl.fl,))
106  res=self.cur.fetchone()
107  if res is None:
108  print 'WARNING',fl.fl,'not found'
109  # adding it
110  id=self.insert('t_files',['fl_name'],[fl.fl])
111  else:
112  # receive existing entry
113  id=res[0]
114  fl.dbid=id
115  return id
116 
117  def addFunction(self,fn):
118  if self.parser.filelist.has_key(fn.fl_id):
119  fl=self.parser.filelist[fn.fl_id]
120  fid=self.addFile(fl)
121  else:
122  fid=0
123  cmd='select id from t_functions where fn_name=? and fid=? and issue=?'
124  self._execute(cmd,(fn.fn,fid,fn.issue))
125  res=self.cur.fetchone()
126  if res is None:
127  # adding it
128  hargs=['fn_name','fid','issue','lib']
129  args=[fn.fn,fid,fn.issue,fn.lib]
130  id = self.insert('t_functions',hargs,args)
131  else:
132  id = res[0]
133  fn.dbid=id
134 
135  def addFunctions(self,parser):
136  self.parser=parser
137  for fn in parser.functions.values():
138  self.addFunction(fn)
139  self.cnx.commit()
140 
141  def addCountAndLinks(self,rid,parser=None):
142  if parser!=None:
143  self.parser=parser
144 
145  success=True
146  for fn in parser.functions.values():
147  if fn.dbid is None:
148  print 'ERROR fn=',fn.fn,'not in db yet'
149  self.addFunction(fn)
150 
151  hargs=['rid','fid','selfcounts','totalcounts']
152  args=[rid,fn.dbid,fn.selfcost,fn.totalcost]
153  # add counts to db
154  try:
155  self.insert('t_counts',hargs,args)
156  except IntegrityError as detail:
157  print 'IntegrityError',detail
158  success=False
159  print 'fn=',fn
160  continue
161 
162  # add sub call links to db
163  for i,counts in fn.calls.iteritems():
164  if i!=0:
165  cfn=self.parser.functions[i]
166  if cfn.dbid is None:
167  print 'ERROR cfn=',cfn.fn,'not in db yet'
168  self.addFunction(cfn)
169  hargs=['rid','fid','cfunid','counts']
170  args=[rid,fn.dbid,cfn.dbid,counts]
171  try:
172  self.insert('t_links',hargs,args)
173  except IntegrityError as detail:
174  print 'IntegrityError',detail
175  success=False
176  print 'fn=',fn
177  print 'cfn=',cfn
178 
179  if success:
180  self.cnx.commit()
181  else:
182  print 'some ERROR occured rolling back'
183  self.cnx.rollback()
184 
185 
186 if __name__=="__main__":
187  # create input files (callgrind.out & athena.profile.txt if not present already
188  #db=DataBase('rtt.sqlite')
189  doTest=False
190  if doTest:
191  db=DataBase('./rttProfile.sqlite')
192  else:
193  db=DataBase('/afs/cern.ch/atlas/groups/Simulation/rtt/rttProfile.sqlite')
194 
195  db.openConnection()
196 
197  cmd="select distinct machine from t_run"
198  db._execute(cmd)
199  res = db.cur.fetchall()
200  ll=list(zip(*res)[0])
201 
202 
203  try:
204  db._execute("select * from t_hosts");
205  db.cur.fetchall()
206  except OperationalError as detail:
207  print detail
208  print "table t_hosts not found: try create one"
209  db.cnx.rollback()
210  cmd="create table t_hosts (id integer primary key autoincrement, hostname varchar(32), os_type varchar(32), model varchar(32), cpuf float, ncpus integer , maxmem varchar(32), maxswp varchar(32), server varchar(6), resources text)"
211  db._execute(cmd);
212  db.cnx.commit()
213 
214  for hostname in ll:
215  print 'check',hostname
216  #info = getHost(hostname)
217  #print info
218 
219  cmd='select id from t_hosts where hostname=?'
220  db._execute(cmd,(hostname,))
221  res=db.cur.fetchone()
222  if res is None:
223  # add to database
224  print hostname,"not found, adding it now"
225 
226  hargs=['hostname', 'os_type', 'model', 'cpuf', 'ncpus', 'maxmem', 'maxswp', 'server', 'resources']
227  bargs=['HOST_NAME', 'type', 'model', 'cpuf', 'ncpus', 'maxmem', 'maxswp', 'server', 'RESOURCES']
228  hostInfo=getHost(hostname)
229  hostInfo=dict(zip(*hostInfo))
230  hostInfo['maxmem']=int(hostInfo['maxmem'].strip('M'))
231  hostInfo['maxswp']=int(hostInfo['maxswp'].strip('M'))
232  args=[hostInfo[h] for h in bargs]
233 
234  db.insert('t_hosts',hargs,args)
235  db.cnx.commit()
236  #
237  #int(x.strip('M'))
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
sim_check_batch.DataBase.addFunctions
def addFunctions(self, parser)
Definition: sim_check_batch.py:135
sim_check_batch.DataBase.__init__
def __init__(self, fname)
Definition: sim_check_batch.py:27
sim_check_batch.DataBase.openConnection
def openConnection(self)
Definition: sim_check_batch.py:32
sim_check_batch.DataBase.addFunction
def addFunction(self, fn)
Definition: sim_check_batch.py:117
sim_check_batch.DataBase
Definition: sim_check_batch.py:26
LArG4FSStartPointFilterLegacy.execute
execute
Definition: LArG4FSStartPointFilterLegacy.py:20
sim_check_batch.DataBase.cur
cur
Definition: sim_check_batch.py:30
sim_check_batch.DataBase.addRun
def addRun(self)
Definition: sim_check_batch.py:74
sim_check_batch.DataBase._dumpTable
def _dumpTable(self, t)
Definition: sim_check_batch.py:41
sim_check_batch.DataBase.parser
parser
Definition: sim_check_batch.py:136
sim_check_batch.DataBase.cnx
cnx
Definition: sim_check_batch.py:29
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
sim_check_batch.getHost
def getHost(hostname=None)
Definition: sim_check_batch.py:14
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
sim_check_batch.DataBase.fname
fname
Definition: sim_check_batch.py:28
sim_check_batch.DataBase.addFile
def addFile(self, fl)
Definition: sim_check_batch.py:97
sim_check_batch.DataBase._execute
def _execute(self, cmd, args=())
Definition: sim_check_batch.py:37
sim_check_batch.DataBase.addCountAndLinks
def addCountAndLinks(self, rid, parser=None)
Definition: sim_check_batch.py:141
query_example.cursor
cursor
Definition: query_example.py:21
pickleTool.object
object
Definition: pickleTool.py:30
calibdata.commit
bool commit
Definition: calibdata.py:832
sim_check_batch.DataBase.dummyHostInfo
def dummyHostInfo(self)
Definition: sim_check_batch.py:62
readCCLHist.float
float
Definition: readCCLHist.py:83
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
sim_check_batch.DataBase.insert
def insert(self, tname='t_run', hargs=[], args=[])
Definition: sim_check_batch.py:52
sim_check_batch.DataBase.rid
rid
Definition: sim_check_batch.py:59