Debian Codegraph @

visa/iga/IGALibrary/Timer/Timer.cpp

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2017-2021 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 #ifdef _WIN32
10 #ifndef NOMINMAX
11 #define NOMINMAX // omit min()/max() macros (favor std::min/std::max)
12 #endif
13 #include <Windows.h>
14 #endif
15
16 #if !defined(_WIN32)
17 #include <time.h> // clock_gettime()
18 #endif
19
20 #include <cstdint>
21 #include <fstream>
22 #include <iostream>
23 #include <string>
24
25 #include "Timer.hpp"
26 #include "../strings.hpp"
27
28 // actually do real timing
29 #ifdef _MSC_VER
30 // MSVC way of declaring a thread-local variable
31 #define _THREAD __declspec(thread)
32 #elif ANDROID
33 // drop it on android
34 #define _THREAD
35 #define CLOCK_TYPE CLOCK_MONOTONIC
36 #else
37 // gcc, clang, mingw, and cygwin all use this
38 #define _THREAD __thread
39 #define CLOCK_TYPE CLOCK_MONOTONIC
40 #endif
41
42 static const char *timerNames[TIMER_NUM_TIMERS]{"Total", "GED"};
43
44 #ifdef _WIN32
45 [[maybe_unused]] static int64_t CurrTicks() {
46 LARGE_INTEGER li;
47 QueryPerformanceCounter(&li);
48 return li.QuadPart;
49 }
50 [[maybe_unused]] static int64_t CurrFreq() {
51 LARGE_INTEGER li;
52 QueryPerformanceFrequency(&li);
53 return li.QuadPart;
54 }
55 #else
56 // Linux
57 [[maybe_unused]] static int64_t CurrTicks() {
58 struct timespec t;
59 if (clock_gettime(CLOCK_TYPE, &t) != 0) {
60 return 0;
61 }
62 return 1000000000LL * t.tv_sec + t.tv_nsec;
63 }
64 [[maybe_unused]] static int64_t CurrFreq() {
65 // clock_gettime is in nanos
66 return 1000;
67 }
68 #endif
69
70 namespace iga {
71
72 struct Timer {
73 double time;
74 int64_t currentStart;
75 const char *name;
76 int64_t ticks;
77 bool started;
78 };
79
80 } // namespace iga
81
82 static _THREAD iga::Timer timers[TIMER_NUM_TIMERS];
83 static _THREAD char kernelAsmName[256] = "";
84 static _THREAD int64_t proc_freq;
85 static _THREAD int numTimers = TIMER_NUM_TIMERS;
86
87 void initIGATimer() {
88
89 numTimers = 0;
90 for (int i = 0; i < TIMER_NUM_TIMERS; i++) {
91 timers[i].time = 0;
92 timers[i].currentStart = 0;
93 timers[i].name = NULL;
94 timers[i].ticks = 0;
95 timers[i].started = false;
96 createIGANewTimer(timerNames[i]);
97 }
98 proc_freq = CurrFreq();
99 }
100
101 void setIGAKernelName(const char *name) {
102 iga::copyOutString(kernelAsmName, sizeof(kernelAsmName), nullptr, name);
103 }
104
105 int createIGANewTimer(const char *name) {
106 timers[numTimers].name = name;
107 return numTimers++;
108 }
109
110 void startIGATimer(int timer) {
111 #ifdef MEASURE_COMPILATION_TIME
112 if (timer < TIMER_NUM_TIMERS) {
113 #if defined(_DEBUG) && defined(CHECK_TIMER)
114 if (timers[timer].started) {
115 std::cerr << "***********************************************\n";
116 std::cerr << "Timer already started.\n";
117 assert(false);
118 }
119 #endif
120 timers[timer].currentStart = CurrTicks();
121 #if defined(_DEBUG) && defined(CHECK_TIMER)
122 timers[timer].started = true;
123 #endif
124 } else {
125 #ifdef _DEBUG
126 std::cerr << "***********************************************\n";
127 std::cerr << "Invalid index used when invoking startTimer\n";
128 #endif
129 }
130 #endif
131 }
132
133 void stopIGATimer(int timer) {
134 #ifdef MEASURE_COMPILATION_TIME
135 if (timer < TIMER_NUM_TIMERS) {
136 int64_t stop = CurrTicks();
137 timers[timer].time +=
138 (double)((stop - timers[timer].currentStart) / (double)proc_freq);
139 timers[timer].ticks += (stop - timers[timer].currentStart);
140 timers[timer].currentStart = 0;
141 #if defined(_DEBUG) && defined(CHECK_TIMER)
142 timers[timer].started = false;
143 #endif
144 } else {
145 #ifdef _DEBUG
146 std::cerr << "***********************************************\n";
147 std::cerr << "Invalid index used when invoking stopTimer\n";
148 #endif
149 }
150 #endif
151 }
152
153 static unsigned int getIGATotalTimers() { return numTimers; }
154
155 std::string getIGATimerNames(unsigned int idx) { return timerNames[idx]; }
156
157 double getIGATimerCounts(unsigned int idx) { return timers[idx].time; }
158
159 int64_t getIGATimerTicks(unsigned int idx) { return timers[idx].ticks; }
160
161 double getIGATimerUS(unsigned int idx) {
162 return (timers[idx].ticks * 1000000) / (double)proc_freq;
163 }
164
165 void dumpAllIGATimers(bool outputTime) {
166 std::ofstream krnlOutput;
167 krnlOutput.open("jit_time.txt", std::ios_base::app);
168
169 krnlOutput << kernelAsmName << "\n";
170
171 for (unsigned int i = 0; i < getIGATotalTimers(); i++) {
172 krnlOutput << getIGATimerNames(i).c_str() << "\t";
173 }
174 krnlOutput << "\n";
175
176 for (unsigned int i = 0; i < getIGATotalTimers(); i++) {
177 if (outputTime) {
178 krnlOutput << timers[i].time << "\t";
179 } else {
180 krnlOutput << timers[i].ticks << "\t";
181 }
182 }
183
184 krnlOutput << "\n";
185
186 krnlOutput.close();
187 }