blob: 52d70fbf6e38d2091cff2090d9b4f3b88ac03952 [file] [log] [blame]
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001/** @file kmp_stats_timing.cpp
2 * Timing functions
3 */
4
5
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
Jim Cownie4cc4bb42014-10-07 16:25:50 +000019#include <iomanip>
Jonathan Peyton30419822017-05-12 18:01:32 +000020#include <iostream>
Jim Cownie4cc4bb42014-10-07 16:25:50 +000021#include <sstream>
22
Jonathan Peytonf7413122015-12-17 16:58:26 +000023#include "kmp.h"
Jim Cownie4cc4bb42014-10-07 16:25:50 +000024#include "kmp_stats_timing.h"
25
26using namespace std;
27
Jonathan Peyton8b524592015-12-17 17:27:51 +000028#if KMP_HAVE_TICK_TIME
Jonathan Peyton30419822017-05-12 18:01:32 +000029#if KMP_MIC
30double tsc_tick_count::tick_time() {
31 // pretty bad assumption of 1GHz clock for MIC
32 return 1 / ((double)1000 * 1.e6);
Jim Cownie4cc4bb42014-10-07 16:25:50 +000033}
Jonathan Peyton30419822017-05-12 18:01:32 +000034#elif KMP_ARCH_X86 || KMP_ARCH_X86_64
35#include <string.h>
Jim Cownie4cc4bb42014-10-07 16:25:50 +000036// Extract the value from the CPUID information
Jonathan Peyton30419822017-05-12 18:01:32 +000037double tsc_tick_count::tick_time() {
38 static double result = 0.0;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000039
Jonathan Peyton30419822017-05-12 18:01:32 +000040 if (result == 0.0) {
41 kmp_cpuid_t cpuinfo;
42 char brand[256];
Jim Cownie4cc4bb42014-10-07 16:25:50 +000043
Jonathan Peyton30419822017-05-12 18:01:32 +000044 __kmp_x86_cpuid(0x80000000, 0, &cpuinfo);
45 memset(brand, 0, sizeof(brand));
46 int ids = cpuinfo.eax;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000047
Jonathan Peyton30419822017-05-12 18:01:32 +000048 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)));
Jim Cownie4cc4bb42014-10-07 16:25:50 +000051
Jonathan Peyton30419822017-05-12 18:01:32 +000052 char *start = &brand[0];
53 for (; *start == ' '; start++)
54 ;
Jonathan Peyton072772b2016-04-05 18:48:48 +000055
Jonathan Peyton30419822017-05-12 18:01:32 +000056 char *end = brand + KMP_STRLEN(brand) - 3;
57 uint64_t multiplier;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000058
Jonathan Peyton30419822017-05-12 18:01:32 +000059 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);
Jim Cownie4cc4bb42014-10-07 16:25:50 +000068 }
Jonathan Peyton30419822017-05-12 18:01:32 +000069 *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;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000083}
Jonathan Peyton30419822017-05-12 18:01:32 +000084#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +000085#endif
86
87static 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).
Jonathan Peyton30419822017-05-12 18:01:32 +000091std::string formatSI(double interval, int width, char unit) {
92 std::stringstream os;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000093
Jonathan Peyton30419822017-05-12 18:01:32 +000094 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'}};
Jonathan Peyton072772b2016-04-05 18:48:48 +0000104
Jonathan Peyton30419822017-05-12 18:01:32 +0000105 if (interval == 0.0) {
106 os << std::setw(width - 3) << std::right << "0.00" << std::setw(3)
107 << unit;
108 return os.str();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000109 }
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000110
Jonathan Peyton30419822017-05-12 18:01:32 +0000111 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();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000132}