-
Notifications
You must be signed in to change notification settings - Fork 2
/
chrono.cpp
47 lines (37 loc) · 816 Bytes
/
chrono.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
/*
* author : Shuichi TAKANO
* since : Thu Aug 29 2019 1:19:5
*/
#include "chrono.h"
#include <encoding.h> // read_cycle()
#include <sysctl.h>
#include <bsp.h>
namespace
{
constexpr int CORE_COUNT = 2;
uint64_t cpuClock_ = 0;
clock_t prevClock_[CORE_COUNT];
} // namespace
void initChrono()
{
cpuClock_ = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU);
prevClock_[0] = prevClock_[1] = read_cycle();
printf("cpu clock: %d\n", (int)cpuClock_);
}
clock_t getClockCounter()
{
return read_cycle();
}
uint32_t clockToMicroSec(clock_t v)
{
return v * 1000000 / cpuClock_;
}
uint32_t getTickTimeInMicroSec()
{
uint64_t core = current_coreid();
auto prev = prevClock_[core];
auto clk = read_cycle();
auto r = clockToMicroSec(clk - prev);
prevClock_[core] = clk;
return r;
}