77
|
1 #include <sys/time.h>
|
|
2 #ifdef __FreeBSD__
|
|
3 #include <machine/cpufunc.h>
|
|
4 #include <sys/types.h>
|
|
5 #include <sys/sysctl.h>
|
|
6 #endif
|
|
7 #include "mb_timer.h"
|
|
8
|
|
9
|
|
10 #ifdef __FreeBSD__
|
|
11 void get_now(mb_timeval_t *tmo) {
|
|
12 struct timeval tv;
|
|
13 static uint64_t cpufreq;
|
|
14 static mb_timeval_t tm = {0, 0};
|
|
15 static uint64_t last_ts;
|
|
16 mb_timeval_t diff_tm;
|
|
17 uint64_t ts, diff, udiff, sdiff;
|
|
18 size_t sysctl_sz;
|
78
|
19 int r;
|
77
|
20
|
|
21 if(MB_TIMEVAL_SEC(&tm) == 0) {
|
|
22 sysctl_sz = sizeof(uint64_t);
|
78
|
23 r = sysctlbyname("kern.timecounter.tc.TSC.frequency",
|
|
24 &cpufreq, &sysctl_sz,
|
|
25 NULL, 0);
|
|
26 if(r == -1) {
|
|
27 perror("sysctl");
|
|
28 return;
|
|
29 }
|
77
|
30
|
|
31 gettimeofday(&tv, NULL);
|
|
32 last_ts = rdtsc();
|
|
33
|
|
34 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec);
|
|
35 MB_TIMEVAL_CP(&tm, tmo);
|
|
36 diff = 0;
|
|
37 } else {
|
|
38 ts = rdtsc();
|
|
39 diff += ts - last_ts;
|
|
40 sdiff = diff / cpufreq;
|
|
41 udiff = (diff % cpufreq) * 1000000 / cpufreq;
|
|
42
|
|
43 MB_TIMEVAL_SET(&diff_tm, sdiff, udiff);
|
|
44 MB_TIMEVAL_CP(tmo, &tm);
|
|
45 MB_TIMEVAL_ADD(tmo, &diff_tm);
|
|
46
|
|
47 MB_TIMEVAL_SET(&diff_tm, sdiff, 0);
|
|
48 MB_TIMEVAL_ADD(&tm, &diff_tm);
|
|
49
|
|
50 last_ts += sdiff;
|
|
51 }
|
|
52 }
|
|
53 #else /* __FreeBSD__ */
|
|
54 void get_now(mb_timeval_t *tmo) {
|
|
55 struct timeval tv;
|
|
56
|
|
57 gettimeofday(&tv, NULL);
|
|
58 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec);
|
|
59 }
|
|
60 #endif /* __FreeBSD__ */
|
|
61
|