CMS 3D CMS Logo

Timing.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Services
4 // Class : Timing
5 //
6 // Implementation:
7 //
8 // Original Author: Jim Kowalkowski
9 //
10 
12 
26 
27 #include <iostream>
28 #include <sstream>
29 #include <sys/resource.h>
30 #include <sys/time.h>
31 #include <atomic>
32 
33 namespace edm {
34 
35  namespace service {
36  class Timing : public TimingServiceBase {
37  public:
39  ~Timing() override;
40 
41  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
42 
43  void addToCPUTime(StreamID id, double iTime) override;
44  double getTotalCPU() const override;
45 
46  private:
47 
48  void postBeginJob();
49  void postEndJob();
50 
51  void preEvent(StreamContext const&);
52  void postEvent(StreamContext const&);
53 
55 
58 
59  void preSourceLumi();
60  void postSourceLumi();
61 
62  void preSourceRun();
63  void postSourceRun();
64 
65  void preOpenFile(std::string const&, bool);
66  void postOpenFile(std::string const&, bool);
67 
68  void preModule(ModuleDescription const& md);
69  void postModule(ModuleDescription const& md);
70 
73 
76 
77  double postCommon() const;
78 
79  double curr_job_time_; // seconds
80  double curr_job_cpu_; // seconds
81  //use last run time for determining end of processing
82  std::atomic<double> last_run_time_;
83  std::atomic<double> last_run_cpu_;
84  std::vector<double> curr_events_time_; // seconds
87  double threshold_;
88  //
89  // Min Max and total event times for each Stream.
90  // Used for summary at end of job
91  std::vector<double> max_events_time_; // seconds
92  std::vector<double> min_events_time_; // seconds
93  std::vector<double> sum_events_time_;
94  std::atomic<unsigned long> total_event_count_;
95  unsigned int nStreams_;
96  unsigned int nThreads_;
97  };
98  }
99 }
100 
101 namespace edm {
102  namespace service {
103 
104  static std::string d2str(double d) {
105  std::stringstream t;
106  t << d;
107  return t.str();
108  }
109 
110  static std::string ui2str(unsigned int i) {
111  std::stringstream t;
112  t << i;
113  return t.str();
114  }
115 
116 
117  static double getTime() {
118  struct timeval t;
119  if(gettimeofday(&t, nullptr) < 0)
120  throw cms::Exception("SysCallFailed", "Failed call to gettimeofday");
121  return static_cast<double>(t.tv_sec) + (static_cast<double>(t.tv_usec) * 1E-6);
122  }
123 
124  static double getCPU() {
125  struct rusage usage;
126  getrusage(RUSAGE_SELF, &usage);
127 
128  double totalCPUTime = 0.0;
129  // User code
130  totalCPUTime = (double)usage.ru_utime.tv_sec + (double(usage.ru_utime.tv_usec) * 1E-6);
131  // System functions
132  totalCPUTime += (double)usage.ru_stime.tv_sec + (double(usage.ru_stime.tv_usec) * 1E-6);
133 
134  // Additionally, add in CPU usage from our child processes.
135  getrusage(RUSAGE_CHILDREN, &usage);
136  totalCPUTime += (double)usage.ru_utime.tv_sec + (double(usage.ru_utime.tv_usec) * 1E-6);
137  totalCPUTime += (double)usage.ru_stime.tv_sec + (double(usage.ru_stime.tv_usec) * 1E-6);
138 
139  return totalCPUTime;
140  }
141 
142  //NOTE: We use a per thread stack for module times since unscheduled
143  // exectuion or tbb task spawning can cause a module to run on the
144  // same thread as an already running module
145  static
146  std::vector<double>& moduleTimeStack() {
147  static thread_local std::vector<double> s_stack;
148  return s_stack;
149  }
150 
151  static
152  double popStack() {
153  auto& modStack = moduleTimeStack();
154  assert(!modStack.empty());
155  double curr_module_time = modStack.back();
156  modStack.pop_back();
157  double t = getTime() - curr_module_time;
158  return t;
159  }
160 
161  static
162  void pushStack() {
163  auto& modStack = moduleTimeStack();
164  modStack.push_back(getTime());
165  }
166 
167  Timing::Timing(ParameterSet const& iPS, ActivityRegistry& iRegistry) :
168  curr_job_time_(0.),
169  curr_job_cpu_(0.),
170  last_run_time_(0.0),
171  last_run_cpu_(0.0),
173  summary_only_(iPS.getUntrackedParameter<bool>("summaryOnly")),
174  report_summary_(iPS.getUntrackedParameter<bool>("useJobReport")),
175  threshold_(iPS.getUntrackedParameter<double>("excessiveTimeThreshold")),
178  total_event_count_(0) {
179  iRegistry.watchPostBeginJob(this, &Timing::postBeginJob);
180  iRegistry.watchPostEndJob(this, &Timing::postEndJob);
181 
182  iRegistry.watchPreEvent(this, &Timing::preEvent);
183  iRegistry.watchPostEvent(this, &Timing::postEvent);
184 
185  bool checkThreshold = true;
186  if (threshold_ <= 0.0) {
187  //we need to ignore the threshold check
189  checkThreshold = false;
190  }
191 
192  if( (not summary_only_) || (checkThreshold) ) {
195  }
196  if(checkThreshold) {
199 
200  iRegistry.watchPreSourceLumi(this, &Timing::preSourceLumi);
202 
203  iRegistry.watchPreSourceRun(this, &Timing::preSourceRun);
204  iRegistry.watchPostSourceRun(this, &Timing::postSourceRun);
205 
206  iRegistry.watchPreOpenFile(this, &Timing::preOpenFile);
207  iRegistry.watchPostOpenFile(this, &Timing::postOpenFile);
208 
211 
214 
215  iRegistry.watchPreModuleBeginJob(this, &Timing::preModule);
217 
218  iRegistry.watchPreModuleEndJob(this, &Timing::preModule);
219  iRegistry.watchPostModuleEndJob(this, &Timing::postModule);
220 
225 
230 
235 
240 
243  }
244 
245  iRegistry.preallocateSignal_.connect([this](service::SystemBounds const& iBounds){
246  nStreams_ = iBounds.maxNumberOfStreams();
247  nThreads_ = iBounds.maxNumberOfThreads();
248  curr_events_time_.resize(nStreams_,0.);
249  sum_events_time_.resize(nStreams_,0.);
250  max_events_time_.resize(nStreams_,0.);
251  min_events_time_.resize(nStreams_,1.E6);
252 
253  });
254 
255  iRegistry.postGlobalEndRunSignal_.connect([this](edm::GlobalContext const&) {
257  last_run_cpu_ = getCPU();
258  });
259  }
260 
262  }
263 
264  void Timing::addToCPUTime(StreamID id, double iTime) {
265  //For accounting purposes we effectively can say we started earlier
266  curr_job_cpu_ -= iTime;
267  }
268 
269  double Timing::getTotalCPU() const {
270  return getCPU();
271  }
272 
275  desc.addUntracked<bool>("summaryOnly", false)->setComment(
276  "If 'true' do not report timing for each event");
277  desc.addUntracked<bool>("useJobReport", true)->setComment(
278  "If 'true' write summary information to JobReport");
279  desc.addUntracked<double>("excessiveTimeThreshold", 0.)->setComment(
280  "Amount of time in seconds before reporting a module or source has taken excessive time. A value of 0.0 turns off this reporting.");
281  descriptions.add("Timing", desc);
282  descriptions.setComment(
283  "This service reports the time it takes to run each module in a job.");
284  }
285 
288  curr_job_cpu_ = getCPU();
289 
290  if(not summary_only_) {
291  LogImportant("TimeReport")
292  << "TimeReport> Report activated" << "\n"
293  << "TimeReport> Report columns headings for events: "
294  << "eventnum runnum timetaken\n"
295  << "TimeReport> Report columns headings for modules: "
296  << "eventnum runnum modulelabel modulename timetakeni\n"
297  << "TimeReport> JobTime=" << curr_job_time_ << " JobCPU=" << curr_job_cpu_ << "\n";
298  }
299  }
300 
302  const double job_end_time =getTime();
303  const double job_end_cpu =getCPU();
304  double total_job_time = job_end_time - jobStartTime();
305 
306  double total_job_cpu = job_end_cpu;
307 
308  const double total_initialization_time = curr_job_time_ - jobStartTime();
309  const double total_initialization_cpu = curr_job_cpu_;
310 
311  if( 0.0 == jobStartTime()) {
312  //did not capture beginning time
313  total_job_time =job_end_time - curr_job_time_;
314  total_job_cpu =job_end_cpu - curr_job_cpu_;
315  }
316 
317  double min_event_time = *(std::min_element(min_events_time_.begin(),
318  min_events_time_.end()));
319  double max_event_time = *(std::max_element(max_events_time_.begin(),
320  max_events_time_.end()));
321 
322  auto total_loop_time = last_run_time_ - curr_job_time_;
323  auto total_loop_cpu = last_run_cpu_ - curr_job_cpu_;
324 
325  if(last_run_time_ == 0.0) {
326  total_loop_time = 0.0;
327  total_loop_cpu = 0.0;
328  }
329 
330  double sum_all_events_time = 0;
331  for(auto t : sum_events_time_) { sum_all_events_time += t; }
332 
333  double average_event_time = 0.0;
334  if(total_event_count_ != 0) {
335  average_event_time = sum_all_events_time / total_event_count_;
336  }
337 
338  double event_throughput = 0.0;
339  if(total_loop_time != 0.0) {
340  event_throughput = total_event_count_ / total_loop_time;
341  }
342 
343  LogImportant("TimeReport")
344  << "TimeReport> Time report complete in "
345  << total_job_time << " seconds"
346  << "\n"
347  << " Time Summary: \n"
348  << " - Min event: " << min_event_time << "\n"
349  << " - Max event: " << max_event_time << "\n"
350  << " - Avg event: " << average_event_time << "\n"
351  << " - Total loop: " << total_loop_time <<"\n"
352  << " - Total init: " << total_initialization_time <<"\n"
353  << " - Total job: " << total_job_time << "\n"
354  << " Event Throughput: "<< event_throughput <<" ev/s\n"
355  << " CPU Summary: \n"
356  << " - Total loop: " << total_loop_cpu << "\n"
357  << " - Total init: " << total_initialization_cpu <<"\n"
358  << " - Total job: " << total_job_cpu << "\n";
359 
360  if(report_summary_) {
361  Service<JobReport> reportSvc;
362  std::map<std::string, std::string> reportData;
363 
364  reportData.insert(std::make_pair("MinEventTime", d2str(min_event_time)));
365  reportData.insert(std::make_pair("MaxEventTime", d2str(max_event_time)));
366  reportData.insert(std::make_pair("AvgEventTime", d2str(average_event_time)));
367  reportData.insert(std::make_pair("EventThroughput", d2str(event_throughput)));
368  reportData.insert(std::make_pair("TotalJobTime", d2str(total_job_time)));
369  reportData.insert(std::make_pair("TotalJobCPU", d2str(total_job_cpu)));
370  reportData.insert(std::make_pair("TotalLoopTime", d2str(total_loop_time)));
371  reportData.insert(std::make_pair("TotalLoopCPU", d2str(total_loop_cpu)));
372  reportData.insert(std::make_pair("TotalInitTime", d2str(total_initialization_time)));
373  reportData.insert(std::make_pair("TotalInitCPU", d2str(total_initialization_cpu)));
374  reportData.insert(std::make_pair("NumberOfStreams",ui2str(nStreams_)));
375  reportData.insert(std::make_pair("NumberOfThreads",ui2str(nThreads_)));
376  reportSvc->reportPerformanceSummary("Timing", reportData);
377  }
378  }
379 
380  void Timing::preEvent(StreamContext const& iStream) {
381  auto index = iStream.streamID().value();
383  }
384 
385  void Timing::postEvent(StreamContext const& iStream) {
386  auto index = iStream.streamID().value();
387 
388  double curr_event_time = getTime() - curr_events_time_[index];
389  sum_events_time_[index] +=curr_event_time;
390 
391  if(not summary_only_) {
392  auto const & eventID = iStream.eventID();
393  LogPrint("TimeEvent")
394  << "TimeEvent> "
395  << eventID.event() << " "
396  << eventID.run() << " "
397  << curr_event_time ;
398  }
399  if(curr_event_time > max_events_time_[index]) max_events_time_[index] = curr_event_time;
400  if(curr_event_time < min_events_time_[index]) min_events_time_[index] = curr_event_time;
402  }
403 
404  void Timing::postModuleEvent(StreamContext const& iStream, ModuleCallingContext const& iModule) {
405  auto const & eventID = iStream.eventID();
406  auto const & desc = *(iModule.moduleDescription());
407  double t = postCommon();
408  if ( not summary_only_) {
409  LogPrint("TimeModule") << "TimeModule> "
410  << eventID.event() << " "
411  << eventID.run() << " "
412  << desc.moduleLabel() << " "
413  << desc.moduleName() << " "
414  << t;
415  }
416  }
417 
419  pushStack();
420  }
421 
423  postCommon();
424  }
425 
427  pushStack();
428  }
429 
431  postCommon();
432  }
433 
435  pushStack();
436  }
437 
439  postCommon();
440  }
441 
442  void Timing::preOpenFile(std::string const& lfn, bool b) {
443  pushStack();
444  }
445 
446  void Timing::postOpenFile(std::string const& lfn, bool b) {
447  postCommon();
448  }
449 
450  void
452  pushStack();
453  }
454 
455  void
457  postCommon();
458  }
459 
460  void
462  pushStack();
463  }
464 
465  void
467  postCommon();
468  }
469 
470  void
472  pushStack();
473  }
474 
475  void
477  postCommon();
478  }
479 
480  double
482  double t = popStack();
483  if(t > threshold_) {
484  LogError("ExcessiveTime")
485  << "ExcessiveTime: Module used " << t
486  << " seconds of time which exceeds the error threshold configured in the Timing Service of "
487  << threshold_ << " seconds.";
488  }
489  return t;
490  }
491  }
492 }
493 
495 
void watchPostModuleGlobalEndLumi(PostModuleGlobalEndLumi::slot_type const &iSlot)
void watchPostModuleConstruction(PostModuleConstruction::slot_type const &iSlot)
std::vector< double > sum_events_time_
Definition: Timing.cc:93
unsigned int maxNumberOfThreads() const
Definition: SystemBounds.h:46
void watchPreModuleGlobalBeginRun(PreModuleGlobalBeginRun::slot_type const &iSlot)
void setComment(std::string const &value)
void watchPreEvent(PreEvent::slot_type const &iSlot)
double curr_job_time_
Definition: Timing.cc:79
std::atomic< double > last_run_cpu_
Definition: Timing.cc:83
#define DEFINE_FWK_SERVICE_MAKER(concrete, maker)
Definition: ServiceMaker.h:117
std::vector< double > max_events_time_
Definition: Timing.cc:91
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void watchPostEndJob(PostEndJob::slot_type const &iSlot)
std::vector< double > curr_events_time_
Definition: Timing.cc:84
void watchPreModuleEvent(PreModuleEvent::slot_type const &iSlot)
void watchPreModuleConstruction(PreModuleConstruction::slot_type const &iSlot)
void watchPostEvent(PostEvent::slot_type const &iSlot)
double getTotalCPU() const override
Definition: Timing.cc:269
void preModule(ModuleDescription const &md)
Definition: Timing.cc:451
void watchPreSourceConstruction(PreSourceConstruction::slot_type const &iSlot)
static double getCPU()
Definition: Timing.cc:124
void watchPostSourceConstruction(PostSourceConstruction::slot_type const &iSlot)
void watchPreEventReadFromSource(PreEventReadFromSource::slot_type const &iSlot)
void watchPostModuleEvent(PostModuleEvent::slot_type const &iSlot)
void watchPostModuleGlobalBeginLumi(PostModuleGlobalBeginLumi::slot_type const &iSlot)
void watchPostModuleStreamEndLumi(PostModuleStreamEndLumi::slot_type const &iSlot)
void postEvent(StreamContext const &)
Definition: Timing.cc:385
void preModuleStream(StreamContext const &, ModuleCallingContext const &)
Definition: Timing.cc:471
double curr_job_cpu_
Definition: Timing.cc:80
void watchPostModuleStreamBeginRun(PostModuleStreamBeginRun::slot_type const &iSlot)
void watchPostSourceEvent(PostSourceEvent::slot_type const &iSlot)
edm::serviceregistry::AllArgsMaker< edm::TimingServiceBase, Timing > TimingMaker
Definition: Timing.cc:496
void watchPreModuleGlobalEndRun(PreModuleGlobalEndRun::slot_type const &iSlot)
unsigned int nThreads_
Definition: Timing.cc:96
void preOpenFile(std::string const &, bool)
Definition: Timing.cc:442
unsigned int nStreams_
Definition: Timing.cc:95
void preModuleGlobal(GlobalContext const &, ModuleCallingContext const &)
Definition: Timing.cc:461
void postModule(ModuleDescription const &md)
Definition: Timing.cc:456
static void pushStack()
Definition: Timing.cc:162
Preallocate preallocateSignal_
signal is emitted before beginJob
unsigned int maxNumberOfStreams() const
Definition: SystemBounds.h:43
void watchPreOpenFile(PreOpenFile::slot_type const &iSlot)
void postModuleStream(StreamContext const &, ModuleCallingContext const &)
Definition: Timing.cc:476
void watchPostSourceRun(PostSourceRun::slot_type const &iSlot)
Timing(ParameterSet const &, ActivityRegistry &)
Definition: Timing.cc:167
void watchPreSourceLumi(PreSourceLumi::slot_type const &iSlot)
ModuleDescription const * moduleDescription() const
void postModuleEvent(StreamContext const &, ModuleCallingContext const &)
Definition: Timing.cc:404
std::atomic< double > last_run_time_
Definition: Timing.cc:82
void watchPreModuleEndJob(PreModuleEndJob::slot_type const &iSlot)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: Timing.cc:273
~Timing() override
Definition: Timing.cc:261
void watchPreSourceRun(PreSourceRun::slot_type const &iSlot)
void preSourceLumi()
Definition: Timing.cc:426
void watchPreModuleBeginJob(PreModuleBeginJob::slot_type const &iSlot)
static double jobStartTime()
void postSourceRun()
Definition: Timing.cc:438
void watchPreModuleGlobalBeginLumi(PreModuleGlobalBeginLumi::slot_type const &iSlot)
void watchPostModuleStreamEndRun(PostModuleStreamEndRun::slot_type const &iSlot)
StreamID const & streamID() const
Definition: StreamContext.h:57
void addToCPUTime(StreamID id, double iTime) override
Definition: Timing.cc:264
static std::string ui2str(unsigned int i)
Definition: Timing.cc:110
void watchPreModuleStreamBeginLumi(PreModuleStreamBeginLumi::slot_type const &iSlot)
void setComment(std::string const &value)
unsigned int value() const
Definition: StreamID.h:46
void watchPostSourceLumi(PostSourceLumi::slot_type const &iSlot)
void watchPostModuleGlobalEndRun(PostModuleGlobalEndRun::slot_type const &iSlot)
void watchPostModuleStreamBeginLumi(PostModuleStreamBeginLumi::slot_type const &iSlot)
std::atomic< unsigned long > total_event_count_
Definition: Timing.cc:94
void watchPreModuleStreamEndLumi(PreModuleStreamEndLumi::slot_type const &iSlot)
void watchPreModuleStreamBeginRun(PreModuleStreamBeginRun::slot_type const &iSlot)
static std::vector< double > & moduleTimeStack()
Definition: Timing.cc:146
double b
Definition: hdecay.h:120
static std::string d2str(double d)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void watchPostOpenFile(PostOpenFile::slot_type const &iSlot)
double postCommon() const
Definition: Timing.cc:481
PostGlobalEndRun postGlobalEndRunSignal_
void postSourceLumi()
Definition: Timing.cc:430
void postModuleGlobal(GlobalContext const &, ModuleCallingContext const &)
Definition: Timing.cc:466
HLT enums.
void watchPreModuleStreamEndRun(PreModuleStreamEndRun::slot_type const &iSlot)
void postOpenFile(std::string const &, bool)
Definition: Timing.cc:446
static double getTime()
Definition: Timing.cc:117
void watchPostModuleBeginJob(PostModuleBeginJob::slot_type const &iSlot)
void postSourceEvent(StreamID)
Definition: Timing.cc:422
void watchPostEventReadFromSource(PostEventReadFromSource::slot_type const &iSlot)
void watchPostModuleGlobalBeginRun(PostModuleGlobalBeginRun::slot_type const &iSlot)
EventID const & eventID() const
Definition: StreamContext.h:59
void watchPreSourceEvent(PreSourceEvent::slot_type const &iSlot)
static double popStack()
Definition: Timing.cc:152
void preSourceEvent(StreamID)
Definition: Timing.cc:418
void watchPostModuleEndJob(PostModuleEndJob::slot_type const &iSlot)
void connect(U iFunc)
Definition: Signal.h:63
void preEvent(StreamContext const &)
Definition: Timing.cc:380
void watchPreModuleGlobalEndLumi(PreModuleGlobalEndLumi::slot_type const &iSlot)
std::vector< double > min_events_time_
Definition: Timing.cc:92
void watchPostBeginJob(PostBeginJob::slot_type const &iSlot)
convenience function for attaching to signal