Improved statev stuff:
[libfirm] / ir / stat / timing.h
1
2 #ifndef _TICKS_H
3 #define _TICKS_H
4
5 /*
6  * To use the Pentium RDTSC timer
7  * define TIMING_USE_RDTSC when including
8  */
9 #define TIMING_USE_RDTSC
10
11 #include <unistd.h>
12 #include <time.h>
13 #include <sys/time.h>
14
15 /* define GNU macro for processor affinity stuff if on linux */
16 #if defined __linux__ && !defined __USE_GNU
17 #define __USE_GNU
18 #endif
19 #include <sched.h>
20
21 typedef struct {
22 #ifdef _POSIX_PRIORITY_SCHEDULING
23         struct sched_param params;
24 #endif
25         int scheduler;
26 #ifdef __linux__
27         cpu_set_t affinity;
28 #endif
29 } timing_sched_env_t;
30
31 /* only use rdtsc on GNU C with x86 */
32 #if defined TIMING_USE_RDTSC && defined __GNUC__ && defined __i386__
33
34 typedef unsigned long long timing_ticks_t;
35 #define timing_ticks(t)              __asm__ __volatile__ ("rdtsc" : "=A" (t))
36 #define timing_ticks_init(t)         ((t) = 0)
37 #define timing_ticks_cmp(a, b, cmp)  ((a) cmp (b))
38 #define timing_ticks_sub(r, a)       ((r) = (r) - (a))
39 #define timing_ticks_add(r, a)       ((r) = (r) + (a))
40 #define timing_ticks_ulong(t)        ((unsigned long) (t))
41 #define timing_ticks_dbl(t)          ((double) (t))
42
43 #else
44
45 typedef struct timeval timing_ticks_t;
46 #define timing_ticks(t)              (gettimeofday(&(t), NULL))
47 #define timing_ticks_init(t)         ((t).tv_sec = 0, (t).tv_usec = 0)
48
49 /*
50  * This shamelessly stolen and modified from glibc's
51  * /usr/include/sys/time.h
52  */
53 #define timing_ticks_cmp(a, b, CMP)   \
54   (((a).tv_sec == (b).tv_sec) ?           \
55    ((a).tv_usec CMP (b).tv_usec) :    \
56    ((a).tv_sec CMP (b).tv_sec))
57
58 #define timing_ticks_add(r, a)                       \
59         do {                                                                     \
60                 (r).tv_sec = (r).tv_sec + (a).tv_sec;        \
61                 (r).tv_usec = (r).tv_usec + (a).tv_usec;     \
62                 if ((r).tv_usec >= 1000000) {                        \
63                         ++(r).tv_sec;                            \
64                         (r).tv_usec -= 1000000;                  \
65                 }                                                                                \
66         } while (0)
67
68 #define timing_ticks_sub(r, a)                        \
69         do {                                                                              \
70                 (r).tv_sec = (r).tv_sec - (a).tv_sec;         \
71                 (r).tv_usec = (r).tv_usec - (a).tv_usec;      \
72                 if ((r).tv_usec < 0) {                                        \
73                         --(r).tv_sec;                                                 \
74                         (r).tv_usec += 1000000;                                   \
75                 }                                                                                 \
76         } while (0)
77
78 #define timing_ticks_ulong(t)        ((unsigned long) ((t).tv_usec + 1000000 * (t).tv_sec))
79 #define timing_ticks_dbl(t)          (((t).tv_usec + 1000000.0 * (t).tv_sec))
80
81 #endif /* TIMING_USE_RDTSC ... */
82
83 /**
84  * Set the current schedule parameters.
85  * @return 1, if succeeded, 0 if not (see errno, for details).
86  */
87 int timing_sched_set(const timing_sched_env_t *env);
88
89 /**
90  * Get the schedule parameters.
91  * @return 1, if succeeded, 0 if not (see errno, for details).
92  */
93 timing_sched_env_t *timing_sched_get(timing_sched_env_t *env);
94
95 /**
96  * Prepare schedule parameters which limit the process on one CPU
97  * and set the maximum task priority.
98  * @return The paramter @p env.
99  */
100 timing_sched_env_t *timing_sched_prepare_max_prio(timing_sched_env_t *env);
101
102 #endif /* _TICKS_H */