blob: c1b637758dced4f0a63edb781d6d90a565bd033b [file] [log] [blame]
Guido van Rossumf84a5391997-08-15 00:04:24 +00001
Guido van Rossum2e631391996-12-18 18:37:27 +00002#include "Python.h"
Martin v. Löwis688357e2002-04-08 21:28:20 +00003#include "structseq.h"
Guido van Rossum2e631391996-12-18 18:37:27 +00004#include <sys/resource.h>
5#include <sys/time.h>
Guido van Rossum2e631391996-12-18 18:37:27 +00006#include <string.h>
7#include <errno.h>
Martin v. Löwisf26d63b2003-03-30 17:23:49 +00008/* for sysconf */
9#if defined(HAVE_UNISTD_H)
10#include <unistd.h>
11#endif
Guido van Rossum2e631391996-12-18 18:37:27 +000012
Guido van Rossum607b33a1997-08-17 16:24:30 +000013/* On some systems, these aren't in any header file.
14 On others they are, with inconsistent prototypes.
15 We declare the (default) return type, to shut up gcc -Wall;
16 but we can't declare the prototype, to avoid errors
17 when the header files declare it different.
18 Worse, on some Linuxes, getpagesize() returns a size_t... */
Guido van Rossum2e631391996-12-18 18:37:27 +000019
20#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
21
22static PyObject *ResourceError;
23
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000024PyDoc_STRVAR(struct_rusage__doc__,
25"struct_rusage: Result from getrusage.\n\n"
26"This object may be accessed either as a tuple of\n"
27" (utime,stime,maxrss,ixrss,idrss,isrss,minflt,majflt,\n"
28" nswap,inblock,oublock,msgsnd,msgrcv,nsignals,nvcsw,nivcsw)\n"
29"or via the attributes ru_utime, ru_stime, ru_maxrss, and so on.");
Martin v. Löwis688357e2002-04-08 21:28:20 +000030
31static PyStructSequence_Field struct_rusage_fields[] = {
32 {"ru_utime", "user time used"},
33 {"ru_stime", "system time used"},
34 {"ru_maxrss", "max. resident set size"},
35 {"ru_ixrss", "shared memory size"},
36 {"ru_idrss", "unshared data size"},
37 {"ru_isrss", "unshared stack size"},
38 {"ru_minflt", "page faults not requiring I/O"},
39 {"ru_majflt", "page faults requiring I/O"},
40 {"ru_nswap", "number of swap outs"},
41 {"ru_inblock", "block input operations"},
42 {"ru_oublock", "block output operations"},
43 {"ru_msgsnd", "IPC messages sent"},
44 {"ru_msgrcv", "IPC messages received"},
45 {"ru_nsignals", "signals received"},
46 {"ru_nvcsw", "voluntary context switches"},
47 {"ru_nivcsw", "involuntary context switches"},
48 {0}
49};
50
51static PyStructSequence_Desc struct_rusage_desc = {
52 "resource.struct_rusage", /* name */
53 struct_rusage__doc__, /* doc */
54 struct_rusage_fields, /* fields */
55 16 /* n_in_sequence */
56};
57
58static PyTypeObject StructRUsageType;
59
Guido van Rossum2e631391996-12-18 18:37:27 +000060static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +000061resource_getrusage(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +000062{
63 int who;
64 struct rusage ru;
Martin v. Löwis688357e2002-04-08 21:28:20 +000065 PyObject *result;
Guido van Rossum2e631391996-12-18 18:37:27 +000066
Guido van Rossum43713e52000-02-29 13:59:29 +000067 if (!PyArg_ParseTuple(args, "i:getrusage", &who))
Guido van Rossum2e631391996-12-18 18:37:27 +000068 return NULL;
69
70 if (getrusage(who, &ru) == -1) {
71 if (errno == EINVAL) {
72 PyErr_SetString(PyExc_ValueError,
73 "invalid who parameter");
74 return NULL;
75 }
76 PyErr_SetFromErrno(ResourceError);
77 return NULL;
78 }
79
Martin v. Löwis688357e2002-04-08 21:28:20 +000080 result = PyStructSequence_New(&StructRUsageType);
81 if (!result)
82 return NULL;
83
84 PyStructSequence_SET_ITEM(result, 0,
85 PyFloat_FromDouble(doubletime(ru.ru_utime)));
86 PyStructSequence_SET_ITEM(result, 1,
87 PyFloat_FromDouble(doubletime(ru.ru_stime)));
88 PyStructSequence_SET_ITEM(result, 2, PyInt_FromLong(ru.ru_maxrss));
89 PyStructSequence_SET_ITEM(result, 3, PyInt_FromLong(ru.ru_ixrss));
90 PyStructSequence_SET_ITEM(result, 4, PyInt_FromLong(ru.ru_idrss));
91 PyStructSequence_SET_ITEM(result, 5, PyInt_FromLong(ru.ru_isrss));
92 PyStructSequence_SET_ITEM(result, 6, PyInt_FromLong(ru.ru_minflt));
93 PyStructSequence_SET_ITEM(result, 7, PyInt_FromLong(ru.ru_majflt));
94 PyStructSequence_SET_ITEM(result, 8, PyInt_FromLong(ru.ru_nswap));
95 PyStructSequence_SET_ITEM(result, 9, PyInt_FromLong(ru.ru_inblock));
96 PyStructSequence_SET_ITEM(result, 10, PyInt_FromLong(ru.ru_oublock));
97 PyStructSequence_SET_ITEM(result, 11, PyInt_FromLong(ru.ru_msgsnd));
98 PyStructSequence_SET_ITEM(result, 12, PyInt_FromLong(ru.ru_msgrcv));
99 PyStructSequence_SET_ITEM(result, 13, PyInt_FromLong(ru.ru_nsignals));
100 PyStructSequence_SET_ITEM(result, 14, PyInt_FromLong(ru.ru_nvcsw));
101 PyStructSequence_SET_ITEM(result, 15, PyInt_FromLong(ru.ru_nivcsw));
102
103 if (PyErr_Occurred()) {
104 Py_DECREF(result);
105 return NULL;
106 }
107
108 return result;
Guido van Rossum2e631391996-12-18 18:37:27 +0000109}
110
111
112static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000113resource_getrlimit(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +0000114{
115 struct rlimit rl;
116 int resource;
Guido van Rossum2e631391996-12-18 18:37:27 +0000117
Guido van Rossum43713e52000-02-29 13:59:29 +0000118 if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
Guido van Rossum2e631391996-12-18 18:37:27 +0000119 return NULL;
120
121 if (resource < 0 || resource >= RLIM_NLIMITS) {
122 PyErr_SetString(PyExc_ValueError,
123 "invalid resource specified");
124 return NULL;
125 }
126
127 if (getrlimit(resource, &rl) == -1) {
128 PyErr_SetFromErrno(ResourceError);
129 return NULL;
130 }
Guido van Rossum763737b1999-01-06 18:44:57 +0000131
132#if defined(HAVE_LONG_LONG)
133 if (sizeof(rl.rlim_cur) > sizeof(long)) {
134 return Py_BuildValue("LL",
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000135 (PY_LONG_LONG) rl.rlim_cur,
136 (PY_LONG_LONG) rl.rlim_max);
Guido van Rossum763737b1999-01-06 18:44:57 +0000137 }
138#endif
Neal Norwitz94a83fd2002-11-02 17:46:24 +0000139 return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
Guido van Rossum2e631391996-12-18 18:37:27 +0000140}
141
142static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000143resource_setrlimit(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +0000144{
145 struct rlimit rl;
146 int resource;
Guido van Rossum763737b1999-01-06 18:44:57 +0000147 PyObject *curobj, *maxobj;
Guido van Rossum2e631391996-12-18 18:37:27 +0000148
Jeremy Hyltond95efe42002-04-23 20:15:04 +0000149 if (!PyArg_ParseTuple(args, "i(OO):setrlimit",
150 &resource, &curobj, &maxobj))
Guido van Rossum2e631391996-12-18 18:37:27 +0000151 return NULL;
152
153 if (resource < 0 || resource >= RLIM_NLIMITS) {
154 PyErr_SetString(PyExc_ValueError,
155 "invalid resource specified");
156 return NULL;
157 }
158
Guido van Rossum763737b1999-01-06 18:44:57 +0000159#if !defined(HAVE_LARGEFILE_SUPPORT)
160 rl.rlim_cur = PyInt_AsLong(curobj);
Jeremy Hyltond95efe42002-04-23 20:15:04 +0000161 if (rl.rlim_cur == -1 && PyErr_Occurred())
162 return NULL;
Guido van Rossum763737b1999-01-06 18:44:57 +0000163 rl.rlim_max = PyInt_AsLong(maxobj);
Jeremy Hyltond95efe42002-04-23 20:15:04 +0000164 if (rl.rlim_max == -1 && PyErr_Occurred())
165 return NULL;
Guido van Rossum763737b1999-01-06 18:44:57 +0000166#else
167 /* The limits are probably bigger than a long */
168 rl.rlim_cur = PyLong_Check(curobj) ?
169 PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
Jeremy Hyltond95efe42002-04-23 20:15:04 +0000170 if (rl.rlim_cur == -1 && PyErr_Occurred())
171 return NULL;
Guido van Rossum763737b1999-01-06 18:44:57 +0000172 rl.rlim_max = PyLong_Check(maxobj) ?
173 PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
Jeremy Hyltond95efe42002-04-23 20:15:04 +0000174 if (rl.rlim_max == -1 && PyErr_Occurred())
175 return NULL;
Guido van Rossum763737b1999-01-06 18:44:57 +0000176#endif
177
Guido van Rossum2e631391996-12-18 18:37:27 +0000178 rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
179 rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
180 if (setrlimit(resource, &rl) == -1) {
181 if (errno == EINVAL)
182 PyErr_SetString(PyExc_ValueError,
183 "current limit exceeds maximum limit");
184 else if (errno == EPERM)
185 PyErr_SetString(PyExc_ValueError,
186 "not allowed to raise maximum limit");
187 else
188 PyErr_SetFromErrno(ResourceError);
189 return NULL;
190 }
191 Py_INCREF(Py_None);
192 return Py_None;
193}
194
195static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000196resource_getpagesize(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +0000197{
Neal Norwitz1ac3e392003-03-30 20:51:29 +0000198 long pagesize = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000199 if (!PyArg_ParseTuple(args, ":getpagesize"))
Guido van Rossum2e631391996-12-18 18:37:27 +0000200 return NULL;
Martin v. Löwisf26d63b2003-03-30 17:23:49 +0000201
Martin v. Löwisf26d63b2003-03-30 17:23:49 +0000202#if defined(HAVE_GETPAGESIZE)
203 pagesize = getpagesize();
204#elif defined(HAVE_SYSCONF)
205 pagesize = sysconf(_SC_PAGE_SIZE);
206#endif
207 return Py_BuildValue("i", pagesize);
208
Guido van Rossum2e631391996-12-18 18:37:27 +0000209}
210
211/* List of functions */
212
213static struct PyMethodDef
214resource_methods[] = {
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000215 {"getrusage", resource_getrusage, METH_VARARGS},
216 {"getrlimit", resource_getrlimit, METH_VARARGS},
217 {"setrlimit", resource_setrlimit, METH_VARARGS},
218 {"getpagesize", resource_getpagesize, METH_VARARGS},
Guido van Rossum2e631391996-12-18 18:37:27 +0000219 {NULL, NULL} /* sentinel */
220};
221
222
223/* Module initialization */
224
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000225PyMODINIT_FUNC
Andrew M. Kuchlingfda3c3d2001-09-17 16:19:16 +0000226initresource(void)
Guido van Rossum2e631391996-12-18 18:37:27 +0000227{
Neil Schemenauer29ac3cb2002-03-24 22:27:39 +0000228 PyObject *m, *v;
Guido van Rossum2e631391996-12-18 18:37:27 +0000229
230 /* Create the module and add the functions */
231 m = Py_InitModule("resource", resource_methods);
232
233 /* Add some symbolic constants to the module */
Fred Drakecca657b2002-02-14 06:59:26 +0000234 if (ResourceError == NULL) {
235 ResourceError = PyErr_NewException("resource.error",
236 NULL, NULL);
237 }
238 Py_INCREF(ResourceError);
239 PyModule_AddObject(m, "error", ResourceError);
Martin v. Löwis688357e2002-04-08 21:28:20 +0000240 PyStructSequence_InitType(&StructRUsageType, &struct_rusage_desc);
241 PyModule_AddObject(m, "struct_rusage",
242 (PyObject*) &StructRUsageType);
Guido van Rossum2e631391996-12-18 18:37:27 +0000243
244 /* insert constants */
245#ifdef RLIMIT_CPU
Fred Drakecca657b2002-02-14 06:59:26 +0000246 PyModule_AddIntConstant(m, "RLIMIT_CPU", RLIMIT_CPU);
Guido van Rossum2e631391996-12-18 18:37:27 +0000247#endif
248
249#ifdef RLIMIT_FSIZE
Fred Drakecca657b2002-02-14 06:59:26 +0000250 PyModule_AddIntConstant(m, "RLIMIT_FSIZE", RLIMIT_FSIZE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000251#endif
252
253#ifdef RLIMIT_DATA
Fred Drakecca657b2002-02-14 06:59:26 +0000254 PyModule_AddIntConstant(m, "RLIMIT_DATA", RLIMIT_DATA);
Guido van Rossum2e631391996-12-18 18:37:27 +0000255#endif
256
257#ifdef RLIMIT_STACK
Fred Drakecca657b2002-02-14 06:59:26 +0000258 PyModule_AddIntConstant(m, "RLIMIT_STACK", RLIMIT_STACK);
Guido van Rossum2e631391996-12-18 18:37:27 +0000259#endif
260
261#ifdef RLIMIT_CORE
Fred Drakecca657b2002-02-14 06:59:26 +0000262 PyModule_AddIntConstant(m, "RLIMIT_CORE", RLIMIT_CORE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000263#endif
264
265#ifdef RLIMIT_NOFILE
Fred Drakecca657b2002-02-14 06:59:26 +0000266 PyModule_AddIntConstant(m, "RLIMIT_NOFILE", RLIMIT_NOFILE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000267#endif
268
269#ifdef RLIMIT_OFILE
Fred Drakecca657b2002-02-14 06:59:26 +0000270 PyModule_AddIntConstant(m, "RLIMIT_OFILE", RLIMIT_OFILE);
Guido van Rossum2e631391996-12-18 18:37:27 +0000271#endif
272
273#ifdef RLIMIT_VMEM
Fred Drakecca657b2002-02-14 06:59:26 +0000274 PyModule_AddIntConstant(m, "RLIMIT_VMEM", RLIMIT_VMEM);
Guido van Rossum2e631391996-12-18 18:37:27 +0000275#endif
276
277#ifdef RLIMIT_AS
Fred Drakecca657b2002-02-14 06:59:26 +0000278 PyModule_AddIntConstant(m, "RLIMIT_AS", RLIMIT_AS);
Guido van Rossum2e631391996-12-18 18:37:27 +0000279#endif
280
281#ifdef RLIMIT_RSS
Fred Drakecca657b2002-02-14 06:59:26 +0000282 PyModule_AddIntConstant(m, "RLIMIT_RSS", RLIMIT_RSS);
Guido van Rossum2e631391996-12-18 18:37:27 +0000283#endif
284
285#ifdef RLIMIT_NPROC
Fred Drakecca657b2002-02-14 06:59:26 +0000286 PyModule_AddIntConstant(m, "RLIMIT_NPROC", RLIMIT_NPROC);
Guido van Rossum2e631391996-12-18 18:37:27 +0000287#endif
288
289#ifdef RLIMIT_MEMLOCK
Fred Drakecca657b2002-02-14 06:59:26 +0000290 PyModule_AddIntConstant(m, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
Guido van Rossum2e631391996-12-18 18:37:27 +0000291#endif
292
293#ifdef RUSAGE_SELF
Fred Drakecca657b2002-02-14 06:59:26 +0000294 PyModule_AddIntConstant(m, "RUSAGE_SELF", RUSAGE_SELF);
Guido van Rossum2e631391996-12-18 18:37:27 +0000295#endif
296
Guido van Rossumebd05eb1997-12-09 19:35:48 +0000297#ifdef RUSAGE_CHILDREN
Fred Drakecca657b2002-02-14 06:59:26 +0000298 PyModule_AddIntConstant(m, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
Guido van Rossum2e631391996-12-18 18:37:27 +0000299#endif
300
301#ifdef RUSAGE_BOTH
Fred Drakecca657b2002-02-14 06:59:26 +0000302 PyModule_AddIntConstant(m, "RUSAGE_BOTH", RUSAGE_BOTH);
Guido van Rossum2e631391996-12-18 18:37:27 +0000303#endif
Neil Schemenauer29ac3cb2002-03-24 22:27:39 +0000304
305#if defined(HAVE_LONG_LONG)
306 if (sizeof(RLIM_INFINITY) > sizeof(long)) {
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000307 v = PyLong_FromLongLong((PY_LONG_LONG) RLIM_INFINITY);
Neil Schemenauer29ac3cb2002-03-24 22:27:39 +0000308 } else
309#endif
310 {
311 v = PyInt_FromLong((long) RLIM_INFINITY);
312 }
313 if (v) {
314 PyModule_AddObject(m, "RLIM_INFINITY", v);
315 }
Guido van Rossum2e631391996-12-18 18:37:27 +0000316}