34 #include <sys/resource.h> 41 namespace eventsetup {
42 struct ComponentDescription;
147 if (gettimeofday(&
t,
nullptr) < 0)
148 throw cms::Exception(
"SysCallFailed",
"Failed call to gettimeofday");
149 return static_cast<double>(
t.tv_sec) + (static_cast<double>(
t.tv_usec) * 1E-6);
155 getrusage(RUSAGE_CHILDREN, &
usage);
156 double totalCPUTime = (double)
usage.ru_utime.tv_sec + (
double(
usage.ru_utime.tv_usec) * 1E-6);
157 totalCPUTime += (double)
usage.ru_stime.tv_sec + (
double(
usage.ru_stime.tv_usec) * 1E-6);
164 getrusage(RUSAGE_SELF, &
usage);
166 double totalCPUTime = 0.0;
168 totalCPUTime = (double)
usage.ru_utime.tv_sec + (
double(
usage.ru_utime.tv_usec) * 1E-6);
170 totalCPUTime += (double)
usage.ru_stime.tv_sec + (
double(
usage.ru_stime.tv_usec) * 1E-6);
173 getrusage(RUSAGE_CHILDREN, &
usage);
174 totalCPUTime += (double)
usage.ru_utime.tv_sec + (
double(
usage.ru_utime.tv_usec) * 1E-6);
175 totalCPUTime += (double)
usage.ru_stime.tv_sec + (
double(
usage.ru_stime.tv_usec) * 1E-6);
184 static thread_local std::vector<double> s_stack;
190 assert(!modStack.empty());
191 double curr_module_time = modStack.back();
193 double t =
getTime() - curr_module_time;
197 static void pushStack(
bool configuredInTopLevelProcess) {
198 if (!configuredInTopLevelProcess) {
206 : curr_job_time_(0.),
212 summary_only_(iPS.getUntrackedParameter<
bool>(
"summaryOnly")),
213 report_summary_(iPS.getUntrackedParameter<
bool>(
"useJobReport")),
214 threshold_(iPS.getUntrackedParameter<double>(
"excessiveTimeThreshold")),
217 total_event_count_(0),
218 begin_lumi_count_(0),
220 configuredInTopLevelProcess_{
false},
224 iRegistry.preEndJobSignal_.connect([
this]() {
233 bool checkThreshold =
true;
234 if (threshold_ <= 0.0) {
237 checkThreshold =
false;
240 if ((not summary_only_) || (checkThreshold)) {
244 if (checkThreshold) {
296 iRegistry.preESModuleSignal_.connect([
this](
auto const& recordKey,
auto const&
context) {
299 bool foundSlot =
false;
301 for (
size_t i = 0;
i < eventSetupModuleStartTimes_.size(); ++
i) {
302 auto& slot = *eventSetupModuleStartTimes_[
i];
304 if (slot.compare_exchange_strong(expect,
startTime)) {
306 eventSetupModuleCallInfo_[
i].first = uintptr_t(
context.componentDescription());
307 eventSetupModuleCallInfo_[
i].second = recordKey;
313 }
while (not foundSlot);
315 iRegistry.postESModuleSignal_.connect([
this](
auto const& recordKey,
auto const&
context) {
317 for (
size_t i = 0;
i < eventSetupModuleStartTimes_.size(); ++
i) {
318 auto const&
info = eventSetupModuleCallInfo_[
i];
319 if (
info.first == uintptr_t(
context.componentDescription()) and
info.second == recordKey) {
320 auto startTime = eventSetupModuleStartTimes_[
i]->exchange(0.);
321 auto expect = accumulatedEventSetupModuleTimings_.load();
323 auto accumulatedTime = expect + timeDiff;
324 while (not accumulatedEventSetupModuleTimings_.compare_exchange_strong(expect, accumulatedTime)) {
325 accumulatedTime = expect + timeDiff;
334 iRegistry.preallocateSignal_.connect([
this](service::SystemBounds
const& iBounds) {
335 nStreams_ = iBounds.maxNumberOfStreams();
336 nThreads_ = iBounds.maxNumberOfThreads();
337 curr_events_time_.resize(nStreams_, 0.);
338 sum_events_time_.resize(nStreams_, 0.);
339 max_events_time_.resize(nStreams_, 0.);
340 min_events_time_.resize(nStreams_, 1.E6);
341 eventSetupModuleStartTimes_.reserve(nThreads_);
342 for (
unsigned int i = 0;
i < nThreads_; ++
i) {
343 eventSetupModuleStartTimes_.emplace_back(std::make_unique<std::atomic<double>>(0.));
345 eventSetupModuleCallInfo_.resize(nThreads_);
347 for (
unsigned int i = 0;
i < nStreams_; ++
i) {
348 countSubProcessesPreEvent_.emplace_back(std::make_unique<std::atomic<unsigned int>>(0));
349 countSubProcessesPostEvent_.emplace_back(std::make_unique<std::atomic<unsigned int>>(0));
359 while (not
extra_job_cpu_.compare_exchange_strong(expected, expected + iTime)) {
367 desc.addUntracked<
bool>(
"summaryOnly",
false)->setComment(
"If 'true' do not report timing for each event");
368 desc.addUntracked<
bool>(
"useJobReport",
true)->setComment(
"If 'true' write summary information to JobReport");
369 desc.addUntracked<
double>(
"excessiveTimeThreshold", 0.)
371 "Amount of time in seconds before reporting a module or source has taken excessive time. A value of 0.0 " 372 "turns off this reporting.");
373 descriptions.
add(
"Timing",
desc);
374 descriptions.
setComment(
"This service reports the time it takes to run each module in a job.");
393 LogImportant(
"TimeReport") <<
"TimeReport> Report activated" 395 <<
"TimeReport> Report columns headings for events: " 396 <<
"eventnum runnum timetaken\n" 397 <<
"TimeReport> Report columns headings for modules: " 398 <<
"eventnum runnum modulelabel modulename timetakeni\n" 405 LogImportant(
"TimeReport") <<
"\nTimeReport> This instance of the Timing Service will be disabled because it " 406 "is configured in a SubProcess.\n" 407 <<
"If multiple instances of the TimingService were configured only the one in the " 408 "top level process will function.\n" 409 <<
"The other instance(s) will simply print this message and do nothing.\n\n";
413 const double job_end_time =
getTime();
414 const double job_end_cpu =
getCPU();
437 total_loop_time = 0.0;
438 total_loop_cpu = 0.0;
441 double sum_all_events_time = 0;
443 sum_all_events_time +=
t;
446 double average_event_time = 0.0;
451 double event_throughput = 0.0;
452 if (total_loop_time != 0.0) {
456 LogImportant(
"TimeReport") <<
"TimeReport> Time report complete in " << total_job_time <<
" seconds" 458 <<
" Time Summary: \n" 459 <<
" - Min event: " << min_event_time <<
"\n" 460 <<
" - Max event: " << max_event_time <<
"\n" 461 <<
" - Avg event: " << average_event_time <<
"\n" 462 <<
" - Total loop: " << total_loop_time <<
"\n" 463 <<
" - Total init: " << total_initialization_time <<
"\n" 464 <<
" - Total job: " << total_job_time <<
"\n" 466 <<
" Event Throughput: " << event_throughput <<
" ev/s\n" 467 <<
" CPU Summary: \n" 468 <<
" - Total loop: " << total_loop_cpu <<
"\n" 469 <<
" - Total init: " << total_initialization_cpu <<
"\n" 471 <<
" - Total children: " << job_end_children_cpu <<
"\n" 472 <<
" - Total job: " << total_job_cpu <<
"\n" 473 <<
" Processing Summary: \n" 480 std::map<std::string, std::string> reportData;
482 reportData.insert(std::make_pair(
"MinEventTime",
d2str(min_event_time)));
483 reportData.insert(std::make_pair(
"MaxEventTime",
d2str(max_event_time)));
484 reportData.insert(std::make_pair(
"AvgEventTime",
d2str(average_event_time)));
485 reportData.insert(std::make_pair(
"EventThroughput",
d2str(event_throughput)));
486 reportData.insert(std::make_pair(
"TotalJobTime",
d2str(total_job_time)));
487 reportData.insert(std::make_pair(
"TotalJobCPU",
d2str(total_job_cpu)));
488 reportData.insert(std::make_pair(
"TotalJobChildrenCPU",
d2str(job_end_children_cpu)));
489 reportData.insert(std::make_pair(
"TotalLoopTime",
d2str(total_loop_time)));
491 reportData.insert(std::make_pair(
"TotalLoopCPU",
d2str(total_loop_cpu)));
492 reportData.insert(std::make_pair(
"TotalInitTime",
d2str(total_initialization_time)));
493 reportData.insert(std::make_pair(
"TotalInitCPU",
d2str(total_initialization_cpu)));
494 reportData.insert(std::make_pair(
"NumberOfStreams",
ui2str(
nStreams_)));
495 reportData.insert(std::make_pair(
"NumberOfThreads",
ui2str(
nThreads_)));
496 reportSvc->reportPerformanceSummary(
"Timing", reportData);
498 std::map<std::string, std::string> reportData1;
502 reportSvc->reportPerformanceSummary(
"ProcessingSummary", reportData1);
543 auto const& eventID = iStream.
eventID();
544 LogPrint(
"TimeEvent") <<
"TimeEvent> " << eventID.event() <<
" " << eventID.run() <<
" " << curr_event_time;
557 auto const& eventID = iStream.
eventID();
561 LogPrint(
"TimeModule") <<
"TimeModule> " << eventID.event() <<
" " << eventID.run() <<
" " <<
desc.moduleLabel()
562 <<
" " <<
desc.moduleName() <<
" " <<
t;
623 <<
"ExcessiveTime: Module used " <<
t 624 <<
" seconds of time which exceeds the error threshold configured in the Timing Service of " <<
threshold_ std::vector< double > sum_events_time_
ModuleDescription const * moduleDescription() const
#define DEFINE_FWK_SERVICE_MAKER(concrete, maker)
std::vector< double > max_events_time_
std::vector< double > curr_events_time_
void preModule(ModuleDescription const &md)
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)
std::vector< std::unique_ptr< std::atomic< double > > > eventSetupModuleStartTimes_
Log< level::Error, false > LogError
void preSourceLumi(LuminosityBlockIndex)
void preModuleGlobal(GlobalContext const &, ModuleCallingContext const &)
void postModule(ModuleDescription const &md)
void postGlobalBeginRun(GlobalContext const &)
void preSourceRun(RunIndex)
double postCommon() const
void postModuleStream(StreamContext const &, ModuleCallingContext const &)
void preOpenFile(std::string const &)
std::atomic< double > end_loop_time_
Timing(ParameterSet const &, ActivityRegistry &)
StreamID const & streamID() const
void postModuleEvent(StreamContext const &, ModuleCallingContext const &)
Log< level::Error, true > LogImportant
void postSourceLumi(LuminosityBlockIndex)
static void pushStack(bool configuredInTopLevelProcess)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::atomic< double > end_loop_cpu_
ProcessContext const * processContext() const
Log< level::Warning, true > LogPrint
double getTotalCPU() const override
std::vector< std::unique_ptr< std::atomic< unsigned int > > > countSubProcessesPreEvent_
static double jobStartTime()
std::vector< std::unique_ptr< std::atomic< unsigned int > > > countSubProcessesPostEvent_
static std::string ui2str(unsigned int i)
void postOpenFile(std::string const &)
void setComment(std::string const &value)
void postGlobalBeginLumi(GlobalContext const &)
std::atomic< unsigned long > begin_run_count_
std::atomic< double > accumulatedEventSetupModuleTimings_
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)
bool configuredInTopLevelProcess_
edm::serviceregistry::AllArgsMaker< edm::TimingServiceBase, Timing > TimingMaker
void postSourceRun(RunIndex)
void postModuleGlobal(GlobalContext const &, ModuleCallingContext const &)
void preBeginJob(PathsAndConsumesOfModulesBase const &, ProcessContext const &)
bool isSubProcess() const
EventID const & eventID() const
void postSourceEvent(StreamID)
unsigned int value() const
std::atomic< double > extra_job_cpu_
void preSourceEvent(StreamID)
void preEvent(StreamContext const &)
static double getChildrenCPU()
std::vector< std::pair< uintptr_t, eventsetup::EventSetupRecordKey > > eventSetupModuleCallInfo_
std::vector< double > min_events_time_
unsigned int nSubProcesses_