Removed unused variables.
[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  * @version $Id$
25  */
26 #include "config.h"
27
28 #include "stat_timing.h"
29
30 #include <stdio.h>
31
32 #ifdef __linux__
33
34 #include <unistd.h>
35 #include <time.h>
36 #include <sys/time.h>
37
38 /* define GNU macro for processor affinity stuff if on linux */
39 #if defined __linux__ && !defined __USE_GNU
40 #define __USE_GNU
41 #endif
42 #include <sched.h>
43
44 /* we can only use the scheduling stuff, if that macro is defined in unistd.h */
45 #if defined(_XOPEN_REALTIME) && _XOPEN_REALTIME != -1
46
47 #define HAVE_IMPL
48
49 static int                 in_max_prio = 0;
50 static cpu_set_t           affinity;
51 static int                 scheduler;
52 static struct sched_param  sched_params;
53
54 void timing_enter_max_prio(void)
55 {
56         int                 res;
57         int                 new_scheduler = SCHED_FIFO;
58         struct sched_param  new_sched_params;
59         cpu_set_t           new_affinity;
60
61         if (in_max_prio)
62                 return;
63
64         /* remember old scheduler settings */
65         res = sched_getaffinity(0, sizeof(affinity), &affinity);
66         if (res < 0)
67                 return;
68         scheduler = sched_getscheduler(0);
69         if (scheduler < 0)
70                 return;
71         res = sched_getparam(0, &sched_params);
72         if (res < 0)
73                 return;
74
75         /* set high prio */
76         CPU_ZERO(&new_affinity);
77         CPU_SET(0, &new_affinity);
78         res = sched_setaffinity(0, sizeof(new_affinity), &new_affinity);
79         if (res < 0)
80                 return;
81         new_scheduler = SCHED_FIFO;
82         new_sched_params = sched_params;
83         new_sched_params.sched_priority = sched_get_priority_max(new_scheduler);
84         sched_setscheduler(0, new_scheduler, &new_sched_params);
85         if (res < 0)
86                 return;
87
88         in_max_prio = 1;
89 }
90
91 void timing_leave_max_prio(void)
92 {
93         int res;
94
95         if (!in_max_prio)
96                 return;
97
98         /* restore old settings */
99         res = sched_setaffinity(0, sizeof(affinity), &affinity);
100         if (res < 0)
101                 return;
102
103         sched_setscheduler(0, scheduler, &sched_params);
104         if (res < 0)
105                 return;
106
107         in_max_prio = 0;
108 }
109
110 #endif
111 #endif
112
113
114 #ifndef HAVE_IMPL
115
116 /* dummy implementation */
117
118 void timing_enter_max_prio(void)
119 {
120 }
121
122 void timing_leave_max_prio(void)
123 {
124 }
125
126 #endif