forked from chaos/slurm-spank-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setsched.c
222 lines (187 loc) · 5.53 KB
/
setsched.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/***************************************************************************\
* setsched.c - Spank Plugin to enforce a particular kernel scheduling policy
***************************************************************************
*
* Copyright CEA/DAM/DIF (2009)
*
* Written by Matthieu Hautreux <[email protected]>
*
* This file is part of slurm-spank-plugins, a set of spank plugins
* for SLURM.
*
* This 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 2 of the License, or
* (at your option) any later version.
*
* This 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
\***************************************************************************/
/*
* To compile it : gcc -fPIC -shared -o setsched.so setsched.c
*
* This plugin can be used to enforce a particular scheduling policy
* as well as the associated priority of tasks spawned by slurm.
*
* The following configuration parameters are available on server side :
*
* policy : set the kernel scheduling policy to use (default is 0)
* priority : set the priority to configure with the policy (default is 0)
* default : set setsched plugin default behavior i.e. enabled/disabled
*
* Users can alter the enabled/disabled behavior on command line using
* --setsched
*
* setsched can be used only if at least one of the policy or priority
* parameters are set to a non-zero value
*
* Here is an example of configuration :
*
* optional setsched.so policy=55 priority=0 default=disabled
*
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/resource.h>
#include <limits.h>
#ifdef _POSIX_PRIORITY_SCHEDULING
#include <sched.h>
#else
#warning "no Posix priority scheduling primitives detected on this system"
#endif
#include <errno.h>
extern int errno;
#include <slurm/spank.h>
#define SPANK_SETSCHED_VERSION "0.1.4"
static int setsched_pol=0;
static int setsched_prio=0;
static int setsched_default=0;
#define xinfo slurm_info
#define xerror slurm_error
#define xdebug slurm_debug
/*
* All spank plugins must define this macro for the SLURM plugin loader.
*/
SPANK_PLUGIN(setsched, 1);
static int _str2int (const char *str, int *p2int)
{
long int l;
char *p;
l = strtol (str, &p, 10);
/* check for underflow and overflow */
if ( l == LONG_MIN || l == LONG_MAX )
return (-1);
*p2int = (int) l;
return (0);
}
static int _setsched_opt_process (int val, const char *optarg, int remote)
{
if (optarg == NULL) {
return 0;
}
if (strncmp ("no", optarg, 2) == 0) {
setsched_default=0;
xdebug("setsched: disabled on user request");
}
else if (strncmp ("yes", optarg, 3) == 0) {
setsched_default=1;
xdebug("setsched: enabled on user request");
}
else if (strncmp ("auto", optarg, 4) != 0) {
xerror ("setsched: bad parameter %s", optarg);
return (-1);
}
return (0);
}
/*
* Provide a --setsched=[yes|no|auto] option to srun:
*/
struct spank_option spank_options[] =
{
{ "setsched", "[yes|no|auto]", "Activate/Desactivate scheduling policy "
"setting of Setsched spank plugin", 2, 0,
(spank_opt_cb_f) _setsched_opt_process
},
SPANK_OPTIONS_TABLE_END
};
/*
* Called from both srun and slurmd.
*/
int slurm_spank_init (spank_t sp, int ac, char **av)
{
int i;
int pol=0;
int prio=0;
/* do something in remote mode only */
if ( ! spank_remote(sp) )
return 0;
for (i = 0; i < ac; i++) {
if (strncmp ("policy=", av[i], 7) == 0) {
const char *optarg = av[i] + 7;
if (_str2int (optarg, &pol) < 0)
xerror ("setsched: ignoring invalid policy "
"value: %s", av[i]);
}
else if (strncmp ("priority=", av[i], 9) == 0) {
const char *optarg = av[i] + 9;
if (_str2int (optarg, &prio) < 0)
xerror ("setsched: ignoring invalid priority "
"value: %s", av[i]);
}
else if (strncmp ("default=enabled", av[i], 15) == 0) {
setsched_default=1;
}
else if (strncmp ("default=disabled", av[i], 16) == 0) {
setsched_default=0;
}
else {
xerror ("setsched: "
"invalid option: %s", av[i]);
}
}
if ( pol > 0 || prio > 0 ) {
setsched_pol=pol;
setsched_prio=prio;
xdebug("setsched: configuration is policy=%d "
"priority=%d default=%s (version %s)",
setsched_pol,setsched_prio,
setsched_default?"enabled":"disabled",
SPANK_SETSCHED_VERSION);
}
return 0;
}
int slurm_spank_task_post_fork (spank_t sp, int ac, char **av)
{
int status = 0;
pid_t pid;
int taskid;
int pol;
struct sched_param spar;
if ( setsched_default && ( setsched_pol > 0 || setsched_prio > 0 ) ) {
pol=setsched_pol;
spar.sched_priority=setsched_prio;
spank_get_item (sp, S_TASK_GLOBAL_ID, &taskid);
spank_get_item (sp, S_TASK_PID, &pid);
status = sched_setscheduler(pid, pol, &spar);
if (status < 0) {
xerror("setsched: unable to set scheduling "
"policy of task%d pid %d : %s",
taskid, pid,strerror(errno));
}
else
xinfo("setsched: "
"scheduling policy of task%d pid %d is "
"now %d (prio=%d)",
taskid, pid, pol, setsched_prio);
}
return status;
}