source: python/trunk/Modules/resource.c@ 20

Last change on this file since 20 was 10, checked in by Yuri Dario, 15 years ago

python: merged offline changes.

  • Property svn:eol-style set to native
File size: 8.6 KB
Line 
1
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>
8/* for sysconf */
9#if defined(HAVE_UNISTD_H)
10#include <unistd.h>
11#endif
12
13/* 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... */
19
20#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
21
22static PyObject *ResourceError;
23
24PyDoc_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.");
30
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 int initialized;
59static PyTypeObject StructRUsageType;
60
61static PyObject *
62resource_getrusage(PyObject *self, PyObject *args)
63{
64 int who;
65 struct rusage ru;
66 PyObject *result;
67
68 if (!PyArg_ParseTuple(args, "i:getrusage", &who))
69 return NULL;
70
71#ifndef __OS2__
72 if (getrusage(who, &ru) == -1) {
73 if (errno == EINVAL) {
74 PyErr_SetString(PyExc_ValueError,
75 "invalid who parameter");
76 return NULL;
77 }
78 PyErr_SetFromErrno(ResourceError);
79 return NULL;
80 }
81#endif
82 result = PyStructSequence_New(&StructRUsageType);
83 if (!result)
84 return NULL;
85
86 PyStructSequence_SET_ITEM(result, 0,
87 PyFloat_FromDouble(doubletime(ru.ru_utime)));
88 PyStructSequence_SET_ITEM(result, 1,
89 PyFloat_FromDouble(doubletime(ru.ru_stime)));
90 PyStructSequence_SET_ITEM(result, 2, PyInt_FromLong(ru.ru_maxrss));
91 PyStructSequence_SET_ITEM(result, 3, PyInt_FromLong(ru.ru_ixrss));
92 PyStructSequence_SET_ITEM(result, 4, PyInt_FromLong(ru.ru_idrss));
93 PyStructSequence_SET_ITEM(result, 5, PyInt_FromLong(ru.ru_isrss));
94 PyStructSequence_SET_ITEM(result, 6, PyInt_FromLong(ru.ru_minflt));
95 PyStructSequence_SET_ITEM(result, 7, PyInt_FromLong(ru.ru_majflt));
96 PyStructSequence_SET_ITEM(result, 8, PyInt_FromLong(ru.ru_nswap));
97 PyStructSequence_SET_ITEM(result, 9, PyInt_FromLong(ru.ru_inblock));
98 PyStructSequence_SET_ITEM(result, 10, PyInt_FromLong(ru.ru_oublock));
99 PyStructSequence_SET_ITEM(result, 11, PyInt_FromLong(ru.ru_msgsnd));
100 PyStructSequence_SET_ITEM(result, 12, PyInt_FromLong(ru.ru_msgrcv));
101 PyStructSequence_SET_ITEM(result, 13, PyInt_FromLong(ru.ru_nsignals));
102 PyStructSequence_SET_ITEM(result, 14, PyInt_FromLong(ru.ru_nvcsw));
103 PyStructSequence_SET_ITEM(result, 15, PyInt_FromLong(ru.ru_nivcsw));
104
105 if (PyErr_Occurred()) {
106 Py_DECREF(result);
107 return NULL;
108 }
109
110 return result;
111}
112
113
114static PyObject *
115resource_getrlimit(PyObject *self, PyObject *args)
116{
117 struct rlimit rl;
118 int resource;
119
120 if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
121 return NULL;
122
123 if (resource < 0 || resource >= RLIM_NLIMITS) {
124 PyErr_SetString(PyExc_ValueError,
125 "invalid resource specified");
126 return NULL;
127 }
128
129 if (getrlimit(resource, &rl) == -1) {
130 PyErr_SetFromErrno(ResourceError);
131 return NULL;
132 }
133
134#if defined(HAVE_LONG_LONG)
135 if (sizeof(rl.rlim_cur) > sizeof(long)) {
136 return Py_BuildValue("LL",
137 (PY_LONG_LONG) rl.rlim_cur,
138 (PY_LONG_LONG) rl.rlim_max);
139 }
140#endif
141 return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
142}
143
144static PyObject *
145resource_setrlimit(PyObject *self, PyObject *args)
146{
147 struct rlimit rl;
148 int resource;
149 PyObject *curobj, *maxobj;
150
151 if (!PyArg_ParseTuple(args, "i(OO):setrlimit",
152 &resource, &curobj, &maxobj))
153 return NULL;
154
155 if (resource < 0 || resource >= RLIM_NLIMITS) {
156 PyErr_SetString(PyExc_ValueError,
157 "invalid resource specified");
158 return NULL;
159 }
160
161#if !defined(HAVE_LARGEFILE_SUPPORT)
162 rl.rlim_cur = PyInt_AsLong(curobj);
163 if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
164 return NULL;
165 rl.rlim_max = PyInt_AsLong(maxobj);
166 if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
167 return NULL;
168#else
169 /* The limits are probably bigger than a long */
170 rl.rlim_cur = PyLong_Check(curobj) ?
171 PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
172 if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
173 return NULL;
174 rl.rlim_max = PyLong_Check(maxobj) ?
175 PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
176 if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
177 return NULL;
178#endif
179
180 rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
181 rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
182 if (setrlimit(resource, &rl) == -1) {
183 if (errno == EINVAL)
184 PyErr_SetString(PyExc_ValueError,
185 "current limit exceeds maximum limit");
186 else if (errno == EPERM)
187 PyErr_SetString(PyExc_ValueError,
188 "not allowed to raise maximum limit");
189 else
190 PyErr_SetFromErrno(ResourceError);
191 return NULL;
192 }
193 Py_INCREF(Py_None);
194 return Py_None;
195}
196
197static PyObject *
198resource_getpagesize(PyObject *self, PyObject *unused)
199{
200 long pagesize = 0;
201#if defined(HAVE_GETPAGESIZE)
202 pagesize = getpagesize();
203#elif defined(HAVE_SYSCONF)
204#if defined(_SC_PAGE_SIZE)
205 pagesize = sysconf(_SC_PAGE_SIZE);
206#else
207 /* Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE */
208 pagesize = sysconf(_SC_PAGESIZE);
209#endif
210#endif
211 return Py_BuildValue("i", pagesize);
212
213}
214
215/* List of functions */
216
217static struct PyMethodDef
218resource_methods[] = {
219 {"getrusage", resource_getrusage, METH_VARARGS},
220 {"getrlimit", resource_getrlimit, METH_VARARGS},
221 {"setrlimit", resource_setrlimit, METH_VARARGS},
222 {"getpagesize", resource_getpagesize, METH_NOARGS},
223 {NULL, NULL} /* sentinel */
224};
225
226
227/* Module initialization */
228
229PyMODINIT_FUNC
230initresource(void)
231{
232 PyObject *m, *v;
233
234 /* Create the module and add the functions */
235 m = Py_InitModule("resource", resource_methods);
236 if (m == NULL)
237 return;
238
239 /* Add some symbolic constants to the module */
240 if (ResourceError == NULL) {
241 ResourceError = PyErr_NewException("resource.error",
242 NULL, NULL);
243 }
244 Py_INCREF(ResourceError);
245 PyModule_AddObject(m, "error", ResourceError);
246 if (!initialized)
247 PyStructSequence_InitType(&StructRUsageType,
248 &struct_rusage_desc);
249 Py_INCREF(&StructRUsageType);
250 PyModule_AddObject(m, "struct_rusage",
251 (PyObject*) &StructRUsageType);
252
253 /* insert constants */
254#ifdef RLIMIT_CPU
255 PyModule_AddIntConstant(m, "RLIMIT_CPU", RLIMIT_CPU);
256#endif
257
258#ifdef RLIMIT_FSIZE
259 PyModule_AddIntConstant(m, "RLIMIT_FSIZE", RLIMIT_FSIZE);
260#endif
261
262#ifdef RLIMIT_DATA
263 PyModule_AddIntConstant(m, "RLIMIT_DATA", RLIMIT_DATA);
264#endif
265
266#ifdef RLIMIT_STACK
267 PyModule_AddIntConstant(m, "RLIMIT_STACK", RLIMIT_STACK);
268#endif
269
270#ifdef RLIMIT_CORE
271 PyModule_AddIntConstant(m, "RLIMIT_CORE", RLIMIT_CORE);
272#endif
273
274#ifdef RLIMIT_NOFILE
275 PyModule_AddIntConstant(m, "RLIMIT_NOFILE", RLIMIT_NOFILE);
276#endif
277
278#ifdef RLIMIT_OFILE
279 PyModule_AddIntConstant(m, "RLIMIT_OFILE", RLIMIT_OFILE);
280#endif
281
282#ifdef RLIMIT_VMEM
283 PyModule_AddIntConstant(m, "RLIMIT_VMEM", RLIMIT_VMEM);
284#endif
285
286#ifdef RLIMIT_AS
287 PyModule_AddIntConstant(m, "RLIMIT_AS", RLIMIT_AS);
288#endif
289
290#ifdef RLIMIT_RSS
291 PyModule_AddIntConstant(m, "RLIMIT_RSS", RLIMIT_RSS);
292#endif
293
294#ifdef RLIMIT_NPROC
295 PyModule_AddIntConstant(m, "RLIMIT_NPROC", RLIMIT_NPROC);
296#endif
297
298#ifdef RLIMIT_MEMLOCK
299 PyModule_AddIntConstant(m, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
300#endif
301
302#ifdef RLIMIT_SBSIZE
303 PyModule_AddIntConstant(m, "RLIMIT_SBSIZE", RLIMIT_SBSIZE);
304#endif
305
306#ifdef RUSAGE_SELF
307 PyModule_AddIntConstant(m, "RUSAGE_SELF", RUSAGE_SELF);
308#endif
309
310#ifdef RUSAGE_CHILDREN
311 PyModule_AddIntConstant(m, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
312#endif
313
314#ifdef RUSAGE_BOTH
315 PyModule_AddIntConstant(m, "RUSAGE_BOTH", RUSAGE_BOTH);
316#endif
317
318#if defined(HAVE_LONG_LONG)
319 if (sizeof(RLIM_INFINITY) > sizeof(long)) {
320 v = PyLong_FromLongLong((PY_LONG_LONG) RLIM_INFINITY);
321 } else
322#endif
323 {
324 v = PyInt_FromLong((long) RLIM_INFINITY);
325 }
326 if (v) {
327 PyModule_AddObject(m, "RLIM_INFINITY", v);
328 }
329 initialized = 1;
330}
Note: See TracBrowser for help on using the repository browser.