Gaudi Framework, version v24r2

Home   Generated: Wed Dec 4 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TimingAuditor.cpp
Go to the documentation of this file.
1 // $Id: TimingAuditor.cpp,v 1.5 2008/04/05 08:04:22 marcocle Exp $
2 // ============================================================================
3 // CVS tag $Name: $, version $Revision: 1.5 $
4 // ============================================================================
5 #include "GaudiKernel/Auditor.h"
6 #include "GaudiKernel/IToolSvc.h"
9 #include "GaudiKernel/IToolSvc.h"
10 #include "GaudiKernel/VectorMap.h"
11 #include "GaudiKernel/HashMap.h"
12 #include "GaudiKernel/AudFactory.h"
13 #include "GaudiKernel/MsgStream.h"
14 // ============================================================================
15 // GaudiAlg
16 // ============================================================================
18 // ============================================================================
19 #ifdef __ICC
20 // disable icc warning #654: overloaded virtual function "B::Y" is only partially overridden in class "C"
21 // TODO: there is only a partial overload of IAuditor::before and IAuditor::after
22 #pragma warning(disable:654)
23 #endif
24 
33 class TimingAuditor: public extends1<Auditor, IIncidentListener> {
34 public:
35 
36  virtual void before(StandardEventType evt, INamedInterface* alg);
37  virtual void after(StandardEventType evt, INamedInterface* alg, const StatusCode &sc);
38 
39  using Auditor::before; // avoid hiding base-class methods
40  virtual void before(CustomEventTypeRef evt, const std::string& name);
41  using Auditor::after; // avoid hiding base-class methods
42  virtual void after(CustomEventTypeRef evt, const std::string& name, const StatusCode &sc);
43 
44 private:
47  void i_beforeFinalize( INamedInterface* alg );
48  void i_beforeExecute( INamedInterface* alg );
49  void i_afterExecute( INamedInterface* alg);
50 
51 public:
53  virtual void handle ( const Incident& ) ;
54 
55 public:
56  virtual StatusCode initialize () ;
57  virtual StatusCode finalize () ;
58 
59 public:
62  ( const std::string& name ,
63  ISvcLocator* pSvc )
64  : base_class ( name , pSvc )
65  //
66  , m_toolSvc ( 0 )
67  , m_incSvc ( 0 )
68  //
69  , m_timer ( 0 )
70  //
71  , m_appMgr ( 0 )
72  //
73  , m_map ( )
74  , m_indent ( 0 )
75  , m_inEvent ( false )
76  , m_goodForDOD ( false )
77  , m_mapUser ( )
78  , m_histoSaved ( false )
79  {
80  declareProperty ( "OptimizedForDOD" , m_goodForDOD ) ;
81  } ;
83  virtual ~TimingAuditor() {}
84 private:
85  // the default constructor is disabled
86  TimingAuditor () ;
87  // copy constructor is disabled
88  TimingAuditor ( const TimingAuditor& ) ;
89  // assignement operator is disabled
91 private:
92  // tool service
94  // incident service
96  // the timer tool
98  // ApplicationManager
100  //
103  // indentation level
104  int m_indent ;
105  // "in event"
106  bool m_inEvent ;
107  // "optimized for Data-On-Demand Service"
108  bool m_goodForDOD ;
109  //
112 
113  // Whether the timing has been saved already
115 
116 
117 } ;
118 // ============================================================================
120 // ============================================================================
122 // ============================================================================
123 StatusCode TimingAuditor::initialize ()
124 {
126  if ( sc.isFailure() ) { return sc ; } // RETURN
127 
128  MsgStream log ( msgSvc() , name() ) ;
129 
130  // get tool service
131  if ( 0 == m_toolSvc )
132  {
133  sc = Auditor::service ( "ToolSvc" , m_toolSvc ) ;
134  if ( sc.isFailure() )
135  {
136  log << "Could not retrieve 'ToolSvc' " << sc << endmsg ;
137  return sc ; // RETURN
138  }
139  if ( 0 == m_timer )
140  {
141  sc = m_toolSvc->retrieveTool
142  ( "SequencerTimerTool/TIMER" , m_timer , this , true ) ;
143  if ( sc.isFailure() )
144  {
145  log << MSG::ERROR
146  << "Could not retrieve ISequencerTimerTool" << endmsg ;
147  return sc ;
148  }
149  }
150  }
151  // get incident service
152  if ( 0 == m_incSvc )
153  {
154  sc = Auditor::service ( "IncidentSvc" , m_incSvc ) ;
155  if ( sc.isFailure() )
156  {
157  log << MSG::ERROR
158  << "Could not retrieve 'IncidentSvc'" << sc << endmsg ;
159  return sc ;
160  }
161  m_incSvc -> addListener ( this , IncidentType::BeginEvent ) ;
162  m_incSvc -> addListener ( this , IncidentType::EndEvent ) ;
163  }
164  // get the application manager
165  if ( 0 == m_appMgr )
166  {
167  sc = Auditor::service ( "ApplicationMgr" , m_appMgr ) ;
168  if ( sc.isFailure() )
169  {
170  log << MSG::ERROR
171  << "Could not retrieve 'ApplicationMgr'" << sc << endmsg ;
172  return sc ;
173  }
174  if ( m_map.end() == m_map.find( m_appMgr ) )
175  {
176  int timer = m_timer->addTimer( "EVENT LOOP" ) ;
177  m_map.insert ( m_appMgr , timer ) ;
178  }
179  }
180  //
181  return StatusCode::SUCCESS ;
182 }
183 // ============================================================================
185 {
186  if ( 0 != m_incSvc )
187  {
188  m_incSvc -> removeListener ( this , IncidentType::BeginEvent ) ;
189  m_incSvc -> removeListener ( this , IncidentType::EndEvent ) ;
190  m_incSvc -> release () ;
191  m_incSvc = 0 ;
192  }
193  if ( 0 != m_toolSvc )
194  {
195  // the 2 following line are commented out: it is
196  // is a temporary hack which prevent a crash due to a problem in
197  // the reference counting
198  // if ( 0 != m_timer )
199  // { m_toolSvc -> releaseTool ( m_timer ) . ignore() ; m_timer = 0 ; }
200  m_toolSvc -> release () ;
201  m_toolSvc = 0 ;
202  }
203  if ( 0 != m_appMgr ) { m_appMgr -> release () ; m_appMgr = 0 ; }
204  // clear the map
205  m_map.clear() ;
206  // finalize the base class
207  return Auditor::finalize () ;
208 }
209 // ============================================================================
210 void TimingAuditor::before(StandardEventType evt, INamedInterface *alg)
211 {
212  switch (evt) {
213  case IAuditor::Initialize : i_beforeInitialize( alg ); break;
214  case IAuditor::Execute : i_beforeExecute( alg ); break;
215  case IAuditor::Finalize : i_beforeFinalize( alg ); break;
216  default: break;
217  }
218 }
219 // ============================================================================
220 void TimingAuditor::after(StandardEventType evt, INamedInterface *alg, const StatusCode &)
221 {
222  switch (evt) {
223  case IAuditor::Initialize : i_afterInitialize( alg ); break;
224  case IAuditor::Execute : i_afterExecute( alg ); break;
225  default: break;
226  }
227 }
228 // ============================================================================
230 {
231  if (!m_histoSaved)
232  {
234  m_histoSaved = true;
235  }
236 }
237 
238 // ============================================================================
240 {
241  if ( m_goodForDOD ) { return ; }
242  //
243  if ( 0 == alg ) { return ; }
244  Map::iterator found = m_map.find( alg ) ;
245  if ( m_map.end() != found ) { return ; }
246  ++m_indent ;
247  std::string nick = alg->name() ;
248  if ( 0 < m_indent ) { nick = std::string ( m_indent , ' ') + nick ; }
249  if ( m_inEvent )
250  {
251  nick[0] = '*' ;
252  MsgStream log( msgSvc() , name() ) ;
253  log << MSG::DEBUG
254  << "Insert non-structural component '"
255  << alg->name() << "' of type '"
256  << System::typeinfoName(typeid(*alg)) << "' at level "
257  << m_indent << endmsg ;
258  }
259  int timer = m_timer->addTimer( nick ) ;
260  m_map.insert ( alg , timer ) ;
261  m_timer->start( timer ) ;
262 }
263 // ============================================================================
265 {
266  if ( m_goodForDOD ) { return ; }
267  if ( 0 == alg ) { return ; }
268  --m_indent ;
269 }
270 // ============================================================================
272 {
273  if ( 0 == alg ) { return ; }
274  ++m_indent ;
275  Map::iterator found = m_map.find( alg ) ;
276  if ( m_map.end() == found )
277  {
278  MsgStream log( msgSvc() , name() ) ;
279  log << MSG::DEBUG
280  << "Insert non-structural component '"
281  << alg->name() << "' of type '"
282  << System::typeinfoName(typeid(*alg)) << "' at level "
283  << m_indent << endmsg ;
284  std::string nick = alg->name() ;
285  if ( 0 < m_indent ) { nick = std::string ( m_indent , ' ') + nick ; }
286  if ( !m_goodForDOD ) { nick[0]='*' ;}
287  int timer = m_timer->addTimer( nick ) ;
288  m_map.insert ( alg , timer ) ;
289  m_timer->start( timer ) ;
290  return ;
291  }
292  m_timer->start( found->second ) ;
293 }
294 // ============================================================================
296 {
297  if ( 0 == alg ) { return ; }
298  Map::iterator found = m_map.find( alg ) ;
299  if ( m_map.end() == found ) { return ; }
300  m_timer->stop( found->second ) ;
301  --m_indent ;
302 }
303 // ============================================================================
304 void TimingAuditor::before(CustomEventTypeRef evt, const std::string& name)
305 {
306  // Ignore obvious mistakes
307  if ( name.empty() && evt.empty() ) { return; }
308 
309  // look for the user timer in the map
310  int timer = 0;
311  std::string nick = name + ":" + evt;
312  MapUser::iterator found = m_mapUser.find( nick );
313 
314  if ( m_mapUser.end() == found ) {
315  // add a new timer if not yet available
316  timer = m_timer->addTimer( nick ) ;
317  m_mapUser[nick] = timer;
318  }
319  else {
320  timer = found->second;
321  }
322 
323  m_timer->start( timer );
324 }
325 // ============================================================================
326 void TimingAuditor::after(CustomEventTypeRef evt, const std::string& name, const StatusCode &)
327 {
328  // Ignore obvious mistakes
329  if ( name.empty() && evt.empty() ) { return; }
330 
331  // look for the user timer in the map
332  std::string nick = name + ":" + evt;
333  MapUser::iterator found = m_mapUser.find( nick );
334 
335  // We cannot do much if the timer is not available
336  if ( m_mapUser.end() == found ) {
337  MsgStream log(msgSvc(), this->name());
338  log << MSG::WARNING << "Trying to stop the measure of the timing for '"
339  << nick << "' but it was never started. Check the code"
340  << endmsg;
341  return;
342  }
343  m_timer->stop( found->second );
344 }
345 // ============================================================================
347 {
348  if ( IncidentType::BeginEvent == i.type () )
349  {
350  m_timer -> start ( m_map[ m_appMgr ] ) ;
351  ++m_indent ;
352  m_inEvent = true ;
353  }
354  else if ( IncidentType::EndEvent == i.type () )
355  {
356  m_timer -> stop ( m_map[ m_appMgr ] ) ;
357  --m_indent ;
358  m_inEvent = false ;
359  }
360 }
361 // ============================================================================
362 // The END
363 // ============================================================================

Generated at Wed Dec 4 2013 14:33:07 for Gaudi Framework, version v24r2 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004