14cc4bb4cSJim Cownie /** @file kmp_stats_timing.cpp
24cc4bb4cSJim Cownie * Timing functions
34cc4bb4cSJim Cownie */
44cc4bb4cSJim Cownie
54cc4bb4cSJim Cownie //===----------------------------------------------------------------------===//
64cc4bb4cSJim Cownie //
7*57b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8*57b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
9*57b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
104cc4bb4cSJim Cownie //
114cc4bb4cSJim Cownie //===----------------------------------------------------------------------===//
124cc4bb4cSJim Cownie
134cc4bb4cSJim Cownie #include <stdlib.h>
144cc4bb4cSJim Cownie #include <unistd.h>
154cc4bb4cSJim Cownie
164cc4bb4cSJim Cownie #include <iomanip>
173041982dSJonathan Peyton #include <iostream>
184cc4bb4cSJim Cownie #include <sstream>
194cc4bb4cSJim Cownie
20f741312cSJonathan Peyton #include "kmp.h"
214cc4bb4cSJim Cownie #include "kmp_stats_timing.h"
224cc4bb4cSJim Cownie
234cc4bb4cSJim Cownie using namespace std;
244cc4bb4cSJim Cownie
258b524597SJonathan Peyton #if KMP_HAVE_TICK_TIME
264cc4bb4cSJim Cownie #if KMP_MIC
tick_time()273041982dSJonathan Peyton double tsc_tick_count::tick_time() {
284cc4bb4cSJim Cownie // pretty bad assumption of 1GHz clock for MIC
294cc4bb4cSJim Cownie return 1 / ((double)1000 * 1.e6);
304cc4bb4cSJim Cownie }
318b524597SJonathan Peyton #elif KMP_ARCH_X86 || KMP_ARCH_X86_64
324cc4bb4cSJim Cownie #include <string.h>
334cc4bb4cSJim Cownie // Extract the value from the CPUID information
tick_time()343041982dSJonathan Peyton double tsc_tick_count::tick_time() {
354cc4bb4cSJim Cownie static double result = 0.0;
364cc4bb4cSJim Cownie
373041982dSJonathan Peyton if (result == 0.0) {
38f741312cSJonathan Peyton kmp_cpuid_t cpuinfo;
394cc4bb4cSJim Cownie char brand[256];
404cc4bb4cSJim Cownie
41f741312cSJonathan Peyton __kmp_x86_cpuid(0x80000000, 0, &cpuinfo);
424cc4bb4cSJim Cownie memset(brand, 0, sizeof(brand));
43f741312cSJonathan Peyton int ids = cpuinfo.eax;
444cc4bb4cSJim Cownie
454cc4bb4cSJim Cownie for (unsigned int i = 2; i < (ids ^ 0x80000000) + 2; i++)
463041982dSJonathan Peyton __kmp_x86_cpuid(i | 0x80000000, 0,
473041982dSJonathan Peyton (kmp_cpuid_t *)(brand + (i - 2) * sizeof(kmp_cpuid_t)));
484cc4bb4cSJim Cownie
494cc4bb4cSJim Cownie char *start = &brand[0];
504cc4bb4cSJim Cownie for (; *start == ' '; start++)
514cc4bb4cSJim Cownie ;
524cc4bb4cSJim Cownie
5374bf17b8SAndrey Churbanov char *end = brand + KMP_STRLEN(brand) - 3;
544cc4bb4cSJim Cownie uint64_t multiplier;
554cc4bb4cSJim Cownie
563041982dSJonathan Peyton if (*end == 'M')
573041982dSJonathan Peyton multiplier = 1000LL * 1000LL;
583041982dSJonathan Peyton else if (*end == 'G')
593041982dSJonathan Peyton multiplier = 1000LL * 1000LL * 1000LL;
603041982dSJonathan Peyton else if (*end == 'T')
613041982dSJonathan Peyton multiplier = 1000LL * 1000LL * 1000LL * 1000LL;
623041982dSJonathan Peyton else {
634cc4bb4cSJim Cownie cout << "Error determining multiplier '" << *end << "'\n";
644cc4bb4cSJim Cownie exit(-1);
654cc4bb4cSJim Cownie }
664cc4bb4cSJim Cownie *end = 0;
673041982dSJonathan Peyton while (*end != ' ')
683041982dSJonathan Peyton end--;
694cc4bb4cSJim Cownie end++;
704cc4bb4cSJim Cownie
714cc4bb4cSJim Cownie double freq = strtod(end, &start);
723041982dSJonathan Peyton if (freq == 0.0) {
734cc4bb4cSJim Cownie cout << "Error calculating frequency " << end << "\n";
744cc4bb4cSJim Cownie exit(-1);
754cc4bb4cSJim Cownie }
764cc4bb4cSJim Cownie
774cc4bb4cSJim Cownie result = ((double)1.0) / (freq * multiplier);
784cc4bb4cSJim Cownie }
794cc4bb4cSJim Cownie return result;
804cc4bb4cSJim Cownie }
814cc4bb4cSJim Cownie #endif
824cc4bb4cSJim Cownie #endif
834cc4bb4cSJim Cownie
844cc4bb4cSJim Cownie static bool useSI = true;
854cc4bb4cSJim Cownie
864cc4bb4cSJim Cownie // Return a formatted string after normalising the value into
874cc4bb4cSJim Cownie // engineering style and using a suitable unit prefix (e.g. ms, us, ns).
formatSI(double interval,int width,char unit)883041982dSJonathan Peyton std::string formatSI(double interval, int width, char unit) {
894cc4bb4cSJim Cownie std::stringstream os;
904cc4bb4cSJim Cownie
913041982dSJonathan Peyton if (useSI) {
923041982dSJonathan Peyton // Preserve accuracy for small numbers, since we only multiply and the
933041982dSJonathan Peyton // positive powers of ten are precisely representable.
943041982dSJonathan Peyton static struct {
953041982dSJonathan Peyton double scale;
963041982dSJonathan Peyton char prefix;
97f0682ac4SJonathan Peyton } ranges[] = {{1.e21, 'y'}, {1.e18, 'z'}, {1.e15, 'a'}, {1.e12, 'f'},
98f0682ac4SJonathan Peyton {1.e9, 'p'}, {1.e6, 'n'}, {1.e3, 'u'}, {1.0, 'm'},
99f0682ac4SJonathan Peyton {1.e-3, ' '}, {1.e-6, 'k'}, {1.e-9, 'M'}, {1.e-12, 'G'},
100f0682ac4SJonathan Peyton {1.e-15, 'T'}, {1.e-18, 'P'}, {1.e-21, 'E'}, {1.e-24, 'Z'},
101f0682ac4SJonathan Peyton {1.e-27, 'Y'}};
1024cc4bb4cSJim Cownie
1033041982dSJonathan Peyton if (interval == 0.0) {
1043041982dSJonathan Peyton os << std::setw(width - 3) << std::right << "0.00" << std::setw(3)
1053041982dSJonathan Peyton << unit;
1064cc4bb4cSJim Cownie return os.str();
1074cc4bb4cSJim Cownie }
1084cc4bb4cSJim Cownie
1094cc4bb4cSJim Cownie bool negative = false;
1103041982dSJonathan Peyton if (interval < 0.0) {
1114cc4bb4cSJim Cownie negative = true;
1124cc4bb4cSJim Cownie interval = -interval;
1134cc4bb4cSJim Cownie }
1144cc4bb4cSJim Cownie
1153041982dSJonathan Peyton for (int i = 0; i < (int)(sizeof(ranges) / sizeof(ranges[0])); i++) {
1163041982dSJonathan Peyton if (interval * ranges[i].scale < 1.e0) {
1174cc4bb4cSJim Cownie interval = interval * 1000.e0 * ranges[i].scale;
1183041982dSJonathan Peyton os << std::fixed << std::setprecision(2) << std::setw(width - 3)
1193041982dSJonathan Peyton << std::right << (negative ? -interval : interval) << std::setw(2)
1203041982dSJonathan Peyton << ranges[i].prefix << std::setw(1) << unit;
1214cc4bb4cSJim Cownie
1224cc4bb4cSJim Cownie return os.str();
1234cc4bb4cSJim Cownie }
1244cc4bb4cSJim Cownie }
1254cc4bb4cSJim Cownie }
1263041982dSJonathan Peyton os << std::setprecision(2) << std::fixed << std::right << std::setw(width - 3)
1273041982dSJonathan Peyton << interval << std::setw(3) << unit;
1284cc4bb4cSJim Cownie
1294cc4bb4cSJim Cownie return os.str();
1304cc4bb4cSJim Cownie }
131