beifg: Simplify the quite complicated way to divide a number by 2 in be_ifg_stat().
[libfirm] / ir / stat / stat_timing.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   OS abstraction from time measurement
9  * @author  Sebastian Hack, Michael Beck, Matthias Braun
10  */
11 #include "config.h"
12
13 #include "stat_timing.h"
14
15 #include <stdio.h>
16
17 #ifdef __linux__
18
19 #include <unistd.h>
20 #include <time.h>
21 #include <sys/time.h>
22
23 /* define GNU macro for processor affinity stuff if on linux */
24 #if defined __linux__ && !defined __USE_GNU
25 #define __USE_GNU
26 #endif
27 #include <sched.h>
28
29 /* we can only use the scheduling stuff, if that macro is defined in unistd.h */
30 #if defined(_XOPEN_REALTIME) && _XOPEN_REALTIME != -1
31
32 #define HAVE_IMPL
33
34 static int                 in_max_prio = 0;
35 static cpu_set_t           affinity;
36 static int                 scheduler;
37 static struct sched_param  sched_params;
38
39 void timing_enter_max_prio(void)
40 {
41         int                 res;
42         int                 new_scheduler = SCHED_FIFO;
43         struct sched_param  new_sched_params;
44         cpu_set_t           new_affinity;
45
46         if (in_max_prio)
47                 return;
48
49         /* remember old scheduler settings */
50         res = sched_getaffinity(0, sizeof(affinity), &affinity);
51         if (res < 0)
52                 return;
53         scheduler = sched_getscheduler(0);
54         if (scheduler < 0)
55                 return;
56         res = sched_getparam(0, &sched_params);
57         if (res < 0)
58                 return;
59
60         /* set high prio */
61         CPU_ZERO(&new_affinity);
62         CPU_SET(0, &new_affinity);
63         res = sched_setaffinity(0, sizeof(new_affinity), &new_affinity);
64         if (res < 0)
65                 return;
66         new_scheduler = SCHED_FIFO;
67         new_sched_params = sched_params;
68         new_sched_params.sched_priority = sched_get_priority_max(new_scheduler);
69         sched_setscheduler(0, new_scheduler, &new_sched_params);
70         if (res < 0)
71                 return;
72
73         in_max_prio = 1;
74 }
75
76 void timing_leave_max_prio(void)
77 {
78         int res;
79
80         if (!in_max_prio)
81                 return;
82
83         /* restore old settings */
84         res = sched_setaffinity(0, sizeof(affinity), &affinity);
85         if (res < 0)
86                 return;
87
88         sched_setscheduler(0, scheduler, &sched_params);
89         if (res < 0)
90                 return;
91
92         in_max_prio = 0;
93 }
94
95 #endif
96 #endif
97
98
99 #ifndef HAVE_IMPL
100
101 /* dummy implementation */
102
103 void timing_enter_max_prio(void)
104 {
105 }
106
107 void timing_leave_max_prio(void)
108 {
109 }
110
111 #endif