annotate src/timertool.c @ 120:5df7403b6fbc

Fix bug of get_now()
author Thinker K.F. Li <thinker@branda.to>
date Sun, 14 Sep 2008 23:05:30 +0800
parents 069868161f63
children c7e5b8779bb5
rev   line source
92
3f619ae03678 Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents: 78
diff changeset
1 #include <stdio.h>
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include <sys/time.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 #ifdef __FreeBSD__
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 #include <machine/cpufunc.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include <sys/types.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 #include <sys/sysctl.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 #endif
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 #include "mb_timer.h"
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 #ifdef __FreeBSD__
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 void get_now(mb_timeval_t *tmo) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 struct timeval tv;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 static uint64_t cpufreq;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 107
diff changeset
15 static uint64_t diff;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 static mb_timeval_t tm = {0, 0};
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 static uint64_t last_ts;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 mb_timeval_t diff_tm;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 107
diff changeset
19 uint64_t ts, udiff, sdiff;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 size_t sysctl_sz;
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
21 int r;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 if(MB_TIMEVAL_SEC(&tm) == 0) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 sysctl_sz = sizeof(uint64_t);
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
25 r = sysctlbyname("kern.timecounter.tc.TSC.frequency",
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
26 &cpufreq, &sysctl_sz,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
27 NULL, 0);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
28 if(r == -1) {
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
29 perror("sysctl");
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
30 return;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
31 }
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 gettimeofday(&tv, NULL);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 last_ts = rdtsc();
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 MB_TIMEVAL_CP(&tm, tmo);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 diff = 0;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 } else {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 ts = rdtsc();
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 107
diff changeset
41 diff += ts - last_ts;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 sdiff = diff / cpufreq;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43 udiff = (diff % cpufreq) * 1000000 / cpufreq;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 MB_TIMEVAL_SET(&diff_tm, sdiff, udiff);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 MB_TIMEVAL_CP(tmo, &tm);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 MB_TIMEVAL_ADD(tmo, &diff_tm);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 MB_TIMEVAL_SET(&diff_tm, sdiff, 0);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 MB_TIMEVAL_ADD(&tm, &diff_tm);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 107
diff changeset
52 diff %= cpufreq;
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 107
diff changeset
53 last_ts = ts;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 #else /* __FreeBSD__ */
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 void get_now(mb_timeval_t *tmo) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 struct timeval tv;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 gettimeofday(&tv, NULL);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 #endif /* __FreeBSD__ */
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64