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