comparison src/timertool.c @ 77:a6763f080da5

-
author Thinker K.F. Li <thinker@branda.to>
date Wed, 20 Aug 2008 00:32:11 +0800
parents
children 3645e29e4986
comparison
equal deleted inserted replaced
76:8706356a61b4 77:a6763f080da5
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
17 mb_timeval_t diff_tm;
18 uint64_t ts, diff, udiff, sdiff;
19 size_t sysctl_sz;
20
21 if(MB_TIMEVAL_SEC(&tm) == 0) {
22 sysctl_sz = sizeof(uint64_t);
23 cpufreq = sysctlbyname("kern.timecounter.tc.TSC.frequency",
24 &cpufreq, &sysctl_sz,
25 NULL, 0);
26
27 gettimeofday(&tv, NULL);
28 last_ts = rdtsc();
29
30 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec);
31 MB_TIMEVAL_CP(&tm, tmo);
32 diff = 0;
33 } else {
34 ts = rdtsc();
35 diff += ts - last_ts;
36 sdiff = diff / cpufreq;
37 udiff = (diff % cpufreq) * 1000000 / cpufreq;
38
39 MB_TIMEVAL_SET(&diff_tm, sdiff, udiff);
40 MB_TIMEVAL_CP(tmo, &tm);
41 MB_TIMEVAL_ADD(tmo, &diff_tm);
42
43 MB_TIMEVAL_SET(&diff_tm, sdiff, 0);
44 MB_TIMEVAL_ADD(&tm, &diff_tm);
45
46 last_ts += sdiff;
47 }
48 }
49 #else /* __FreeBSD__ */
50 void get_now(mb_timeval_t *tmo) {
51 struct timeval tv;
52
53 gettimeofday(&tv, NULL);
54 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec);
55 }
56 #endif /* __FreeBSD__ */
57