blob: 9da87af88d0a46e1865243479f6c073dc75d78f1 [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... */
25#ifndef linux
26int getrusage();
27int getpagesize();
Guido van Rossum668e4411997-08-05 02:24:57 +000028#endif
Guido van Rossum2e631391996-12-18 18:37:27 +000029
30#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
31
32static PyObject *ResourceError;
33
34static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +000035resource_getrusage(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +000036{
37 int who;
38 struct rusage ru;
39
Guido van Rossum43713e52000-02-29 13:59:29 +000040 if (!PyArg_ParseTuple(args, "i:getrusage", &who))
Guido van Rossum2e631391996-12-18 18:37:27 +000041 return NULL;
42
43 if (getrusage(who, &ru) == -1) {
44 if (errno == EINVAL) {
45 PyErr_SetString(PyExc_ValueError,
46 "invalid who parameter");
47 return NULL;
48 }
49 PyErr_SetFromErrno(ResourceError);
50 return NULL;
51 }
52
53 /* Yeah, this 16-tuple is way ugly. It's probably a lot less
54 ugly than a dictionary with keys (or object attributes)
55 named things like 'ixrss'.
56 */
57 return Py_BuildValue(
58 "ddiiiiiiiiiiiiii",
59 doubletime(ru.ru_utime), /* user time used */
60 doubletime(ru.ru_stime), /* system time used */
61 ru.ru_maxrss, /* max. resident set size */
62 ru.ru_ixrss, /* shared memory size */
63 ru.ru_idrss, /* unshared memory size */
64 ru.ru_isrss, /* unshared stack size */
65 ru.ru_minflt, /* page faults not requiring I/O*/
66 ru.ru_majflt, /* page faults requiring I/O */
67 ru.ru_nswap, /* number of swap outs */
68 ru.ru_inblock, /* block input operations */
69 ru.ru_oublock, /* block output operations */
70 ru.ru_msgsnd, /* messages sent */
71 ru.ru_msgrcv, /* messages received */
72 ru.ru_nsignals, /* signals received */
Thomas Wouters7e474022000-07-16 12:04:32 +000073 ru.ru_nvcsw, /* voluntary context switches */
74 ru.ru_nivcsw /* involuntary context switches */
Guido van Rossum2e631391996-12-18 18:37:27 +000075 );
76}
77
78
79static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +000080resource_getrlimit(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +000081{
82 struct rlimit rl;
83 int resource;
Guido van Rossum2e631391996-12-18 18:37:27 +000084
Guido van Rossum43713e52000-02-29 13:59:29 +000085 if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
Guido van Rossum2e631391996-12-18 18:37:27 +000086 return NULL;
87
88 if (resource < 0 || resource >= RLIM_NLIMITS) {
89 PyErr_SetString(PyExc_ValueError,
90 "invalid resource specified");
91 return NULL;
92 }
93
94 if (getrlimit(resource, &rl) == -1) {
95 PyErr_SetFromErrno(ResourceError);
96 return NULL;
97 }
Guido van Rossum763737b1999-01-06 18:44:57 +000098
99#if defined(HAVE_LONG_LONG)
100 if (sizeof(rl.rlim_cur) > sizeof(long)) {
101 return Py_BuildValue("LL",
102 (LONG_LONG) rl.rlim_cur,
103 (LONG_LONG) rl.rlim_max);
104 }
105#endif
106 return Py_BuildValue("ii", (long) rl.rlim_cur, (long) rl.rlim_max);
Guido van Rossum2e631391996-12-18 18:37:27 +0000107}
108
109static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000110resource_setrlimit(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +0000111{
112 struct rlimit rl;
113 int resource;
Guido van Rossum763737b1999-01-06 18:44:57 +0000114 PyObject *curobj, *maxobj;
Guido van Rossum2e631391996-12-18 18:37:27 +0000115
Guido van Rossum43713e52000-02-29 13:59:29 +0000116 if (!PyArg_ParseTuple(args, "i(OO):setrlimit", &resource, &curobj, &maxobj))
Guido van Rossum2e631391996-12-18 18:37:27 +0000117 return NULL;
118
119 if (resource < 0 || resource >= RLIM_NLIMITS) {
120 PyErr_SetString(PyExc_ValueError,
121 "invalid resource specified");
122 return NULL;
123 }
124
Guido van Rossum763737b1999-01-06 18:44:57 +0000125#if !defined(HAVE_LARGEFILE_SUPPORT)
126 rl.rlim_cur = PyInt_AsLong(curobj);
127 rl.rlim_max = PyInt_AsLong(maxobj);
128#else
129 /* The limits are probably bigger than a long */
130 rl.rlim_cur = PyLong_Check(curobj) ?
131 PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
132 rl.rlim_max = PyLong_Check(maxobj) ?
133 PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
134#endif
135
Guido van Rossum2e631391996-12-18 18:37:27 +0000136 rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
137 rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
138 if (setrlimit(resource, &rl) == -1) {
139 if (errno == EINVAL)
140 PyErr_SetString(PyExc_ValueError,
141 "current limit exceeds maximum limit");
142 else if (errno == EPERM)
143 PyErr_SetString(PyExc_ValueError,
144 "not allowed to raise maximum limit");
145 else
146 PyErr_SetFromErrno(ResourceError);
147 return NULL;
148 }
149 Py_INCREF(Py_None);
150 return Py_None;
151}
152
153static PyObject *
Peter Schneider-Kamp416d4132000-07-10 12:15:54 +0000154resource_getpagesize(PyObject *self, PyObject *args)
Guido van Rossum2e631391996-12-18 18:37:27 +0000155{
Guido van Rossum43713e52000-02-29 13:59:29 +0000156 if (!PyArg_ParseTuple(args, ":getpagesize"))
Guido van Rossum2e631391996-12-18 18:37:27 +0000157 return NULL;
158 return Py_BuildValue("i", getpagesize());
159}
160
161/* List of functions */
162
163static struct PyMethodDef
164resource_methods[] = {
165 {"getrusage", resource_getrusage, 1},
166 {"getrlimit", resource_getrlimit, 1},
167 {"setrlimit", resource_setrlimit, 1},
168 {"getpagesize", resource_getpagesize, 1},
169 {NULL, NULL} /* sentinel */
170};
171
172
173/* Module initialization */
174
175static void
176ins(PyObject *dict, char *name, int value)
177{
178 PyObject *v = PyInt_FromLong((long) value);
179 if (v) {
180 PyDict_SetItemString(dict, name, v);
181 Py_DECREF(v);
182 }
183 /* errors will be checked by initresource() */
184}
185
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000186void initresource(void)
Guido van Rossum2e631391996-12-18 18:37:27 +0000187{
188 PyObject *m, *d;
189
190 /* Create the module and add the functions */
191 m = Py_InitModule("resource", resource_methods);
192
193 /* Add some symbolic constants to the module */
194 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000195 ResourceError = PyErr_NewException("resource.error", NULL, NULL);
Guido van Rossum2e631391996-12-18 18:37:27 +0000196 PyDict_SetItemString(d, "error", ResourceError);
197
198 /* insert constants */
199#ifdef RLIMIT_CPU
200 ins(d, "RLIMIT_CPU", RLIMIT_CPU);
201#endif
202
203#ifdef RLIMIT_FSIZE
204 ins(d, "RLIMIT_FSIZE", RLIMIT_FSIZE);
205#endif
206
207#ifdef RLIMIT_DATA
208 ins(d, "RLIMIT_DATA", RLIMIT_DATA);
209#endif
210
211#ifdef RLIMIT_STACK
212 ins(d, "RLIMIT_STACK", RLIMIT_STACK);
213#endif
214
215#ifdef RLIMIT_CORE
216 ins(d, "RLIMIT_CORE", RLIMIT_CORE);
217#endif
218
219#ifdef RLIMIT_NOFILE
220 ins(d, "RLIMIT_NOFILE", RLIMIT_NOFILE);
221#endif
222
223#ifdef RLIMIT_OFILE
224 ins(d, "RLIMIT_OFILE", RLIMIT_OFILE);
225#endif
226
227#ifdef RLIMIT_VMEM
228 ins(d, "RLIMIT_VMEM", RLIMIT_VMEM);
229#endif
230
231#ifdef RLIMIT_AS
232 ins(d, "RLIMIT_AS", RLIMIT_AS);
233#endif
234
235#ifdef RLIMIT_RSS
236 ins(d, "RLIMIT_RSS", RLIMIT_RSS);
237#endif
238
239#ifdef RLIMIT_NPROC
240 ins(d, "RLIMIT_NPROC", RLIMIT_NPROC);
241#endif
242
243#ifdef RLIMIT_MEMLOCK
244 ins(d, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
245#endif
246
247#ifdef RUSAGE_SELF
248 ins(d, "RUSAGE_SELF", RUSAGE_SELF);
249#endif
250
Guido van Rossumebd05eb1997-12-09 19:35:48 +0000251#ifdef RUSAGE_CHILDREN
Guido van Rossum2e631391996-12-18 18:37:27 +0000252 ins(d, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
253#endif
254
255#ifdef RUSAGE_BOTH
256 ins(d, "RUSAGE_BOTH", RUSAGE_BOTH);
257#endif
Guido van Rossum2e631391996-12-18 18:37:27 +0000258}