Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Modules / resource.c
blob8653f038802e0e84fe92ba6bb427538ad150cab9
2 #include "Python.h"
3 #include "structseq.h"
4 #include <sys/resource.h>
5 #include <sys/time.h>
6 #include <string.h>
7 #include <errno.h>
9 /* On some systems, these aren't in any header file.
10 On others they are, with inconsistent prototypes.
11 We declare the (default) return type, to shut up gcc -Wall;
12 but we can't declare the prototype, to avoid errors
13 when the header files declare it different.
14 Worse, on some Linuxes, getpagesize() returns a size_t... */
16 #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
18 static PyObject *ResourceError;
20 PyDoc_STRVAR(struct_rusage__doc__,
21 "struct_rusage: Result from getrusage.\n\n"
22 "This object may be accessed either as a tuple of\n"
23 " (utime,stime,maxrss,ixrss,idrss,isrss,minflt,majflt,\n"
24 " nswap,inblock,oublock,msgsnd,msgrcv,nsignals,nvcsw,nivcsw)\n"
25 "or via the attributes ru_utime, ru_stime, ru_maxrss, and so on.");
27 static PyStructSequence_Field struct_rusage_fields[] = {
28 {"ru_utime", "user time used"},
29 {"ru_stime", "system time used"},
30 {"ru_maxrss", "max. resident set size"},
31 {"ru_ixrss", "shared memory size"},
32 {"ru_idrss", "unshared data size"},
33 {"ru_isrss", "unshared stack size"},
34 {"ru_minflt", "page faults not requiring I/O"},
35 {"ru_majflt", "page faults requiring I/O"},
36 {"ru_nswap", "number of swap outs"},
37 {"ru_inblock", "block input operations"},
38 {"ru_oublock", "block output operations"},
39 {"ru_msgsnd", "IPC messages sent"},
40 {"ru_msgrcv", "IPC messages received"},
41 {"ru_nsignals", "signals received"},
42 {"ru_nvcsw", "voluntary context switches"},
43 {"ru_nivcsw", "involuntary context switches"},
44 {0}
47 static PyStructSequence_Desc struct_rusage_desc = {
48 "resource.struct_rusage", /* name */
49 struct_rusage__doc__, /* doc */
50 struct_rusage_fields, /* fields */
51 16 /* n_in_sequence */
54 static PyTypeObject StructRUsageType;
56 static PyObject *
57 resource_getrusage(PyObject *self, PyObject *args)
59 int who;
60 struct rusage ru;
61 PyObject *result;
63 if (!PyArg_ParseTuple(args, "i:getrusage", &who))
64 return NULL;
66 if (getrusage(who, &ru) == -1) {
67 if (errno == EINVAL) {
68 PyErr_SetString(PyExc_ValueError,
69 "invalid who parameter");
70 return NULL;
72 PyErr_SetFromErrno(ResourceError);
73 return NULL;
76 result = PyStructSequence_New(&StructRUsageType);
77 if (!result)
78 return NULL;
80 PyStructSequence_SET_ITEM(result, 0,
81 PyFloat_FromDouble(doubletime(ru.ru_utime)));
82 PyStructSequence_SET_ITEM(result, 1,
83 PyFloat_FromDouble(doubletime(ru.ru_stime)));
84 PyStructSequence_SET_ITEM(result, 2, PyInt_FromLong(ru.ru_maxrss));
85 PyStructSequence_SET_ITEM(result, 3, PyInt_FromLong(ru.ru_ixrss));
86 PyStructSequence_SET_ITEM(result, 4, PyInt_FromLong(ru.ru_idrss));
87 PyStructSequence_SET_ITEM(result, 5, PyInt_FromLong(ru.ru_isrss));
88 PyStructSequence_SET_ITEM(result, 6, PyInt_FromLong(ru.ru_minflt));
89 PyStructSequence_SET_ITEM(result, 7, PyInt_FromLong(ru.ru_majflt));
90 PyStructSequence_SET_ITEM(result, 8, PyInt_FromLong(ru.ru_nswap));
91 PyStructSequence_SET_ITEM(result, 9, PyInt_FromLong(ru.ru_inblock));
92 PyStructSequence_SET_ITEM(result, 10, PyInt_FromLong(ru.ru_oublock));
93 PyStructSequence_SET_ITEM(result, 11, PyInt_FromLong(ru.ru_msgsnd));
94 PyStructSequence_SET_ITEM(result, 12, PyInt_FromLong(ru.ru_msgrcv));
95 PyStructSequence_SET_ITEM(result, 13, PyInt_FromLong(ru.ru_nsignals));
96 PyStructSequence_SET_ITEM(result, 14, PyInt_FromLong(ru.ru_nvcsw));
97 PyStructSequence_SET_ITEM(result, 15, PyInt_FromLong(ru.ru_nivcsw));
99 if (PyErr_Occurred()) {
100 Py_DECREF(result);
101 return NULL;
104 return result;
108 static PyObject *
109 resource_getrlimit(PyObject *self, PyObject *args)
111 struct rlimit rl;
112 int resource;
114 if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
115 return NULL;
117 if (resource < 0 || resource >= RLIM_NLIMITS) {
118 PyErr_SetString(PyExc_ValueError,
119 "invalid resource specified");
120 return NULL;
123 if (getrlimit(resource, &rl) == -1) {
124 PyErr_SetFromErrno(ResourceError);
125 return NULL;
128 #if defined(HAVE_LONG_LONG)
129 if (sizeof(rl.rlim_cur) > sizeof(long)) {
130 return Py_BuildValue("LL",
131 (LONG_LONG) rl.rlim_cur,
132 (LONG_LONG) rl.rlim_max);
134 #endif
135 return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
138 static PyObject *
139 resource_setrlimit(PyObject *self, PyObject *args)
141 struct rlimit rl;
142 int resource;
143 PyObject *curobj, *maxobj;
145 if (!PyArg_ParseTuple(args, "i(OO):setrlimit",
146 &resource, &curobj, &maxobj))
147 return NULL;
149 if (resource < 0 || resource >= RLIM_NLIMITS) {
150 PyErr_SetString(PyExc_ValueError,
151 "invalid resource specified");
152 return NULL;
155 #if !defined(HAVE_LARGEFILE_SUPPORT)
156 rl.rlim_cur = PyInt_AsLong(curobj);
157 if (rl.rlim_cur == -1 && PyErr_Occurred())
158 return NULL;
159 rl.rlim_max = PyInt_AsLong(maxobj);
160 if (rl.rlim_max == -1 && PyErr_Occurred())
161 return NULL;
162 #else
163 /* The limits are probably bigger than a long */
164 rl.rlim_cur = PyLong_Check(curobj) ?
165 PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
166 if (rl.rlim_cur == -1 && PyErr_Occurred())
167 return NULL;
168 rl.rlim_max = PyLong_Check(maxobj) ?
169 PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
170 if (rl.rlim_max == -1 && PyErr_Occurred())
171 return NULL;
172 #endif
174 rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
175 rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
176 if (setrlimit(resource, &rl) == -1) {
177 if (errno == EINVAL)
178 PyErr_SetString(PyExc_ValueError,
179 "current limit exceeds maximum limit");
180 else if (errno == EPERM)
181 PyErr_SetString(PyExc_ValueError,
182 "not allowed to raise maximum limit");
183 else
184 PyErr_SetFromErrno(ResourceError);
185 return NULL;
187 Py_INCREF(Py_None);
188 return Py_None;
191 static PyObject *
192 resource_getpagesize(PyObject *self, PyObject *args)
194 if (!PyArg_ParseTuple(args, ":getpagesize"))
195 return NULL;
196 return Py_BuildValue("i", getpagesize());
199 /* List of functions */
201 static struct PyMethodDef
202 resource_methods[] = {
203 {"getrusage", resource_getrusage, METH_VARARGS},
204 {"getrlimit", resource_getrlimit, METH_VARARGS},
205 {"setrlimit", resource_setrlimit, METH_VARARGS},
206 {"getpagesize", resource_getpagesize, METH_VARARGS},
207 {NULL, NULL} /* sentinel */
211 /* Module initialization */
213 PyMODINIT_FUNC
214 initresource(void)
216 PyObject *m, *v;
218 /* Create the module and add the functions */
219 m = Py_InitModule("resource", resource_methods);
221 /* Add some symbolic constants to the module */
222 if (ResourceError == NULL) {
223 ResourceError = PyErr_NewException("resource.error",
224 NULL, NULL);
226 Py_INCREF(ResourceError);
227 PyModule_AddObject(m, "error", ResourceError);
228 PyStructSequence_InitType(&StructRUsageType, &struct_rusage_desc);
229 PyModule_AddObject(m, "struct_rusage",
230 (PyObject*) &StructRUsageType);
232 /* insert constants */
233 #ifdef RLIMIT_CPU
234 PyModule_AddIntConstant(m, "RLIMIT_CPU", RLIMIT_CPU);
235 #endif
237 #ifdef RLIMIT_FSIZE
238 PyModule_AddIntConstant(m, "RLIMIT_FSIZE", RLIMIT_FSIZE);
239 #endif
241 #ifdef RLIMIT_DATA
242 PyModule_AddIntConstant(m, "RLIMIT_DATA", RLIMIT_DATA);
243 #endif
245 #ifdef RLIMIT_STACK
246 PyModule_AddIntConstant(m, "RLIMIT_STACK", RLIMIT_STACK);
247 #endif
249 #ifdef RLIMIT_CORE
250 PyModule_AddIntConstant(m, "RLIMIT_CORE", RLIMIT_CORE);
251 #endif
253 #ifdef RLIMIT_NOFILE
254 PyModule_AddIntConstant(m, "RLIMIT_NOFILE", RLIMIT_NOFILE);
255 #endif
257 #ifdef RLIMIT_OFILE
258 PyModule_AddIntConstant(m, "RLIMIT_OFILE", RLIMIT_OFILE);
259 #endif
261 #ifdef RLIMIT_VMEM
262 PyModule_AddIntConstant(m, "RLIMIT_VMEM", RLIMIT_VMEM);
263 #endif
265 #ifdef RLIMIT_AS
266 PyModule_AddIntConstant(m, "RLIMIT_AS", RLIMIT_AS);
267 #endif
269 #ifdef RLIMIT_RSS
270 PyModule_AddIntConstant(m, "RLIMIT_RSS", RLIMIT_RSS);
271 #endif
273 #ifdef RLIMIT_NPROC
274 PyModule_AddIntConstant(m, "RLIMIT_NPROC", RLIMIT_NPROC);
275 #endif
277 #ifdef RLIMIT_MEMLOCK
278 PyModule_AddIntConstant(m, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
279 #endif
281 #ifdef RUSAGE_SELF
282 PyModule_AddIntConstant(m, "RUSAGE_SELF", RUSAGE_SELF);
283 #endif
285 #ifdef RUSAGE_CHILDREN
286 PyModule_AddIntConstant(m, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
287 #endif
289 #ifdef RUSAGE_BOTH
290 PyModule_AddIntConstant(m, "RUSAGE_BOTH", RUSAGE_BOTH);
291 #endif
293 #if defined(HAVE_LONG_LONG)
294 if (sizeof(RLIM_INFINITY) > sizeof(long)) {
295 v = PyLong_FromLongLong((LONG_LONG) RLIM_INFINITY);
296 } else
297 #endif
299 v = PyInt_FromLong((long) RLIM_INFINITY);
301 if (v) {
302 PyModule_AddObject(m, "RLIM_INFINITY", v);