Guido van Rossum | f84a539 | 1997-08-15 00:04:24 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f84a539 | 1997-08-15 00:04:24 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f84a539 | 1997-08-15 00:04:24 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 11 | #include "Python.h" |
Guido van Rossum | 86aeb90 | 1997-10-31 16:55:51 +0000 | [diff] [blame] | 12 | #include "mytime.h" /* needed for SunOS4.1 */ |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 13 | #include <sys/resource.h> |
| 14 | #include <sys/time.h> |
| 15 | #include <unistd.h> |
| 16 | #include <string.h> |
| 17 | #include <errno.h> |
| 18 | |
Guido van Rossum | 607b33a | 1997-08-17 16:24:30 +0000 | [diff] [blame] | 19 | /* 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 |
| 26 | int getrusage(); |
| 27 | int getpagesize(); |
Guido van Rossum | 668e441 | 1997-08-05 02:24:57 +0000 | [diff] [blame] | 28 | #endif |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 29 | |
| 30 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 31 | |
| 32 | static PyObject *ResourceError; |
| 33 | |
| 34 | static PyObject * |
| 35 | resource_getrusage(self, args) |
| 36 | PyObject *self; |
| 37 | PyObject *args; |
| 38 | { |
| 39 | int who; |
| 40 | struct rusage ru; |
| 41 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 42 | if (!PyArg_ParseTuple(args, "i:getrusage", &who)) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 43 | return NULL; |
| 44 | |
| 45 | if (getrusage(who, &ru) == -1) { |
| 46 | if (errno == EINVAL) { |
| 47 | PyErr_SetString(PyExc_ValueError, |
| 48 | "invalid who parameter"); |
| 49 | return NULL; |
| 50 | } |
| 51 | PyErr_SetFromErrno(ResourceError); |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | /* Yeah, this 16-tuple is way ugly. It's probably a lot less |
| 56 | ugly than a dictionary with keys (or object attributes) |
| 57 | named things like 'ixrss'. |
| 58 | */ |
| 59 | return Py_BuildValue( |
| 60 | "ddiiiiiiiiiiiiii", |
| 61 | doubletime(ru.ru_utime), /* user time used */ |
| 62 | doubletime(ru.ru_stime), /* system time used */ |
| 63 | ru.ru_maxrss, /* max. resident set size */ |
| 64 | ru.ru_ixrss, /* shared memory size */ |
| 65 | ru.ru_idrss, /* unshared memory size */ |
| 66 | ru.ru_isrss, /* unshared stack size */ |
| 67 | ru.ru_minflt, /* page faults not requiring I/O*/ |
| 68 | ru.ru_majflt, /* page faults requiring I/O */ |
| 69 | ru.ru_nswap, /* number of swap outs */ |
| 70 | ru.ru_inblock, /* block input operations */ |
| 71 | ru.ru_oublock, /* block output operations */ |
| 72 | ru.ru_msgsnd, /* messages sent */ |
| 73 | ru.ru_msgrcv, /* messages received */ |
| 74 | ru.ru_nsignals, /* signals received */ |
| 75 | ru.ru_nvcsw, /* voluntary context switchs */ |
| 76 | ru.ru_nivcsw /* involuntary context switchs */ |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | static PyObject * |
| 82 | resource_getrlimit(self, args) |
| 83 | PyObject *self; |
| 84 | PyObject *args; |
| 85 | { |
| 86 | struct rlimit rl; |
| 87 | int resource; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 89 | if (!PyArg_ParseTuple(args, "i:getrlimit", &resource)) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 90 | return NULL; |
| 91 | |
| 92 | if (resource < 0 || resource >= RLIM_NLIMITS) { |
| 93 | PyErr_SetString(PyExc_ValueError, |
| 94 | "invalid resource specified"); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | if (getrlimit(resource, &rl) == -1) { |
| 99 | PyErr_SetFromErrno(ResourceError); |
| 100 | return NULL; |
| 101 | } |
Guido van Rossum | 763737b | 1999-01-06 18:44:57 +0000 | [diff] [blame] | 102 | |
| 103 | #if defined(HAVE_LONG_LONG) |
| 104 | if (sizeof(rl.rlim_cur) > sizeof(long)) { |
| 105 | return Py_BuildValue("LL", |
| 106 | (LONG_LONG) rl.rlim_cur, |
| 107 | (LONG_LONG) rl.rlim_max); |
| 108 | } |
| 109 | #endif |
| 110 | return Py_BuildValue("ii", (long) rl.rlim_cur, (long) rl.rlim_max); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static PyObject * |
| 114 | resource_setrlimit(self, args) |
| 115 | PyObject *self; |
| 116 | PyObject *args; |
| 117 | { |
| 118 | struct rlimit rl; |
| 119 | int resource; |
Guido van Rossum | 763737b | 1999-01-06 18:44:57 +0000 | [diff] [blame] | 120 | PyObject *curobj, *maxobj; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 122 | if (!PyArg_ParseTuple(args, "i(OO):setrlimit", &resource, &curobj, &maxobj)) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 123 | return NULL; |
| 124 | |
| 125 | if (resource < 0 || resource >= RLIM_NLIMITS) { |
| 126 | PyErr_SetString(PyExc_ValueError, |
| 127 | "invalid resource specified"); |
| 128 | return NULL; |
| 129 | } |
| 130 | |
Guido van Rossum | 763737b | 1999-01-06 18:44:57 +0000 | [diff] [blame] | 131 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 132 | rl.rlim_cur = PyInt_AsLong(curobj); |
| 133 | rl.rlim_max = PyInt_AsLong(maxobj); |
| 134 | #else |
| 135 | /* The limits are probably bigger than a long */ |
| 136 | rl.rlim_cur = PyLong_Check(curobj) ? |
| 137 | PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj); |
| 138 | rl.rlim_max = PyLong_Check(maxobj) ? |
| 139 | PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj); |
| 140 | #endif |
| 141 | |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 142 | rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY; |
| 143 | rl.rlim_max = rl.rlim_max & RLIM_INFINITY; |
| 144 | if (setrlimit(resource, &rl) == -1) { |
| 145 | if (errno == EINVAL) |
| 146 | PyErr_SetString(PyExc_ValueError, |
| 147 | "current limit exceeds maximum limit"); |
| 148 | else if (errno == EPERM) |
| 149 | PyErr_SetString(PyExc_ValueError, |
| 150 | "not allowed to raise maximum limit"); |
| 151 | else |
| 152 | PyErr_SetFromErrno(ResourceError); |
| 153 | return NULL; |
| 154 | } |
| 155 | Py_INCREF(Py_None); |
| 156 | return Py_None; |
| 157 | } |
| 158 | |
| 159 | static PyObject * |
| 160 | resource_getpagesize(self, args) |
| 161 | PyObject *self; |
| 162 | PyObject *args; |
| 163 | { |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 164 | if (!PyArg_ParseTuple(args, ":getpagesize")) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 165 | return NULL; |
| 166 | return Py_BuildValue("i", getpagesize()); |
| 167 | } |
| 168 | |
| 169 | /* List of functions */ |
| 170 | |
| 171 | static struct PyMethodDef |
| 172 | resource_methods[] = { |
| 173 | {"getrusage", resource_getrusage, 1}, |
| 174 | {"getrlimit", resource_getrlimit, 1}, |
| 175 | {"setrlimit", resource_setrlimit, 1}, |
| 176 | {"getpagesize", resource_getpagesize, 1}, |
| 177 | {NULL, NULL} /* sentinel */ |
| 178 | }; |
| 179 | |
| 180 | |
| 181 | /* Module initialization */ |
| 182 | |
| 183 | static void |
| 184 | ins(PyObject *dict, char *name, int value) |
| 185 | { |
| 186 | PyObject *v = PyInt_FromLong((long) value); |
| 187 | if (v) { |
| 188 | PyDict_SetItemString(dict, name, v); |
| 189 | Py_DECREF(v); |
| 190 | } |
| 191 | /* errors will be checked by initresource() */ |
| 192 | } |
| 193 | |
| 194 | void initresource() |
| 195 | { |
| 196 | PyObject *m, *d; |
| 197 | |
| 198 | /* Create the module and add the functions */ |
| 199 | m = Py_InitModule("resource", resource_methods); |
| 200 | |
| 201 | /* Add some symbolic constants to the module */ |
| 202 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 203 | ResourceError = PyErr_NewException("resource.error", NULL, NULL); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 204 | PyDict_SetItemString(d, "error", ResourceError); |
| 205 | |
| 206 | /* insert constants */ |
| 207 | #ifdef RLIMIT_CPU |
| 208 | ins(d, "RLIMIT_CPU", RLIMIT_CPU); |
| 209 | #endif |
| 210 | |
| 211 | #ifdef RLIMIT_FSIZE |
| 212 | ins(d, "RLIMIT_FSIZE", RLIMIT_FSIZE); |
| 213 | #endif |
| 214 | |
| 215 | #ifdef RLIMIT_DATA |
| 216 | ins(d, "RLIMIT_DATA", RLIMIT_DATA); |
| 217 | #endif |
| 218 | |
| 219 | #ifdef RLIMIT_STACK |
| 220 | ins(d, "RLIMIT_STACK", RLIMIT_STACK); |
| 221 | #endif |
| 222 | |
| 223 | #ifdef RLIMIT_CORE |
| 224 | ins(d, "RLIMIT_CORE", RLIMIT_CORE); |
| 225 | #endif |
| 226 | |
| 227 | #ifdef RLIMIT_NOFILE |
| 228 | ins(d, "RLIMIT_NOFILE", RLIMIT_NOFILE); |
| 229 | #endif |
| 230 | |
| 231 | #ifdef RLIMIT_OFILE |
| 232 | ins(d, "RLIMIT_OFILE", RLIMIT_OFILE); |
| 233 | #endif |
| 234 | |
| 235 | #ifdef RLIMIT_VMEM |
| 236 | ins(d, "RLIMIT_VMEM", RLIMIT_VMEM); |
| 237 | #endif |
| 238 | |
| 239 | #ifdef RLIMIT_AS |
| 240 | ins(d, "RLIMIT_AS", RLIMIT_AS); |
| 241 | #endif |
| 242 | |
| 243 | #ifdef RLIMIT_RSS |
| 244 | ins(d, "RLIMIT_RSS", RLIMIT_RSS); |
| 245 | #endif |
| 246 | |
| 247 | #ifdef RLIMIT_NPROC |
| 248 | ins(d, "RLIMIT_NPROC", RLIMIT_NPROC); |
| 249 | #endif |
| 250 | |
| 251 | #ifdef RLIMIT_MEMLOCK |
| 252 | ins(d, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK); |
| 253 | #endif |
| 254 | |
| 255 | #ifdef RUSAGE_SELF |
| 256 | ins(d, "RUSAGE_SELF", RUSAGE_SELF); |
| 257 | #endif |
| 258 | |
Guido van Rossum | ebd05eb | 1997-12-09 19:35:48 +0000 | [diff] [blame] | 259 | #ifdef RUSAGE_CHILDREN |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 260 | ins(d, "RUSAGE_CHILDREN", RUSAGE_CHILDREN); |
| 261 | #endif |
| 262 | |
| 263 | #ifdef RUSAGE_BOTH |
| 264 | ins(d, "RUSAGE_BOTH", RUSAGE_BOTH); |
| 265 | #endif |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 266 | } |