annotate src/timertool.c @ 78:3645e29e4986

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