From 565a03db5df7eac76219d4ebc96f02827c1e4232 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Sun, 1 Mar 2020 10:09:42 -0500 Subject: [PATCH] refactor: make resource limits function --- runtime/src/main.c | 47 ++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/runtime/src/main.c b/runtime/src/main.c index a5cb866..e233235 100644 --- a/runtime/src/main.c +++ b/runtime/src/main.c @@ -32,7 +32,6 @@ get_time() return (Tp.tv_sec * 1000000 + Tp.tv_usec); } - static void usage(char *cmd) { @@ -40,6 +39,30 @@ usage(char *cmd) debuglog("%s \n", cmd); } +// Sets the process data segment (RLIMIT_DATA) and # file descriptors +// (RLIMIT_NOFILE) soft limit to its hard limit (see man getrlimit) +void set_resource_limits_to_max(){ + struct rlimit resource_limit; + if (getrlimit(RLIMIT_DATA, &resource_limit) < 0) { + perror("getrlimit RLIMIT_DATA"); + exit(-1); + } + resource_limit.rlim_cur = resource_limit.rlim_max; + if (setrlimit(RLIMIT_DATA, &resource_limit) < 0) { + perror("setrlimit RLIMIT_DATA"); + exit(-1); + } + if (getrlimit(RLIMIT_NOFILE, &resource_limit) < 0) { + perror("getrlimit RLIMIT_NOFILE"); + exit(-1); + } + resource_limit.rlim_cur = resource_limit.rlim_max; + if (setrlimit(RLIMIT_NOFILE, &resource_limit) < 0) { + perror("setrlimit RLIMIT_NOFILE"); + exit(-1); + } +} + int main(int argc, char **argv) { @@ -57,27 +80,7 @@ main(int argc, char **argv) exit(-1); } - // Sets the process data segment (RLIMIT_DATA) and # file descriptors - // (RLIMIT_NOFILE) soft limit to its hard limit (see man getrlimit) - struct rlimit r; - if (getrlimit(RLIMIT_DATA, &r) < 0) { - perror("getrlimit RLIMIT_DATA"); - exit(-1); - } - r.rlim_cur = r.rlim_max; - if (setrlimit(RLIMIT_DATA, &r) < 0) { - perror("setrlimit RLIMIT_DATA"); - exit(-1); - } - if (getrlimit(RLIMIT_NOFILE, &r) < 0) { - perror("getrlimit RLIMIT_NOFILE"); - exit(-1); - } - r.rlim_cur = r.rlim_max; - if (setrlimit(RLIMIT_NOFILE, &r) < 0) { - perror("setrlimit RLIMIT_NOFILE"); - exit(-1); - } + set_resource_limits_to_max(); // Find the number of processors currently online ncores = sysconf(_SC_NPROCESSORS_ONLN);