blob: 876831537236f615b46afef449fc23539e9a04de [file] [log] [blame]
Guido van Rossumf84a5391997-08-15 00:04:24 +00001
Guido van Rossum2e631391996-12-18 18:37:27 +00002#include "Python.h"
3#include <sys/resource.h>
4#include <sys/time.h>
Guido van Rossum2e631391996-12-18 18:37:27 +00005#include <string.h>
6#include <errno.h>
Martin v. Löwisf26d63b2003-03-30 17:23:49 +00007/* for sysconf */
8#if defined(HAVE_UNISTD_H)
9#include <unistd.h>
10#endif
Guido van Rossum2e631391996-12-18 18:37:27 +000011
Guido van Rossum607b33a1997-08-17 16:24:30 +000012/* On some systems, these aren't in any header file.
13 On others they are, with inconsistent prototypes.
14 We declare the (default) return type, to shut up gcc -Wall;
15 but we can't declare the prototype, to avoid errors
16 when the header files declare it different.
17 Worse, on some Linuxes, getpagesize() returns a size_t... */
Guido van Rossum2e631391996-12-18 18:37:27 +000018
19#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
20
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000021PyDoc_STRVAR(struct_rusage__doc__,
22"struct_rusage: Result from getrusage.\n\n"
23"This object may be accessed either as a tuple of\n"
24" (utime,stime,maxrss,ixrss,idrss,isrss,minflt,majflt,\n"
25" nswap,inblock,oublock,msgsnd,msgrcv,nsignals,nvcsw,nivcsw)\n"
26"or via the attributes ru_utime, ru_stime, ru_maxrss, and so on.");
Martin v. Löwis688357e2002-04-08 21:28:20 +000027
28static PyStructSequence_Field struct_rusage_fields[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 {"ru_utime", "user time used"},
30 {"ru_stime", "system time used"},
31 {"ru_maxrss", "max. resident set size"},
32 {"ru_ixrss", "shared memory size"},
33 {"ru_idrss", "unshared data size"},
34 {"ru_isrss", "unshared stack size"},
35 {"ru_minflt", "page faults not requiring I/O"},
36 {"ru_majflt", "page faults requiring I/O"},
37 {"ru_nswap", "number of swap outs"},
38 {"ru_inblock", "block input operations"},
39 {"ru_oublock", "block output operations"},
40 {"ru_msgsnd", "IPC messages sent"},
41 {"ru_msgrcv", "IPC messages received"},
42 {"ru_nsignals", "signals received"},
43 {"ru_nvcsw", "voluntary context switches"},
44 {"ru_nivcsw", "involuntary context switches"},
45 {0}
Martin v. Löwis688357e2002-04-08 21:28:20 +000046};
47
48static PyStructSequence_Desc struct_rusage_desc = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 "resource.struct_rusage", /* name */
50 struct_rusage__doc__, /* doc */
51 struct_rusage_fields, /* fields */
52 16 /* n_in_sequence */
Martin v. Löwis688357e2002-04-08 21:28:20 +000053};
54
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055static int initialized;
Martin v. Löwis688357e2002-04-08 21:28:20 +000056static PyTypeObject StructRUsageType;
57
Guido van Rossum2e631391996-12-18 18:37:27 +000058static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +000059resource_getrusage(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +000060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 int who;
62 struct rusage ru;
63 PyObject *result;
Guido van Rossum2e631391996-12-18 18:37:27 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (!PyArg_ParseTuple(args, "i:getrusage", &who))
66 return NULL;
Guido van Rossum2e631391996-12-18 18:37:27 +000067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (getrusage(who, &ru) == -1) {
69 if (errno == EINVAL) {
70 PyErr_SetString(PyExc_ValueError,
71 "invalid who parameter");
72 return NULL;
73 }
Benjamin Peterson2122cf72011-12-10 17:50:22 -050074 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 return NULL;
76 }
Guido van Rossum2e631391996-12-18 18:37:27 +000077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 result = PyStructSequence_New(&StructRUsageType);
79 if (!result)
80 return NULL;
Martin v. Löwis688357e2002-04-08 21:28:20 +000081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 PyStructSequence_SET_ITEM(result, 0,
83 PyFloat_FromDouble(doubletime(ru.ru_utime)));
84 PyStructSequence_SET_ITEM(result, 1,
85 PyFloat_FromDouble(doubletime(ru.ru_stime)));
86 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong(ru.ru_maxrss));
87 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong(ru.ru_ixrss));
88 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong(ru.ru_idrss));
89 PyStructSequence_SET_ITEM(result, 5, PyLong_FromLong(ru.ru_isrss));
90 PyStructSequence_SET_ITEM(result, 6, PyLong_FromLong(ru.ru_minflt));
91 PyStructSequence_SET_ITEM(result, 7, PyLong_FromLong(ru.ru_majflt));
92 PyStructSequence_SET_ITEM(result, 8, PyLong_FromLong(ru.ru_nswap));
93 PyStructSequence_SET_ITEM(result, 9, PyLong_FromLong(ru.ru_inblock));
94 PyStructSequence_SET_ITEM(result, 10, PyLong_FromLong(ru.ru_oublock));
95 PyStructSequence_SET_ITEM(result, 11, PyLong_FromLong(ru.ru_msgsnd));
96 PyStructSequence_SET_ITEM(result, 12, PyLong_FromLong(ru.ru_msgrcv));
97 PyStructSequence_SET_ITEM(result, 13, PyLong_FromLong(ru.ru_nsignals));
98 PyStructSequence_SET_ITEM(result, 14, PyLong_FromLong(ru.ru_nvcsw));
99 PyStructSequence_SET_ITEM(result, 15, PyLong_FromLong(ru.ru_nivcsw));
Martin v. Löwis688357e2002-04-08 21:28:20 +0000100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 if (PyErr_Occurred()) {
102 Py_DECREF(result);
103 return NULL;
104 }
Martin v. Löwis688357e2002-04-08 21:28:20 +0000105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 return result;
Guido van Rossum2e631391996-12-18 18:37:27 +0000107}
108
109
110static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000111resource_getrlimit(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +0000112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 struct rlimit rl;
114 int resource;
Guido van Rossum2e631391996-12-18 18:37:27 +0000115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
117 return NULL;
Guido van Rossum2e631391996-12-18 18:37:27 +0000118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 if (resource < 0 || resource >= RLIM_NLIMITS) {
120 PyErr_SetString(PyExc_ValueError,
121 "invalid resource specified");
122 return NULL;
123 }
Guido van Rossum2e631391996-12-18 18:37:27 +0000124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 if (getrlimit(resource, &rl) == -1) {
Benjamin Peterson2122cf72011-12-10 17:50:22 -0500126 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 return NULL;
128 }
Guido van Rossum763737b1999-01-06 18:44:57 +0000129
130#if defined(HAVE_LONG_LONG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 if (sizeof(rl.rlim_cur) > sizeof(long)) {
132 return Py_BuildValue("LL",
133 (PY_LONG_LONG) rl.rlim_cur,
134 (PY_LONG_LONG) rl.rlim_max);
135 }
Guido van Rossum763737b1999-01-06 18:44:57 +0000136#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
Guido van Rossum2e631391996-12-18 18:37:27 +0000138}
139
140static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000141resource_setrlimit(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +0000142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 struct rlimit rl;
144 int resource;
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200145 PyObject *limits, *curobj, *maxobj;
Guido van Rossum2e631391996-12-18 18:37:27 +0000146
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200147 if (!PyArg_ParseTuple(args, "iO:setrlimit", &resource, &limits))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 return NULL;
Guido van Rossum2e631391996-12-18 18:37:27 +0000149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 if (resource < 0 || resource >= RLIM_NLIMITS) {
151 PyErr_SetString(PyExc_ValueError,
152 "invalid resource specified");
153 return NULL;
154 }
Guido van Rossum2e631391996-12-18 18:37:27 +0000155
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200156 limits = PySequence_Tuple(limits);
157 if (!limits)
158 /* Here limits is a borrowed reference */
159 return NULL;
160
161 if (PyTuple_GET_SIZE(limits) != 2) {
162 PyErr_SetString(PyExc_ValueError,
163 "expected a tuple of 2 integers");
164 goto error;
165 }
166 curobj = PyTuple_GET_ITEM(limits, 0);
167 maxobj = PyTuple_GET_ITEM(limits, 1);
168
Guido van Rossum763737b1999-01-06 18:44:57 +0000169#if !defined(HAVE_LARGEFILE_SUPPORT)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 rl.rlim_cur = PyLong_AsLong(curobj);
171 if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200172 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 rl.rlim_max = PyLong_AsLong(maxobj);
174 if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200175 goto error;
Guido van Rossum763737b1999-01-06 18:44:57 +0000176#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 /* The limits are probably bigger than a long */
178 rl.rlim_cur = PyLong_AsLongLong(curobj);
179 if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200180 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 rl.rlim_max = PyLong_AsLongLong(maxobj);
182 if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200183 goto error;
Guido van Rossum763737b1999-01-06 18:44:57 +0000184#endif
185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
187 rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
188 if (setrlimit(resource, &rl) == -1) {
189 if (errno == EINVAL)
190 PyErr_SetString(PyExc_ValueError,
191 "current limit exceeds maximum limit");
192 else if (errno == EPERM)
193 PyErr_SetString(PyExc_ValueError,
194 "not allowed to raise maximum limit");
195 else
Benjamin Peterson2122cf72011-12-10 17:50:22 -0500196 PyErr_SetFromErrno(PyExc_OSError);
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200197 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 }
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200199 Py_DECREF(limits);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 Py_INCREF(Py_None);
201 return Py_None;
Serhiy Storchaka19c4e0d2013-02-04 12:47:24 +0200202
203 error:
204 Py_DECREF(limits);
205 return NULL;
Guido van Rossum2e631391996-12-18 18:37:27 +0000206}
207
208static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000209resource_getpagesize(PyObject *self, PyObject *unused)
Guido van Rossum2e631391996-12-18 18:37:27 +0000210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 long pagesize = 0;
Martin v. Löwisf26d63b2003-03-30 17:23:49 +0000212#if defined(HAVE_GETPAGESIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 pagesize = getpagesize();
Martin v. Löwisf26d63b2003-03-30 17:23:49 +0000214#elif defined(HAVE_SYSCONF)
Martin v. Löwis0cb3c632004-08-12 13:26:56 +0000215#if defined(_SC_PAGE_SIZE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 pagesize = sysconf(_SC_PAGE_SIZE);
Martin v. Löwis0cb3c632004-08-12 13:26:56 +0000217#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 /* Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE */
219 pagesize = sysconf(_SC_PAGESIZE);
Martin v. Löwis0cb3c632004-08-12 13:26:56 +0000220#endif
Martin v. Löwisf26d63b2003-03-30 17:23:49 +0000221#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 return Py_BuildValue("i", pagesize);
Martin v. Löwisf26d63b2003-03-30 17:23:49 +0000223
Guido van Rossum2e631391996-12-18 18:37:27 +0000224}
225
226/* List of functions */
227
228static struct PyMethodDef
229resource_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 {"getrusage", resource_getrusage, METH_VARARGS},
231 {"getrlimit", resource_getrlimit, METH_VARARGS},
232 {"setrlimit", resource_setrlimit, METH_VARARGS},
233 {"getpagesize", resource_getpagesize, METH_NOARGS},
234 {NULL, NULL} /* sentinel */
Guido van Rossum2e631391996-12-18 18:37:27 +0000235};
236
237
238/* Module initialization */
239
Martin v. Löwis1a214512008-06-11 05:26:20 +0000240
241static struct PyModuleDef resourcemodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 PyModuleDef_HEAD_INIT,
243 "resource",
244 NULL,
245 -1,
246 resource_methods,
247 NULL,
248 NULL,
249 NULL,
250 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000251};
252
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000253PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000254PyInit_resource(void)
Guido van Rossum2e631391996-12-18 18:37:27 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 PyObject *m, *v;
Guido van Rossum2e631391996-12-18 18:37:27 +0000257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 /* Create the module and add the functions */
259 m = PyModule_Create(&resourcemodule);
260 if (m == NULL)
261 return NULL;
Guido van Rossum2e631391996-12-18 18:37:27 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 /* Add some symbolic constants to the module */
Benjamin Peterson2122cf72011-12-10 17:50:22 -0500264 Py_INCREF(PyExc_OSError);
265 PyModule_AddObject(m, "error", PyExc_OSError);
Victor Stinner1c8f0592013-07-22 22:24:54 +0200266 if (!initialized) {
267 if (PyStructSequence_InitType2(&StructRUsageType,
268 &struct_rusage_desc) < 0)
269 return NULL;
270 }
271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 Py_INCREF(&StructRUsageType);
273 PyModule_AddObject(m, "struct_rusage",
274 (PyObject*) &StructRUsageType);
Guido van Rossum2e631391996-12-18 18:37:27 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 /* insert constants */
Guido van Rossum2e631391996-12-18 18:37:27 +0000277#ifdef RLIMIT_CPU
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200278 PyModule_AddIntMacro(m, RLIMIT_CPU);
Guido van Rossum2e631391996-12-18 18:37:27 +0000279#endif
280
281#ifdef RLIMIT_FSIZE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200282 PyModule_AddIntMacro(m, RLIMIT_FSIZE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000283#endif
284
285#ifdef RLIMIT_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200286 PyModule_AddIntMacro(m, RLIMIT_DATA);
Guido van Rossum2e631391996-12-18 18:37:27 +0000287#endif
288
289#ifdef RLIMIT_STACK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200290 PyModule_AddIntMacro(m, RLIMIT_STACK);
Guido van Rossum2e631391996-12-18 18:37:27 +0000291#endif
292
293#ifdef RLIMIT_CORE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200294 PyModule_AddIntMacro(m, RLIMIT_CORE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000295#endif
296
297#ifdef RLIMIT_NOFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200298 PyModule_AddIntMacro(m, RLIMIT_NOFILE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000299#endif
300
301#ifdef RLIMIT_OFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200302 PyModule_AddIntMacro(m, RLIMIT_OFILE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000303#endif
304
305#ifdef RLIMIT_VMEM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200306 PyModule_AddIntMacro(m, RLIMIT_VMEM);
Guido van Rossum2e631391996-12-18 18:37:27 +0000307#endif
308
309#ifdef RLIMIT_AS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200310 PyModule_AddIntMacro(m, RLIMIT_AS);
Guido van Rossum2e631391996-12-18 18:37:27 +0000311#endif
312
313#ifdef RLIMIT_RSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200314 PyModule_AddIntMacro(m, RLIMIT_RSS);
Guido van Rossum2e631391996-12-18 18:37:27 +0000315#endif
316
317#ifdef RLIMIT_NPROC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200318 PyModule_AddIntMacro(m, RLIMIT_NPROC);
Guido van Rossum2e631391996-12-18 18:37:27 +0000319#endif
320
321#ifdef RLIMIT_MEMLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200322 PyModule_AddIntMacro(m, RLIMIT_MEMLOCK);
Guido van Rossum2e631391996-12-18 18:37:27 +0000323#endif
324
Thomas Wouters89f507f2006-12-13 04:49:30 +0000325#ifdef RLIMIT_SBSIZE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200326 PyModule_AddIntMacro(m, RLIMIT_SBSIZE);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000327#endif
328
Guido van Rossum2e631391996-12-18 18:37:27 +0000329#ifdef RUSAGE_SELF
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200330 PyModule_AddIntMacro(m, RUSAGE_SELF);
Guido van Rossum2e631391996-12-18 18:37:27 +0000331#endif
332
Guido van Rossumebd05eb1997-12-09 19:35:48 +0000333#ifdef RUSAGE_CHILDREN
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200334 PyModule_AddIntMacro(m, RUSAGE_CHILDREN);
Guido van Rossum2e631391996-12-18 18:37:27 +0000335#endif
336
337#ifdef RUSAGE_BOTH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200338 PyModule_AddIntMacro(m, RUSAGE_BOTH);
Guido van Rossum2e631391996-12-18 18:37:27 +0000339#endif
Neil Schemenauer29ac3cb2002-03-24 22:27:39 +0000340
Antoine Pitroub6d4ee52010-11-17 16:19:35 +0000341#ifdef RUSAGE_THREAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +0200342 PyModule_AddIntMacro(m, RUSAGE_THREAD);
Antoine Pitroub6d4ee52010-11-17 16:19:35 +0000343#endif
344
Neil Schemenauer29ac3cb2002-03-24 22:27:39 +0000345#if defined(HAVE_LONG_LONG)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 if (sizeof(RLIM_INFINITY) > sizeof(long)) {
347 v = PyLong_FromLongLong((PY_LONG_LONG) RLIM_INFINITY);
348 } else
Neil Schemenauer29ac3cb2002-03-24 22:27:39 +0000349#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 {
351 v = PyLong_FromLong((long) RLIM_INFINITY);
352 }
353 if (v) {
354 PyModule_AddObject(m, "RLIM_INFINITY", v);
355 }
356 initialized = 1;
357 return m;
Guido van Rossum2e631391996-12-18 18:37:27 +0000358}