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 
31 
32 #include <iostream>
33 #include <sstream>
34 #include <sys/resource.h>
35 #include <sys/time.h>
36 #include <atomic>
37 #include <exception>
38 
39 namespace edm {
40 
41  namespace eventsetup {
42  struct ComponentDescription;
43  class DataKey;
44  class EventSetupRecordKey;
45  } // namespace eventsetup
46 
47  namespace service {
48  class Timing : public TimingServiceBase {
49  public:
50  using time_point = std::chrono::steady_clock::time_point;
51  using double_seconds = std::chrono::duration<double, std::ratio<1, 1>>;
52 
54  ~Timing() override;
55 
56  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
57 
58  void addToCPUTime(double iTime) override;
59  double getTotalCPU() const override;
60 
61  private:
63  void beginProcessing();
64  void postEndJob();
65 
66  void preEvent(StreamContext const&);
67  void postEvent(StreamContext const&);
68  void lastPostEvent(std::chrono::steady_clock::duration curr_event_time,
69  unsigned int index,
70  StreamContext const& iStream);
71 
73 
76 
79 
80  void preSourceRun(RunIndex);
81  void postSourceRun(RunIndex);
82 
83  void preOpenFile(std::string const&);
84  void postOpenFile(std::string const&);
85 
86  void preModule(ModuleDescription const& md);
87  void postModule(ModuleDescription const& md);
88 
91 
92  void postGlobalBeginRun(GlobalContext const&);
94 
97 
98  double postCommon() const;
99 
101  inline void addTask() { runningTasksChanged(true); }
102  inline void removeTask() { runningTasksChanged(false); }
103  void runningTasksChanged(bool iMoreTasks);
104 
106  double curr_job_cpu_; // seconds
107  std::atomic<double> extra_job_cpu_; //seconds
108  //use last run time for determining end of processing
109  std::atomic<time_point> end_loop_time_;
110  std::atomic<double> end_loop_cpu_;
111  std::vector<time_point> curr_events_time_;
114  double threshold_;
115 
116  std::atomic<bool> updating_task_info_ = false;
120  //
121  // Min Max and total event times for each Stream.
122  // Used for summary at end of job
123  std::vector<double> max_events_time_; // seconds
124  std::vector<double> min_events_time_; // seconds
125  std::vector<double> sum_events_time_;
126  std::atomic<unsigned long> total_event_count_;
127  std::atomic<unsigned long> begin_lumi_count_;
128  std::atomic<unsigned long> begin_run_count_;
129  unsigned int nStreams_;
130  unsigned int nThreads_;
131 
132  std::vector<std::unique_ptr<std::atomic<time_point>>> eventSetupModuleStartTimes_;
133  std::vector<std::pair<uintptr_t, eventsetup::EventSetupRecordKey>> eventSetupModuleCallInfo_;
134  std::atomic<double> accumulatedEventSetupModuleTimings_ = 0.; //seconds
135 
136  std::vector<std::unique_ptr<std::atomic<unsigned int>>> countSubProcessesPreEvent_;
137  std::vector<std::unique_ptr<std::atomic<unsigned int>>> countSubProcessesPostEvent_;
138 
140  unsigned int nSubProcesses_;
141  };
142  } // namespace service
143 } // namespace edm
144 
145 namespace edm {
146  namespace service {
147 
148  static std::string d2str(double d) {
149  std::stringstream t;
150  t << d;
151  return t.str();
152  }
153 
154  static std::string ui2str(unsigned int i) {
155  std::stringstream t;
156  t << i;
157  return t.str();
158  }
159 
160  static std::chrono::steady_clock::time_point getTime() { return std::chrono::steady_clock::now(); }
161 
162  static double getChildrenCPU() {
163  struct rusage usage;
164 
165  getrusage(RUSAGE_CHILDREN, &usage);
166  double totalCPUTime = (double)usage.ru_utime.tv_sec + (double(usage.ru_utime.tv_usec) * 1E-6);
167  totalCPUTime += (double)usage.ru_stime.tv_sec + (double(usage.ru_stime.tv_usec) * 1E-6);
168 
169  return totalCPUTime;
170  }
171 
172  static double getCPU() {
173  struct rusage usage;
174  getrusage(RUSAGE_SELF, &usage);
175 
176  double totalCPUTime = 0.0;
177  // User code
178  totalCPUTime = (double)usage.ru_utime.tv_sec + (double(usage.ru_utime.tv_usec) * 1E-6);
179  // System functions
180  totalCPUTime += (double)usage.ru_stime.tv_sec + (double(usage.ru_stime.tv_usec) * 1E-6);
181 
182  // Additionally, add in CPU usage from our child processes.
183  getrusage(RUSAGE_CHILDREN, &usage);
184  totalCPUTime += (double)usage.ru_utime.tv_sec + (double(usage.ru_utime.tv_usec) * 1E-6);
185  totalCPUTime += (double)usage.ru_stime.tv_sec + (double(usage.ru_stime.tv_usec) * 1E-6);
186 
187  return totalCPUTime;
188  }
189 
190  //NOTE: We use a per thread stack for module times since unscheduled
191  // exectuion or tbb task spawning can cause a module to run on the
192  // same thread as an already running module
193  static std::vector<std::chrono::steady_clock::time_point>& moduleTimeStack() {
194  static thread_local std::vector<std::chrono::steady_clock::time_point> s_stack;
195  return s_stack;
196  }
197 
198  static double popStack() {
199  auto& modStack = moduleTimeStack();
200  assert(!modStack.empty());
201  auto curr_module_time = modStack.back();
202  modStack.pop_back();
203  std::chrono::duration<double, std::ratio<1, 1>> t = getTime() - curr_module_time;
204  return t.count();
205  }
206 
207  static void pushStack(bool configuredInTopLevelProcess) {
208  if (!configuredInTopLevelProcess) {
209  return;
210  }
211  auto& modStack = moduleTimeStack();
212  modStack.push_back(getTime());
213  }
214 
216  : curr_job_time_(),
217  curr_job_cpu_(0.),
218  extra_job_cpu_(0.0),
219  end_loop_time_(),
220  end_loop_cpu_(0.0),
221  curr_events_time_(),
222  summary_only_(iPS.getUntrackedParameter<bool>("summaryOnly")),
223  report_summary_(iPS.getUntrackedParameter<bool>("useJobReport")),
224  threshold_(iPS.getUntrackedParameter<double>("excessiveTimeThreshold")),
225  max_events_time_(),
226  min_events_time_(),
227  total_event_count_(0),
228  begin_lumi_count_(0),
229  begin_run_count_(0),
230  configuredInTopLevelProcess_{false},
231  nSubProcesses_{0} {
232  iRegistry.watchPreBeginJob(this, &Timing::preBeginJob);
233  iRegistry.watchBeginProcessing(this, &Timing::beginProcessing);
234  iRegistry.watchPreEndJob([this]() {
235  end_loop_time_ = getTime();
236  end_loop_cpu_ = getCPU();
237  });
238  iRegistry.watchPostEndJob(this, &Timing::postEndJob);
239 
240  iRegistry.watchPreEvent(this, &Timing::preEvent);
241  iRegistry.watchPostEvent(this, &Timing::postEvent);
242 
243  bool checkThreshold = true;
244  if (threshold_ <= 0.0) {
245  //we need to ignore the threshold check
246  threshold_ = std::numeric_limits<double>::max();
247  checkThreshold = false;
248  }
249 
250  if ((not summary_only_) || (checkThreshold)) {
251  iRegistry.watchPreModuleEvent(this, &Timing::preModuleStream);
252  iRegistry.watchPostModuleEvent(this, &Timing::postModuleEvent);
253  iRegistry.watchPreModuleEventAcquire(this, &Timing::preModuleStream);
254  iRegistry.watchPostModuleEventAcquire(this, &Timing::postModuleEvent);
255  }
256  if (checkThreshold) {
257  iRegistry.watchPreSourceEvent(this, &Timing::preSourceEvent);
258  iRegistry.watchPostSourceEvent(this, &Timing::postSourceEvent);
259 
260  iRegistry.watchPreSourceLumi(this, &Timing::preSourceLumi);
261  iRegistry.watchPostSourceLumi(this, &Timing::postSourceLumi);
262 
263  iRegistry.watchPreSourceRun(this, &Timing::preSourceRun);
264  iRegistry.watchPostSourceRun(this, &Timing::postSourceRun);
265 
266  iRegistry.watchPreOpenFile(this, &Timing::preOpenFile);
267  iRegistry.watchPostOpenFile(this, &Timing::postOpenFile);
268 
269  iRegistry.watchPreEventReadFromSource(this, &Timing::preModuleStream);
270  iRegistry.watchPostEventReadFromSource(this, &Timing::postModuleStream);
271 
272  iRegistry.watchPreModuleConstruction(this, &Timing::preModule);
273  iRegistry.watchPostModuleConstruction(this, &Timing::postModule);
274 
275  iRegistry.watchPreModuleDestruction(this, &Timing::preModule);
276  iRegistry.watchPostModuleDestruction(this, &Timing::postModule);
277 
278  iRegistry.watchPreModuleBeginJob(this, &Timing::preModule);
279  iRegistry.watchPostModuleBeginJob(this, &Timing::postModule);
280 
281  iRegistry.watchPreModuleEndJob(this, &Timing::preModule);
282  iRegistry.watchPostModuleEndJob(this, &Timing::postModule);
283 
284  iRegistry.watchPreModuleStreamBeginRun(this, &Timing::preModuleStream);
285  iRegistry.watchPostModuleStreamBeginRun(this, &Timing::postModuleStream);
286  iRegistry.watchPreModuleStreamEndRun(this, &Timing::preModuleStream);
287  iRegistry.watchPostModuleStreamEndRun(this, &Timing::postModuleStream);
288 
289  iRegistry.watchPreModuleStreamBeginLumi(this, &Timing::preModuleStream);
290  iRegistry.watchPostModuleStreamBeginLumi(this, &Timing::postModuleStream);
291  iRegistry.watchPreModuleStreamEndLumi(this, &Timing::preModuleStream);
292  iRegistry.watchPostModuleStreamEndLumi(this, &Timing::postModuleStream);
293 
294  iRegistry.watchPreModuleGlobalBeginRun(this, &Timing::preModuleGlobal);
295  iRegistry.watchPostModuleGlobalBeginRun(this, &Timing::postModuleGlobal);
296  iRegistry.watchPreModuleGlobalEndRun(this, &Timing::preModuleGlobal);
297  iRegistry.watchPostModuleGlobalEndRun(this, &Timing::postModuleGlobal);
298 
299  iRegistry.watchPreModuleGlobalBeginLumi(this, &Timing::preModuleGlobal);
300  iRegistry.watchPostModuleGlobalBeginLumi(this, &Timing::postModuleGlobal);
301  iRegistry.watchPreModuleGlobalEndLumi(this, &Timing::preModuleGlobal);
302  iRegistry.watchPostModuleGlobalEndLumi(this, &Timing::postModuleGlobal);
303 
304  iRegistry.watchPreSourceConstruction(this, &Timing::preModule);
305  iRegistry.watchPostSourceConstruction(this, &Timing::postModule);
306  }
307 
308  auto preESModuleLambda = [this](auto const& recordKey, auto const& context) {
309  addTask();
310  //find available slot
311  auto startTime = getTime();
312  bool foundSlot = false;
313  do {
314  for (size_t i = 0; i < eventSetupModuleStartTimes_.size(); ++i) {
315  auto& slot = *eventSetupModuleStartTimes_[i];
316  std::chrono::steady_clock::time_point expect;
317  if (slot.compare_exchange_strong(expect, startTime)) {
318  foundSlot = true;
319  eventSetupModuleCallInfo_[i].first = uintptr_t(context.componentDescription());
320  eventSetupModuleCallInfo_[i].second = recordKey;
321  break;
322  }
323  }
324  //if foundSlot == false then other threads stole the slots before this thread
325  // so should check starting over again
326  } while (not foundSlot);
327  };
328  iRegistry.watchPreESModule(preESModuleLambda);
329  iRegistry.watchPreESModuleAcquire(preESModuleLambda);
330 
331  auto postESModuleLambda = [this](auto const& recordKey, auto const& context) {
332  removeTask();
333  auto stopTime = getTime();
334  for (size_t i = 0; i < eventSetupModuleStartTimes_.size(); ++i) {
335  auto const& info = eventSetupModuleCallInfo_[i];
336  if (info.first == uintptr_t(context.componentDescription()) and info.second == recordKey) {
337  auto startTime = eventSetupModuleStartTimes_[i]->exchange(std::chrono::steady_clock::time_point());
338  auto expect = accumulatedEventSetupModuleTimings_.load();
339  double_seconds timeDiff = stopTime - startTime;
340  auto accumulatedTime = expect + timeDiff.count();
341  while (not accumulatedEventSetupModuleTimings_.compare_exchange_strong(expect, accumulatedTime)) {
342  accumulatedTime = expect + timeDiff.count();
343  }
344  break;
345  }
346  }
347  };
348  iRegistry.watchPostESModule(postESModuleLambda);
349  iRegistry.watchPostESModuleAcquire(postESModuleLambda);
350 
351  iRegistry.watchPostGlobalBeginRun(this, &Timing::postGlobalBeginRun);
352  iRegistry.watchPostGlobalBeginLumi(this, &Timing::postGlobalBeginLumi);
353 
354  iRegistry.watchPreallocate([this](service::SystemBounds const& iBounds) {
355  nStreams_ = iBounds.maxNumberOfStreams();
356  nThreads_ = iBounds.maxNumberOfThreads();
357  curr_events_time_.resize(nStreams_, time_point());
358  sum_events_time_.resize(nStreams_, 0.);
359  max_events_time_.resize(nStreams_, 0.);
360  min_events_time_.resize(nStreams_, 1.E6);
361  eventSetupModuleStartTimes_.reserve(nThreads_);
362  for (unsigned int i = 0; i < nThreads_; ++i) {
363  eventSetupModuleStartTimes_.emplace_back(std::make_unique<std::atomic<time_point>>());
364  }
365  eventSetupModuleCallInfo_.resize(nThreads_);
366 
367  for (unsigned int i = 0; i < nStreams_; ++i) {
368  countSubProcessesPreEvent_.emplace_back(std::make_unique<std::atomic<unsigned int>>(0));
369  countSubProcessesPostEvent_.emplace_back(std::make_unique<std::atomic<unsigned int>>(0));
370  }
371  });
372  setTaskCallbacks(iRegistry);
373  }
374 
376  iRegistry.watchPreSourceEvent([this](auto) { addTask(); });
377  iRegistry.watchPostSourceEvent([this](auto) { removeTask(); });
378 
379  iRegistry.watchPreModuleEvent([this](auto, auto) { addTask(); });
380  iRegistry.watchPostModuleEvent([this](auto, auto) { removeTask(); });
381  iRegistry.watchPreModuleEventAcquire([this](auto, auto) { addTask(); });
382  iRegistry.watchPostModuleEventAcquire([this](auto, auto) { removeTask(); });
383 
384  iRegistry.watchPreSourceLumi([this](auto) { addTask(); });
385  iRegistry.watchPostSourceLumi([this](auto) { removeTask(); });
386 
387  iRegistry.watchPreSourceRun([this](auto) { addTask(); });
388  iRegistry.watchPostSourceRun([this](auto) { removeTask(); });
389 
390  iRegistry.watchPreEventReadFromSource([this](auto, auto) { addTask(); });
391  iRegistry.watchPostEventReadFromSource([this](auto, auto) { removeTask(); });
392 
393  iRegistry.watchPreModuleStreamBeginRun([this](auto, auto) { addTask(); });
394  iRegistry.watchPostModuleStreamBeginRun([this](auto, auto) { removeTask(); });
395  iRegistry.watchPreModuleStreamEndRun([this](auto, auto) { addTask(); });
396  iRegistry.watchPostModuleStreamEndRun([this](auto, auto) { removeTask(); });
397 
398  iRegistry.watchPreModuleStreamBeginLumi([this](auto, auto) { addTask(); });
399  iRegistry.watchPostModuleStreamBeginLumi([this](auto, auto) { removeTask(); });
400  iRegistry.watchPreModuleStreamEndLumi([this](auto, auto) { addTask(); });
401  iRegistry.watchPostModuleStreamEndLumi([this](auto, auto) { removeTask(); });
402 
403  iRegistry.watchPreModuleGlobalBeginRun([this](auto, auto) { addTask(); });
404  iRegistry.watchPostModuleGlobalBeginRun([this](auto, auto) { removeTask(); });
405  iRegistry.watchPreModuleGlobalEndRun([this](auto, auto) { addTask(); });
406  iRegistry.watchPostModuleGlobalEndRun([this](auto, auto) { removeTask(); });
407 
408  iRegistry.watchPreModuleGlobalBeginLumi([this](auto, auto) { addTask(); });
409  iRegistry.watchPostModuleGlobalBeginLumi([this](auto, auto) { removeTask(); });
410  iRegistry.watchPreModuleGlobalEndLumi([this](auto, auto) { addTask(); });
411  iRegistry.watchPostModuleGlobalEndLumi([this](auto, auto) { removeTask(); });
412 
413  //account for any time ESSources spend looking up new IOVs
414  iRegistry.watchPreESSyncIOV([this](auto const&) { addTask(); });
415  iRegistry.watchPostESSyncIOV([this](auto const&) { removeTask(); });
416  }
417 
419 
420  void Timing::addToCPUTime(double iTime) {
421  //For accounting purposes we effectively can say we started earlier
422  double expected = extra_job_cpu_.load();
423  while (not extra_job_cpu_.compare_exchange_strong(expected, expected + iTime)) {
424  }
425  }
426 
427  double Timing::getTotalCPU() const { return getCPU(); }
428 
431  desc.addUntracked<bool>("summaryOnly", false)->setComment("If 'true' do not report timing for each event");
432  desc.addUntracked<bool>("useJobReport", true)->setComment("If 'true' write summary information to JobReport");
433  desc.addUntracked<double>("excessiveTimeThreshold", 0.)
434  ->setComment(
435  "Amount of time in seconds before reporting a module or source has taken excessive time. A value of 0.0 "
436  "turns off this reporting.");
437  descriptions.add("Timing", desc);
438  descriptions.setComment("This service reports the time it takes to run each module in a job.");
439  }
440 
441  void Timing::preBeginJob(PathsAndConsumesOfModulesBase const& pathsAndConsumes, ProcessContext const& pc) {
442  if (pc.isSubProcess()) {
443  ++nSubProcesses_;
444  } else {
446  }
447  }
448 
451  return;
452  }
454  curr_job_cpu_ = getCPU();
456 
457  if (not summary_only_) {
458  LogImportant("TimeReport")
459  << "TimeReport> Report activated"
460  << "\n"
461  << "TimeReport> Report columns headings for events: "
462  << "eventnum runnum timetaken\n"
463  << "TimeReport> Report columns headings for modules: "
464  << "eventnum runnum modulelabel modulename timetaken\n"
465  << "TimeReport> JobTime="
466  << std::chrono::time_point_cast<std::chrono::seconds>(curr_job_time_).time_since_epoch().count()
467  << " JobCPU=" << curr_job_cpu_ << "\n";
468  }
469  }
470 
473  LogImportant("TimeReport") << "\nTimeReport> This instance of the Timing Service will be disabled because it "
474  "is configured in a SubProcess.\n"
475  << "If multiple instances of the TimingService were configured only the one in the "
476  "top level process will function.\n"
477  << "The other instance(s) will simply print this message and do nothing.\n\n";
478  return;
479  }
480 
481  const auto job_end_time = getTime();
482  const double job_end_cpu = getCPU();
483  auto total_job_time = double_seconds(job_end_time - jobStartTime()).count();
484 
485  double total_job_cpu = job_end_cpu + extra_job_cpu_;
486 
487  const double job_end_children_cpu = getChildrenCPU();
488 
489  const double total_initialization_time = double_seconds(curr_job_time_ - jobStartTime()).count();
490  const double total_initialization_cpu = curr_job_cpu_;
491 
492  if (time_point() == jobStartTime()) {
493  //did not capture beginning time
494  total_job_time = double_seconds(job_end_time - curr_job_time_).count();
495  total_job_cpu = job_end_cpu + extra_job_cpu_ - curr_job_cpu_;
496  }
497 
498  double min_event_time = *(std::min_element(min_events_time_.begin(), min_events_time_.end()));
499  double max_event_time = *(std::max_element(max_events_time_.begin(), max_events_time_.end()));
500 
501  auto total_loop_time = double_seconds(end_loop_time_.load() - curr_job_time_).count();
502  auto total_loop_cpu = end_loop_cpu_ + extra_job_cpu_ - curr_job_cpu_;
503 
504  if (end_loop_time_.load() == time_point()) {
505  total_loop_time = 0.0;
506  total_loop_cpu = 0.0;
507  }
508 
509  double sum_all_events_time = 0;
510  for (auto t : sum_events_time_) {
511  sum_all_events_time += t;
512  }
513 
514  double average_event_time = 0.0;
515  if (total_event_count_ != 0) {
516  average_event_time = sum_all_events_time / total_event_count_;
517  }
518 
519  double event_throughput = 0.0;
520  if (total_loop_time != 0.0) {
521  event_throughput = total_event_count_ / total_loop_time;
522  }
523 
524  LogImportant("TimeReport") << "TimeReport> Time report complete in " << total_job_time << " seconds"
525  << "\n"
526  << " Time Summary: \n"
527  << " - Min event: " << min_event_time << "\n"
528  << " - Max event: " << max_event_time << "\n"
529  << " - Avg event: " << average_event_time << "\n"
530  << " - Total loop: " << total_loop_time << "\n"
531  << " - Total init: " << total_initialization_time << "\n"
532  << " - Total job: " << total_job_time << "\n"
533  << " - Total EventSetup: " << accumulatedEventSetupModuleTimings_.load() << "\n"
534  << " - Total non-module: " << total_time_without_tasks_ << "\n"
535  << " Event Throughput: " << event_throughput << " ev/s\n"
536  << " CPU Summary: \n"
537  << " - Total loop: " << total_loop_cpu << "\n"
538  << " - Total init: " << total_initialization_cpu << "\n"
539  << " - Total extra: " << extra_job_cpu_ << "\n"
540  << " - Total children: " << job_end_children_cpu << "\n"
541  << " - Total job: " << total_job_cpu << "\n"
542  << " Processing Summary: \n"
543  << " - Number of Events: " << total_event_count_ << "\n"
544  << " - Number of Global Begin Lumi Calls: " << begin_lumi_count_ << "\n"
545  << " - Number of Global Begin Run Calls: " << begin_run_count_ << "\n";
546 
547  if (report_summary_) {
548  Service<JobReport> reportSvc;
549  std::map<std::string, std::string> reportData;
550 
551  reportData.insert(std::make_pair("MinEventTime", d2str(min_event_time)));
552  reportData.insert(std::make_pair("MaxEventTime", d2str(max_event_time)));
553  reportData.insert(std::make_pair("AvgEventTime", d2str(average_event_time)));
554  reportData.insert(std::make_pair("EventThroughput", d2str(event_throughput)));
555  reportData.insert(std::make_pair("TotalJobTime", d2str(total_job_time)));
556  reportData.insert(std::make_pair("TotalJobCPU", d2str(total_job_cpu)));
557  reportData.insert(std::make_pair("TotalJobChildrenCPU", d2str(job_end_children_cpu)));
558  reportData.insert(std::make_pair("TotalLoopTime", d2str(total_loop_time)));
559  reportData.insert(std::make_pair("TotalEventSetupTime", d2str(accumulatedEventSetupModuleTimings_.load())));
560  reportData.insert(std::make_pair("TotalNonModuleTime", d2str(total_time_without_tasks_)));
561  reportData.insert(std::make_pair("TotalLoopCPU", d2str(total_loop_cpu)));
562  reportData.insert(std::make_pair("TotalInitTime", d2str(total_initialization_time)));
563  reportData.insert(std::make_pair("TotalInitCPU", d2str(total_initialization_cpu)));
564  reportData.insert(std::make_pair("NumberOfStreams", ui2str(nStreams_)));
565  reportData.insert(std::make_pair("NumberOfThreads", ui2str(nThreads_)));
566  reportSvc->reportPerformanceSummary("Timing", reportData);
567 
568  std::map<std::string, std::string> reportData1;
569  reportData1.insert(std::make_pair("NumberEvents", ui2str(total_event_count_)));
570  reportData1.insert(std::make_pair("NumberBeginLumiCalls", ui2str(begin_lumi_count_)));
571  reportData1.insert(std::make_pair("NumberBeginRunCalls", ui2str(begin_run_count_)));
572  reportSvc->reportPerformanceSummary("ProcessingSummary", reportData1);
573  }
574  }
575 
576  void Timing::preEvent(StreamContext const& iStream) {
578  return;
579  }
580  auto index = iStream.streamID().value();
581  if (nSubProcesses_ == 0u) {
583  } else {
584  unsigned int count = ++(*countSubProcessesPreEvent_[index]);
585  if (count == 1) {
587  } else if (count == (nSubProcesses_ + 1)) {
589  }
590  }
591  }
592 
593  void Timing::postEvent(StreamContext const& iStream) {
595  return;
596  }
597  auto index = iStream.streamID().value();
598  if (nSubProcesses_ == 0u) {
600  } else {
601  unsigned int count = ++(*countSubProcessesPostEvent_[index]);
602  if (count == (nSubProcesses_ + 1)) {
605  }
606  }
607  }
608 
609  void Timing::lastPostEvent(std::chrono::steady_clock::duration curr_event_time,
610  unsigned int index,
611  StreamContext const& iStream) {
612  double curr_event_time_d = double_seconds(curr_event_time).count();
613  sum_events_time_[index] += curr_event_time_d;
614 
615  if (not summary_only_) {
616  auto const& eventID = iStream.eventID();
617  LogPrint("TimeEvent") << "TimeEvent> " << eventID.event() << " " << eventID.run() << " " << curr_event_time_d;
618  }
619  if (curr_event_time_d > max_events_time_[index])
620  max_events_time_[index] = curr_event_time_d;
621  if (curr_event_time_d < min_events_time_[index])
622  min_events_time_[index] = curr_event_time_d;
624  }
625 
626  void Timing::postModuleEvent(StreamContext const& iStream, ModuleCallingContext const& iModule) {
628  return;
629  }
630  auto const& eventID = iStream.eventID();
631  auto const& desc = *(iModule.moduleDescription());
632  double t = postCommon();
633  if (not summary_only_) {
634  LogPrint("TimeModule") << "TimeModule> " << eventID.event() << " " << eventID.run() << " " << desc.moduleLabel()
635  << " " << desc.moduleName() << " " << t;
636  }
637  }
638 
640 
642 
644 
646 
648 
650 
652 
654 
656 
658 
661  }
662 
664 
667  return;
668  }
669  if (!gc.processContext()->isSubProcess()) {
671  }
672  }
673 
676  return;
677  }
678  if (!gc.processContext()->isSubProcess()) {
680  }
681  }
682 
685  }
686 
688 
689  double Timing::postCommon() const {
691  return 0.0;
692  }
693  double t = popStack();
694  if (t > threshold_) {
695  LogError("ExcessiveTime")
696  << "ExcessiveTime: Module used " << t
697  << " seconds of time which exceeds the error threshold configured in the Timing Service of " << threshold_
698  << " seconds.";
699  }
700  return t;
701  }
702 
703  void Timing::runningTasksChanged(bool iMoreTasks) {
704  const auto presentTime = getTime();
705  bool expected = false;
706  while (not updating_task_info_.compare_exchange_strong(expected, true)) {
707  expected = false;
708  }
709  auto previousNumberOfTasks = iMoreTasks ? num_running_tasks_++ : num_running_tasks_--;
711  (nThreads_ - previousNumberOfTasks) * double_seconds(presentTime - last_task_change_time_).count();
712  last_task_change_time_ = presentTime;
713  updating_task_info_ = false;
714  }
715  } // namespace service
716 } // namespace edm
717 
719 
void watchPostModuleGlobalEndLumi(PostModuleGlobalEndLumi::slot_type const &iSlot)
std::vector< double > sum_events_time_
Definition: Timing.cc:125
void watchPreModuleGlobalBeginRun(PreModuleGlobalBeginRun::slot_type const &iSlot)
static const TGPicture * info(bool iBackgroundIsBlack)
#define DEFINE_FWK_SERVICE_MAKER(concrete, maker)
Definition: ServiceMaker.h:102
double seconds()
std::vector< double > max_events_time_
Definition: Timing.cc:123
void watchPreModuleEventAcquire(PreModuleEventAcquire::slot_type const &iSlot)
void setTaskCallbacks(ActivityRegistry &)
Definition: Timing.cc:375
static std::chrono::steady_clock::time_point getTime()
Definition: Timing.cc:160
void watchPreModuleEvent(PreModuleEvent::slot_type const &iSlot)
void preModule(ModuleDescription const &md)
Definition: Timing.cc:655
static double getCPU()
Definition: Timing.cc:172
void lastPostEvent(std::chrono::steady_clock::duration curr_event_time, unsigned int index, StreamContext const &iStream)
Definition: Timing.cc:609
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:593
void preModuleStream(StreamContext const &, ModuleCallingContext const &)
Definition: Timing.cc:683
double curr_job_cpu_
Definition: Timing.cc:106
void watchPostModuleStreamBeginRun(PostModuleStreamBeginRun::slot_type const &iSlot)
void watchPostSourceEvent(PostSourceEvent::slot_type const &iSlot)
std::atomic< unsigned long > begin_lumi_count_
Definition: Timing.cc:127
Log< level::Error, false > LogError
void preSourceLumi(LuminosityBlockIndex)
Definition: Timing.cc:643
ModuleDescription const * moduleDescription() const noexcept
void watchPreESSyncIOV(PreESSyncIOV::slot_type const &iSlot)
assert(be >=bs)
void watchPreModuleGlobalEndRun(PreModuleGlobalEndRun::slot_type const &iSlot)
unsigned int nThreads_
Definition: Timing.cc:130
unsigned int nStreams_
Definition: Timing.cc:129
void preModuleGlobal(GlobalContext const &, ModuleCallingContext const &)
Definition: Timing.cc:659
void postModule(ModuleDescription const &md)
Definition: Timing.cc:657
unsigned int num_running_tasks_
Definition: Timing.cc:117
std::atomic< bool > updating_task_info_
Definition: Timing.cc:116
void postGlobalBeginRun(GlobalContext const &)
Definition: Timing.cc:665
void preSourceRun(RunIndex)
Definition: Timing.cc:647
double postCommon() const
Definition: Timing.cc:689
void postModuleStream(StreamContext const &, ModuleCallingContext const &)
Definition: Timing.cc:687
void preOpenFile(std::string const &)
Definition: Timing.cc:651
void watchPostSourceRun(PostSourceRun::slot_type const &iSlot)
double total_time_without_tasks_
Definition: Timing.cc:119
Timing(ParameterSet const &, ActivityRegistry &)
Definition: Timing.cc:215
void watchPreSourceLumi(PreSourceLumi::slot_type const &iSlot)
StreamID const & streamID() const
Definition: StreamContext.h:55
static std::vector< std::chrono::steady_clock::time_point > & moduleTimeStack()
Definition: Timing.cc:193
void postModuleEvent(StreamContext const &, ModuleCallingContext const &)
Definition: Timing.cc:626
#define CMS_THREAD_GUARD(_var_)
void watchPostModuleEventAcquire(PostModuleEventAcquire::slot_type const &iSlot)
Log< level::Error, true > LogImportant
void runningTasksChanged(bool iMoreTasks)
Definition: Timing.cc:703
void postSourceLumi(LuminosityBlockIndex)
Definition: Timing.cc:645
static void pushStack(bool configuredInTopLevelProcess)
Definition: Timing.cc:207
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: Timing.cc:429
std::chrono::steady_clock::time_point time_point
Definition: Timing.cc:50
std::atomic< double > end_loop_cpu_
Definition: Timing.cc:110
~Timing() override
Definition: Timing.cc:418
void watchPreSourceRun(PreSourceRun::slot_type const &iSlot)
ProcessContext const * processContext() const
Definition: GlobalContext.h:70
Log< level::Warning, true > LogPrint
std::vector< time_point > curr_events_time_
Definition: Timing.cc:111
double getTotalCPU() const override
Definition: Timing.cc:427
d
Definition: ztail.py:151
std::vector< std::unique_ptr< std::atomic< unsigned int > > > countSubProcessesPreEvent_
Definition: Timing.cc:136
std::vector< std::unique_ptr< std::atomic< unsigned int > > > countSubProcessesPostEvent_
Definition: Timing.cc:137
void watchPreModuleGlobalBeginLumi(PreModuleGlobalBeginLumi::slot_type const &iSlot)
void watchPostModuleStreamEndRun(PostModuleStreamEndRun::slot_type const &iSlot)
static std::string ui2str(unsigned int i)
Definition: Timing.cc:154
void watchPreModuleStreamBeginLumi(PreModuleStreamBeginLumi::slot_type const &iSlot)
void postOpenFile(std::string const &)
Definition: Timing.cc:653
void setComment(std::string const &value)
void postGlobalBeginLumi(GlobalContext const &)
Definition: Timing.cc:674
std::atomic< unsigned long > begin_run_count_
Definition: Timing.cc:128
std::atomic< time_point > end_loop_time_
Definition: Timing.cc:109
std::atomic< double > accumulatedEventSetupModuleTimings_
Definition: Timing.cc:134
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:126
void watchPreModuleStreamEndLumi(PreModuleStreamEndLumi::slot_type const &iSlot)
void watchPreModuleStreamBeginRun(PreModuleStreamBeginRun::slot_type const &iSlot)
void addToCPUTime(double iTime) override
Definition: Timing.cc:420
static std::string d2str(double d)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
bool configuredInTopLevelProcess_
Definition: Timing.cc:139
edm::serviceregistry::AllArgsMaker< edm::TimingServiceBase, Timing > TimingMaker
Definition: Timing.cc:720
void postSourceRun(RunIndex)
Definition: Timing.cc:649
void postModuleGlobal(GlobalContext const &, ModuleCallingContext const &)
Definition: Timing.cc:663
time_point curr_job_time_
Definition: Timing.cc:105
void preBeginJob(PathsAndConsumesOfModulesBase const &, ProcessContext const &)
Definition: Timing.cc:441
bool isSubProcess() const
HLT enums.
void watchPreModuleStreamEndRun(PreModuleStreamEndRun::slot_type const &iSlot)
EventID const & eventID() const
Definition: StreamContext.h:60
void usage()
Definition: array2xmlEB.cc:14
std::chrono::duration< double, std::ratio< 1, 1 > > double_seconds
Definition: Timing.cc:51
void postSourceEvent(StreamID)
Definition: Timing.cc:641
void watchPostEventReadFromSource(PostEventReadFromSource::slot_type const &iSlot)
void watchPostModuleGlobalBeginRun(PostModuleGlobalBeginRun::slot_type const &iSlot)
void beginProcessing()
Definition: Timing.cc:449
void watchPreSourceEvent(PreSourceEvent::slot_type const &iSlot)
unsigned int value() const
Definition: StreamID.h:43
static std::chrono::steady_clock::time_point jobStartTime()
std::atomic< double > extra_job_cpu_
Definition: Timing.cc:107
time_point last_task_change_time_
Definition: Timing.cc:118
static double popStack()
Definition: Timing.cc:198
std::vector< std::unique_ptr< std::atomic< time_point > > > eventSetupModuleStartTimes_
Definition: Timing.cc:132
void preSourceEvent(StreamID)
Definition: Timing.cc:639
void preEvent(StreamContext const &)
Definition: Timing.cc:576
void watchPreModuleGlobalEndLumi(PreModuleGlobalEndLumi::slot_type const &iSlot)
static double getChildrenCPU()
Definition: Timing.cc:162
std::vector< std::pair< uintptr_t, eventsetup::EventSetupRecordKey > > eventSetupModuleCallInfo_
Definition: Timing.cc:133
std::vector< double > min_events_time_
Definition: Timing.cc:124
unsigned int nSubProcesses_
Definition: Timing.cc:140
void watchPostESSyncIOV(PostESSyncIOV::slot_type const &iSlot)