forked from jemalloc/jemalloc-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
36 lines (29 loc) · 801 Bytes
/
util.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
#include <cstdint>
namespace util {
inline void compilerBarrier() {
asm volatile("" : : : "memory");
}
inline std::uint64_t rdtsc() noexcept {
std::uint32_t lo, hi;
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
return ((uint64_t)lo) | ((uint64_t)hi << 32);
}
// begin and end must be a multiple of 64.
inline void flushCache(void* beginv, std::size_t size) {
char* begin = static_cast<char*>(beginv);
char* end = begin + size;
for (char* ptr = begin; ptr != end; ptr += 64) {
__builtin_ia32_clflush(static_cast<void*>(ptr));
}
}
// Returns time to execute func, in cycles.
template <typename Func>
std::uint64_t runTimed(Func func) {
std::uint64_t begin = rdtsc();
compilerBarrier();
func();
compilerBarrier();
std::uint64_t end = rdtsc();
return end - begin;
}
}