6fb50323f78c76891eb9660009fb8f1e9a436b41
[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         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