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