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(USER_mwait, 0, arg) \
262 macro(KMP_allocate_team, 0, arg) \
263 macro(KMP_setup_icv_copy, 0, arg) \
264 macro(USER_icv_copy, 0, arg) \
265 macro (FOR_static_steal_stolen, \
266 stats_flags_e::noUnits | stats_flags_e::noTotal, arg) \
267 macro (FOR_static_steal_chunks, \
268 stats_flags_e::noUnits | stats_flags_e::noTotal, arg)
270 #define KMP_FOREACH_DEVELOPER_TIMER(macro, arg)
293 #define KMP_FOREACH_EXPLICIT_TIMER(macro, arg) KMP_FOREACH_TIMER(macro, arg)
295 #define ENUMERATE(name, ignore, prefix) prefix##name,
296 enum timer_e { KMP_FOREACH_TIMER(ENUMERATE, TIMER_) TIMER_LAST };
298 enum explicit_timer_e {
325 uint32_t KMP_ALIGN_CACHE zeroCount;
331 static double binMax[numBins];
337 uint64_t t = zeroCount;
338 for (
int i = 0; i < numBins; i++)
340 KMP_DEBUG_ASSERT(t == _total);
343 void check()
const {}
347 logHistogram() { reset(); }
349 logHistogram(logHistogram
const &o) {
350 for (
int i = 0; i < numBins; i++)
359 for (
int i = 0; i < numBins; i++) {
368 uint32_t count(
int b)
const {
return bins[b + logOffset].count; }
369 double total(
int b)
const {
return bins[b + logOffset].total; }
370 static uint32_t findBin(
double sample);
372 logHistogram &operator+=(logHistogram
const &o) {
373 zeroCount += o.zeroCount;
374 for (
int i = 0; i < numBins; i++) {
375 bins[i].count += o.bins[i].count;
376 bins[i].total += o.bins[i].total;
386 void addSample(
double sample);
390 std::string format(
char)
const;
394 double KMP_ALIGN_CACHE minVal;
398 uint64_t sampleCount;
404 statistic(
bool doHist =
bool(KMP_STATS_HIST)) {
406 collectingHist = doHist;
408 statistic(statistic
const &o)
409 : minVal(o.minVal), maxVal(o.maxVal), meanVal(o.meanVal), m2(o.m2),
410 sampleCount(o.sampleCount), offset(o.offset),
411 collectingHist(o.collectingHist), hist(o.hist) {}
412 statistic(
double minv,
double maxv,
double meanv, uint64_t sc,
double sd)
413 : minVal(minv), maxVal(maxv), meanVal(meanv), m2(sd * sd * sc),
414 sampleCount(sc), offset(0.0), collectingHist(false) {}
415 bool haveHist()
const {
return collectingHist; }
416 double getMin()
const {
return minVal; }
417 double getMean()
const {
return meanVal; }
418 double getMax()
const {
return maxVal; }
419 uint64_t getCount()
const {
return sampleCount; }
420 double getSD()
const {
return sqrt(m2 / sampleCount); }
421 double getTotal()
const {
return sampleCount * meanVal; }
422 logHistogram
const *getHist()
const {
return &hist; }
423 void setOffset(
double d) { offset = d; }
426 minVal = (std::numeric_limits<double>::max)();
434 void addSample(
double sample);
435 void scale(
double factor);
436 void scaleDown(
double f) { scale(1. / f); }
437 void forceCount(uint64_t count) { sampleCount = count; }
438 statistic &operator+=(statistic
const &other);
440 std::string format(
char unit,
bool total =
false)
const;
441 std::string formatHist(
char unit)
const {
return hist.format(unit); }
449 class timeStat :
public statistic {
450 static statInfo timerInfo[];
453 timeStat() : statistic() {}
454 static const char *name(timer_e e) {
return timerInfo[e].name; }
455 static bool noTotal(timer_e e) {
458 static bool masterOnly(timer_e e) {
461 static bool workerOnly(timer_e e) {
464 static bool noUnits(timer_e e) {
470 static void clearEventFlags() {
471 for (
int i = 0; i < TIMER_LAST; i++) {
480 class explicitTimer {
482 timer_e timerEnumValue;
483 tsc_tick_count startTime;
484 tsc_tick_count pauseStartTime;
485 tsc_tick_count::tsc_interval_t totalPauseTime;
488 explicitTimer(timeStat *s, timer_e te)
489 : stat(s), timerEnumValue(te), startTime(), pauseStartTime(0),
493 void start(tsc_tick_count tick);
494 void pause(tsc_tick_count tick) { pauseStartTime = tick; }
495 void resume(tsc_tick_count tick) {
496 totalPauseTime += (tick - pauseStartTime);
498 void stop(tsc_tick_count tick, kmp_stats_list *stats_ptr =
nullptr);
504 timer_e get_type()
const {
return timerEnumValue; }
513 class partitionedTimers {
515 std::vector<explicitTimer> timer_stack;
519 void init(explicitTimer timer);
520 void exchange(explicitTimer timer);
521 void push(explicitTimer timer);
528 class blockPartitionedTimer {
529 partitionedTimers *part_timers;
532 blockPartitionedTimer(partitionedTimers *pt, explicitTimer timer)
534 part_timers->push(timer);
536 ~blockPartitionedTimer() { part_timers->pop(); }
542 class blockThreadState {
548 : state_pointer(thread_state_pointer), old_state(*thread_state_pointer) {
549 *state_pointer = new_state;
551 ~blockThreadState() { *state_pointer = old_state; }
559 static const statInfo counterInfo[];
562 counter() : value(0) {}
563 void increment() { value++; }
564 uint64_t getValue()
const {
return value; }
565 void reset() { value = 0; }
566 static const char *name(counter_e e) {
return counterInfo[e].name; }
567 static bool masterOnly(counter_e e) {
605 class kmp_stats_event {
613 : start(0), stop(0), nest_level(0), timer_name(TIMER_LAST) {}
614 kmp_stats_event(uint64_t strt, uint64_t stp,
int nst, timer_e nme)
615 : start(strt), stop(stp), nest_level(nst), timer_name(nme) {}
616 inline uint64_t getStart()
const {
return start; }
617 inline uint64_t getStop()
const {
return stop; }
618 inline int getNestLevel()
const {
return nest_level; }
619 inline timer_e getTimerName()
const {
return timer_name; }
648 class kmp_stats_event_vector {
649 kmp_stats_event *events;
652 static const int INIT_SIZE = 1024;
655 kmp_stats_event_vector() {
657 (kmp_stats_event *)__kmp_allocate(
sizeof(kmp_stats_event) * INIT_SIZE);
659 allocated_size = INIT_SIZE;
661 ~kmp_stats_event_vector() {}
662 inline void reset() { internal_size = 0; }
663 inline int size()
const {
return internal_size; }
664 void push_back(uint64_t start_time, uint64_t stop_time,
int nest_level,
667 if (internal_size == allocated_size) {
668 kmp_stats_event *tmp = (kmp_stats_event *)__kmp_allocate(
669 sizeof(kmp_stats_event) * allocated_size * 2);
670 for (i = 0; i < internal_size; i++)
676 events[internal_size] =
677 kmp_stats_event(start_time, stop_time, nest_level, name);
683 const kmp_stats_event &operator[](
int index)
const {
return events[index]; }
684 kmp_stats_event &operator[](
int index) {
return events[index]; }
685 const kmp_stats_event &at(
int index)
const {
return events[index]; }
686 kmp_stats_event &at(
int index) {
return events[index]; }
716 class kmp_stats_list {
718 timeStat _timers[TIMER_LAST + 1];
719 counter _counters[COUNTER_LAST + 1];
720 explicitTimer thread_life_timer;
721 partitionedTimers _partitionedTimers;
723 kmp_stats_event_vector _event_vector;
724 kmp_stats_list *next;
725 kmp_stats_list *prev;
727 int thread_is_idle_flag;
731 : thread_life_timer(&_timers[TIMER_OMP_worker_thread_life],
732 TIMER_OMP_worker_thread_life),
733 _nestLevel(0), _event_vector(), next(this), prev(this), state(IDLE),
734 thread_is_idle_flag(0) {}
736 inline timeStat *getTimer(timer_e idx) {
return &_timers[idx]; }
737 inline counter *getCounter(counter_e idx) {
return &_counters[idx]; }
738 inline partitionedTimers *getPartitionedTimers() {
739 return &_partitionedTimers;
741 inline timeStat *getTimers() {
return _timers; }
742 inline counter *getCounters() {
return _counters; }
743 inline kmp_stats_event_vector &getEventVector() {
return _event_vector; }
744 inline void startLife() { thread_life_timer.start(tsc_tick_count::now()); }
745 inline void endLife() { thread_life_timer.stop(tsc_tick_count::now(),
this); }
746 inline void resetEventVector() { _event_vector.reset(); }
747 inline void incrementNestValue() { _nestLevel++; }
748 inline int getNestValue() {
return _nestLevel; }
749 inline void decrementNestValue() { _nestLevel--; }
750 inline int getGtid()
const {
return gtid; }
751 inline void setGtid(
int newgtid) { gtid = newgtid; }
752 inline void setState(
stats_state_e newstate) { state = newstate; }
755 inline bool isIdle() {
return thread_is_idle_flag == 1; }
756 inline void setIdleFlag() { thread_is_idle_flag = 1; }
757 inline void resetIdleFlag() { thread_is_idle_flag = 0; }
758 kmp_stats_list *push_back(
int gtid);
759 inline void push_event(uint64_t start_time, uint64_t stop_time,
760 int nest_level, timer_e name) {
761 _event_vector.push_back(start_time, stop_time, nest_level, name);
765 kmp_stats_list::iterator begin();
766 kmp_stats_list::iterator end();
770 friend kmp_stats_list::iterator kmp_stats_list::begin();
771 friend kmp_stats_list::iterator kmp_stats_list::end();
776 iterator operator++();
777 iterator operator++(
int dummy);
778 iterator operator--();
779 iterator operator--(
int dummy);
780 bool operator!=(
const iterator &rhs);
781 bool operator==(
const iterator &rhs);
782 kmp_stats_list *operator*()
const;
815 class kmp_stats_output_module {
825 std::string outputFileName;
826 static const char *eventsFileName;
827 static const char *plotFileName;
828 static int printPerThreadFlag;
829 static int printPerThreadEventsFlag;
830 static const rgb_color globalColorArray[];
831 static rgb_color timerColorInfo[];
834 static void setupEventColors();
835 static void printPloticusFile();
836 static void printHeaderInfo(FILE *statsOut);
837 static void printTimerStats(FILE *statsOut, statistic
const *theStats,
838 statistic
const *totalStats);
839 static void printCounterStats(FILE *statsOut, statistic
const *theStats);
840 static void printCounters(FILE *statsOut, counter
const *theCounters);
841 static void printEvents(FILE *eventsOut, kmp_stats_event_vector *theEvents,
843 static rgb_color getEventColor(timer_e e) {
return timerColorInfo[e]; }
844 static void windupExplicitTimers();
845 bool eventPrintingEnabled()
const {
return printPerThreadEventsFlag; }
848 kmp_stats_output_module() { init(); }
849 void outputStats(
const char *heading);
855 void __kmp_stats_init();
856 void __kmp_stats_fini();
857 void __kmp_reset_stats();
858 void __kmp_output_stats(
const char *);
859 void __kmp_accumulate_stats_at_exit(
void);
861 extern KMP_THREAD_LOCAL kmp_stats_list *__kmp_stats_thread_ptr;
863 extern kmp_stats_list *__kmp_stats_list;
865 extern kmp_tas_lock_t __kmp_stats_lock;
867 extern tsc_tick_count __kmp_stats_start_time;
869 extern kmp_stats_output_module __kmp_stats_output;
888 #define KMP_COUNT_VALUE(name, value) \
889 __kmp_stats_thread_ptr->getTimer(TIMER_##name)->addSample((double)value)
901 #define KMP_COUNT_BLOCK(name) \
902 __kmp_stats_thread_ptr->getCounter(COUNTER_##name)->increment()
921 #define KMP_OUTPUT_STATS(heading_string) __kmp_output_stats(heading_string)
930 #define KMP_INIT_PARTITIONED_TIMERS(name) \
931 __kmp_stats_thread_ptr->getPartitionedTimers()->init(explicitTimer( \
932 __kmp_stats_thread_ptr->getTimer(TIMER_##name), TIMER_##name))
934 #define KMP_TIME_PARTITIONED_BLOCK(name) \
935 blockPartitionedTimer __PBLOCKTIME__( \
936 __kmp_stats_thread_ptr->getPartitionedTimers(), \
937 explicitTimer(__kmp_stats_thread_ptr->getTimer(TIMER_##name), \
940 #define KMP_PUSH_PARTITIONED_TIMER(name) \
941 __kmp_stats_thread_ptr->getPartitionedTimers()->push(explicitTimer( \
942 __kmp_stats_thread_ptr->getTimer(TIMER_##name), TIMER_##name))
944 #define KMP_POP_PARTITIONED_TIMER() \
945 __kmp_stats_thread_ptr->getPartitionedTimers()->pop()
947 #define KMP_EXCHANGE_PARTITIONED_TIMER(name) \
948 __kmp_stats_thread_ptr->getPartitionedTimers()->exchange(explicitTimer( \
949 __kmp_stats_thread_ptr->getTimer(TIMER_##name), TIMER_##name))
951 #define KMP_SET_THREAD_STATE(state_name) \
952 __kmp_stats_thread_ptr->setState(state_name)
954 #define KMP_GET_THREAD_STATE() __kmp_stats_thread_ptr->getState()
956 #define KMP_SET_THREAD_STATE_BLOCK(state_name) \
957 blockThreadState __BTHREADSTATE__(__kmp_stats_thread_ptr->getStatePointer(), \
967 #define KMP_RESET_STATS() __kmp_reset_stats()
969 #if (KMP_DEVELOPER_STATS)
970 #define KMP_COUNT_DEVELOPER_VALUE(n, v) KMP_COUNT_VALUE(n, v)
971 #define KMP_COUNT_DEVELOPER_BLOCK(n) KMP_COUNT_BLOCK(n)
972 #define KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(n) KMP_TIME_PARTITIONED_BLOCK(n)
973 #define KMP_PUSH_DEVELOPER_PARTITIONED_TIMER(n) KMP_PUSH_PARTITIONED_TIMER(n)
974 #define KMP_POP_DEVELOPER_PARTITIONED_TIMER(n) KMP_POP_PARTITIONED_TIMER(n)
975 #define KMP_EXCHANGE_DEVELOPER_PARTITIONED_TIMER(n) \
976 KMP_EXCHANGE_PARTITIONED_TIMER(n)
979 #define KMP_COUNT_DEVELOPER_VALUE(n, v) ((void)0)
980 #define KMP_COUNT_DEVELOPER_BLOCK(n) ((void)0)
981 #define KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(n) ((void)0)
982 #define KMP_PUSH_DEVELOPER_PARTITIONED_TIMER(n) ((void)0)
983 #define KMP_POP_DEVELOPER_PARTITIONED_TIMER(n) ((void)0)
984 #define KMP_EXCHANGE_DEVELOPER_PARTITIONED_TIMER(n) ((void)0)
990 #define KMP_COUNT_VALUE(n, v) ((void)0)
991 #define KMP_COUNT_BLOCK(n) ((void)0)
993 #define KMP_OUTPUT_STATS(heading_string) ((void)0)
994 #define KMP_RESET_STATS() ((void)0)
996 #define KMP_COUNT_DEVELOPER_VALUE(n, v) ((void)0)
997 #define KMP_COUNT_DEVELOPER_BLOCK(n) ((void)0)
998 #define KMP_TIME_DEVELOPER_PARTITIONED_BLOCK(n) ((void)0)
999 #define KMP_PUSH_DEVELOPER_PARTITIONED_TIMER(n) ((void)0)
1000 #define KMP_POP_DEVELOPER_PARTITIONED_TIMER(n) ((void)0)
1001 #define KMP_EXCHANGE_DEVELOPER_PARTITIONED_TIMER(n) ((void)0)
1002 #define KMP_INIT_PARTITIONED_TIMERS(name) ((void)0)
1003 #define KMP_TIME_PARTITIONED_BLOCK(name) ((void)0)
1004 #define KMP_PUSH_PARTITIONED_TIMER(name) ((void)0)
1005 #define KMP_POP_PARTITIONED_TIMER() ((void)0)
1006 #define KMP_SET_THREAD_STATE(state_name) ((void)0)
1007 #define KMP_GET_THREAD_STATE() ((void)0)
1008 #define KMP_SET_THREAD_STATE_BLOCK(state_name) ((void)0)
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.
#define KMP_FOREACH_EXPLICIT_TIMER(macro, arg)
Add new explicit timers under KMP_FOREACH_EXPLICIT_TIMER() macro.
stats_state_e
the states which a thread can be in
@ notInMaster
statistic is valid only for non-master threads
@ noUnits
statistic doesn't need units printed next to it
@ noTotal
do not show a TOTAL_aggregation for this statistic
@ onlyInMaster
statistic is valid only for master