-
Notifications
You must be signed in to change notification settings - Fork 2
/
performance_counter.h
83 lines (68 loc) · 2.08 KB
/
performance_counter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
#define GYOPERFORMANCE_COUNTER
/*
In this file:
Mostly performance counter related functions.
- get_os_timer_freq() to know the frequency of OS timers
- read_os_timer() to get the current os timer clock cycle
- estimate_cpu_frequency() to estimate the cpu rdtsc timer frequency.
- read_cpu_timer() to get the current cpu timer clock cycle (rdtsc is faster than os timers)
*/
#if _WIN32
#ifndef DISABLE_INCLUDES
#include <windows.h>
#ifdef __GNUC__
#include <x86intrin.h>
#endif
#endif
static u64 get_os_timer_freq() {
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
return freq.QuadPart;
}
static u64 read_os_timer() {
LARGE_INTEGER value;
QueryPerformanceCounter(&value);
return value.QuadPart;
}
inline u64 read_cpu_timer() {
return __rdtsc();
}
#else
#ifndef DISABLE_INCLUDES
#include <x86intrin.h>
#include <sys/time.h>
#endif
static u64 get_os_timer_freq() {
return 1000000;
}
static u64 read_os_timer() {
timeval value;
gettimeofday(&value, 0);
u64 result = get_os_timer_freq()*(u64)value.tv_sec + (u64)value.tv_usec;
return result;
}
inline u64 read_cpu_timer() {
return 0; //TODO(cogno): rdtsc on arm? I don't know which one to use
}
#endif
u64 estimate_cpu_frequency(int ms_to_wait = 1) {
// calculate how many os clocks to wait
u64 os_timer_freq = get_os_timer_freq();
u64 os_timer_to_wait = os_timer_freq * ms_to_wait / 1000;
// wait for those clocks, saving cpu timer start and end
u64 os_clocks_elapsed = 0;
u64 os_counter_end = 0;
u64 os_counter_start = read_os_timer();
u64 cpu_counter_start = read_cpu_timer();
while(os_clocks_elapsed < os_timer_to_wait) {
os_counter_end = read_os_timer();
os_clocks_elapsed = os_counter_end - os_counter_start;
}
u64 cpu_counter_end = read_cpu_timer();
// estimate cpu freq from os timer, formula comes from this:
// cpu timer / cpu freq = os timer / os freq
// cpu freq = cpu timer * os freq / os timer
u64 freq_estimate = (cpu_counter_end - cpu_counter_start) * os_timer_freq / os_clocks_elapsed;
return freq_estimate;
}