Loading [MathJax]/extensions/tex2jax.js
Go to the documentation of this file.
20 #include "boost/algorithm/string/erase.hpp"
21 #include "boost/algorithm/string/predicate.hpp"
22 #include "boost/circular_buffer.hpp"
29 namespace ba = boost::algorithm;
31 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
32 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
37 unsigned long totalRefCount(
const C& toolList )
41 [&](
unsigned long count,
const IAlgTool*
tool ) {
return count +
tool->refCount(); } );
46 void remove( C&
c,
typename C::const_reference i ) {
53 if ( itm ==
range.second ) {
54 throw GaudiException(
"Inconsistency between tool vector and tool map", __PRETTY_FUNCTION__,
101 info() <<
"Removing all tools created by ToolSvc" <<
endmsg;
106 log <<
" Tool List : ";
107 for (
const auto& iTool :
m_instancesTools ) {
log << iTool->name() <<
":" << iTool->refCount() <<
" "; }
129 unsigned long startRefCount = 0;
131 unsigned long startMinRefCount = 0;
133 while ( toolCount > 0 && endRefCount > 0 && ( endRefCount != startRefCount || endMinRefCount != startMinRefCount ) ) {
134 ON_DEBUG if ( endMinRefCount != startMinRefCount ) {
135 debug() << toolCount <<
" tools left to finalize. Summed refCounts: " << endRefCount <<
endmsg;
136 debug() <<
"Will finalize tools with refCount <= " << endMinRefCount <<
endmsg;
138 startMinRefCount = endMinRefCount;
139 startRefCount = endRefCount;
140 unsigned long maxLoop = toolCount + 1;
145 unsigned long count = pTool->
refCount();
148 if ( count <= startMinRefCount ) {
149 ON_DEBUG debug() <<
" Performing finalization of " << toolName <<
" (refCount " << count <<
")" <<
endmsg;
153 warning() <<
" FAILURE finalizing " << toolName <<
endmsg;
157 finalizedTools.push_back( pTool );
161 ON_DEBUG debug() <<
" Delaying finalization of " << toolName <<
" (refCount " << count <<
")" <<
endmsg;
182 ON_DEBUG debug() <<
"Deleting " << finalizedTools.size() <<
" finalized tools" <<
endmsg;
183 auto maxLoop = totalRefCount( finalizedTools ) + 1;
184 while ( --maxLoop > 0 && !finalizedTools.empty() ) {
185 IAlgTool* pTool = finalizedTools.front();
186 finalizedTools.pop_front();
193 finalizedTools.push_back( pTool );
201 error() <<
"Unable to finalize and delete the following tools : ";
202 for (
const auto& iTool :
m_instancesTools ) { error() << iTool->name() <<
": " << iTool->refCount() <<
" "; }
207 if ( !finalizedTools.empty() ) {
208 error() <<
"Failed to delete the following " << finalizedTools.size()
209 <<
" finalized tools. Bug in ToolSvc::finalize()?: ";
210 for (
const auto& iTool : finalizedTools ) { error() << iTool->name() <<
": " << iTool->refCount() <<
" "; }
235 if ( ba::ends_with( tooltype, s_PUBLIC ) ) {
237 tooltype.remove_suffix( s_PUBLIC.size() );
242 if ( tooltype.empty() ) {
243 error() <<
"retrieve(): No Tool Type/Name given" <<
endmsg;
246 auto pos = tooltype.find(
'/' );
247 if ( std::string_view::npos == pos ) {
return retrieve( tooltype, tooltype, iid,
tool, parent, createIf ); }
248 return retrieve( tooltype.substr( 0, pos ), tooltype.substr( pos + 1 ), iid,
tool, parent, createIf );
260 if (
toolname.empty() || ( std::string_view::npos != tooltype.find(
'/' ) ) ) {
261 return retrieve( tooltype, iid,
tool, parent, createIf );
265 if ( ba::ends_with(
toolname, s_PUBLIC ) ) {
267 toolname.remove_suffix( s_PUBLIC.size() );
271 auto lock = std::scoped_lock{
m_mut };
279 if ( !parent ) parent =
this;
285 auto it =
std::find_if(
range.first,
range.second, [&](
auto const& p ) { return p.second->parent() == parent; } );
286 if ( it !=
range.second ) {
295 warning() <<
"Tool " <<
toolname <<
" not found and creation not requested" <<
endmsg;
305 error() <<
"Tool " <<
toolname <<
" either does not implement the correct interface, or its version is incompatible"
322 auto lock = std::scoped_lock{
m_mut };
333 auto lock = std::scoped_lock{
m_mut };
336 [](
const IAlgTool*
t ) {
return t->name(); } );
343 auto lock = std::scoped_lock{
m_mut };
350 auto lock = std::scoped_lock{
m_mut };
354 unsigned long count =
tool->refCount();
361 debug() <<
" Performing finalization of " <<
tool->name() <<
" (refCount " << count <<
")" <<
endmsg;
363 debug() <<
" Performing deletion of " <<
tool->name() <<
endmsg;
365 debug() <<
"Performing finalization and deletion of " <<
tool->name() <<
endmsg;
388 template <
typename T,
typename TM>
389 class ToolCreateGuard {
397 explicit ToolCreateGuard( T& tools, TM& toolsMap ) : m_tools( tools ), m_toolsMap( toolsMap ) {}
401 ToolCreateGuard( ToolCreateGuard&& ) noexcept =
default;
403 void create( const
std::
string& tooltype, const
std::
string& fullname, const
IInterface* parent ) {
409 m_tool = AlgTool::Factory::create( tooltype, tooltype, fullname, parent );
412 m_tools.push_back( m_tool.
get() );
413 m_toolsMap.emplace( m_tool->
name(), m_tool.
get() );
426 if ( m_tool )
remove( m_tools, m_tool.
get() );
430 template <
typename C,
typename CM>
431 ToolCreateGuard<C, CM> make_toolCreateGuard( C&
c, CM&
cm ) {
432 return ToolCreateGuard<C, CM>{
c,
cm };
449 auto lock = std::scoped_lock{
m_mut };
451 if ( tooltype.
empty() ) {
452 error() <<
"create(): No Tool Type given" <<
endmsg;
457 if ( !parent ) parent =
this;
472 error() <<
"Tool " << fullname <<
" already exists with the same parent" <<
endmsg;
473 if ( parent ==
this )
474 error() <<
"... In addition, the parent is the ToolSvc: public tools cannot be cloned!" <<
endmsg;
483 toolguard.create( tooltype, fullname, parent );
484 if ( !toolguard.get() ) {
485 error() <<
"Cannot create tool " << tooltype <<
" (No factory found)" <<
endmsg;
490 fatal() <<
"Exception with tag=" << Exception.tag() <<
" is caught whilst instantiating tool '" << tooltype <<
"'"
494 fatal() << Exception <<
endmsg;
498 fatal() <<
"Standard std::exception is caught whilst instantiating tool '" << tooltype <<
"'" <<
endmsg;
501 fatal() << Exception.what() <<
endmsg;
505 fatal() <<
"UNKNOWN Exception is caught whilst instantiating tool '" << tooltype <<
"'" <<
endmsg;
519 sc = toolguard->sysInitialize();
523 error() <<
"GaudiException with tag=" << Exception.tag() <<
" caught whilst initializing tool '" << fullname <<
"'"
527 error() <<
"Standard std::exception caught whilst initializing tool '" << fullname <<
"'" <<
endmsg
528 << Exception.what() <<
endmsg;
531 error() <<
"UNKNOWN Exception caught whilst initializing tool '" << fullname <<
"'" <<
endmsg;
537 error() <<
"Error initializing tool '" << fullname <<
"'" <<
endmsg;
544 bool propsSet =
false;
548 bool isSet = joSvc.
isSet( mytool->
name() +
"." + prop->name() );
549 if ( isSet ) propsSet =
true;
552 warning() << tooltype <<
"/" << fullname
553 <<
" : Explicitly named tools should be configured! (assigned name=" <<
toolname <<
", default is "
554 << tooltype <<
")" <<
endmsg;
561 sc = toolguard->sysStart();
564 error() <<
"Error starting tool '" << fullname <<
"'" <<
endmsg;
571 tool = toolguard.release();
594 if ( named_parent ) {
599 error() <<
"Private Tools only allowed for components implementing INamedInterface" <<
endmsg;
608 auto lock = std::scoped_lock{ m_mut };
609 return m_instancesToolsMap.find( fullname ) !=
std::end( m_instancesToolsMap );
618 const auto& toolName = itool->name();
623 sc = itool->sysFinalize();
627 error() <<
"GaudiException with tag=" << Exception.tag() <<
" caught whilst finalizing tool '" << toolName <<
"'"
631 error() <<
"Standard std::exception caught whilst finalizing tool '" << toolName <<
"'" <<
endmsg
632 << Exception.what() <<
endmsg;
635 error() <<
"UNKNOWN Exception caught whilst finalizing tool '" << toolName <<
"'" <<
endmsg;
663 auto lock = std::scoped_lock{
m_mut };
665 auto lock = std::scoped_lock{
m_mut };
680 info() <<
"Listing Data Dependencies of all Tools";
685 for (
auto& dh : idh->
inputHandles() ) { ost <<
"\n INPUT " << dh->fullKey(); }
686 for (
auto&
id : idh->
extraInputDeps() ) { ost <<
"\n EXTRA INPUT " <<
id; }
687 for (
auto& dh : idh->
outputHandles() ) { ost <<
"\n OUTPUT " << dh->fullKey(); }
689 if ( ost.
str().length() > 0 ) { info() <<
"\n" << iTool->name() << ost.
str(); }
691 error() <<
"can't cast " << iTool->name() <<
" to IDataHandleHolder!" <<
endmsg;
701 if ( !iTool->sysStart().isSuccess() ) {
703 error() << iTool->name() <<
" failed to start()" <<
endmsg;
708 error() <<
"One or more AlgTools failed to start()" <<
endmsg;
725 if ( !iTool->sysStop().isSuccess() ) {
727 error() << iTool->name() <<
" failed to stop()" <<
endmsg;
732 error() <<
"One or more AlgTools failed to stop()" <<
endmsg;
virtual StatusCode registerAlgTool(const IAlgTool &)=0
virtual const DataObjIDColl & extraInputDeps() const =0
Gaudi::StateMachine::State m_state
Service state
get
decorate the vector of properties
virtual const std::string & name() const =0
Retrieve the name of the instance.
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.
StatusCode finalize() override
void bindPropertiesTo(Gaudi::Interfaces::IOptionsSvc &optsSvc)
const std::vector< IAlgTool * > & tools() const
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
const std::string & name() const override
Retrieve name of the service
virtual const DataObjIDColl & extraOutputDeps() const =0
const ValueType & value() const
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
virtual std::vector< Gaudi::DataHandle * > inputHandles() const =0
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
constexpr static const auto SUCCESS
#define DECLARE_COMPONENT(type)
virtual std::vector< Gaudi::DataHandle * > outputHandles() const =0
Gaudi::StateMachine::State m_targetState
Service state
constexpr static const auto FAILURE
virtual unsigned long refCount() const =0
Current reference count.
virtual unsigned long release()=0
Release Interface instance.
Interface for a component that manages application configuration options.
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.
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
virtual bool isSet(const std::string &key) const =0
Test if an option key was explicitly set or not.