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