32 #include <sys/resource.h> 39 namespace eventsetup {
40 struct ComponentDescription;
53 void addToCPUTime(
double iTime)
override;
54 double getTotalCPU()
const override;
63 void lastPostEvent(
double curr_event_time,
unsigned int index,
StreamContext const& iStream);
91 double postCommon()
const;
112 void accumulateTimeBegin(std::atomic<CountAndTime*>& countAndTime,
double& accumulatedTime);
113 void accumulateTimeEnd(std::atomic<CountAndTime*>& countAndTime,
double& accumulatedTime);
145 std::vector<
std::unique_ptr<
std::atomic<
unsigned int>>> countSubProcessesPreEvent_;
146 std::vector<
std::unique_ptr<
std::atomic<
unsigned int>>> countSubProcessesPostEvent_;
148 bool configuredInTopLevelProcess_;
149 unsigned int nSubProcesses_;
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));
338 double expected = extra_job_cpu_.load();
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.");
361 configuredInTopLevelProcess_ =
true;
366 if (!configuredInTopLevelProcess_) {
372 if (not summary_only_) {
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" 379 <<
"TimeReport> JobTime=" << curr_job_time_ <<
" JobCPU=" << curr_job_cpu_ <<
"\n";
384 if (!configuredInTopLevelProcess_) {
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();
399 const double total_initialization_time = curr_job_time_ -
jobStartTime();
405 total_job_cpu = job_end_cpu + extra_job_cpu_ -
curr_job_cpu_;
408 double min_event_time = *(std::min_element(min_events_time_.begin(), min_events_time_.end()));
409 double max_event_time = *(std::max_element(max_events_time_.begin(), max_events_time_.end()));
412 auto total_loop_cpu = last_run_cpu_ + extra_job_cpu_ -
curr_job_cpu_;
414 if (last_run_time_ == 0.0) {
415 total_loop_time = 0.0;
416 total_loop_cpu = 0.0;
419 double sum_all_events_time = 0;
420 for (
auto t : sum_events_time_) {
421 sum_all_events_time +=
t;
424 double average_event_time = 0.0;
425 if (total_event_count_ != 0) {
429 double event_throughput = 0.0;
430 if (total_loop_time != 0.0) {
431 event_throughput = total_event_count_ / total_loop_time;
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" 443 <<
" - EventSetup Lock: " << accumulatedTimeForLock_ <<
"\n" 444 <<
" - EventSetup Get: " << accumulatedTimeForGet_ <<
"\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" 449 <<
" - Total extra: " << extra_job_cpu_ <<
"\n" 450 <<
" - Total job: " << total_job_cpu <<
"\n" 451 <<
" Processing Summary: \n" 452 <<
" - Number of Events: " << total_event_count_ <<
"\n" 453 <<
" - Number of Global Begin Lumi Calls: " << begin_lumi_count_ <<
"\n" 454 <<
" - Number of Global Begin Run Calls: " << begin_run_count_ <<
"\n";
456 if (report_summary_) {
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_)));
472 reportData.insert(std::make_pair(
"EventSetup Lock",
d2str(accumulatedTimeForLock_)));
473 reportData.insert(std::make_pair(
"EventSetup Get",
d2str(accumulatedTimeForGet_)));
474 reportSvc->reportPerformanceSummary(
"Timing", reportData);
476 std::map<std::string, std::string> reportData1;
477 reportData1.insert(std::make_pair(
"NumberEvents",
ui2str(total_event_count_)));
478 reportData1.insert(std::make_pair(
"NumberBeginLumiCalls",
ui2str(begin_lumi_count_)));
479 reportData1.insert(std::make_pair(
"NumberBeginRunCalls",
ui2str(begin_run_count_)));
480 reportSvc->reportPerformanceSummary(
"ProcessingSummary", reportData1);
485 if (!configuredInTopLevelProcess_) {
489 if (nSubProcesses_ == 0u) {
492 unsigned int count = ++(*countSubProcessesPreEvent_[
index]);
495 }
else if (count == (nSubProcesses_ + 1)) {
496 *countSubProcessesPreEvent_[
index] = 0;
502 if (!configuredInTopLevelProcess_) {
506 if (nSubProcesses_ == 0u) {
509 unsigned int count = ++(*countSubProcessesPostEvent_[
index]);
510 if (count == (nSubProcesses_ + 1)) {
512 *countSubProcessesPostEvent_[
index] = 0;
518 sum_events_time_[
index] += curr_event_time;
520 if (not summary_only_) {
521 auto const& eventID = iStream.
eventID();
522 LogPrint(
"TimeEvent") <<
"TimeEvent> " << eventID.event() <<
" " << eventID.run() <<
" " << curr_event_time;
524 if (curr_event_time > max_events_time_[index])
525 max_events_time_[
index] = curr_event_time;
526 if (curr_event_time < min_events_time_[index])
527 min_events_time_[
index] = curr_event_time;
532 if (!configuredInTopLevelProcess_) {
535 auto const& eventID = iStream.
eventID();
538 if (not summary_only_) {
539 LogPrint(
"TimeModule") <<
"TimeModule> " << eventID.event() <<
" " << eventID.run() <<
" " << desc.moduleLabel()
540 <<
" " << desc.moduleName() <<
" " <<
t;
571 if (!configuredInTopLevelProcess_) {
580 if (!configuredInTopLevelProcess_) {
595 if (!configuredInTopLevelProcess_) {
599 if (t > threshold_) {
601 <<
"ExcessiveTime: Module used " << t
602 <<
" seconds of time which exceeds the error threshold configured in the Timing Service of " << threshold_
611 if (!configuredInTopLevelProcess_) {
620 if (!configuredInTopLevelProcess_) {
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)) {
646 newStat->
count_ = oldStat->count_ + 1;
647 if (oldStat->count_ != 0) {
648 accumulatedTime += (newTime - oldStat->time_) * oldStat->count_;
650 countAndTime.store(newStat.release());
651 if (oldStat != &countAndTimeZero_) {
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_;
670 countAndTime.store(&countAndTimeZero_);
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);
std::vector< double > sum_events_time_
void setComment(std::string const &value)
std::atomic< double > last_run_cpu_
std::vector< double > max_events_time_
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
std::vector< double > curr_events_time_
void postLockEventSetupGet(eventsetup::ComponentDescription const *, eventsetup::EventSetupRecordKey const &, eventsetup::DataKey const &)
double getTotalCPU() const override
void preModule(ModuleDescription const &md)
#define DEFINE_FWK_SERVICE_MAKER(concrete, maker)
void postEvent(StreamContext const &)
void preModuleStream(StreamContext const &, ModuleCallingContext const &)
std::atomic< unsigned long > begin_lumi_count_
void lastPostEvent(double curr_event_time, unsigned int index, StreamContext const &iStream)
void accumulateTimeBegin(std::atomic< CountAndTime * > &countAndTime, double &accumulatedTime)
void preSourceLumi(LuminosityBlockIndex)
void preOpenFile(std::string const &, bool)
void preModuleGlobal(GlobalContext const &, ModuleCallingContext const &)
void postModule(ModuleDescription const &md)
void postGlobalBeginRun(GlobalContext const &)
void preSourceRun(RunIndex)
void postModuleStream(StreamContext const &, ModuleCallingContext const &)
void accumulateTimeEnd(std::atomic< CountAndTime * > &countAndTime, double &accumulatedTime)
CountAndTime(unsigned int count, double time)
ModuleDescription const * moduleDescription() const
void postModuleEvent(StreamContext const &, ModuleCallingContext const &)
#define CMS_THREAD_GUARD(_var_)
std::atomic< double > last_run_time_
void postSourceLumi(LuminosityBlockIndex)
static void pushStack(bool configuredInTopLevelProcess)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::atomic< CountAndTime * > countAndTimeForLock_
void preLockEventSetupGet(eventsetup::ComponentDescription const *, eventsetup::EventSetupRecordKey const &, eventsetup::DataKey const &)
static double jobStartTime()
StreamID const & streamID() const
static std::string ui2str(unsigned int i)
void setComment(std::string const &value)
void postGlobalBeginLumi(GlobalContext const &)
std::atomic< unsigned long > begin_run_count_
unsigned int value() const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void postEventSetupGet(eventsetup::ComponentDescription const *, eventsetup::EventSetupRecordKey const &, eventsetup::DataKey const &)
std::atomic< unsigned long > total_event_count_
static std::vector< double > & moduleTimeStack()
void addToCPUTime(double iTime) override
static std::string d2str(double d)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
double postCommon() const
edm::serviceregistry::AllArgsMaker< edm::TimingServiceBase, Timing > TimingMaker
ProcessContext const * processContext() const
void postSourceRun(RunIndex)
void postModuleGlobal(GlobalContext const &, ModuleCallingContext const &)
void preBeginJob(PathsAndConsumesOfModulesBase const &, ProcessContext const &)
CountAndTime countAndTimeZero_
void postOpenFile(std::string const &, bool)
void postSourceEvent(StreamID)
EventID const & eventID() const
std::atomic< double > extra_job_cpu_
bool isSubProcess() const
void preSourceEvent(StreamID)
void preEvent(StreamContext const &)
std::vector< double > min_events_time_
unsigned int nSubProcesses_