[2] | 1 |
|
---|
| 2 | #include "Python.h"
|
---|
| 3 | #include "structseq.h"
|
---|
| 4 | #include <sys/resource.h>
|
---|
| 5 | #include <sys/time.h>
|
---|
| 6 | #include <string.h>
|
---|
| 7 | #include <errno.h>
|
---|
| 8 | /* for sysconf */
|
---|
| 9 | #if defined(HAVE_UNISTD_H)
|
---|
| 10 | #include <unistd.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
| 13 | /* On some systems, these aren't in any header file.
|
---|
| 14 | On others they are, with inconsistent prototypes.
|
---|
| 15 | We declare the (default) return type, to shut up gcc -Wall;
|
---|
| 16 | but we can't declare the prototype, to avoid errors
|
---|
| 17 | when the header files declare it different.
|
---|
| 18 | Worse, on some Linuxes, getpagesize() returns a size_t... */
|
---|
| 19 |
|
---|
| 20 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
|
---|
| 21 |
|
---|
| 22 | static PyObject *ResourceError;
|
---|
| 23 |
|
---|
| 24 | PyDoc_STRVAR(struct_rusage__doc__,
|
---|
| 25 | "struct_rusage: Result from getrusage.\n\n"
|
---|
| 26 | "This object may be accessed either as a tuple of\n"
|
---|
| 27 | " (utime,stime,maxrss,ixrss,idrss,isrss,minflt,majflt,\n"
|
---|
| 28 | " nswap,inblock,oublock,msgsnd,msgrcv,nsignals,nvcsw,nivcsw)\n"
|
---|
| 29 | "or via the attributes ru_utime, ru_stime, ru_maxrss, and so on.");
|
---|
| 30 |
|
---|
| 31 | static PyStructSequence_Field struct_rusage_fields[] = {
|
---|
[391] | 32 | {"ru_utime", "user time used"},
|
---|
| 33 | {"ru_stime", "system time used"},
|
---|
| 34 | {"ru_maxrss", "max. resident set size"},
|
---|
| 35 | {"ru_ixrss", "shared memory size"},
|
---|
| 36 | {"ru_idrss", "unshared data size"},
|
---|
| 37 | {"ru_isrss", "unshared stack size"},
|
---|
| 38 | {"ru_minflt", "page faults not requiring I/O"},
|
---|
| 39 | {"ru_majflt", "page faults requiring I/O"},
|
---|
| 40 | {"ru_nswap", "number of swap outs"},
|
---|
| 41 | {"ru_inblock", "block input operations"},
|
---|
| 42 | {"ru_oublock", "block output operations"},
|
---|
| 43 | {"ru_msgsnd", "IPC messages sent"},
|
---|
| 44 | {"ru_msgrcv", "IPC messages received"},
|
---|
| 45 | {"ru_nsignals", "signals received"},
|
---|
| 46 | {"ru_nvcsw", "voluntary context switches"},
|
---|
| 47 | {"ru_nivcsw", "involuntary context switches"},
|
---|
| 48 | {0}
|
---|
[2] | 49 | };
|
---|
| 50 |
|
---|
| 51 | static PyStructSequence_Desc struct_rusage_desc = {
|
---|
[391] | 52 | "resource.struct_rusage", /* name */
|
---|
| 53 | struct_rusage__doc__, /* doc */
|
---|
| 54 | struct_rusage_fields, /* fields */
|
---|
| 55 | 16 /* n_in_sequence */
|
---|
[2] | 56 | };
|
---|
| 57 |
|
---|
| 58 | static int initialized;
|
---|
| 59 | static PyTypeObject StructRUsageType;
|
---|
| 60 |
|
---|
| 61 | static PyObject *
|
---|
| 62 | resource_getrusage(PyObject *self, PyObject *args)
|
---|
| 63 | {
|
---|
[391] | 64 | int who;
|
---|
| 65 | struct rusage ru;
|
---|
| 66 | PyObject *result;
|
---|
[2] | 67 |
|
---|
[391] | 68 | if (!PyArg_ParseTuple(args, "i:getrusage", &who))
|
---|
| 69 | return NULL;
|
---|
[2] | 70 |
|
---|
[10] | 71 | #ifndef __OS2__
|
---|
[391] | 72 | if (getrusage(who, &ru) == -1) {
|
---|
| 73 | if (errno == EINVAL) {
|
---|
| 74 | PyErr_SetString(PyExc_ValueError,
|
---|
| 75 | "invalid who parameter");
|
---|
| 76 | return NULL;
|
---|
| 77 | }
|
---|
| 78 | PyErr_SetFromErrno(ResourceError);
|
---|
| 79 | return NULL;
|
---|
| 80 | }
|
---|
[10] | 81 | #endif
|
---|
[2] | 82 |
|
---|
[391] | 83 | result = PyStructSequence_New(&StructRUsageType);
|
---|
| 84 | if (!result)
|
---|
| 85 | return NULL;
|
---|
[2] | 86 |
|
---|
[391] | 87 | PyStructSequence_SET_ITEM(result, 0,
|
---|
| 88 | PyFloat_FromDouble(doubletime(ru.ru_utime)));
|
---|
| 89 | PyStructSequence_SET_ITEM(result, 1,
|
---|
| 90 | PyFloat_FromDouble(doubletime(ru.ru_stime)));
|
---|
| 91 | PyStructSequence_SET_ITEM(result, 2, PyInt_FromLong(ru.ru_maxrss));
|
---|
| 92 | PyStructSequence_SET_ITEM(result, 3, PyInt_FromLong(ru.ru_ixrss));
|
---|
| 93 | PyStructSequence_SET_ITEM(result, 4, PyInt_FromLong(ru.ru_idrss));
|
---|
| 94 | PyStructSequence_SET_ITEM(result, 5, PyInt_FromLong(ru.ru_isrss));
|
---|
| 95 | PyStructSequence_SET_ITEM(result, 6, PyInt_FromLong(ru.ru_minflt));
|
---|
| 96 | PyStructSequence_SET_ITEM(result, 7, PyInt_FromLong(ru.ru_majflt));
|
---|
| 97 | PyStructSequence_SET_ITEM(result, 8, PyInt_FromLong(ru.ru_nswap));
|
---|
| 98 | PyStructSequence_SET_ITEM(result, 9, PyInt_FromLong(ru.ru_inblock));
|
---|
| 99 | PyStructSequence_SET_ITEM(result, 10, PyInt_FromLong(ru.ru_oublock));
|
---|
| 100 | PyStructSequence_SET_ITEM(result, 11, PyInt_FromLong(ru.ru_msgsnd));
|
---|
| 101 | PyStructSequence_SET_ITEM(result, 12, PyInt_FromLong(ru.ru_msgrcv));
|
---|
| 102 | PyStructSequence_SET_ITEM(result, 13, PyInt_FromLong(ru.ru_nsignals));
|
---|
| 103 | PyStructSequence_SET_ITEM(result, 14, PyInt_FromLong(ru.ru_nvcsw));
|
---|
| 104 | PyStructSequence_SET_ITEM(result, 15, PyInt_FromLong(ru.ru_nivcsw));
|
---|
[2] | 105 |
|
---|
[391] | 106 | if (PyErr_Occurred()) {
|
---|
| 107 | Py_DECREF(result);
|
---|
| 108 | return NULL;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | return result;
|
---|
[2] | 112 | }
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | static PyObject *
|
---|
| 116 | resource_getrlimit(PyObject *self, PyObject *args)
|
---|
| 117 | {
|
---|
[391] | 118 | struct rlimit rl;
|
---|
| 119 | int resource;
|
---|
[2] | 120 |
|
---|
[391] | 121 | if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))
|
---|
| 122 | return NULL;
|
---|
[2] | 123 |
|
---|
[391] | 124 | if (resource < 0 || resource >= RLIM_NLIMITS) {
|
---|
| 125 | PyErr_SetString(PyExc_ValueError,
|
---|
| 126 | "invalid resource specified");
|
---|
| 127 | return NULL;
|
---|
| 128 | }
|
---|
[2] | 129 |
|
---|
[391] | 130 | if (getrlimit(resource, &rl) == -1) {
|
---|
| 131 | PyErr_SetFromErrno(ResourceError);
|
---|
| 132 | return NULL;
|
---|
| 133 | }
|
---|
[2] | 134 |
|
---|
| 135 | #if defined(HAVE_LONG_LONG)
|
---|
[391] | 136 | if (sizeof(rl.rlim_cur) > sizeof(long)) {
|
---|
| 137 | return Py_BuildValue("LL",
|
---|
| 138 | (PY_LONG_LONG) rl.rlim_cur,
|
---|
| 139 | (PY_LONG_LONG) rl.rlim_max);
|
---|
| 140 | }
|
---|
[2] | 141 | #endif
|
---|
[391] | 142 | return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);
|
---|
[2] | 143 | }
|
---|
| 144 |
|
---|
| 145 | static PyObject *
|
---|
| 146 | resource_setrlimit(PyObject *self, PyObject *args)
|
---|
| 147 | {
|
---|
[391] | 148 | struct rlimit rl;
|
---|
| 149 | int resource;
|
---|
| 150 | PyObject *limits, *curobj, *maxobj;
|
---|
[2] | 151 |
|
---|
[391] | 152 | if (!PyArg_ParseTuple(args, "iO:setrlimit", &resource, &limits))
|
---|
| 153 | return NULL;
|
---|
[2] | 154 |
|
---|
[391] | 155 | if (resource < 0 || resource >= RLIM_NLIMITS) {
|
---|
| 156 | PyErr_SetString(PyExc_ValueError,
|
---|
| 157 | "invalid resource specified");
|
---|
| 158 | return NULL;
|
---|
| 159 | }
|
---|
[2] | 160 |
|
---|
[391] | 161 | limits = PySequence_Tuple(limits);
|
---|
| 162 | if (!limits)
|
---|
| 163 | /* Here limits is a borrowed reference */
|
---|
| 164 | return NULL;
|
---|
| 165 |
|
---|
| 166 | if (PyTuple_GET_SIZE(limits) != 2) {
|
---|
| 167 | PyErr_SetString(PyExc_ValueError,
|
---|
| 168 | "expected a tuple of 2 integers");
|
---|
| 169 | goto error;
|
---|
| 170 | }
|
---|
| 171 | curobj = PyTuple_GET_ITEM(limits, 0);
|
---|
| 172 | maxobj = PyTuple_GET_ITEM(limits, 1);
|
---|
| 173 |
|
---|
[2] | 174 | #if !defined(HAVE_LARGEFILE_SUPPORT)
|
---|
[391] | 175 | rl.rlim_cur = PyInt_AsLong(curobj);
|
---|
| 176 | if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
|
---|
| 177 | goto error;
|
---|
| 178 | rl.rlim_max = PyInt_AsLong(maxobj);
|
---|
| 179 | if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
|
---|
| 180 | goto error;
|
---|
[2] | 181 | #else
|
---|
[391] | 182 | /* The limits are probably bigger than a long */
|
---|
| 183 | rl.rlim_cur = PyLong_Check(curobj) ?
|
---|
| 184 | PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
|
---|
| 185 | if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
|
---|
| 186 | goto error;
|
---|
| 187 | rl.rlim_max = PyLong_Check(maxobj) ?
|
---|
| 188 | PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
|
---|
| 189 | if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
|
---|
| 190 | goto error;
|
---|
[2] | 191 | #endif
|
---|
| 192 |
|
---|
[391] | 193 | rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
|
---|
| 194 | rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
|
---|
| 195 | if (setrlimit(resource, &rl) == -1) {
|
---|
| 196 | if (errno == EINVAL)
|
---|
| 197 | PyErr_SetString(PyExc_ValueError,
|
---|
| 198 | "current limit exceeds maximum limit");
|
---|
| 199 | else if (errno == EPERM)
|
---|
| 200 | PyErr_SetString(PyExc_ValueError,
|
---|
| 201 | "not allowed to raise maximum limit");
|
---|
| 202 | else
|
---|
| 203 | PyErr_SetFromErrno(ResourceError);
|
---|
| 204 | goto error;
|
---|
| 205 | }
|
---|
| 206 | Py_DECREF(limits);
|
---|
| 207 | Py_INCREF(Py_None);
|
---|
| 208 | return Py_None;
|
---|
| 209 |
|
---|
| 210 | error:
|
---|
| 211 | Py_DECREF(limits);
|
---|
| 212 | return NULL;
|
---|
[2] | 213 | }
|
---|
| 214 |
|
---|
| 215 | static PyObject *
|
---|
| 216 | resource_getpagesize(PyObject *self, PyObject *unused)
|
---|
| 217 | {
|
---|
[391] | 218 | long pagesize = 0;
|
---|
[2] | 219 | #if defined(HAVE_GETPAGESIZE)
|
---|
[391] | 220 | pagesize = getpagesize();
|
---|
[2] | 221 | #elif defined(HAVE_SYSCONF)
|
---|
| 222 | #if defined(_SC_PAGE_SIZE)
|
---|
[391] | 223 | pagesize = sysconf(_SC_PAGE_SIZE);
|
---|
[2] | 224 | #else
|
---|
[391] | 225 | /* Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE */
|
---|
| 226 | pagesize = sysconf(_SC_PAGESIZE);
|
---|
[2] | 227 | #endif
|
---|
| 228 | #endif
|
---|
[391] | 229 | return Py_BuildValue("i", pagesize);
|
---|
[2] | 230 |
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | /* List of functions */
|
---|
| 234 |
|
---|
| 235 | static struct PyMethodDef
|
---|
| 236 | resource_methods[] = {
|
---|
[391] | 237 | {"getrusage", resource_getrusage, METH_VARARGS},
|
---|
| 238 | {"getrlimit", resource_getrlimit, METH_VARARGS},
|
---|
| 239 | {"setrlimit", resource_setrlimit, METH_VARARGS},
|
---|
| 240 | {"getpagesize", resource_getpagesize, METH_NOARGS},
|
---|
| 241 | {NULL, NULL} /* sentinel */
|
---|
[2] | 242 | };
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | /* Module initialization */
|
---|
| 246 |
|
---|
| 247 | PyMODINIT_FUNC
|
---|
| 248 | initresource(void)
|
---|
| 249 | {
|
---|
[391] | 250 | PyObject *m, *v;
|
---|
[2] | 251 |
|
---|
[391] | 252 | /* Create the module and add the functions */
|
---|
| 253 | m = Py_InitModule("resource", resource_methods);
|
---|
| 254 | if (m == NULL)
|
---|
| 255 | return;
|
---|
[2] | 256 |
|
---|
[391] | 257 | /* Add some symbolic constants to the module */
|
---|
| 258 | if (ResourceError == NULL) {
|
---|
| 259 | ResourceError = PyErr_NewException("resource.error",
|
---|
| 260 | NULL, NULL);
|
---|
| 261 | }
|
---|
| 262 | Py_INCREF(ResourceError);
|
---|
| 263 | PyModule_AddObject(m, "error", ResourceError);
|
---|
| 264 | if (!initialized)
|
---|
| 265 | PyStructSequence_InitType(&StructRUsageType,
|
---|
| 266 | &struct_rusage_desc);
|
---|
| 267 | Py_INCREF(&StructRUsageType);
|
---|
| 268 | PyModule_AddObject(m, "struct_rusage",
|
---|
| 269 | (PyObject*) &StructRUsageType);
|
---|
[2] | 270 |
|
---|
[391] | 271 | /* insert constants */
|
---|
[2] | 272 | #ifdef RLIMIT_CPU
|
---|
[391] | 273 | PyModule_AddIntConstant(m, "RLIMIT_CPU", RLIMIT_CPU);
|
---|
[2] | 274 | #endif
|
---|
| 275 |
|
---|
| 276 | #ifdef RLIMIT_FSIZE
|
---|
[391] | 277 | PyModule_AddIntConstant(m, "RLIMIT_FSIZE", RLIMIT_FSIZE);
|
---|
[2] | 278 | #endif
|
---|
| 279 |
|
---|
| 280 | #ifdef RLIMIT_DATA
|
---|
[391] | 281 | PyModule_AddIntConstant(m, "RLIMIT_DATA", RLIMIT_DATA);
|
---|
[2] | 282 | #endif
|
---|
| 283 |
|
---|
| 284 | #ifdef RLIMIT_STACK
|
---|
[391] | 285 | PyModule_AddIntConstant(m, "RLIMIT_STACK", RLIMIT_STACK);
|
---|
[2] | 286 | #endif
|
---|
| 287 |
|
---|
| 288 | #ifdef RLIMIT_CORE
|
---|
[391] | 289 | PyModule_AddIntConstant(m, "RLIMIT_CORE", RLIMIT_CORE);
|
---|
[2] | 290 | #endif
|
---|
| 291 |
|
---|
| 292 | #ifdef RLIMIT_NOFILE
|
---|
[391] | 293 | PyModule_AddIntConstant(m, "RLIMIT_NOFILE", RLIMIT_NOFILE);
|
---|
[2] | 294 | #endif
|
---|
| 295 |
|
---|
| 296 | #ifdef RLIMIT_OFILE
|
---|
[391] | 297 | PyModule_AddIntConstant(m, "RLIMIT_OFILE", RLIMIT_OFILE);
|
---|
[2] | 298 | #endif
|
---|
| 299 |
|
---|
| 300 | #ifdef RLIMIT_VMEM
|
---|
[391] | 301 | PyModule_AddIntConstant(m, "RLIMIT_VMEM", RLIMIT_VMEM);
|
---|
[2] | 302 | #endif
|
---|
| 303 |
|
---|
| 304 | #ifdef RLIMIT_AS
|
---|
[391] | 305 | PyModule_AddIntConstant(m, "RLIMIT_AS", RLIMIT_AS);
|
---|
[2] | 306 | #endif
|
---|
| 307 |
|
---|
| 308 | #ifdef RLIMIT_RSS
|
---|
[391] | 309 | PyModule_AddIntConstant(m, "RLIMIT_RSS", RLIMIT_RSS);
|
---|
[2] | 310 | #endif
|
---|
| 311 |
|
---|
| 312 | #ifdef RLIMIT_NPROC
|
---|
[391] | 313 | PyModule_AddIntConstant(m, "RLIMIT_NPROC", RLIMIT_NPROC);
|
---|
[2] | 314 | #endif
|
---|
| 315 |
|
---|
| 316 | #ifdef RLIMIT_MEMLOCK
|
---|
[391] | 317 | PyModule_AddIntConstant(m, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
|
---|
[2] | 318 | #endif
|
---|
| 319 |
|
---|
| 320 | #ifdef RLIMIT_SBSIZE
|
---|
[391] | 321 | PyModule_AddIntConstant(m, "RLIMIT_SBSIZE", RLIMIT_SBSIZE);
|
---|
[2] | 322 | #endif
|
---|
| 323 |
|
---|
| 324 | #ifdef RUSAGE_SELF
|
---|
[391] | 325 | PyModule_AddIntConstant(m, "RUSAGE_SELF", RUSAGE_SELF);
|
---|
[2] | 326 | #endif
|
---|
| 327 |
|
---|
| 328 | #ifdef RUSAGE_CHILDREN
|
---|
[391] | 329 | PyModule_AddIntConstant(m, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
|
---|
[2] | 330 | #endif
|
---|
| 331 |
|
---|
| 332 | #ifdef RUSAGE_BOTH
|
---|
[391] | 333 | PyModule_AddIntConstant(m, "RUSAGE_BOTH", RUSAGE_BOTH);
|
---|
[2] | 334 | #endif
|
---|
| 335 |
|
---|
| 336 | #if defined(HAVE_LONG_LONG)
|
---|
[391] | 337 | if (sizeof(RLIM_INFINITY) > sizeof(long)) {
|
---|
| 338 | v = PyLong_FromLongLong((PY_LONG_LONG) RLIM_INFINITY);
|
---|
| 339 | } else
|
---|
[2] | 340 | #endif
|
---|
[391] | 341 | {
|
---|
| 342 | v = PyInt_FromLong((long) RLIM_INFINITY);
|
---|
| 343 | }
|
---|
| 344 | if (v) {
|
---|
| 345 | PyModule_AddObject(m, "RLIM_INFINITY", v);
|
---|
| 346 | }
|
---|
| 347 | initialized = 1;
|
---|
[2] | 348 | }
|
---|