+++ /dev/null
-\r
-#include "Python.h"\r
-#include "structseq.h"\r
-#include <sys/resource.h>\r
-#include <sys/time.h>\r
-#include <string.h>\r
-#include <errno.h>\r
-/* for sysconf */\r
-#if defined(HAVE_UNISTD_H)\r
-#include <unistd.h>\r
-#endif\r
-\r
-/* On some systems, these aren't in any header file.\r
- On others they are, with inconsistent prototypes.\r
- We declare the (default) return type, to shut up gcc -Wall;\r
- but we can't declare the prototype, to avoid errors\r
- when the header files declare it different.\r
- Worse, on some Linuxes, getpagesize() returns a size_t... */\r
-\r
-#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)\r
-\r
-static PyObject *ResourceError;\r
-\r
-PyDoc_STRVAR(struct_rusage__doc__,\r
-"struct_rusage: Result from getrusage.\n\n"\r
-"This object may be accessed either as a tuple of\n"\r
-" (utime,stime,maxrss,ixrss,idrss,isrss,minflt,majflt,\n"\r
-" nswap,inblock,oublock,msgsnd,msgrcv,nsignals,nvcsw,nivcsw)\n"\r
-"or via the attributes ru_utime, ru_stime, ru_maxrss, and so on.");\r
-\r
-static PyStructSequence_Field struct_rusage_fields[] = {\r
- {"ru_utime", "user time used"},\r
- {"ru_stime", "system time used"},\r
- {"ru_maxrss", "max. resident set size"},\r
- {"ru_ixrss", "shared memory size"},\r
- {"ru_idrss", "unshared data size"},\r
- {"ru_isrss", "unshared stack size"},\r
- {"ru_minflt", "page faults not requiring I/O"},\r
- {"ru_majflt", "page faults requiring I/O"},\r
- {"ru_nswap", "number of swap outs"},\r
- {"ru_inblock", "block input operations"},\r
- {"ru_oublock", "block output operations"},\r
- {"ru_msgsnd", "IPC messages sent"},\r
- {"ru_msgrcv", "IPC messages received"},\r
- {"ru_nsignals", "signals received"},\r
- {"ru_nvcsw", "voluntary context switches"},\r
- {"ru_nivcsw", "involuntary context switches"},\r
- {0}\r
-};\r
-\r
-static PyStructSequence_Desc struct_rusage_desc = {\r
- "resource.struct_rusage", /* name */\r
- struct_rusage__doc__, /* doc */\r
- struct_rusage_fields, /* fields */\r
- 16 /* n_in_sequence */\r
-};\r
-\r
-static int initialized;\r
-static PyTypeObject StructRUsageType;\r
-\r
-static PyObject *\r
-resource_getrusage(PyObject *self, PyObject *args)\r
-{\r
- int who;\r
- struct rusage ru;\r
- PyObject *result;\r
-\r
- if (!PyArg_ParseTuple(args, "i:getrusage", &who))\r
- return NULL;\r
-\r
- if (getrusage(who, &ru) == -1) {\r
- if (errno == EINVAL) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "invalid who parameter");\r
- return NULL;\r
- }\r
- PyErr_SetFromErrno(ResourceError);\r
- return NULL;\r
- }\r
-\r
- result = PyStructSequence_New(&StructRUsageType);\r
- if (!result)\r
- return NULL;\r
-\r
- PyStructSequence_SET_ITEM(result, 0,\r
- PyFloat_FromDouble(doubletime(ru.ru_utime)));\r
- PyStructSequence_SET_ITEM(result, 1,\r
- PyFloat_FromDouble(doubletime(ru.ru_stime)));\r
- PyStructSequence_SET_ITEM(result, 2, PyInt_FromLong(ru.ru_maxrss));\r
- PyStructSequence_SET_ITEM(result, 3, PyInt_FromLong(ru.ru_ixrss));\r
- PyStructSequence_SET_ITEM(result, 4, PyInt_FromLong(ru.ru_idrss));\r
- PyStructSequence_SET_ITEM(result, 5, PyInt_FromLong(ru.ru_isrss));\r
- PyStructSequence_SET_ITEM(result, 6, PyInt_FromLong(ru.ru_minflt));\r
- PyStructSequence_SET_ITEM(result, 7, PyInt_FromLong(ru.ru_majflt));\r
- PyStructSequence_SET_ITEM(result, 8, PyInt_FromLong(ru.ru_nswap));\r
- PyStructSequence_SET_ITEM(result, 9, PyInt_FromLong(ru.ru_inblock));\r
- PyStructSequence_SET_ITEM(result, 10, PyInt_FromLong(ru.ru_oublock));\r
- PyStructSequence_SET_ITEM(result, 11, PyInt_FromLong(ru.ru_msgsnd));\r
- PyStructSequence_SET_ITEM(result, 12, PyInt_FromLong(ru.ru_msgrcv));\r
- PyStructSequence_SET_ITEM(result, 13, PyInt_FromLong(ru.ru_nsignals));\r
- PyStructSequence_SET_ITEM(result, 14, PyInt_FromLong(ru.ru_nvcsw));\r
- PyStructSequence_SET_ITEM(result, 15, PyInt_FromLong(ru.ru_nivcsw));\r
-\r
- if (PyErr_Occurred()) {\r
- Py_DECREF(result);\r
- return NULL;\r
- }\r
-\r
- return result;\r
-}\r
-\r
-\r
-static PyObject *\r
-resource_getrlimit(PyObject *self, PyObject *args)\r
-{\r
- struct rlimit rl;\r
- int resource;\r
-\r
- if (!PyArg_ParseTuple(args, "i:getrlimit", &resource))\r
- return NULL;\r
-\r
- if (resource < 0 || resource >= RLIM_NLIMITS) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "invalid resource specified");\r
- return NULL;\r
- }\r
-\r
- if (getrlimit(resource, &rl) == -1) {\r
- PyErr_SetFromErrno(ResourceError);\r
- return NULL;\r
- }\r
-\r
-#if defined(HAVE_LONG_LONG)\r
- if (sizeof(rl.rlim_cur) > sizeof(long)) {\r
- return Py_BuildValue("LL",\r
- (PY_LONG_LONG) rl.rlim_cur,\r
- (PY_LONG_LONG) rl.rlim_max);\r
- }\r
-#endif\r
- return Py_BuildValue("ll", (long) rl.rlim_cur, (long) rl.rlim_max);\r
-}\r
-\r
-static PyObject *\r
-resource_setrlimit(PyObject *self, PyObject *args)\r
-{\r
- struct rlimit rl;\r
- int resource;\r
- PyObject *curobj, *maxobj;\r
-\r
- if (!PyArg_ParseTuple(args, "i(OO):setrlimit",\r
- &resource, &curobj, &maxobj))\r
- return NULL;\r
-\r
- if (resource < 0 || resource >= RLIM_NLIMITS) {\r
- PyErr_SetString(PyExc_ValueError,\r
- "invalid resource specified");\r
- return NULL;\r
- }\r
-\r
-#if !defined(HAVE_LARGEFILE_SUPPORT)\r
- rl.rlim_cur = PyInt_AsLong(curobj);\r
- if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())\r
- return NULL;\r
- rl.rlim_max = PyInt_AsLong(maxobj);\r
- if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())\r
- return NULL;\r
-#else\r
- /* The limits are probably bigger than a long */\r
- rl.rlim_cur = PyLong_Check(curobj) ?\r
- PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);\r
- if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())\r
- return NULL;\r
- rl.rlim_max = PyLong_Check(maxobj) ?\r
- PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);\r
- if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())\r
- return NULL;\r
-#endif\r
-\r
- rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;\r
- rl.rlim_max = rl.rlim_max & RLIM_INFINITY;\r
- if (setrlimit(resource, &rl) == -1) {\r
- if (errno == EINVAL)\r
- PyErr_SetString(PyExc_ValueError,\r
- "current limit exceeds maximum limit");\r
- else if (errno == EPERM)\r
- PyErr_SetString(PyExc_ValueError,\r
- "not allowed to raise maximum limit");\r
- else\r
- PyErr_SetFromErrno(ResourceError);\r
- return NULL;\r
- }\r
- Py_INCREF(Py_None);\r
- return Py_None;\r
-}\r
-\r
-static PyObject *\r
-resource_getpagesize(PyObject *self, PyObject *unused)\r
-{\r
- long pagesize = 0;\r
-#if defined(HAVE_GETPAGESIZE)\r
- pagesize = getpagesize();\r
-#elif defined(HAVE_SYSCONF)\r
-#if defined(_SC_PAGE_SIZE)\r
- pagesize = sysconf(_SC_PAGE_SIZE);\r
-#else\r
- /* Irix 5.3 has _SC_PAGESIZE, but not _SC_PAGE_SIZE */\r
- pagesize = sysconf(_SC_PAGESIZE);\r
-#endif\r
-#endif\r
- return Py_BuildValue("i", pagesize);\r
-\r
-}\r
-\r
-/* List of functions */\r
-\r
-static struct PyMethodDef\r
-resource_methods[] = {\r
- {"getrusage", resource_getrusage, METH_VARARGS},\r
- {"getrlimit", resource_getrlimit, METH_VARARGS},\r
- {"setrlimit", resource_setrlimit, METH_VARARGS},\r
- {"getpagesize", resource_getpagesize, METH_NOARGS},\r
- {NULL, NULL} /* sentinel */\r
-};\r
-\r
-\r
-/* Module initialization */\r
-\r
-PyMODINIT_FUNC\r
-initresource(void)\r
-{\r
- PyObject *m, *v;\r
-\r
- /* Create the module and add the functions */\r
- m = Py_InitModule("resource", resource_methods);\r
- if (m == NULL)\r
- return;\r
-\r
- /* Add some symbolic constants to the module */\r
- if (ResourceError == NULL) {\r
- ResourceError = PyErr_NewException("resource.error",\r
- NULL, NULL);\r
- }\r
- Py_INCREF(ResourceError);\r
- PyModule_AddObject(m, "error", ResourceError);\r
- if (!initialized)\r
- PyStructSequence_InitType(&StructRUsageType,\r
- &struct_rusage_desc);\r
- Py_INCREF(&StructRUsageType);\r
- PyModule_AddObject(m, "struct_rusage",\r
- (PyObject*) &StructRUsageType);\r
-\r
- /* insert constants */\r
-#ifdef RLIMIT_CPU\r
- PyModule_AddIntConstant(m, "RLIMIT_CPU", RLIMIT_CPU);\r
-#endif\r
-\r
-#ifdef RLIMIT_FSIZE\r
- PyModule_AddIntConstant(m, "RLIMIT_FSIZE", RLIMIT_FSIZE);\r
-#endif\r
-\r
-#ifdef RLIMIT_DATA\r
- PyModule_AddIntConstant(m, "RLIMIT_DATA", RLIMIT_DATA);\r
-#endif\r
-\r
-#ifdef RLIMIT_STACK\r
- PyModule_AddIntConstant(m, "RLIMIT_STACK", RLIMIT_STACK);\r
-#endif\r
-\r
-#ifdef RLIMIT_CORE\r
- PyModule_AddIntConstant(m, "RLIMIT_CORE", RLIMIT_CORE);\r
-#endif\r
-\r
-#ifdef RLIMIT_NOFILE\r
- PyModule_AddIntConstant(m, "RLIMIT_NOFILE", RLIMIT_NOFILE);\r
-#endif\r
-\r
-#ifdef RLIMIT_OFILE\r
- PyModule_AddIntConstant(m, "RLIMIT_OFILE", RLIMIT_OFILE);\r
-#endif\r
-\r
-#ifdef RLIMIT_VMEM\r
- PyModule_AddIntConstant(m, "RLIMIT_VMEM", RLIMIT_VMEM);\r
-#endif\r
-\r
-#ifdef RLIMIT_AS\r
- PyModule_AddIntConstant(m, "RLIMIT_AS", RLIMIT_AS);\r
-#endif\r
-\r
-#ifdef RLIMIT_RSS\r
- PyModule_AddIntConstant(m, "RLIMIT_RSS", RLIMIT_RSS);\r
-#endif\r
-\r
-#ifdef RLIMIT_NPROC\r
- PyModule_AddIntConstant(m, "RLIMIT_NPROC", RLIMIT_NPROC);\r
-#endif\r
-\r
-#ifdef RLIMIT_MEMLOCK\r
- PyModule_AddIntConstant(m, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);\r
-#endif\r
-\r
-#ifdef RLIMIT_SBSIZE\r
- PyModule_AddIntConstant(m, "RLIMIT_SBSIZE", RLIMIT_SBSIZE);\r
-#endif\r
-\r
-#ifdef RUSAGE_SELF\r
- PyModule_AddIntConstant(m, "RUSAGE_SELF", RUSAGE_SELF);\r
-#endif\r
-\r
-#ifdef RUSAGE_CHILDREN\r
- PyModule_AddIntConstant(m, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);\r
-#endif\r
-\r
-#ifdef RUSAGE_BOTH\r
- PyModule_AddIntConstant(m, "RUSAGE_BOTH", RUSAGE_BOTH);\r
-#endif\r
-\r
-#if defined(HAVE_LONG_LONG)\r
- if (sizeof(RLIM_INFINITY) > sizeof(long)) {\r
- v = PyLong_FromLongLong((PY_LONG_LONG) RLIM_INFINITY);\r
- } else\r
-#endif\r
- {\r
- v = PyInt_FromLong((long) RLIM_INFINITY);\r
- }\r
- if (v) {\r
- PyModule_AddObject(m, "RLIM_INFINITY", v);\r
- }\r
- initialized = 1;\r
-}\r