The Gaudi Framework  master (37c0b60a)
ToolSvc Class Reference
Inheritance diagram for ToolSvc:
Collaboration diagram for ToolSvc:

Classes

class  ToolList
 Common Tools. More...
 

Public Member Functions

 ~ToolSvc () override
 Destructor. More...
 
StatusCode finalize () override
 Finalize the service. More...
 
StatusCode start () override
 
StatusCode stop () override
 
StatusCode retrieve (std::string_view type, const InterfaceID &iid, IAlgTool *&tool, const IInterface *parent, bool createIf) override
 Retrieve tool, create it by default as common tool if it does not already exist. More...
 
StatusCode retrieve (std::string_view tooltype, std::string_view toolname, const InterfaceID &iid, IAlgTool *&tool, const IInterface *parent, bool createIf) override
 Retrieve tool, create it by default as common tool if it does not already exist. More...
 
std::vector< std::stringgetInstances (std::string_view toolType) override
 Get names of all tool instances of a given type. More...
 
std::vector< std::stringgetInstances () const override
 Get names of all tool instances. More...
 
std::vector< IAlgTool * > getTools () const override
 Get pointers to all tool instances. More...
 
StatusCode releaseTool (IAlgTool *tool) override
 Release tool. More...
 
StatusCode create (const std::string &type, const IInterface *parent, IAlgTool *&tool)
 Create Tool standard way with automatically assigned name. More...
 
StatusCode create (const std::string &type, const std::string &name, const IInterface *parent, IAlgTool *&tool)
 Create Tool standard way with specified name. More...
 
bool existsTool (std::string_view fullname) const
 Check if the tool instance exists. More...
 
std::string nameTool (std::string_view nameByUser, const IInterface *parent)
 Get Tool full name by combining nameByUser and "parent" part. More...
 
void registerObserver (IToolSvc::Observer *obs) override
 
- Public Member Functions inherited from extends< Service, IToolSvc >
void * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast. More...
 
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface. More...
 
std::vector< std::stringgetInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames. More...
 
- Public Member Functions inherited from Service
const std::stringname () const override
 Retrieve name of the service
More...
 
StatusCode configure () override
 
StatusCode initialize () override
 
StatusCode start () override
 
StatusCode stop () override
 
StatusCode finalize () override
 
StatusCode terminate () override
 
Gaudi::StateMachine::State FSMState () const override
 
Gaudi::StateMachine::State targetFSMState () const override
 
StatusCode reinitialize () override
 
StatusCode restart () override
 
StatusCode sysInitialize () override
 Initialize Service
More...
 
StatusCode sysStart () override
 Initialize Service
More...
 
StatusCode sysStop () override
 Initialize Service
More...
 
StatusCode sysFinalize () override
 Finalize Service
More...
 
StatusCode sysReinitialize () override
 Re-initialize the Service. More...
 
StatusCode sysRestart () override
 Re-initialize the Service. More...
 
 Service (std::string name, ISvcLocator *svcloc)
 Standard Constructor
More...
 
SmartIF< ISvcLocator > & serviceLocator () const override
 Retrieve pointer to service locator
More...
 
template<class T >
StatusCode service (const std::string &name, const T *&psvc, bool createIf=true) const
 Access a service by name, creating it if it doesn't already exist. More...
 
template<class T >
StatusCode service (const std::string &name, T *&psvc, bool createIf=true) const
 
template<typename IFace = IService>
SmartIF< IFace > service (const std::string &name, bool createIf=true) const
 
template<class T >
StatusCode service (const std::string &svcType, const std::string &svcName, T *&psvc) const
 Access a service by name and type, creating it if it doesn't already exist. More...
 
template<class T >
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, ToolHandle< T > &hndl, const std::string &doc="none")
 
template<class T >
StatusCode declareTool (ToolHandle< T > &handle, bool createIf=true)
 
template<class T >
StatusCode declareTool (ToolHandle< T > &handle, const std::string &toolTypeAndName, bool createIf=true)
 Declare used tool. More...
 
template<class T >
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, ToolHandleArray< T > &hndlArr, const std::string &doc="none")
 
template<class T >
void addToolsArray (ToolHandleArray< T > &hndlArr)
 
const std::vector< IAlgTool * > & tools () const
 
SmartIF< IAuditorSvc > & auditorSvc () const
 The standard auditor service.May not be invoked before sysInitialize() has been invoked. More...
 
- Public Member Functions inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
 PropertyHolder ()=default
 
Gaudi::Details::PropertyBasedeclareProperty (Gaudi::Details::PropertyBase &prop)
 Declare a property. More...
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, TYPE &value, const std::string &doc="none")
 Helper to wrap a regular data member and use it as a regular property. More...
 
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, Gaudi::Property< TYPE, VERIFIER, HANDLERS > &prop, const std::string &doc="none")
 Declare a PropertyBase instance setting name and documentation. More...
 
Gaudi::Details::PropertyBasedeclareRemoteProperty (const std::string &name, IProperty *rsvc, const std::string &rname="")
 Declare a remote property. More...
 
StatusCode setProperty (const std::string &name, const Gaudi::Details::PropertyBase &p) override
 set the property from another property with a different name More...
 
StatusCode setProperty (const std::string &s) override
 set the property from the formatted string More...
 
StatusCode setProperty (const Gaudi::Details::PropertyBase &p)
 Set the property from a property. More...
 
virtual StatusCode setProperty (const std::string &name, const Gaudi::Details::PropertyBase &p)=0
 Set the property from a property with a different name. More...
 
virtual StatusCode setProperty (const std::string &s)=0
 Set the property by string. More...
 
StatusCode setProperty (const std::string &name, const char *v)
 Special case for string literals. More...
 
StatusCode setProperty (const std::string &name, const std::string &v)
 Special case for std::string. More...
 
StatusCode setProperty (const std::string &name, const TYPE &value)
 set the property form the value More...
 
StatusCode setPropertyRepr (const std::string &n, const std::string &r) override
 set the property from name and value string representation More...
 
StatusCode getProperty (Gaudi::Details::PropertyBase *p) const override
 get the property More...
 
const Gaudi::Details::PropertyBasegetProperty (std::string_view name) const override
 get the property by name More...
 
StatusCode getProperty (std::string_view n, std::string &v) const override
 convert the property to the string More...
 
const std::vector< Gaudi::Details::PropertyBase * > & getProperties () const override
 get all properties More...
 
bool hasProperty (std::string_view name) const override
 Return true if we have a property with the given name. More...
 
Gaudi::Details::PropertyBaseproperty (std::string_view name) const
 \fixme property and bindPropertiesTo should be protected More...
 
void bindPropertiesTo (Gaudi::Interfaces::IOptionsSvc &optsSvc)
 
 PropertyHolder (const PropertyHolder &)=delete
 
PropertyHolderoperator= (const PropertyHolder &)=delete
 
- Public Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
MSG::Level msgLevel () const
 get the cached level (originally extracted from the embedded MsgStream) More...
 
bool msgLevel (MSG::Level lvl) const
 get the output level from the embedded MsgStream More...
 

Private Member Functions

StatusCode finalizeTool (IAlgTool *itool) const
 Finalize the given tool, with exception handling. More...
 

Private Attributes

Gaudi::Property< bool > m_checkNamedToolsConfigured
 
Gaudi::Property< bool > m_showToolDataDeps { this, "ShowDataDeps", false, "show the data dependencies of AlgTools" }
 
std::recursive_mutex m_mut
 
ToolList m_instancesTools
 
SmartIF< IHistorySvcm_pHistorySvc
 Pointer to HistorySvc. More...
 
std::vector< IToolSvc::Observer * > m_observers
 

Additional Inherited Members

- Public Types inherited from extends< Service, IToolSvc >
using base_class = extends
 Typedef to this class. More...
 
using extend_interfaces_base = extend_interfaces< Interfaces... >
 Typedef to the base of this class. More...
 
- Public Types inherited from Service
using Factory = Gaudi::PluginService::Factory< IService *(const std::string &, ISvcLocator *)>
 
- Public Types inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
using PropertyHolderImpl = PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
 Typedef used to refer to this class from derived classes, as in. More...
 
- Public Types inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
using base_class = CommonMessaging
 
- Public Types inherited from extend_interfaces< Interfaces... >
using ext_iids = typename Gaudi::interface_list_cat< typename Interfaces::ext_iids... >::type
 take union of the ext_iids of all Interfaces... More...
 
- Protected Member Functions inherited from Service
std::vector< IAlgTool * > & tools ()
 
 ~Service () override
 Standard Destructor
More...
 
int outputLevel () const
 get the Service's output level More...
 
- Protected Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
MSG::Level setUpMessaging () const
 Set up local caches. More...
 
MSG::Level resetMessaging ()
 Reinitialize internal states. More...
 
void updateMsgStreamOutputLevel (int level)
 Update the output level of the cached MsgStream. More...
 
- Protected Attributes inherited from Service
Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE
 Service state
More...
 
Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE
 Service state
More...
 
Gaudi::Property< int > m_outputLevel { this, "OutputLevel", MSG::NIL, "output level" }
 flag indicating whether ToolHandle tools have been added to m_tools More...
 
Gaudi::Property< bool > m_auditInit { this, "AuditServices", false, "[[deprecated]] unused" }
 
Gaudi::Property< bool > m_auditorInitialize { this, "AuditInitialize", false, "trigger auditor on initialize()" }
 
Gaudi::Property< bool > m_auditorStart { this, "AuditStart", false, "trigger auditor on start()" }
 
Gaudi::Property< bool > m_auditorStop { this, "AuditStop", false, "trigger auditor on stop()" }
 
Gaudi::Property< bool > m_auditorFinalize { this, "AuditFinalize", false, "trigger auditor on finalize()" }
 
Gaudi::Property< bool > m_auditorReinitialize { this, "AuditReinitialize", false, "trigger auditor on reinitialize()" }
 
Gaudi::Property< bool > m_auditorRestart { this, "AuditRestart", false, "trigger auditor on restart()" }
 
Gaudi::Property< bool > m_autoRetrieveTools
 
Gaudi::Property< bool > m_checkToolDeps
 
SmartIF< IAuditorSvcm_pAuditorSvc
 Auditor Service
More...
 

Detailed Description

This service manages tools. Tools can be common, in which case a single instance can be shared by different algorithms, or private in which case it is necessary to specify the parent when requesting it. The parent of a tool can be an algortihm or a Service The environment of a tool is set by using that of the parent. A common tool is considered to belong to the ToolSvc itself.

Author
G. Corti, P. Mato

Definition at line 48 of file ToolSvc.cpp.

Constructor & Destructor Documentation

◆ ~ToolSvc()

ToolSvc::~ToolSvc ( )
override

Destructor.

Definition at line 207 of file ToolSvc.cpp.

207  {
208  // tell the remaining observers that we're gone, and forget about unregistering..
210  [&]( IToolSvc::Observer* obs ) { obs->setUnregister( {} ); } );
211 }

Member Function Documentation

◆ create() [1/2]

StatusCode ToolSvc::create ( const std::string type,
const IInterface parent,
IAlgTool *&  tool 
)

Create Tool standard way with automatically assigned name.

Definition at line 510 of file ToolSvc.cpp.

512 {
513  const std::string& toolname = tooltype;
514  return create( tooltype, toolname, parent, tool );
515 }

◆ create() [2/2]

StatusCode ToolSvc::create ( const std::string tooltype,
const std::string toolname,
const IInterface parent,
IAlgTool *&  tool 
)

Create Tool standard way with specified name.

Now able to handle clones.

The test of tool existence is performed according to three criteria: name, type and parent. If a tool is private, i.e. the parent is not the tool Svc, and it exist but the parent is not the specified one, a clone is handed over. No clones of public tools are allowed since they would be undistinguishable.

invoke create callbacks...

Definition at line 569 of file ToolSvc.cpp.

572 {
573 
574  // protect against empty type
575  if ( tooltype.empty() ) {
576  error() << "create(): No Tool Type given" << endmsg;
577  return StatusCode::FAILURE;
578  }
579 
580  // If parent has not been specified, assume it is the ToolSvc
581  if ( !parent ) parent = this;
582 
583  tool = nullptr;
584  // Automatically deletes the tool if not explicitly kept (i.e. on success).
585  // The tool is removed from the list of known tools too.
586  auto lock = std::scoped_lock{ m_mut };
587  auto toolguard = make_toolCreateGuard( m_instancesTools );
588 
589  // Check if the tool already exist : this could happen with clones
590  std::string fullname = nameTool( toolname, parent );
591  if ( existsTool( fullname ) ) {
592  // Now check if the parent is the same. This allows for clones
593  for ( IAlgTool* iAlgTool : m_instancesTools ) {
594  if ( iAlgTool->name() == toolname && iAlgTool->parent() == parent ) {
595  // The tool exist with this name, type and parent: this is bad!
596  // This excludes the possibility of cloning public tools intrinsecally
597  error() << "Tool " << fullname << " already exists with the same parent" << endmsg;
598  if ( parent == this )
599  error() << "... In addition, the parent is the ToolSvc: public tools cannot be cloned!" << endmsg;
600 
601  return StatusCode::FAILURE;
602  }
603  }
604  ON_DEBUG debug() << "Creating clone of " << fullname << endmsg;
605  }
606  // instantiate the tool using the factory
607  try {
608  toolguard.create( tooltype, fullname, parent );
609  if ( !toolguard.get() ) {
610  error() << "Cannot create tool " << tooltype << " (No factory found)" << endmsg;
611  return StatusCode::FAILURE;
612  }
613  } catch ( const GaudiException& Exception ) {
614  // (1) perform the printout of message
615  fatal() << "Exception with tag=" << Exception.tag() << " is caught whilst instantiating tool '" << tooltype << "'"
616  << endmsg;
617  // (2) print the exception itself
618  // (NB! - GaudiException is a linked list of all "previous exceptions")
619  fatal() << Exception << endmsg;
620  return StatusCode::FAILURE;
621  } catch ( const std::exception& Exception ) {
622  // (1) perform the printout of message
623  fatal() << "Standard std::exception is caught whilst instantiating tool '" << tooltype << "'" << endmsg;
624  // (2) print the exception itself
625  // (NB! - GaudiException is a linked list of all "previous exceptions")
626  fatal() << Exception.what() << endmsg;
627  return StatusCode::FAILURE;
628  } catch ( ... ) {
629  // (1) perform the printout
630  fatal() << "UNKNOWN Exception is caught whilst instantiating tool '" << tooltype << "'" << endmsg;
631  return StatusCode::FAILURE;
632  }
633  ON_VERBOSE verbose() << "Created tool " << tooltype << "/" << fullname << endmsg;
634 
635  // Since only AlgTool has the setProperties() method it is necessary to cast
636  // to downcast IAlgTool to AlgTool in order to set the properties via the JobOptions
637  // service
638  AlgTool* mytool = dynamic_cast<AlgTool*>( toolguard.get() );
639  if ( mytool ) mytool->bindPropertiesTo( serviceLocator()->getOptsSvc() );
640 
641  // Initialize the Tool
643  try {
644  sc = toolguard->sysInitialize();
645  }
646  // Catch any exceptions
647  catch ( const GaudiException& Exception ) {
648  error() << "GaudiException with tag=" << Exception.tag() << " caught whilst initializing tool '" << fullname << "'"
649  << endmsg << Exception << endmsg;
650  return StatusCode::FAILURE;
651  } catch ( const std::exception& Exception ) {
652  error() << "Standard std::exception caught whilst initializing tool '" << fullname << "'" << endmsg
653  << Exception.what() << endmsg;
654  return StatusCode::FAILURE;
655  } catch ( ... ) {
656  error() << "UNKNOWN Exception caught whilst initializing tool '" << fullname << "'" << endmsg;
657  return StatusCode::FAILURE;
658  }
659 
660  // Status of tool initialization
661  if ( sc.isFailure() ) {
662  error() << "Error initializing tool '" << fullname << "'" << endmsg;
663  return sc;
664  }
665 
667  // Check to ensure that non-default named tools are configured.
668  if ( toolname != tooltype ) {
669  bool propsSet = false;
670  Gaudi::Interfaces::IOptionsSvc& joSvc = this->serviceLocator()->getOptsSvc();
671  // Check that at least one of the properties has been set:
672  for ( const auto prop : mytool->getProperties() ) {
673  bool isSet = joSvc.isSet( mytool->name() + "." + prop->name() );
674  if ( isSet ) propsSet = true;
675  }
676  if ( !propsSet ) {
677  warning() << tooltype << "/" << fullname
678  << " : Explicitly named tools should be configured! (assigned name=" << toolname << ", default is "
679  << tooltype << ")" << endmsg;
680  }
681  }
682  }
683 
684  // Start the tool if we are running.
686  sc = toolguard->sysStart();
687 
688  if ( sc.isFailure() ) {
689  error() << "Error starting tool '" << fullname << "'" << endmsg;
690  return sc;
691  }
692  }
693 
694  // The tool has been successfully created and initialized,
695  // so we inform the guard that it can release it
696  tool = toolguard.release();
697 
701  std::for_each( m_observers.begin(), m_observers.end(), [&]( IToolSvc::Observer* obs ) { obs->onCreate( tool ); } );
702  // TODO: replace by generic callback
703  // Register the tool with the HistorySvc
704  m_pHistorySvc = service( "HistorySvc", false );
705  if ( m_pHistorySvc ) { m_pHistorySvc->registerAlgTool( *tool ).ignore(); }
706  return StatusCode::SUCCESS;
707 }

◆ existsTool()

bool ToolSvc::existsTool ( std::string_view  fullname) const

Check if the tool instance exists.

Definition at line 729 of file ToolSvc.cpp.

731 {
732  auto lock = std::scoped_lock{ m_mut };
733  return m_instancesTools.contains( fullname );
734 }

◆ finalize()

StatusCode ToolSvc::finalize ( )
override

Finalize the service.

Algorithm: 2 passes. First pass:

  • do not explicitly release any tools
  • finalize tools, in the order of increasing number of refCounts Second pass:
  • explicitly release all tools, one release() on all tools per loop. -> tools are deleted in the order of increasing number of refCounts.

Inner loop: full loop over all left-over tools

  • finalize tools with the minimum number of refCounts in the list.
  • Remove finalized tools from list of tools, and add them to the list of finalized tools, to be deleted at the end. This way, any non-finalized tools who still reference already finalized tools in their finalize() will still find a live tool. Outer loop: keep on going until nothing changes in the list of tools. Checking for:
  • number of left-over tools
  • total number of refcounts
  • minimum number of refcounts

Definition at line 214 of file ToolSvc.cpp.

216 {
217  // Finalize and delete all left-over tools. Normally all tools created with
218  // ToolSvc are left over, since ToolSvc holds a refCount (via AlgTool ctor).
219  // Several cases need to be covered:
220  // 1) Simple dependencies: no circular dependencies between tools,
221  // and tools not using other tools
222  // 2) Tools-using-tools (but no circular dependencies)
223  // a) release() is called in the tool::finalize() (e.g. via base class)
224  // b) release() is called in the tool destructor (e.g. via ToolHandle)
225  // 3) Circular dependencies between tools
226  // a) release() is called in the tool::finalize() (e.g. via base class)
227  // b) release() is called in the tool destructor (e.g. via ToolHandle)
228  // 4) In addition to each of the above cases, refCounting of a particular tool
229  // might not have been done correctly in the code. Typically release()
230  // is not called, so we are left with a too high refCount.
231  // What to do with those, and how to avoid a crash while handling them...
232 
241  info() << "Removing all tools created by ToolSvc" << endmsg;
242  auto tools = std::move( m_instancesTools ).grab();
243 
244  // Print out list of tools
245  ON_DEBUG {
246  auto& log = debug();
247  log << " Tool List : ";
248  for ( const auto& iTool : tools ) { log << iTool->name() << ":" << iTool->refCount() << " "; }
249  log << endmsg;
250  }
251 
252  //
253  // first pass: Finalize all tools (but don't delete them)
254  //
267  boost::circular_buffer<IAlgTool*> finalizedTools( tools.size() ); // list of tools that have been finalized
268  bool fail( false );
269  size_t toolCount = tools.size();
270  unsigned long startRefCount = 0;
271  unsigned long endRefCount = totalRefCount( tools );
272  unsigned long startMinRefCount = 0;
273  unsigned long endMinRefCount = minimumRefCount( tools );
274  while ( toolCount > 0 && endRefCount > 0 && ( endRefCount != startRefCount || endMinRefCount != startMinRefCount ) ) {
275  ON_DEBUG if ( endMinRefCount != startMinRefCount ) {
276  debug() << toolCount << " tools left to finalize. Summed refCounts: " << endRefCount << endmsg;
277  debug() << "Will finalize tools with refCount <= " << endMinRefCount << endmsg;
278  }
279  startMinRefCount = endMinRefCount;
280  startRefCount = endRefCount;
281  unsigned long maxLoop = toolCount + 1;
282  while ( --maxLoop > 0 && !tools.empty() ) {
283  IAlgTool* pTool = tools.back();
284  // removing tool from list makes ToolSvc::releaseTool( IAlgTool* ) a noop
285  tools.pop_back();
286  unsigned long count = pTool->refCount();
287  // cache tool name
288  const std::string& toolName = pTool->name();
289  if ( count <= startMinRefCount ) {
290  ON_DEBUG debug() << " Performing finalization of " << toolName << " (refCount " << count << ")" << endmsg;
291  // finalize of one tool may trigger a release of another tool
292  // pTool->sysFinalize().ignore();
293  if ( !finalizeTool( pTool ).isSuccess() ) {
294  warning() << " FAILURE finalizing " << toolName << endmsg;
295  fail = true;
296  }
297  // postpone deletion
298  finalizedTools.push_back( pTool );
299  } else {
300  // Place back at the front of the list to try again later
301  // ToolSvc::releaseTool( IAlgTool* ) remains active for this tool
302  ON_DEBUG debug() << " Delaying finalization of " << toolName << " (refCount " << count << ")" << endmsg;
303  tools.insert( std::begin( tools ), pTool );
304  }
305  } // end of inner loop
306  toolCount = tools.size();
307  endRefCount = totalRefCount( tools );
308  endMinRefCount = minimumRefCount( tools );
309  }; // end of outer loop
310 
311  //
312  // Second pass: Delete all finalized tools
313  //
314  // Delete in the order of increasing number of refCounts.
315  // Loop over tools in the same order as the order in which they were finalized.
316  // All tools in the list of finalized tools are no longer in the instancesTools list.
317  // If a tool destructor calls releaseTool() on another tool, this will have no
318  // effect on the 'other tool' if this 'other tool' is in the list of finalized tools.
319  // If this 'other tool' is still in the instancesTools list, it may trigger finalization
320  // (in releaseTool()), deletion and removal from the instancesTools list.
321  // Therefore, the check on non-finalised tools should happen *after* the deletion
322  // of the finalized tools.
323  ON_DEBUG debug() << "Deleting " << finalizedTools.size() << " finalized tools" << endmsg;
324  auto maxLoop = totalRefCount( finalizedTools ) + 1;
325  while ( --maxLoop > 0 && !finalizedTools.empty() ) {
326  IAlgTool* pTool = finalizedTools.front();
327  finalizedTools.pop_front();
328  auto count = pTool->refCount();
329  if ( count == 1 ) {
330  ON_DEBUG debug() << " Performing deletion of " << pTool->name() << endmsg;
331  } else {
332  ON_VERBOSE verbose() << " Delaying deletion of " << pTool->name() << " (refCount " << count << ")" << endmsg;
333  // Move to the end when refCount still not zero
334  finalizedTools.push_back( pTool );
335  }
336  // do a forced release
337  pTool->release();
338  }
339 
340  // Error if by now not all tools are properly finalised
341  if ( !tools.empty() ) {
342  error() << "Unable to finalize and delete the following tools : ";
343  for ( const auto& iTool : tools ) { error() << iTool->name() << ": " << iTool->refCount() << " "; }
344  error() << endmsg;
345  }
346 
347  // by now, all tools should be deleted and removed.
348  if ( !finalizedTools.empty() ) {
349  error() << "Failed to delete the following " << finalizedTools.size()
350  << " finalized tools. Bug in ToolSvc::finalize()?: ";
351  for ( const auto& iTool : finalizedTools ) { error() << iTool->name() << ": " << iTool->refCount() << " "; }
352  error() << endmsg;
353  }
354 
355  // Finalize this specific service
356  return ( Service::finalize().isSuccess() && !fail ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
357 }

◆ finalizeTool()

StatusCode ToolSvc::finalizeTool ( IAlgTool itool) const
private

Finalize the given tool, with exception handling.

Definition at line 737 of file ToolSvc.cpp.

739 {
740 
741  // Cache tool name in case of errors
742  const auto& toolName = itool->name();
743  StatusCode sc;
744 
745  // Finalise the tool inside a try block
746  try {
747  sc = itool->sysFinalize();
748  }
749  // Catch any exceptions
750  catch ( const GaudiException& Exception ) {
751  error() << "GaudiException with tag=" << Exception.tag() << " caught whilst finalizing tool '" << toolName << "'"
752  << endmsg << Exception << endmsg;
753  sc = StatusCode::FAILURE;
754  } catch ( const std::exception& Exception ) {
755  error() << "Standard std::exception caught whilst finalizing tool '" << toolName << "'" << endmsg
756  << Exception.what() << endmsg;
757  sc = StatusCode::FAILURE;
758  } catch ( ... ) {
759  error() << "UNKNOWN Exception caught whilst finalizing tool '" << toolName << "'" << endmsg;
760  sc = StatusCode::FAILURE;
761  }
762 
763  return sc;
764 }

◆ getInstances() [1/2]

std::vector< std::string > ToolSvc::getInstances ( ) const
override

Get names of all tool instances.

Definition at line 463 of file ToolSvc.cpp.

465 {
466  auto lock = std::scoped_lock{ m_mut };
469  []( const IAlgTool* t ) { return t->name(); } );
470  return tools;
471 }

◆ getInstances() [2/2]

std::vector< std::string > ToolSvc::getInstances ( std::string_view  toolType)
override

Get names of all tool instances of a given type.

Definition at line 451 of file ToolSvc.cpp.

453 {
454 
456  auto lock = std::scoped_lock{ m_mut };
457  for ( const auto& tool : m_instancesTools ) {
458  if ( tool->type() == toolType ) tools.push_back( tool->name() );
459  }
460  return tools;
461 }

◆ getTools()

std::vector< IAlgTool * > ToolSvc::getTools ( ) const
override

Get pointers to all tool instances.

Definition at line 473 of file ToolSvc.cpp.

475 {
476  auto lock = std::scoped_lock{ m_mut };
478 }

◆ nameTool()

std::string ToolSvc::nameTool ( std::string_view  nameByUser,
const IInterface parent 
)

Get Tool full name by combining nameByUser and "parent" part.

Definition at line 710 of file ToolSvc.cpp.

712 {
713 
714  if ( !parent ) { return std::string{ this->name() }.append( "." ).append( toolname ); } // RETURN
715 
716  // check that parent has a name!
717  auto named_parent = SmartIF<INamedInterface>( const_cast<IInterface*>( parent ) );
718  if ( named_parent ) {
719  auto fullname = std::string{ named_parent->name() }.append( "." ).append( toolname );
720  return fullname; // RETURN
721  }
722 
723  error() << "Private Tools only allowed for components implementing INamedInterface" << endmsg;
724  //
725  return std::string{ "." }.append( toolname );
726 }

◆ registerObserver()

void ToolSvc::registerObserver ( IToolSvc::Observer obs)
override

Definition at line 767 of file ToolSvc.cpp.

767  {
768  //------------------------------------------------------------------------------
769  if ( !obs ) throw GaudiException( "Received NULL pointer", this->name() + "::registerObserver", StatusCode::FAILURE );
770 
771  auto lock = std::scoped_lock{ m_mut };
772  obs->setUnregister( [this, obs]() {
773  auto lock = std::scoped_lock{ m_mut };
774  auto i = std::find( m_observers.begin(), m_observers.end(), obs );
775  if ( i != m_observers.end() ) m_observers.erase( i );
776  } );
777  m_observers.push_back( obs );
778 }

◆ releaseTool()

StatusCode ToolSvc::releaseTool ( IAlgTool tool)
override

Release tool.

Definition at line 480 of file ToolSvc.cpp.

482 {
483  auto lock = std::scoped_lock{ m_mut };
485  // test if tool is in known list (protect trying to access a previously deleted tool)
486  if ( m_instancesTools.contains( tool ) ) {
487  unsigned long count = tool->refCount();
488  if ( count == 1 ) {
489  // finalize the tool
490 
492  // We are being called during ToolSvc::finalize()
493  // message format matches the one in ToolSvc::finalize()
494  debug() << " Performing finalization of " << tool->name() << " (refCount " << count << ")" << endmsg;
495  // message format matches the one in ToolSvc::finalize()
496  debug() << " Performing deletion of " << tool->name() << endmsg;
497  } else {
498  debug() << "Performing finalization and deletion of " << tool->name() << endmsg;
499  }
500  sc = finalizeTool( tool );
501  // remove from known tools...
503  }
504  tool->release();
505  }
506  return sc;
507 }

◆ retrieve() [1/2]

StatusCode ToolSvc::retrieve ( std::string_view  tooltype,
std::string_view  toolname,
const InterfaceID iid,
IAlgTool *&  tool,
const IInterface parent,
bool  createIf 
)
override

Retrieve tool, create it by default as common tool if it does not already exist.

invoke retrieve callbacks...

Definition at line 393 of file ToolSvc.cpp.

396 {
397  // check the applicability of another method:
398  // ignore the provided name if it is empty or the type contains a name
399  if ( toolname.empty() || ( std::string_view::npos != tooltype.find( '/' ) ) ) {
400  return retrieve( tooltype, iid, tool, parent, createIf );
401  }
402 
403  // check for tools, which by name are required to be public:
404  if ( ba::ends_with( toolname, s_PUBLIC ) ) {
405  // parent for PUBLIC tool is this, i.e. ToolSvc
406  toolname.remove_suffix( s_PUBLIC.size() );
407  return retrieve( tooltype, toolname, iid, tool, this, createIf );
408  }
409 
411 
412  tool = nullptr;
413 
414  // If parent is not specified it means it is the ToolSvc itself
415  if ( !parent ) parent = this;
416  const std::string fullname = nameTool( toolname, parent );
417 
418  // Find tool in list of those already existing, and tell its
419  // interface that it has been used one more time
420  auto lock = std::scoped_lock{ m_mut };
421  IAlgTool* itool = m_instancesTools.find( fullname, parent );
422  if ( itool ) { ON_DEBUG debug() << "Retrieved tool " << toolname << " with parent " << parent << endmsg; }
423 
424  if ( !itool ) {
425  // Instances of this tool do not exist, create an instance if desired
426  // otherwise return failure
427  if ( !createIf ) {
428  warning() << "Tool " << toolname << " not found and creation not requested" << endmsg;
429  return sc;
430  }
431  sc = create( std::string{ tooltype }, std::string{ toolname }, parent, itool );
432  if ( sc.isFailure() ) { return sc; }
433  }
434 
435  // Get the right interface of it
436  sc = itool->queryInterface( iid, pp_cast<void>( &tool ) );
437  if ( sc.isFailure() ) {
438  error() << "Tool " << toolname << " either does not implement the correct interface, or its version is incompatible"
439  << endmsg;
440  return sc;
441  }
442 
447  [&]( IToolSvc::Observer* obs ) { obs->onRetrieve( itool ); } );
448  return sc;
449 }

◆ retrieve() [2/2]

StatusCode ToolSvc::retrieve ( std::string_view  type,
const InterfaceID iid,
IAlgTool *&  tool,
const IInterface parent,
bool  createIf 
)
override

Retrieve tool, create it by default as common tool if it does not already exist.

Definition at line 369 of file ToolSvc.cpp.

372 {
373  // check for tools, which by name are required to be public:
374  if ( ba::ends_with( tooltype, s_PUBLIC ) ) {
375  // parent for PUBLIC tool is 'this', i.e. ToolSvc
376  tooltype.remove_suffix( s_PUBLIC.size() );
377  return retrieve( tooltype, iid, tool, this, createIf );
378  }
379 
380  // protect against empty type
381  if ( tooltype.empty() ) {
382  error() << "retrieve(): No Tool Type/Name given" << endmsg;
383  return StatusCode::FAILURE;
384  }
385  auto pos = tooltype.find( '/' );
386  if ( std::string_view::npos == pos ) { return retrieve( tooltype, tooltype, iid, tool, parent, createIf ); }
387  return retrieve( tooltype.substr( 0, pos ), tooltype.substr( pos + 1 ), iid, tool, parent, createIf );
388 }

◆ start()

StatusCode ToolSvc::start ( )
override

Definition at line 781 of file ToolSvc.cpp.

783 {
784 
785  ON_DEBUG debug() << "START transition for AlgTools" << endmsg;
786 
787  if ( m_showToolDataDeps.value() ) {
788  info() << "Listing Data Dependencies of all Tools";
789  for ( auto& iTool : m_instancesTools ) {
790  IDataHandleHolder* idh = dynamic_cast<IDataHandleHolder*>( iTool );
791  if ( idh ) {
792  std::ostringstream ost;
793  for ( auto& dh : idh->inputHandles() ) { ost << "\n INPUT " << dh->fullKey(); }
794  for ( auto& id : idh->extraInputDeps() ) { ost << "\n EXTRA INPUT " << id; }
795  for ( auto& dh : idh->outputHandles() ) { ost << "\n OUTPUT " << dh->fullKey(); }
796  for ( auto& id : idh->extraOutputDeps() ) { ost << "\n EXTRA OUTPUT " << id; }
797  if ( ost.str().length() > 0 ) { info() << "\n" << iTool->name() << ost.str(); }
798  } else {
799  error() << "can't cast " << iTool->name() << " to IDataHandleHolder!" << endmsg;
800  }
801  }
802  info() << endmsg;
803  }
804 
805  bool fail( false );
806  for ( auto& iTool : m_instancesTools ) {
807  ON_VERBOSE verbose() << iTool->name() << "::start()" << endmsg;
808 
809  if ( !iTool->sysStart().isSuccess() ) {
810  fail = true;
811  error() << iTool->name() << " failed to start()" << endmsg;
812  }
813  }
814 
815  if ( fail ) {
816  error() << "One or more AlgTools failed to start()" << endmsg;
817  return StatusCode::FAILURE;
818  }
819  return StatusCode::SUCCESS;
820 }

◆ stop()

StatusCode ToolSvc::stop ( )
override

Definition at line 823 of file ToolSvc.cpp.

825 {
826 
827  ON_DEBUG debug() << "STOP transition for AlgTools" << endmsg;
828 
829  bool fail( false );
830  for ( auto& iTool : m_instancesTools ) {
831  ON_VERBOSE verbose() << iTool->name() << "::stop()" << endmsg;
832 
833  if ( !iTool->sysStop().isSuccess() ) {
834  fail = true;
835  error() << iTool->name() << " failed to stop()" << endmsg;
836  }
837  }
838 
839  if ( fail ) {
840  error() << "One or more AlgTools failed to stop()" << endmsg;
841  return StatusCode::FAILURE;
842  }
843  return StatusCode::SUCCESS;
844 }

Member Data Documentation

◆ m_checkNamedToolsConfigured

Gaudi::Property<bool> ToolSvc::m_checkNamedToolsConfigured
private
Initial value:
{
this, "CheckedNamedToolsConfigured", false,
"Check that tools which do not have the default name have some explicit configuration." }

Definition at line 105 of file ToolSvc.cpp.

◆ m_instancesTools

ToolList ToolSvc::m_instancesTools
private

Definition at line 177 of file ToolSvc.cpp.

◆ m_mut

std::recursive_mutex ToolSvc::m_mut
mutableprivate

Definition at line 176 of file ToolSvc.cpp.

◆ m_observers

std::vector<IToolSvc::Observer*> ToolSvc::m_observers
private

Definition at line 182 of file ToolSvc.cpp.

◆ m_pHistorySvc

SmartIF<IHistorySvc> ToolSvc::m_pHistorySvc
private

Pointer to HistorySvc.

Definition at line 180 of file ToolSvc.cpp.

◆ m_showToolDataDeps

Gaudi::Property<bool> ToolSvc::m_showToolDataDeps { this, "ShowDataDeps", false, "show the data dependencies of AlgTools" }
private

Definition at line 109 of file ToolSvc.cpp.


The documentation for this class was generated from the following file:
IDataHandleHolder
Definition: IDataHandleHolder.h:24
ToolSvc::m_observers
std::vector< IToolSvc::Observer * > m_observers
Definition: ToolSvc.cpp:182
ToolSvc::m_mut
std::recursive_mutex m_mut
Definition: ToolSvc.cpp:176
std::lock
T lock(T... args)
std::for_each
T for_each(T... args)
ToolSvc::finalizeTool
StatusCode finalizeTool(IAlgTool *itool) const
Finalize the given tool, with exception handling.
Definition: ToolSvc.cpp:737
std::string
STL class.
IAlgTool
Definition: IAlgTool.h:33
ToolSvc::m_pHistorySvc
SmartIF< IHistorySvc > m_pHistorySvc
Pointer to HistorySvc.
Definition: ToolSvc.cpp:180
ToolSvc::nameTool
std::string nameTool(std::string_view nameByUser, const IInterface *parent)
Get Tool full name by combining nameByUser and "parent" part.
Definition: ToolSvc.cpp:710
Gaudi.Configuration.log
log
Definition: Configuration.py:28
std::exception
STL class.
IToolSvc::Observer::setUnregister
void setUnregister(std::function< void()> unregister)
Definition: IToolSvc.h:235
IDataHandleHolder::extraInputDeps
virtual const DataObjIDColl & extraInputDeps() const =0
std::move
T move(T... args)
Service::m_state
Gaudi::StateMachine::State m_state
Service state
Definition: Service.h:203
ToolSvc::existsTool
bool existsTool(std::string_view fullname) const
Check if the tool instance exists.
Definition: ToolSvc.cpp:729
std::vector< std::string >
std::find
T find(T... args)
std::vector::size
T size(T... args)
GaudiException
Definition: GaudiException.h:31
IAlgTool::sysFinalize
virtual StatusCode sysFinalize()=0
Finalization of the Tool.
INamedInterface::name
virtual const std::string & name() const =0
Retrieve the name of the instance.
IInterface::queryInterface
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.
std::vector::back
T back(T... args)
ON_VERBOSE
#define ON_VERBOSE
Definition: ToolSvc.cpp:33
GaudiPartProp.tests.id
id
Definition: tests.py:111
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:222
AlgTool::name
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition: AlgTool.cpp:67
ToolSvc::ToolList::contains
bool contains(std::string_view name) const
Definition: ToolSvc.cpp:151
PropertyHolder::bindPropertiesTo
void bindPropertiesTo(Gaudi::Interfaces::IOptionsSvc &optsSvc)
Definition: PropertyHolder.h:252
std::vector::push_back
T push_back(T... args)
Service::tools
const std::vector< IAlgTool * > & tools() const
Definition: Service.cpp:409
PropertyHolder::getProperties
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
Definition: PropertyHolder.h:222
bug_34121.t
t
Definition: bug_34121.py:31
bug_34121.tool
tool
Definition: bug_34121.py:18
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:332
StatusCode
Definition: StatusCode.h:65
IDataHandleHolder::extraOutputDeps
virtual const DataObjIDColl & extraOutputDeps() const =0
Gaudi::StateMachine::OFFLINE
@ OFFLINE
Definition: StateMachine.h:23
ToolSvc::m_checkNamedToolsConfigured
Gaudi::Property< bool > m_checkNamedToolsConfigured
Definition: ToolSvc.cpp:105
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
std::vector::erase
T erase(T... args)
ToolSvc::ToolList::remove
void remove(IAlgTool *tool)
Definition: ToolSvc.cpp:132
SmartIF
Definition: IConverter.h:25
genconfuser.verbose
verbose
Definition: genconfuser.py:28
ToolSvc::ToolList::find
auto find(std::string_view name, const IInterface *parent) const
Definition: ToolSvc.cpp:159
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
std::vector::pop_back
T pop_back(T... args)
std::transform
T transform(T... args)
IDataHandleHolder::inputHandles
virtual std::vector< Gaudi::DataHandle * > inputHandles() const =0
Gaudi::StateMachine::RUNNING
@ RUNNING
Definition: StateMachine.h:26
IToolSvc::Observer
allow call-backs when a tool is a created or retrieved
Definition: IToolSvc.h:230
std::string::append
T append(T... args)
ToolSvc::create
StatusCode create(const std::string &type, const IInterface *parent, IAlgTool *&tool)
Create Tool standard way with automatically assigned name.
Definition: ToolSvc.cpp:510
std::ostringstream
STL class.
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
ToolSvc::retrieve
StatusCode retrieve(std::string_view type, const InterfaceID &iid, IAlgTool *&tool, const IInterface *parent, bool createIf) override
Retrieve tool, create it by default as common tool if it does not already exist.
Definition: ToolSvc.cpp:369
IToolSvc::Observer::onRetrieve
virtual void onRetrieve(const IAlgTool *)
Definition: IToolSvc.h:238
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
gaudirun.toolname
toolname
Definition: gaudirun.py:454
ToolSvc::ToolList::size
auto size() const
Definition: ToolSvc.cpp:156
AlgTool
Definition: AlgTool.h:62
std::begin
T begin(T... args)
std::vector::insert
T insert(T... args)
IInterface
Definition: IInterface.h:239
IDataHandleHolder::outputHandles
virtual std::vector< Gaudi::DataHandle * > outputHandles() const =0
std::count
T count(T... args)
std::string::empty
T empty(T... args)
Service::m_targetState
Gaudi::StateMachine::State m_targetState
Service state
Definition: Service.h:205
std::ostringstream::str
T str(T... args)
ON_DEBUG
#define ON_DEBUG
Definition: ToolSvc.cpp:32
std::end
T end(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
IInterface::refCount
virtual unsigned long refCount() const =0
Current reference count.
IInterface::release
virtual unsigned long release()=0
Release Interface instance.
Gaudi::Details::Property::ParsingErrorPolicy::Exception
@ Exception
ToolSvc::m_instancesTools
ToolList m_instancesTools
Definition: ToolSvc.cpp:177
Gaudi::Interfaces::IOptionsSvc
Interface for a component that manages application configuration options.
Definition: IOptionsSvc.h:46
ToolSvc::m_showToolDataDeps
Gaudi::Property< bool > m_showToolDataDeps
Definition: ToolSvc.cpp:109
Service::service
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:89
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:335
Gaudi::Interfaces::IOptionsSvc::isSet
virtual bool isSet(const std::string &key) const =0
Test if an option key was explicitly set or not.