blob: 76fd73122b7d68968dab493b6ce936bf0b516d3f [file] [log] [blame]
Guido van Rossumf84a5391997-08-15 00:04:24 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossumf84a5391997-08-15 00:04:24 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossumf84a5391997-08-15 00:04:24 +00009******************************************************************/
10
Guido van Rossum2e631391996-12-18 18:37:27 +000011#include "Python.h"
Guido van Rossum86aeb901997-10-31 16:55:51 +000012#include "mytime.h" /* needed for SunOS4.1 */
Guido van Rossum2e631391996-12-18 18:37:27 +000013#include <sys/resource.h>
14#include <sys/time.h>
15#include <unistd.h>
16#include <string.h>
17#include <errno.h>
18
Guido van Rossum607b33a1997-08-17 16:24:30 +000019/* On some systems, these aren't in any header file.
20 On others they are, with inconsistent prototypes.
21 We declare the (default) return type, to shut up gcc -Wall;
22 but we can't declare the prototype, to avoid errors
23 when the header files declare it different.
24 Worse, on some Linuxes, getpagesize() returns a size_t... */
Guido van Rossum2e631391996-12-18 18:37:27 +000025
26#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
27
28static PyObject *ResourceError;
29
30static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +000031resource_getrusage(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +000032{
33 int who;
34 struct rusage ru;
35
Guido van Rossum43713e52000-02-29 13:59:29 +000036 if (!PyArg_ParseTuple(args, "i:getrusage", &who))
Guido van Rossum2e631391996-12-18 18:37:27 +000037 return NULL;
38
39 if (getrusage(who, &ru) == -1) {
40 if (errno == EINVAL) {
41 PyErr_SetString(PyExc_ValueError,
42 "invalid who parameter");
43 return NULL;
44 }
45 PyErr_SetFromErrno(ResourceError);
46 return NULL;
47 }
48
49 /* Yeah, this 16-tuple is way ugly. It's probably a lot less
50 ugly than a dictionary with keys (or object attributes)
51 named things like 'ixrss'.
52 */
53 return Py_BuildValue(
54 "ddiiiiiiiiiiiiii",
55 doubletime(ru.ru_utime), /* user time used */
56 doubletime(ru.ru_stime), /* system time used */
57 ru.ru_maxrss, /* max. resident set size */
58 ru.ru_ixrss, /* shared memory size */
59 ru.ru_idrss, /* unshared memory size */
60 ru.ru_isrss, /* unshared stack size */
61 ru.ru_minflt, /* page faults not requiring I/O*/
62 ru.ru_majflt, /* page faults requiring I/O */
63 ru.ru_nswap, /* number of swap outs */
64 ru.ru_inblock, /* block input operations */
65 ru.ru_oublock, /* block output operations */
66 ru.ru_msgsnd, /* messages sent */
67 ru.ru_msgrcv, /* messages received */
68 ru.ru_nsignals, /* signals received */
Thomas Wouters7e474022000-07-16 12:04:32 +000069 ru.ru_nvcsw, /* voluntary context switches */
70 ru.ru_nivcsw /* involuntary context switches */
Guido van Rossum2e631391996-12-18 18:37:27 +000071 );
72}
73
74
75static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +000076resource_getrlimit(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +000077{
78 struct rlimit rl;
79 int resource;
Guido van Rossum2e631391996-12-18 18:37:27 +000080
Guido van Rossum43713e52000-02-29 13:59:29 +000081 if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
Guido van Rossum2e631391996-12-18 18:37:27 +000082 return NULL;
83
84 if (resource < 0 || resource >= RLIM_NLIMITS) {
85 PyErr_SetString(PyExc_ValueError,
86 "invalid resource specified");
87 return NULL;
88 }
89
90 if (getrlimit(resource, &rl) == -1) {
91 PyErr_SetFromErrno(ResourceError);
92 return NULL;
93 }
Guido van Rossum763737b1999-01-06 18:44:57 +000094
95#if defined(HAVE_LONG_LONG)
96 if (sizeof(rl.rlim_cur) > sizeof(long)) {
97 return Py_BuildValue("LL",
98 (LONG_LONG) rl.rlim_cur,
99 (LONG_LONG) rl.rlim_max);
100 }
101#endif
102 return Py_BuildValue("ii", (long) rl.rlim_cur, (long) rl.rlim_max);
Guido van Rossum2e631391996-12-18 18:37:27 +0000103}
104
105static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000106resource_setrlimit(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +0000107{
108 struct rlimit rl;
109 int resource;
Guido van Rossum763737b1999-01-06 18:44:57 +0000110 PyObject *curobj, *maxobj;
Guido van Rossum2e631391996-12-18 18:37:27 +0000111
Guido van Rossum43713e52000-02-29 13:59:29 +0000112 if (!PyArg_ParseTuple(args, "i(OO):setrlimit", &resource, &curobj, &maxobj))
Guido van Rossum2e631391996-12-18 18:37:27 +0000113 return NULL;
114
115 if (resource < 0 || resource >= RLIM_NLIMITS) {
116 PyErr_SetString(PyExc_ValueError,
117 "invalid resource specified");
118 return NULL;
119 }
120
Guido van Rossum763737b1999-01-06 18:44:57 +0000121#if !defined(HAVE_LARGEFILE_SUPPORT)
122 rl.rlim_cur = PyInt_AsLong(curobj);
123 rl.rlim_max = PyInt_AsLong(maxobj);
124#else
125 /* The limits are probably bigger than a long */
126 rl.rlim_cur = PyLong_Check(curobj) ?
127 PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
128 rl.rlim_max = PyLong_Check(maxobj) ?
129 PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
130#endif
131
Guido van Rossum2e631391996-12-18 18:37:27 +0000132 rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
133 rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
134 if (setrlimit(resource, &rl) == -1) {
135 if (errno == EINVAL)
136 PyErr_SetString(PyExc_ValueError,
137 "current limit exceeds maximum limit");
138 else if (errno == EPERM)
139 PyErr_SetString(PyExc_ValueError,
140 "not allowed to raise maximum limit");
141 else
142 PyErr_SetFromErrno(ResourceError);
143 return NULL;
144 }
145 Py_INCREF(Py_None);
146 return Py_None;
147}
148
149static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000150resource_getpagesize(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +0000151{
Guido van Rossum43713e52000-02-29 13:59:29 +0000152 if (!PyArg_ParseTuple(args, ":getpagesize"))
Guido van Rossum2e631391996-12-18 18:37:27 +0000153 return NULL;
154 return Py_BuildValue("i", getpagesize());
155}
156
157/* List of functions */
158
159static struct PyMethodDef
160resource_methods[] = {
161 {"getrusage", resource_getrusage, 1},
162 {"getrlimit", resource_getrlimit, 1},
163 {"setrlimit", resource_setrlimit, 1},
164 {"getpagesize", resource_getpagesize, 1},
165 {NULL, NULL} /* sentinel */
166};
167
168
169/* Module initialization */
170
171static void
172ins(PyObject *dict, char *name, int value)
173{
174 PyObject *v = PyInt_FromLong((long) value);
175 if (v) {
176 PyDict_SetItemString(dict, name, v);
177 Py_DECREF(v);
178 }
179 /* errors will be checked by initresource() */
180}
181
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000182void initresource(void)
Guido van Rossum2e631391996-12-18 18:37:27 +0000183{
184 PyObject *m, *d;
185
186 /* Create the module and add the functions */
187 m = Py_InitModule("resource", resource_methods);
188
189 /* Add some symbolic constants to the module */
190 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000191 ResourceError = PyErr_NewException("resource.error", NULL, NULL);
Guido van Rossum2e631391996-12-18 18:37:27 +0000192 PyDict_SetItemString(d, "error", ResourceError);
193
194 /* insert constants */
195#ifdef RLIMIT_CPU
196 ins(d, "RLIMIT_CPU", RLIMIT_CPU);
197#endif
198
199#ifdef RLIMIT_FSIZE
200 ins(d, "RLIMIT_FSIZE", RLIMIT_FSIZE);
201#endif
202
203#ifdef RLIMIT_DATA
204 ins(d, "RLIMIT_DATA", RLIMIT_DATA);
205#endif
206
207#ifdef RLIMIT_STACK
208 ins(d, "RLIMIT_STACK", RLIMIT_STACK);
209#endif
210
211#ifdef RLIMIT_CORE
212 ins(d, "RLIMIT_CORE", RLIMIT_CORE);
213#endif
214
215#ifdef RLIMIT_NOFILE
216 ins(d, "RLIMIT_NOFILE", RLIMIT_NOFILE);
217#endif
218
219#ifdef RLIMIT_OFILE
220 ins(d, "RLIMIT_OFILE", RLIMIT_OFILE);
221#endif
222
223#ifdef RLIMIT_VMEM
224 ins(d, "RLIMIT_VMEM", RLIMIT_VMEM);
225#endif
226
227#ifdef RLIMIT_AS
228 ins(d, "RLIMIT_AS", RLIMIT_AS);
229#endif
230
231#ifdef RLIMIT_RSS
232 ins(d, "RLIMIT_RSS", RLIMIT_RSS);
233#endif
234
235#ifdef RLIMIT_NPROC
236 ins(d, "RLIMIT_NPROC", RLIMIT_NPROC);
237#endif
238
239#ifdef RLIMIT_MEMLOCK
240 ins(d, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
241#endif
242
243#ifdef RUSAGE_SELF
244 ins(d, "RUSAGE_SELF", RUSAGE_SELF);
245#endif
246
Guido van Rossumebd05eb1997-12-09 19:35:48 +0000247#ifdef RUSAGE_CHILDREN
Guido van Rossum2e631391996-12-18 18:37:27 +0000248 ins(d, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
249#endif
250
251#ifdef RUSAGE_BOTH
252 ins(d, "RUSAGE_BOTH", RUSAGE_BOTH);
253#endif
Guido van Rossum2e631391996-12-18 18:37:27 +0000254}