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
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;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 static mb_timeval_t tm = {0, 0};
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 static uint64_t last_ts;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 mb_timeval_t diff_tm;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 uint64_t ts, diff, udiff, sdiff;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 size_t sysctl_sz;
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
20 int r;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 if(MB_TIMEVAL_SEC(&tm) == 0) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 sysctl_sz = sizeof(uint64_t);
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
24 r = sysctlbyname("kern.timecounter.tc.TSC.frequency",
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
25 &cpufreq, &sysctl_sz,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
26 NULL, 0);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
27 if(r == -1) {
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
28 perror("sysctl");
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
29 return;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
30 }
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 gettimeofday(&tv, NULL);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 last_ts = rdtsc();
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 MB_TIMEVAL_CP(&tm, tmo);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 diff = 0;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 } else {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 ts = rdtsc();
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 diff += ts - last_ts;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 sdiff = diff / cpufreq;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 udiff = (diff % cpufreq) * 1000000 / cpufreq;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 MB_TIMEVAL_SET(&diff_tm, sdiff, udiff);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 MB_TIMEVAL_CP(tmo, &tm);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 MB_TIMEVAL_ADD(tmo, &diff_tm);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 MB_TIMEVAL_SET(&diff_tm, sdiff, 0);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 MB_TIMEVAL_ADD(&tm, &diff_tm);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 last_ts += sdiff;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 #else /* __FreeBSD__ */
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 void get_now(mb_timeval_t *tmo) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 struct timeval tv;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 gettimeofday(&tv, NULL);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 #endif /* __FreeBSD__ */
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62