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 #include "boost/utility.hpp"
37 
40 
41 #include <sstream>
42 
43 namespace edm {
44  class EventPrincipal;
45  class EarlyDeleteHelper;
46 
47  class Worker : private boost::noncopyable {
48  public:
49  enum State { Ready, Pass, Fail, Exception };
50 
51  Worker(ModuleDescription const& iMD, WorkerParams const& iWP);
52  virtual ~Worker();
53 
54  template <typename T>
55  bool doWork(typename T::MyPrincipal&, EventSetup const& c,
56  CurrentProcessingContext const* cpc,
57  CPUTimer *const timer);
58  void beginJob() ;
59  void endJob();
64 
66  void postForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren) {implPostForkReacquireResources(iChildIndex, iNumberOfChildren);}
67 
68  void reset() { state_ = Ready; }
69 
72 
73  ModuleDescription const& description() const {return md_;}
74  ModuleDescription const* descPtr() const {return &md_; }
77  void setActivityRegistry(boost::shared_ptr<ActivityRegistry> areg);
78 
80 
81  std::pair<double, double> timeCpuReal() const {
82  return std::pair<double, double>(stopwatch_->cpuTime(), stopwatch_->realTime());
83  }
84 
85  void clearCounters() {
87  }
88 
89  void useStopwatch();
90 
91  int timesRun() const { return timesRun_; }
92  int timesVisited() const { return timesVisited_; }
93  int timesPassed() const { return timesPassed_; }
94  int timesFailed() const { return timesFailed_; }
95  int timesExcept() const { return timesExcept_; }
96  State state() const { return state_; }
97 
98  int timesPass() const { return timesPassed(); } // for backward compatibility only - to be removed soon
99 
100  protected:
101  virtual std::string workerType() const = 0;
102  virtual bool implDoBegin(EventPrincipal&, EventSetup const& c,
103  CurrentProcessingContext const* cpc) = 0;
104  virtual bool implDoEnd(EventPrincipal&, EventSetup const& c,
105  CurrentProcessingContext const* cpc) = 0;
106  virtual bool implDoBegin(RunPrincipal& rp, EventSetup const& c,
107  CurrentProcessingContext const* cpc) = 0;
108  virtual bool implDoEnd(RunPrincipal& rp, EventSetup const& c,
109  CurrentProcessingContext const* cpc) = 0;
110  virtual bool implDoBegin(LuminosityBlockPrincipal& lbp, EventSetup const& c,
111  CurrentProcessingContext const* cpc) = 0;
112  virtual bool implDoEnd(LuminosityBlockPrincipal& lbp, EventSetup const& c,
113  CurrentProcessingContext const* cpc) = 0;
114  virtual void implBeginJob() = 0;
115  virtual void implEndJob() = 0;
116 
117  private:
118  virtual void implRespondToOpenInputFile(FileBlock const& fb) = 0;
119  virtual void implRespondToCloseInputFile(FileBlock const& fb) = 0;
120  virtual void implRespondToOpenOutputFiles(FileBlock const& fb) = 0;
121  virtual void implRespondToCloseOutputFiles(FileBlock const& fb) = 0;
122 
123  virtual void implPreForkReleaseResources() = 0;
124  virtual void implPostForkReacquireResources(unsigned int iChildIndex,
125  unsigned int iNumberOfChildren) = 0;
127 
134 
136  ActionTable const* actions_; // memory assumed to be managed elsewhere
137  boost::shared_ptr<cms::Exception> cached_exception_; // if state is 'exception'
138 
139  boost::shared_ptr<ActivityRegistry> actReg_;
140 
142  };
143 
144  namespace {
145  template <typename T>
146  class ModuleSignalSentry {
147  public:
148  ModuleSignalSentry(ActivityRegistry *a, ModuleDescription& md) : a_(a), md_(&md) {
149  if(a_) T::preModuleSignal(a_, md_);
150  }
151  ~ModuleSignalSentry() {
152  if(a_) T::postModuleSignal(a_, md_);
153  }
154  private:
155  ActivityRegistry* a_;
156  ModuleDescription* md_;
157  };
158 
159  template <typename T>
160  void exceptionContext(typename T::MyPrincipal const& principal,
161  cms::Exception& ex,
162  CurrentProcessingContext const* cpc) {
163  std::ostringstream ost;
164  if (T::isEvent_) {
165  ost << "Calling event method";
166  }
167  else if (T::begin_ && T::branchType_ == InRun) {
168  ost << "Calling beginRun";
169  }
170  else if (T::begin_ && T::branchType_ == InLumi) {
171  ost << "Calling beginLuminosityBlock";
172  }
173  else if (!T::begin_ && T::branchType_ == InLumi) {
174  ost << "Calling endLuminosityBlock";
175  }
176  else if (!T::begin_ && T::branchType_ == InRun) {
177  ost << "Calling endRun";
178  }
179  else {
180  // It should be impossible to get here ...
181  ost << "Calling unknown function";
182  }
183  if (cpc && cpc->moduleDescription()) {
184  ost << " for module " << cpc->moduleDescription()->moduleName() << "/'" << cpc->moduleDescription()->moduleLabel() << "'";
185  }
186  ex.addContext(ost.str());
187  ost.str("");
188  ost << "Running path '";
189  if (cpc && cpc->pathName()) {
190  ost << *cpc->pathName() << "'";
191  }
192  else {
193  ost << "unknown'";
194  }
195  ex.addContext(ost.str());
196  ost.str("");
197  ost << "Processing ";
198  ost << principal.id();
199  ex.addContext(ost.str());
200  }
201  }
202 
203  template <typename T>
204  bool Worker::doWork(typename T::MyPrincipal& ep,
205  EventSetup const& es,
206  CurrentProcessingContext const* cpc,
207  CPUTimer* const iTimer) {
208 
209  // A RunStopwatch, but only if we are processing an event.
211  iTimer);
212 
213  if (T::isEvent_) {
214  ++timesVisited_;
215  }
216  bool rc = false;
217 
218  switch(state_) {
219  case Ready: break;
220  case Pass: return true;
221  case Fail: return false;
222  case Exception: {
223  cached_exception_->raise();
224  }
225  }
226 
227  if (T::isEvent_) ++timesRun_;
228 
229  try {
230  try {
231 
232  ModuleSignalSentry<T> cpp(actReg_.get(), md_);
233  if (T::begin_) {
234  rc = implDoBegin(ep, es, cpc);
235  } else {
236  rc = implDoEnd(ep, es, cpc);
237  }
238 
239  if (rc) {
240  state_ = Pass;
241  if (T::isEvent_) ++timesPassed_;
242  } else {
243  state_ = Fail;
244  if (T::isEvent_) ++timesFailed_;
245  }
246  }
247  catch (cms::Exception& e) { throw; }
248  catch(std::bad_alloc& bda) { convertException::badAllocToEDM(); }
249  catch (std::exception& e) { convertException::stdToEDM(e); }
250  catch(std::string& s) { convertException::stringToEDM(s); }
251  catch(char const* c) { convertException::charPtrToEDM(c); }
252  catch (...) { convertException::unknownToEDM(); }
253  }
254  catch(cms::Exception& ex) {
255 
256  // NOTE: the warning printed as a result of ignoring or failing
257  // a module will only be printed during the full true processing
258  // pass of this module
259 
260  // Get the action corresponding to this exception. However, if processing
261  // something other than an event (e.g. run, lumi) always rethrow.
263 
264  // If we are processing an endpath and the module was scheduled, treat SkipEvent or FailPath
265  // as IgnoreCompletely, so any subsequent OutputModules are still run.
266  // For unscheduled modules only treat FailPath as IgnoreCompletely but still allow SkipEvent to throw
267  if (cpc && cpc->isEndPath()) {
268  if ((action == actions::SkipEvent && !cpc->isUnscheduled()) ||
269  action == actions::FailPath) action = actions::IgnoreCompletely;
270  }
271  switch(action) {
273  rc = true;
274  ++timesPassed_;
275  state_ = Pass;
276  exceptionContext<T>(ep, ex, cpc);
277  edm::printCmsExceptionWarning("IgnoreCompletely", ex);
278  break;
279  default:
280  if (T::isEvent_) ++timesExcept_;
281  state_ = Exception;
282  cached_exception_.reset(ex.clone());
283  cached_exception_->raise();
284  }
285  }
286  return rc;
287  }
288 }
289 #endif
void pathFinished(EventPrincipal &)
Definition: Worker.cc:120
ModuleDescription const & description() const
Definition: Worker.h:73
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:139
int timesPassed() const
Definition: Worker.h:93
void setActivityRegistry(boost::shared_ptr< ActivityRegistry > areg)
Definition: Worker.cc:64
virtual void implRespondToCloseInputFile(FileBlock const &fb)=0
State state() const
Definition: Worker.h:96
void respondToOpenOutputFiles(FileBlock const &fb)
Definition: Worker.h:62
void clearCounters()
Definition: Worker.h:85
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:81
EarlyDeleteHelper * earlyDeleteHelper_
Definition: Worker.h:141
bool isUnscheduled() const
Returns true if the module is being called via unscheduled execution.
int timesExcept() const
Definition: Worker.h:95
int timesExcept_
Definition: Worker.h:132
virtual bool implDoBegin(EventPrincipal &, EventSetup const &c, CurrentProcessingContext const *cpc)=0
boost::shared_ptr< CPUTimer > StopwatchPointer
Definition: RunStopwatch.h:23
void reset()
Definition: Worker.h:68
int timesVisited() const
Definition: Worker.h:92
virtual void implRespondToCloseOutputFiles(FileBlock const &fb)=0
void stdToEDM(std::exception const &e)
int timesPassed_
Definition: Worker.h:130
ModuleDescription const * descPtr() const
Definition: Worker.h:74
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:91
virtual void implEndJob()=0
void postForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren)
Definition: Worker.h:66
void postDoEvent(EventPrincipal &)
Definition: Worker.cc:125
void respondToCloseOutputFiles(FileBlock const &fb)
Definition: Worker.h:63
void charPtrToEDM(char const *c)
void stringToEDM(std::string &s)
boost::shared_ptr< cms::Exception > cached_exception_
Definition: Worker.h:137
virtual void implBeginJob()=0
void respondToOpenInputFile(FileBlock const &fb)
Definition: Worker.h:60
int timesRun_
Definition: Worker.h:128
int timesPass() const
Definition: Worker.h:98
void addContext(std::string const &context)
Definition: Exception.cc:227
ActionTable const * actions_
Definition: Worker.h:136
int timesFailed() const
Definition: Worker.h:94
void respondToCloseInputFile(FileBlock const &fb)
Definition: Worker.h:61
Worker(ModuleDescription const &iMD, WorkerParams const &iWP)
Definition: Worker.cc:44
void preForkReleaseResources()
Definition: Worker.h:65
ModuleDescription md_
Definition: Worker.h:135
double a
Definition: hdecay.h:121
void beginJob()
Definition: Worker.cc:72
RunStopwatch::StopwatchPointer stopwatch_
Definition: Worker.h:126
actions::ActionCodes find(const std::string &category) const
Definition: Actions.cc:93
int timesFailed_
Definition: Worker.h:131
bool doWork(typename T::MyPrincipal &, EventSetup const &c, CurrentProcessingContext const *cpc, CPUTimer *const timer)
Definition: Worker.h:204
virtual void implRespondToOpenInputFile(FileBlock const &fb)=0
State state_
Definition: Worker.h:133
int timesVisited_
Definition: Worker.h:129