Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1 | /** @file kmp_stats.cpp |
| 2 | * Statistics gathering and processing. |
| 3 | */ |
| 4 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 5 | //===----------------------------------------------------------------------===// |
| 6 | // |
| 7 | // The LLVM Compiler Infrastructure |
| 8 | // |
| 9 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 10 | // Source Licenses. See LICENSE.txt for details. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 14 | #include "kmp.h" |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 15 | #include "kmp_lock.h" |
| 16 | #include "kmp_stats.h" |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 17 | #include "kmp_str.h" |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
Jonathan Peyton | 6e98d798 | 2016-03-15 20:28:47 +0000 | [diff] [blame] | 20 | #include <ctime> |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 21 | #include <iomanip> |
| 22 | #include <sstream> |
| 23 | #include <stdlib.h> // for atexit |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 24 | |
| 25 | #define STRINGIZE2(x) #x |
| 26 | #define STRINGIZE(x) STRINGIZE2(x) |
| 27 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 28 | #define expandName(name, flags, ignore) {STRINGIZE(name), flags}, |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 29 | statInfo timeStat::timerInfo[] = { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 30 | KMP_FOREACH_TIMER(expandName, 0){"TIMER_LAST", 0}}; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 31 | const statInfo counter::counterInfo[] = { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 32 | KMP_FOREACH_COUNTER(expandName, 0){"COUNTER_LAST", 0}}; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 33 | #undef expandName |
| 34 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 35 | #define expandName(ignore1, ignore2, ignore3) {0.0, 0.0, 0.0}, |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 36 | kmp_stats_output_module::rgb_color kmp_stats_output_module::timerColorInfo[] = { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 37 | KMP_FOREACH_TIMER(expandName, 0){0.0, 0.0, 0.0}}; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 38 | #undef expandName |
| 39 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 40 | const kmp_stats_output_module::rgb_color |
| 41 | kmp_stats_output_module::globalColorArray[] = { |
| 42 | {1.0, 0.0, 0.0}, // red |
| 43 | {1.0, 0.6, 0.0}, // orange |
| 44 | {1.0, 1.0, 0.0}, // yellow |
| 45 | {0.0, 1.0, 0.0}, // green |
| 46 | {0.0, 0.0, 1.0}, // blue |
| 47 | {0.6, 0.2, 0.8}, // purple |
| 48 | {1.0, 0.0, 1.0}, // magenta |
| 49 | {0.0, 0.4, 0.2}, // dark green |
| 50 | {1.0, 1.0, 0.6}, // light yellow |
| 51 | {0.6, 0.4, 0.6}, // dirty purple |
| 52 | {0.0, 1.0, 1.0}, // cyan |
| 53 | {1.0, 0.4, 0.8}, // pink |
| 54 | {0.5, 0.5, 0.5}, // grey |
| 55 | {0.8, 0.7, 0.5}, // brown |
| 56 | {0.6, 0.6, 1.0}, // light blue |
| 57 | {1.0, 0.7, 0.5}, // peach |
| 58 | {0.8, 0.5, 1.0}, // lavender |
| 59 | {0.6, 0.0, 0.0}, // dark red |
| 60 | {0.7, 0.6, 0.0}, // gold |
| 61 | {0.0, 0.0, 0.0} // black |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | // Ensure that the atexit handler only runs once. |
| 65 | static uint32_t statsPrinted = 0; |
| 66 | |
| 67 | // output interface |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 68 | static kmp_stats_output_module *__kmp_stats_global_output = NULL; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 69 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 70 | /* ************* statistic member functions ************* */ |
| 71 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 72 | void statistic::addSample(double sample) { |
| 73 | double delta = sample - meanVal; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 74 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 75 | sampleCount = sampleCount + 1; |
| 76 | meanVal = meanVal + delta / sampleCount; |
| 77 | m2 = m2 + delta * (sample - meanVal); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 78 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 79 | minVal = std::min(minVal, sample); |
| 80 | maxVal = std::max(maxVal, sample); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 83 | statistic &statistic::operator+=(const statistic &other) { |
| 84 | if (sampleCount == 0) { |
| 85 | *this = other; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 86 | return *this; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | uint64_t newSampleCount = sampleCount + other.sampleCount; |
| 90 | double dnsc = double(newSampleCount); |
| 91 | double dsc = double(sampleCount); |
| 92 | double dscBydnsc = dsc / dnsc; |
| 93 | double dosc = double(other.sampleCount); |
| 94 | double delta = other.meanVal - meanVal; |
| 95 | |
| 96 | // Try to order these calculations to avoid overflows. If this were Fortran, |
| 97 | // then the compiler would not be able to re-order over brackets. In C++ it |
| 98 | // may be legal to do that (we certainly hope it doesn't, and CC+ Programming |
| 99 | // Language 2nd edition suggests it shouldn't, since it says that exploitation |
| 100 | // of associativity can only be made if the operation really is associative |
| 101 | // (which floating addition isn't...)). |
| 102 | meanVal = meanVal * dscBydnsc + other.meanVal * (1 - dscBydnsc); |
| 103 | m2 = m2 + other.m2 + dscBydnsc * dosc * delta * delta; |
| 104 | minVal = std::min(minVal, other.minVal); |
| 105 | maxVal = std::max(maxVal, other.maxVal); |
| 106 | sampleCount = newSampleCount; |
| 107 | |
| 108 | return *this; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 111 | void statistic::scale(double factor) { |
| 112 | minVal = minVal * factor; |
| 113 | maxVal = maxVal * factor; |
| 114 | meanVal = meanVal * factor; |
| 115 | m2 = m2 * factor * factor; |
| 116 | return; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 119 | std::string statistic::format(char unit, bool total) const { |
| 120 | std::string result = formatSI(sampleCount, 9, ' '); |
Jonathan Peyton | 072772b | 2016-04-05 18:48:48 +0000 | [diff] [blame] | 121 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 122 | if (sampleCount == 0) { |
| 123 | result = result + std::string(", ") + formatSI(0.0, 9, unit); |
| 124 | result = result + std::string(", ") + formatSI(0.0, 9, unit); |
| 125 | result = result + std::string(", ") + formatSI(0.0, 9, unit); |
| 126 | if (total) |
| 127 | result = result + std::string(", ") + formatSI(0.0, 9, unit); |
| 128 | result = result + std::string(", ") + formatSI(0.0, 9, unit); |
| 129 | } else { |
| 130 | result = result + std::string(", ") + formatSI(minVal, 9, unit); |
| 131 | result = result + std::string(", ") + formatSI(meanVal, 9, unit); |
| 132 | result = result + std::string(", ") + formatSI(maxVal, 9, unit); |
| 133 | if (total) |
| 134 | result = |
| 135 | result + std::string(", ") + formatSI(meanVal * sampleCount, 9, unit); |
| 136 | result = result + std::string(", ") + formatSI(getSD(), 9, unit); |
| 137 | } |
| 138 | return result; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 141 | /* ************* explicitTimer member functions ************* */ |
| 142 | |
Jonathan Peyton | 072772b | 2016-04-05 18:48:48 +0000 | [diff] [blame] | 143 | void explicitTimer::start(timer_e timerEnumValue) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 144 | startTime = tsc_tick_count::now(); |
| 145 | totalPauseTime = 0; |
| 146 | if (timeStat::logEvent(timerEnumValue)) { |
| 147 | __kmp_stats_thread_ptr->incrementNestValue(); |
| 148 | } |
| 149 | return; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 152 | void explicitTimer::stop(timer_e timerEnumValue, |
| 153 | kmp_stats_list *stats_ptr /* = nullptr */) { |
| 154 | if (startTime.getValue() == 0) |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 155 | return; |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 156 | |
| 157 | tsc_tick_count finishTime = tsc_tick_count::now(); |
| 158 | |
| 159 | // stat->addSample ((tsc_tick_count::now() - startTime).ticks()); |
| 160 | stat->addSample(((finishTime - startTime) - totalPauseTime).ticks()); |
| 161 | |
| 162 | if (timeStat::logEvent(timerEnumValue)) { |
| 163 | if (!stats_ptr) |
| 164 | stats_ptr = __kmp_stats_thread_ptr; |
| 165 | stats_ptr->push_event( |
| 166 | startTime.getValue() - __kmp_stats_start_time.getValue(), |
| 167 | finishTime.getValue() - __kmp_stats_start_time.getValue(), |
| 168 | __kmp_stats_thread_ptr->getNestValue(), timerEnumValue); |
| 169 | stats_ptr->decrementNestValue(); |
| 170 | } |
| 171 | |
| 172 | /* We accept the risk that we drop a sample because it really did start at |
| 173 | t==0. */ |
| 174 | startTime = 0; |
| 175 | return; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Jonathan Peyton | 11dc82f | 2016-05-05 16:15:57 +0000 | [diff] [blame] | 178 | /* ************* partitionedTimers member functions ************* */ |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 179 | partitionedTimers::partitionedTimers() { timer_stack.reserve(8); } |
Jonathan Peyton | 11dc82f | 2016-05-05 16:15:57 +0000 | [diff] [blame] | 180 | |
| 181 | // add a timer to this collection of partitioned timers. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 182 | void partitionedTimers::add_timer(explicit_timer_e timer_index, |
| 183 | explicitTimer *timer_pointer) { |
| 184 | KMP_DEBUG_ASSERT((int)timer_index < (int)EXPLICIT_TIMER_LAST + 1); |
| 185 | timers[timer_index] = timer_pointer; |
Jonathan Peyton | 11dc82f | 2016-05-05 16:15:57 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // initialize the paritioned timers to an initial timer |
| 189 | void partitionedTimers::init(timerPair init_timer_pair) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 190 | KMP_DEBUG_ASSERT(this->timer_stack.size() == 0); |
| 191 | timer_stack.push_back(init_timer_pair); |
| 192 | timers[init_timer_pair.get_index()]->start(init_timer_pair.get_timer()); |
Jonathan Peyton | 11dc82f | 2016-05-05 16:15:57 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // stop/save the current timer, and start the new timer (timer_pair) |
| 196 | // There is a special condition where if the current timer is equal to |
| 197 | // the one you are trying to push, then it only manipulates the stack, |
| 198 | // and it won't stop/start the currently running timer. |
| 199 | void partitionedTimers::push(timerPair timer_pair) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 200 | // get the current timer |
| 201 | // stop current timer |
| 202 | // push new timer |
| 203 | // start the new timer |
| 204 | KMP_DEBUG_ASSERT(this->timer_stack.size() > 0); |
| 205 | timerPair current_timer = timer_stack.back(); |
| 206 | timer_stack.push_back(timer_pair); |
| 207 | if (current_timer != timer_pair) { |
| 208 | timers[current_timer.get_index()]->pause(); |
| 209 | timers[timer_pair.get_index()]->start(timer_pair.get_timer()); |
| 210 | } |
Jonathan Peyton | 11dc82f | 2016-05-05 16:15:57 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // stop/discard the current timer, and start the previously saved timer |
| 214 | void partitionedTimers::pop() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 215 | // get the current timer |
| 216 | // stop current timer |
| 217 | // pop current timer |
| 218 | // get the new current timer and start it back up |
| 219 | KMP_DEBUG_ASSERT(this->timer_stack.size() > 1); |
| 220 | timerPair current_timer = timer_stack.back(); |
| 221 | timer_stack.pop_back(); |
| 222 | timerPair new_timer = timer_stack.back(); |
| 223 | if (current_timer != new_timer) { |
| 224 | timers[current_timer.get_index()]->stop(current_timer.get_timer()); |
| 225 | timers[new_timer.get_index()]->resume(); |
| 226 | } |
Jonathan Peyton | 11dc82f | 2016-05-05 16:15:57 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // Wind up all the currently running timers. |
| 230 | // This pops off all the timers from the stack and clears the stack |
| 231 | // After this is called, init() must be run again to initialize the |
| 232 | // stack of timers |
| 233 | void partitionedTimers::windup() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 234 | while (timer_stack.size() > 1) { |
| 235 | this->pop(); |
| 236 | } |
| 237 | if (timer_stack.size() > 0) { |
| 238 | timerPair last_timer = timer_stack.back(); |
| 239 | timer_stack.pop_back(); |
| 240 | timers[last_timer.get_index()]->stop(last_timer.get_timer()); |
| 241 | } |
Jonathan Peyton | 11dc82f | 2016-05-05 16:15:57 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 244 | /* ************* kmp_stats_event_vector member functions ************* */ |
| 245 | |
| 246 | void kmp_stats_event_vector::deallocate() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 247 | __kmp_free(events); |
| 248 | internal_size = 0; |
| 249 | allocated_size = 0; |
| 250 | events = NULL; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | // This function is for qsort() which requires the compare function to return |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 254 | // either a negative number if event1 < event2, a positive number if event1 > |
| 255 | // event2 or zero if event1 == event2. This sorts by start time (lowest to |
| 256 | // highest). |
| 257 | int compare_two_events(const void *event1, const void *event2) { |
| 258 | kmp_stats_event *ev1 = (kmp_stats_event *)event1; |
| 259 | kmp_stats_event *ev2 = (kmp_stats_event *)event2; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 260 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 261 | if (ev1->getStart() < ev2->getStart()) |
| 262 | return -1; |
| 263 | else if (ev1->getStart() > ev2->getStart()) |
| 264 | return 1; |
| 265 | else |
| 266 | return 0; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void kmp_stats_event_vector::sort() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 270 | qsort(events, internal_size, sizeof(kmp_stats_event), compare_two_events); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 273 | /* ************* kmp_stats_list member functions ************* */ |
| 274 | |
| 275 | // returns a pointer to newly created stats node |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 276 | kmp_stats_list *kmp_stats_list::push_back(int gtid) { |
| 277 | kmp_stats_list *newnode = |
| 278 | (kmp_stats_list *)__kmp_allocate(sizeof(kmp_stats_list)); |
| 279 | // placement new, only requires space and pointer and initializes (so |
| 280 | // __kmp_allocate instead of C++ new[] is used) |
| 281 | new (newnode) kmp_stats_list(); |
| 282 | newnode->setGtid(gtid); |
| 283 | newnode->prev = this->prev; |
| 284 | newnode->next = this; |
| 285 | newnode->prev->next = newnode; |
| 286 | newnode->next->prev = newnode; |
| 287 | return newnode; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 288 | } |
| 289 | void kmp_stats_list::deallocate() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 290 | kmp_stats_list *ptr = this->next; |
| 291 | kmp_stats_list *delptr = this->next; |
| 292 | while (ptr != this) { |
| 293 | delptr = ptr; |
| 294 | ptr = ptr->next; |
| 295 | // placement new means we have to explicitly call destructor. |
| 296 | delptr->_event_vector.deallocate(); |
| 297 | delptr->~kmp_stats_list(); |
| 298 | __kmp_free(delptr); |
| 299 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 300 | } |
| 301 | kmp_stats_list::iterator kmp_stats_list::begin() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 302 | kmp_stats_list::iterator it; |
| 303 | it.ptr = this->next; |
| 304 | return it; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 305 | } |
| 306 | kmp_stats_list::iterator kmp_stats_list::end() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 307 | kmp_stats_list::iterator it; |
| 308 | it.ptr = this; |
| 309 | return it; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 310 | } |
| 311 | int kmp_stats_list::size() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 312 | int retval; |
| 313 | kmp_stats_list::iterator it; |
| 314 | for (retval = 0, it = begin(); it != end(); it++, retval++) { |
| 315 | } |
| 316 | return retval; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 319 | /* ************* kmp_stats_list::iterator member functions ************* */ |
| 320 | |
Jonathan Peyton | 072772b | 2016-04-05 18:48:48 +0000 | [diff] [blame] | 321 | kmp_stats_list::iterator::iterator() : ptr(NULL) {} |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 322 | kmp_stats_list::iterator::~iterator() {} |
| 323 | kmp_stats_list::iterator kmp_stats_list::iterator::operator++() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 324 | this->ptr = this->ptr->next; |
| 325 | return *this; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 326 | } |
| 327 | kmp_stats_list::iterator kmp_stats_list::iterator::operator++(int dummy) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 328 | this->ptr = this->ptr->next; |
| 329 | return *this; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 330 | } |
| 331 | kmp_stats_list::iterator kmp_stats_list::iterator::operator--() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 332 | this->ptr = this->ptr->prev; |
| 333 | return *this; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 334 | } |
| 335 | kmp_stats_list::iterator kmp_stats_list::iterator::operator--(int dummy) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 336 | this->ptr = this->ptr->prev; |
| 337 | return *this; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 338 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 339 | bool kmp_stats_list::iterator::operator!=(const kmp_stats_list::iterator &rhs) { |
| 340 | return this->ptr != rhs.ptr; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 341 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 342 | bool kmp_stats_list::iterator::operator==(const kmp_stats_list::iterator &rhs) { |
| 343 | return this->ptr == rhs.ptr; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 344 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 345 | kmp_stats_list *kmp_stats_list::iterator::operator*() const { |
| 346 | return this->ptr; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 349 | /* ************* kmp_stats_output_module functions ************** */ |
| 350 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 351 | const char *kmp_stats_output_module::eventsFileName = NULL; |
| 352 | const char *kmp_stats_output_module::plotFileName = NULL; |
| 353 | int kmp_stats_output_module::printPerThreadFlag = 0; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 354 | int kmp_stats_output_module::printPerThreadEventsFlag = 0; |
| 355 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 356 | // init() is called very near the beginning of execution time in the constructor |
| 357 | // of __kmp_stats_global_output |
| 358 | void kmp_stats_output_module::init() { |
| 359 | char *statsFileName = getenv("KMP_STATS_FILE"); |
| 360 | eventsFileName = getenv("KMP_STATS_EVENTS_FILE"); |
| 361 | plotFileName = getenv("KMP_STATS_PLOT_FILE"); |
| 362 | char *threadStats = getenv("KMP_STATS_THREADS"); |
| 363 | char *threadEvents = getenv("KMP_STATS_EVENTS"); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 364 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 365 | // set the stats output filenames based on environment variables and defaults |
| 366 | if (statsFileName) { |
| 367 | // append the process id to the output filename |
| 368 | // events.csv --> events-pid.csv |
| 369 | size_t index; |
| 370 | std::string baseFileName, pid, suffix; |
| 371 | std::stringstream ss; |
| 372 | outputFileName = std::string(statsFileName); |
| 373 | index = outputFileName.find_last_of('.'); |
| 374 | if (index == std::string::npos) { |
| 375 | baseFileName = outputFileName; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 376 | } else { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 377 | baseFileName = outputFileName.substr(0, index); |
| 378 | suffix = outputFileName.substr(index); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 379 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 380 | ss << getpid(); |
| 381 | pid = ss.str(); |
| 382 | outputFileName = baseFileName + "-" + pid + suffix; |
| 383 | } |
| 384 | eventsFileName = eventsFileName ? eventsFileName : "events.dat"; |
| 385 | plotFileName = plotFileName ? plotFileName : "events.plt"; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 386 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 387 | // set the flags based on environment variables matching: true, on, 1, .true. |
| 388 | // , .t. , yes |
| 389 | printPerThreadFlag = __kmp_str_match_true(threadStats); |
| 390 | printPerThreadEventsFlag = __kmp_str_match_true(threadEvents); |
| 391 | |
| 392 | if (printPerThreadEventsFlag) { |
| 393 | // assigns a color to each timer for printing |
| 394 | setupEventColors(); |
| 395 | } else { |
| 396 | // will clear flag so that no event will be logged |
| 397 | timeStat::clearEventFlags(); |
| 398 | } |
| 399 | |
| 400 | return; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | void kmp_stats_output_module::setupEventColors() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 404 | int i; |
| 405 | int globalColorIndex = 0; |
| 406 | int numGlobalColors = sizeof(globalColorArray) / sizeof(rgb_color); |
| 407 | for (i = 0; i < TIMER_LAST; i++) { |
| 408 | if (timeStat::logEvent((timer_e)i)) { |
| 409 | timerColorInfo[i] = globalColorArray[globalColorIndex]; |
| 410 | globalColorIndex = (globalColorIndex + 1) % numGlobalColors; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 411 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 412 | } |
| 413 | return; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 416 | void kmp_stats_output_module::printTimerStats(FILE *statsOut, |
| 417 | statistic const *theStats, |
| 418 | statistic const *totalStats) { |
| 419 | fprintf(statsOut, "Timer, SampleCount, Min, " |
| 420 | "Mean, Max, Total, SD\n"); |
| 421 | for (timer_e s = timer_e(0); s < TIMER_LAST; s = timer_e(s + 1)) { |
| 422 | statistic const *stat = &theStats[s]; |
| 423 | char tag = timeStat::noUnits(s) ? ' ' : 'T'; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 424 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 425 | fprintf(statsOut, "%-28s, %s\n", timeStat::name(s), |
| 426 | stat->format(tag, true).c_str()); |
| 427 | } |
| 428 | // Also print the Total_ versions of times. |
| 429 | for (timer_e s = timer_e(0); s < TIMER_LAST; s = timer_e(s + 1)) { |
| 430 | char tag = timeStat::noUnits(s) ? ' ' : 'T'; |
| 431 | if (totalStats && !timeStat::noTotal(s)) |
| 432 | fprintf(statsOut, "Total_%-22s, %s\n", timeStat::name(s), |
| 433 | totalStats[s].format(tag, true).c_str()); |
| 434 | } |
Jonathan Peyton | e2554af | 2016-03-11 20:20:49 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 437 | void kmp_stats_output_module::printCounterStats(FILE *statsOut, |
| 438 | statistic const *theStats) { |
| 439 | fprintf(statsOut, "Counter, ThreadCount, Min, Mean, " |
| 440 | " Max, Total, SD\n"); |
| 441 | for (int s = 0; s < COUNTER_LAST; s++) { |
| 442 | statistic const *stat = &theStats[s]; |
| 443 | fprintf(statsOut, "%-25s, %s\n", counter::name(counter_e(s)), |
| 444 | stat->format(' ', true).c_str()); |
| 445 | } |
Jonathan Peyton | e2554af | 2016-03-11 20:20:49 +0000 | [diff] [blame] | 446 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 447 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 448 | void kmp_stats_output_module::printCounters(FILE *statsOut, |
| 449 | counter const *theCounters) { |
| 450 | // We print all the counters even if they are zero. |
| 451 | // That makes it easier to slice them into a spreadsheet if you need to. |
| 452 | fprintf(statsOut, "\nCounter, Count\n"); |
| 453 | for (int c = 0; c < COUNTER_LAST; c++) { |
| 454 | counter const *stat = &theCounters[c]; |
| 455 | fprintf(statsOut, "%-25s, %s\n", counter::name(counter_e(c)), |
| 456 | formatSI(stat->getValue(), 9, ' ').c_str()); |
| 457 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 460 | void kmp_stats_output_module::printEvents(FILE *eventsOut, |
| 461 | kmp_stats_event_vector *theEvents, |
| 462 | int gtid) { |
| 463 | // sort by start time before printing |
| 464 | theEvents->sort(); |
| 465 | for (int i = 0; i < theEvents->size(); i++) { |
| 466 | kmp_stats_event ev = theEvents->at(i); |
| 467 | rgb_color color = getEventColor(ev.getTimerName()); |
| 468 | fprintf(eventsOut, "%d %lu %lu %1.1f rgb(%1.1f,%1.1f,%1.1f) %s\n", gtid, |
| 469 | ev.getStart(), ev.getStop(), 1.2 - (ev.getNestLevel() * 0.2), |
| 470 | color.r, color.g, color.b, timeStat::name(ev.getTimerName())); |
| 471 | } |
| 472 | return; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 475 | void kmp_stats_output_module::windupExplicitTimers() { |
| 476 | // Wind up any explicit timers. We assume that it's fair at this point to just |
| 477 | // walk all the explcit timers in all threads and say "it's over". |
| 478 | // If the timer wasn't running, this won't record anything anyway. |
| 479 | kmp_stats_list::iterator it; |
| 480 | for (it = __kmp_stats_list->begin(); it != __kmp_stats_list->end(); it++) { |
| 481 | kmp_stats_list *ptr = *it; |
| 482 | ptr->getPartitionedTimers()->windup(); |
| 483 | for (int timer = 0; timer < EXPLICIT_TIMER_LAST; timer++) { |
| 484 | ptr->getExplicitTimer(explicit_timer_e(timer))->stop((timer_e)timer, ptr); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 485 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 486 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void kmp_stats_output_module::printPloticusFile() { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 490 | int i; |
| 491 | int size = __kmp_stats_list->size(); |
| 492 | FILE *plotOut = fopen(plotFileName, "w+"); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 493 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 494 | fprintf(plotOut, "#proc page\n" |
| 495 | " pagesize: 15 10\n" |
| 496 | " scale: 1.0\n\n"); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 497 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 498 | fprintf(plotOut, "#proc getdata\n" |
| 499 | " file: %s\n\n", |
| 500 | eventsFileName); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 501 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 502 | fprintf(plotOut, "#proc areadef\n" |
| 503 | " title: OpenMP Sampling Timeline\n" |
| 504 | " titledetails: align=center size=16\n" |
| 505 | " rectangle: 1 1 13 9\n" |
| 506 | " xautorange: datafield=2,3\n" |
| 507 | " yautorange: -1 %d\n\n", |
| 508 | size); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 509 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 510 | fprintf(plotOut, "#proc xaxis\n" |
| 511 | " stubs: inc\n" |
| 512 | " stubdetails: size=12\n" |
| 513 | " label: Time (ticks)\n" |
| 514 | " labeldetails: size=14\n\n"); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 515 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 516 | fprintf(plotOut, "#proc yaxis\n" |
| 517 | " stubs: inc 1\n" |
| 518 | " stubrange: 0 %d\n" |
| 519 | " stubdetails: size=12\n" |
| 520 | " label: Thread #\n" |
| 521 | " labeldetails: size=14\n\n", |
| 522 | size - 1); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 523 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 524 | fprintf(plotOut, "#proc bars\n" |
| 525 | " exactcolorfield: 5\n" |
| 526 | " axis: x\n" |
| 527 | " locfield: 1\n" |
| 528 | " segmentfields: 2 3\n" |
| 529 | " barwidthfield: 4\n\n"); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 530 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 531 | // create legend entries corresponding to the timer color |
| 532 | for (i = 0; i < TIMER_LAST; i++) { |
| 533 | if (timeStat::logEvent((timer_e)i)) { |
| 534 | rgb_color c = getEventColor((timer_e)i); |
| 535 | fprintf(plotOut, "#proc legendentry\n" |
| 536 | " sampletype: color\n" |
| 537 | " label: %s\n" |
| 538 | " details: rgb(%1.1f,%1.1f,%1.1f)\n\n", |
| 539 | timeStat::name((timer_e)i), c.r, c.g, c.b); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 540 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 541 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 542 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 543 | fprintf(plotOut, "#proc legend\n" |
| 544 | " format: down\n" |
| 545 | " location: max max\n\n"); |
| 546 | fclose(plotOut); |
| 547 | return; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 550 | /* Print some useful information about |
| 551 | * the date and time this experiment ran. |
| 552 | * the machine on which it ran. |
| 553 | We output all of this as stylised comments, though we may decide to parse |
| 554 | some of it. */ |
| 555 | void kmp_stats_output_module::printHeaderInfo(FILE *statsOut) { |
| 556 | std::time_t now = std::time(0); |
| 557 | char buffer[40]; |
| 558 | char hostName[80]; |
Jonathan Peyton | 6e98d798 | 2016-03-15 20:28:47 +0000 | [diff] [blame] | 559 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 560 | std::strftime(&buffer[0], sizeof(buffer), "%c", std::localtime(&now)); |
| 561 | fprintf(statsOut, "# Time of run: %s\n", &buffer[0]); |
| 562 | if (gethostname(&hostName[0], sizeof(hostName)) == 0) |
| 563 | fprintf(statsOut, "# Hostname: %s\n", &hostName[0]); |
Jonathan Peyton | 6e98d798 | 2016-03-15 20:28:47 +0000 | [diff] [blame] | 564 | #if KMP_ARCH_X86 || KMP_ARCH_X86_64 |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 565 | fprintf(statsOut, "# CPU: %s\n", &__kmp_cpuinfo.name[0]); |
| 566 | fprintf(statsOut, "# Family: %d, Model: %d, Stepping: %d\n", |
| 567 | __kmp_cpuinfo.family, __kmp_cpuinfo.model, __kmp_cpuinfo.stepping); |
| 568 | if (__kmp_cpuinfo.frequency == 0) |
| 569 | fprintf(statsOut, "# Nominal frequency: Unknown\n"); |
| 570 | else |
| 571 | fprintf(statsOut, "# Nominal frequency: %sz\n", |
| 572 | formatSI(double(__kmp_cpuinfo.frequency), 9, 'H').c_str()); |
Jonathan Peyton | 6e98d798 | 2016-03-15 20:28:47 +0000 | [diff] [blame] | 573 | #endif |
| 574 | } |
| 575 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 576 | void kmp_stats_output_module::outputStats(const char *heading) { |
| 577 | // Stop all the explicit timers in all threads |
| 578 | // Do this before declaring the local statistics because thay have |
| 579 | // constructors so will take time to create. |
| 580 | windupExplicitTimers(); |
Jonathan Peyton | 6e98d798 | 2016-03-15 20:28:47 +0000 | [diff] [blame] | 581 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 582 | statistic allStats[TIMER_LAST]; |
| 583 | statistic totalStats[TIMER_LAST]; /* Synthesized, cross threads versions of |
| 584 | normal timer stats */ |
| 585 | statistic allCounters[COUNTER_LAST]; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 586 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 587 | FILE *statsOut = |
| 588 | !outputFileName.empty() ? fopen(outputFileName.c_str(), "a+") : stderr; |
| 589 | if (!statsOut) |
| 590 | statsOut = stderr; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 591 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 592 | FILE *eventsOut; |
| 593 | if (eventPrintingEnabled()) { |
| 594 | eventsOut = fopen(eventsFileName, "w+"); |
| 595 | } |
| 596 | |
| 597 | printHeaderInfo(statsOut); |
| 598 | fprintf(statsOut, "%s\n", heading); |
| 599 | // Accumulate across threads. |
| 600 | kmp_stats_list::iterator it; |
| 601 | for (it = __kmp_stats_list->begin(); it != __kmp_stats_list->end(); it++) { |
| 602 | int t = (*it)->getGtid(); |
| 603 | // Output per thread stats if requested. |
| 604 | if (printPerThreadFlag) { |
| 605 | fprintf(statsOut, "Thread %d\n", t); |
| 606 | printTimerStats(statsOut, (*it)->getTimers(), 0); |
| 607 | printCounters(statsOut, (*it)->getCounters()); |
| 608 | fprintf(statsOut, "\n"); |
| 609 | } |
| 610 | // Output per thread events if requested. |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 611 | if (eventPrintingEnabled()) { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 612 | kmp_stats_event_vector events = (*it)->getEventVector(); |
| 613 | printEvents(eventsOut, &events, t); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 616 | // Accumulate timers. |
| 617 | for (timer_e s = timer_e(0); s < TIMER_LAST; s = timer_e(s + 1)) { |
| 618 | // See if we should ignore this timer when aggregating |
| 619 | if ((timeStat::masterOnly(s) && (t != 0)) || // Timer only valid on master |
| 620 | // and this thread is worker |
| 621 | (timeStat::workerOnly(s) && (t == 0)) // Timer only valid on worker |
| 622 | // and this thread is the master |
| 623 | ) { |
| 624 | continue; |
| 625 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 626 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 627 | statistic *threadStat = (*it)->getTimer(s); |
| 628 | allStats[s] += *threadStat; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 629 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 630 | // Add Total stats for timers that are valid in more than one thread |
| 631 | if (!timeStat::noTotal(s)) |
| 632 | totalStats[s].addSample(threadStat->getTotal()); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 635 | // Accumulate counters. |
| 636 | for (counter_e c = counter_e(0); c < COUNTER_LAST; c = counter_e(c + 1)) { |
| 637 | if (counter::masterOnly(c) && t != 0) |
| 638 | continue; |
| 639 | allCounters[c].addSample((*it)->getCounter(c)->getValue()); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 640 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 641 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 642 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 643 | if (eventPrintingEnabled()) { |
| 644 | printPloticusFile(); |
| 645 | fclose(eventsOut); |
| 646 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 647 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 648 | fprintf(statsOut, "Aggregate for all threads\n"); |
| 649 | printTimerStats(statsOut, &allStats[0], &totalStats[0]); |
| 650 | fprintf(statsOut, "\n"); |
| 651 | printCounterStats(statsOut, &allCounters[0]); |
| 652 | |
| 653 | if (statsOut != stderr) |
| 654 | fclose(statsOut); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 657 | /* ************* exported C functions ************** */ |
| 658 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 659 | // no name mangling for these functions, we want the c files to be able to get |
| 660 | // at these functions |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 661 | extern "C" { |
| 662 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 663 | void __kmp_reset_stats() { |
| 664 | kmp_stats_list::iterator it; |
| 665 | for (it = __kmp_stats_list->begin(); it != __kmp_stats_list->end(); it++) { |
| 666 | timeStat *timers = (*it)->getTimers(); |
| 667 | counter *counters = (*it)->getCounters(); |
| 668 | explicitTimer *eTimers = (*it)->getExplicitTimers(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 669 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 670 | for (int t = 0; t < TIMER_LAST; t++) |
| 671 | timers[t].reset(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 672 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 673 | for (int c = 0; c < COUNTER_LAST; c++) |
| 674 | counters[c].reset(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 675 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 676 | for (int t = 0; t < EXPLICIT_TIMER_LAST; t++) |
| 677 | eTimers[t].reset(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 678 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 679 | // reset the event vector so all previous events are "erased" |
| 680 | (*it)->resetEventVector(); |
| 681 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 684 | // This function will reset all stats and stop all threads' explicit timers if |
| 685 | // they haven't been stopped already. |
| 686 | void __kmp_output_stats(const char *heading) { |
| 687 | __kmp_stats_global_output->outputStats(heading); |
| 688 | __kmp_reset_stats(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 691 | void __kmp_accumulate_stats_at_exit(void) { |
| 692 | // Only do this once. |
| 693 | if (KMP_XCHG_FIXED32(&statsPrinted, 1) != 0) |
| 694 | return; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 695 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 696 | __kmp_output_stats("Statistics on exit"); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 699 | void __kmp_stats_init(void) { |
| 700 | __kmp_init_tas_lock(&__kmp_stats_lock); |
| 701 | __kmp_stats_start_time = tsc_tick_count::now(); |
| 702 | __kmp_stats_global_output = new kmp_stats_output_module(); |
| 703 | __kmp_stats_list = new kmp_stats_list(); |
Jonathan Peyton | 5375fe8 | 2016-11-14 21:13:44 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 706 | void __kmp_stats_fini(void) { |
| 707 | __kmp_accumulate_stats_at_exit(); |
| 708 | __kmp_stats_list->deallocate(); |
| 709 | delete __kmp_stats_global_output; |
| 710 | delete __kmp_stats_list; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Jonathan Peyton | 072772b | 2016-04-05 18:48:48 +0000 | [diff] [blame] | 713 | } // extern "C" |