blob: fe85dc4582b62389215a73a68269806ee95578be [file] [log] [blame]
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001/** @file kmp_stats_timing.cpp
2 * Timing functions
3 */
4
Jim Cownie4cc4bb42014-10-07 16:25:50 +00005//===----------------------------------------------------------------------===//
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
Jim Cownie4cc4bb42014-10-07 16:25:50 +000014#include <stdlib.h>
15#include <unistd.h>
16
Jim Cownie4cc4bb42014-10-07 16:25:50 +000017#include <iomanip>
Jonathan Peyton30419822017-05-12 18:01:32 +000018#include <iostream>
Jim Cownie4cc4bb42014-10-07 16:25:50 +000019#include <sstream>
20
Jonathan Peytonf7413122015-12-17 16:58:26 +000021#include "kmp.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000022#include "kmp_stats_timing.h"
23
24using namespace std;
25
Jonathan Peyton8b524592015-12-17 17:27:51 +000026#if KMP_HAVE_TICK_TIME
Jonathan Peyton30419822017-05-12 18:01:32 +000027#if KMP_MIC
28double tsc_tick_count::tick_time() {
29 // pretty bad assumption of 1GHz clock for MIC
30 return 1 / ((double)1000 * 1.e6);
Jim Cownie4cc4bb42014-10-07 16:25:50 +000031}
Jonathan Peyton30419822017-05-12 18:01:32 +000032#elif KMP_ARCH_X86 || KMP_ARCH_X86_64
33#include <string.h>
Jim Cownie4cc4bb42014-10-07 16:25:50 +000034// Extract the value from the CPUID information
Jonathan Peyton30419822017-05-12 18:01:32 +000035double tsc_tick_count::tick_time() {
36 static double result = 0.0;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000037
Jonathan Peyton30419822017-05-12 18:01:32 +000038 if (result == 0.0) {
39 kmp_cpuid_t cpuinfo;
40 char brand[256];
Jim Cownie4cc4bb42014-10-07 16:25:50 +000041
Jonathan Peyton30419822017-05-12 18:01:32 +000042 __kmp_x86_cpuid(0x80000000, 0, &cpuinfo);
43 memset(brand, 0, sizeof(brand));
44 int ids = cpuinfo.eax;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000045
Jonathan Peyton30419822017-05-12 18:01:32 +000046 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)));
Jim Cownie4cc4bb42014-10-07 16:25:50 +000049
Jonathan Peyton30419822017-05-12 18:01:32 +000050 char *start = &brand[0];
51 for (; *start == ' '; start++)
52 ;
Jonathan Peyton072772b2016-04-05 18:48:48 +000053
Jonathan Peyton30419822017-05-12 18:01:32 +000054 char *end = brand + KMP_STRLEN(brand) - 3;
55 uint64_t multiplier;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000056
Jonathan Peyton30419822017-05-12 18:01:32 +000057 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);
Jim Cownie4cc4bb42014-10-07 16:25:50 +000066 }
Jonathan Peyton30419822017-05-12 18:01:32 +000067 *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;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000081}
Jonathan Peyton30419822017-05-12 18:01:32 +000082#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +000083#endif
84
85static 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).
Jonathan Peyton30419822017-05-12 18:01:32 +000089std::string formatSI(double interval, int width, char unit) {
90 std::stringstream os;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000091
Jonathan Peyton30419822017-05-12 18:01:32 +000092 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.e12, 'f'}, {1.e9, 'p'}, {1.e6, 'n'}, {1.e3, 'u'},
99 {1.0, 'm'}, {1.e-3, ' '}, {1.e-6, 'k'}, {1.e-9, 'M'},
100 {1.e-12, 'G'}, {1.e-15, 'T'}, {1.e-18, 'P'}, {1.e-21, 'E'},
101 {1.e-24, 'Z'}, {1.e-27, 'Y'}};
Jonathan Peyton072772b2016-04-05 18:48:48 +0000102
Jonathan Peyton30419822017-05-12 18:01:32 +0000103 if (interval == 0.0) {
104 os << std::setw(width - 3) << std::right << "0.00" << std::setw(3)
105 << unit;
106 return os.str();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000107 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000108
Jonathan Peyton30419822017-05-12 18:01:32 +0000109 bool negative = false;
110 if (interval < 0.0) {
111 negative = true;
112 interval = -interval;
113 }
114
115 for (int i = 0; i < (int)(sizeof(ranges) / sizeof(ranges[0])); i++) {
116 if (interval * ranges[i].scale < 1.e0) {
117 interval = interval * 1000.e0 * ranges[i].scale;
118 os << std::fixed << std::setprecision(2) << std::setw(width - 3)
119 << std::right << (negative ? -interval : interval) << std::setw(2)
120 << ranges[i].prefix << std::setw(1) << unit;
121
122 return os.str();
123 }
124 }
125 }
126 os << std::setprecision(2) << std::fixed << std::right << std::setw(width - 3)
127 << interval << std::setw(3) << unit;
128
129 return os.str();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000130}