blob: 2c0eabef26aa2e936f86e6d4ba13bd8432510cb2 [file] [log] [blame]
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001/** @file kmp_stats.cpp
2 * Statistics gathering and processing.
3 */
4
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005//===----------------------------------------------------------------------===//
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 Cownie4cc4bb42014-10-07 16:25:50 +000014#include "kmp.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000015#include "kmp_lock.h"
16#include "kmp_stats.h"
Jonathan Peyton30419822017-05-12 18:01:32 +000017#include "kmp_str.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000018
19#include <algorithm>
Jonathan Peyton6e98d7982016-03-15 20:28:47 +000020#include <ctime>
Jonathan Peyton30419822017-05-12 18:01:32 +000021#include <iomanip>
22#include <sstream>
23#include <stdlib.h> // for atexit
Jonathan Peytonf0682ac2018-07-30 17:41:08 +000024#include <cmath>
Jim Cownie4cc4bb42014-10-07 16:25:50 +000025
26#define STRINGIZE2(x) #x
27#define STRINGIZE(x) STRINGIZE2(x)
28
Jonathan Peyton30419822017-05-12 18:01:32 +000029#define expandName(name, flags, ignore) {STRINGIZE(name), flags},
Jim Cownie4cc4bb42014-10-07 16:25:50 +000030statInfo timeStat::timerInfo[] = {
Jonathan Peyton30419822017-05-12 18:01:32 +000031 KMP_FOREACH_TIMER(expandName, 0){"TIMER_LAST", 0}};
Jim Cownie4cc4bb42014-10-07 16:25:50 +000032const statInfo counter::counterInfo[] = {
Jonathan Peyton30419822017-05-12 18:01:32 +000033 KMP_FOREACH_COUNTER(expandName, 0){"COUNTER_LAST", 0}};
Jim Cownie4cc4bb42014-10-07 16:25:50 +000034#undef expandName
35
Jonathan Peyton30419822017-05-12 18:01:32 +000036#define expandName(ignore1, ignore2, ignore3) {0.0, 0.0, 0.0},
Jim Cownie4cc4bb42014-10-07 16:25:50 +000037kmp_stats_output_module::rgb_color kmp_stats_output_module::timerColorInfo[] = {
Jonathan Peyton30419822017-05-12 18:01:32 +000038 KMP_FOREACH_TIMER(expandName, 0){0.0, 0.0, 0.0}};
Jim Cownie4cc4bb42014-10-07 16:25:50 +000039#undef expandName
40
Jonathan Peyton30419822017-05-12 18:01:32 +000041const kmp_stats_output_module::rgb_color
42 kmp_stats_output_module::globalColorArray[] = {
43 {1.0, 0.0, 0.0}, // red
44 {1.0, 0.6, 0.0}, // orange
45 {1.0, 1.0, 0.0}, // yellow
46 {0.0, 1.0, 0.0}, // green
47 {0.0, 0.0, 1.0}, // blue
48 {0.6, 0.2, 0.8}, // purple
49 {1.0, 0.0, 1.0}, // magenta
50 {0.0, 0.4, 0.2}, // dark green
51 {1.0, 1.0, 0.6}, // light yellow
52 {0.6, 0.4, 0.6}, // dirty purple
53 {0.0, 1.0, 1.0}, // cyan
54 {1.0, 0.4, 0.8}, // pink
55 {0.5, 0.5, 0.5}, // grey
56 {0.8, 0.7, 0.5}, // brown
57 {0.6, 0.6, 1.0}, // light blue
58 {1.0, 0.7, 0.5}, // peach
59 {0.8, 0.5, 1.0}, // lavender
60 {0.6, 0.0, 0.0}, // dark red
61 {0.7, 0.6, 0.0}, // gold
62 {0.0, 0.0, 0.0} // black
Jim Cownie4cc4bb42014-10-07 16:25:50 +000063};
64
65// Ensure that the atexit handler only runs once.
66static uint32_t statsPrinted = 0;
67
68// output interface
Jonathan Peyton30419822017-05-12 18:01:32 +000069static kmp_stats_output_module *__kmp_stats_global_output = NULL;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000070
Jonathan Peytonf0682ac2018-07-30 17:41:08 +000071double logHistogram::binMax[] = {
72 1.e1l, 1.e2l, 1.e3l, 1.e4l, 1.e5l, 1.e6l, 1.e7l, 1.e8l,
73 1.e9l, 1.e10l, 1.e11l, 1.e12l, 1.e13l, 1.e14l, 1.e15l, 1.e16l,
74 1.e17l, 1.e18l, 1.e19l, 1.e20l, 1.e21l, 1.e22l, 1.e23l, 1.e24l,
75 1.e25l, 1.e26l, 1.e27l, 1.e28l, 1.e29l, 1.e30l};
76
Jim Cownie4cc4bb42014-10-07 16:25:50 +000077/* ************* statistic member functions ************* */
78
Jonathan Peyton30419822017-05-12 18:01:32 +000079void statistic::addSample(double sample) {
Jonathan Peytonf0682ac2018-07-30 17:41:08 +000080 sample -= offset;
81 KMP_DEBUG_ASSERT(std::isfinite(sample));
82
Jonathan Peyton30419822017-05-12 18:01:32 +000083 double delta = sample - meanVal;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000084
Jonathan Peyton30419822017-05-12 18:01:32 +000085 sampleCount = sampleCount + 1;
86 meanVal = meanVal + delta / sampleCount;
87 m2 = m2 + delta * (sample - meanVal);
Jim Cownie4cc4bb42014-10-07 16:25:50 +000088
Jonathan Peyton30419822017-05-12 18:01:32 +000089 minVal = std::min(minVal, sample);
90 maxVal = std::max(maxVal, sample);
Jonathan Peytonf0682ac2018-07-30 17:41:08 +000091 if (collectingHist)
92 hist.addSample(sample);
Jim Cownie4cc4bb42014-10-07 16:25:50 +000093}
94
Jonathan Peyton30419822017-05-12 18:01:32 +000095statistic &statistic::operator+=(const statistic &other) {
Jonathan Peytonf0682ac2018-07-30 17:41:08 +000096 if (other.sampleCount == 0)
97 return *this;
98
Jonathan Peyton30419822017-05-12 18:01:32 +000099 if (sampleCount == 0) {
100 *this = other;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000101 return *this;
Jonathan Peyton30419822017-05-12 18:01:32 +0000102 }
103
104 uint64_t newSampleCount = sampleCount + other.sampleCount;
105 double dnsc = double(newSampleCount);
106 double dsc = double(sampleCount);
107 double dscBydnsc = dsc / dnsc;
108 double dosc = double(other.sampleCount);
109 double delta = other.meanVal - meanVal;
110
111 // Try to order these calculations to avoid overflows. If this were Fortran,
112 // then the compiler would not be able to re-order over brackets. In C++ it
113 // may be legal to do that (we certainly hope it doesn't, and CC+ Programming
114 // Language 2nd edition suggests it shouldn't, since it says that exploitation
115 // of associativity can only be made if the operation really is associative
116 // (which floating addition isn't...)).
117 meanVal = meanVal * dscBydnsc + other.meanVal * (1 - dscBydnsc);
118 m2 = m2 + other.m2 + dscBydnsc * dosc * delta * delta;
119 minVal = std::min(minVal, other.minVal);
120 maxVal = std::max(maxVal, other.maxVal);
121 sampleCount = newSampleCount;
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000122 if (collectingHist)
123 hist += other.hist;
Jonathan Peyton30419822017-05-12 18:01:32 +0000124
125 return *this;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000126}
127
Jonathan Peyton30419822017-05-12 18:01:32 +0000128void statistic::scale(double factor) {
129 minVal = minVal * factor;
130 maxVal = maxVal * factor;
131 meanVal = meanVal * factor;
132 m2 = m2 * factor * factor;
133 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000134}
135
Jonathan Peyton30419822017-05-12 18:01:32 +0000136std::string statistic::format(char unit, bool total) const {
137 std::string result = formatSI(sampleCount, 9, ' ');
Jonathan Peyton072772b2016-04-05 18:48:48 +0000138
Jonathan Peyton30419822017-05-12 18:01:32 +0000139 if (sampleCount == 0) {
140 result = result + std::string(", ") + formatSI(0.0, 9, unit);
141 result = result + std::string(", ") + formatSI(0.0, 9, unit);
142 result = result + std::string(", ") + formatSI(0.0, 9, unit);
143 if (total)
144 result = result + std::string(", ") + formatSI(0.0, 9, unit);
145 result = result + std::string(", ") + formatSI(0.0, 9, unit);
146 } else {
147 result = result + std::string(", ") + formatSI(minVal, 9, unit);
148 result = result + std::string(", ") + formatSI(meanVal, 9, unit);
149 result = result + std::string(", ") + formatSI(maxVal, 9, unit);
150 if (total)
151 result =
152 result + std::string(", ") + formatSI(meanVal * sampleCount, 9, unit);
153 result = result + std::string(", ") + formatSI(getSD(), 9, unit);
154 }
155 return result;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000156}
157
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000158/* ************* histogram member functions ************* */
159
160// Lowest bin that has anything in it
161int logHistogram::minBin() const {
162 for (int i = 0; i < numBins; i++) {
163 if (bins[i].count != 0)
164 return i - logOffset;
165 }
166 return -logOffset;
167}
168
169// Highest bin that has anything in it
170int logHistogram::maxBin() const {
171 for (int i = numBins - 1; i >= 0; i--) {
172 if (bins[i].count != 0)
173 return i - logOffset;
174 }
175 return -logOffset;
176}
177
178// Which bin does this sample belong in ?
179uint32_t logHistogram::findBin(double sample) {
180 double v = std::fabs(sample);
181 // Simply loop up looking which bin to put it in.
182 // According to a micro-architect this is likely to be faster than a binary
183 // search, since
184 // it will only have one branch mis-predict
185 for (int b = 0; b < numBins; b++)
186 if (binMax[b] > v)
187 return b;
188 fprintf(stderr,
189 "Trying to add a sample that is too large into a histogram\n");
190 KMP_ASSERT(0);
191 return -1;
192}
193
194void logHistogram::addSample(double sample) {
195 if (sample == 0.0) {
196 zeroCount += 1;
197#ifdef KMP_DEBUG
198 _total++;
199 check();
200#endif
201 return;
202 }
203 KMP_DEBUG_ASSERT(std::isfinite(sample));
204 uint32_t bin = findBin(sample);
205 KMP_DEBUG_ASSERT(0 <= bin && bin < numBins);
206
207 bins[bin].count += 1;
208 bins[bin].total += sample;
209#ifdef KMP_DEBUG
210 _total++;
211 check();
212#endif
213}
214
215// This may not be the format we want, but it'll do for now
216std::string logHistogram::format(char unit) const {
217 std::stringstream result;
218
219 result << "Bin, Count, Total\n";
220 if (zeroCount) {
221 result << "0, " << formatSI(zeroCount, 9, ' ') << ", ",
222 formatSI(0.0, 9, unit);
223 if (count(minBin()) == 0)
224 return result.str();
225 result << "\n";
226 }
227 for (int i = minBin(); i <= maxBin(); i++) {
228 result << "10**" << i << "<=v<10**" << (i + 1) << ", "
229 << formatSI(count(i), 9, ' ') << ", " << formatSI(total(i), 9, unit);
230 if (i != maxBin())
231 result << "\n";
232 }
233
234 return result.str();
235}
236
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000237/* ************* explicitTimer member functions ************* */
238
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000239void explicitTimer::start(tsc_tick_count tick) {
240 startTime = tick;
Jonathan Peyton30419822017-05-12 18:01:32 +0000241 totalPauseTime = 0;
242 if (timeStat::logEvent(timerEnumValue)) {
243 __kmp_stats_thread_ptr->incrementNestValue();
244 }
245 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000246}
247
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000248void explicitTimer::stop(tsc_tick_count tick,
Jonathan Peyton30419822017-05-12 18:01:32 +0000249 kmp_stats_list *stats_ptr /* = nullptr */) {
250 if (startTime.getValue() == 0)
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000251 return;
Jonathan Peyton30419822017-05-12 18:01:32 +0000252
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000253 stat->addSample(((tick - startTime) - totalPauseTime).ticks());
Jonathan Peyton30419822017-05-12 18:01:32 +0000254
255 if (timeStat::logEvent(timerEnumValue)) {
256 if (!stats_ptr)
257 stats_ptr = __kmp_stats_thread_ptr;
258 stats_ptr->push_event(
259 startTime.getValue() - __kmp_stats_start_time.getValue(),
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000260 tick.getValue() - __kmp_stats_start_time.getValue(),
Jonathan Peyton30419822017-05-12 18:01:32 +0000261 __kmp_stats_thread_ptr->getNestValue(), timerEnumValue);
262 stats_ptr->decrementNestValue();
263 }
264
265 /* We accept the risk that we drop a sample because it really did start at
266 t==0. */
267 startTime = 0;
268 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000269}
270
Jonathan Peyton11dc82f2016-05-05 16:15:57 +0000271/* ************* partitionedTimers member functions ************* */
Jonathan Peyton30419822017-05-12 18:01:32 +0000272partitionedTimers::partitionedTimers() { timer_stack.reserve(8); }
Jonathan Peyton11dc82f2016-05-05 16:15:57 +0000273
Jonathan Peyton11dc82f2016-05-05 16:15:57 +0000274// initialize the paritioned timers to an initial timer
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000275void partitionedTimers::init(explicitTimer timer) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000276 KMP_DEBUG_ASSERT(this->timer_stack.size() == 0);
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000277 timer_stack.push_back(timer);
278 timer_stack.back().start(tsc_tick_count::now());
Jonathan Peyton11dc82f2016-05-05 16:15:57 +0000279}
280
281// stop/save the current timer, and start the new timer (timer_pair)
282// There is a special condition where if the current timer is equal to
283// the one you are trying to push, then it only manipulates the stack,
284// and it won't stop/start the currently running timer.
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000285void partitionedTimers::push(explicitTimer timer) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000286 // get the current timer
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000287 // pause current timer
Jonathan Peyton30419822017-05-12 18:01:32 +0000288 // push new timer
289 // start the new timer
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000290 explicitTimer *current_timer, *new_timer;
291 size_t stack_size;
Jonathan Peyton30419822017-05-12 18:01:32 +0000292 KMP_DEBUG_ASSERT(this->timer_stack.size() > 0);
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000293 timer_stack.push_back(timer);
294 stack_size = timer_stack.size();
295 current_timer = &(timer_stack[stack_size - 2]);
296 new_timer = &(timer_stack[stack_size - 1]);
297 tsc_tick_count tick = tsc_tick_count::now();
298 current_timer->pause(tick);
299 new_timer->start(tick);
Jonathan Peyton11dc82f2016-05-05 16:15:57 +0000300}
301
302// stop/discard the current timer, and start the previously saved timer
303void partitionedTimers::pop() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000304 // get the current timer
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000305 // stop current timer (record event/sample)
Jonathan Peyton30419822017-05-12 18:01:32 +0000306 // pop current timer
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000307 // get the new current timer and resume
308 explicitTimer *old_timer, *new_timer;
309 size_t stack_size = timer_stack.size();
310 KMP_DEBUG_ASSERT(stack_size > 1);
311 old_timer = &(timer_stack[stack_size - 1]);
312 new_timer = &(timer_stack[stack_size - 2]);
313 tsc_tick_count tick = tsc_tick_count::now();
314 old_timer->stop(tick);
315 new_timer->resume(tick);
Jonathan Peyton30419822017-05-12 18:01:32 +0000316 timer_stack.pop_back();
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000317}
318
319void partitionedTimers::exchange(explicitTimer timer) {
320 // get the current timer
321 // stop current timer (record event/sample)
322 // push new timer
323 // start the new timer
324 explicitTimer *current_timer, *new_timer;
325 size_t stack_size;
326 KMP_DEBUG_ASSERT(this->timer_stack.size() > 0);
327 tsc_tick_count tick = tsc_tick_count::now();
328 stack_size = timer_stack.size();
329 current_timer = &(timer_stack[stack_size - 1]);
330 current_timer->stop(tick);
331 timer_stack.pop_back();
332 timer_stack.push_back(timer);
333 new_timer = &(timer_stack[stack_size - 1]);
334 new_timer->start(tick);
Jonathan Peyton11dc82f2016-05-05 16:15:57 +0000335}
336
337// Wind up all the currently running timers.
338// This pops off all the timers from the stack and clears the stack
339// After this is called, init() must be run again to initialize the
340// stack of timers
341void partitionedTimers::windup() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000342 while (timer_stack.size() > 1) {
343 this->pop();
344 }
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000345 // Pop the timer from the init() call
Jonathan Peyton30419822017-05-12 18:01:32 +0000346 if (timer_stack.size() > 0) {
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000347 timer_stack.back().stop(tsc_tick_count::now());
Jonathan Peyton30419822017-05-12 18:01:32 +0000348 timer_stack.pop_back();
Jonathan Peyton30419822017-05-12 18:01:32 +0000349 }
Jonathan Peyton11dc82f2016-05-05 16:15:57 +0000350}
351
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000352/* ************* kmp_stats_event_vector member functions ************* */
353
354void kmp_stats_event_vector::deallocate() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000355 __kmp_free(events);
356 internal_size = 0;
357 allocated_size = 0;
358 events = NULL;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000359}
360
361// This function is for qsort() which requires the compare function to return
Jonathan Peyton30419822017-05-12 18:01:32 +0000362// either a negative number if event1 < event2, a positive number if event1 >
363// event2 or zero if event1 == event2. This sorts by start time (lowest to
364// highest).
365int compare_two_events(const void *event1, const void *event2) {
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000366 const kmp_stats_event *ev1 = RCAST(const kmp_stats_event *, event1);
367 const kmp_stats_event *ev2 = RCAST(const kmp_stats_event *, event2);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000368
Jonathan Peyton30419822017-05-12 18:01:32 +0000369 if (ev1->getStart() < ev2->getStart())
370 return -1;
371 else if (ev1->getStart() > ev2->getStart())
372 return 1;
373 else
374 return 0;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000375}
376
377void kmp_stats_event_vector::sort() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000378 qsort(events, internal_size, sizeof(kmp_stats_event), compare_two_events);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000379}
380
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000381/* ************* kmp_stats_list member functions ************* */
382
383// returns a pointer to newly created stats node
Jonathan Peyton30419822017-05-12 18:01:32 +0000384kmp_stats_list *kmp_stats_list::push_back(int gtid) {
385 kmp_stats_list *newnode =
386 (kmp_stats_list *)__kmp_allocate(sizeof(kmp_stats_list));
387 // placement new, only requires space and pointer and initializes (so
388 // __kmp_allocate instead of C++ new[] is used)
389 new (newnode) kmp_stats_list();
390 newnode->setGtid(gtid);
391 newnode->prev = this->prev;
392 newnode->next = this;
393 newnode->prev->next = newnode;
394 newnode->next->prev = newnode;
395 return newnode;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000396}
397void kmp_stats_list::deallocate() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000398 kmp_stats_list *ptr = this->next;
399 kmp_stats_list *delptr = this->next;
400 while (ptr != this) {
401 delptr = ptr;
402 ptr = ptr->next;
403 // placement new means we have to explicitly call destructor.
404 delptr->_event_vector.deallocate();
405 delptr->~kmp_stats_list();
406 __kmp_free(delptr);
407 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000408}
409kmp_stats_list::iterator kmp_stats_list::begin() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000410 kmp_stats_list::iterator it;
411 it.ptr = this->next;
412 return it;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000413}
414kmp_stats_list::iterator kmp_stats_list::end() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000415 kmp_stats_list::iterator it;
416 it.ptr = this;
417 return it;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000418}
419int kmp_stats_list::size() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000420 int retval;
421 kmp_stats_list::iterator it;
422 for (retval = 0, it = begin(); it != end(); it++, retval++) {
423 }
424 return retval;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000425}
426
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000427/* ************* kmp_stats_list::iterator member functions ************* */
428
Jonathan Peyton072772b2016-04-05 18:48:48 +0000429kmp_stats_list::iterator::iterator() : ptr(NULL) {}
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000430kmp_stats_list::iterator::~iterator() {}
431kmp_stats_list::iterator kmp_stats_list::iterator::operator++() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000432 this->ptr = this->ptr->next;
433 return *this;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000434}
435kmp_stats_list::iterator kmp_stats_list::iterator::operator++(int dummy) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000436 this->ptr = this->ptr->next;
437 return *this;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000438}
439kmp_stats_list::iterator kmp_stats_list::iterator::operator--() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000440 this->ptr = this->ptr->prev;
441 return *this;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000442}
443kmp_stats_list::iterator kmp_stats_list::iterator::operator--(int dummy) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000444 this->ptr = this->ptr->prev;
445 return *this;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000446}
Jonathan Peyton30419822017-05-12 18:01:32 +0000447bool kmp_stats_list::iterator::operator!=(const kmp_stats_list::iterator &rhs) {
448 return this->ptr != rhs.ptr;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000449}
Jonathan Peyton30419822017-05-12 18:01:32 +0000450bool kmp_stats_list::iterator::operator==(const kmp_stats_list::iterator &rhs) {
451 return this->ptr == rhs.ptr;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000452}
Jonathan Peyton30419822017-05-12 18:01:32 +0000453kmp_stats_list *kmp_stats_list::iterator::operator*() const {
454 return this->ptr;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000455}
456
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000457/* ************* kmp_stats_output_module functions ************** */
458
Jonathan Peyton30419822017-05-12 18:01:32 +0000459const char *kmp_stats_output_module::eventsFileName = NULL;
460const char *kmp_stats_output_module::plotFileName = NULL;
461int kmp_stats_output_module::printPerThreadFlag = 0;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000462int kmp_stats_output_module::printPerThreadEventsFlag = 0;
463
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000464static char const *lastName(char *name) {
465 int l = strlen(name);
466 for (int i = l - 1; i >= 0; --i) {
467 if (name[i] == '.')
468 name[i] = '_';
469 if (name[i] == '/')
470 return name + i + 1;
471 }
472 return name;
473}
474
475/* Read the name of the executable from /proc/self/cmdline */
476static char const *getImageName(char *buffer, size_t buflen) {
477 FILE *f = fopen("/proc/self/cmdline", "r");
478 buffer[0] = char(0);
479 if (!f)
480 return buffer;
481
482 // The file contains char(0) delimited words from the commandline.
483 // This just returns the last filename component of the first word on the
484 // line.
485 size_t n = fread(buffer, 1, buflen, f);
486 if (n == 0) {
487 fclose(f);
488 KMP_CHECK_SYSFAIL("fread", 1)
489 }
490 fclose(f);
491 buffer[buflen - 1] = char(0);
492 return lastName(buffer);
493}
494
495static void getTime(char *buffer, size_t buflen, bool underscores = false) {
496 time_t timer;
497
498 time(&timer);
499
500 struct tm *tm_info = localtime(&timer);
501 if (underscores)
502 strftime(buffer, buflen, "%Y-%m-%d_%H%M%S", tm_info);
503 else
504 strftime(buffer, buflen, "%Y-%m-%d %H%M%S", tm_info);
505}
506
507/* Generate a stats file name, expanding prototypes */
508static std::string generateFilename(char const *prototype,
509 char const *imageName) {
510 std::string res;
511
512 for (int i = 0; prototype[i] != char(0); i++) {
513 char ch = prototype[i];
514
515 if (ch == '%') {
516 i++;
517 if (prototype[i] == char(0))
518 break;
519
520 switch (prototype[i]) {
521 case 't': // Insert time and date
522 {
523 char date[26];
524 getTime(date, sizeof(date), true);
525 res += date;
526 } break;
527 case 'e': // Insert executable name
528 res += imageName;
529 break;
530 case 'p': // Insert pid
531 {
532 std::stringstream ss;
533 ss << getpid();
534 res += ss.str();
535 } break;
536 default:
537 res += prototype[i];
538 break;
539 }
540 } else
541 res += ch;
542 }
543 return res;
544}
545
Jonathan Peyton30419822017-05-12 18:01:32 +0000546// init() is called very near the beginning of execution time in the constructor
547// of __kmp_stats_global_output
548void kmp_stats_output_module::init() {
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000549
550 fprintf(stderr, "*** Stats enabled OpenMP* runtime ***\n");
Jonathan Peyton30419822017-05-12 18:01:32 +0000551 char *statsFileName = getenv("KMP_STATS_FILE");
552 eventsFileName = getenv("KMP_STATS_EVENTS_FILE");
553 plotFileName = getenv("KMP_STATS_PLOT_FILE");
554 char *threadStats = getenv("KMP_STATS_THREADS");
555 char *threadEvents = getenv("KMP_STATS_EVENTS");
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000556
Jonathan Peyton30419822017-05-12 18:01:32 +0000557 // set the stats output filenames based on environment variables and defaults
558 if (statsFileName) {
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000559 char imageName[1024];
560 // Process any escapes (e.g., %p, %e, %t) in the name
561 outputFileName = generateFilename(
562 statsFileName, getImageName(&imageName[0], sizeof(imageName)));
Jonathan Peyton30419822017-05-12 18:01:32 +0000563 }
564 eventsFileName = eventsFileName ? eventsFileName : "events.dat";
565 plotFileName = plotFileName ? plotFileName : "events.plt";
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000566
Jonathan Peyton30419822017-05-12 18:01:32 +0000567 // set the flags based on environment variables matching: true, on, 1, .true.
568 // , .t. , yes
569 printPerThreadFlag = __kmp_str_match_true(threadStats);
570 printPerThreadEventsFlag = __kmp_str_match_true(threadEvents);
571
572 if (printPerThreadEventsFlag) {
573 // assigns a color to each timer for printing
574 setupEventColors();
575 } else {
576 // will clear flag so that no event will be logged
577 timeStat::clearEventFlags();
578 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000579}
580
581void kmp_stats_output_module::setupEventColors() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000582 int i;
583 int globalColorIndex = 0;
584 int numGlobalColors = sizeof(globalColorArray) / sizeof(rgb_color);
585 for (i = 0; i < TIMER_LAST; i++) {
586 if (timeStat::logEvent((timer_e)i)) {
587 timerColorInfo[i] = globalColorArray[globalColorIndex];
588 globalColorIndex = (globalColorIndex + 1) % numGlobalColors;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000589 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000590 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000591}
592
Jonathan Peyton30419822017-05-12 18:01:32 +0000593void kmp_stats_output_module::printTimerStats(FILE *statsOut,
594 statistic const *theStats,
595 statistic const *totalStats) {
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000596 fprintf(statsOut,
597 "Timer, SampleCount, Min, "
598 "Mean, Max, Total, SD\n");
Jonathan Peyton30419822017-05-12 18:01:32 +0000599 for (timer_e s = timer_e(0); s < TIMER_LAST; s = timer_e(s + 1)) {
600 statistic const *stat = &theStats[s];
601 char tag = timeStat::noUnits(s) ? ' ' : 'T';
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000602
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000603 fprintf(statsOut, "%-35s, %s\n", timeStat::name(s),
Jonathan Peyton30419822017-05-12 18:01:32 +0000604 stat->format(tag, true).c_str());
605 }
606 // Also print the Total_ versions of times.
607 for (timer_e s = timer_e(0); s < TIMER_LAST; s = timer_e(s + 1)) {
608 char tag = timeStat::noUnits(s) ? ' ' : 'T';
609 if (totalStats && !timeStat::noTotal(s))
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000610 fprintf(statsOut, "Total_%-29s, %s\n", timeStat::name(s),
Jonathan Peyton30419822017-05-12 18:01:32 +0000611 totalStats[s].format(tag, true).c_str());
612 }
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000613
614 // Print historgram of statistics
615 if (theStats[0].haveHist()) {
616 fprintf(statsOut, "\nTimer distributions\n");
617 for (int s = 0; s < TIMER_LAST; s++) {
618 statistic const *stat = &theStats[s];
619
620 if (stat->getCount() != 0) {
621 char tag = timeStat::noUnits(timer_e(s)) ? ' ' : 'T';
622
623 fprintf(statsOut, "%s\n", timeStat::name(timer_e(s)));
624 fprintf(statsOut, "%s\n", stat->getHist()->format(tag).c_str());
625 }
626 }
627 }
Jonathan Peytone2554af2016-03-11 20:20:49 +0000628}
629
Jonathan Peyton30419822017-05-12 18:01:32 +0000630void kmp_stats_output_module::printCounterStats(FILE *statsOut,
631 statistic const *theStats) {
632 fprintf(statsOut, "Counter, ThreadCount, Min, Mean, "
633 " Max, Total, SD\n");
634 for (int s = 0; s < COUNTER_LAST; s++) {
635 statistic const *stat = &theStats[s];
636 fprintf(statsOut, "%-25s, %s\n", counter::name(counter_e(s)),
637 stat->format(' ', true).c_str());
638 }
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000639 // Print histogram of counters
640 if (theStats[0].haveHist()) {
641 fprintf(statsOut, "\nCounter distributions\n");
642 for (int s = 0; s < COUNTER_LAST; s++) {
643 statistic const *stat = &theStats[s];
644
645 if (stat->getCount() != 0) {
646 fprintf(statsOut, "%s\n", counter::name(counter_e(s)));
647 fprintf(statsOut, "%s\n", stat->getHist()->format(' ').c_str());
648 }
649 }
650 }
Jonathan Peytone2554af2016-03-11 20:20:49 +0000651}
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000652
Jonathan Peyton30419822017-05-12 18:01:32 +0000653void kmp_stats_output_module::printCounters(FILE *statsOut,
654 counter const *theCounters) {
655 // We print all the counters even if they are zero.
656 // That makes it easier to slice them into a spreadsheet if you need to.
657 fprintf(statsOut, "\nCounter, Count\n");
658 for (int c = 0; c < COUNTER_LAST; c++) {
659 counter const *stat = &theCounters[c];
660 fprintf(statsOut, "%-25s, %s\n", counter::name(counter_e(c)),
661 formatSI(stat->getValue(), 9, ' ').c_str());
662 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000663}
664
Jonathan Peyton30419822017-05-12 18:01:32 +0000665void kmp_stats_output_module::printEvents(FILE *eventsOut,
666 kmp_stats_event_vector *theEvents,
667 int gtid) {
668 // sort by start time before printing
669 theEvents->sort();
670 for (int i = 0; i < theEvents->size(); i++) {
671 kmp_stats_event ev = theEvents->at(i);
672 rgb_color color = getEventColor(ev.getTimerName());
673 fprintf(eventsOut, "%d %lu %lu %1.1f rgb(%1.1f,%1.1f,%1.1f) %s\n", gtid,
674 ev.getStart(), ev.getStop(), 1.2 - (ev.getNestLevel() * 0.2),
675 color.r, color.g, color.b, timeStat::name(ev.getTimerName()));
676 }
677 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000678}
679
Jonathan Peyton30419822017-05-12 18:01:32 +0000680void kmp_stats_output_module::windupExplicitTimers() {
681 // Wind up any explicit timers. We assume that it's fair at this point to just
682 // walk all the explcit timers in all threads and say "it's over".
683 // If the timer wasn't running, this won't record anything anyway.
684 kmp_stats_list::iterator it;
685 for (it = __kmp_stats_list->begin(); it != __kmp_stats_list->end(); it++) {
686 kmp_stats_list *ptr = *it;
687 ptr->getPartitionedTimers()->windup();
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000688 ptr->endLife();
Jonathan Peyton30419822017-05-12 18:01:32 +0000689 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000690}
691
692void kmp_stats_output_module::printPloticusFile() {
Jonathan Peyton30419822017-05-12 18:01:32 +0000693 int i;
694 int size = __kmp_stats_list->size();
695 FILE *plotOut = fopen(plotFileName, "w+");
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000696
Jonathan Peyton30419822017-05-12 18:01:32 +0000697 fprintf(plotOut, "#proc page\n"
698 " pagesize: 15 10\n"
699 " scale: 1.0\n\n");
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000700
Jonathan Peyton30419822017-05-12 18:01:32 +0000701 fprintf(plotOut, "#proc getdata\n"
702 " file: %s\n\n",
703 eventsFileName);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000704
Jonathan Peyton30419822017-05-12 18:01:32 +0000705 fprintf(plotOut, "#proc areadef\n"
706 " title: OpenMP Sampling Timeline\n"
707 " titledetails: align=center size=16\n"
708 " rectangle: 1 1 13 9\n"
709 " xautorange: datafield=2,3\n"
710 " yautorange: -1 %d\n\n",
711 size);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000712
Jonathan Peyton30419822017-05-12 18:01:32 +0000713 fprintf(plotOut, "#proc xaxis\n"
714 " stubs: inc\n"
715 " stubdetails: size=12\n"
716 " label: Time (ticks)\n"
717 " labeldetails: size=14\n\n");
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000718
Jonathan Peyton30419822017-05-12 18:01:32 +0000719 fprintf(plotOut, "#proc yaxis\n"
720 " stubs: inc 1\n"
721 " stubrange: 0 %d\n"
722 " stubdetails: size=12\n"
723 " label: Thread #\n"
724 " labeldetails: size=14\n\n",
725 size - 1);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000726
Jonathan Peyton30419822017-05-12 18:01:32 +0000727 fprintf(plotOut, "#proc bars\n"
728 " exactcolorfield: 5\n"
729 " axis: x\n"
730 " locfield: 1\n"
731 " segmentfields: 2 3\n"
732 " barwidthfield: 4\n\n");
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000733
Jonathan Peyton30419822017-05-12 18:01:32 +0000734 // create legend entries corresponding to the timer color
735 for (i = 0; i < TIMER_LAST; i++) {
736 if (timeStat::logEvent((timer_e)i)) {
737 rgb_color c = getEventColor((timer_e)i);
738 fprintf(plotOut, "#proc legendentry\n"
739 " sampletype: color\n"
740 " label: %s\n"
741 " details: rgb(%1.1f,%1.1f,%1.1f)\n\n",
742 timeStat::name((timer_e)i), c.r, c.g, c.b);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000743 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000744 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000745
Jonathan Peyton30419822017-05-12 18:01:32 +0000746 fprintf(plotOut, "#proc legend\n"
747 " format: down\n"
748 " location: max max\n\n");
749 fclose(plotOut);
750 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000751}
752
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000753static void outputEnvVariable(FILE *statsOut, char const *name) {
754 char const *value = getenv(name);
755 fprintf(statsOut, "# %s = %s\n", name, value ? value : "*unspecified*");
756}
757
Jonathan Peyton30419822017-05-12 18:01:32 +0000758/* Print some useful information about
759 * the date and time this experiment ran.
760 * the machine on which it ran.
761 We output all of this as stylised comments, though we may decide to parse
762 some of it. */
763void kmp_stats_output_module::printHeaderInfo(FILE *statsOut) {
764 std::time_t now = std::time(0);
765 char buffer[40];
766 char hostName[80];
Jonathan Peyton6e98d7982016-03-15 20:28:47 +0000767
Jonathan Peyton30419822017-05-12 18:01:32 +0000768 std::strftime(&buffer[0], sizeof(buffer), "%c", std::localtime(&now));
769 fprintf(statsOut, "# Time of run: %s\n", &buffer[0]);
770 if (gethostname(&hostName[0], sizeof(hostName)) == 0)
771 fprintf(statsOut, "# Hostname: %s\n", &hostName[0]);
Jonathan Peyton6e98d7982016-03-15 20:28:47 +0000772#if KMP_ARCH_X86 || KMP_ARCH_X86_64
Jonathan Peyton30419822017-05-12 18:01:32 +0000773 fprintf(statsOut, "# CPU: %s\n", &__kmp_cpuinfo.name[0]);
774 fprintf(statsOut, "# Family: %d, Model: %d, Stepping: %d\n",
775 __kmp_cpuinfo.family, __kmp_cpuinfo.model, __kmp_cpuinfo.stepping);
776 if (__kmp_cpuinfo.frequency == 0)
777 fprintf(statsOut, "# Nominal frequency: Unknown\n");
778 else
779 fprintf(statsOut, "# Nominal frequency: %sz\n",
780 formatSI(double(__kmp_cpuinfo.frequency), 9, 'H').c_str());
Jonathan Peytonf0682ac2018-07-30 17:41:08 +0000781 outputEnvVariable(statsOut, "KMP_HW_SUBSET");
782 outputEnvVariable(statsOut, "KMP_AFFINITY");
783 outputEnvVariable(statsOut, "KMP_BLOCKTIME");
784 outputEnvVariable(statsOut, "KMP_LIBRARY");
785 fprintf(statsOut, "# Production runtime built " __DATE__ " " __TIME__ "\n");
Jonathan Peyton6e98d7982016-03-15 20:28:47 +0000786#endif
787}
788
Jonathan Peyton30419822017-05-12 18:01:32 +0000789void kmp_stats_output_module::outputStats(const char *heading) {
790 // Stop all the explicit timers in all threads
791 // Do this before declaring the local statistics because thay have
792 // constructors so will take time to create.
793 windupExplicitTimers();
Jonathan Peyton6e98d7982016-03-15 20:28:47 +0000794
Jonathan Peyton30419822017-05-12 18:01:32 +0000795 statistic allStats[TIMER_LAST];
796 statistic totalStats[TIMER_LAST]; /* Synthesized, cross threads versions of
797 normal timer stats */
798 statistic allCounters[COUNTER_LAST];
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000799
Jonathan Peyton30419822017-05-12 18:01:32 +0000800 FILE *statsOut =
801 !outputFileName.empty() ? fopen(outputFileName.c_str(), "a+") : stderr;
802 if (!statsOut)
803 statsOut = stderr;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000804
Jonathan Peyton30419822017-05-12 18:01:32 +0000805 FILE *eventsOut;
806 if (eventPrintingEnabled()) {
807 eventsOut = fopen(eventsFileName, "w+");
808 }
809
810 printHeaderInfo(statsOut);
811 fprintf(statsOut, "%s\n", heading);
812 // Accumulate across threads.
813 kmp_stats_list::iterator it;
814 for (it = __kmp_stats_list->begin(); it != __kmp_stats_list->end(); it++) {
815 int t = (*it)->getGtid();
816 // Output per thread stats if requested.
817 if (printPerThreadFlag) {
818 fprintf(statsOut, "Thread %d\n", t);
819 printTimerStats(statsOut, (*it)->getTimers(), 0);
820 printCounters(statsOut, (*it)->getCounters());
821 fprintf(statsOut, "\n");
822 }
823 // Output per thread events if requested.
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000824 if (eventPrintingEnabled()) {
Jonathan Peyton30419822017-05-12 18:01:32 +0000825 kmp_stats_event_vector events = (*it)->getEventVector();
826 printEvents(eventsOut, &events, t);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000827 }
828
Jonathan Peyton30419822017-05-12 18:01:32 +0000829 // Accumulate timers.
830 for (timer_e s = timer_e(0); s < TIMER_LAST; s = timer_e(s + 1)) {
831 // See if we should ignore this timer when aggregating
832 if ((timeStat::masterOnly(s) && (t != 0)) || // Timer only valid on master
833 // and this thread is worker
834 (timeStat::workerOnly(s) && (t == 0)) // Timer only valid on worker
835 // and this thread is the master
836 ) {
837 continue;
838 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000839
Jonathan Peyton30419822017-05-12 18:01:32 +0000840 statistic *threadStat = (*it)->getTimer(s);
841 allStats[s] += *threadStat;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000842
Jonathan Peyton30419822017-05-12 18:01:32 +0000843 // Add Total stats for timers that are valid in more than one thread
844 if (!timeStat::noTotal(s))
845 totalStats[s].addSample(threadStat->getTotal());
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000846 }
847
Jonathan Peyton30419822017-05-12 18:01:32 +0000848 // Accumulate counters.
849 for (counter_e c = counter_e(0); c < COUNTER_LAST; c = counter_e(c + 1)) {
850 if (counter::masterOnly(c) && t != 0)
851 continue;
852 allCounters[c].addSample((*it)->getCounter(c)->getValue());
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000853 }
Jonathan Peyton30419822017-05-12 18:01:32 +0000854 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000855
Jonathan Peyton30419822017-05-12 18:01:32 +0000856 if (eventPrintingEnabled()) {
857 printPloticusFile();
858 fclose(eventsOut);
859 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000860
Jonathan Peyton30419822017-05-12 18:01:32 +0000861 fprintf(statsOut, "Aggregate for all threads\n");
862 printTimerStats(statsOut, &allStats[0], &totalStats[0]);
863 fprintf(statsOut, "\n");
864 printCounterStats(statsOut, &allCounters[0]);
865
866 if (statsOut != stderr)
867 fclose(statsOut);
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000868}
869
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000870/* ************* exported C functions ************** */
871
Jonathan Peyton30419822017-05-12 18:01:32 +0000872// no name mangling for these functions, we want the c files to be able to get
873// at these functions
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000874extern "C" {
875
Jonathan Peyton30419822017-05-12 18:01:32 +0000876void __kmp_reset_stats() {
877 kmp_stats_list::iterator it;
878 for (it = __kmp_stats_list->begin(); it != __kmp_stats_list->end(); it++) {
879 timeStat *timers = (*it)->getTimers();
880 counter *counters = (*it)->getCounters();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000881
Jonathan Peyton30419822017-05-12 18:01:32 +0000882 for (int t = 0; t < TIMER_LAST; t++)
883 timers[t].reset();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000884
Jonathan Peyton30419822017-05-12 18:01:32 +0000885 for (int c = 0; c < COUNTER_LAST; c++)
886 counters[c].reset();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000887
Jonathan Peyton30419822017-05-12 18:01:32 +0000888 // reset the event vector so all previous events are "erased"
889 (*it)->resetEventVector();
890 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000891}
892
Jonathan Peyton30419822017-05-12 18:01:32 +0000893// This function will reset all stats and stop all threads' explicit timers if
894// they haven't been stopped already.
895void __kmp_output_stats(const char *heading) {
896 __kmp_stats_global_output->outputStats(heading);
897 __kmp_reset_stats();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000898}
899
Jonathan Peyton30419822017-05-12 18:01:32 +0000900void __kmp_accumulate_stats_at_exit(void) {
901 // Only do this once.
902 if (KMP_XCHG_FIXED32(&statsPrinted, 1) != 0)
903 return;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000904
Jonathan Peyton30419822017-05-12 18:01:32 +0000905 __kmp_output_stats("Statistics on exit");
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000906}
907
Jonathan Peyton30419822017-05-12 18:01:32 +0000908void __kmp_stats_init(void) {
909 __kmp_init_tas_lock(&__kmp_stats_lock);
910 __kmp_stats_start_time = tsc_tick_count::now();
911 __kmp_stats_global_output = new kmp_stats_output_module();
912 __kmp_stats_list = new kmp_stats_list();
Jonathan Peyton5375fe82016-11-14 21:13:44 +0000913}
914
Jonathan Peyton30419822017-05-12 18:01:32 +0000915void __kmp_stats_fini(void) {
916 __kmp_accumulate_stats_at_exit();
917 __kmp_stats_list->deallocate();
918 delete __kmp_stats_global_output;
919 delete __kmp_stats_list;
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000920}
921
Jonathan Peyton072772b2016-04-05 18:48:48 +0000922} // extern "C"