Mercurial > MadButterfly
annotate src/timertool.c @ 893:cad38ddb1253
Backed out changeset 23dffb564ace
author | wycc |
---|---|
date | Mon, 27 Sep 2010 23:00:51 +0800 |
parents | 586e50f82c1f |
children |
rev | line source |
---|---|
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
186
diff
changeset
|
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*- |
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
186
diff
changeset
|
2 // vim: sw=4:ts=8:sts=4 |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
3 #include <stdio.h> |
77 | 4 #include <sys/time.h> |
5 #ifdef __FreeBSD__ | |
6 #include <machine/cpufunc.h> | |
7 #include <sys/types.h> | |
8 #include <sys/sysctl.h> | |
9 #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
|
10 #include "mb_timer.h" |
77 | 11 |
12 | |
13 #ifdef __FreeBSD__ | |
14 void get_now(mb_timeval_t *tmo) { | |
15 struct timeval tv; | |
16 static uint64_t cpufreq; | |
120 | 17 static uint64_t diff; |
77 | 18 static mb_timeval_t tm = {0, 0}; |
19 static uint64_t last_ts; | |
20 mb_timeval_t diff_tm; | |
120 | 21 uint64_t ts, udiff, sdiff; |
77 | 22 size_t sysctl_sz; |
78 | 23 int r; |
77 | 24 |
25 if(MB_TIMEVAL_SEC(&tm) == 0) { | |
26 sysctl_sz = sizeof(uint64_t); | |
78 | 27 r = sysctlbyname("kern.timecounter.tc.TSC.frequency", |
28 &cpufreq, &sysctl_sz, | |
29 NULL, 0); | |
30 if(r == -1) { | |
31 perror("sysctl"); | |
32 return; | |
33 } | |
77 | 34 |
35 gettimeofday(&tv, NULL); | |
36 last_ts = rdtsc(); | |
37 | |
38 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec); | |
39 MB_TIMEVAL_CP(&tm, tmo); | |
40 diff = 0; | |
41 } else { | |
42 ts = rdtsc(); | |
120 | 43 diff += ts - last_ts; |
77 | 44 sdiff = diff / cpufreq; |
45 udiff = (diff % cpufreq) * 1000000 / cpufreq; | |
46 | |
47 MB_TIMEVAL_SET(&diff_tm, sdiff, udiff); | |
48 MB_TIMEVAL_CP(tmo, &tm); | |
49 MB_TIMEVAL_ADD(tmo, &diff_tm); | |
50 | |
51 MB_TIMEVAL_SET(&diff_tm, sdiff, 0); | |
52 MB_TIMEVAL_ADD(&tm, &diff_tm); | |
53 | |
120 | 54 diff %= cpufreq; |
55 last_ts = ts; | |
77 | 56 } |
57 } | |
58 #else /* __FreeBSD__ */ | |
59 void get_now(mb_timeval_t *tmo) { | |
60 struct timeval tv; | |
61 | |
62 gettimeofday(&tv, NULL); | |
63 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec); | |
64 } | |
65 #endif /* __FreeBSD__ */ | |
66 |