LLVM OpenMP* Runtime Library
kmp_stats_timing.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 
16 #include <stdlib.h>
17 #include <unistd.h>
18 
19 #include <iomanip>
20 #include <iostream>
21 #include <sstream>
22 
23 #include "kmp.h"
24 #include "kmp_stats_timing.h"
25 
26 using namespace std;
27 
28 #if KMP_HAVE_TICK_TIME
29 #if KMP_MIC
30 double tsc_tick_count::tick_time() {
31  // pretty bad assumption of 1GHz clock for MIC
32  return 1 / ((double)1000 * 1.e6);
33 }
34 #elif KMP_ARCH_X86 || KMP_ARCH_X86_64
35 #include <string.h>
36 // Extract the value from the CPUID information
37 double tsc_tick_count::tick_time() {
38  static double result = 0.0;
39 
40  if (result == 0.0) {
41  kmp_cpuid_t cpuinfo;
42  char brand[256];
43 
44  __kmp_x86_cpuid(0x80000000, 0, &cpuinfo);
45  memset(brand, 0, sizeof(brand));
46  int ids = cpuinfo.eax;
47 
48  for (unsigned int i = 2; i < (ids ^ 0x80000000) + 2; i++)
49  __kmp_x86_cpuid(i | 0x80000000, 0,
50  (kmp_cpuid_t *)(brand + (i - 2) * sizeof(kmp_cpuid_t)));
51 
52  char *start = &brand[0];
53  for (; *start == ' '; start++)
54  ;
55 
56  char *end = brand + KMP_STRLEN(brand) - 3;
57  uint64_t multiplier;
58 
59  if (*end == 'M')
60  multiplier = 1000LL * 1000LL;
61  else if (*end == 'G')
62  multiplier = 1000LL * 1000LL * 1000LL;
63  else if (*end == 'T')
64  multiplier = 1000LL * 1000LL * 1000LL * 1000LL;
65  else {
66  cout << "Error determining multiplier '" << *end << "'\n";
67  exit(-1);
68  }
69  *end = 0;
70  while (*end != ' ')
71  end--;
72  end++;
73 
74  double freq = strtod(end, &start);
75  if (freq == 0.0) {
76  cout << "Error calculating frequency " << end << "\n";
77  exit(-1);
78  }
79 
80  result = ((double)1.0) / (freq * multiplier);
81  }
82  return result;
83 }
84 #endif
85 #endif
86 
87 static bool useSI = true;
88 
89 // Return a formatted string after normalising the value into
90 // engineering style and using a suitable unit prefix (e.g. ms, us, ns).
91 std::string formatSI(double interval, int width, char unit) {
92  std::stringstream os;
93 
94  if (useSI) {
95  // Preserve accuracy for small numbers, since we only multiply and the
96  // positive powers of ten are precisely representable.
97  static struct {
98  double scale;
99  char prefix;
100  } ranges[] = {{1.e12, 'f'}, {1.e9, 'p'}, {1.e6, 'n'}, {1.e3, 'u'},
101  {1.0, 'm'}, {1.e-3, ' '}, {1.e-6, 'k'}, {1.e-9, 'M'},
102  {1.e-12, 'G'}, {1.e-15, 'T'}, {1.e-18, 'P'}, {1.e-21, 'E'},
103  {1.e-24, 'Z'}, {1.e-27, 'Y'}};
104 
105  if (interval == 0.0) {
106  os << std::setw(width - 3) << std::right << "0.00" << std::setw(3)
107  << unit;
108  return os.str();
109  }
110 
111  bool negative = false;
112  if (interval < 0.0) {
113  negative = true;
114  interval = -interval;
115  }
116 
117  for (int i = 0; i < (int)(sizeof(ranges) / sizeof(ranges[0])); i++) {
118  if (interval * ranges[i].scale < 1.e0) {
119  interval = interval * 1000.e0 * ranges[i].scale;
120  os << std::fixed << std::setprecision(2) << std::setw(width - 3)
121  << std::right << (negative ? -interval : interval) << std::setw(2)
122  << ranges[i].prefix << std::setw(1) << unit;
123 
124  return os.str();
125  }
126  }
127  }
128  os << std::setprecision(2) << std::fixed << std::right << std::setw(width - 3)
129  << interval << std::setw(3) << unit;
130 
131  return os.str();
132 }