blob: 9f2d9c06a747ccf51c6bfffe29666afc4da393fc [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>
5#include <unistd.h>
6#include <string.h>
7#include <errno.h>
8
Guido van Rossum607b33a1997-08-17 16:24:30 +00009/* 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... */
Guido van Rossum2e631391996-12-18 18:37:27 +000015
16#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
17
18static PyObject *ResourceError;
19
20static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +000021resource_getrusage(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +000022{
23 int who;
24 struct rusage ru;
25
Guido van Rossum43713e52000-02-29 13:59:29 +000026 if (!PyArg_ParseTuple(args, "i:getrusage", &who))
Guido van Rossum2e631391996-12-18 18:37:27 +000027 return NULL;
28
29 if (getrusage(who, &ru) == -1) {
30 if (errno == EINVAL) {
31 PyErr_SetString(PyExc_ValueError,
32 "invalid who parameter");
33 return NULL;
34 }
35 PyErr_SetFromErrno(ResourceError);
36 return NULL;
37 }
38
39 /* Yeah, this 16-tuple is way ugly. It's probably a lot less
40 ugly than a dictionary with keys (or object attributes)
41 named things like 'ixrss'.
42 */
43 return Py_BuildValue(
44 "ddiiiiiiiiiiiiii",
45 doubletime(ru.ru_utime), /* user time used */
46 doubletime(ru.ru_stime), /* system time used */
47 ru.ru_maxrss, /* max. resident set size */
48 ru.ru_ixrss, /* shared memory size */
49 ru.ru_idrss, /* unshared memory size */
50 ru.ru_isrss, /* unshared stack size */
51 ru.ru_minflt, /* page faults not requiring I/O*/
52 ru.ru_majflt, /* page faults requiring I/O */
53 ru.ru_nswap, /* number of swap outs */
54 ru.ru_inblock, /* block input operations */
55 ru.ru_oublock, /* block output operations */
56 ru.ru_msgsnd, /* messages sent */
57 ru.ru_msgrcv, /* messages received */
58 ru.ru_nsignals, /* signals received */
Thomas Wouters7e474022000-07-16 12:04:32 +000059 ru.ru_nvcsw, /* voluntary context switches */
60 ru.ru_nivcsw /* involuntary context switches */
Guido van Rossum2e631391996-12-18 18:37:27 +000061 );
62}
63
64
65static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +000066resource_getrlimit(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +000067{
68 struct rlimit rl;
69 int resource;
Guido van Rossum2e631391996-12-18 18:37:27 +000070
Guido van Rossum43713e52000-02-29 13:59:29 +000071 if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
Guido van Rossum2e631391996-12-18 18:37:27 +000072 return NULL;
73
74 if (resource < 0 || resource >= RLIM_NLIMITS) {
75 PyErr_SetString(PyExc_ValueError,
76 "invalid resource specified");
77 return NULL;
78 }
79
80 if (getrlimit(resource, &rl) == -1) {
81 PyErr_SetFromErrno(ResourceError);
82 return NULL;
83 }
Guido van Rossum763737b1999-01-06 18:44:57 +000084
85#if defined(HAVE_LONG_LONG)
86 if (sizeof(rl.rlim_cur) > sizeof(long)) {
87 return Py_BuildValue("LL",
88 (LONG_LONG) rl.rlim_cur,
89 (LONG_LONG) rl.rlim_max);
90 }
91#endif
92 return Py_BuildValue("ii", (long) rl.rlim_cur, (long) rl.rlim_max);
Guido van Rossum2e631391996-12-18 18:37:27 +000093}
94
95static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +000096resource_setrlimit(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +000097{
98 struct rlimit rl;
99 int resource;
Guido van Rossum763737b1999-01-06 18:44:57 +0000100 PyObject *curobj, *maxobj;
Guido van Rossum2e631391996-12-18 18:37:27 +0000101
Guido van Rossum43713e52000-02-29 13:59:29 +0000102 if (!PyArg_ParseTuple(args, "i(OO):setrlimit", &resource, &curobj, &maxobj))
Guido van Rossum2e631391996-12-18 18:37:27 +0000103 return NULL;
104
105 if (resource < 0 || resource >= RLIM_NLIMITS) {
106 PyErr_SetString(PyExc_ValueError,
107 "invalid resource specified");
108 return NULL;
109 }
110
Guido van Rossum763737b1999-01-06 18:44:57 +0000111#if !defined(HAVE_LARGEFILE_SUPPORT)
112 rl.rlim_cur = PyInt_AsLong(curobj);
113 rl.rlim_max = PyInt_AsLong(maxobj);
114#else
115 /* The limits are probably bigger than a long */
116 rl.rlim_cur = PyLong_Check(curobj) ?
117 PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
118 rl.rlim_max = PyLong_Check(maxobj) ?
119 PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
120#endif
121
Guido van Rossum2e631391996-12-18 18:37:27 +0000122 rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
123 rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
124 if (setrlimit(resource, &rl) == -1) {
125 if (errno == EINVAL)
126 PyErr_SetString(PyExc_ValueError,
127 "current limit exceeds maximum limit");
128 else if (errno == EPERM)
129 PyErr_SetString(PyExc_ValueError,
130 "not allowed to raise maximum limit");
131 else
132 PyErr_SetFromErrno(ResourceError);
133 return NULL;
134 }
135 Py_INCREF(Py_None);
136 return Py_None;
137}
138
139static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000140resource_getpagesize(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +0000141{
Guido van Rossum43713e52000-02-29 13:59:29 +0000142 if (!PyArg_ParseTuple(args, ":getpagesize"))
Guido van Rossum2e631391996-12-18 18:37:27 +0000143 return NULL;
144 return Py_BuildValue("i", getpagesize());
145}
146
147/* List of functions */
148
149static struct PyMethodDef
150resource_methods[] = {
151 {"getrusage", resource_getrusage, 1},
152 {"getrlimit", resource_getrlimit, 1},
153 {"setrlimit", resource_setrlimit, 1},
154 {"getpagesize", resource_getpagesize, 1},
155 {NULL, NULL} /* sentinel */
156};
157
158
159/* Module initialization */
160
161static void
162ins(PyObject *dict, char *name, int value)
163{
164 PyObject *v = PyInt_FromLong((long) value);
165 if (v) {
166 PyDict_SetItemString(dict, name, v);
167 Py_DECREF(v);
168 }
169 /* errors will be checked by initresource() */
170}
171
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000172void initresource(void)
Guido van Rossum2e631391996-12-18 18:37:27 +0000173{
174 PyObject *m, *d;
175
176 /* Create the module and add the functions */
177 m = Py_InitModule("resource", resource_methods);
178
179 /* Add some symbolic constants to the module */
180 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000181 ResourceError = PyErr_NewException("resource.error", NULL, NULL);
Guido van Rossum2e631391996-12-18 18:37:27 +0000182 PyDict_SetItemString(d, "error", ResourceError);
183
184 /* insert constants */
185#ifdef RLIMIT_CPU
186 ins(d, "RLIMIT_CPU", RLIMIT_CPU);
187#endif
188
189#ifdef RLIMIT_FSIZE
190 ins(d, "RLIMIT_FSIZE", RLIMIT_FSIZE);
191#endif
192
193#ifdef RLIMIT_DATA
194 ins(d, "RLIMIT_DATA", RLIMIT_DATA);
195#endif
196
197#ifdef RLIMIT_STACK
198 ins(d, "RLIMIT_STACK", RLIMIT_STACK);
199#endif
200
201#ifdef RLIMIT_CORE
202 ins(d, "RLIMIT_CORE", RLIMIT_CORE);
203#endif
204
205#ifdef RLIMIT_NOFILE
206 ins(d, "RLIMIT_NOFILE", RLIMIT_NOFILE);
207#endif
208
209#ifdef RLIMIT_OFILE
210 ins(d, "RLIMIT_OFILE", RLIMIT_OFILE);
211#endif
212
213#ifdef RLIMIT_VMEM
214 ins(d, "RLIMIT_VMEM", RLIMIT_VMEM);
215#endif
216
217#ifdef RLIMIT_AS
218 ins(d, "RLIMIT_AS", RLIMIT_AS);
219#endif
220
221#ifdef RLIMIT_RSS
222 ins(d, "RLIMIT_RSS", RLIMIT_RSS);
223#endif
224
225#ifdef RLIMIT_NPROC
226 ins(d, "RLIMIT_NPROC", RLIMIT_NPROC);
227#endif
228
229#ifdef RLIMIT_MEMLOCK
230 ins(d, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
231#endif
232
233#ifdef RUSAGE_SELF
234 ins(d, "RUSAGE_SELF", RUSAGE_SELF);
235#endif
236
Guido van Rossumebd05eb1997-12-09 19:35:48 +0000237#ifdef RUSAGE_CHILDREN
Guido van Rossum2e631391996-12-18 18:37:27 +0000238 ins(d, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
239#endif
240
241#ifdef RUSAGE_BOTH
242 ins(d, "RUSAGE_BOTH", RUSAGE_BOTH);
243#endif
Guido van Rossum2e631391996-12-18 18:37:27 +0000244}