16 #include "kmp_config.h" 17 #include "kmp_debug.h" 28 #include "kmp_stats_timing.h" 39 #define KMP_DEVELOPER_STATS 0 42 #define KMP_STATS_HIST 0 95 #define KMP_FOREACH_COUNTER(macro, arg) \ 96 macro(OMP_PARALLEL,stats_flags_e::onlyInMaster|stats_flags_e::noTotal,arg) \ 97 macro(OMP_NESTED_PARALLEL, 0, arg) \ 98 macro(OMP_LOOP_STATIC, 0, arg) \ 99 macro(OMP_LOOP_STATIC_STEAL, 0, arg) \ 100 macro(OMP_LOOP_DYNAMIC, 0, arg) \ 101 macro(OMP_DISTRIBUTE, 0, arg) \ 102 macro(OMP_BARRIER, 0, arg) \ 103 macro(OMP_CRITICAL, 0, arg) \ 104 macro(OMP_SINGLE, 0, arg) \ 105 macro(OMP_MASTER, 0, arg) \ 106 macro(OMP_TEAMS, 0, arg) \ 107 macro(OMP_set_lock, 0, arg) \ 108 macro(OMP_test_lock, 0, arg) \ 109 macro(REDUCE_wait, 0, arg) \ 110 macro(REDUCE_nowait, 0, arg) \ 111 macro(OMP_TASKYIELD, 0, arg) \ 112 macro(OMP_TASKLOOP, 0, arg) \ 113 macro(TASK_executed, 0, arg) \ 114 macro(TASK_cancelled, 0, arg) \ 115 macro(TASK_stolen, 0, arg) 137 #define KMP_FOREACH_TIMER(macro, arg) \ 138 macro (OMP_worker_thread_life, stats_flags_e::logEvent, arg) \ 139 macro (OMP_parallel, stats_flags_e::logEvent, arg) \ 140 macro (OMP_parallel_overhead, stats_flags_e::logEvent, arg) \ 141 macro (OMP_teams, stats_flags_e::logEvent, arg) \ 142 macro (OMP_teams_overhead, stats_flags_e::logEvent, arg) \ 143 macro (OMP_loop_static, 0, arg) \ 144 macro (OMP_loop_static_scheduling, 0, arg) \ 145 macro (OMP_loop_dynamic, 0, arg) \ 146 macro (OMP_loop_dynamic_scheduling, 0, arg) \ 147 macro (OMP_distribute, 0, arg) \ 148 macro (OMP_distribute_scheduling, 0, arg) \ 149 macro (OMP_critical, 0, arg) \ 150 macro (OMP_critical_wait, 0, arg) \ 151 macro (OMP_single, 0, arg) \ 152 macro (OMP_master, 0, arg) \ 153 macro (OMP_task_immediate, 0, arg) \ 154 macro (OMP_task_taskwait, 0, arg) \ 155 macro (OMP_task_taskyield, 0, arg) \ 156 macro (OMP_task_taskgroup, 0, arg) \ 157 macro (OMP_task_join_bar, 0, arg) \ 158 macro (OMP_task_plain_bar, 0, arg) \ 159 macro (OMP_taskloop_scheduling, 0, arg) \ 160 macro (OMP_plain_barrier, stats_flags_e::logEvent, arg) \ 161 macro (OMP_idle, stats_flags_e::logEvent, arg) \ 162 macro (OMP_fork_barrier, stats_flags_e::logEvent, arg) \ 163 macro (OMP_join_barrier, stats_flags_e::logEvent, arg) \ 164 macro (OMP_serial, stats_flags_e::logEvent, arg) \ 165 macro (OMP_set_numthreads, stats_flags_e::noUnits | stats_flags_e::noTotal, \ 167 macro (OMP_PARALLEL_args, stats_flags_e::noUnits | stats_flags_e::noTotal, \ 169 macro (OMP_loop_static_iterations, \ 170 stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \ 171 macro (OMP_loop_static_total_iterations, \ 172 stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \ 173 macro (OMP_loop_dynamic_iterations, \ 174 stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \ 175 macro (OMP_loop_dynamic_total_iterations, \ 176 stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \ 177 macro (OMP_distribute_iterations, \ 178 stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \ 179 KMP_FOREACH_DEVELOPER_TIMER(macro, arg) 230 #if (KMP_DEVELOPER_STATS) 247 #define KMP_FOREACH_DEVELOPER_TIMER(macro, arg) \ 248 macro(KMP_fork_call, 0, arg) \ 249 macro(KMP_join_call, 0, arg) \ 250 macro(KMP_end_split_barrier, 0, arg) \ 251 macro(KMP_hier_gather, 0, arg) \ 252 macro(KMP_hier_release, 0, arg) \ 253 macro(KMP_hyper_gather, 0, arg) \ 254 macro(KMP_hyper_release, 0, arg) \ 255 macro(KMP_linear_gather, 0, arg) \ 256 macro(KMP_linear_release, 0, arg) \ 257 macro(KMP_tree_gather, 0, arg) \ 258 macro(KMP_tree_release, 0, arg) \ 259 macro(USER_resume, 0, arg) \ 260 macro(USER_suspend, 0, arg) \ 261 macro(KMP_allocate_team, 0, arg) \ 262 macro(KMP_setup_icv_copy, 0, arg) \ 263 macro(USER_icv_copy, 0, arg) \ 264 macro (FOR_static_steal_stolen, \ 265 stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \ 266 macro (FOR_static_steal_chunks, \ 267 stats_flags_e::noUnits | stats_flags_e::noTotal, arg) 269 #define KMP_FOREACH_DEVELOPER_TIMER(macro, arg) 292 #define KMP_FOREACH_EXPLICIT_TIMER(macro, arg) KMP_FOREACH_TIMER(macro, arg) 294 #define ENUMERATE(name, ignore, prefix) prefix##name, 295 enum timer_e { KMP_FOREACH_TIMER(ENUMERATE, TIMER_) TIMER_LAST };
297 enum explicit_timer_e {
324 uint32_t KMP_ALIGN_CACHE zeroCount;
330 static double binMax[numBins];
336 uint64_t t = zeroCount;
337 for (
int i = 0; i < numBins; i++)
339 KMP_DEBUG_ASSERT(t == _total);
342 void check()
const {}
346 logHistogram() { reset(); }
348 logHistogram(logHistogram
const &o) {
349 for (
int i = 0; i < numBins; i++)
358 for (
int i = 0; i < numBins; i++) {
367 uint32_t count(
int b)
const {
return bins[b + logOffset].count; }
368 double total(
int b)
const {
return bins[b + logOffset].total; }
369 static uint32_t findBin(
double sample);
371 logHistogram &operator+=(logHistogram
const &o) {
372 zeroCount += o.zeroCount;
373 for (
int i = 0; i < numBins; i++) {
374 bins[i].count += o.bins[i].count;
375 bins[i].total += o.bins[i].total;
385 void addSample(
double sample);
389 std::string format(
char)
const;
393 double KMP_ALIGN_CACHE minVal;
397 uint64_t sampleCount;
403 statistic(
bool doHist =
bool(KMP_STATS_HIST)) {
405 collectingHist = doHist;
407 statistic(statistic
const &o)
408 : minVal(o.minVal), maxVal(o.maxVal), meanVal(o.meanVal), m2(o.m2),
409 sampleCount(o.sampleCount), offset(o.offset),
410 collectingHist(o.collectingHist), hist(o.hist) {}
411 statistic(
double minv,
double maxv,
double meanv, uint64_t sc,
double sd)
412 : minVal(minv), maxVal(maxv), meanVal(meanv), m2(sd * sd * sc),
413 sampleCount(sc), offset(0.0), collectingHist(
false) {}
414 bool haveHist()
const {
return collectingHist; }
415 double getMin()
const {
return minVal; }
416 double getMean()
const {
return meanVal; }
417 double getMax()
const {
return maxVal; }
418 uint64_t getCount()
const {
return sampleCount; }
419 double getSD()
const {
return sqrt(m2 / sampleCount); }
420 double getTotal()
const {
return sampleCount * meanVal; }
421 logHistogram
const *getHist()
const {
return &hist; }
422 void setOffset(
double d) { offset = d; }
425 minVal = std::numeric_limits<double>::max();
433 void addSample(
double sample);
434 void scale(
double factor);
435 void scaleDown(
double f) { scale(1. / f); }
436 void forceCount(uint64_t count) { sampleCount = count; }
437 statistic &operator+=(statistic
const &other);
439 std::string format(
char unit,
bool total =
false)
const;
440 std::string formatHist(
char unit)
const {
return hist.format(unit); }
448 class timeStat :
public statistic {
449 static statInfo timerInfo[];
452 timeStat() : statistic() {}
453 static const char *name(timer_e e) {
return timerInfo[e].name; }
454 static bool noTotal(timer_e e) {
457 static bool masterOnly(timer_e e) {
460 static bool workerOnly(timer_e e) {
463 static bool noUnits(timer_e e) {
469 static void clearEventFlags() {
470 for (
int i = 0; i < TIMER_LAST; i++) {
479 class explicitTimer {
481 timer_e timerEnumValue;
482 tsc_tick_count startTime;
483 tsc_tick_count pauseStartTime;
484 tsc_tick_count::tsc_interval_t totalPauseTime;
487 explicitTimer(timeStat *s, timer_e te)
488 : stat(s), timerEnumValue(te), startTime(), pauseStartTime(0),
492 void start(tsc_tick_count tick);
493 void pause(tsc_tick_count tick) { pauseStartTime = tick; }
494 void resume(tsc_tick_count tick) {
495 totalPauseTime += (tick - pauseStartTime);
497 void stop(tsc_tick_count tick, kmp_stats_list *stats_ptr =
nullptr);
503 timer_e get_type()
const {
return timerEnumValue; }
512 class partitionedTimers {
514 std::vector<explicitTimer> timer_stack;
518 void init(explicitTimer timer);
519 void exchange(explicitTimer timer);
520 void push(explicitTimer timer);
527 class blockPartitionedTimer {
528 partitionedTimers *part_timers;
531 blockPartitionedTimer(partitionedTimers *pt, explicitTimer timer)
533 part_timers->push(timer);
535 ~blockPartitionedTimer() { part_timers->pop(); }
541 class blockThreadState {
547 : state_pointer(thread_state_pointer), old_state(*thread_state_pointer) {
548 *state_pointer = new_state;
550 ~blockThreadState() { *state_pointer = old_state; }
558 static const statInfo counterInfo[];
561 counter() : value(0) {}
562 void increment() { value++; }
563 uint64_t getValue()
const {
return value; }
564 void reset() { value = 0; }
565 static const char *name(counter_e e) {
return counterInfo[e].name; }
566 static bool masterOnly(counter_e e) {
604 class kmp_stats_event {
612 : start(0), stop(0), nest_level(0), timer_name(TIMER_LAST) {}
613 kmp_stats_event(uint64_t strt, uint64_t stp,
int nst, timer_e nme)
614 : start(strt), stop(stp), nest_level(nst), timer_name(nme) {}
615 inline uint64_t getStart()
const {
return start; }
616 inline uint64_t getStop()
const {
return stop; }
617 inline int getNestLevel()
const {
return nest_level; }
618 inline timer_e getTimerName()
const {
return timer_name; }
647 class kmp_stats_event_vector {
648 kmp_stats_event *events;
651 static const int INIT_SIZE = 1024;
654 kmp_stats_event_vector() {
656 (kmp_stats_event *)__kmp_allocate(
sizeof(kmp_stats_event) * INIT_SIZE);
658 allocated_size = INIT_SIZE;
660 ~kmp_stats_event_vector() {}
661 inline void reset() { internal_size = 0; }
662 inline int size()
const {
return internal_size; }
663 void push_back(uint64_t start_time, uint64_t stop_time,
int nest_level,
666 if (internal_size == allocated_size) {
667 kmp_stats_event *tmp = (kmp_stats_event *)__kmp_allocate(
668 sizeof(kmp_stats_event) * allocated_size * 2);
669 for (i = 0; i < internal_size; i++)
675 events[internal_size] =
676 kmp_stats_event(start_time, stop_time, nest_level, name);
682 const kmp_stats_event &operator[](
int index)
const {
return events[index]; }
683 kmp_stats_event &operator[](
int index) {
return events[index]; }
684 const kmp_stats_event &at(
int index)
const {
return events[index]; }
685 kmp_stats_event &at(
int index) {
return events[index]; }
715 class kmp_stats_list {
717 timeStat _timers[TIMER_LAST + 1];
718 counter _counters[COUNTER_LAST + 1];
719 explicitTimer thread_life_timer;
720 partitionedTimers _partitionedTimers;
722 kmp_stats_event_vector _event_vector;
723 kmp_stats_list *next;
724 kmp_stats_list *prev;
726 int thread_is_idle_flag;
730 : thread_life_timer(&_timers[TIMER_OMP_worker_thread_life],
731 TIMER_OMP_worker_thread_life),
732 _nestLevel(0), _event_vector(), next(
this), prev(
this), state(IDLE),
733 thread_is_idle_flag(0) {}
735 inline timeStat *getTimer(timer_e idx) {
return &_timers[idx]; }
736 inline counter *getCounter(counter_e idx) {
return &_counters[idx]; }
737 inline partitionedTimers *getPartitionedTimers() {
738 return &_partitionedTimers;
740 inline timeStat *getTimers() {
return _timers; }
741 inline counter *getCounters() {
return _counters; }
742 inline kmp_stats_event_vector &getEventVector() {
return _event_vector; }
743 inline void startLife() { thread_life_timer.start(tsc_tick_count::now()); }
744 inline void endLife() { thread_life_timer.stop(tsc_tick_count::now(),
this); }
745 inline void resetEventVector() { _event_vector.reset(); }
746 inline void incrementNestValue() { _nestLevel++; }
747 inline int getNestValue() {
return _nestLevel; }
748 inline void decrementNestValue() { _nestLevel--; }
749 inline int getGtid()
const {
return gtid; }
750 inline void setGtid(
int newgtid) { gtid = newgtid; }
751 inline void setState(
stats_state_e newstate) { state = newstate; }
754 inline bool isIdle() {
return thread_is_idle_flag == 1; }
755 inline void setIdleFlag() { thread_is_idle_flag = 1; }
756 inline void resetIdleFlag() { thread_is_idle_flag = 0; }
757 kmp_stats_list *push_back(
int gtid);
758 inline void push_event(uint64_t start_time, uint64_t stop_time,
759 int nest_level, timer_e name) {
760 _event_vector.push_back(start_time, stop_time, nest_level, name);
764 kmp_stats_list::iterator begin();
765 kmp_stats_list::iterator end();
769 friend kmp_stats_list::iterator kmp_stats_list::begin();
770 friend kmp_stats_list::iterator kmp_stats_list::end();
775 iterator operator++();
776 iterator operator++(
int dummy);
777 iterator operator--();
778 iterator operator--(
int dummy);
779 bool operator!=(
const iterator &rhs);
780 bool operator==(
const iterator &rhs);
781 kmp_stats_list *operator*()
const;
814 class kmp_stats_output_module {
824 std::string outputFileName;
825 static const char *eventsFileName;
826 static const char *plotFileName;
827 static int printPerThreadFlag;
828 static int printPerThreadEventsFlag;
829 static const rgb_color globalColorArray[];
830 static rgb_color timerColorInfo[];
833 static void setupEventColors();
834 static void printPloticusFile();
835 static void printHeaderInfo(FILE *statsOut);
836 static void printTimerStats(FILE *statsOut, statistic
const *theStats,
837 statistic
const *totalStats);
838 static void printCounterStats(FILE *statsOut, statistic
const *theStats);
839 static void printCounters(FILE *statsOut, counter
const *theCounters);
840 static void printEvents(FILE *eventsOut, kmp_stats_event_vector *theEvents,
842 static rgb_color getEventColor(timer_e e) {
return timerColorInfo[e]; }
843 static void windupExplicitTimers();
844 bool eventPrintingEnabled()
const {
return printPerThreadEventsFlag; }
847 kmp_stats_output_module() { init(); }
848 void outputStats(
const char *heading);
854 void __kmp_stats_init();
855 void __kmp_stats_fini();
856 void __kmp_reset_stats();
857 void __kmp_output_stats(
const char *);
858 void __kmp_accumulate_stats_at_exit(
void);
860 extern KMP_THREAD_LOCAL kmp_stats_list *__kmp_stats_thread_ptr;
862 extern kmp_stats_list *__kmp_stats_list;
864 extern kmp_tas_lock_t __kmp_stats_lock;
866 extern tsc_tick_count __kmp_stats_start_time;
868 extern kmp_stats_output_module __kmp_stats_output;
887 #define KMP_COUNT_VALUE(name, value) \ 888 __kmp_stats_thread_ptr->getTimer(TIMER_##name)->addSample(value) 900 #define KMP_COUNT_BLOCK(name) \ 901 __kmp_stats_thread_ptr->getCounter(COUNTER_##name)->increment() 920 #define KMP_OUTPUT_STATS(heading_string) __kmp_output_stats(heading_string) 929 #define KMP_INIT_PARTITIONED_TIMERS(name) \ 930 __kmp_stats_thread_ptr->getPartitionedTimers()->init(explicitTimer( \ 931 __kmp_stats_thread_ptr->getTimer(TIMER_##name), TIMER_##name)) 933 #define KMP_TIME_PARTITIONED_BLOCK(name) \ 934 blockPartitionedTimer __PBLOCKTIME__( \ 935 __kmp_stats_thread_ptr->getPartitionedTimers(), \ 936 explicitTimer(__kmp_stats_thread_ptr->getTimer(TIMER_##name), \ 939 #define KMP_PUSH_PARTITIONED_TIMER(name) \ 940 __kmp_stats_thread_ptr->getPartitionedTimers()->push(explicitTimer( \ 941 __kmp_stats_thread_ptr->getTimer(TIMER_##name), TIMER_##name)) 943 #define KMP_POP_PARTITIONED_TIMER() \ 944 __kmp_stats_thread_ptr->getPartitionedTimers()->pop() 946 #define KMP_EXCHANGE_PARTITIONED_TIMER(name) \ 947 __kmp_stats_thread_ptr->getPartitionedTimers()->exchange(explicitTimer( \ 948 __kmp_stats_thread_ptr->getTimer(TIMER_##name), TIMER_##name)) 950 #define KMP_SET_THREAD_STATE(state_name) \ 951 __kmp_stats_thread_ptr->setState(state_name) 953 #define KMP_GET_THREAD_STATE() __kmp_stats_thread_ptr->getState() 955 #define KMP_SET_THREAD_STATE_BLOCK(state_name) \ 956 blockThreadState __BTHREADSTATE__(__kmp_stats_thread_ptr->getStatePointer(), \ 966 #define KMP_RESET_STATS() __kmp_reset_stats() 968 #if (KMP_DEVELOPER_STATS) 969 #define KMP_COUNT_DEVELOPER_VALUE(n, v) KMP_COUNT_VALUE(n, v) 970 #define KMP_COUNT_DEVELOPER_BLOCK(n) KMP_COUNT_BLOCK(n) 971 #define KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(n) KMP_TIME_PARTITIONED_BLOCK(n) 972 #define KMP_PUSH_DEVELOPER_PARTITIONED_TIMER(n) KMP_PUSH_PARTITIONED_TIMER(n) 973 #define KMP_POP_DEVELOPER_PARTITIONED_TIMER(n) KMP_POP_PARTITIONED_TIMER(n) 974 #define KMP_EXCHANGE_DEVELOPER_PARTITIONED_TIMER(n) \ 975 KMP_EXCHANGE_PARTITIONED_TIMER(n) 978 #define KMP_COUNT_DEVELOPER_VALUE(n, v) ((void)0) 979 #define KMP_COUNT_DEVELOPER_BLOCK(n) ((void)0) 980 #define KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(n) ((void)0) 981 #define KMP_PUSH_DEVELOPER_PARTITIONED_TIMER(n) ((void)0) 982 #define KMP_POP_DEVELOPER_PARTITIONED_TIMER(n) ((void)0) 983 #define KMP_EXCHANGE_DEVELOPER_PARTITIONED_TIMER(n) ((void)0) 986 #else // KMP_STATS_ENABLED 989 #define KMP_COUNT_VALUE(n, v) ((void)0) 990 #define KMP_COUNT_BLOCK(n) ((void)0) 992 #define KMP_OUTPUT_STATS(heading_string) ((void)0) 993 #define KMP_RESET_STATS() ((void)0) 995 #define KMP_COUNT_DEVELOPER_VALUE(n, v) ((void)0) 996 #define KMP_COUNT_DEVELOPER_BLOCK(n) ((void)0) 997 #define KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(n) ((void)0) 998 #define KMP_PUSH_DEVELOPER_PARTITIONED_TIMER(n) ((void)0) 999 #define KMP_POP_DEVELOPER_PARTITIONED_TIMER(n) ((void)0) 1000 #define KMP_EXCHANGE_DEVELOPER_PARTITIONED_TIMER(n) ((void)0) 1001 #define KMP_INIT_PARTITIONED_TIMERS(name) ((void)0) 1002 #define KMP_TIME_PARTITIONED_BLOCK(name) ((void)0) 1003 #define KMP_PUSH_PARTITIONED_TIMER(name) ((void)0) 1004 #define KMP_POP_PARTITIONED_TIMER() ((void)0) 1005 #define KMP_SET_THREAD_STATE(state_name) ((void)0) 1006 #define KMP_GET_THREAD_STATE() ((void)0) 1007 #define KMP_SET_THREAD_STATE_BLOCK(state_name) ((void)0) 1008 #endif // KMP_STATS_ENABLED 1010 #endif // KMP_STATS_H statistic is valid only for master
statistic is valid only for non-master threads
do not show a TOTAL_aggregation for this statistic
#define KMP_FOREACH_EXPLICIT_TIMER(macro, arg)
Add new explicit timers under KMP_FOREACH_EXPLICIT_TIMER() macro.
statistic doesn't need units printed next to it
stats_flags_e
flags to describe the statistic (timer or counter)
#define KMP_FOREACH_COUNTER(macro, arg)
Add new counters under KMP_FOREACH_COUNTER() macro in kmp_stats.h.
stats_state_e
the states which a thread can be in