Skip to content

Instantly share code, notes, and snippets.

@mosquito
Created October 30, 2020 15:03
Show Gist options
  • Save mosquito/882ab37acf77bf8f507e9acda076a33c to your computer and use it in GitHub Desktop.
Save mosquito/882ab37acf77bf8f507e9acda076a33c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import resource"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"LIMITS = {x: getattr(resource, x) for x in filter(lambda x: x.startswith(\"RLIMIT_\"), dir(resource))}\n",
"USAGES = {x: getattr(resource, x) for x in filter(lambda x: x.startswith(\"RUSAGE_\"), dir(resource))}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def test():\n",
" limits = {}\n",
" usage = {}\n",
" for name, item in LIMITS.items():\n",
" soft, hard = resource.getrlimit(item)\n",
" limits[name] = soft, hard\n",
" \n",
" usage_keys = (\n",
" 'utime', 'stime', 'maxrss', \n",
" 'ixrss', 'idrss', 'isrss', \n",
" 'minflt', 'majflt', 'nswap', \n",
" 'inblock', 'outblock', 'msgsnd',\n",
" 'msgrcv', 'nsignals', 'nvcsw', 'nivcsw'\n",
" )\n",
" \n",
" for name, item in USAGES.items():\n",
" usage[name] = dict(zip(usage_keys, resource.getrusage(item)))\n",
" \n",
" return limits, usage"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"33.3 µs ± 844 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%timeit test()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"({'RLIMIT_AS': (-1, -1),\n",
" 'RLIMIT_CORE': (0, -1),\n",
" 'RLIMIT_CPU': (-1, -1),\n",
" 'RLIMIT_DATA': (-1, -1),\n",
" 'RLIMIT_FSIZE': (-1, -1),\n",
" 'RLIMIT_MEMLOCK': (16777216, 16777216),\n",
" 'RLIMIT_MSGQUEUE': (819200, 819200),\n",
" 'RLIMIT_NICE': (0, 0),\n",
" 'RLIMIT_NOFILE': (4096, 1048576),\n",
" 'RLIMIT_NPROC': (-1, -1),\n",
" 'RLIMIT_OFILE': (4096, 1048576),\n",
" 'RLIMIT_RSS': (-1, -1),\n",
" 'RLIMIT_RTPRIO': (0, 0),\n",
" 'RLIMIT_RTTIME': (-1, -1),\n",
" 'RLIMIT_SIGPENDING': (63728, 63728),\n",
" 'RLIMIT_STACK': (8388608, -1)},\n",
" {'RUSAGE_CHILDREN': {'utime': 0.0,\n",
" 'stime': 0.002567,\n",
" 'maxrss': 36800,\n",
" 'ixrss': 0,\n",
" 'idrss': 0,\n",
" 'isrss': 0,\n",
" 'minflt': 152,\n",
" 'majflt': 0,\n",
" 'nswap': 0,\n",
" 'inblock': 0,\n",
" 'outblock': 0,\n",
" 'msgsnd': 0,\n",
" 'msgrcv': 0,\n",
" 'nsignals': 0,\n",
" 'nvcsw': 1,\n",
" 'nivcsw': 0},\n",
" 'RUSAGE_SELF': {'utime': 2.176746,\n",
" 'stime': 1.156351,\n",
" 'maxrss': 50896,\n",
" 'ixrss': 0,\n",
" 'idrss': 0,\n",
" 'isrss': 0,\n",
" 'minflt': 13106,\n",
" 'majflt': 0,\n",
" 'nswap': 0,\n",
" 'inblock': 5088,\n",
" 'outblock': 1648,\n",
" 'msgsnd': 0,\n",
" 'msgrcv': 0,\n",
" 'nsignals': 0,\n",
" 'nvcsw': 695,\n",
" 'nivcsw': 145},\n",
" 'RUSAGE_THREAD': {'utime': 2.163491,\n",
" 'stime': 1.144787,\n",
" 'maxrss': 50896,\n",
" 'ixrss': 0,\n",
" 'idrss': 0,\n",
" 'isrss': 0,\n",
" 'minflt': 12924,\n",
" 'majflt': 0,\n",
" 'nswap': 0,\n",
" 'inblock': 5088,\n",
" 'outblock': 360,\n",
" 'msgsnd': 0,\n",
" 'msgrcv': 0,\n",
" 'nsignals': 0,\n",
" 'nvcsw': 388,\n",
" 'nivcsw': 126}})"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9",
"language": "python",
"name": "python-3.9"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0+"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment