-
Notifications
You must be signed in to change notification settings - Fork 2
/
cpu_frequency.cpp
61 lines (51 loc) · 1.72 KB
/
cpu_frequency.cpp
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
#include "public/perfmon/cpu_frequency.h"
#include "public/perfmon.h"
#include "public/perfmon/ticks.h"
#include <algorithm>
#include <chrono>
#include <thread>
#include <vector>
namespace perfmon {
namespace internal {
namespace {
/** Simple estimation of the cpu frequency. */
template <class ClockType, class Rep, class Period>
double GetCpuFrequencySampleImpl(
const std::chrono::duration<Rep, Period> &sleep_duration) {
const auto startTick = ReadTickCounter();
const auto startClock = ClockType::now();
std::this_thread::sleep_for(sleep_duration);
const auto stopTick = ReadTickCounter();
const auto stopClock = ClockType::now();
const auto secondsElapsed =
std::chrono::duration_cast<std::chrono::duration<double>>(stopClock -
startClock)
.count();
return TicksElapsed(startTick, stopTick) / secondsElapsed;
}
template <class Rep, class Period>
double GetCpuFrequencySample(
const std::chrono::duration<Rep, Period> &sleep_duration) {
return GetCpuFrequencySampleImpl<std::chrono::high_resolution_clock>(
sleep_duration);
}
/** Estimation based on median. */
template <class Rep, class Period>
double EstimateCpuFrequency(
const std::chrono::duration<Rep, Period> &sleep_duration,
size_t sample_size) {
std::vector<double> sample(sample_size);
for (auto &value : sample) {
value = GetCpuFrequencySample(sleep_duration);
}
std::sort(sample.begin(), sample.end());
return sample.at(sample.size() / 2);
}
} // namespace
double EstimateCpuFrequency() {
static const auto result =
EstimateCpuFrequency(std::chrono::milliseconds(5), 11);
return result;
}
} // namespace internal
} // namespace perfmon