Debian Codegraph @

psutil/arch/linux/proc.c

raw
1 /*
2 * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 #include "../../arch/all/init.h"
8
9 #include <Python.h>
10 #include <sys/syscall.h>
11 #include <sched.h>
12 #include <unistd.h>
13
14 #ifdef PSUTIL_HAS_IOPRIO
15 enum {
16 IOPRIO_WHO_PROCESS = 1,
17 };
18
19 static inline int
20 ioprio_get(int which, int who) {
21 return syscall(__NR_ioprio_get, which, who);
22 }
23
24 static inline int
25 ioprio_set(int which, int who, int ioprio) {
26 return syscall(__NR_ioprio_set, which, who, ioprio);
27 }
28
29 #define IOPRIO_CLASS_SHIFT 13
30 #define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
31
32 #define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
33 #define IOPRIO_PRIO_DATA(mask) ((mask) & IOPRIO_PRIO_MASK)
34 #define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
35
36
37 // Return a (ioclass, iodata) Python tuple representing process I/O
38 // priority.
39 PyObject *
40 psutil_proc_ioprio_get(PyObject *self, PyObject *args) {
41 pid_t pid;
42 int ioprio, ioclass, iodata;
43 if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid))
44 return NULL;
45 ioprio = ioprio_get(IOPRIO_WHO_PROCESS, pid);
46 if (ioprio == -1)
47 return PyErr_SetFromErrno(PyExc_OSError);
48 ioclass = IOPRIO_PRIO_CLASS(ioprio);
49 iodata = IOPRIO_PRIO_DATA(ioprio);
50 return Py_BuildValue("ii", ioclass, iodata);
51 }
52
53
54 // A wrapper around ioprio_set(); sets process I/O priority. ioclass
55 // can be either IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE or
56 // 0. iodata goes from 0 to 7 depending on ioclass specified.
57 PyObject *
58 psutil_proc_ioprio_set(PyObject *self, PyObject *args) {
59 pid_t pid;
60 int ioprio, ioclass, iodata;
61 int retval;
62
63 if (! PyArg_ParseTuple(
64 args, _Py_PARSE_PID "ii", &pid, &ioclass, &iodata)) {
65 return NULL;
66 }
67 ioprio = IOPRIO_PRIO_VALUE(ioclass, iodata);
68 retval = ioprio_set(IOPRIO_WHO_PROCESS, pid, ioprio);
69 if (retval == -1)
70 return PyErr_SetFromErrno(PyExc_OSError);
71 Py_RETURN_NONE;
72 }
73 #endif // PSUTIL_HAS_IOPRIO
74
75
76 #ifdef PSUTIL_HAS_CPU_AFFINITY
77
78 // Return process CPU affinity as a Python list.
79 PyObject *
80 psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args) {
81 int cpu, ncpus, count, cpucount_s;
82 pid_t pid;
83 size_t setsize;
84 cpu_set_t *mask = NULL;
85 PyObject *py_list = NULL;
86
87 if (!PyArg_ParseTuple(args, _Py_PARSE_PID, &pid))
88 return NULL;
89
90 // The minimum number of CPUs allocated in a cpu_set_t.
91 ncpus = sizeof(unsigned long) * CHAR_BIT;
92 while (1) {
93 setsize = CPU_ALLOC_SIZE(ncpus);
94 mask = CPU_ALLOC(ncpus);
95 if (mask == NULL) {
96 psutil_debug("CPU_ALLOC() failed");
97 return PyErr_NoMemory();
98 }
99 if (sched_getaffinity(pid, setsize, mask) == 0)
100 break;
101 CPU_FREE(mask);
102 if (errno != EINVAL)
103 return PyErr_SetFromErrno(PyExc_OSError);
104 if (ncpus > INT_MAX / 2) {
105 PyErr_SetString(PyExc_OverflowError, "could not allocate "
106 "a large enough CPU set");
107 return NULL;
108 }
109 ncpus = ncpus * 2;
110 }
111
112 py_list = PyList_New(0);
113 if (py_list == NULL)
114 goto error;
115
116 cpucount_s = CPU_COUNT_S(setsize, mask);
117 for (cpu = 0, count = cpucount_s; count; cpu++) {
118 if (CPU_ISSET_S(cpu, setsize, mask)) {
119 PyObject *cpu_num = PyLong_FromLong(cpu);
120 if (cpu_num == NULL)
121 goto error;
122 if (PyList_Append(py_list, cpu_num)) {
123 Py_DECREF(cpu_num);
124 goto error;
125 }
126 Py_DECREF(cpu_num);
127 --count;
128 }
129 }
130 CPU_FREE(mask);
131 return py_list;
132
133 error:
134 if (mask)
135 CPU_FREE(mask);
136 Py_XDECREF(py_list);
137 return NULL;
138 }
139
140
141 // Set process CPU affinity; expects a bitmask.
142 PyObject *
143 psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) {
144 cpu_set_t cpu_set;
145 size_t len;
146 pid_t pid;
147 Py_ssize_t i, seq_len;
148 PyObject *py_cpu_set;
149
150 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O", &pid, &py_cpu_set))
151 return NULL;
152
153 if (!PySequence_Check(py_cpu_set)) {
154 return PyErr_Format(
155 PyExc_TypeError,
156 "sequence argument expected, got %R", Py_TYPE(py_cpu_set)
157 );
158 }
159
160 seq_len = PySequence_Size(py_cpu_set);
161 if (seq_len < 0) {
162 return NULL;
163 }
164 CPU_ZERO(&cpu_set);
165 for (i = 0; i < seq_len; i++) {
166 PyObject *item = PySequence_GetItem(py_cpu_set, i);
167 if (!item) {
168 return NULL;
169 }
170 long value = PyLong_AsLong(item);
171 Py_XDECREF(item);
172 if ((value == -1) || PyErr_Occurred()) {
173 if (!PyErr_Occurred())
174 PyErr_SetString(PyExc_ValueError, "invalid CPU value");
175 return NULL;
176 }
177 CPU_SET(value, &cpu_set);
178 }
179
180 len = sizeof(cpu_set);
181 if (sched_setaffinity(pid, len, &cpu_set)) {
182 return PyErr_SetFromErrno(PyExc_OSError);
183 }
184
185 Py_RETURN_NONE;
186 }
187 #endif // PSUTIL_HAS_CPU_AFFINITY