aboutsummaryrefslogtreecommitdiffstats
path: root/python-schedutils/schedutils.c
blob: 65f1da7a9beaac0b197d5c6ef208e4e2ca0c33d8 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <Python.h>
#include <sched.h>
#include <errno.h>
#include <syscall.h>

#ifndef __unused
#define __unused __attribute__ ((unused))
#endif

#ifndef CPU_ALLOC

/* From glibc-headers-2.12-1.7.el6.x86_64 */

/* Basic access functions.  */
#define __CPUELT(cpu)  ((cpu) / __NCPUBITS)
#define __CPUMASK(cpu) ((__cpu_mask) 1 << ((cpu) % __NCPUBITS))

#define CPU_ALLOC_SIZE(ncpus) \
	((((ncpus) + __NCPUBITS - 1) / __NCPUBITS) * sizeof (__cpu_mask))

/* Access functions for CPU masks.  */
# if __GNUC_PREREQ (2, 91)
#  define __CPU_ZERO_S(setsize, cpusetp) \
  do __builtin_memset (cpusetp, '\0', setsize); while (0)
# else
#  define __CPU_ZERO_S(setsize, cpusetp) \
  do {                                                                        \
    size_t __i;                                                               \
    size_t __imax = (setsize) / sizeof (__cpu_mask);                          \
    __cpu_mask *__bits = (cpusetp)->__bits;                                   \
    for (__i = 0; __i < __imax; ++__i)                                        \
      __bits[__i] = 0;                                                        \
  } while (0)
# endif

#define CPU_FREE(cpus)  free(cpus)
#define CPU_ALLOC(ncpus) malloc(CPU_ALLOC_SIZE(ncpus))

# define __CPU_SET_S(cpu, setsize, cpusetp) \
  (__extension__                                                              \
   ({ size_t __cpu = (cpu);                                                   \
      __cpu < 8 * (setsize)                                                   \
      ? (((__cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)]               \
         |= __CPUMASK (__cpu))                                                \
      : 0; }))

# define __CPU_ISSET_S(cpu, setsize, cpusetp) \
  (__extension__                                                              \
   ({ size_t __cpu = (cpu);                                                   \
      __cpu < 8 * (setsize)                                                   \
      ? ((((__const __cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)]      \
          & __CPUMASK (__cpu))) != 0                                          \
      : 0; }))

#define CPU_SET_S(cpu, setsize, cpusetp) __CPU_SET_S (cpu, setsize, cpusetp)
#define CPU_ISSET_S(cpu, setsize, cpusetp) __CPU_ISSET_S (cpu, setsize, cpusetp)
#define CPU_ZERO_S(setsize, cpusetp) __CPU_ZERO_S (setsize, cpusetp)

#endif /* CPU_ALLOC */


#ifndef SCHED_RESET_ON_FORK
#define SCHED_RESET_ON_FORK 0x40000000
#endif

/*
 * The following bitmask declarations, bitmask_*() routines, and associated
 * _setbit() and _getbit() routines are:
 * Copyright (c) 2004 Silicon Graphics, Inc. (SGI) All rights reserved.
 * SGI publishes it under the terms of the GNU General Public License, v2,
 * as published by the Free Software Foundation.
 */
#define howmany(x,y) (((x)+((y)-1))/(y))
#define bitsperlong (8 * sizeof(unsigned long))
#define longsperbits(n) howmany(n, bitsperlong)
#define bytesperbits(x) ((x+7)/8)

/*
 * Number of bits in a CPU bitmask on current system
 */
static int __get_max_number_of_cpus(void)
{
	int n;
	int cpus = 2048;

	for (;;) {
		unsigned long buffer[longsperbits(cpus)];
		memset(buffer, 0, sizeof(buffer));
		/* the library version does not return size of cpumask_t */
		n = syscall(SYS_sched_getaffinity, 0, bytesperbits(cpus), &buffer);
		if (n < 0 && errno == EINVAL && cpus < 1024 * 1024) {
			cpus *= 2;
			continue;
		}
		return n * 8;
	}
	return -1;
}

static PyObject *get_max_number_of_cpus(PyObject *self __unused, PyObject *args __unused)
{
	int ret = __get_max_number_of_cpus();

	if (ret < 0)
		return PyErr_SetFromErrno(PyExc_OSError);

	return Py_BuildValue("i", ret);
}

static PyObject *get_affinity(PyObject *self __unused, PyObject *args)
{
	PyObject *list = NULL;
	cpu_set_t *cpus;
	int pid, cpu;
	size_t cpusetsize;
	int max_cpus;

	if (!PyArg_ParseTuple(args, "i", &pid))
		goto out_error;

	max_cpus = __get_max_number_of_cpus();
	if (max_cpus < 0)
		goto out_error;

	cpus = CPU_ALLOC(max_cpus);
	if (cpus == NULL)
		return PyErr_NoMemory();

	cpusetsize = CPU_ALLOC_SIZE(max_cpus);
	CPU_ZERO_S(cpusetsize, cpus);

	if (sched_getaffinity(pid, cpusetsize, cpus) < 0)
		goto out_free;

	list = PyList_New(0);
	for (cpu = 0; cpu < max_cpus; ++cpu)
		if (CPU_ISSET_S(cpu, cpusetsize, cpus))
			PyList_Append(list, Py_BuildValue("i", cpu));

	CPU_FREE(cpus);

	return list;
out_free:
	CPU_FREE(cpus);
out_error:
	return PyErr_SetFromErrno(PyExc_OSError);
}

static PyObject *set_affinity(PyObject *self __unused, PyObject *args)
{
	int pid, nr_elements, i, max_cpus;
	cpu_set_t *cpus;
	PyObject *list;
	size_t cpusetsize;

	if (!PyArg_ParseTuple(args, "iO", &pid, &list))
		goto out_error;

	max_cpus = __get_max_number_of_cpus();
	if (max_cpus < 0)
		goto out_error;

	cpus = CPU_ALLOC(max_cpus);
	if (cpus == NULL)
		return PyErr_NoMemory();

	cpusetsize = CPU_ALLOC_SIZE(max_cpus);
	CPU_ZERO_S(cpusetsize, cpus);

	nr_elements = PyList_Size(list);
	for (i = 0; i < nr_elements; ++i) {
		int cpu = PyInt_AsLong(PyList_GetItem(list, i));

		if (cpu >= max_cpus) {
			PyErr_SetString(PyExc_OSError, "Invalid CPU");
			CPU_FREE(cpus);
			return NULL;
		}
		CPU_SET_S(cpu, cpusetsize, cpus);
	}

	i = sched_setaffinity(pid, sizeof(cpus), cpus);
	CPU_FREE(cpus);

	if (i < 0)
		return PyErr_SetFromErrno(PyExc_OSError);

	Py_INCREF(Py_None);
	return Py_None;
out_error:
	return PyErr_SetFromErrno(PyExc_OSError);
}

static PyObject *get_scheduler(PyObject *self __unused, PyObject *args)
{
	int pid, scheduler;

	if (!PyArg_ParseTuple(args, "i", &pid))
		return NULL;

	scheduler = sched_getscheduler(pid);
	if (scheduler < 0)
		return PyErr_SetFromErrno(PyExc_OSError);

	return Py_BuildValue("i", scheduler);
}

static PyObject *set_scheduler(PyObject *self __unused, PyObject *args)
{
	int pid, policy, priority;
	struct sched_param param;

	if (!PyArg_ParseTuple(args, "iii", &pid, &policy, &priority))
		return NULL;

	memset(&param, 0, sizeof(param));
	param.sched_priority = priority;

	if (sched_setscheduler(pid, policy, &param) < 0)
		return PyErr_SetFromErrno(PyExc_OSError);

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *get_priority(PyObject *self __unused, PyObject *args)
{
	int pid;
	struct sched_param param = { .sched_priority = -1, };

	if (!PyArg_ParseTuple(args, "i", &pid))
		return NULL;

	if (sched_getparam(pid, &param) != 0)
		return PyErr_SetFromErrno(PyExc_OSError);

	return Py_BuildValue("i", param.sched_priority);
}

#ifndef SCHED_BATCH
#define SCHED_BATCH             3
#endif
#ifndef SCHED_IDLE
#define SCHED_IDLE              5
#endif

static PyObject *schedstr(PyObject *self __unused, PyObject *args)
{
	int scheduler;
	char *s;

	if (!PyArg_ParseTuple(args, "i", &scheduler))
		return NULL;

	switch (scheduler & ~SCHED_RESET_ON_FORK) {
	case SCHED_OTHER: s = "SCHED_OTHER"; break;
	case SCHED_RR:	  s = "SCHED_RR";    break;
	case SCHED_FIFO:  s = "SCHED_FIFO";  break;
	case SCHED_BATCH: s = "SCHED_BATCH"; break;
	case SCHED_IDLE:  s = "SCHED_IDLE";  break;
	default:	  s = "UNKNOWN";     break;
	}

	return PyString_FromString(s);
}

static PyObject *schedfromstr(PyObject *self __unused, PyObject *args)
{
	int scheduler;
	char *s;

	if (!PyArg_ParseTuple(args, "s", &s))
		return NULL;

	if (strcmp(s, "SCHED_OTHER") == 0)
		scheduler = SCHED_OTHER;
	else if (strcmp(s, "SCHED_RR") == 0)
		scheduler = SCHED_RR;
	else if (strcmp(s, "SCHED_FIFO") == 0)
		scheduler = SCHED_FIFO;
	else if (strcmp(s, "SCHED_BATCH") == 0)
		scheduler = SCHED_BATCH;
	else if (strcmp(s, "SCHED_IDLE") == 0)
		scheduler = SCHED_IDLE;
	else {
		PyErr_SetString(PyExc_OSError, "Unknown scheduler");
		return NULL;
	}

	return Py_BuildValue("i", scheduler);
}

static PyObject *get_priority_min(PyObject *self __unused, PyObject *args)
{
	int policy, min;

	if (!PyArg_ParseTuple(args, "i", &policy))
		return NULL;

	min = sched_get_priority_min(policy);
	if (min < 0)
		return PyErr_SetFromErrno(PyExc_OSError);

	return Py_BuildValue("i", min);
}

static PyObject *get_priority_max(PyObject *self __unused, PyObject *args)
{
	int policy, max;

	if (!PyArg_ParseTuple(args, "i", &policy))
		return NULL;

	max = sched_get_priority_max(policy);
	if (max < 0)
		return PyErr_SetFromErrno(PyExc_OSError);

	return Py_BuildValue("i", max);
}

static struct PyMethodDef PySchedutilsModuleMethods[] = {
	{
		.ml_name = "get_affinity",
		.ml_meth = (PyCFunction)get_affinity,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "set_affinity",
		.ml_meth = (PyCFunction)set_affinity,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "get_scheduler",
		.ml_meth = (PyCFunction)get_scheduler,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "set_scheduler",
		.ml_meth = (PyCFunction)set_scheduler,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "get_priority",
		.ml_meth = (PyCFunction)get_priority,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "schedstr",
		.ml_meth = (PyCFunction)schedstr,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "schedfromstr",
		.ml_meth = (PyCFunction)schedfromstr,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "get_priority_min",
		.ml_meth = (PyCFunction)get_priority_min,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "get_priority_max",
		.ml_meth = (PyCFunction)get_priority_max,
		.ml_flags = METH_VARARGS,
	},
	{
		.ml_name = "get_max_number_of_cpus",
		.ml_meth = (PyCFunction)get_max_number_of_cpus,
	},
	{	.ml_name = NULL, },
};

PyMODINIT_FUNC initschedutils(void)
{
	PyObject *m = Py_InitModule("schedutils", PySchedutilsModuleMethods);
	if (m == NULL)
		return;

	PyModule_AddIntConstant(m, "SCHED_OTHER", SCHED_OTHER);
	PyModule_AddIntConstant(m, "SCHED_FIFO", SCHED_FIFO);
	PyModule_AddIntConstant(m, "SCHED_RR", SCHED_RR);
	PyModule_AddIntConstant(m, "SCHED_BATCH", SCHED_BATCH);
	PyModule_AddIntConstant(m, "SCHED_IDLE", SCHED_IDLE);
	PyModule_AddIntConstant(m, "SCHED_RESET_ON_FORK", SCHED_RESET_ON_FORK);
}