The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
ToolSvc Class Reference

This service manages tools. More...

Inheritance diagram for ToolSvc:
Collaboration diagram for ToolSvc:

Classes

class  ToolList
 Common Tools. More...
 

Public Member Functions

 ~ToolSvc () override
 
StatusCode finalize () override
 Finalize the service.
 
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.
 
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.
 
std::vector< std::string > getInstances (std::string_view toolType) override
 Get names of all tool instances of a given type.
 
std::vector< std::string > getInstances () const override
 Get names of all tool instances.
 
std::vector< IAlgTool * > getTools () const override
 Get pointers to all tool instances.
 
StatusCode releaseTool (IAlgTool *tool) override
 Release tool.
 
StatusCode create (const std::string &type, const IInterface *parent, IAlgTool *&tool)
 Create Tool standard way with automatically assigned name.
 
StatusCode create (const std::string &type, const std::string &name, const IInterface *parent, IAlgTool *&tool)
 Create Tool standard way with specified name.
 
bool existsTool (std::string_view fullname) const
 Check if the tool instance exists.
 
std::string nameTool (std::string_view nameByUser, const IInterface *parent)
 Get Tool full name by combining nameByUser and "parent" part.
 
void registerObserver (IToolSvc::Observer *obs) override
 
- Public Member Functions inherited from extends< Service, IToolSvc >
void const * i_cast (const InterfaceID &tid) const override
 Implementation of IInterface::i_cast.
 
StatusCode queryInterface (const InterfaceID &ti, void **pp) override
 Implementation of IInterface::queryInterface.
 
std::vector< std::string > getInterfaceNames () const override
 Implementation of IInterface::getInterfaceNames.
 
- Public Member Functions inherited from Service
const std::string & name () const override
 Retrieve name of the service.
 
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.
 
StatusCode sysStart () override
 Initialize Service.
 
StatusCode sysStop () override
 Initialize Service.
 
StatusCode sysFinalize () override
 Finalize Service.
 
StatusCode sysReinitialize () override
 Re-initialize the Service.
 
StatusCode sysRestart () override
 Re-initialize the Service.
 
 Service (std::string name, ISvcLocator *svcloc)
 Standard Constructor.
 
SmartIF< ISvcLocator > & serviceLocator () const override
 Retrieve pointer to service locator.
 
template<typename IFace = IService>
SmartIF< IFace > service (const std::string &name, bool createIf=true) const
 
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.
 
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.
 
- Public Member Functions inherited from PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >
StatusCode setProperty (const Gaudi::Details::PropertyBase &p)
 Set the property from a property.
 
StatusCode setProperty (const std::string &name, const char *v)
 Special case for string literals.
 
StatusCode setProperty (const std::string &name, const std::string &v)
 Special case for std::string.
 
StatusCode setProperty (const std::string &name, const TYPE &value)
 set the property form the value
 
 PropertyHolder ()=default
 
Gaudi::Details::PropertyBasedeclareProperty (Gaudi::Details::PropertyBase &prop)
 Declare a property.
 
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.
 
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.
 
Gaudi::Details::PropertyBasedeclareRemoteProperty (const std::string &name, IProperty *rsvc, const std::string &rname="")
 Declare a remote property.
 
StatusCode setProperty (const std::string &name, const Gaudi::Details::PropertyBase &p) override
 set the property from another property with a different name
 
StatusCode setProperty (const std::string &s) override
 set the property from the formatted string
 
StatusCode setProperty (const Gaudi::Details::PropertyBase &p)
 Set the property from a property.
 
StatusCode setProperty (const std::string &name, const char *v)
 Special case for string literals.
 
StatusCode setProperty (const std::string &name, const std::string &v)
 Special case for std::string.
 
StatusCode setProperty (const std::string &name, const TYPE &value)
 set the property form the value
 
StatusCode setPropertyRepr (const std::string &n, const std::string &r) override
 set the property from name and value string representation
 
StatusCode getProperty (Gaudi::Details::PropertyBase *p) const override
 get the property
 
const Gaudi::Details::PropertyBasegetProperty (std::string_view name) const override
 get the property by name
 
StatusCode getProperty (std::string_view n, std::string &v) const override
 convert the property to the string
 
const std::vector< Gaudi::Details::PropertyBase * > & getProperties () const override
 get all properties
 
bool hasProperty (std::string_view name) const override
 Return true if we have a property with the given name.
 
Gaudi::Details::PropertyBaseproperty (std::string_view name) const
 \fixme property and bindPropertiesTo should be protected
 
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)
 
bool msgLevel (MSG::Level lvl) const
 get the output level from the embedded MsgStream
 
- Public Member Functions inherited from CommonMessagingBase
virtual ~CommonMessagingBase ()=default
 Virtual destructor.
 
const SmartIF< IMessageSvc > & msgSvc () const
 The standard message service.
 
MsgStreammsgStream () const
 Return an uninitialized MsgStream.
 
MsgStreammsgStream (const MSG::Level level) const
 Predefined configurable message stream for the efficient printouts.
 
MsgStreamalways () const
 shortcut for the method msgStream(MSG::ALWAYS)
 
MsgStreamfatal () const
 shortcut for the method msgStream(MSG::FATAL)
 
MsgStreamerr () const
 shortcut for the method msgStream(MSG::ERROR)
 
MsgStreamerror () const
 shortcut for the method msgStream(MSG::ERROR)
 
MsgStreamwarning () const
 shortcut for the method msgStream(MSG::WARNING)
 
MsgStreaminfo () const
 shortcut for the method msgStream(MSG::INFO)
 
MsgStreamdebug () const
 shortcut for the method msgStream(MSG::DEBUG)
 
MsgStreamverbose () const
 shortcut for the method msgStream(MSG::VERBOSE)
 
MsgStreammsg () const
 shortcut for the method msgStream(MSG::INFO)
 

Private Member Functions

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

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.
 
std::vector< IToolSvc::Observer * > m_observers
 

Additional Inherited Members

- Public Types inherited from extends< Service, IToolSvc >
using base_class
 Typedef to this class.
 
using extend_interfaces_base
 Typedef to the base of this class.
 
- 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
 Typedef used to refer to this class from derived classes, as in.
 
- Public Types inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
using base_class
 
- Public Types inherited from extend_interfaces< Interfaces... >
using ext_iids
 take union of the ext_iids of all Interfaces...
 
- Protected Member Functions inherited from Service
std::vector< IAlgTool * > & tools ()
 
 ~Service () override
 
int outputLevel () const
 get the Service's output level
 
- Protected Member Functions inherited from CommonMessaging< implements< IService, IProperty, IStateful > >
MSG::Level setUpMessaging () const
 Set up local caches.
 
MSG::Level resetMessaging ()
 Reinitialize internal states.
 
void updateMsgStreamOutputLevel (int level)
 Update the output level of the cached MsgStream.
 
- Protected Attributes inherited from Service
Gaudi::StateMachine::State m_state = Gaudi::StateMachine::OFFLINE
 Service state.
 
Gaudi::StateMachine::State m_targetState = Gaudi::StateMachine::OFFLINE
 Service state.
 
Gaudi::Property< int > m_outputLevel { this, "OutputLevel", MSG::NIL, "output level" }
 flag indicating whether ToolHandle tools have been added to m_tools
 
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.
 

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 45 of file ToolSvc.cpp.

Constructor & Destructor Documentation

◆ ~ToolSvc()

ToolSvc::~ToolSvc ( )
override

Definition at line 180 of file ToolSvc.cpp.

180 {
181 // tell the remaining observers that we're gone, and forget about unregistering..
182 std::for_each( std::begin( m_observers ), std::end( m_observers ),
183 [&]( IToolSvc::Observer* obs ) { obs->setUnregister( {} ); } );
184}
void setUnregister(std::function< void()> unregister)
Definition IToolSvc.h:234
std::vector< IToolSvc::Observer * > m_observers
Definition ToolSvc.cpp:158

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 458 of file ToolSvc.cpp.

458 {
459 const std::string& toolname = tooltype;
460 return create( tooltype, toolname, parent, tool );
461}
StatusCode create(const std::string &type, const IInterface *parent, IAlgTool *&tool)
Create Tool standard way with automatically assigned name.
Definition ToolSvc.cpp:458

◆ 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 514 of file ToolSvc.cpp.

515 {
516
517 // protect against empty type
518 if ( tooltype.empty() ) {
519 error() << "create(): No Tool Type given" << endmsg;
520 return StatusCode::FAILURE;
521 }
522
523 // If parent has not been specified, assume it is the ToolSvc
524 if ( !parent ) parent = this;
525
526 tool = nullptr;
527 // Automatically deletes the tool if not explicitly kept (i.e. on success).
528 // The tool is removed from the list of known tools too.
529 auto lock = std::scoped_lock{ m_mut };
530 auto toolguard = make_toolCreateGuard( m_instancesTools );
531
532 // Check if the tool already exist : this could happen with clones
533 std::string fullname = nameTool( toolname, parent );
534 if ( existsTool( fullname ) ) {
535 // Now check if the parent is the same. This allows for clones
536 for ( IAlgTool* iAlgTool : m_instancesTools ) {
537 if ( iAlgTool->name() == toolname && iAlgTool->parent() == parent ) {
538 // The tool exist with this name, type and parent: this is bad!
539 // This excludes the possibility of cloning public tools intrinsecally
540 error() << "Tool " << fullname << " already exists with the same parent" << endmsg;
541 if ( parent == this )
542 error() << "... In addition, the parent is the ToolSvc: public tools cannot be cloned!" << endmsg;
543
544 return StatusCode::FAILURE;
545 }
546 }
547 ON_DEBUG debug() << "Creating clone of " << fullname << endmsg;
548 }
549 // instantiate the tool using the factory
550 try {
551 toolguard.create( tooltype, fullname, parent );
552 if ( !toolguard.get() ) {
553 error() << "Cannot create tool " << tooltype << " (No factory found)" << endmsg;
554 return StatusCode::FAILURE;
555 }
556 } catch ( const GaudiException& Exception ) {
557 // (1) perform the printout of message
558 fatal() << "Exception with tag=" << Exception.tag() << " is caught whilst instantiating tool '" << tooltype << "'"
559 << endmsg;
560 // (2) print the exception itself
561 // (NB! - GaudiException is a linked list of all "previous exceptions")
562 fatal() << Exception << endmsg;
563 return StatusCode::FAILURE;
564 } catch ( const std::exception& Exception ) {
565 // (1) perform the printout of message
566 fatal() << "Standard std::exception is caught whilst instantiating tool '" << tooltype << "'" << endmsg;
567 // (2) print the exception itself
568 // (NB! - GaudiException is a linked list of all "previous exceptions")
569 fatal() << Exception.what() << endmsg;
570 return StatusCode::FAILURE;
571 } catch ( ... ) {
572 // (1) perform the printout
573 fatal() << "UNKNOWN Exception is caught whilst instantiating tool '" << tooltype << "'" << endmsg;
574 return StatusCode::FAILURE;
575 }
576 ON_VERBOSE verbose() << "Created tool " << tooltype << "/" << fullname << endmsg;
577
578 // Since only AlgTool has the setProperties() method it is necessary to cast
579 // to downcast IAlgTool to AlgTool in order to set the properties via the JobOptions
580 // service
581 AlgTool* mytool = dynamic_cast<AlgTool*>( toolguard.get() );
582 if ( mytool ) mytool->bindPropertiesTo( serviceLocator()->getOptsSvc() );
583
584 // Initialize the Tool
585 StatusCode sc = StatusCode::FAILURE;
586 try {
587 sc = toolguard->sysInitialize();
588 }
589 // Catch any exceptions
590 catch ( const GaudiException& Exception ) {
591 error() << "GaudiException with tag=" << Exception.tag() << " caught whilst initializing tool '" << fullname << "'"
592 << endmsg << Exception << endmsg;
593 return StatusCode::FAILURE;
594 } catch ( const std::exception& Exception ) {
595 error() << "Standard std::exception caught whilst initializing tool '" << fullname << "'" << endmsg
596 << Exception.what() << endmsg;
597 return StatusCode::FAILURE;
598 } catch ( ... ) {
599 error() << "UNKNOWN Exception caught whilst initializing tool '" << fullname << "'" << endmsg;
600 return StatusCode::FAILURE;
601 }
602
603 // Status of tool initialization
604 if ( sc.isFailure() ) {
605 error() << "Error initializing tool '" << fullname << "'" << endmsg;
606 return sc;
607 }
608
610 // Check to ensure that non-default named tools are configured.
611 if ( toolname != tooltype ) {
612 bool propsSet = false;
613 Gaudi::Interfaces::IOptionsSvc& joSvc = this->serviceLocator()->getOptsSvc();
614 // Check that at least one of the properties has been set:
615 for ( const auto prop : mytool->getProperties() ) {
616 bool isSet = joSvc.isSet( mytool->name() + "." + prop->name() );
617 if ( isSet ) propsSet = true;
618 }
619 if ( !propsSet ) {
620 warning() << tooltype << "/" << fullname
621 << " : Explicitly named tools should be configured! (assigned name=" << toolname << ", default is "
622 << tooltype << ")" << endmsg;
623 }
624 }
625 }
626
627 // Start the tool if we are running.
629 sc = toolguard->sysStart();
630
631 if ( sc.isFailure() ) {
632 error() << "Error starting tool '" << fullname << "'" << endmsg;
633 return sc;
634 }
635 }
636
637 // The tool has been successfully created and initialized,
638 // so we inform the guard that it can release it
639 tool = toolguard.release();
640
644 std::for_each( m_observers.begin(), m_observers.end(), [&]( IToolSvc::Observer* obs ) { obs->onCreate( tool ); } );
645 // TODO: replace by generic callback
646 // Register the tool with the HistorySvc
647 m_pHistorySvc = service( "HistorySvc", false );
648 if ( m_pHistorySvc ) { m_pHistorySvc->registerAlgTool( *tool ).ignore(); }
649 return StatusCode::SUCCESS;
650}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define ON_VERBOSE
Definition ToolSvc.cpp:30
#define ON_DEBUG
Definition ToolSvc.cpp:29
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition AlgTool.cpp:72
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Gaudi::Interfaces::IOptionsSvc & getOptsSvc()
Direct access to Gaudi::Interfaces::IOptionsSvc implementation.
void bindPropertiesTo(Gaudi::Interfaces::IOptionsSvc &optsSvc)
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
Gaudi::StateMachine::State m_state
Service state.
Definition Service.h:155
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
Definition Service.h:79
bool isFailure() const
Definition StatusCode.h:129
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
bool existsTool(std::string_view fullname) const
Check if the tool instance exists.
Definition ToolSvc.cpp:668
std::recursive_mutex m_mut
Definition ToolSvc.cpp:152
std::string nameTool(std::string_view nameByUser, const IInterface *parent)
Get Tool full name by combining nameByUser and "parent" part.
Definition ToolSvc.cpp:652
ToolList m_instancesTools
Definition ToolSvc.cpp:153
Gaudi::Property< bool > m_checkNamedToolsConfigured
Definition ToolSvc.cpp:99
SmartIF< IHistorySvc > m_pHistorySvc
Pointer to HistorySvc.
Definition ToolSvc.cpp:156
virtual bool isSet(const std::string &key) const =0
Test if an option key was explicitly set or not.

◆ existsTool()

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

Check if the tool instance exists.

Definition at line 668 of file ToolSvc.cpp.

668 {
669 auto lock = std::scoped_lock{ m_mut };
670 return m_instancesTools.contains( fullname );
671}

◆ 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 186 of file ToolSvc.cpp.

186 {
187 // Finalize and delete all left-over tools. Normally all tools created with
188 // ToolSvc are left over, since ToolSvc holds a refCount (via AlgTool ctor).
189 // Several cases need to be covered:
190 // 1) Simple dependencies: no circular dependencies between tools,
191 // and tools not using other tools
192 // 2) Tools-using-tools (but no circular dependencies)
193 // a) release() is called in the tool::finalize() (e.g. via base class)
194 // b) release() is called in the tool destructor (e.g. via ToolHandle)
195 // 3) Circular dependencies between tools
196 // a) release() is called in the tool::finalize() (e.g. via base class)
197 // b) release() is called in the tool destructor (e.g. via ToolHandle)
198 // 4) In addition to each of the above cases, refCounting of a particular tool
199 // might not have been done correctly in the code. Typically release()
200 // is not called, so we are left with a too high refCount.
201 // What to do with those, and how to avoid a crash while handling them...
202
211 info() << "Removing all tools created by ToolSvc" << endmsg;
212 auto tools = std::move( m_instancesTools ).grab();
213
214 // Print out list of tools
215 ON_DEBUG {
216 auto& log = debug();
217 log << " Tool List : ";
218 for ( const auto& iTool : tools ) { log << iTool->name() << ":" << iTool->refCount() << " "; }
219 log << endmsg;
220 }
221
222 //
223 // first pass: Finalize all tools (but don't delete them)
224 //
237 boost::circular_buffer<IAlgTool*> finalizedTools( tools.size() ); // list of tools that have been finalized
238 bool fail( false );
239 size_t toolCount = tools.size();
240 unsigned long startRefCount = 0;
241 unsigned long endRefCount = totalRefCount( tools );
242 unsigned long startMinRefCount = 0;
243 unsigned long endMinRefCount = minimumRefCount( tools );
244 while ( toolCount > 0 && endRefCount > 0 && ( endRefCount != startRefCount || endMinRefCount != startMinRefCount ) ) {
245 ON_DEBUG if ( endMinRefCount != startMinRefCount ) {
246 debug() << toolCount << " tools left to finalize. Summed refCounts: " << endRefCount << endmsg;
247 debug() << "Will finalize tools with refCount <= " << endMinRefCount << endmsg;
248 }
249 startMinRefCount = endMinRefCount;
250 startRefCount = endRefCount;
251 unsigned long maxLoop = toolCount + 1;
252 while ( --maxLoop > 0 && !tools.empty() ) {
253 IAlgTool* pTool = tools.back();
254 // removing tool from list makes ToolSvc::releaseTool( IAlgTool* ) a noop
255 tools.pop_back();
256 unsigned long count = pTool->refCount();
257 // cache tool name
258 const std::string& toolName = pTool->name();
259 if ( count <= startMinRefCount ) {
260 ON_DEBUG debug() << " Performing finalization of " << toolName << " (refCount " << count << ")" << endmsg;
261 // finalize of one tool may trigger a release of another tool
262 // pTool->sysFinalize().ignore();
263 if ( !finalizeTool( pTool ).isSuccess() ) {
264 warning() << " FAILURE finalizing " << toolName << endmsg;
265 fail = true;
266 }
267 // postpone deletion
268 finalizedTools.push_back( pTool );
269 } else {
270 // Place back at the front of the list to try again later
271 // ToolSvc::releaseTool( IAlgTool* ) remains active for this tool
272 ON_DEBUG debug() << " Delaying finalization of " << toolName << " (refCount " << count << ")" << endmsg;
273 tools.insert( std::begin( tools ), pTool );
274 }
275 } // end of inner loop
276 toolCount = tools.size();
277 endRefCount = totalRefCount( tools );
278 endMinRefCount = minimumRefCount( tools );
279 }; // end of outer loop
280
281 //
282 // Second pass: Delete all finalized tools
283 //
284 // Delete in the order of increasing number of refCounts.
285 // Loop over tools in the same order as the order in which they were finalized.
286 // All tools in the list of finalized tools are no longer in the instancesTools list.
287 // If a tool destructor calls releaseTool() on another tool, this will have no
288 // effect on the 'other tool' if this 'other tool' is in the list of finalized tools.
289 // If this 'other tool' is still in the instancesTools list, it may trigger finalization
290 // (in releaseTool()), deletion and removal from the instancesTools list.
291 // Therefore, the check on non-finalised tools should happen *after* the deletion
292 // of the finalized tools.
293 ON_DEBUG debug() << "Deleting " << finalizedTools.size() << " finalized tools" << endmsg;
294 auto maxLoop = totalRefCount( finalizedTools ) + 1;
295 while ( --maxLoop > 0 && !finalizedTools.empty() ) {
296 IAlgTool* pTool = finalizedTools.front();
297 finalizedTools.pop_front();
298 auto count = pTool->refCount();
299 if ( count == 1 ) {
300 ON_DEBUG debug() << " Performing deletion of " << pTool->name() << endmsg;
301 } else {
302 ON_VERBOSE verbose() << " Delaying deletion of " << pTool->name() << " (refCount " << count << ")" << endmsg;
303 // Move to the end when refCount still not zero
304 finalizedTools.push_back( pTool );
305 }
306 // do a forced release
307 pTool->release();
308 }
309
310 // Error if by now not all tools are properly finalised
311 if ( !tools.empty() ) {
312 error() << "Unable to finalize and delete the following tools : ";
313 for ( const auto& iTool : tools ) { error() << iTool->name() << ": " << iTool->refCount() << " "; }
314 error() << endmsg;
315 }
316
317 // by now, all tools should be deleted and removed.
318 if ( !finalizedTools.empty() ) {
319 error() << "Failed to delete the following " << finalizedTools.size()
320 << " finalized tools. Bug in ToolSvc::finalize()?: ";
321 for ( const auto& iTool : finalizedTools ) { error() << iTool->name() << ": " << iTool->refCount() << " "; }
322 error() << endmsg;
323 }
324
325 // Finalize this specific service
326 return ( Service::finalize().isSuccess() && !fail ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
327}
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
virtual unsigned long refCount() const =0
Current reference count.
virtual unsigned long release() const =0
Release Interface instance.
virtual const std::string & name() const =0
Retrieve the name of the instance.
StatusCode finalize() override
Definition Service.cpp:223
const std::vector< IAlgTool * > & tools() const
Definition Service.cpp:411
StatusCode finalizeTool(IAlgTool *itool) const
Finalize the given tool, with exception handling.
Definition ToolSvc.cpp:673

◆ finalizeTool()

StatusCode ToolSvc::finalizeTool ( IAlgTool * itool) const
private

Finalize the given tool, with exception handling.

Definition at line 673 of file ToolSvc.cpp.

673 {
674
675 // Cache tool name in case of errors
676 const auto& toolName = itool->name();
677 StatusCode sc;
678
679 // Finalise the tool inside a try block
680 try {
681 sc = itool->sysFinalize();
682 }
683 // Catch any exceptions
684 catch ( const GaudiException& Exception ) {
685 error() << "GaudiException with tag=" << Exception.tag() << " caught whilst finalizing tool '" << toolName << "'"
686 << endmsg << Exception << endmsg;
688 } catch ( const std::exception& Exception ) {
689 error() << "Standard std::exception caught whilst finalizing tool '" << toolName << "'" << endmsg
690 << Exception.what() << endmsg;
692 } catch ( ... ) {
693 error() << "UNKNOWN Exception caught whilst finalizing tool '" << toolName << "'" << endmsg;
695 }
696
697 return sc;
698}
virtual StatusCode sysFinalize()=0
Finalization of the Tool.

◆ getInstances() [1/2]

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

Get names of all tool instances.

Definition at line 418 of file ToolSvc.cpp.

418 {
419 auto lock = std::scoped_lock{ m_mut };
420 std::vector<std::string> tools{ m_instancesTools.size() };
421 std::transform( std::begin( m_instancesTools ), std::end( m_instancesTools ), std::begin( tools ),
422 []( const IAlgTool* t ) { return t->name(); } );
423 return tools;
424}

◆ 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 408 of file ToolSvc.cpp.

408 {
409
410 std::vector<std::string> tools;
411 auto lock = std::scoped_lock{ m_mut };
412 for ( const auto& tool : m_instancesTools ) {
413 if ( tool->type() == toolType ) tools.push_back( tool->name() );
414 }
415 return tools;
416}

◆ getTools()

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

Get pointers to all tool instances.

Definition at line 426 of file ToolSvc.cpp.

426 {
427 auto lock = std::scoped_lock{ m_mut };
428 return { std::begin( m_instancesTools ), std::end( m_instancesTools ) };
429}

◆ 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 652 of file ToolSvc.cpp.

652 {
653
654 if ( !parent ) { return std::string{ this->name() }.append( "." ).append( toolname ); }
655
656 // check that parent has a name!
657 auto named_parent = SmartIF<INamedInterface>( const_cast<IInterface*>( parent ) );
658 if ( named_parent ) {
659 auto fullname = std::string{ named_parent->name() }.append( "." ).append( toolname );
660 return fullname;
661 }
662
663 error() << "Private Tools only allowed for components implementing INamedInterface" << endmsg;
664 //
665 return std::string{ "." }.append( toolname );
666}
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333

◆ registerObserver()

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

Definition at line 700 of file ToolSvc.cpp.

700 {
701 if ( !obs ) throw GaudiException( "Received NULL pointer", this->name() + "::registerObserver", StatusCode::FAILURE );
702
703 auto lock = std::scoped_lock{ m_mut };
704 obs->setUnregister( [this, obs]() {
705 auto lock = std::scoped_lock{ m_mut };
706 auto i = std::find( m_observers.begin(), m_observers.end(), obs );
707 if ( i != m_observers.end() ) m_observers.erase( i );
708 } );
709 m_observers.push_back( obs );
710}

◆ releaseTool()

StatusCode ToolSvc::releaseTool ( IAlgTool * tool)
override

Release tool.

Definition at line 431 of file ToolSvc.cpp.

431 {
432 auto lock = std::scoped_lock{ m_mut };
433 StatusCode sc( StatusCode::SUCCESS );
434 // test if tool is in known list (protect trying to access a previously deleted tool)
435 if ( m_instancesTools.contains( tool ) ) {
436 unsigned long count = tool->refCount();
437 if ( count == 1 ) {
438 // finalize the tool
439
441 // We are being called during ToolSvc::finalize()
442 // message format matches the one in ToolSvc::finalize()
443 debug() << " Performing finalization of " << tool->name() << " (refCount " << count << ")" << endmsg;
444 // message format matches the one in ToolSvc::finalize()
445 debug() << " Performing deletion of " << tool->name() << endmsg;
446 } else {
447 debug() << "Performing finalization and deletion of " << tool->name() << endmsg;
448 }
449 sc = finalizeTool( tool );
450 // remove from known tools...
451 m_instancesTools.remove( tool );
452 }
453 tool->release();
454 }
455 return sc;
456}
Gaudi::StateMachine::State m_targetState
Service state.
Definition Service.h:157

◆ 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 352 of file ToolSvc.cpp.

353 {
354 // check the applicability of another method:
355 // ignore the provided name if it is empty or the type contains a name
356 if ( toolname.empty() || ( std::string_view::npos != tooltype.find( '/' ) ) ) {
357 return retrieve( tooltype, iid, tool, parent, createIf );
358 }
359
360 // check for tools, which by name are required to be public:
361 if ( toolname.ends_with( s_PUBLIC ) ) {
362 // parent for PUBLIC tool is this, i.e. ToolSvc
363 toolname.remove_suffix( s_PUBLIC.size() );
364 return retrieve( tooltype, toolname, iid, tool, this, createIf );
365 }
366
367 StatusCode sc( StatusCode::FAILURE );
368
369 tool = nullptr;
370
371 // If parent is not specified it means it is the ToolSvc itself
372 if ( !parent ) parent = this;
373 const std::string fullname = nameTool( toolname, parent );
374
375 // Find tool in list of those already existing, and tell its
376 // interface that it has been used one more time
377 auto lock = std::scoped_lock{ m_mut };
378 IAlgTool* itool = m_instancesTools.find( fullname, parent );
379 if ( itool ) { ON_DEBUG debug() << "Retrieved tool " << toolname << " with parent " << parent << endmsg; }
380
381 if ( !itool ) {
382 // Instances of this tool do not exist, create an instance if desired
383 // otherwise return failure
384 if ( !createIf ) {
385 warning() << "Tool " << toolname << " not found and creation not requested" << endmsg;
386 return sc;
387 }
388 sc = create( std::string{ tooltype }, std::string{ toolname }, parent, itool );
389 if ( sc.isFailure() ) { return sc; }
390 }
391
392 // Get the right interface of it
393 sc = itool->queryInterface( iid, pp_cast<void>( &tool ) );
394 if ( sc.isFailure() ) {
395 error() << "Tool " << toolname << " either does not implement the correct interface, or its version is incompatible"
396 << endmsg;
397 return sc;
398 }
399
403 std::for_each( std::begin( m_observers ), std::end( m_observers ),
404 [&]( IToolSvc::Observer* obs ) { obs->onRetrieve( itool ); } );
405 return sc;
406}
DEST ** pp_cast(SRC **ptr)
Small function to be used instead of the construct (void**)&pointer, which produces,...
Definition IInterface.h:370
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.
virtual void onRetrieve(const IAlgTool *)
Definition IToolSvc.h:237
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:333

◆ 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 333 of file ToolSvc.cpp.

334 {
335 // check for tools, which by name are required to be public:
336 if ( tooltype.ends_with( s_PUBLIC ) ) {
337 // parent for PUBLIC tool is 'this', i.e. ToolSvc
338 tooltype.remove_suffix( s_PUBLIC.size() );
339 return retrieve( tooltype, iid, tool, this, createIf );
340 }
341
342 // protect against empty type
343 if ( tooltype.empty() ) {
344 error() << "retrieve(): No Tool Type/Name given" << endmsg;
345 return StatusCode::FAILURE;
346 }
347 auto pos = tooltype.find( '/' );
348 if ( std::string_view::npos == pos ) { return retrieve( tooltype, tooltype, iid, tool, parent, createIf ); }
349 return retrieve( tooltype.substr( 0, pos ), tooltype.substr( pos + 1 ), iid, tool, parent, createIf );
350}

◆ start()

StatusCode ToolSvc::start ( )
override

Definition at line 712 of file ToolSvc.cpp.

712 {
713
714 ON_DEBUG debug() << "START transition for AlgTools" << endmsg;
715
716 if ( m_showToolDataDeps.value() ) {
717 info() << "Listing Data Dependencies of all Tools";
718 for ( auto& iTool : m_instancesTools ) {
719 IDataHandleHolder* idh = dynamic_cast<IDataHandleHolder*>( iTool );
720 if ( idh ) {
721 std::ostringstream ost;
722 for ( auto& dh : idh->inputHandles() ) { ost << "\n INPUT " << dh->fullKey(); }
723 for ( auto& id : idh->extraInputDeps() ) { ost << "\n EXTRA INPUT " << id; }
724 for ( auto& dh : idh->outputHandles() ) { ost << "\n OUTPUT " << dh->fullKey(); }
725 for ( auto& id : idh->extraOutputDeps() ) { ost << "\n EXTRA OUTPUT " << id; }
726 if ( ost.str().length() > 0 ) { info() << "\n" << iTool->name() << ost.str(); }
727 } else {
728 error() << "can't cast " << iTool->name() << " to IDataHandleHolder!" << endmsg;
729 }
730 }
731 info() << endmsg;
732 }
733
734 bool fail( false );
735 for ( auto& iTool : m_instancesTools ) {
736 ON_VERBOSE verbose() << iTool->name() << "::start()" << endmsg;
737
738 if ( !iTool->sysStart().isSuccess() ) {
739 fail = true;
740 error() << iTool->name() << " failed to start()" << endmsg;
741 }
742 }
743
744 if ( fail ) {
745 error() << "One or more AlgTools failed to start()" << endmsg;
746 return StatusCode::FAILURE;
747 }
748 return StatusCode::SUCCESS;
749}
virtual const DataObjIDColl & extraOutputDeps() const =0
virtual std::vector< Gaudi::DataHandle * > outputHandles() const =0
virtual std::vector< Gaudi::DataHandle * > inputHandles() const =0
virtual const DataObjIDColl & extraInputDeps() const =0
Gaudi::Property< bool > m_showToolDataDeps
Definition ToolSvc.cpp:103

◆ stop()

StatusCode ToolSvc::stop ( )
override

Definition at line 751 of file ToolSvc.cpp.

751 {
752
753 ON_DEBUG debug() << "STOP transition for AlgTools" << endmsg;
754
755 bool fail( false );
756 for ( auto& iTool : m_instancesTools ) {
757 ON_VERBOSE verbose() << iTool->name() << "::stop()" << endmsg;
758
759 if ( !iTool->sysStop().isSuccess() ) {
760 fail = true;
761 error() << iTool->name() << " failed to stop()" << endmsg;
762 }
763 }
764
765 if ( fail ) {
766 error() << "One or more AlgTools failed to stop()" << endmsg;
767 return StatusCode::FAILURE;
768 }
769 return StatusCode::SUCCESS;
770}

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 99 of file ToolSvc.cpp.

99 {
100 this, "CheckedNamedToolsConfigured", false,
101 "Check that tools which do not have the default name have some explicit configuration." };

◆ m_instancesTools

ToolList ToolSvc::m_instancesTools
private

Definition at line 153 of file ToolSvc.cpp.

◆ m_mut

std::recursive_mutex ToolSvc::m_mut
mutableprivate

Definition at line 152 of file ToolSvc.cpp.

◆ m_observers

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

Definition at line 158 of file ToolSvc.cpp.

◆ m_pHistorySvc

SmartIF<IHistorySvc> ToolSvc::m_pHistorySvc
private

Pointer to HistorySvc.

Definition at line 156 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 103 of file ToolSvc.cpp.

103{ this, "ShowDataDeps", false, "show the data dependencies of AlgTools" };

The documentation for this class was generated from the following file: