Imported Upstream version 3.2.2
[debian/gnuradio] / gruel / src / lib / realtime.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2007,2008 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <gruel/realtime.h>
28
29 #ifdef HAVE_SCHED_H
30 #include <sched.h>
31 #endif
32
33 #include <algorithm>
34 #include <math.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <stdio.h>
38
39 #if defined(HAVE_PTHREAD_SETSCHEDPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
40 #include <pthread.h>
41
42 namespace gruel {
43
44   /*!
45    * Rescale our virtual priority so that it maps to the middle 1/2 of
46    * the priorities given by min_real_pri and max_real_pri.
47    */
48   static int
49   rescale_virtual_pri(int virtual_pri, int min_real_pri, int max_real_pri)
50   {
51     float rmin = min_real_pri + (0.25 * (max_real_pri - min_real_pri));
52     float rmax = min_real_pri + (0.75 * (max_real_pri - min_real_pri));
53     float m = (rmax - rmin) / (rt_priority_max() - rt_priority_min());
54     float y = m * (virtual_pri - rt_priority_min()) + rmin;
55     int   y_int = static_cast<int>(rint(y));
56     return std::max(min_real_pri, std::min(max_real_pri, y_int));
57   }
58
59 }  // namespace gruel
60
61 #endif
62
63
64 #if defined(HAVE_PTHREAD_SETSCHEDPARAM)
65
66 namespace gruel {
67
68   rt_status_t
69   enable_realtime_scheduling(rt_sched_param p)
70   {
71     int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR;
72     int min_real_pri = sched_get_priority_min(policy);
73     int max_real_pri = sched_get_priority_max(policy);
74     int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri);
75
76     // FIXME check hard and soft limits with getrlimit, and limit the value we ask for.
77     // fprintf(stderr, "pthread_setschedparam: policy = %d, pri = %d\n", policy, pri);
78
79     struct sched_param param;
80     memset (&param, 0, sizeof (param));
81     param.sched_priority = pri;
82     int result = pthread_setschedparam (pthread_self(), policy, &param);
83     if (result != 0) {
84       if (result == EPERM)      // N.B., return value, not errno
85         return RT_NO_PRIVS;
86       else {
87         fprintf(stderr,
88                 "pthread_setschedparam: failed to set real time priority: %s\n",
89                 strerror(result));
90         return RT_OTHER_ERROR;
91       }
92     }
93   
94     //printf("SCHED_FIFO enabled with priority = %d\n", pri);
95     return RT_OK;
96   }
97 } // namespace gruel
98
99
100 #elif defined(HAVE_SCHED_SETSCHEDULER)
101
102 namespace gruel {
103
104   rt_status_t
105   enable_realtime_scheduling(rt_sched_param p)
106   {
107     int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR;
108     int min_real_pri = sched_get_priority_min(policy);
109     int max_real_pri = sched_get_priority_max(policy);
110     int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri);
111
112     // FIXME check hard and soft limits with getrlimit, and limit the value we ask for.
113     // fprintf(stderr, "sched_setscheduler: policy = %d, pri = %d\n", policy, pri);
114
115     int pid = 0;  // this process
116     struct sched_param param;
117     memset(&param, 0, sizeof(param));
118     param.sched_priority = pri;
119     int result = sched_setscheduler(pid, policy, &param);
120     if (result != 0){
121       if (errno == EPERM)
122         return RT_NO_PRIVS;
123       else {
124         perror ("sched_setscheduler: failed to set real time priority");
125         return RT_OTHER_ERROR;
126       }
127     }
128     
129     //printf("SCHED_FIFO enabled with priority = %d\n", pri);
130     return RT_OK;
131   }
132
133 } // namespace gruel
134
135 #else
136
137 namespace gruel {
138
139   rt_status_t
140   enable_realtime_scheduling(rt_sched_param p)
141   {
142     return RT_NOT_IMPLEMENTED;
143   }
144 } // namespace gruel
145
146 #endif