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