10 #include "Teuchos_StackedTimer.hpp"
23 level_(std::numeric_limits<unsigned>::max()),name_(
"INVALID"),parent_(nullptr)
34 for (
unsigned i=0; i<level_; ++i)
36 os << name_<<
":"<<
accumulatedTime()<<
" [" << count_started_<<
"] ("<< count_updates_ <<
")"<<std::endl;
38 for (
size_t i=0; i<sub_timers_.size(); ++i) {
39 t_total += sub_timers_[i].accumulatedTime();
40 sub_timers_[i].report(os);
42 if ( sub_timers_.size() == 0 )
44 for (
unsigned i=0; i<=level_; ++i)
53 if (get_full_name() == name) {
57 for (
unsigned i=0;i<sub_timers_.size(); ++i){
58 t = sub_timers_[i].findBaseTimer(name);
68 BaseTimer::TimeInfo t;
69 auto full_name = get_full_name();
70 if (full_name.size() > name.size())
72 if ( strncmp(full_name.c_str(), name.c_str(), full_name.size()))
74 if (get_full_name() == name) {
75 t = BaseTimer::TimeInfo(
this);
79 for (
unsigned i=0;i<sub_timers_.size(); ++i){
80 t = sub_timers_[i].findTimer(name,found);
91 flat_names_.
resize(num_timers);
93 timer_.addTimerNames(flat_names_, pos);
100 flat_names_ = all_names;
106 int num_names = flat_names_.
size();
109 updates_.
resize(num_names);
110 active_.
resize(num_names);
112 if (options.output_minmax || options.output_histogram || options.output_proc_minmax) {
115 if ( options.output_minmax )
116 sum_sq_.
resize(num_names);
125 if (options.output_proc_minmax) {
126 procmin_.
resize(num_names);
127 procmax_.
resize(num_names);
131 if (options.output_histogram ) {
132 hist_.resize(options.num_histogram);
133 for (
int i=0;i<options.num_histogram ; ++i)
134 hist_[i].resize(num_names);
137 if (options.output_per_proc_stddev) {
138 per_proc_stddev_min_.
resize(num_names);
139 per_proc_stddev_max_.
resize(num_names);
146 if (options.output_total_updates)
147 updates.
resize(num_names);
151 if (options.output_per_proc_stddev)
152 per_proc_stddev.
resize(num_names);
154 if (options.output_histogram)
158 for (
int i=0;i<num_names; ++i) {
163 used[i] = t.count==0? 0:1;
164 if (options.output_total_updates)
165 updates[i] = t.updates;
166 if (options.output_per_proc_stddev)
167 per_proc_stddev[i] = t.stdDev;
177 for (
int i=0;i<num_names;++i)
181 for (
int i=0;i<num_names;++i)
184 if (procmin_.
size()) {
187 int commRank = comm->getRank();
188 for (
int i=0;i<num_names; ++i) {
189 if (used[i] && (min_[i]==time[i]))
190 procmin[i] = commRank;
193 if (used[i] && (max_[i]==time[i]))
194 procmax[i] = commRank;
203 if (options.output_histogram) {
204 for (
int i=0;i<num_names; ++i) {
206 double dh = (max_[i]-min_[i])/options.num_histogram;
210 int bin=(time[i]- min_[i])/dh;
211 bins[i] = std::max(std::min(bin,options.num_histogram-1) , 0);
216 for (
int j=0; j<options.num_histogram; ++j){
217 for (
int i=0;i<num_names; ++i) {
227 if (sum_sq_.
size()) {
228 for (
int i=0;i<num_names; ++i)
233 if (options.output_per_proc_stddev) {
240 std::pair<std::string, std::string> getPrefix(
const std::string &name) {
241 for (std::size_t i=name.size()-1; i>0; --i)
242 if (name[i] ==
'@') {
243 return std::pair<std::string, std::string>(name.substr(0,i), name.substr(i+1));
245 return std::pair<std::string, std::string>(std::string(
""), name);
251 std::vector<bool> &printed,
257 double total_time = 0.0;
259 for (
int i=0; i<flat_names_.
size(); ++i ) {
260 if (sum_[i]/active_[i] <= options.drop_time)
264 int level = std::count(flat_names_[i].begin(), flat_names_[i].end(),
'@');
265 if ( (level != print_level) || (level >= options.max_levels) )
267 auto split_names = getPrefix(flat_names_[i]);
268 if ( prefix != split_names.first)
273 std::ostringstream os;
274 for (
int l=0; l<level; ++l)
277 os << split_names.second <<
": ";
278 alignments_.timer_names_= std::max(alignments_.timer_names_,os.str().size());
283 std::ostringstream os;
284 os << sum_[i]/active_[i];
285 alignments_.average_time_ = std::max(alignments_.average_time_,os.str().size());
289 if ( options.output_fraction && parent_time>0) {
290 std::ostringstream os;
291 os <<
" - "<<sum_[i]/active_[i]/parent_time*100<<
"%";
292 alignments_.fraction_ = std::max(alignments_.fraction_,os.str().size());
297 std::ostringstream os;
298 os <<
" ["<<count_[i]/active_[i]<<
"]";
299 alignments_.count_ = std::max(alignments_.count_,os.str().size());
303 if ( options.output_total_updates) {
304 std::ostringstream os;
305 os <<
" ("<<updates_[i]/active_[i]<<
")";
306 alignments_.total_updates_ = std::max(alignments_.total_updates_,os.str().size());
310 if ( options.output_minmax && active_[i]>1) {
312 std::ostringstream os;
313 os <<
" {min=" << min_[i];
314 alignments_.min_ = std::max(alignments_.min_,os.str().size());
317 std::ostringstream os;
318 os <<
", max=" << max_[i];
321 alignments_.max_ = std::max(alignments_.max_,os.str().size());
323 if (procmin_.
size()) {
324 std::ostringstream os;
325 os <<
", proc min=" << procmin_[i];
328 alignments_.procmin_ = std::min(alignments_.procmin_,os.str().size());
330 if (procmax_.
size()) {
331 std::ostringstream os;
332 os <<
", proc max=" << procmax_[i];
335 alignments_.procmax_ = std::max(alignments_.procmax_,os.str().size());
338 std::ostringstream os;
339 os <<
", std dev=" << sqrt(std::max<double>(sum_sq_[i]-sum_[i]*sum_[i]/active_[i],0.0)/(active_[i]-1));
341 alignments_.stddev_ = std::max(alignments_.stddev_,os.str().size());
345 if ( options.output_histogram && active_[i] >1 ) {
346 std::ostringstream os;
348 for (
int h=0;h<options.num_histogram; ++h) {
350 os <<
", "<<hist_[h][i];
355 alignments_.histogram_ = std::max(alignments_.histogram_,os.str().size());
363 if (options.print_names_before_values) {
364 std::ostringstream tmp;
365 for (
int l=0; l<=level; ++l)
367 tmp <<
"Remainder: ";
368 alignments_.timer_names_ = std::max(alignments_.timer_names_,tmp.str().size());
371 std::ostringstream tmp;
372 tmp << sum_[i]/active_[i]- sub_time;
373 alignments_.average_time_ = std::max(alignments_.average_time_,tmp.str().size());
375 if ( options.output_fraction && (sum_[i]/active_[i] > 0.) ) {
376 std::ostringstream tmp;
377 tmp <<
" - "<< (sum_[i]/active_[i]- sub_time)/(sum_[i]/active_[i])*100 <<
"%";
378 alignments_.fraction_ = std::max(alignments_.fraction_,tmp.str().size());
382 total_time += sum_[i]/active_[i];
395 double total_time = 0.0;
397 for (
int i=0; i<flat_names_.
size(); ++i ) {
398 if (sum_[i]/active_[i] <= options.drop_time) {
403 int level = std::count(flat_names_[i].begin(), flat_names_[i].end(),
'@');
404 if ( (level != print_level) || (level >= options.max_levels) )
406 auto split_names = getPrefix(flat_names_[i]);
407 if ( prefix != split_names.first)
411 if (options.print_names_before_values) {
412 std::ostringstream tmp;
413 for (
int l=0; l<level; ++l) {
417 tmp << split_names.second <<
": ";
418 if (options.align_columns)
419 os << std::left << std::setw(alignments_.timer_names_);
424 std::ostringstream tmp;
425 tmp << sum_[i]/active_[i];
426 if (options.align_columns)
427 os << std::left << std::setw(alignments_.average_time_);
431 if ( options.output_fraction && parent_time>0) {
432 std::ostringstream tmp;
433 tmp <<
" - "<<sum_[i]/active_[i]/parent_time*100<<
"%";
434 if (options.align_columns)
435 os << std::left << std::setw(alignments_.fraction_);
439 else if (options.output_fraction) {
440 if (options.align_columns)
441 os << std::setw(alignments_.fraction_) <<
" ";
445 std::ostringstream tmp;
446 tmp <<
" ["<<count_[i]/active_[i]<<
"]";
447 if (options.align_columns)
448 os << std::left << std::setw(alignments_.count_);
452 if ( options.output_total_updates ) {
453 std::ostringstream tmp;
454 tmp <<
" ("<<updates_[i]/active_[i]<<
")";
455 if (options.align_columns)
456 os << std::left << std::setw(alignments_.total_updates_);
460 if ( options.output_minmax && active_[i]>1) {
462 std::ostringstream tmp;
463 tmp <<
" {min="<<min_[i];
464 if (options.align_columns)
465 os << std::left << std::setw(alignments_.min_);
469 std::ostringstream tmp;
470 tmp <<
", max="<<max_[i];
473 if (options.align_columns)
474 os << std::left << std::setw(alignments_.max_);
477 if (procmin_.
size()) {
478 std::ostringstream tmp;
479 tmp <<
", proc min="<<procmin_[i];
482 if (options.align_columns)
483 os << std::left << std::setw(alignments_.procmin_);
486 if (procmax_.
size()) {
487 std::ostringstream tmp;
488 tmp <<
", proc max="<<procmax_[i];
491 if (options.align_columns)
492 os << std::left << std::setw(alignments_.procmax_);
496 std::ostringstream tmp;
497 tmp <<
", std dev="<<sqrt(std::max<double>(sum_sq_[i]-sum_[i]*sum_[i]/active_[i],0.0)/(active_[i]-1));
499 if (options.align_columns)
500 os << std::left << std::setw(alignments_.stddev_);
504 else if ( options.output_minmax) {
506 size_t offset = alignments_.min_ + alignments_.max_ + alignments_.stddev_;
507 for (
size_t j=0; j < offset; ++j)
512 if ( options.output_histogram && active_[i] >1 ) {
513 std::ostringstream tmp;
515 for (
int h=0;h<options.num_histogram; ++h) {
517 tmp <<
", "<<hist_[h][i];
522 if (options.align_columns)
523 os << std::left << std::setw(alignments_.histogram_);
526 else if ( options.output_histogram) {
528 for (
size_t j=0; j < alignments_.histogram_; ++j)
532 if (options.output_per_proc_stddev) {
533 std::ostringstream tmp;
534 tmp <<
", std dev per proc min/max=";
535 tmp << per_proc_stddev_min_[i];
537 tmp << per_proc_stddev_max_[i];
541 if (! options.print_names_before_values) {
542 std::ostringstream tmp;
544 for (
int l=0; l<level; ++l) {
548 tmp << split_names.second <<
": ";
554 double sub_time =
printLevel(flat_names_[i], level+1, os, printed, sum_[i]/active_[i], options);
558 if (options.print_names_before_values) {
559 std::ostringstream tmp;
560 for (
int l=0; l<=level; ++l)
562 tmp <<
"Remainder: ";
563 if (options.align_columns)
564 os << std::left << std::setw(alignments_.timer_names_);
568 std::ostringstream tmp;
569 tmp << sum_[i]/active_[i]- sub_time;
570 if (options.align_columns)
571 os << std::left << std::setw(alignments_.average_time_);
574 if ( options.output_fraction && (sum_[i]/active_[i] > 0.) ) {
575 if (options.align_columns)
576 os << std::left << std::setw(alignments_.fraction_);
577 std::ostringstream tmp;
578 tmp <<
" - "<< (sum_[i]/active_[i]- sub_time)/(sum_[i]/active_[i])*100 <<
"%";
581 if (! options.print_names_before_values) {
584 offset += alignments_.count_;
585 if (options.output_total_updates)
586 offset += alignments_.total_updates_;
587 if (options.output_minmax)
588 offset += alignments_.min_ + alignments_.max_ + alignments_.stddev_;
589 if (options.output_histogram)
590 offset += alignments_.histogram_;
591 for (
size_t j=0; j < offset; ++j)
594 std::ostringstream tmp;
596 for (
int l=0; l<=level; ++l)
598 tmp <<
"Remainder: ";
599 if (options.align_columns)
600 os << std::left << std::setw(alignments_.timer_names_);
605 total_time += sum_[i]/active_[i];
610 static void printXMLEscapedString(std::ostream& os,
const std::string& str)
646 StackedTimer::printLevelXML (std::string prefix,
int print_level, std::ostream& os, std::vector<bool> &printed,
double parent_time,
const std::string& rootName)
648 constexpr
int indSpaces = 2;
649 int indent = indSpaces * print_level;
651 double total_time = 0.0;
653 for (
int i=0; i<flat_names_.
size(); ++i) {
656 int level = std::count(flat_names_[i].begin(), flat_names_[i].end(),
'@');
657 if ( level != print_level)
659 auto split_names = getPrefix(flat_names_[i]);
660 if ( prefix != split_names.first)
663 for (
int j = 0; j < indent; j++)
665 os <<
"<timing name=\"";
666 if(level == 0 && rootName.length())
667 printXMLEscapedString(os, rootName);
669 printXMLEscapedString(os, split_names.second);
670 os <<
"\" value=\"" << sum_[i]/active_[i] <<
"\"";
674 std::ostringstream osInner;
675 double sub_time =
printLevelXML(flat_names_[i], print_level+1, osInner, printed, sum_[i]/active_[i]);
676 std::string innerContents = osInner.str();
677 if(innerContents.length())
683 for (
int j = 0; j < indent + indSpaces; j++)
685 os <<
"<timing name=\"Remainder\" value=\"" << (sum_[i]/active_[i] - sub_time) <<
"\"/>\n";
688 for (
int j = 0; j < indent; j++)
697 total_time += sum_[i]/active_[i];
705 if (rank(*comm) == 0 ) {
706 if (options.print_warnings) {
707 os <<
"*** Teuchos::StackedTimer::report() - Remainder for a level will be ***"
708 <<
"\n*** incorrect if a timer in the level does not exist on every rank ***"
709 <<
"\n*** of the MPI Communicator. ***"
712 if ( (options.max_levels != INT_MAX) && options.print_warnings) {
713 os <<
"Teuchos::StackedTimer::report() - max_levels manually set to " << options.max_levels
714 <<
". \nTo print more levels, increase value of OutputOptions::max_levels." << std::endl;
716 if ( (! options.print_names_before_values) && (! options.align_columns)) {
717 options.align_columns =
true;
718 if (options.print_warnings)
719 os <<
"Teuchos::StackedTimer::report() - option print_names_before_values=false "
720 <<
"\nrequires that the option align_columns=true too. Setting the value for "
721 <<
"\nalign_column to true."
724 if (options.align_columns) {
725 std::vector<bool> printed(flat_names_.
size(),
false);
729 std::vector<bool> printed(flat_names_.
size(),
false);
739 if (rank(*comm) == 0 ) {
740 std::vector<bool> printed(flat_names_.
size(),
false);
741 os <<
"<?xml version=\"1.0\"?>\n";
742 os <<
"<performance-report date=\"" << timestamp <<
"\" name=\"nightly_run_" << datestamp <<
"\" time-units=\"seconds\">\n";
744 os <<
"</performance-report>\n";
750 const char* rawWatchrDir = getenv(
"WATCHR_PERF_DIR");
751 const char* rawBuildName = getenv(
"WATCHR_BUILD_NAME");
752 const char* rawGitSHA = getenv(
"TRILINOS_GIT_SHA");
753 const char* rawBuildDateOverride = getenv(
"WATCHR_BUILD_DATE");
757 std::string watchrDir = rawWatchrDir;
758 if(!watchrDir.length())
764 std::string buildName = rawBuildName ? rawBuildName :
"";
765 std::string datestamp;
766 std::string timestamp;
772 tstruct = gmtime(&t);
773 if(rawBuildDateOverride)
776 int year = 0, month = 0, day = 0;
777 sscanf(rawBuildDateOverride,
"%d_%d_%d", &year, &month, &day);
779 if(year <= 2000 || year > 2100)
780 throw std::invalid_argument(
"$WATCHR_BUILD_DATE has invalid year or is not in YYYY_MM_DD format.");
781 if(month < 1 || month > 12)
782 throw std::invalid_argument(
"$WATCHR_BUILD_DATE has invalid month or is not in YYYY_MM_DD format.");
783 if(day < 1 || day > 31)
784 throw std::invalid_argument(
"$WATCHR_BUILD_DATE has invalid day or is not in YYYY_MM_DD format.");
785 snprintf(buf, 256,
"%04d_%02d_%02d", year, month, day);
787 strftime(buf, 256,
"T%H:%M:%S", tstruct);
788 std::string justTime = buf;
789 snprintf(buf, 256,
"%04d-%02d-%02d", year, month, day);
790 timestamp = std::string(buf) + justTime;
794 strftime(buf, 256,
"%Y_%m_%d", tstruct);
796 strftime(buf, 256,
"%FT%H:%M:%S", tstruct);
802 std::string fullFile;
804 if(rank(*comm) == 0) {
805 std::string nameNoSpaces = name;
806 for(
char& c : nameNoSpaces)
811 if(buildName.length())
814 std::string buildNameNoSpaces = buildName;
815 for(
char& c : buildNameNoSpaces)
820 fullFile = watchrDir +
'/' + buildNameNoSpaces +
"-" + nameNoSpaces +
'_' + datestamp +
".xml";
823 fullFile = watchrDir +
'/' + nameNoSpaces +
'_' + datestamp +
".xml";
824 std::ofstream os(fullFile);
825 std::vector<bool> printed(flat_names_.
size(),
false);
826 os <<
"<?xml version=\"1.0\"?>\n";
827 os <<
"<performance-report date=\"" << timestamp <<
"\" name=\"nightly_run_" << datestamp <<
"\" time-units=\"seconds\">\n";
830 std::string gitSHA(rawGitSHA);
832 if(gitSHA.length() > 10)
833 gitSHA = gitSHA.substr(0, 10);
834 os <<
" <metadata key=\"Trilinos Version\" value=\"" << gitSHA <<
"\"/>\n";
836 printLevelXML(
"", 0, os, printed, 0.0, buildName +
": " + name);
837 os <<
"</performance-report>\n";
862 global_mpi_aggregation_called_ =
true;
868 return sum_[i] / active_[i];
874 return static_cast<double>(count_[i]) / static_cast<double>(active_[i]);
880 "ERROR: StackedTimer::getAverageMpiTime() - must call aggregateMpiData() first!");
882 auto search = std::find(flat_names_.
begin(),flat_names_.
end(),flat_timer_name);
885 "ERROR: StackedTimer::getAverageMpiTime() - the timer named \""
886 << flat_timer_name <<
"\" does not exist!");
888 auto i = std::distance(flat_names_.
begin(),search);
889 return static_cast<int>(i);
895 "ERROR: StackedTimer::isTimer() - must call aggregateMpiData() before using this query!");
897 auto search = std::find(flat_names_.
begin(),flat_names_.
end(),flat_timer_name);
898 return (search == flat_names_.
end()) ?
false :
true;
bool isTimer(const std::string &flat_timer_name)
void setVerboseOstream(const Teuchos::RCP< std::ostream > &os)
Set the ostream for verbose mode(defaults to std::cout).
void enableVerboseTimestamps(const unsigned levels)
Enable timestamps in verbose mode for the number of levels specified.
void enableVerbose(const bool enable_verbose)
If set to true, print timer start/stop to verbose ostream.
void merge(Teuchos::RCP< const Teuchos::Comm< int > > comm)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool enable_verbose_
If set to true, prints to the debug ostream. At construction, default value is set from environment v...
T * getRawPtr()
Return a raw pointer to beginning of array or NULL if unsized.
double printLevelXML(std::string prefix, int level, std::ostream &os, std::vector< bool > &printed, double parent_time, const std::string &rootName="")
double getMpiAverageCount(const std::string &flat_timer_name)
bool enable_timers_
Used to disable timers for asynchronous work.
std::string reportWatchrXML(const std::string &name, Teuchos::RCP< const Teuchos::Comm< int > > comm)
unsigned verbose_timestamp_levels_
If set to a value greater than 0, verbose mode will print that many levels of timers with timestamps...
BaseTimer::TimeInfo findTimer(const std::string &name, bool &found)
double printLevel(std::string prefix, int level, std::ostream &os, std::vector< bool > &printed, double parent_time, const OutputOptions &options)
double accumulatedTime(const std::string &name="")
void report(std::ostream &os)
void error_out(const std::string &msg, const bool)
Error reporting function for stacked timer.
The basic timer used internally, uses std::chrono::high_resolution_clock.
double computeColumnWidthsForAligment(std::string prefix, int print_level, std::vector< bool > &printed, double parent_time, const OutputOptions &options)
void reportXML(std::ostream &os, const std::string &datestamp, const std::string ×tamp, Teuchos::RCP< const Teuchos::Comm< int > > comm)
void collectRemoteData(Teuchos::RCP< const Teuchos::Comm< int > > comm, const OutputOptions &options)
void mergeCounterNames(const Comm< int > &comm, const Array< std::string > &localNames, Array< std::string > &globalNames, const ECounterSetOp setOp)
Merge counter names over all processors.
void resize(size_type new_size, const value_type &x=value_type())
Teuchos::RCP< std::ostream > verbose_ostream_
For debugging, this is the ostream used for printing.
LevelTimer timer_
Base timer.
double getMpiAverageTime(const std::string &flat_timer_name)
int getFlatNameIndex(const std::string &flat_timer_name)
LevelTimer()
Default constructor, shouldn't be used but needed for std::vector.
const BaseTimer * findBaseTimer(const std::string &name) const
void aggregateMpiData(Teuchos::RCP< const Teuchos::Comm< int > > comm, OutputOptions options=OutputOptions())
Smart reference counting pointer class for automatic garbage collection.