- get_Block_cfgpred_arr() IS supported, but should not be in the official
[libfirm] / ir / stat / 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 #if defined(_WIN32)
12
13 #ifdef TIMING_USE_RDTSC
14
15 typedef unsigned __int64 timing_ticks_t;
16
17 __inline timing_ticks_t __timing_ticks(void) { __asm { rdtsc } }
18
19 #define timing_ticks(t)              ((t) = __timing_ticks())
20 #define timing_ticks_init(t)         ((t) = 0)
21 #define timing_ticks_cmp(a, b, cmp)  ((a) cmp (b))
22 #define timing_ticks_sub(r, a)       ((r) = (r) - (a))
23 #define timing_ticks_add(r, a)       ((r) = (r) + (a))
24 #define timing_ticks_ulong(t)        ((unsigned long) (t))
25 #define timing_ticks_dbl(t)          ((double) (t))
26
27 #else
28 #error NOT IMPLEMENTED YET
29 #endif /* TIMING_USE_RDTSC */
30
31 typedef struct {
32         int dummy;
33 } timing_sched_env_t;
34
35 #else /* POSIX/Linux stuff */
36
37 #include <unistd.h>
38 #include <time.h>
39 #include <sys/time.h>
40
41 /* define GNU macro for processor affinity stuff if on linux */
42 #if defined __linux__ && !defined __USE_GNU
43 #define __USE_GNU
44 #endif
45 #include <sched.h>
46
47 typedef struct {
48 #ifdef _POSIX_PRIORITY_SCHEDULING
49         struct sched_param params;
50 #endif
51         int scheduler;
52 #ifdef __linux__
53         cpu_set_t affinity;
54 #endif
55 } timing_sched_env_t;
56
57 /* only use rdtsc on GNU C with x86 */
58 #if defined TIMING_USE_RDTSC && defined __GNUC__ && defined __i386__
59
60 typedef unsigned long long timing_ticks_t;
61 #define timing_ticks(t)              __asm__ __volatile__ ("rdtsc" : "=A" (t))
62 #define timing_ticks_init(t)         ((t) = 0)
63 #define timing_ticks_cmp(a, b, cmp)  ((a) cmp (b))
64 #define timing_ticks_sub(r, a)       ((r) = (r) - (a))
65 #define timing_ticks_add(r, a)       ((r) = (r) + (a))
66 #define timing_ticks_ulong(t)        ((unsigned long) (t))
67 #define timing_ticks_dbl(t)          ((double) (t))
68
69 #else
70
71 typedef struct timeval timing_ticks_t;
72 #define timing_ticks(t)              (gettimeofday(&(t), NULL))
73 #define timing_ticks_init(t)         ((t).tv_sec = 0, (t).tv_usec = 0)
74
75 /*
76  * This shamelessly stolen and modified from glibc's
77  * /usr/include/sys/time.h
78  */
79 #define timing_ticks_cmp(a, b, CMP)   \
80   (((a).tv_sec == (b).tv_sec) ?           \
81    ((a).tv_usec CMP (b).tv_usec) :    \
82    ((a).tv_sec CMP (b).tv_sec))
83
84 #define timing_ticks_add(r, a)                       \
85         do {                                                                     \
86                 (r).tv_sec = (r).tv_sec + (a).tv_sec;        \
87                 (r).tv_usec = (r).tv_usec + (a).tv_usec;     \
88                 if ((r).tv_usec >= 1000000) {                        \
89                         ++(r).tv_sec;                            \
90                         (r).tv_usec -= 1000000;                  \
91                 }                                                                                \
92         } while (0)
93
94 #define timing_ticks_sub(r, a)                        \
95         do {                                                                              \
96                 (r).tv_sec = (r).tv_sec - (a).tv_sec;         \
97                 (r).tv_usec = (r).tv_usec - (a).tv_usec;      \
98                 if ((r).tv_usec < 0) {                                        \
99                         --(r).tv_sec;                                                 \
100                         (r).tv_usec += 1000000;                                   \
101                 }                                                                                 \
102         } while (0)
103
104 #define timing_ticks_ulong(t)        ((unsigned long) ((t).tv_usec + 1000000 * (t).tv_sec))
105 #define timing_ticks_dbl(t)          (((t).tv_usec + 1000000.0 * (t).tv_sec))
106
107 #endif /* TIMING_USE_RDTSC ... */
108
109 #endif /* _WIN32 */
110
111 /**
112  * Set the current schedule parameters.
113  * @return 1, if succeeded, 0 if not (see errno, for details).
114  */
115 int timing_sched_set(const timing_sched_env_t *env);
116
117 /**
118  * Get the schedule parameters.
119  * @return 1, if succeeded, 0 if not (see errno, for details).
120  */
121 timing_sched_env_t *timing_sched_get(timing_sched_env_t *env);
122
123 /**
124  * Prepare schedule parameters which limit the process on one CPU
125  * and set the maximum task priority.
126  * @return The paramter @p env.
127  */
128 timing_sched_env_t *timing_sched_prepare_max_prio(timing_sched_env_t *env);
129
130 #endif /* _TICKS_H */