CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Worker.h
Go to the documentation of this file.
1 #ifndef FWCore_Framework_Worker_h
2 #define FWCore_Framework_Worker_h
3 
4 /*----------------------------------------------------------------------
5 
6 Worker: this is a basic scheduling unit - an abstract base class to
7 something that is really a producer or filter.
8 
9 A worker will not actually call through to the module unless it is
10 in a Ready state. After a module is actually run, the state will not
11 be Ready. The Ready state can only be reestablished by doing a reset().
12 
13 Pre/post module signals are posted only in the Ready state.
14 
15 Execution statistics are kept here.
16 
17 If a module has thrown an exception during execution, that exception
18 will be rethrown if the worker is entered again and the state is not Ready.
19 In other words, execution results (status) are cached and reused until
20 the worker is reset().
21 
22 ----------------------------------------------------------------------*/
23 
34 
35 #include "boost/shared_ptr.hpp"
36 
39 
40 #include <sstream>
41 
42 namespace edm {
43  class EventPrincipal;
44  class EarlyDeleteHelper;
45  class ProductHolderIndexHelper;
46 
47  class Worker {
48  public:
49  enum State { Ready, Pass, Fail, Exception };
51 
52  Worker(ModuleDescription const& iMD, WorkerParams const& iWP);
53  virtual ~Worker();
54 
55  Worker(Worker const&) = delete; // Disallow copying and moving
56  Worker& operator=(Worker const&) = delete; // Disallow copying and moving
57 
58  template <typename T>
59  bool doWork(typename T::MyPrincipal&, EventSetup const& c,
60  CurrentProcessingContext const* cpc,
61  CPUTimer *const timer);
62  void beginJob() ;
63  void endJob();
68 
70  void postForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren) {implPostForkReacquireResources(iChildIndex, iNumberOfChildren);}
71 
72  void reset() { state_ = Ready; }
73 
76 
77  ModuleDescription const& description() const {return md_;}
78  ModuleDescription const* descPtr() const {return &md_; }
81  void setActivityRegistry(boost::shared_ptr<ActivityRegistry> areg);
82 
84 
85  //Used to make EDGetToken work
86  virtual void updateLookup(BranchType iBranchType,
87  ProductHolderIndexHelper const&) = 0;
88 
89 
90  virtual Types moduleType() const =0;
91 
92  std::pair<double, double> timeCpuReal() const {
93  return std::pair<double, double>(stopwatch_->cpuTime(), stopwatch_->realTime());
94  }
95 
96  void clearCounters() {
98  }
99 
100  void useStopwatch();
101 
102  int timesRun() const { return timesRun_; }
103  int timesVisited() const { return timesVisited_; }
104  int timesPassed() const { return timesPassed_; }
105  int timesFailed() const { return timesFailed_; }
106  int timesExcept() const { return timesExcept_; }
107  State state() const { return state_; }
108 
109  int timesPass() const { return timesPassed(); } // for backward compatibility only - to be removed soon
110 
111  protected:
112  virtual std::string workerType() const = 0;
113  virtual bool implDoBegin(EventPrincipal&, EventSetup const& c,
114  CurrentProcessingContext const* cpc) = 0;
115  virtual bool implDoEnd(EventPrincipal&, EventSetup const& c,
116  CurrentProcessingContext const* cpc) = 0;
117  virtual bool implDoBegin(RunPrincipal& rp, EventSetup const& c,
118  CurrentProcessingContext const* cpc) = 0;
119  virtual bool implDoEnd(RunPrincipal& rp, EventSetup const& c,
120  CurrentProcessingContext const* cpc) = 0;
121  virtual bool implDoBegin(LuminosityBlockPrincipal& lbp, EventSetup const& c,
122  CurrentProcessingContext const* cpc) = 0;
123  virtual bool implDoEnd(LuminosityBlockPrincipal& lbp, EventSetup const& c,
124  CurrentProcessingContext const* cpc) = 0;
125  virtual void implBeginJob() = 0;
126  virtual void implEndJob() = 0;
127 
128  private:
129  virtual void implRespondToOpenInputFile(FileBlock const& fb) = 0;
130  virtual void implRespondToCloseInputFile(FileBlock const& fb) = 0;
131  virtual void implRespondToOpenOutputFiles(FileBlock const& fb) = 0;
132  virtual void implRespondToCloseOutputFiles(FileBlock const& fb) = 0;
133 
134  virtual void implPreForkReleaseResources() = 0;
135  virtual void implPostForkReacquireResources(unsigned int iChildIndex,
136  unsigned int iNumberOfChildren) = 0;
138 
145 
147  ActionTable const* actions_; // memory assumed to be managed elsewhere
148  boost::shared_ptr<cms::Exception> cached_exception_; // if state is 'exception'
149 
150  boost::shared_ptr<ActivityRegistry> actReg_;
151 
153  };
154 
155  namespace {
156  template <typename T>
157  class ModuleSignalSentry {
158  public:
159  ModuleSignalSentry(ActivityRegistry *a, ModuleDescription& md) : a_(a), md_(&md) {
160  if(a_) T::preModuleSignal(a_, md_);
161  }
162  ~ModuleSignalSentry() {
163  if(a_) T::postModuleSignal(a_, md_);
164  }
165  private:
166  ActivityRegistry* a_;
167  ModuleDescription* md_;
168  };
169 
170  template <typename T>
171  void exceptionContext(typename T::MyPrincipal const& principal,
172  cms::Exception& ex,
173  CurrentProcessingContext const* cpc) {
174  std::ostringstream ost;
175  if (T::isEvent_) {
176  ost << "Calling event method";
177  }
178  else if (T::begin_ && T::branchType_ == InRun) {
179  ost << "Calling beginRun";
180  }
181  else if (T::begin_ && T::branchType_ == InLumi) {
182  ost << "Calling beginLuminosityBlock";
183  }
184  else if (!T::begin_ && T::branchType_ == InLumi) {
185  ost << "Calling endLuminosityBlock";
186  }
187  else if (!T::begin_ && T::branchType_ == InRun) {
188  ost << "Calling endRun";
189  }
190  else {
191  // It should be impossible to get here ...
192  ost << "Calling unknown function";
193  }
194  if (cpc && cpc->moduleDescription()) {
195  ost << " for module " << cpc->moduleDescription()->moduleName() << "/'" << cpc->moduleDescription()->moduleLabel() << "'";
196  }
197  ex.addContext(ost.str());
198  ost.str("");
199  ost << "Running path '";
200  if (cpc && cpc->pathName()) {
201  ost << *cpc->pathName() << "'";
202  }
203  else {
204  ost << "unknown'";
205  }
206  ex.addContext(ost.str());
207  ost.str("");
208  ost << "Processing ";
209  ost << principal.id();
210  ex.addContext(ost.str());
211  }
212  }
213 
214  template <typename T>
215  bool Worker::doWork(typename T::MyPrincipal& ep,
216  EventSetup const& es,
217  CurrentProcessingContext const* cpc,
218  CPUTimer* const iTimer) {
219 
220  // A RunStopwatch, but only if we are processing an event.
222  iTimer);
223 
224  if (T::isEvent_) {
225  ++timesVisited_;
226  }
227  bool rc = false;
228 
229  switch(state_) {
230  case Ready: break;
231  case Pass: return true;
232  case Fail: return false;
233  case Exception: {
234  cached_exception_->raise();
235  }
236  }
237 
238  if (T::isEvent_) ++timesRun_;
239 
240  try {
241  try {
242 
243  ModuleSignalSentry<T> cpp(actReg_.get(), md_);
244  if (T::begin_) {
245  rc = implDoBegin(ep, es, cpc);
246  } else {
247  rc = implDoEnd(ep, es, cpc);
248  }
249 
250  if (rc) {
251  state_ = Pass;
252  if (T::isEvent_) ++timesPassed_;
253  } else {
254  state_ = Fail;
255  if (T::isEvent_) ++timesFailed_;
256  }
257  }
258  catch (cms::Exception& e) { throw; }
259  catch(std::bad_alloc& bda) { convertException::badAllocToEDM(); }
260  catch (std::exception& e) { convertException::stdToEDM(e); }
262  catch(char const* c) { convertException::charPtrToEDM(c); }
263  catch (...) { convertException::unknownToEDM(); }
264  }
265  catch(cms::Exception& ex) {
266 
267  // NOTE: the warning printed as a result of ignoring or failing
268  // a module will only be printed during the full true processing
269  // pass of this module
270 
271  // Get the action corresponding to this exception. However, if processing
272  // something other than an event (e.g. run, lumi) always rethrow.
274 
275  // If we are processing an endpath and the module was scheduled, treat SkipEvent or FailPath
276  // as IgnoreCompletely, so any subsequent OutputModules are still run.
277  // For unscheduled modules only treat FailPath as IgnoreCompletely but still allow SkipEvent to throw
278  if (cpc && cpc->isEndPath()) {
279  if ((action == actions::SkipEvent && !cpc->isUnscheduled()) ||
280  action == actions::FailPath) action = actions::IgnoreCompletely;
281  }
282  switch(action) {
284  rc = true;
285  ++timesPassed_;
286  state_ = Pass;
287  exceptionContext<T>(ep, ex, cpc);
288  edm::printCmsExceptionWarning("IgnoreCompletely", ex);
289  break;
290  default:
291  if (T::isEvent_) ++timesExcept_;
292  state_ = Exception;
293  cached_exception_.reset(ex.clone());
294  cached_exception_->raise();
295  }
296  }
297  return rc;
298  }
299 }
300 #endif
void pathFinished(EventPrincipal &)
Definition: Worker.cc:120
ModuleDescription const & description() const
Definition: Worker.h:77
void setEarlyDeleteHelper(EarlyDeleteHelper *iHelper)
Definition: Worker.cc:68
bool isEndPath() const
Return true if the path is an end path, and false otherwise.
virtual void implPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren)=0
virtual ~Worker()
Definition: Worker.cc:61
void endJob()
Definition: Worker.cc:94
virtual void implRespondToOpenOutputFiles(FileBlock const &fb)=0
boost::shared_ptr< ActivityRegistry > actReg_
Definition: Worker.h:150
int timesPassed() const
Definition: Worker.h:104
void setActivityRegistry(boost::shared_ptr< ActivityRegistry > areg)
Definition: Worker.cc:64
virtual void implRespondToCloseInputFile(FileBlock const &fb)=0
State state() const
Definition: Worker.h:107
void respondToOpenOutputFiles(FileBlock const &fb)
Definition: Worker.h:66
void clearCounters()
Definition: Worker.h:96
std::string const & moduleName() const
std::string const & category() const
Definition: Exception.cc:183
virtual void implPreForkReleaseResources()=0
virtual std::string workerType() const =0
virtual bool implDoEnd(EventPrincipal &, EventSetup const &c, CurrentProcessingContext const *cpc)=0
std::pair< double, double > timeCpuReal() const
Definition: Worker.h:92
EarlyDeleteHelper * earlyDeleteHelper_
Definition: Worker.h:152
BranchType
Definition: BranchType.h:11
bool isUnscheduled() const
Returns true if the module is being called via unscheduled execution.
int timesExcept() const
Definition: Worker.h:106
int timesExcept_
Definition: Worker.h:143
virtual bool implDoBegin(EventPrincipal &, EventSetup const &c, CurrentProcessingContext const *cpc)=0
virtual void updateLookup(BranchType iBranchType, ProductHolderIndexHelper const &)=0
Worker & operator=(Worker const &)=delete
boost::shared_ptr< CPUTimer > StopwatchPointer
Definition: RunStopwatch.h:23
void reset()
Definition: Worker.h:72
int timesVisited() const
Definition: Worker.h:103
virtual void implRespondToCloseOutputFiles(FileBlock const &fb)=0
void stdToEDM(std::exception const &e)
int timesPassed_
Definition: Worker.h:141
ModuleDescription const * descPtr() const
Definition: Worker.h:78
void useStopwatch()
Definition: Worker.cc:116
void printCmsExceptionWarning(char const *behavior, cms::Exception const &e, edm::JobReport *jobRep=0, int rc=-1)
int timesRun() const
Definition: Worker.h:102
virtual void implEndJob()=0
void postForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren)
Definition: Worker.h:70
virtual Types moduleType() const =0
void postDoEvent(EventPrincipal &)
Definition: Worker.cc:125
void respondToCloseOutputFiles(FileBlock const &fb)
Definition: Worker.h:67
void charPtrToEDM(char const *c)
void stringToEDM(std::string &s)
boost::shared_ptr< cms::Exception > cached_exception_
Definition: Worker.h:148
virtual void implBeginJob()=0
void respondToOpenInputFile(FileBlock const &fb)
Definition: Worker.h:64
int timesRun_
Definition: Worker.h:139
int timesPass() const
Definition: Worker.h:109
void addContext(std::string const &context)
Definition: Exception.cc:227
ActionTable const * actions_
Definition: Worker.h:147
int timesFailed() const
Definition: Worker.h:105
void respondToCloseInputFile(FileBlock const &fb)
Definition: Worker.h:65
Worker(ModuleDescription const &iMD, WorkerParams const &iWP)
Definition: Worker.cc:44
void preForkReleaseResources()
Definition: Worker.h:69
ModuleDescription md_
Definition: Worker.h:146
double a
Definition: hdecay.h:121
void beginJob()
Definition: Worker.cc:72
RunStopwatch::StopwatchPointer stopwatch_
Definition: Worker.h:137
actions::ActionCodes find(const std::string &category) const
Definition: Actions.cc:93
int timesFailed_
Definition: Worker.h:142
bool doWork(typename T::MyPrincipal &, EventSetup const &c, CurrentProcessingContext const *cpc, CPUTimer *const timer)
Definition: Worker.h:215
virtual void implRespondToOpenInputFile(FileBlock const &fb)=0
State state_
Definition: Worker.h:144
int timesVisited_
Definition: Worker.h:140