ATLAS Offline Software
Auditor.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 # File: Hephaestus/Auditor.py
4 # Author Wim Lavrijsen (WLavrijsen@lbl.gov)
5 
6 __version__ = '1.1.0'
7 __author__ = 'Wim Lavrijsen (WLavrijsen@lbl.gov)'
8 
9 import AthenaPython.PyAthena as PyAthena
10 
11 GlobalSettings = { 'label' : 'hephaestus', 'event-based' : True }
12 
14  def __init__( self, name = "HephaestusAuditor", mode = 'leak-check', **kw ):
15  kw[ 'name' ] = name
16  super( HephaestusAuditor, self ).__init__( **kw )
17 
18  # event counter
19  self._evt = 0
20 
21  # settable parameter: subselection to audit on
22  try:
23  self.auditOn = kw[ 'auditOn' ]
24  except KeyError:
25  self.auditOn = [ 'execute' ]
26 
27  # settable parameter: determine event boundaries on the master sequence
28  try:
29  self.topSequenceName = kw[ 'topSequenceName' ]
30  except KeyError:
31  self.topSequenceName = 'AthMasterSeq'
32 
33  self.__setupMemoryTracker( mode, True )
34 
35  def __setupMemoryTracker( self, mode, startup = True ):
36  # get going immediately, so that this is the first memory hook
37  import MemoryTracker
38  self._memtrack = MemoryTracker
39 
40  if mode == 'leak-check':
41  self._start = self._memtrack.start
42  self._stop = self._memtrack.stop
43 
44  if startup or 'full' in self.auditOn:
45  self._memtrack.save()
46  self._memtrack.install()
47 
48  elif mode == 'delete-check':
49  import DeleteChecker
50  self._start = DeleteChecker.start
51  self._stop = DeleteChecker.stop
52 
53  else:
54  raise RuntimeError( 'unknown mode: %s (expected leak-check or delete-check)' % mode )
55 
56  # for full, just get going and never look back
57  if 'full' in self.auditOn:
58  self._start()
59 
60  def initialize( self ):
61  if 'full-athena' in self.auditOn:
62  self._start()
63 
64  if (self._memtrack.configure() & self._memtrack.PROFILE):
65  if not GlobalSettings[ 'event-based' ]:
66  self._memtrack._profname( GlobalSettings[ 'label' ] )
67 
68  return PyAthena.StatusCode.Success
69 
70  def finalize( self ):
71  if 'full-athena' in self.auditOn:
72  self._stop()
73 
74  return PyAthena.StatusCode.Success
75 
76  def before( self, evt_name, comp_name ):
77  if not self.auditOn or evt_name.lower() in self.auditOn:
78  if (self._memtrack.configure() & self._memtrack.PROFILE):
79  if GlobalSettings[ 'event-based' ] and evt_name.lower() == 'execute'\
80  and comp_name == self.topSequenceName:
81  self._evt += 1
82  self._memtrack._profname( GlobalSettings[ 'label' ] + '_execute.%d' % self._evt )
83  self._start()
84  return
85 
86  def after( self, evt_name, comp_name, sc ):
87  if not self.auditOn or evt_name.lower() in self.auditOn:
88  self._stop()
89  return
90 
91  # pickle support
92  def __getstate__( self ):
93  # determine mode used
94  mode = 'leak-check'
95  if self._memtrack.start != self._start:
96  mode = 'delete-check'
97 
98  # the base class will try to pickle all parts of the dict indiscriminately ...
99  mtmod = self._memtrack
100  del self._memtrack
101  dct = super( HephaestusAuditor, self ).__getstate__()
102 
103  # but need to be kept alive if the job does not end on the pickle (resetting
104  # in __setupMemoryTracker is fine, since it is cached in sys.modules)
105  self._memtrack = mtmod
106 
107  # store mode used
108  dct[ 'mode' ] = mode
109 
110  # store the module-level variables, as they may have been modified
111  dct[ 'GlobalSettings' ] = GlobalSettings
112 
113  # store the pid of the current process: if we get re-loaded into the same
114  # process, then no restart should be done (this isn't foolproof, but there
115  # is no current way of checking otherwise)
116  import os
117  dct[ 'pid' ] = os.getpid()
118 
119  return dct
120 
121  def __setstate__( self, dct ):
122  super( HephaestusAuditor, self ).__setstate__( dct )
123 
124  # copy the values instead of resetting the reference, in case it has
125  # been imported somewhere
126  global GlobalSettings
127  for k,v in dct[ 'GlobalSettings' ].items():
128  GlobalSettings[ k ] = v
129 
130  import os
131  self.__setupMemoryTracker( dct[ 'mode' ], dct[ 'pid' ] != os.getpid() )
132 
133  # the following is a guess; similar problem really as start(), really
134  if dct[ 'pid' ] == os.getpid():
135  self._evt = dct[ '_evt' ]
136  else:
137  self._evt = 0
python.Auditor.HephaestusAuditor._stop
_stop
Definition: Auditor.py:42
python.Auditor.HephaestusAuditor._evt
_evt
Definition: Auditor.py:19
PyAthena::Aud::initialize
virtual StatusCode initialize() override
Gaudi Aud Implementation.
Definition: PyAthenaAud.cxx:60
configure
bool configure(asg::AnaToolHandle< ITrigGlobalEfficiencyCorrectionTool > &tool, ToolHandleArray< IAsgElectronEfficiencyCorrectionTool > &electronEffToolsHandles, ToolHandleArray< IAsgElectronEfficiencyCorrectionTool > &electronSFToolsHandles, ToolHandleArray< CP::IMuonTriggerScaleFactors > &muonToolsHandles, ToolHandleArray< IAsgPhotonEfficiencyCorrectionTool > &photonEffToolsHandles, ToolHandleArray< IAsgPhotonEfficiencyCorrectionTool > &photonSFToolsHandles, const std::string &triggers, const std::map< std::string, std::string > &legsPerTool, unsigned long nToys, bool debug)
Definition: TrigGlobEffCorrValidation.cxx:514
python.Auditor.HephaestusAuditor.before
def before(self, evt_name, comp_name)
Definition: Auditor.py:76
python.Auditor.HephaestusAuditor.__getstate__
def __getstate__(self)
Definition: Auditor.py:92
python.Auditor.HephaestusAuditor.__setstate__
def __setstate__(self, dct)
Definition: Auditor.py:121
python.Auditor.HephaestusAuditor.after
def after(self, evt_name, comp_name, sc)
Definition: Auditor.py:86
PyAthena::Aud::finalize
virtual StatusCode finalize() override
Definition: PyAthenaAud.cxx:86
python.Auditor.HephaestusAuditor._memtrack
_memtrack
Definition: Auditor.py:38
checkTP.save
def save(self, fileName="./columbo.out")
Definition: checkTP.py:178
python.Auditor.HephaestusAuditor
Definition: Auditor.py:13
PyAthena::Aud
Definition: PyAthenaAud.h:32
python.Auditor.HephaestusAuditor._start
_start
Definition: Auditor.py:41
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
python.Auditor.HephaestusAuditor.__setupMemoryTracker
def __setupMemoryTracker(self, mode, startup=True)
Definition: Auditor.py:35
python.Auditor.HephaestusAuditor.topSequenceName
topSequenceName
Definition: Auditor.py:29
python.Auditor.HephaestusAuditor.__init__
def __init__(self, name="HephaestusAuditor", mode='leak-check', **kw)
Definition: Auditor.py:14
python.Auditor.HephaestusAuditor.auditOn
auditOn
Definition: Auditor.py:23