annotate src/timertool.c @ 903:4c7f0ec97ad9

Copy src/dfb_supp.c from src/X_supp.c
author Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
date Sun, 03 Oct 2010 16:54:26 +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
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4 #include <sys/time.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #ifdef __FreeBSD__
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6 #include <machine/cpufunc.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 #include <sys/types.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 #include <sys/sysctl.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
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
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 #ifdef __FreeBSD__
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 void get_now(mb_timeval_t *tmo) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 struct timeval tv;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 static uint64_t cpufreq;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 107
diff changeset
17 static uint64_t diff;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 static mb_timeval_t tm = {0, 0};
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 static uint64_t last_ts;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 mb_timeval_t diff_tm;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 107
diff changeset
21 uint64_t ts, udiff, sdiff;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 size_t sysctl_sz;
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
23 int r;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 if(MB_TIMEVAL_SEC(&tm) == 0) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 sysctl_sz = sizeof(uint64_t);
78
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
27 r = sysctlbyname("kern.timecounter.tc.TSC.frequency",
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
28 &cpufreq, &sysctl_sz,
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
29 NULL, 0);
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
30 if(r == -1) {
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
31 perror("sysctl");
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
32 return;
3645e29e4986 Add runtime for Xlib.
Thinker K.F. Li <thinker@branda.to>
parents: 77
diff changeset
33 }
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 gettimeofday(&tv, NULL);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 last_ts = rdtsc();
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 MB_TIMEVAL_CP(&tm, tmo);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
40 diff = 0;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 } else {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 ts = rdtsc();
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 107
diff changeset
43 diff += ts - last_ts;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 sdiff = diff / cpufreq;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 udiff = (diff % cpufreq) * 1000000 / cpufreq;
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, udiff);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 MB_TIMEVAL_CP(tmo, &tm);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 MB_TIMEVAL_ADD(tmo, &diff_tm);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 MB_TIMEVAL_SET(&diff_tm, sdiff, 0);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 MB_TIMEVAL_ADD(&tm, &diff_tm);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 107
diff changeset
54 diff %= cpufreq;
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 107
diff changeset
55 last_ts = ts;
77
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 #else /* __FreeBSD__ */
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 void get_now(mb_timeval_t *tmo) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 struct timeval tv;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
62 gettimeofday(&tv, NULL);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
63 MB_TIMEVAL_SET(tmo, tv.tv_sec, tv.tv_usec);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 #endif /* __FreeBSD__ */
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66