The Gaudi Framework  master (37c0b60a)
PluginServiceV2.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2013-2022 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 
13 
14 #define GAUDI_PLUGIN_SERVICE_V2
16 
17 #include <dirent.h>
18 #include <dlfcn.h>
19 
20 #include <cstdlib>
21 #include <fstream>
22 #include <iostream>
23 #include <memory>
24 #include <regex>
25 
26 #include <cxxabi.h>
27 #include <sys/stat.h>
28 
29 #ifdef _GNU_SOURCE
30 # include <cstring>
31 # include <dlfcn.h>
32 #endif
33 
34 #ifdef USE_BOOST_FILESYSTEM
35 # include <boost/filesystem.hpp>
36 namespace fs = boost::filesystem;
37 #else
38 # include <filesystem>
39 namespace fs = std::filesystem;
40 #endif // USE_BOOST_FILESYSTEM
41 
42 #if __cplusplus >= 201703
43 # include <string_view>
44 #else
45 # include <experimental/string_view>
46 namespace std {
47  using experimental::string_view;
48 }
49 #endif
50 
51 namespace {
52  std::mutex registrySingletonMutex;
53 }
54 
55 #include <algorithm>
56 
57 namespace {
58  struct OldStyleCnv {
60  void operator()( const char c ) {
61  switch ( c ) {
62  case '<':
63  case '>':
64  case ',':
65  case '(':
66  case ')':
67  case ':':
68  case '.':
69  name.push_back( '_' );
70  break;
71  case '&':
72  name.push_back( 'r' );
73  break;
74  case '*':
75  name.push_back( 'p' );
76  break;
77  case ' ':
78  break;
79  default:
80  name.push_back( c );
81  break;
82  }
83  }
84  };
86  std::string old_style_name( const std::string& name ) {
87  return std::for_each( name.begin(), name.end(), OldStyleCnv() ).name;
88  }
89 } // namespace
90 
91 namespace Gaudi {
92  namespace PluginService {
94  namespace Details {
96  int status;
98  abi::__cxa_demangle( id.c_str(), nullptr, nullptr, &status ), free );
99  if ( !realname ) return id;
100 #if _GLIBCXX_USE_CXX11_ABI
101  return std::regex_replace(
102  realname.get(),
103  std::regex{ "std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >( (?=>))?" },
104  "std::string" );
105 #else
106  return std::string{ realname.get() };
107 #endif
108  }
109  std::string demangle( const std::type_info& id ) { return demangle( id.name() ); }
110 
111  Registry& Registry::instance() {
112  auto _guard = std::scoped_lock{ ::registrySingletonMutex };
113  static Registry r;
114  return r;
115  }
116 
117  void reportBadAnyCast( const std::type_info& factory_type, const std::string& id ) {
118  if ( logger().level() <= Logger::Debug ) {
120  const auto& info = Registry::instance().getInfo( id );
121  msg << "bad any_cast: requested factory " << id << " of type " << demangle( factory_type ) << ", got ";
122  if ( info.is_set() )
123  msg << demangle( info.factory.type() ) << " from " << info.library;
124  else
125  msg << "nothing";
126  logger().debug( msg.str() );
127  }
128  }
129 
130  Registry::Properties::mapped_type Registry::FactoryInfo::getprop( const Properties::key_type& name ) const {
131  auto p = properties.find( name );
132  return ( p != end( properties ) ) ? p->second : Properties::mapped_type{};
133  }
134 
135  Registry::Registry() {}
136 
137  void Registry::initialize() {
138  auto _guard = std::scoped_lock{ m_mutex };
139 #if defined( _WIN32 )
140  const char* envVar = "PATH";
141  const char sep = ';';
142 #elif defined( __APPLE__ )
143  const char* envVar = "GAUDI_PLUGIN_PATH";
144  const char sep = ':';
145 #else
146  const char* envVar = "LD_LIBRARY_PATH";
147  const char sep = ':';
148 #endif
149 
150  std::regex line_format{ "^(?:[[:space:]]*(?:(v[0-9]+)::)?([^:]+):(.*[^[:space:]]))?[[:space:]]*(?:#.*)?$" };
151  std::smatch m;
152 
153  std::string_view search_path;
154  if ( auto ptr = std::getenv( envVar ) ) search_path = std::string_view{ ptr };
155  if ( !search_path.empty() ) {
156  logger().debug( std::string( "searching factories in " ) + envVar );
157 
158  std::string_view::size_type start_pos = 0, end_pos = 0;
159  while ( start_pos != std::string_view::npos ) {
160  // correctly handle begin of string or path separator
161  if ( start_pos ) ++start_pos;
162 
163  end_pos = search_path.find( sep, start_pos );
164  fs::path dirName =
165 #ifdef USE_BOOST_FILESYSTEM
166  std::string{ search_path.substr( start_pos, end_pos - start_pos ) };
167 #else
168  search_path.substr( start_pos, end_pos - start_pos );
169 #endif
170  start_pos = end_pos;
171  logger().debug( " looking into " + dirName.string() );
172  // look for files called "*.components" in the directory
173  if ( is_directory( dirName ) ) {
174  for ( auto& p : fs::directory_iterator( dirName ) ) {
175  if ( p.path().extension() == ".components" && is_regular_file( p.path() ) ) {
176  // read the file
177  const auto& fullPath = p.path().string();
178  logger().debug( " reading " + p.path().filename().string() );
179  std::ifstream factories{ fullPath };
181  int factoriesCount = 0;
182  int lineCount = 0;
183  while ( !factories.eof() ) {
184  ++lineCount;
186  if ( regex_match( line, m, line_format ) ) {
187  if ( m[1] == "v2" ) { // ignore non "v2" and "empty" lines
188  const std::string lib{ m[2] };
189  const std::string fact{ m[3] };
190  m_factories.emplace( fact, FactoryInfo{ lib, {}, { { "ClassName", fact } } } );
191 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
192  // add an alias for the factory using the Reflex convention
193  std::string old_name = old_style_name( fact );
194  if ( fact != old_name ) {
195  m_factories.emplace(
196  old_name, FactoryInfo{ lib, {}, { { "ReflexName", "true" }, { "ClassName", fact } } } );
197  }
198 #endif
199  ++factoriesCount;
200  }
201  } else {
202  logger().warning( "failed to parse line " + fullPath + ':' + std::to_string( lineCount ) );
203  }
204  }
205  if ( logger().level() <= Logger::Debug ) {
206  logger().debug( " found " + std::to_string( factoriesCount ) + " factories" );
207  }
208  }
209  }
210  }
211  }
212  }
213  }
214 
215  const Registry::FactoryMap& Registry::factories() const {
216  std::call_once( m_initialized, &Registry::initialize, const_cast<Registry*>( this ) );
217  return m_factories;
218  }
219 
220  Registry::FactoryMap& Registry::factories() {
221  std::call_once( m_initialized, &Registry::initialize, this );
222  return m_factories;
223  }
224 
225  Registry::FactoryInfo& Registry::add( const KeyType& id, FactoryInfo info ) {
226  auto _guard = std::scoped_lock{ m_mutex };
227  FactoryMap& facts = factories();
228 
229 #ifdef GAUDI_REFLEX_COMPONENT_ALIASES
230  // add an alias for the factory using the Reflex convention
231  const auto old_name = old_style_name( id );
232  if ( id != old_name ) {
233  auto new_info = info;
234 
235  new_info.properties["ReflexName"] = "true";
236 
237  add( old_name, new_info );
238  }
239 #endif
240 
241  auto entry = facts.find( id );
242  if ( entry == facts.end() ) {
243  // this factory was not known yet
244  entry = facts.emplace( id, std::move( info ) ).first;
245  } else {
246  // do not replace an existing factory with a new one
247  if ( !entry->second.is_set() ) entry->second = std::move( info );
248  }
249  return entry->second;
250  }
251 
252  Registry::FactoryMap::size_type Registry::erase( const KeyType& id ) {
253  auto _guard = std::scoped_lock{ m_mutex };
254  FactoryMap& facts = factories();
255  return facts.erase( id );
256  }
257 
258  const Registry::FactoryInfo& Registry::getInfo( const KeyType& id, const bool load ) const {
259  auto _guard = std::scoped_lock{ m_mutex };
260  static const FactoryInfo unknown = { "unknown" };
261  const FactoryMap& facts = factories();
262  auto f = facts.find( id );
263 
264  if ( f != facts.end() ) {
265  if ( load && !f->second.is_set() ) {
266  const std::string library = f->second.library;
267 #ifdef __APPLE__
268  std::string_view search_path;
269  if ( auto ptr = std::getenv( "GAUDI_PLUGIN_PATH" ) ) search_path = std::string_view{ ptr };
270  if ( !search_path.empty() ) {
271 
272  std::string_view::size_type start_pos = 0, end_pos = 0;
273  while ( start_pos != std::string_view::npos ) {
274  // correctly handle begin of string or path separator
275  if ( start_pos ) ++start_pos;
276 
277  end_pos = search_path.find( ":", start_pos );
278  fs::path dirName =
279 # ifdef USE_BOOST_FILESYSTEM
280  std::string{ search_path.substr( start_pos, end_pos - start_pos ) };
281 # else
282  search_path.substr( start_pos, end_pos - start_pos );
283 # endif
284  start_pos = end_pos;
285 
286  logger().debug( " looking into " + dirName.string() );
287  // look for files called "*.components" in the directory
288  if ( is_directory( dirName ) && is_regular_file( dirName / fs::path( library ) ) ) {
289  logger().debug( "found " + dirName.string() + "/" + library.c_str() + " for factory " + id );
290  if ( !dlopen( ( dirName.string() + "/" + library ).c_str(), RTLD_LAZY | RTLD_GLOBAL ) ) {
291  logger().warning( "cannot load " + library + " for factory " + id );
292  char* dlmsg = dlerror();
293  if ( dlmsg ) logger().warning( dlmsg );
294  return unknown;
295  }
296  }
297  }
298  }
299 #else
300 
301  if ( !dlopen( library.c_str(), RTLD_LAZY | RTLD_GLOBAL ) ) {
302  logger().warning( "cannot load " + library + " for factory " + id );
303  char* dlmsg = dlerror();
304  if ( dlmsg ) logger().warning( dlmsg );
305  return unknown;
306  }
307 #endif
308  f = facts.find( id ); // ensure that the iterator is valid
309  }
310  return f->second;
311  } else {
312  return unknown;
313  }
314  }
315 
316  Registry& Registry::addProperty( const KeyType& id, const KeyType& k, const std::string& v ) {
317  auto _guard = std::scoped_lock{ m_mutex };
318  FactoryMap& facts = factories();
319  auto f = facts.find( id );
320 
321  if ( f != facts.end() ) f->second.properties[k] = v;
322  return *this;
323  }
324 
325  void Registry::setError( const KeyType& warning ) { m_werror.insert( warning ); }
326 
327  void Registry::unsetError( const KeyType& warning ) { m_werror.erase( warning ); }
328 
329  std::set<Registry::KeyType> Registry::loadedFactoryNames() const {
330  auto _guard = std::scoped_lock{ m_mutex };
332  for ( const auto& f : factories() ) {
333  if ( f.second.is_set() ) l.insert( f.first );
334  }
335  return l;
336  }
337 
338  void Logger::report( Level lvl, const std::string& msg ) {
339  static const char* levels[] = { "DEBUG : ", "INFO : ", "WARNING: ", "ERROR : " };
340  if ( lvl >= level() ) { std::cerr << levels[lvl] << msg << std::endl; }
341  }
342 
343  static auto s_logger = std::make_unique<Logger>();
344  Logger& logger() { return *s_logger; }
345  void setLogger( Logger* logger ) { s_logger.reset( logger ); }
346 
347  // This chunk of code was taken from GaudiKernel (genconf) DsoUtils.h
348  std::string getDSONameFor( void* fptr ) {
349 #if defined _GNU_SOURCE || defined __APPLE__
350  Dl_info info;
351  if ( dladdr( fptr, &info ) == 0 ) return "";
352 
353  auto pos = std::strrchr( info.dli_fname, '/' );
354  if ( pos )
355  ++pos;
356  else
357  return info.dli_fname;
358  return pos;
359 #else
360  return "";
361 #endif
362  }
363  } // namespace Details
364 
365  void SetDebug( int debugLevel ) {
366  using namespace Details;
367  Logger& l = logger();
368  if ( debugLevel > 1 )
369  l.setLevel( Logger::Debug );
370  else if ( debugLevel > 0 )
371  l.setLevel( Logger::Info );
372  else
373  l.setLevel( Logger::Warning );
374  }
375 
376  int Debug() {
377  using namespace Details;
378  switch ( logger().level() ) {
379  case Logger::Debug:
380  return 2;
381  case Logger::Info:
382  return 1;
383  default:
384  return 0;
385  }
386  }
387  }
388  } // namespace PluginService
389 } // namespace Gaudi
std::call_once
T call_once(T... args)
std::for_each
T for_each(T... args)
std::strrchr
T strrchr(T... args)
std::string
STL class.
Gaudi::PluginService::v2::Details::reportBadAnyCast
void reportBadAnyCast(const std::type_info &factory_type, const std::string &id)
Definition: PluginServiceV2.cpp:117
std::move
T move(T... args)
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
Gaudi::PluginService::v2::SetDebug
void SetDebug(int debugLevel)
Definition: PluginServiceV2.cpp:365
std::string::find
T find(T... args)
std::type_info
GAUDI_PLUGIN_SERVICE_V2_INLINE
#define GAUDI_PLUGIN_SERVICE_V2_INLINE
Definition: PluginServiceCommon.h:17
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
std::stringstream
STL class.
std::regex_match
T regex_match(T... args)
gaudirun.c
c
Definition: gaudirun.py:525
GaudiPartProp.tests.id
id
Definition: tests.py:111
PluginService.h
Gaudi::PluginService::v2::Details::setLogger
void setLogger(Logger *logger)
Definition: PluginServiceV2.cpp:345
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:108
std::cerr
std::string::c_str
T c_str(T... args)
GaudiPartProp.tests.v2
v2
Definition: tests.py:59
std::getenv
T getenv(T... args)
std::to_string
T to_string(T... args)
Gaudi::PluginService::v2::Details::getDSONameFor
std::string getDSONameFor(void *fptr)
Definition: PluginServiceV2.cpp:348
cpluginsvc.factories
def factories()
Definition: cpluginsvc.py:93
std::regex
gaudirun.level
level
Definition: gaudirun.py:364
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
gaudiComponentHelp.properties
properties
Definition: gaudiComponentHelp.py:68
Gaudi::PluginService::v2::Details::logger
Logger & logger()
Definition: PluginServiceV2.cpp:344
Gaudi::PluginService::v2::Debug
int Debug()
Definition: PluginServiceV2.cpp:376
std::string::substr
T substr(T... args)
MSG::Level
Level
Definition: IMessageSvc.h:25
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
std::endl
T endl(T... args)
gaudirun.l
dictionary l
Definition: gaudirun.py:583
std::getline
T getline(T... args)
std
STL namespace.
plotSpeedupsPyRoot.line
line
Definition: plotSpeedupsPyRoot.py:198
Properties.v
v
Definition: Properties.py:122
std::mutex
STL class.
std::smatch
IOTest.end
end
Definition: IOTest.py:125
std::unique_ptr
STL class.
std::regex_replace
T regex_replace(T... args)
std::set
STL class.
GaudiPython.Persistency.add
def add(instance)
Definition: Persistency.py:50
Gaudi::PluginService::v2::Details::demangle
std::string demangle(const std::string &id)
Definition: PluginServiceV2.cpp:95
std::ifstream
STL class.