32 #include <sys/resource.h>
39 namespace eventsetup {
40 struct ComponentDescription;
112 void accumulateTimeBegin(std::atomic<CountAndTime*>& countAndTime,
double& accumulatedTime);
113 void accumulateTimeEnd(std::atomic<CountAndTime*>& countAndTime,
double& accumulatedTime);
171 if (gettimeofday(&
t,
nullptr) < 0)
172 throw cms::Exception(
"SysCallFailed",
"Failed call to gettimeofday");
173 return static_cast<double>(
t.tv_sec) + (static_cast<double>(
t.tv_usec) * 1E-6);
178 getrusage(RUSAGE_SELF, &
usage);
180 double totalCPUTime = 0.0;
182 totalCPUTime = (double)
usage.ru_utime.tv_sec + (
double(
usage.ru_utime.tv_usec) * 1E-6);
184 totalCPUTime += (double)
usage.ru_stime.tv_sec + (
double(
usage.ru_stime.tv_usec) * 1E-6);
187 getrusage(RUSAGE_CHILDREN, &
usage);
188 totalCPUTime += (double)
usage.ru_utime.tv_sec + (
double(
usage.ru_utime.tv_usec) * 1E-6);
189 totalCPUTime += (double)
usage.ru_stime.tv_sec + (
double(
usage.ru_stime.tv_usec) * 1E-6);
198 static thread_local std::vector<double> s_stack;
204 assert(!modStack.empty());
205 double curr_module_time = modStack.back();
207 double t =
getTime() - curr_module_time;
211 static void pushStack(
bool configuredInTopLevelProcess) {
212 if (!configuredInTopLevelProcess) {
220 : curr_job_time_(0.),
226 summary_only_(iPS.getUntrackedParameter<
bool>(
"summaryOnly")),
227 report_summary_(iPS.getUntrackedParameter<
bool>(
"useJobReport")),
228 threshold_(iPS.getUntrackedParameter<double>(
"excessiveTimeThreshold")),
231 total_event_count_(0),
232 begin_lumi_count_(0),
234 countAndTimeZero_{0, 0.0},
235 countAndTimeForLock_{&countAndTimeZero_},
236 accumulatedTimeForLock_{0.0},
237 countAndTimeForGet_{&countAndTimeZero_},
238 accumulatedTimeForGet_{0.0},
239 configuredInTopLevelProcess_{
false},
252 bool checkThreshold =
true;
253 if (threshold_ <= 0.0) {
256 checkThreshold =
false;
259 if ((not summary_only_) || (checkThreshold)) {
263 if (checkThreshold) {
315 iRegistry.preallocateSignal_.connect([
this](service::SystemBounds
const& iBounds) {
316 nStreams_ = iBounds.maxNumberOfStreams();
317 nThreads_ = iBounds.maxNumberOfThreads();
318 curr_events_time_.resize(nStreams_, 0.);
319 sum_events_time_.resize(nStreams_, 0.);
320 max_events_time_.resize(nStreams_, 0.);
321 min_events_time_.resize(nStreams_, 1.E6);
322 for (
unsigned int i = 0;
i < nStreams_; ++
i) {
323 countSubProcessesPreEvent_.emplace_back(std::make_unique<std::atomic<unsigned int>>(0));
324 countSubProcessesPostEvent_.emplace_back(std::make_unique<std::atomic<unsigned int>>(0));
339 while (not
extra_job_cpu_.compare_exchange_strong(expected, expected + iTime)) {
347 desc.
addUntracked<
bool>(
"summaryOnly",
false)->setComment(
"If 'true' do not report timing for each event");
348 desc.
addUntracked<
bool>(
"useJobReport",
true)->setComment(
"If 'true' write summary information to JobReport");
351 "Amount of time in seconds before reporting a module or source has taken excessive time. A value of 0.0 "
352 "turns off this reporting.");
353 descriptions.
add(
"Timing", desc);
354 descriptions.
setComment(
"This service reports the time it takes to run each module in a job.");
373 LogImportant(
"TimeReport") <<
"TimeReport> Report activated"
375 <<
"TimeReport> Report columns headings for events: "
376 <<
"eventnum runnum timetaken\n"
377 <<
"TimeReport> Report columns headings for modules: "
378 <<
"eventnum runnum modulelabel modulename timetakeni\n"
385 LogImportant(
"TimeReport") <<
"\nTimeReport> This instance of the Timing Service will be disabled because it "
386 "is configured in a SubProcess.\n"
387 <<
"If multiple instances of the TimingService were configured only the one in the "
388 "top level process will function.\n"
389 <<
"The other instance(s) will simply print this message and do nothing.\n\n";
393 const double job_end_time =
getTime();
394 const double job_end_cpu =
getCPU();
415 total_loop_time = 0.0;
416 total_loop_cpu = 0.0;
419 double sum_all_events_time = 0;
421 sum_all_events_time +=
t;
424 double average_event_time = 0.0;
429 double event_throughput = 0.0;
430 if (total_loop_time != 0.0) {
434 LogImportant(
"TimeReport") <<
"TimeReport> Time report complete in " << total_job_time <<
" seconds"
436 <<
" Time Summary: \n"
437 <<
" - Min event: " << min_event_time <<
"\n"
438 <<
" - Max event: " << max_event_time <<
"\n"
439 <<
" - Avg event: " << average_event_time <<
"\n"
440 <<
" - Total loop: " << total_loop_time <<
"\n"
441 <<
" - Total init: " << total_initialization_time <<
"\n"
442 <<
" - Total job: " << total_job_time <<
"\n"
445 <<
" Event Throughput: " << event_throughput <<
" ev/s\n"
446 <<
" CPU Summary: \n"
447 <<
" - Total loop: " << total_loop_cpu <<
"\n"
448 <<
" - Total init: " << total_initialization_cpu <<
"\n"
450 <<
" - Total job: " << total_job_cpu <<
"\n"
451 <<
" Processing Summary: \n"
458 std::map<std::string, std::string> reportData;
460 reportData.insert(std::make_pair(
"MinEventTime",
d2str(min_event_time)));
461 reportData.insert(std::make_pair(
"MaxEventTime",
d2str(max_event_time)));
462 reportData.insert(std::make_pair(
"AvgEventTime",
d2str(average_event_time)));
463 reportData.insert(std::make_pair(
"EventThroughput",
d2str(event_throughput)));
464 reportData.insert(std::make_pair(
"TotalJobTime",
d2str(total_job_time)));
465 reportData.insert(std::make_pair(
"TotalJobCPU",
d2str(total_job_cpu)));
466 reportData.insert(std::make_pair(
"TotalLoopTime",
d2str(total_loop_time)));
467 reportData.insert(std::make_pair(
"TotalLoopCPU",
d2str(total_loop_cpu)));
468 reportData.insert(std::make_pair(
"TotalInitTime",
d2str(total_initialization_time)));
469 reportData.insert(std::make_pair(
"TotalInitCPU",
d2str(total_initialization_cpu)));
470 reportData.insert(std::make_pair(
"NumberOfStreams",
ui2str(
nStreams_)));
471 reportData.insert(std::make_pair(
"NumberOfThreads",
ui2str(
nThreads_)));
474 reportSvc->reportPerformanceSummary(
"Timing", reportData);
476 std::map<std::string, std::string> reportData1;
480 reportSvc->reportPerformanceSummary(
"ProcessingSummary", reportData1);
521 auto const& eventID = iStream.
eventID();
522 LogPrint(
"TimeEvent") <<
"TimeEvent> " << eventID.event() <<
" " << eventID.run() <<
" " << curr_event_time;
535 auto const& eventID = iStream.
eventID();
539 LogPrint(
"TimeModule") <<
"TimeModule> " << eventID.event() <<
" " << eventID.run() <<
" " << desc.moduleLabel()
540 <<
" " << desc.moduleName() <<
" " <<
t;
601 <<
"ExcessiveTime: Module used " <<
t
602 <<
" seconds of time which exceeds the error threshold configured in the Timing Service of " <<
threshold_
635 auto newStat = std::make_unique<CountAndTime>(0, newTime);
639 if (oldStat ==
nullptr) {
640 oldStat = countAndTime.load();
641 }
else if (countAndTime.compare_exchange_strong(oldStat,
nullptr)) {
647 if (oldStat->
count_ != 0) {
648 accumulatedTime += (newTime - oldStat->
time_) * oldStat->
count_;
650 countAndTime.store(newStat.release());
661 if (oldStat ==
nullptr) {
662 oldStat = countAndTime.load();
663 }
else if (countAndTime.compare_exchange_strong(oldStat,
nullptr)) {
668 if (oldStat->
count_ == 1) {
669 accumulatedTime += newTime - oldStat->
time_;
673 auto newStat = std::make_unique<CountAndTime>(oldStat->
count_ - 1, newTime);
674 accumulatedTime += (newTime - oldStat->
time_) * oldStat->
count_;
675 countAndTime.store(newStat.release());
677 countAndTime.store(oldStat);