Mercurial > MadButterfly
annotate src/timertool.c @ 191:18f8c3126cdb
Refine installation instructions and document event dispatching by wycc.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Tue, 18 Nov 2008 13:55:09 +0800 |
parents | 530bb7728546 |
children | 586e50f82c1f |
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 | |
186
530bb7728546
Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents:
185
diff
changeset
|
8 #include "mb_timer.h" |
77 | 9 |
10 | |
11 #ifdef __FreeBSD__ | |
12 void get_now(mb_timeval_t *tmo) { | |
13 struct timeval tv; | |
14 static uint64_t cpufreq; | |
120 | 15 static uint64_t diff; |
77 | 16 static mb_timeval_t tm = {0, 0}; |
17 static uint64_t last_ts; | |
18 mb_timeval_t diff_tm; | |
120 | 19 uint64_t ts, udiff, sdiff; |
77 | 20 size_t sysctl_sz; |
78 | 21 int r; |
77 | 22 |
23 if(MB_TIMEVAL_SEC(&tm) == 0) { | |
24 sysctl_sz = sizeof(uint64_t); | |
78 | 25 r = sysctlbyname("kern.timecounter.tc.TSC.frequency", |
26 &cpufreq, &sysctl_sz, | |
27 NULL, 0); | |
28 if(r == -1) { | |
29 perror("sysctl"); | |
30 return; | |
31 } | |
77 | 32 |
33 gettimeofday(&tv, NULL); | |
34 last_ts = rdtsc(); | |
35 | |
36 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec); | |
37 MB_TIMEVAL_CP(&tm, tmo); | |
38 diff = 0; | |
39 } else { | |
40 ts = rdtsc(); | |
120 | 41 diff += ts - last_ts; |
77 | 42 sdiff = diff / cpufreq; |
43 udiff = (diff % cpufreq) * 1000000 / cpufreq; | |
44 | |
45 MB_TIMEVAL_SET(&diff_tm, sdiff, udiff); | |
46 MB_TIMEVAL_CP(tmo, &tm); | |
47 MB_TIMEVAL_ADD(tmo, &diff_tm); | |
48 | |
49 MB_TIMEVAL_SET(&diff_tm, sdiff, 0); | |
50 MB_TIMEVAL_ADD(&tm, &diff_tm); | |
51 | |
120 | 52 diff %= cpufreq; |
53 last_ts = ts; | |
77 | 54 } |
55 } | |
56 #else /* __FreeBSD__ */ | |
57 void get_now(mb_timeval_t *tmo) { | |
58 struct timeval tv; | |
59 | |
60 gettimeofday(&tv, NULL); | |
61 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec); | |
62 } | |
63 #endif /* __FreeBSD__ */ | |
64 |