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