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... */ |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 25 | |
| 26 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 27 | |
| 28 | static PyObject *ResourceError; |
| 29 | |
| 30 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 31 | resource_getrusage(PyObject *self, PyObject *args) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 32 | { |
| 33 | int who; |
| 34 | struct rusage ru; |
| 35 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 36 | if (!PyArg_ParseTuple(args, "i:getrusage", &who)) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 37 | 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 Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 69 | ru.ru_nvcsw, /* voluntary context switches */ |
| 70 | ru.ru_nivcsw /* involuntary context switches */ |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 71 | ); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 76 | resource_getrlimit(PyObject *self, PyObject *args) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 77 | { |
| 78 | struct rlimit rl; |
| 79 | int resource; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 80 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 81 | if (!PyArg_ParseTuple(args, "i:getrlimit", &resource)) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 82 | 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 Rossum | 763737b | 1999-01-06 18:44:57 +0000 | [diff] [blame] | 94 | |
| 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 Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 106 | resource_setrlimit(PyObject *self, PyObject *args) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 107 | { |
| 108 | struct rlimit rl; |
| 109 | int resource; |
Guido van Rossum | 763737b | 1999-01-06 18:44:57 +0000 | [diff] [blame] | 110 | PyObject *curobj, *maxobj; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 112 | if (!PyArg_ParseTuple(args, "i(OO):setrlimit", &resource, &curobj, &maxobj)) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 113 | 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 Rossum | 763737b | 1999-01-06 18:44:57 +0000 | [diff] [blame] | 121 | #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 Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 132 | 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 | |
| 149 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 150 | resource_getpagesize(PyObject *self, PyObject *args) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 151 | { |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 152 | if (!PyArg_ParseTuple(args, ":getpagesize")) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 153 | return NULL; |
| 154 | return Py_BuildValue("i", getpagesize()); |
| 155 | } |
| 156 | |
| 157 | /* List of functions */ |
| 158 | |
| 159 | static struct PyMethodDef |
| 160 | resource_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 | |
| 171 | static void |
| 172 | ins(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 Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 182 | void initresource(void) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 183 | { |
| 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 Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 191 | ResourceError = PyErr_NewException("resource.error", NULL, NULL); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 192 | 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 Rossum | ebd05eb | 1997-12-09 19:35:48 +0000 | [diff] [blame] | 247 | #ifdef RUSAGE_CHILDREN |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 248 | ins(d, "RUSAGE_CHILDREN", RUSAGE_CHILDREN); |
| 249 | #endif |
| 250 | |
| 251 | #ifdef RUSAGE_BOTH |
| 252 | ins(d, "RUSAGE_BOTH", RUSAGE_BOTH); |
| 253 | #endif |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 254 | } |