CMS 3D CMS Logo

List of all members | Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | Friends
edmplugin::PluginManager Class Reference

#include <PluginManager.h>

Classes

class  Config
 

Public Types

typedef std::map< std::string, InfosCategoryToInfos
 
typedef std::vector< PluginInfoInfos
 
typedef std::vector< std::string > SearchPath
 

Public Member Functions

const CategoryToInfoscategoryToInfos () const
 
const SharedLibraryload (const std::string &iCategory, const std::string &iPlugin)
 
const std::filesystem::path & loadableFor (const std::string &iCategory, const std::string &iPlugin)
 
const PluginManageroperator= (const PluginManager &)=delete
 
 PluginManager (const PluginManager &)=delete
 
const SharedLibrarytryToLoad (const std::string &iCategory, const std::string &iPlugin)
 
 ~PluginManager ()
 

Static Public Member Functions

static PluginManagerconfigure (const Config &)
 
static PluginManagerget ()
 
static bool isAvailable ()
 
static const std::string & loadingFile ()
 file name of the shared object being loaded More...
 
static const std::string & staticallyLinkedLoadingFileName ()
 if the value returned from loadingFile matches this string then the file is statically linked More...
 

Public Attributes

edm::signalslot::Signal< void(const std::string &, const std::string &)> askedToLoadCategoryWithPlugin_
 
edm::signalslot::Signal< void(const std::filesystem::path &)> goingToLoad_
 
edm::signalslot::Signal< void(const SharedLibrary &)> justLoaded_
 

Private Member Functions

const std::filesystem::path & loadableFor_ (const std::string &iCategory, const std::string &iPlugin, bool &ioThrowIfFailElseSucceedStatus)
 
void newFactory (const PluginFactoryBase *)
 
std::recursive_mutex & pluginLoadMutex ()
 
 PluginManager (const Config &)
 

Static Private Member Functions

static std::string & loadingLibraryNamed_ ()
 
static PluginManager *& singleton ()
 

Private Attributes

CategoryToInfos categoryToInfos_
 
oneapi::tbb::concurrent_unordered_map< std::filesystem::path, std::shared_ptr< SharedLibrary >, PluginManagerPathHasherloadables_
 
std::recursive_mutex pluginLoadMutex_
 
SearchPath searchPath_
 

Friends

class DummyFriend
 

Detailed Description

Definition at line 45 of file PluginManager.h.

Member Typedef Documentation

◆ CategoryToInfos

typedef std::map<std::string, Infos> edmplugin::PluginManager::CategoryToInfos

Definition at line 51 of file PluginManager.h.

◆ Infos

Definition at line 50 of file PluginManager.h.

◆ SearchPath

typedef std::vector<std::string> edmplugin::PluginManager::SearchPath

Definition at line 49 of file PluginManager.h.

Constructor & Destructor Documentation

◆ PluginManager() [1/2]

edmplugin::PluginManager::PluginManager ( const PluginManager )
delete

Referenced by configure().

◆ ~PluginManager()

PluginManager::~PluginManager ( )

Definition at line 129 of file PluginManager.cc.

129 {}

◆ PluginManager() [2/2]

PluginManager::PluginManager ( const Config iConfig)
private

Definition at line 61 of file PluginManager.cc.

References edmplugin::PluginFactoryManager::begin(), edmplugin::standard::cachefileName(), categoryToInfos_, DeadROC_duringRun::dir, MillePedeFileConverter_cfg::e, edmplugin::PluginFactoryManager::end(), Exception, edmplugin::PluginFactoryManager::get(), mps_fire::i, loadingLibraryNamed_(), edmplugin::PluginManager::Config::mustHaveCache(), newFactory(), edmplugin::PluginFactoryManager::newFactory_, EnsembleCalibrationLA_cfg::path, edmplugin::standard::poisonedCachefileName(), edmplugin::readCacheFile(), and searchPath_.

61  : searchPath_(iConfig.searchPath()) {
62  using std::placeholders::_1;
64  // This is the filename of a file which contains plugins which exist in the
65  // base release and which should exists in the local area, otherwise they
66  // were removed and we want to catch their usage.
67  const std::filesystem::path& kPoisonedCacheFile(standard::poisonedCachefileName());
68  //NOTE: This may not be needed :/
70  pfm->newFactory_.connect(std::bind(std::mem_fn(&PluginManager::newFactory), this, _1));
71 
72  // When building a single big executable the plugins are already registered in the
73  // PluginFactoryManager, we therefore only need to populate the categoryToInfos_ map
74  // with the relevant information.
75  for (PluginFactoryManager::const_iterator i = pfm->begin(), e = pfm->end(); i != e; ++i) {
76  categoryToInfos_[(*i)->category()] = (*i)->available();
77  }
78 
79  //read in the files
80  //Since we are looping in the 'precidence' order then the lists in categoryToInfos_ will also be
81  // in that order
82  bool foundAtLeastOneCacheFile = false;
83  std::set<std::string> alreadySeen;
84  for (SearchPath::const_iterator itPath = searchPath_.begin(), itEnd = searchPath_.end(); itPath != itEnd;
85  ++itPath) {
86  //take care of the case where the same path is passed in multiple times
87  if (alreadySeen.find(*itPath) != alreadySeen.end()) {
88  continue;
89  }
90  alreadySeen.insert(*itPath);
91  std::filesystem::path dir(*itPath);
92  if (exists(dir)) {
93  if (not is_directory(dir)) {
94  throw cms::Exception("PluginManagerBadPath")
95  << "The path '" << dir.string() << "' for the PluginManager is not a directory";
96  }
97  std::filesystem::path cacheFile = dir / kCacheFile;
98 
99  if (readCacheFile(cacheFile, dir, categoryToInfos_)) {
100  foundAtLeastOneCacheFile = true;
101  }
102 
103  // We do not check for return code since we do not want to consider a
104  // poison cache file as a valid cache file having been found.
105  std::filesystem::path poisonedCacheFile = dir / kPoisonedCacheFile;
106  readCacheFile(poisonedCacheFile, dir / "poisoned", categoryToInfos_);
107  }
108  }
109  if (not foundAtLeastOneCacheFile and iConfig.mustHaveCache()) {
110  auto ex = cms::Exception("PluginManagerNoCacheFile")
111  << "No cache files named '" << standard::cachefileName() << "' were found in the directories \n";
112  for (auto const& seen : alreadySeen) {
113  ex << " '" << seen << "'\n";
114  }
115  throw ex;
116  }
117  if (iConfig.mustHaveCache() and categoryToInfos_.empty()) {
118  throw cms::Exception("PluginManagerCacheFilesEmpty") << "Cache files were found but all were empty.";
119  }
120  //Since this should not be called until after 'main' has started, we can set the value
121  loadingLibraryNamed_() = "<loaded by another plugin system>";
122  }
CategoryToInfos categoryToInfos_
const std::filesystem::path & poisonedCachefileName()
Definition: standard.cc:52
const std::filesystem::path & cachefileName()
Definition: standard.cc:47
static bool readCacheFile(const std::filesystem::path &cacheFile, const std::filesystem::path &dir, PluginManager::CategoryToInfos &categoryToInfos)
static std::string & loadingLibraryNamed_()
std::vector< const PluginFactoryBase * >::const_iterator const_iterator
void newFactory(const PluginFactoryBase *)
static PluginFactoryManager * get()

Member Function Documentation

◆ categoryToInfos()

const CategoryToInfos& edmplugin::PluginManager::categoryToInfos ( ) const
inline

The container is ordered by category, then plugin name and then by precidence order of the plugin files. Therefore the first match on category and plugin name will be the proper file to load

Definition at line 82 of file PluginManager.h.

References categoryToInfos_.

Referenced by PrintLoadingPlugins::askedToLoad(), FWDetailViewManager::findViewersFor(), FWEveViewManager::FWEveViewManager(), FWItemAccessorFactory::hasAccessor(), main(), and sim::sensitiveDetectorMakers().

82 { return categoryToInfos_; }
CategoryToInfos categoryToInfos_

◆ configure()

PluginManager & PluginManager::configure ( const Config iConfig)
static

Definition at line 321 of file PluginManager.cc.

References Exception, PluginManager(), alignCSCRings::s, edmplugin::PluginManager::Config::searchPath(), and singleton().

Referenced by FWLiteEnabler::enable(), edm::storage::StorageFactory::getMaker(), main(), edm::ProblemTracker::ProblemTracker(), cond::Utilities::run(), and edm::PluginDescription< T >::writeCfi_().

321  {
322  PluginManager*& s = singleton();
323  if (nullptr != s) {
324  throw cms::Exception("PluginManagerReconfigured");
325  }
326 
327  const Config& realConfig = iConfig;
328  if (realConfig.searchPath().empty()) {
329  throw cms::Exception("PluginManagerEmptySearchPath");
330  }
331  s = new PluginManager(realConfig);
332  return *s;
333  }
static PluginManager *& singleton()
PluginManager(const PluginManager &)=delete
Definition: Config.py:1

◆ get()

PluginManager * PluginManager::get ( )
static

Definition at line 312 of file PluginManager.cc.

References Exception, and singleton().

Referenced by Options.Options::__getitem__(), betterConfigParser.BetterConfigParser::__updateDict(), submitPVValidationJobs.BetterConfigParser::__updateDict(), PrintLoadingPlugins::askedToLoad(), edmplugin::PluginFactoryBase::checkProperLoadable(), rrapi.RRApi::columns(), rrapi.RRApi::count(), rrapi.RRApi::data(), edmplugin::PluginFactoryBase::findPMaker(), FWDetailViewManager::findViewersFor(), FWEveViewManager::FWEveViewManager(), betterConfigParser.BetterConfigParser::getCompares(), betterConfigParser.BetterConfigParser::getGeneral(), betterConfigParser.BetterConfigParser::getResultingSection(), submitPVValidationJobs.BetterConfigParser::getResultingSection(), FWItemAccessorFactory::hasAccessor(), edmplugin::PluginCapabilities::load(), edm::service::LoadAllDictionaries::LoadAllDictionaries(), main(), PrintLoadingPlugins::PrintLoadingPlugins(), edm::PluginDescription< T >::printNestedContent_(), rrapi.RRApi::report(), rrapi.RRApi::reports(), sim::sensitiveDetectorMakers(), rrapi.RRApi::tables(), rrapi.RRApi::tags(), rrapi.RRApi::templates(), edmplugin::PluginFactoryBase::tryToFindPMaker(), edmplugin::PluginCapabilities::tryToLoad(), and rrapi.RRApi::workspaces().

312  {
313  PluginManager* manager = singleton();
314  if (nullptr == manager) {
315  throw cms::Exception("PluginManagerNotConfigured")
316  << "PluginManager::get() was called before PluginManager::configure.";
317  }
318  return manager;
319  }
static PluginManager *& singleton()

◆ isAvailable()

bool PluginManager::isAvailable ( )
static

◆ load()

const SharedLibrary & PluginManager::load ( const std::string &  iCategory,
const std::string &  iPlugin 
)

Definition at line 232 of file PluginManager.cc.

References askedToLoadCategoryWithPlugin_, MillePedeFileConverter_cfg::e, goingToLoad_, justLoaded_, loadableFor(), loadables_, loadingLibraryNamed_(), AlCaHLTBitMon_ParallelJobs::p, EnsembleCalibrationLA_cfg::path, pluginLoadMutex(), and alignCSCRings::s.

Referenced by edmplugin::PluginFactoryBase::findPMaker(), and edmplugin::PluginCapabilities::load().

232  {
233  askedToLoadCategoryWithPlugin_(iCategory, iPlugin);
234  const std::filesystem::path& p = loadableFor(iCategory, iPlugin);
235 
236  //have we already loaded this?
237  auto itLoaded = loadables_.find(p);
238  if (itLoaded == loadables_.end()) {
239  //Need to make sure we only have on SharedLibrary loading at a time
240  std::lock_guard<std::recursive_mutex> guard(pluginLoadMutex());
241  //Another thread may have gotten this while we were waiting on the mutex
242  itLoaded = loadables_.find(p);
243  if (itLoaded == loadables_.end()) {
244  //try to make one
245  goingToLoad_(p);
246  Sentry s(loadingLibraryNamed_(), p.string());
247  //std::filesystem::path native(p.string());
248  std::shared_ptr<SharedLibrary> ptr;
249  {
250  //TEMPORARY: to avoid possible deadlocks from ROOT, we must
251  // take the lock ourselves
252  R__LOCKGUARD2(gInterpreterMutex);
253  try {
254  ptr = std::make_shared<SharedLibrary>(p);
255  } catch (cms::Exception& e) {
256  e.addContext("While attempting to load plugin " + iPlugin);
257  throw;
258  }
259  }
260  loadables_.emplace(p, ptr);
261  justLoaded_(*ptr);
262  return *ptr;
263  }
264  }
265  return *(itLoaded->second);
266  }
edm::signalslot::Signal< void(const std::filesystem::path &)> goingToLoad_
oneapi::tbb::concurrent_unordered_map< std::filesystem::path, std::shared_ptr< SharedLibrary >, PluginManagerPathHasher > loadables_
edm::signalslot::Signal< void(const std::string &, const std::string &)> askedToLoadCategoryWithPlugin_
edm::signalslot::Signal< void(const SharedLibrary &)> justLoaded_
std::recursive_mutex & pluginLoadMutex()
const std::filesystem::path & loadableFor(const std::string &iCategory, const std::string &iPlugin)
static std::string & loadingLibraryNamed_()

◆ loadableFor()

const std::filesystem::path & PluginManager::loadableFor ( const std::string &  iCategory,
const std::string &  iPlugin 
)

Definition at line 156 of file PluginManager.cc.

References loadableFor_().

Referenced by edmplugin::PluginFactoryBase::checkProperLoadable(), and load().

156  {
157  bool throwIfFail = true;
158  return loadableFor_(iCategory, iPlugin, throwIfFail);
159  }
const std::filesystem::path & loadableFor_(const std::string &iCategory, const std::string &iPlugin, bool &ioThrowIfFailElseSucceedStatus)

◆ loadableFor_()

const std::filesystem::path & PluginManager::loadableFor_ ( const std::string &  iCategory,
const std::string &  iPlugin,
bool &  ioThrowIfFailElseSucceedStatus 
)
private

Definition at line 161 of file PluginManager.cc.

References categoryToInfos_, Exception, mps_fire::i, EnsembleCalibrationLA_cfg::path, and isotrackApplyRegressor::range.

Referenced by loadableFor(), and tryToLoad().

163  {
164  const bool throwIfFail = ioThrowIfFailElseSucceedStatus;
165  ioThrowIfFailElseSucceedStatus = true;
166  CategoryToInfos::iterator itFound = categoryToInfos_.find(iCategory);
167  if (itFound == categoryToInfos_.end()) {
168  if (throwIfFail) {
169  throw cms::Exception("PluginNotFound") << "Unable to find plugin '" << iPlugin << "' because the category '"
170  << iCategory << "' has no known plugins";
171  } else {
172  ioThrowIfFailElseSucceedStatus = false;
173  static const std::filesystem::path s_path;
174  return s_path;
175  }
176  }
177 
178  PluginInfo i;
179  i.name_ = iPlugin;
180  typedef std::vector<PluginInfo>::iterator PIItr;
181  std::pair<PIItr, PIItr> range = std::equal_range(itFound->second.begin(), itFound->second.end(), i, PICompare());
182 
183  if (range.first == range.second) {
184  if (throwIfFail) {
185  throw cms::Exception("PluginNotFound") << "Unable to find plugin '" << iPlugin << "' in category '" << iCategory
186  << "'. Please check spelling of name.";
187  } else {
188  ioThrowIfFailElseSucceedStatus = false;
189  static const std::filesystem::path s_path;
190  return s_path;
191  }
192  }
193 
194  if (range.second - range.first > 1) {
195  //see if the come from the same directory
196  if (range.first->loadable_.parent_path() == (range.first + 1)->loadable_.parent_path()) {
197  //std::cout<<range.first->name_ <<" " <<(range.first+1)->name_<<std::endl;
198  throw cms::Exception("MultiplePlugins")
199  << "The plugin '" << iPlugin
200  << "' is found in multiple files \n"
201  " '"
202  << range.first->loadable_.filename() << "'\n '" << (range.first + 1)->loadable_.filename()
203  << "'\n"
204  "in directory '"
205  << range.first->loadable_.parent_path().string()
206  << "'.\n"
207  "The code must be changed so the plugin only appears in one plugin file. "
208  "You will need to remove the macro which registers the plugin so it only appears in"
209  " one of these files.\n"
210  " If none of these files register such a plugin, "
211  "then the problem originates in a library to which all these files link.\n"
212  "The plugin registration must be removed from that library since plugins are not allowed in regular "
213  "libraries.";
214  }
215  }
216 
217  return range.first->loadable_;
218  }
CategoryToInfos categoryToInfos_

◆ loadingFile()

static const std::string& edmplugin::PluginManager::loadingFile ( )
inlinestatic

file name of the shared object being loaded

Definition at line 89 of file PluginManager.h.

References loadingLibraryNamed_().

Referenced by edmplugin::PluginFactoryBase::newPlugin(), and edmplugin::PluginFactoryBase::registerPMaker().

89 { return loadingLibraryNamed_(); }
static std::string & loadingLibraryNamed_()

◆ loadingLibraryNamed_()

std::string & PluginManager::loadingLibraryNamed_ ( )
staticprivate

Definition at line 340 of file PluginManager.cc.

References CMS_THREAD_SAFE, staticallyLinkedLoadingFileName(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by load(), loadingFile(), PluginManager(), and tryToLoad().

340  {
341  //NOTE: pluginLoadMutex() indirectly guards this since this value
342  // is only accessible via the Sentry call which us guarded by the mutex
344  return s_name;
345  }
static const std::string & staticallyLinkedLoadingFileName()
if the value returned from loadingFile matches this string then the file is statically linked ...
#define CMS_THREAD_SAFE

◆ newFactory()

void PluginManager::newFactory ( const PluginFactoryBase )
private

Definition at line 146 of file PluginManager.cc.

Referenced by PluginManager().

146 {}

◆ operator=()

const PluginManager& edmplugin::PluginManager::operator= ( const PluginManager )
delete

◆ pluginLoadMutex()

std::recursive_mutex& edmplugin::PluginManager::pluginLoadMutex ( )
inlineprivate

Definition at line 111 of file PluginManager.h.

References pluginLoadMutex_.

Referenced by load(), and tryToLoad().

111 { return pluginLoadMutex_; }
std::recursive_mutex pluginLoadMutex_

◆ singleton()

PluginManager *& PluginManager::singleton ( )
staticprivate

Definition at line 347 of file PluginManager.cc.

References CMS_THREAD_SAFE.

Referenced by configure(), get(), and isAvailable().

347  {
348  CMS_THREAD_SAFE static PluginManager* s_singleton = nullptr;
349  return s_singleton;
350  }
#define CMS_THREAD_SAFE

◆ staticallyLinkedLoadingFileName()

const std::string & PluginManager::staticallyLinkedLoadingFileName ( )
static

if the value returned from loadingFile matches this string then the file is statically linked

Definition at line 335 of file PluginManager.cc.

References AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by edmplugin::PluginFactoryBase::checkProperLoadable(), and loadingLibraryNamed_().

335  {
336  static const std::string s_name("static");
337  return s_name;
338  }

◆ tryToLoad()

const SharedLibrary * PluginManager::tryToLoad ( const std::string &  iCategory,
const std::string &  iPlugin 
)

Definition at line 268 of file PluginManager.cc.

References askedToLoadCategoryWithPlugin_, MillePedeFileConverter_cfg::e, goingToLoad_, justLoaded_, loadableFor_(), loadables_, loadingLibraryNamed_(), AlCaHLTBitMon_ParallelJobs::p, EnsembleCalibrationLA_cfg::path, pluginLoadMutex(), and alignCSCRings::s.

Referenced by edmplugin::PluginFactoryBase::tryToFindPMaker(), and edmplugin::PluginCapabilities::tryToLoad().

268  {
269  askedToLoadCategoryWithPlugin_(iCategory, iPlugin);
270  bool ioThrowIfFailElseSucceedStatus = false;
271  const std::filesystem::path& p = loadableFor_(iCategory, iPlugin, ioThrowIfFailElseSucceedStatus);
272 
273  if (not ioThrowIfFailElseSucceedStatus) {
274  return nullptr;
275  }
276 
277  //have we already loaded this?
278  auto itLoaded = loadables_.find(p);
279  if (itLoaded == loadables_.end()) {
280  //Need to make sure we only have on SharedLibrary loading at a time
281  std::lock_guard<std::recursive_mutex> guard(pluginLoadMutex());
282  //Another thread may have gotten this while we were waiting on the mutex
283  itLoaded = loadables_.find(p);
284  if (itLoaded == loadables_.end()) {
285  //try to make one
286  goingToLoad_(p);
287  Sentry s(loadingLibraryNamed_(), p.string());
288  //std::filesystem::path native(p.string());
289  std::shared_ptr<SharedLibrary> ptr;
290  {
291  //TEMPORARY: to avoid possible deadlocks from ROOT, we must
292  // take the lock ourselves
293  R__LOCKGUARD(gInterpreterMutex);
294  try {
295  ptr = std::make_shared<SharedLibrary>(p);
296  } catch (cms::Exception& e) {
297  e.addContext("While attempting to load plugin " + iPlugin);
298  throw;
299  }
300  }
301  loadables_[p] = ptr;
302  justLoaded_(*ptr);
303  return ptr.get();
304  }
305  }
306  return (itLoaded->second).get();
307  }
edm::signalslot::Signal< void(const std::filesystem::path &)> goingToLoad_
oneapi::tbb::concurrent_unordered_map< std::filesystem::path, std::shared_ptr< SharedLibrary >, PluginManagerPathHasher > loadables_
const std::filesystem::path & loadableFor_(const std::string &iCategory, const std::string &iPlugin, bool &ioThrowIfFailElseSucceedStatus)
edm::signalslot::Signal< void(const std::string &, const std::string &)> askedToLoadCategoryWithPlugin_
edm::signalslot::Signal< void(const SharedLibrary &)> justLoaded_
std::recursive_mutex & pluginLoadMutex()
static std::string & loadingLibraryNamed_()

Friends And Related Function Documentation

◆ DummyFriend

friend class DummyFriend
friend

Definition at line 46 of file PluginManager.h.

Member Data Documentation

◆ askedToLoadCategoryWithPlugin_

edm::signalslot::Signal<void(const std::string&, const std::string&)> edmplugin::PluginManager::askedToLoadCategoryWithPlugin_

Definition at line 102 of file PluginManager.h.

Referenced by load(), PrintLoadingPlugins::PrintLoadingPlugins(), and tryToLoad().

◆ categoryToInfos_

CategoryToInfos edmplugin::PluginManager::categoryToInfos_
private

Definition at line 121 of file PluginManager.h.

Referenced by categoryToInfos(), loadableFor_(), and PluginManager().

◆ goingToLoad_

edm::signalslot::Signal<void(const std::filesystem::path&)> edmplugin::PluginManager::goingToLoad_

Definition at line 100 of file PluginManager.h.

Referenced by load(), PrintLoadingPlugins::PrintLoadingPlugins(), and tryToLoad().

◆ justLoaded_

edm::signalslot::Signal<void(const SharedLibrary&)> edmplugin::PluginManager::justLoaded_

Definition at line 101 of file PluginManager.h.

Referenced by load(), and tryToLoad().

◆ loadables_

oneapi::tbb::concurrent_unordered_map<std::filesystem::path, std::shared_ptr<SharedLibrary>, PluginManagerPathHasher> edmplugin::PluginManager::loadables_
private

Definition at line 119 of file PluginManager.h.

Referenced by load(), and tryToLoad().

◆ pluginLoadMutex_

std::recursive_mutex edmplugin::PluginManager::pluginLoadMutex_
private

Definition at line 122 of file PluginManager.h.

Referenced by pluginLoadMutex().

◆ searchPath_

SearchPath edmplugin::PluginManager::searchPath_
private

Definition at line 117 of file PluginManager.h.

Referenced by PluginManager().