Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 1 | /** @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 Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 19 | #include <iomanip> |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 20 | #include <iostream> |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 21 | #include <sstream> |
| 22 | |
Jonathan Peyton | f741312 | 2015-12-17 16:58:26 +0000 | [diff] [blame] | 23 | #include "kmp.h" |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 24 | #include "kmp_stats_timing.h" |
| 25 | |
| 26 | using namespace std; |
| 27 | |
Jonathan Peyton | 8b52459 | 2015-12-17 17:27:51 +0000 | [diff] [blame] | 28 | #if KMP_HAVE_TICK_TIME |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 29 | #if KMP_MIC |
| 30 | double tsc_tick_count::tick_time() { |
| 31 | // pretty bad assumption of 1GHz clock for MIC |
| 32 | return 1 / ((double)1000 * 1.e6); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 33 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 34 | #elif KMP_ARCH_X86 || KMP_ARCH_X86_64 |
| 35 | #include <string.h> |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 36 | // Extract the value from the CPUID information |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 37 | double tsc_tick_count::tick_time() { |
| 38 | static double result = 0.0; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 39 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 40 | if (result == 0.0) { |
| 41 | kmp_cpuid_t cpuinfo; |
| 42 | char brand[256]; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 43 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 44 | __kmp_x86_cpuid(0x80000000, 0, &cpuinfo); |
| 45 | memset(brand, 0, sizeof(brand)); |
| 46 | int ids = cpuinfo.eax; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 47 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 48 | 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 Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 51 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 52 | char *start = &brand[0]; |
| 53 | for (; *start == ' '; start++) |
| 54 | ; |
Jonathan Peyton | 072772b | 2016-04-05 18:48:48 +0000 | [diff] [blame] | 55 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 56 | char *end = brand + KMP_STRLEN(brand) - 3; |
| 57 | uint64_t multiplier; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 58 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 59 | 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 Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 68 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 69 | *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 Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 83 | } |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 84 | #endif |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
| 87 | static 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 Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 91 | std::string formatSI(double interval, int width, char unit) { |
| 92 | std::stringstream os; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 93 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 94 | 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 Peyton | 072772b | 2016-04-05 18:48:48 +0000 | [diff] [blame] | 104 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 105 | if (interval == 0.0) { |
| 106 | os << std::setw(width - 3) << std::right << "0.00" << std::setw(3) |
| 107 | << unit; |
| 108 | return os.str(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 109 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 110 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 111 | 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 Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 132 | } |