32 #include <sys/resource.h>
39 namespace eventsetup {
40 struct ComponentDescription;
100 void accumulateTimeBegin(std::atomic<CountAndTime*>& countAndTime,
double& accumulatedTime);
101 void accumulateTimeEnd(std::atomic<CountAndTime*>& countAndTime,
double& accumulatedTime);
159 if (gettimeofday(&
t,
nullptr) < 0)
160 throw cms::Exception(
"SysCallFailed",
"Failed call to gettimeofday");
161 return static_cast<double>(
t.tv_sec) + (static_cast<double>(
t.tv_usec) * 1E-6);
166 getrusage(RUSAGE_SELF, &
usage);
168 double totalCPUTime = 0.0;
170 totalCPUTime = (double)
usage.ru_utime.tv_sec + (
double(
usage.ru_utime.tv_usec) * 1E-6);
172 totalCPUTime += (double)
usage.ru_stime.tv_sec + (
double(
usage.ru_stime.tv_usec) * 1E-6);
175 getrusage(RUSAGE_CHILDREN, &
usage);
176 totalCPUTime += (double)
usage.ru_utime.tv_sec + (
double(
usage.ru_utime.tv_usec) * 1E-6);
177 totalCPUTime += (double)
usage.ru_stime.tv_sec + (
double(
usage.ru_stime.tv_usec) * 1E-6);
186 static thread_local std::vector<double> s_stack;
192 assert(!modStack.empty());
193 double curr_module_time = modStack.back();
195 double t =
getTime() - curr_module_time;
199 static void pushStack(
bool configuredInTopLevelProcess) {
200 if (!configuredInTopLevelProcess) {
208 : curr_job_time_(0.),
214 summary_only_(iPS.getUntrackedParameter<
bool>(
"summaryOnly")),
215 report_summary_(iPS.getUntrackedParameter<
bool>(
"useJobReport")),
216 threshold_(iPS.getUntrackedParameter<double>(
"excessiveTimeThreshold")),
219 total_event_count_(0),
220 begin_lumi_count_(0),
222 countAndTimeZero_{0, 0.0},
223 countAndTimeForLock_{&countAndTimeZero_},
224 accumulatedTimeForLock_{0.0},
225 countAndTimeForGet_{&countAndTimeZero_},
226 accumulatedTimeForGet_{0.0},
227 configuredInTopLevelProcess_{
false},
236 bool checkThreshold =
true;
237 if (threshold_ <= 0.0) {
240 checkThreshold =
false;
243 if ((not summary_only_) || (checkThreshold)) {
247 if (checkThreshold) {
302 iRegistry.preallocateSignal_.connect([
this](service::SystemBounds
const& iBounds) {
303 nStreams_ = iBounds.maxNumberOfStreams();
304 nThreads_ = iBounds.maxNumberOfThreads();
305 curr_events_time_.resize(nStreams_, 0.);
306 sum_events_time_.resize(nStreams_, 0.);
307 max_events_time_.resize(nStreams_, 0.);
308 min_events_time_.resize(nStreams_, 1.E6);
309 for (
unsigned int i = 0;
i < nStreams_; ++
i) {
310 countSubProcessesPreEvent_.emplace_back(std::make_unique<std::atomic<unsigned int>>(0));
311 countSubProcessesPostEvent_.emplace_back(std::make_unique<std::atomic<unsigned int>>(0));
326 while (not
extra_job_cpu_.compare_exchange_strong(expected, expected + iTime)) {
334 desc.addUntracked<
bool>(
"summaryOnly",
false)->setComment(
"If 'true' do not report timing for each event");
335 desc.addUntracked<
bool>(
"useJobReport",
true)->setComment(
"If 'true' write summary information to JobReport");
336 desc.addUntracked<
double>(
"excessiveTimeThreshold", 0.)
338 "Amount of time in seconds before reporting a module or source has taken excessive time. A value of 0.0 "
339 "turns off this reporting.");
340 descriptions.
add(
"Timing",
desc);
341 descriptions.
setComment(
"This service reports the time it takes to run each module in a job.");
360 LogImportant(
"TimeReport") <<
"TimeReport> Report activated"
362 <<
"TimeReport> Report columns headings for events: "
363 <<
"eventnum runnum timetaken\n"
364 <<
"TimeReport> Report columns headings for modules: "
365 <<
"eventnum runnum modulelabel modulename timetakeni\n"
372 LogImportant(
"TimeReport") <<
"\nTimeReport> This instance of the Timing Service will be disabled because it "
373 "is configured in a SubProcess.\n"
374 <<
"If multiple instances of the TimingService were configured only the one in the "
375 "top level process will function.\n"
376 <<
"The other instance(s) will simply print this message and do nothing.\n\n";
380 const double job_end_time =
getTime();
381 const double job_end_cpu =
getCPU();
402 total_loop_time = 0.0;
403 total_loop_cpu = 0.0;
406 double sum_all_events_time = 0;
408 sum_all_events_time +=
t;
411 double average_event_time = 0.0;
416 double event_throughput = 0.0;
417 if (total_loop_time != 0.0) {
421 LogImportant(
"TimeReport") <<
"TimeReport> Time report complete in " << total_job_time <<
" seconds"
423 <<
" Time Summary: \n"
424 <<
" - Min event: " << min_event_time <<
"\n"
425 <<
" - Max event: " << max_event_time <<
"\n"
426 <<
" - Avg event: " << average_event_time <<
"\n"
427 <<
" - Total loop: " << total_loop_time <<
"\n"
428 <<
" - Total init: " << total_initialization_time <<
"\n"
429 <<
" - Total job: " << total_job_time <<
"\n"
432 <<
" Event Throughput: " << event_throughput <<
" ev/s\n"
433 <<
" CPU Summary: \n"
434 <<
" - Total loop: " << total_loop_cpu <<
"\n"
435 <<
" - Total init: " << total_initialization_cpu <<
"\n"
437 <<
" - Total job: " << total_job_cpu <<
"\n"
438 <<
" Processing Summary: \n"
445 std::map<std::string, std::string> reportData;
447 reportData.insert(std::make_pair(
"MinEventTime",
d2str(min_event_time)));
448 reportData.insert(std::make_pair(
"MaxEventTime",
d2str(max_event_time)));
449 reportData.insert(std::make_pair(
"AvgEventTime",
d2str(average_event_time)));
450 reportData.insert(std::make_pair(
"EventThroughput",
d2str(event_throughput)));
451 reportData.insert(std::make_pair(
"TotalJobTime",
d2str(total_job_time)));
452 reportData.insert(std::make_pair(
"TotalJobCPU",
d2str(total_job_cpu)));
453 reportData.insert(std::make_pair(
"TotalLoopTime",
d2str(total_loop_time)));
454 reportData.insert(std::make_pair(
"TotalLoopCPU",
d2str(total_loop_cpu)));
455 reportData.insert(std::make_pair(
"TotalInitTime",
d2str(total_initialization_time)));
456 reportData.insert(std::make_pair(
"TotalInitCPU",
d2str(total_initialization_cpu)));
457 reportData.insert(std::make_pair(
"NumberOfStreams",
ui2str(
nStreams_)));
458 reportData.insert(std::make_pair(
"NumberOfThreads",
ui2str(
nThreads_)));
461 reportSvc->reportPerformanceSummary(
"Timing", reportData);
463 std::map<std::string, std::string> reportData1;
467 reportSvc->reportPerformanceSummary(
"ProcessingSummary", reportData1);
508 auto const& eventID = iStream.
eventID();
509 LogPrint(
"TimeEvent") <<
"TimeEvent> " << eventID.event() <<
" " << eventID.run() <<
" " << curr_event_time;
522 auto const& eventID = iStream.
eventID();
526 LogPrint(
"TimeModule") <<
"TimeModule> " << eventID.event() <<
" " << eventID.run() <<
" " <<
desc.moduleLabel()
527 <<
" " <<
desc.moduleName() <<
" " <<
t;
588 <<
"ExcessiveTime: Module used " <<
t
589 <<
" seconds of time which exceeds the error threshold configured in the Timing Service of " <<
threshold_
597 auto newStat = std::make_unique<CountAndTime>(0, newTime);
601 if (oldStat ==
nullptr) {
602 oldStat = countAndTime.load();
603 }
else if (countAndTime.compare_exchange_strong(oldStat,
nullptr)) {
609 if (oldStat->
count_ != 0) {
610 accumulatedTime += (newTime - oldStat->
time_) * oldStat->
count_;
612 countAndTime.store(newStat.release());
623 if (oldStat ==
nullptr) {
624 oldStat = countAndTime.load();
625 }
else if (countAndTime.compare_exchange_strong(oldStat,
nullptr)) {
630 if (oldStat->
count_ == 1) {
631 accumulatedTime += newTime - oldStat->
time_;
635 auto newStat = std::make_unique<CountAndTime>(oldStat->
count_ - 1, newTime);
636 accumulatedTime += (newTime - oldStat->
time_) * oldStat->
count_;
637 countAndTime.store(newStat.release());
639 countAndTime.store(oldStat);