comparison 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
comparison
equal deleted inserted replaced
119:257a1d314bcd 120:5df7403b6fbc
10 10
11 #ifdef __FreeBSD__ 11 #ifdef __FreeBSD__
12 void get_now(mb_timeval_t *tmo) { 12 void get_now(mb_timeval_t *tmo) {
13 struct timeval tv; 13 struct timeval tv;
14 static uint64_t cpufreq; 14 static uint64_t cpufreq;
15 static uint64_t diff;
15 static mb_timeval_t tm = {0, 0}; 16 static mb_timeval_t tm = {0, 0};
16 static uint64_t last_ts; 17 static uint64_t last_ts;
17 mb_timeval_t diff_tm; 18 mb_timeval_t diff_tm;
18 uint64_t ts, diff, udiff, sdiff; 19 uint64_t ts, udiff, sdiff;
19 size_t sysctl_sz; 20 size_t sysctl_sz;
20 int r; 21 int r;
21 22
22 if(MB_TIMEVAL_SEC(&tm) == 0) { 23 if(MB_TIMEVAL_SEC(&tm) == 0) {
23 sysctl_sz = sizeof(uint64_t); 24 sysctl_sz = sizeof(uint64_t);
35 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec); 36 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec);
36 MB_TIMEVAL_CP(&tm, tmo); 37 MB_TIMEVAL_CP(&tm, tmo);
37 diff = 0; 38 diff = 0;
38 } else { 39 } else {
39 ts = rdtsc(); 40 ts = rdtsc();
40 diff = ts - last_ts; 41 diff += ts - last_ts;
41 sdiff = diff / cpufreq; 42 sdiff = diff / cpufreq;
42 udiff = (diff % cpufreq) * 1000000 / cpufreq; 43 udiff = (diff % cpufreq) * 1000000 / cpufreq;
43 44
44 MB_TIMEVAL_SET(&diff_tm, sdiff, udiff); 45 MB_TIMEVAL_SET(&diff_tm, sdiff, udiff);
45 MB_TIMEVAL_CP(tmo, &tm); 46 MB_TIMEVAL_CP(tmo, &tm);
46 MB_TIMEVAL_ADD(tmo, &diff_tm); 47 MB_TIMEVAL_ADD(tmo, &diff_tm);
47 48
48 MB_TIMEVAL_SET(&diff_tm, sdiff, 0); 49 MB_TIMEVAL_SET(&diff_tm, sdiff, 0);
49 MB_TIMEVAL_ADD(&tm, &diff_tm); 50 MB_TIMEVAL_ADD(&tm, &diff_tm);
50 51
51 last_ts += sdiff; 52 diff %= cpufreq;
53 last_ts = ts;
52 } 54 }
53 } 55 }
54 #else /* __FreeBSD__ */ 56 #else /* __FreeBSD__ */
55 void get_now(mb_timeval_t *tmo) { 57 void get_now(mb_timeval_t *tmo) {
56 struct timeval tv; 58 struct timeval tv;