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
94 #define KMP_FOREACH_COUNTER(macro, arg) \
95 macro(OMP_PARALLEL,stats_flags_e::onlyInMaster|stats_flags_e::noTotal,arg) \
96 macro(OMP_NESTED_PARALLEL, 0, arg) \
97 macro(OMP_LOOP_STATIC, 0, arg) \
98 macro(OMP_LOOP_STATIC_STEAL, 0, arg) \
99 macro(OMP_LOOP_DYNAMIC, 0, arg) \
100 macro(OMP_DISTRIBUTE, 0, arg) \
101 macro(OMP_BARRIER, 0, arg) \
102 macro(OMP_CRITICAL, 0, arg) \
103 macro(OMP_SINGLE, 0, arg) \
104 macro(OMP_MASTER, 0, arg) \
105 macro(OMP_TEAMS, 0, arg) \
106 macro(OMP_set_lock, 0, arg) \
107 macro(OMP_test_lock, 0, arg) \
108 macro(REDUCE_wait, 0, arg) \
109 macro(REDUCE_nowait, 0, arg) \
110 macro(OMP_TASKYIELD, 0, arg) \
111 macro(OMP_TASKLOOP, 0, arg) \
112 macro(TASK_executed, 0, arg) \
113 macro(TASK_cancelled, 0, arg) \
114 macro(TASK_stolen, 0, arg)
136 #define KMP_FOREACH_TIMER(macro, arg) \
137 macro (OMP_worker_thread_life, stats_flags_e::logEvent, arg) \
138 macro (OMP_parallel, stats_flags_e::logEvent, arg) \
139 macro (OMP_parallel_overhead, stats_flags_e::logEvent, arg) \
140 macro (OMP_loop_static, 0, arg) \
141 macro (OMP_loop_static_scheduling, 0, arg) \
142 macro (OMP_loop_dynamic, 0, arg) \
143 macro (OMP_loop_dynamic_scheduling, 0, arg) \
144 macro (OMP_critical, 0, arg) \
145 macro (OMP_critical_wait, 0, arg) \
146 macro (OMP_single, 0, arg) \
147 macro (OMP_master, 0, arg) \
148 macro (OMP_task_immediate, 0, arg) \
149 macro (OMP_task_taskwait, 0, arg) \
150 macro (OMP_task_taskyield, 0, arg) \
151 macro (OMP_task_taskgroup, 0, arg) \
152 macro (OMP_task_join_bar, 0, arg) \
153 macro (OMP_task_plain_bar, 0, arg) \
154 macro (OMP_taskloop_scheduling, 0, arg) \
155 macro (OMP_plain_barrier, stats_flags_e::logEvent, arg) \
156 macro (OMP_idle, stats_flags_e::logEvent, arg) \
157 macro (OMP_fork_barrier, stats_flags_e::logEvent, arg) \
158 macro (OMP_join_barrier, stats_flags_e::logEvent, arg) \
159 macro (OMP_serial, stats_flags_e::logEvent, arg) \
160 macro (OMP_set_numthreads, stats_flags_e::noUnits | stats_flags_e::noTotal, \
162 macro (OMP_PARALLEL_args, stats_flags_e::noUnits | stats_flags_e::noTotal, \
164 macro (OMP_loop_static_iterations, \
165 stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \
166 macro (OMP_loop_dynamic_iterations, \
167 stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \
168 KMP_FOREACH_DEVELOPER_TIMER(macro, arg)
219 #if (KMP_DEVELOPER_STATS)
236 #define KMP_FOREACH_DEVELOPER_TIMER(macro, arg) \
237 macro(KMP_fork_call, 0, arg) \
238 macro(KMP_join_call, 0, arg) \
239 macro(KMP_end_split_barrier, 0, arg) \
240 macro(KMP_hier_gather, 0, arg) \
241 macro(KMP_hier_release, 0, arg) \
242 macro(KMP_hyper_gather, 0, arg) \
243 macro(KMP_hyper_release, 0, arg) \
244 macro(KMP_linear_gather, 0, arg) \
245 macro(KMP_linear_release, 0, arg) \
246 macro(KMP_tree_gather, 0, arg) \
247 macro(KMP_tree_release, 0, arg) \
248 macro(USER_resume, 0, arg) \
249 macro(USER_suspend, 0, arg) \
250 macro(KMP_allocate_team, 0, arg) \
251 macro(KMP_setup_icv_copy, 0, arg) \
252 macro(USER_icv_copy, 0, arg) \
253 macro (FOR_static_steal_stolen, \
254 stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \
255 macro (FOR_static_steal_chunks, \
256 stats_flags_e::noUnits | stats_flags_e::noTotal, arg)
258 #define KMP_FOREACH_DEVELOPER_TIMER(macro, arg)
281 #define KMP_FOREACH_EXPLICIT_TIMER(macro, arg) KMP_FOREACH_TIMER(macro, arg)
283 #define ENUMERATE(name, ignore, prefix) prefix##name,
284 enum timer_e { KMP_FOREACH_TIMER(ENUMERATE, TIMER_) TIMER_LAST };
286 enum explicit_timer_e {
313 uint32_t KMP_ALIGN_CACHE zeroCount;
319 static double binMax[numBins];
325 uint64_t t = zeroCount;
326 for (
int i = 0; i < numBins; i++)
328 KMP_DEBUG_ASSERT(t == _total);
331 void check()
const {}
335 logHistogram() { reset(); }
337 logHistogram(logHistogram
const &o) {
338 for (
int i = 0; i < numBins; i++)
347 for (
int i = 0; i < numBins; i++) {
356 uint32_t count(
int b)
const {
return bins[b + logOffset].count; }
357 double total(
int b)
const {
return bins[b + logOffset].total; }
358 static uint32_t findBin(
double sample);
360 logHistogram &operator+=(logHistogram
const &o) {
361 zeroCount += o.zeroCount;
362 for (
int i = 0; i < numBins; i++) {
363 bins[i].count += o.bins[i].count;
364 bins[i].total += o.bins[i].total;
374 void addSample(
double sample);
378 std::string format(
char)
const;
382 double KMP_ALIGN_CACHE minVal;
386 uint64_t sampleCount;
392 statistic(
bool doHist =
bool(KMP_STATS_HIST)) {
394 collectingHist = doHist;
396 statistic(statistic
const &o)
397 : minVal(o.minVal), maxVal(o.maxVal), meanVal(o.meanVal), m2(o.m2),
398 sampleCount(o.sampleCount), offset(o.offset),
399 collectingHist(o.collectingHist), hist(o.hist) {}
400 statistic(
double minv,
double maxv,
double meanv, uint64_t sc,
double sd)
401 : minVal(minv), maxVal(maxv), meanVal(meanv), m2(sd * sd * sc),
402 sampleCount(sc), offset(0.0), collectingHist(false) {}
403 bool haveHist()
const {
return collectingHist; }
404 double getMin()
const {
return minVal; }
405 double getMean()
const {
return meanVal; }
406 double getMax()
const {
return maxVal; }
407 uint64_t getCount()
const {
return sampleCount; }
408 double getSD()
const {
return sqrt(m2 / sampleCount); }
409 double getTotal()
const {
return sampleCount * meanVal; }
410 logHistogram
const *getHist()
const {
return &hist; }
411 void setOffset(
double d) { offset = d; }
414 minVal = std::numeric_limits<double>::max();
422 void addSample(
double sample);
423 void scale(
double factor);
424 void scaleDown(
double f) { scale(1. / f); }
425 void forceCount(uint64_t count) { sampleCount = count; }
426 statistic &operator+=(statistic
const &other);
428 std::string format(
char unit,
bool total =
false)
const;
429 std::string formatHist(
char unit)
const {
return hist.format(unit); }
437 class timeStat :
public statistic {
438 static statInfo timerInfo[];
441 timeStat() : statistic() {}
442 static const char *name(timer_e e) {
return timerInfo[e].name; }
443 static bool noTotal(timer_e e) {
446 static bool masterOnly(timer_e e) {
449 static bool workerOnly(timer_e e) {
452 static bool noUnits(timer_e e) {
458 static void clearEventFlags() {
459 for (
int i = 0; i < TIMER_LAST; i++) {
468 class explicitTimer {
470 timer_e timerEnumValue;
471 tsc_tick_count startTime;
472 tsc_tick_count pauseStartTime;
473 tsc_tick_count::tsc_interval_t totalPauseTime;
476 explicitTimer(timeStat *s, timer_e te)
477 : stat(s), timerEnumValue(te), startTime(), pauseStartTime(0),
481 void start(tsc_tick_count tick);
482 void pause(tsc_tick_count tick) { pauseStartTime = tick; }
483 void resume(tsc_tick_count tick) {
484 totalPauseTime += (tick - pauseStartTime);
486 void stop(tsc_tick_count tick, kmp_stats_list *stats_ptr =
nullptr);
492 timer_e get_type()
const {
return timerEnumValue; }
501 class partitionedTimers {
503 std::vector<explicitTimer> timer_stack;
507 void init(explicitTimer timer);
508 void exchange(explicitTimer timer);
509 void push(explicitTimer timer);
516 class blockPartitionedTimer {
517 partitionedTimers *part_timers;
520 blockPartitionedTimer(partitionedTimers *pt, explicitTimer timer)
522 part_timers->push(timer);
524 ~blockPartitionedTimer() { part_timers->pop(); }
530 class blockThreadState {
536 : state_pointer(thread_state_pointer), old_state(*thread_state_pointer) {
537 *state_pointer = new_state;
539 ~blockThreadState() { *state_pointer = old_state; }
547 static const statInfo counterInfo[];
550 counter() : value(0) {}
551 void increment() { value++; }
552 uint64_t getValue()
const {
return value; }
553 void reset() { value = 0; }
554 static const char *name(counter_e e) {
return counterInfo[e].name; }
555 static bool masterOnly(counter_e e) {
593 class kmp_stats_event {
601 : start(0), stop(0), nest_level(0), timer_name(TIMER_LAST) {}
602 kmp_stats_event(uint64_t strt, uint64_t stp,
int nst, timer_e nme)
603 : start(strt), stop(stp), nest_level(nst), timer_name(nme) {}
604 inline uint64_t getStart()
const {
return start; }
605 inline uint64_t getStop()
const {
return stop; }
606 inline int getNestLevel()
const {
return nest_level; }
607 inline timer_e getTimerName()
const {
return timer_name; }
636 class kmp_stats_event_vector {
637 kmp_stats_event *events;
640 static const int INIT_SIZE = 1024;
643 kmp_stats_event_vector() {
645 (kmp_stats_event *)__kmp_allocate(
sizeof(kmp_stats_event) * INIT_SIZE);
647 allocated_size = INIT_SIZE;
649 ~kmp_stats_event_vector() {}
650 inline void reset() { internal_size = 0; }
651 inline int size()
const {
return internal_size; }
652 void push_back(uint64_t start_time, uint64_t stop_time,
int nest_level,
655 if (internal_size == allocated_size) {
656 kmp_stats_event *tmp = (kmp_stats_event *)__kmp_allocate(
657 sizeof(kmp_stats_event) * allocated_size * 2);
658 for (i = 0; i < internal_size; i++)
664 events[internal_size] =
665 kmp_stats_event(start_time, stop_time, nest_level, name);
671 const kmp_stats_event &operator[](
int index)
const {
return events[index]; }
672 kmp_stats_event &operator[](
int index) {
return events[index]; }
673 const kmp_stats_event &at(
int index)
const {
return events[index]; }
674 kmp_stats_event &at(
int index) {
return events[index]; }
704 class kmp_stats_list {
706 timeStat _timers[TIMER_LAST + 1];
707 counter _counters[COUNTER_LAST + 1];
708 explicitTimer thread_life_timer;
709 partitionedTimers _partitionedTimers;
711 kmp_stats_event_vector _event_vector;
712 kmp_stats_list *next;
713 kmp_stats_list *prev;
715 int thread_is_idle_flag;
719 : thread_life_timer(&_timers[TIMER_OMP_worker_thread_life],
720 TIMER_OMP_worker_thread_life),
721 _nestLevel(0), _event_vector(), next(this), prev(this), state(IDLE),
722 thread_is_idle_flag(0) {}
724 inline timeStat *getTimer(timer_e idx) {
return &_timers[idx]; }
725 inline counter *getCounter(counter_e idx) {
return &_counters[idx]; }
726 inline partitionedTimers *getPartitionedTimers() {
727 return &_partitionedTimers;
729 inline timeStat *getTimers() {
return _timers; }
730 inline counter *getCounters() {
return _counters; }
731 inline kmp_stats_event_vector &getEventVector() {
return _event_vector; }
732 inline void startLife() { thread_life_timer.start(tsc_tick_count::now()); }
733 inline void endLife() { thread_life_timer.stop(tsc_tick_count::now(),
this); }
734 inline void resetEventVector() { _event_vector.reset(); }
735 inline void incrementNestValue() { _nestLevel++; }
736 inline int getNestValue() {
return _nestLevel; }
737 inline void decrementNestValue() { _nestLevel--; }
738 inline int getGtid()
const {
return gtid; }
739 inline void setGtid(
int newgtid) { gtid = newgtid; }
740 inline void setState(
stats_state_e newstate) { state = newstate; }
743 inline bool isIdle() {
return thread_is_idle_flag == 1; }
744 inline void setIdleFlag() { thread_is_idle_flag = 1; }
745 inline void resetIdleFlag() { thread_is_idle_flag = 0; }
746 kmp_stats_list *push_back(
int gtid);
747 inline void push_event(uint64_t start_time, uint64_t stop_time,
748 int nest_level, timer_e name) {
749 _event_vector.push_back(start_time, stop_time, nest_level, name);
753 kmp_stats_list::iterator begin();
754 kmp_stats_list::iterator end();
758 friend kmp_stats_list::iterator kmp_stats_list::begin();
759 friend kmp_stats_list::iterator kmp_stats_list::end();
764 iterator operator++();
765 iterator operator++(
int dummy);
766 iterator operator--();
767 iterator operator--(
int dummy);
768 bool operator!=(
const iterator &rhs);
769 bool operator==(
const iterator &rhs);
770 kmp_stats_list *operator*()
const;
803 class kmp_stats_output_module {
813 std::string outputFileName;
814 static const char *eventsFileName;
815 static const char *plotFileName;
816 static int printPerThreadFlag;
817 static int printPerThreadEventsFlag;
818 static const rgb_color globalColorArray[];
819 static rgb_color timerColorInfo[];
822 static void setupEventColors();
823 static void printPloticusFile();
824 static void printHeaderInfo(FILE *statsOut);
825 static void printTimerStats(FILE *statsOut, statistic
const *theStats,
826 statistic
const *totalStats);
827 static void printCounterStats(FILE *statsOut, statistic
const *theStats);
828 static void printCounters(FILE *statsOut, counter
const *theCounters);
829 static void printEvents(FILE *eventsOut, kmp_stats_event_vector *theEvents,
831 static rgb_color getEventColor(timer_e e) {
return timerColorInfo[e]; }
832 static void windupExplicitTimers();
833 bool eventPrintingEnabled()
const {
return printPerThreadEventsFlag; }
836 kmp_stats_output_module() { init(); }
837 void outputStats(
const char *heading);
843 void __kmp_stats_init();
844 void __kmp_stats_fini();
845 void __kmp_reset_stats();
846 void __kmp_output_stats(
const char *);
847 void __kmp_accumulate_stats_at_exit(
void);
849 extern KMP_THREAD_LOCAL kmp_stats_list *__kmp_stats_thread_ptr;
851 extern kmp_stats_list *__kmp_stats_list;
853 extern kmp_tas_lock_t __kmp_stats_lock;
855 extern tsc_tick_count __kmp_stats_start_time;
857 extern kmp_stats_output_module __kmp_stats_output;
876 #define KMP_COUNT_VALUE(name, value) \
877 __kmp_stats_thread_ptr->getTimer(TIMER_##name)->addSample(value)
889 #define KMP_COUNT_BLOCK(name) \
890 __kmp_stats_thread_ptr->getCounter(COUNTER_##name)->increment()
909 #define KMP_OUTPUT_STATS(heading_string) __kmp_output_stats(heading_string)
918 #define KMP_INIT_PARTITIONED_TIMERS(name) \
919 __kmp_stats_thread_ptr->getPartitionedTimers()->init(explicitTimer( \
920 __kmp_stats_thread_ptr->getTimer(TIMER_##name), TIMER_##name))
922 #define KMP_TIME_PARTITIONED_BLOCK(name) \
923 blockPartitionedTimer __PBLOCKTIME__( \
924 __kmp_stats_thread_ptr->getPartitionedTimers(), \
925 explicitTimer(__kmp_stats_thread_ptr->getTimer(TIMER_##name), \
928 #define KMP_PUSH_PARTITIONED_TIMER(name) \
929 __kmp_stats_thread_ptr->getPartitionedTimers()->push(explicitTimer( \
930 __kmp_stats_thread_ptr->getTimer(TIMER_##name), TIMER_##name))
932 #define KMP_POP_PARTITIONED_TIMER() \
933 __kmp_stats_thread_ptr->getPartitionedTimers()->pop()
935 #define KMP_EXCHANGE_PARTITIONED_TIMER(name) \
936 __kmp_stats_thread_ptr->getPartitionedTimers()->exchange(explicitTimer( \
937 __kmp_stats_thread_ptr->getTimer(TIMER_##name), TIMER_##name))
939 #define KMP_SET_THREAD_STATE(state_name) \
940 __kmp_stats_thread_ptr->setState(state_name)
942 #define KMP_GET_THREAD_STATE() __kmp_stats_thread_ptr->getState()
944 #define KMP_SET_THREAD_STATE_BLOCK(state_name) \
945 blockThreadState __BTHREADSTATE__(__kmp_stats_thread_ptr->getStatePointer(), \
955 #define KMP_RESET_STATS() __kmp_reset_stats()
957 #if (KMP_DEVELOPER_STATS)
958 #define KMP_TIME_DEVELOPER_BLOCK(n) KMP_TIME_BLOCK(n)
959 #define KMP_COUNT_DEVELOPER_VALUE(n, v) KMP_COUNT_VALUE(n, v)
960 #define KMP_COUNT_DEVELOPER_BLOCK(n) KMP_COUNT_BLOCK(n)
961 #define KMP_START_DEVELOPER_EXPLICIT_TIMER(n) KMP_START_EXPLICIT_TIMER(n)
962 #define KMP_STOP_DEVELOPER_EXPLICIT_TIMER(n) KMP_STOP_EXPLICIT_TIMER(n)
963 #define KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(n) KMP_TIME_PARTITIONED_BLOCK(n)
966 #define KMP_TIME_DEVELOPER_BLOCK(n) ((void)0)
967 #define KMP_COUNT_DEVELOPER_VALUE(n, v) ((void)0)
968 #define KMP_COUNT_DEVELOPER_BLOCK(n) ((void)0)
969 #define KMP_START_DEVELOPER_EXPLICIT_TIMER(n) ((void)0)
970 #define KMP_STOP_DEVELOPER_EXPLICIT_TIMER(n) ((void)0)
971 #define KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(n) ((void)0)
974 #else // KMP_STATS_ENABLED
977 #define KMP_TIME_BLOCK(n) ((void)0)
978 #define KMP_COUNT_VALUE(n, v) ((void)0)
979 #define KMP_COUNT_BLOCK(n) ((void)0)
980 #define KMP_START_EXPLICIT_TIMER(n) ((void)0)
981 #define KMP_STOP_EXPLICIT_TIMER(n) ((void)0)
983 #define KMP_OUTPUT_STATS(heading_string) ((void)0)
984 #define KMP_RESET_STATS() ((void)0)
986 #define KMP_TIME_DEVELOPER_BLOCK(n) ((void)0)
987 #define KMP_COUNT_DEVELOPER_VALUE(n, v) ((void)0)
988 #define KMP_COUNT_DEVELOPER_BLOCK(n) ((void)0)
989 #define KMP_START_DEVELOPER_EXPLICIT_TIMER(n) ((void)0)
990 #define KMP_STOP_DEVELOPER_EXPLICIT_TIMER(n) ((void)0)
991 #define KMP_INIT_PARTITIONED_TIMERS(name) ((void)0)
992 #define KMP_TIME_PARTITIONED_BLOCK(name) ((void)0)
993 #define KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(n) ((void)0)
994 #define KMP_PUSH_PARTITIONED_TIMER(name) ((void)0)
995 #define KMP_POP_PARTITIONED_TIMER() ((void)0)
996 #define KMP_SET_THREAD_STATE(state_name) ((void)0)
997 #define KMP_GET_THREAD_STATE() ((void)0)
998 #define KMP_SET_THREAD_STATE_BLOCK(state_name) ((void)0)
999 #endif // KMP_STATS_ENABLED
1001 #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