1 /** @file kmp_stats_timing.cpp
5 //===----------------------------------------------------------------------===//
7 // The LLVM Compiler Infrastructure
9 // This file is dual licensed under the MIT and the University of Illinois Open
10 // Source Licenses. See LICENSE.txt for details.
12 //===----------------------------------------------------------------------===//
22 #include "kmp_stats_timing.h"
26 #if KMP_HAVE_TICK_TIME
28 double tsc_tick_count::tick_time() {
29 // pretty bad assumption of 1GHz clock for MIC
30 return 1 / ((double)1000 * 1.e6);
32 #elif KMP_ARCH_X86 || KMP_ARCH_X86_64
34 // Extract the value from the CPUID information
35 double tsc_tick_count::tick_time() {
36 static double result = 0.0;
42 __kmp_x86_cpuid(0x80000000, 0, &cpuinfo);
43 memset(brand, 0, sizeof(brand));
44 int ids = cpuinfo.eax;
46 for (unsigned int i = 2; i < (ids ^ 0x80000000) + 2; i++)
47 __kmp_x86_cpuid(i | 0x80000000, 0,
48 (kmp_cpuid_t *)(brand + (i - 2) * sizeof(kmp_cpuid_t)));
50 char *start = &brand[0];
51 for (; *start == ' '; start++)
54 char *end = brand + KMP_STRLEN(brand) - 3;
58 multiplier = 1000LL * 1000LL;
60 multiplier = 1000LL * 1000LL * 1000LL;
62 multiplier = 1000LL * 1000LL * 1000LL * 1000LL;
64 cout << "Error determining multiplier '" << *end << "'\n";
72 double freq = strtod(end, &start);
74 cout << "Error calculating frequency " << end << "\n";
78 result = ((double)1.0) / (freq * multiplier);
85 static bool useSI = true;
87 // Return a formatted string after normalising the value into
88 // engineering style and using a suitable unit prefix (e.g. ms, us, ns).
89 std::string formatSI(double interval, int width, char unit) {
93 // Preserve accuracy for small numbers, since we only multiply and the
94 // positive powers of ten are precisely representable.
98 } ranges[] = {{1.e21, 'y'}, {1.e18, 'z'}, {1.e15, 'a'}, {1.e12, 'f'},
99 {1.e9, 'p'}, {1.e6, 'n'}, {1.e3, 'u'}, {1.0, 'm'},
100 {1.e-3, ' '}, {1.e-6, 'k'}, {1.e-9, 'M'}, {1.e-12, 'G'},
101 {1.e-15, 'T'}, {1.e-18, 'P'}, {1.e-21, 'E'}, {1.e-24, 'Z'},
104 if (interval == 0.0) {
105 os << std::setw(width - 3) << std::right << "0.00" << std::setw(3)
110 bool negative = false;
111 if (interval < 0.0) {
113 interval = -interval;
116 for (int i = 0; i < (int)(sizeof(ranges) / sizeof(ranges[0])); i++) {
117 if (interval * ranges[i].scale < 1.e0) {
118 interval = interval * 1000.e0 * ranges[i].scale;
119 os << std::fixed << std::setprecision(2) << std::setw(width - 3)
120 << std::right << (negative ? -interval : interval) << std::setw(2)
121 << ranges[i].prefix << std::setw(1) << unit;
127 os << std::setprecision(2) << std::fixed << std::right << std::setw(width - 3)
128 << interval << std::setw(3) << unit;