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