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