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