Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-06-29 22:58:03

0001 #include "FWCore/Framework/interface/ModuleRegistryUtilities.h"
0002 #include "FWCore/Framework/interface/ModuleRegistry.h"
0003 #include "FWCore/Framework/interface/maker/ModuleSignalSentry.h"
0004 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0005 #include "FWCore/Utilities/interface/make_sentry.h"
0006 #include "FWCore/Utilities/interface/ExceptionCollector.h"
0007 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0008 #include "DataFormats/Provenance/interface/ProductResolverIndexHelper.h"
0009 
0010 namespace edm {
0011   void finishModulesInitialization(ModuleRegistry& iModuleRegistry,
0012                                    ProductRegistry const& iProductRegistry,
0013                                    eventsetup::ESRecordsToProductResolverIndices const& iESIndices,
0014                                    ProcessBlockHelperBase const& processBlockHelperBase,
0015                                    std::string const& processName) {
0016     auto const processBlockLookup = iProductRegistry.productLookup(InProcess);
0017     auto const runLookup = iProductRegistry.productLookup(InRun);
0018     auto const lumiLookup = iProductRegistry.productLookup(InLumi);
0019     auto const eventLookup = iProductRegistry.productLookup(InEvent);
0020 
0021     auto processBlockModuleToIndicies = processBlockLookup->indiciesForModulesInProcess(processName);
0022     auto runModuleToIndicies = runLookup->indiciesForModulesInProcess(processName);
0023     auto lumiModuleToIndicies = lumiLookup->indiciesForModulesInProcess(processName);
0024     auto eventModuleToIndicies = eventLookup->indiciesForModulesInProcess(processName);
0025 
0026     iModuleRegistry.forAllModuleHolders([&](auto& iHolder) {
0027       iHolder->updateLookup(InProcess, *processBlockLookup);
0028       iHolder->updateLookup(InRun, *runLookup);
0029       iHolder->updateLookup(InLumi, *lumiLookup);
0030       iHolder->updateLookup(InEvent, *eventLookup);
0031       iHolder->updateLookup(iESIndices);
0032       iHolder->resolvePutIndicies(InProcess, processBlockModuleToIndicies);
0033       iHolder->resolvePutIndicies(InRun, runModuleToIndicies);
0034       iHolder->resolvePutIndicies(InLumi, lumiModuleToIndicies);
0035       iHolder->resolvePutIndicies(InEvent, eventModuleToIndicies);
0036       iHolder->selectInputProcessBlocks(iProductRegistry, processBlockHelperBase);
0037     });
0038   }
0039 
0040   namespace {
0041     template <typename F>
0042     void captureExceptionAndContinue(std::exception_ptr& iExcept, ModuleCallingContext const& iContext, F&& iF) {
0043       try {
0044         convertException::wrap(std::forward<F&&>(iF));
0045       } catch (cms::Exception& newException) {
0046         if (not iExcept) {
0047           edm::exceptionContext(newException, iContext);
0048           iExcept = std::current_exception();
0049         }
0050       }
0051     }
0052 
0053     template <typename F>
0054     void captureExceptionsAndContinue(ExceptionCollector& iCollector, ModuleCallingContext const& iContext, F&& iF) {
0055       try {
0056         convertException::wrap(std::forward<F&&>(iF));
0057       } catch (cms::Exception& ex) {
0058         edm::exceptionContext(ex, iContext);
0059         iCollector.addException(ex);
0060       }
0061     }
0062     template <typename F>
0063     void captureExceptionsAndContinue(ExceptionCollector& iCollector,
0064                                       std::mutex& iMutex,
0065                                       ModuleCallingContext const& iContext,
0066                                       F&& iF) {
0067       try {
0068         convertException::wrap(std::forward<F&&>(iF));
0069       } catch (cms::Exception& ex) {
0070         edm::exceptionContext(ex, iContext);
0071         std::lock_guard guard(iMutex);
0072         iCollector.addException(ex);
0073       }
0074     }
0075 
0076     class ModuleBeginJobTraits {
0077     public:
0078       using Context = GlobalContext;
0079       static void preModuleSignal(ActivityRegistry* activityRegistry,
0080                                   GlobalContext const*,
0081                                   ModuleCallingContext const* moduleCallingContext) {
0082         activityRegistry->preModuleBeginJobSignal_(*moduleCallingContext->moduleDescription());
0083       }
0084       static void postModuleSignal(ActivityRegistry* activityRegistry,
0085                                    GlobalContext const*,
0086                                    ModuleCallingContext const* moduleCallingContext) {
0087         activityRegistry->postModuleBeginJobSignal_(*moduleCallingContext->moduleDescription());
0088       }
0089     };
0090 
0091     class ModuleEndJobTraits {
0092     public:
0093       using Context = GlobalContext;
0094       static void preModuleSignal(ActivityRegistry* activityRegistry,
0095                                   GlobalContext const*,
0096                                   ModuleCallingContext const* moduleCallingContext) {
0097         activityRegistry->preModuleEndJobSignal_(*moduleCallingContext->moduleDescription());
0098       }
0099       static void postModuleSignal(ActivityRegistry* activityRegistry,
0100                                    GlobalContext const*,
0101                                    ModuleCallingContext const* moduleCallingContext) {
0102         activityRegistry->postModuleEndJobSignal_(*moduleCallingContext->moduleDescription());
0103       }
0104     };
0105 
0106     class ModuleBeginStreamTraits {
0107     public:
0108       using Context = StreamContext;
0109       static void preModuleSignal(ActivityRegistry* activityRegistry,
0110                                   StreamContext const* streamContext,
0111                                   ModuleCallingContext const* moduleCallingContext) {
0112         activityRegistry->preModuleBeginStreamSignal_(*streamContext, *moduleCallingContext);
0113       }
0114       static void postModuleSignal(ActivityRegistry* activityRegistry,
0115                                    StreamContext const* streamContext,
0116                                    ModuleCallingContext const* moduleCallingContext) {
0117         activityRegistry->postModuleBeginStreamSignal_(*streamContext, *moduleCallingContext);
0118       }
0119     };
0120 
0121     class ModuleEndStreamTraits {
0122     public:
0123       using Context = StreamContext;
0124       static void preModuleSignal(ActivityRegistry* activityRegistry,
0125                                   StreamContext const* streamContext,
0126                                   ModuleCallingContext const* moduleCallingContext) {
0127         activityRegistry->preModuleEndStreamSignal_(*streamContext, *moduleCallingContext);
0128       }
0129       static void postModuleSignal(ActivityRegistry* activityRegistry,
0130                                    StreamContext const* streamContext,
0131                                    ModuleCallingContext const* moduleCallingContext) {
0132         activityRegistry->postModuleEndStreamSignal_(*streamContext, *moduleCallingContext);
0133       }
0134     };
0135 
0136   }  // namespace
0137   void runBeginJobForModules(GlobalContext const& iGlobalContext,
0138                              ModuleRegistry& iModuleRegistry,
0139                              edm::ActivityRegistry& iActivityRegistry,
0140                              std::vector<unsigned int>& beginJobFailedForModule) noexcept(false) {
0141     beginJobFailedForModule.clear();
0142 
0143     ParentContext pc(&iGlobalContext);
0144 
0145     std::exception_ptr exceptionPtr;
0146     iModuleRegistry.forAllModuleHolders([&](auto& holder) {
0147       ModuleCallingContext mcc(&holder->moduleDescription());
0148       //Also sets a thread local
0149       ModuleContextSentry mccSentry(&mcc, pc);
0150       captureExceptionAndContinue(exceptionPtr, mcc, [&]() {
0151         ModuleSignalSentry<ModuleBeginJobTraits> signalSentry(&iActivityRegistry, &iGlobalContext, &mcc);
0152         auto failedSentry = make_sentry(&beginJobFailedForModule,
0153                                         [&](auto* failed) { failed->push_back(holder->moduleDescription().id()); });
0154         signalSentry.preModuleSignal();
0155         holder->beginJob();
0156         signalSentry.postModuleSignal();
0157         failedSentry.release();
0158       });
0159     });
0160     if (exceptionPtr) {
0161       std::rethrow_exception(exceptionPtr);
0162     }
0163   }
0164 
0165   void runEndJobForModules(GlobalContext const& iGlobalContext,
0166                            ModuleRegistry& iModuleRegistry,
0167                            ActivityRegistry& iActivityRegistry,
0168                            ExceptionCollector& collector,
0169                            std::vector<unsigned int> const& beginJobFailedForModule) noexcept {
0170     ParentContext pc(&iGlobalContext);
0171     const bool beginJobFailed = not beginJobFailedForModule.empty();
0172     iModuleRegistry.forAllModuleHolders([&](auto& holder) {
0173       if (beginJobFailed and
0174           std::count(
0175               beginJobFailedForModule.begin(), beginJobFailedForModule.end(), holder->moduleDescription().id())) {
0176         // If beginJob was never called, we should not call endJob.
0177         return;
0178       }
0179       ModuleCallingContext mcc(&holder->moduleDescription());
0180       //Also sets a thread local
0181       ModuleContextSentry mccSentry(&mcc, pc);
0182       captureExceptionsAndContinue(collector, mcc, [&]() {
0183         ModuleSignalSentry<ModuleEndJobTraits> signalSentry(&iActivityRegistry, &iGlobalContext, &mcc);
0184         signalSentry.preModuleSignal();
0185         holder->endJob();
0186         signalSentry.postModuleSignal();
0187       });
0188     });
0189   }
0190 
0191   void runBeginStreamForModules(StreamContext const& iStreamContext,
0192                                 ModuleRegistry& iModuleRegistry,
0193                                 edm::ActivityRegistry& iActivityRegistry,
0194                                 std::vector<unsigned int>& beginStreamFailedForModule) noexcept(false) {
0195     beginStreamFailedForModule.clear();
0196 
0197     std::exception_ptr exceptionPtr;
0198     ParentContext pc(&iStreamContext);
0199     iModuleRegistry.forAllModuleHolders([&](auto& holder) {
0200       ModuleCallingContext mcc(&holder->moduleDescription());
0201       //Also sets a thread local
0202       ModuleContextSentry mccSentry(&mcc, pc);
0203       captureExceptionAndContinue(exceptionPtr, mcc, [&]() {
0204         auto failedSentry = make_sentry(&beginStreamFailedForModule,
0205                                         [&](auto* failed) { failed->push_back(holder->moduleDescription().id()); });
0206         ModuleSignalSentry<ModuleBeginStreamTraits> signalSentry(&iActivityRegistry, &iStreamContext, &mcc);
0207         signalSentry.preModuleSignal();
0208         holder->beginStream(iStreamContext.streamID());
0209         signalSentry.postModuleSignal();
0210         failedSentry.release();
0211       });
0212     });
0213     if (exceptionPtr) {
0214       std::rethrow_exception(exceptionPtr);
0215     }
0216   }
0217 
0218   void runEndStreamForModules(StreamContext const& iStreamContext,
0219                               ModuleRegistry& iModuleRegistry,
0220                               ActivityRegistry& iActivityRegistry,
0221                               ExceptionCollector& collector,
0222                               std::mutex& collectorMutex,
0223                               std::vector<unsigned int> const& beginStreamFailedForModule) noexcept {
0224     ParentContext pc(&iStreamContext);
0225     bool const beginStreamFailed = not beginStreamFailedForModule.empty();
0226     iModuleRegistry.forAllModuleHolders([&](auto& holder) {
0227       if (beginStreamFailed and
0228           std::count(
0229               beginStreamFailedForModule.begin(), beginStreamFailedForModule.end(), holder->moduleDescription().id())) {
0230         // If beginStream was never called, we should not call endStream.
0231         return;
0232       }
0233       ModuleCallingContext mcc(&holder->moduleDescription());
0234       //Also sets a thread local
0235       ModuleContextSentry mccSentry(&mcc, pc);
0236       captureExceptionsAndContinue(collector, collectorMutex, mcc, [&]() {
0237         ModuleSignalSentry<ModuleEndStreamTraits> signalSentry(&iActivityRegistry, &iStreamContext, &mcc);
0238         signalSentry.preModuleSignal();
0239         holder->endStream(iStreamContext.streamID());
0240         signalSentry.postModuleSignal();
0241       });
0242     });
0243   }
0244 
0245 }  // namespace edm