remove extended basic block support
[libfirm] / ir / stat / stat_timing.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   OS abstraction from time measurement
23  * @author  Sebastian Hack, Michael Beck, Matthias Braun
24  */
25 #include "config.h"
26
27 #include "stat_timing.h"
28
29 #include <stdio.h>
30
31 #ifdef __linux__
32
33 #include <unistd.h>
34 #include <time.h>
35 #include <sys/time.h>
36
37 /* define GNU macro for processor affinity stuff if on linux */
38 #if defined __linux__ && !defined __USE_GNU
39 #define __USE_GNU
40 #endif
41 #include <sched.h>
42
43 /* we can only use the scheduling stuff, if that macro is defined in unistd.h */
44 #if defined(_XOPEN_REALTIME) && _XOPEN_REALTIME != -1
45
46 #define HAVE_IMPL
47
48 static int                 in_max_prio = 0;
49 static cpu_set_t           affinity;
50 static int                 scheduler;
51 static struct sched_param  sched_params;
52
53 void timing_enter_max_prio(void)
54 {
55         int                 res;
56         int                 new_scheduler = SCHED_FIFO;
57         struct sched_param  new_sched_params;
58         cpu_set_t           new_affinity;
59
60         if (in_max_prio)
61                 return;
62
63         /* remember old scheduler settings */
64         res = sched_getaffinity(0, sizeof(affinity), &affinity);
65         if (res < 0)
66                 return;
67         scheduler = sched_getscheduler(0);
68         if (scheduler < 0)
69                 return;
70         res = sched_getparam(0, &sched_params);
71         if (res < 0)
72                 return;
73
74         /* set high prio */
75         CPU_ZERO(&new_affinity);
76         CPU_SET(0, &new_affinity);
77         res = sched_setaffinity(0, sizeof(new_affinity), &new_affinity);
78         if (res < 0)
79                 return;
80         new_scheduler = SCHED_FIFO;
81         new_sched_params = sched_params;
82         new_sched_params.sched_priority = sched_get_priority_max(new_scheduler);
83         sched_setscheduler(0, new_scheduler, &new_sched_params);
84         if (res < 0)
85                 return;
86
87         in_max_prio = 1;
88 }
89
90 void timing_leave_max_prio(void)
91 {
92         int res;
93
94         if (!in_max_prio)
95                 return;
96
97         /* restore old settings */
98         res = sched_setaffinity(0, sizeof(affinity), &affinity);
99         if (res < 0)
100                 return;
101
102         sched_setscheduler(0, scheduler, &sched_params);
103         if (res < 0)
104                 return;
105
106         in_max_prio = 0;
107 }
108
109 #endif
110 #endif
111
112
113 #ifndef HAVE_IMPL
114
115 /* dummy implementation */
116
117 void timing_enter_max_prio(void)
118 {
119 }
120
121 void timing_leave_max_prio(void)
122 {
123 }
124
125 #endif