]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/openmp/runtime/src/kmp_stats_timing.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / openmp / runtime / src / kmp_stats_timing.cpp
1 /** @file kmp_stats_timing.cpp
2  * Timing functions
3  */
4
5 //===----------------------------------------------------------------------===//
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
14 #include <stdlib.h>
15 #include <unistd.h>
16
17 #include <iomanip>
18 #include <iostream>
19 #include <sstream>
20
21 #include "kmp.h"
22 #include "kmp_stats_timing.h"
23
24 using namespace std;
25
26 #if KMP_HAVE_TICK_TIME
27 #if KMP_MIC
28 double tsc_tick_count::tick_time() {
29   // pretty bad assumption of 1GHz clock for MIC
30   return 1 / ((double)1000 * 1.e6);
31 }
32 #elif KMP_ARCH_X86 || KMP_ARCH_X86_64
33 #include <string.h>
34 // Extract the value from the CPUID information
35 double tsc_tick_count::tick_time() {
36   static double result = 0.0;
37
38   if (result == 0.0) {
39     kmp_cpuid_t cpuinfo;
40     char brand[256];
41
42     __kmp_x86_cpuid(0x80000000, 0, &cpuinfo);
43     memset(brand, 0, sizeof(brand));
44     int ids = cpuinfo.eax;
45
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)));
49
50     char *start = &brand[0];
51     for (; *start == ' '; start++)
52       ;
53
54     char *end = brand + KMP_STRLEN(brand) - 3;
55     uint64_t multiplier;
56
57     if (*end == 'M')
58       multiplier = 1000LL * 1000LL;
59     else if (*end == 'G')
60       multiplier = 1000LL * 1000LL * 1000LL;
61     else if (*end == 'T')
62       multiplier = 1000LL * 1000LL * 1000LL * 1000LL;
63     else {
64       cout << "Error determining multiplier '" << *end << "'\n";
65       exit(-1);
66     }
67     *end = 0;
68     while (*end != ' ')
69       end--;
70     end++;
71
72     double freq = strtod(end, &start);
73     if (freq == 0.0) {
74       cout << "Error calculating frequency " << end << "\n";
75       exit(-1);
76     }
77
78     result = ((double)1.0) / (freq * multiplier);
79   }
80   return result;
81 }
82 #endif
83 #endif
84
85 static bool useSI = true;
86
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) {
90   std::stringstream os;
91
92   if (useSI) {
93     // Preserve accuracy for small numbers, since we only multiply and the
94     // positive powers of ten are precisely representable.
95     static struct {
96       double scale;
97       char prefix;
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'},
102                   {1.e-27, 'Y'}};
103
104     if (interval == 0.0) {
105       os << std::setw(width - 3) << std::right << "0.00" << std::setw(3)
106          << unit;
107       return os.str();
108     }
109
110     bool negative = false;
111     if (interval < 0.0) {
112       negative = true;
113       interval = -interval;
114     }
115
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;
122
123         return os.str();
124       }
125     }
126   }
127   os << std::setprecision(2) << std::fixed << std::right << std::setw(width - 3)
128      << interval << std::setw(3) << unit;
129
130   return os.str();
131 }