Guido van Rossum | f84a539 | 1997-08-15 00:04:24 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 2 | #include "Python.h" |
| 3 | #include <sys/resource.h> |
| 4 | #include <sys/time.h> |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 5 | #include <string.h> |
| 6 | #include <errno.h> |
| 7 | |
Guido van Rossum | 607b33a | 1997-08-17 16:24:30 +0000 | [diff] [blame] | 8 | /* On some systems, these aren't in any header file. |
| 9 | On others they are, with inconsistent prototypes. |
| 10 | We declare the (default) return type, to shut up gcc -Wall; |
| 11 | but we can't declare the prototype, to avoid errors |
| 12 | when the header files declare it different. |
| 13 | Worse, on some Linuxes, getpagesize() returns a size_t... */ |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 14 | |
| 15 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 16 | |
| 17 | static PyObject *ResourceError; |
| 18 | |
| 19 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 20 | resource_getrusage(PyObject *self, PyObject *args) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 21 | { |
| 22 | int who; |
| 23 | struct rusage ru; |
| 24 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 25 | if (!PyArg_ParseTuple(args, "i:getrusage", &who)) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 26 | return NULL; |
| 27 | |
| 28 | if (getrusage(who, &ru) == -1) { |
| 29 | if (errno == EINVAL) { |
| 30 | PyErr_SetString(PyExc_ValueError, |
| 31 | "invalid who parameter"); |
| 32 | return NULL; |
| 33 | } |
| 34 | PyErr_SetFromErrno(ResourceError); |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | /* Yeah, this 16-tuple is way ugly. It's probably a lot less |
| 39 | ugly than a dictionary with keys (or object attributes) |
| 40 | named things like 'ixrss'. |
| 41 | */ |
| 42 | return Py_BuildValue( |
| 43 | "ddiiiiiiiiiiiiii", |
| 44 | doubletime(ru.ru_utime), /* user time used */ |
| 45 | doubletime(ru.ru_stime), /* system time used */ |
| 46 | ru.ru_maxrss, /* max. resident set size */ |
| 47 | ru.ru_ixrss, /* shared memory size */ |
| 48 | ru.ru_idrss, /* unshared memory size */ |
| 49 | ru.ru_isrss, /* unshared stack size */ |
| 50 | ru.ru_minflt, /* page faults not requiring I/O*/ |
| 51 | ru.ru_majflt, /* page faults requiring I/O */ |
| 52 | ru.ru_nswap, /* number of swap outs */ |
| 53 | ru.ru_inblock, /* block input operations */ |
| 54 | ru.ru_oublock, /* block output operations */ |
| 55 | ru.ru_msgsnd, /* messages sent */ |
| 56 | ru.ru_msgrcv, /* messages received */ |
| 57 | ru.ru_nsignals, /* signals received */ |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 58 | ru.ru_nvcsw, /* voluntary context switches */ |
| 59 | ru.ru_nivcsw /* involuntary context switches */ |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 60 | ); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 65 | resource_getrlimit(PyObject *self, PyObject *args) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 66 | { |
| 67 | struct rlimit rl; |
| 68 | int resource; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 70 | if (!PyArg_ParseTuple(args, "i:getrlimit", &resource)) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 71 | return NULL; |
| 72 | |
| 73 | if (resource < 0 || resource >= RLIM_NLIMITS) { |
| 74 | PyErr_SetString(PyExc_ValueError, |
| 75 | "invalid resource specified"); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
| 79 | if (getrlimit(resource, &rl) == -1) { |
| 80 | PyErr_SetFromErrno(ResourceError); |
| 81 | return NULL; |
| 82 | } |
Guido van Rossum | 763737b | 1999-01-06 18:44:57 +0000 | [diff] [blame] | 83 | |
| 84 | #if defined(HAVE_LONG_LONG) |
| 85 | if (sizeof(rl.rlim_cur) > sizeof(long)) { |
| 86 | return Py_BuildValue("LL", |
| 87 | (LONG_LONG) rl.rlim_cur, |
| 88 | (LONG_LONG) rl.rlim_max); |
| 89 | } |
| 90 | #endif |
| 91 | 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] | 92 | } |
| 93 | |
| 94 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 95 | resource_setrlimit(PyObject *self, PyObject *args) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 96 | { |
| 97 | struct rlimit rl; |
| 98 | int resource; |
Guido van Rossum | 763737b | 1999-01-06 18:44:57 +0000 | [diff] [blame] | 99 | PyObject *curobj, *maxobj; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 100 | |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 101 | if (!PyArg_ParseTuple(args, "i(OO):setrlimit", &resource, &curobj, &maxobj)) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 102 | return NULL; |
| 103 | |
| 104 | if (resource < 0 || resource >= RLIM_NLIMITS) { |
| 105 | PyErr_SetString(PyExc_ValueError, |
| 106 | "invalid resource specified"); |
| 107 | return NULL; |
| 108 | } |
| 109 | |
Guido van Rossum | 763737b | 1999-01-06 18:44:57 +0000 | [diff] [blame] | 110 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 111 | rl.rlim_cur = PyInt_AsLong(curobj); |
| 112 | rl.rlim_max = PyInt_AsLong(maxobj); |
| 113 | #else |
| 114 | /* The limits are probably bigger than a long */ |
| 115 | rl.rlim_cur = PyLong_Check(curobj) ? |
| 116 | PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj); |
| 117 | rl.rlim_max = PyLong_Check(maxobj) ? |
| 118 | PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj); |
| 119 | #endif |
| 120 | |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 121 | rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY; |
| 122 | rl.rlim_max = rl.rlim_max & RLIM_INFINITY; |
| 123 | if (setrlimit(resource, &rl) == -1) { |
| 124 | if (errno == EINVAL) |
| 125 | PyErr_SetString(PyExc_ValueError, |
| 126 | "current limit exceeds maximum limit"); |
| 127 | else if (errno == EPERM) |
| 128 | PyErr_SetString(PyExc_ValueError, |
| 129 | "not allowed to raise maximum limit"); |
| 130 | else |
| 131 | PyErr_SetFromErrno(ResourceError); |
| 132 | return NULL; |
| 133 | } |
| 134 | Py_INCREF(Py_None); |
| 135 | return Py_None; |
| 136 | } |
| 137 | |
| 138 | static PyObject * |
Peter Schneider-Kamp | 416d413 | 2000-07-10 12:15:54 +0000 | [diff] [blame] | 139 | resource_getpagesize(PyObject *self, PyObject *args) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 140 | { |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 141 | if (!PyArg_ParseTuple(args, ":getpagesize")) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 142 | return NULL; |
| 143 | return Py_BuildValue("i", getpagesize()); |
| 144 | } |
| 145 | |
| 146 | /* List of functions */ |
| 147 | |
| 148 | static struct PyMethodDef |
| 149 | resource_methods[] = { |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 150 | {"getrusage", resource_getrusage, METH_VARARGS}, |
| 151 | {"getrlimit", resource_getrlimit, METH_VARARGS}, |
| 152 | {"setrlimit", resource_setrlimit, METH_VARARGS}, |
| 153 | {"getpagesize", resource_getpagesize, METH_VARARGS}, |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 154 | {NULL, NULL} /* sentinel */ |
| 155 | }; |
| 156 | |
| 157 | |
| 158 | /* Module initialization */ |
| 159 | |
Andrew M. Kuchling | fda3c3d | 2001-09-17 16:19:16 +0000 | [diff] [blame] | 160 | DL_EXPORT(void) |
| 161 | initresource(void) |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 162 | { |
Neil Schemenauer | 29ac3cb | 2002-03-24 22:27:39 +0000 | [diff] [blame^] | 163 | PyObject *m, *v; |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 164 | |
| 165 | /* Create the module and add the functions */ |
| 166 | m = Py_InitModule("resource", resource_methods); |
| 167 | |
| 168 | /* Add some symbolic constants to the module */ |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 169 | if (ResourceError == NULL) { |
| 170 | ResourceError = PyErr_NewException("resource.error", |
| 171 | NULL, NULL); |
| 172 | } |
| 173 | Py_INCREF(ResourceError); |
| 174 | PyModule_AddObject(m, "error", ResourceError); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 175 | |
| 176 | /* insert constants */ |
| 177 | #ifdef RLIMIT_CPU |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 178 | PyModule_AddIntConstant(m, "RLIMIT_CPU", RLIMIT_CPU); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 179 | #endif |
| 180 | |
| 181 | #ifdef RLIMIT_FSIZE |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 182 | PyModule_AddIntConstant(m, "RLIMIT_FSIZE", RLIMIT_FSIZE); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 183 | #endif |
| 184 | |
| 185 | #ifdef RLIMIT_DATA |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 186 | PyModule_AddIntConstant(m, "RLIMIT_DATA", RLIMIT_DATA); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 187 | #endif |
| 188 | |
| 189 | #ifdef RLIMIT_STACK |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 190 | PyModule_AddIntConstant(m, "RLIMIT_STACK", RLIMIT_STACK); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 191 | #endif |
| 192 | |
| 193 | #ifdef RLIMIT_CORE |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 194 | PyModule_AddIntConstant(m, "RLIMIT_CORE", RLIMIT_CORE); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 195 | #endif |
| 196 | |
| 197 | #ifdef RLIMIT_NOFILE |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 198 | PyModule_AddIntConstant(m, "RLIMIT_NOFILE", RLIMIT_NOFILE); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 199 | #endif |
| 200 | |
| 201 | #ifdef RLIMIT_OFILE |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 202 | PyModule_AddIntConstant(m, "RLIMIT_OFILE", RLIMIT_OFILE); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 203 | #endif |
| 204 | |
| 205 | #ifdef RLIMIT_VMEM |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 206 | PyModule_AddIntConstant(m, "RLIMIT_VMEM", RLIMIT_VMEM); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 207 | #endif |
| 208 | |
| 209 | #ifdef RLIMIT_AS |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 210 | PyModule_AddIntConstant(m, "RLIMIT_AS", RLIMIT_AS); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 211 | #endif |
| 212 | |
| 213 | #ifdef RLIMIT_RSS |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 214 | PyModule_AddIntConstant(m, "RLIMIT_RSS", RLIMIT_RSS); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 215 | #endif |
| 216 | |
| 217 | #ifdef RLIMIT_NPROC |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 218 | PyModule_AddIntConstant(m, "RLIMIT_NPROC", RLIMIT_NPROC); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 219 | #endif |
| 220 | |
| 221 | #ifdef RLIMIT_MEMLOCK |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 222 | PyModule_AddIntConstant(m, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 223 | #endif |
| 224 | |
| 225 | #ifdef RUSAGE_SELF |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 226 | PyModule_AddIntConstant(m, "RUSAGE_SELF", RUSAGE_SELF); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 227 | #endif |
| 228 | |
Guido van Rossum | ebd05eb | 1997-12-09 19:35:48 +0000 | [diff] [blame] | 229 | #ifdef RUSAGE_CHILDREN |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 230 | PyModule_AddIntConstant(m, "RUSAGE_CHILDREN", RUSAGE_CHILDREN); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 231 | #endif |
| 232 | |
| 233 | #ifdef RUSAGE_BOTH |
Fred Drake | cca657b | 2002-02-14 06:59:26 +0000 | [diff] [blame] | 234 | PyModule_AddIntConstant(m, "RUSAGE_BOTH", RUSAGE_BOTH); |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 235 | #endif |
Neil Schemenauer | 29ac3cb | 2002-03-24 22:27:39 +0000 | [diff] [blame^] | 236 | |
| 237 | #if defined(HAVE_LONG_LONG) |
| 238 | if (sizeof(RLIM_INFINITY) > sizeof(long)) { |
| 239 | v = PyLong_FromLongLong((LONG_LONG) RLIM_INFINITY); |
| 240 | } else |
| 241 | #endif |
| 242 | { |
| 243 | v = PyInt_FromLong((long) RLIM_INFINITY); |
| 244 | } |
| 245 | if (v) { |
| 246 | PyModule_AddObject(m, "RLIM_INFINITY", v); |
| 247 | } |
Guido van Rossum | 2e63139 | 1996-12-18 18:37:27 +0000 | [diff] [blame] | 248 | } |