2015-05-27 22:39:42 +03:00
|
|
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
|
2007-04-23 22:53:47 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2015-06-24 06:59:57 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
2007-04-23 22:53:47 +04:00
|
|
|
* University of Stuttgart. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2009-01-11 05:30:00 +03:00
|
|
|
* Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
|
2015-05-27 22:39:42 +03:00
|
|
|
* Copyright (c) 2013-2015 Los Alamos National Security, LLC. All rights
|
|
|
|
* reserved.
|
2014-08-02 22:38:16 +04:00
|
|
|
* Copyright (c) 2014 Intel, Inc. All rights reserved.
|
2015-02-13 08:28:59 +03:00
|
|
|
* Copyright (c) 2015 Research Organization for Information Science
|
|
|
|
* and Technology (RIST). All rights reserved.
|
2020-05-14 06:54:16 +03:00
|
|
|
* Copyright (c) 2020 Mellanox Technologies, Inc.
|
|
|
|
* All rights reserved.
|
2007-04-23 22:53:47 +04:00
|
|
|
* $COPYRIGHT$
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2007-04-23 22:53:47 +04:00
|
|
|
* Additional copyrights may follow
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2007-04-23 22:53:47 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opal_config.h"
|
|
|
|
|
2009-04-29 04:49:23 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
2007-04-23 22:53:47 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
2014-08-02 22:38:16 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2007-04-23 22:53:47 +04:00
|
|
|
|
2009-03-13 05:10:32 +03:00
|
|
|
#include "opal/constants.h"
|
2013-03-28 01:09:41 +04:00
|
|
|
#include "opal/runtime/opal_params.h"
|
2007-04-23 22:53:47 +04:00
|
|
|
|
|
|
|
#include "opal/util/sys_limits.h"
|
2013-04-04 20:00:17 +04:00
|
|
|
#include "opal/util/show_help.h"
|
2007-04-24 18:27:51 +04:00
|
|
|
#include "opal/util/output.h"
|
2013-04-03 22:57:53 +04:00
|
|
|
#include "opal/util/argv.h"
|
2007-04-23 22:53:47 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create and initialize storage for the system limits
|
|
|
|
*/
|
|
|
|
OPAL_DECLSPEC opal_sys_limits_t opal_sys_limits = {
|
|
|
|
/* initialized = */ false,
|
|
|
|
/* num_files = */ -1,
|
|
|
|
/* num_procs = */ -1,
|
|
|
|
/* file_size = */ 0
|
|
|
|
};
|
|
|
|
|
2013-11-13 19:40:34 +04:00
|
|
|
static int opal_setlimit(int resource, char *value, rlim_t *out)
|
2007-04-23 22:53:47 +04:00
|
|
|
{
|
2011-05-17 07:27:43 +04:00
|
|
|
struct rlimit rlim, rlim_set;
|
2013-04-03 22:57:53 +04:00
|
|
|
rlim_t maxlim;
|
|
|
|
|
|
|
|
rlim.rlim_cur = 0;
|
|
|
|
|
|
|
|
if (0 == strcmp(value, "max")) {
|
|
|
|
maxlim = -1;
|
|
|
|
} else if (0 == strncmp(value, "unlimited", strlen(value))) {
|
|
|
|
maxlim = RLIM_INFINITY;
|
|
|
|
} else {
|
|
|
|
maxlim = strtol(value, NULL, 10);
|
|
|
|
}
|
2007-04-23 22:53:47 +04:00
|
|
|
|
2013-04-04 20:00:17 +04:00
|
|
|
if (0 <= getrlimit(resource, &rlim)) {
|
2013-11-13 19:40:34 +04:00
|
|
|
if (rlim.rlim_max < maxlim) {
|
2020-05-14 06:54:16 +03:00
|
|
|
rlim_set.rlim_cur = rlim.rlim_max;
|
2011-05-17 07:27:43 +04:00
|
|
|
rlim_set.rlim_max = rlim.rlim_max;
|
2013-04-03 22:57:53 +04:00
|
|
|
} else {
|
|
|
|
rlim_set.rlim_cur = maxlim;
|
|
|
|
rlim_set.rlim_max = maxlim;
|
|
|
|
}
|
2013-04-04 20:00:17 +04:00
|
|
|
if (0 <= setrlimit(resource, &rlim_set)) {
|
|
|
|
rlim.rlim_cur = rlim_set.rlim_cur;
|
|
|
|
} else if (RLIM_INFINITY == maxlim) {
|
|
|
|
/* if unlimited wasn't allowed, try to set
|
|
|
|
* to max allowed
|
|
|
|
*/
|
|
|
|
rlim_set.rlim_cur = rlim.rlim_max;
|
|
|
|
rlim_set.rlim_max = rlim.rlim_max;
|
|
|
|
if (0 <= setrlimit(resource, &rlim_set)) {
|
|
|
|
rlim.rlim_cur = rlim_set.rlim_cur;
|
|
|
|
} else {
|
2013-11-13 19:40:34 +04:00
|
|
|
return OPAL_ERROR;
|
2013-04-04 20:00:17 +04:00
|
|
|
}
|
|
|
|
} else {
|
2013-11-13 19:40:34 +04:00
|
|
|
return OPAL_ERROR;
|
2007-04-23 22:53:47 +04:00
|
|
|
}
|
2013-04-04 20:00:17 +04:00
|
|
|
} else {
|
2013-11-13 19:40:34 +04:00
|
|
|
return OPAL_ERROR;
|
2013-04-03 22:57:53 +04:00
|
|
|
}
|
2013-11-13 19:40:34 +04:00
|
|
|
*out = rlim.rlim_cur;
|
|
|
|
return OPAL_SUCCESS;
|
2013-04-03 22:57:53 +04:00
|
|
|
}
|
|
|
|
|
2013-04-04 20:00:17 +04:00
|
|
|
int opal_util_init_sys_limits(char **errmsg)
|
2013-04-03 22:57:53 +04:00
|
|
|
{
|
2013-05-15 02:00:01 +04:00
|
|
|
char **lims, **lim=NULL, *setlim;
|
2015-02-13 08:28:59 +03:00
|
|
|
int i, rc = OPAL_ERROR;
|
2013-11-13 19:40:34 +04:00
|
|
|
rlim_t value;
|
2013-04-03 22:57:53 +04:00
|
|
|
|
|
|
|
/* if limits were not given, then nothing to do */
|
|
|
|
if (NULL == opal_set_max_sys_limits) {
|
|
|
|
return OPAL_SUCCESS;
|
2007-04-23 22:53:47 +04:00
|
|
|
}
|
2007-04-25 05:54:37 +04:00
|
|
|
|
2013-04-03 22:57:53 +04:00
|
|
|
/* parse the requested limits to set */
|
|
|
|
lims = opal_argv_split(opal_set_max_sys_limits, ',');
|
2015-05-27 22:39:42 +03:00
|
|
|
if (NULL == lims) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
2013-04-03 22:57:53 +04:00
|
|
|
|
|
|
|
/* each limit is expressed as a "param:value" pair */
|
|
|
|
for (i=0; NULL != lims[i]; i++) {
|
|
|
|
lim = opal_argv_split(lims[i], ':');
|
|
|
|
if (1 == opal_argv_count(lim)) {
|
|
|
|
setlim = "max";
|
|
|
|
} else {
|
|
|
|
setlim = lim[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for historical reasons, a value of "1" means
|
|
|
|
* that we set the limits on #files, #children,
|
|
|
|
* and max file size
|
|
|
|
*/
|
|
|
|
if (0 == strcmp(lim[0], "1")) {
|
2013-12-20 18:24:26 +04:00
|
|
|
#if HAVE_DECL_RLIMIT_NOFILE
|
2013-11-13 19:40:34 +04:00
|
|
|
if (OPAL_SUCCESS !=
|
|
|
|
opal_setlimit(RLIMIT_NOFILE, "max", &value)) {
|
2013-04-04 20:00:17 +04:00
|
|
|
*errmsg = opal_show_help_string("help-opal-util.txt", "sys-limit-failed", true, "openfiles", "max");
|
2015-02-13 08:28:59 +03:00
|
|
|
goto out;
|
2013-04-04 20:00:17 +04:00
|
|
|
}
|
2013-11-13 19:40:34 +04:00
|
|
|
opal_sys_limits.num_files = value;
|
2013-12-20 07:18:07 +04:00
|
|
|
#endif
|
2007-04-25 15:56:31 +04:00
|
|
|
#if HAVE_DECL_RLIMIT_NPROC
|
2013-11-13 19:40:34 +04:00
|
|
|
if (OPAL_SUCCESS != opal_setlimit(RLIMIT_NPROC, "max", &value)) {
|
2013-04-04 20:00:17 +04:00
|
|
|
*errmsg = opal_show_help_string("help-opal-util.txt", "sys-limit-failed", true, "maxchildren", "max");
|
2015-02-13 08:28:59 +03:00
|
|
|
goto out;
|
2013-04-04 20:00:17 +04:00
|
|
|
}
|
2013-11-13 19:40:34 +04:00
|
|
|
opal_sys_limits.num_procs = value;
|
2013-04-03 22:57:53 +04:00
|
|
|
#endif
|
2013-12-20 18:24:26 +04:00
|
|
|
#if HAVE_DECL_RLIMIT_FSIZE
|
2013-11-13 19:40:34 +04:00
|
|
|
if (OPAL_SUCCESS !=
|
|
|
|
opal_setlimit(RLIMIT_FSIZE, "max", &value)) {
|
2013-04-04 20:00:17 +04:00
|
|
|
*errmsg = opal_show_help_string("help-opal-util.txt", "sys-limit-failed", true, "filesize", "max");
|
2015-02-13 08:28:59 +03:00
|
|
|
goto out;
|
2013-04-04 20:00:17 +04:00
|
|
|
}
|
2013-11-13 19:40:34 +04:00
|
|
|
opal_sys_limits.file_size = value;
|
2013-12-20 18:09:43 +04:00
|
|
|
#endif
|
2013-04-03 22:57:53 +04:00
|
|
|
break;
|
|
|
|
} else if (0 == strcmp(lim[0], "0")) {
|
|
|
|
/* user didn't want anything set */
|
2013-12-20 18:09:43 +04:00
|
|
|
break;
|
2007-04-23 22:53:47 +04:00
|
|
|
}
|
2013-12-20 07:18:07 +04:00
|
|
|
|
2013-04-03 22:57:53 +04:00
|
|
|
/* process them separately */
|
|
|
|
if (0 == strcmp(lim[0], "core")) {
|
2013-12-20 18:24:26 +04:00
|
|
|
#if HAVE_DECL_RLIMIT_CORE
|
2013-11-13 19:40:34 +04:00
|
|
|
if (OPAL_SUCCESS != opal_setlimit(RLIMIT_CORE, setlim, &value)) {
|
2013-04-04 20:00:17 +04:00
|
|
|
*errmsg = opal_show_help_string("help-opal-util.txt", "sys-limit-failed", true, "openfiles", setlim);
|
2015-02-13 08:28:59 +03:00
|
|
|
goto out;
|
2013-04-04 20:00:17 +04:00
|
|
|
}
|
2013-12-20 07:18:07 +04:00
|
|
|
#endif
|
2013-04-03 22:57:53 +04:00
|
|
|
} else if (0 == strcmp(lim[0], "filesize")) {
|
2013-12-20 18:24:26 +04:00
|
|
|
#if HAVE_DECL_RLIMIT_FSIZE
|
2013-11-13 19:40:34 +04:00
|
|
|
if (OPAL_SUCCESS != opal_setlimit(RLIMIT_FSIZE, setlim, &value)) {
|
2013-04-04 20:00:17 +04:00
|
|
|
*errmsg = opal_show_help_string("help-opal-util.txt", "sys-limit-failed", true, "filesize", setlim);
|
2015-02-13 08:28:59 +03:00
|
|
|
goto out;
|
2013-04-04 20:00:17 +04:00
|
|
|
}
|
2013-11-13 19:40:34 +04:00
|
|
|
opal_sys_limits.file_size = value;
|
2013-12-20 07:18:07 +04:00
|
|
|
#endif
|
2013-04-03 22:57:53 +04:00
|
|
|
} else if (0 == strcmp(lim[0], "maxmem")) {
|
2013-12-20 18:24:26 +04:00
|
|
|
#if HAVE_DECL_RLIMIT_AS
|
2013-11-13 19:40:34 +04:00
|
|
|
if (OPAL_SUCCESS != opal_setlimit(RLIMIT_AS, setlim, &value)) {
|
2013-04-04 20:00:17 +04:00
|
|
|
*errmsg = opal_show_help_string("help-opal-util.txt", "sys-limit-failed", true, "maxmem", setlim);
|
2015-02-13 08:28:59 +03:00
|
|
|
goto out;
|
2013-04-04 20:00:17 +04:00
|
|
|
}
|
2013-12-20 07:18:07 +04:00
|
|
|
#endif
|
2013-04-03 22:57:53 +04:00
|
|
|
} else if (0 == strcmp(lim[0], "openfiles")) {
|
2013-12-20 18:24:26 +04:00
|
|
|
#if HAVE_DECL_RLIMIT_NOFILE
|
2013-11-13 19:40:34 +04:00
|
|
|
if (OPAL_SUCCESS != opal_setlimit(RLIMIT_NOFILE, setlim, &value)) {
|
2013-04-04 20:00:17 +04:00
|
|
|
*errmsg = opal_show_help_string("help-opal-util.txt", "sys-limit-failed", true, "openfiles", setlim);
|
2015-02-13 08:28:59 +03:00
|
|
|
goto out;
|
2013-04-04 20:00:17 +04:00
|
|
|
}
|
2013-11-13 19:40:34 +04:00
|
|
|
opal_sys_limits.num_files = value;
|
2013-12-20 07:18:07 +04:00
|
|
|
#endif
|
2013-04-03 22:57:53 +04:00
|
|
|
} else if (0 == strcmp(lim[0], "stacksize")) {
|
2013-12-20 18:24:26 +04:00
|
|
|
#if HAVE_DECL_RLIMIT_STACK
|
2013-11-13 19:40:34 +04:00
|
|
|
if (OPAL_SUCCESS != opal_setlimit(RLIMIT_STACK, setlim, &value)) {
|
2013-04-04 20:00:17 +04:00
|
|
|
*errmsg = opal_show_help_string("help-opal-util.txt", "sys-limit-failed", true, "stacksize", setlim);
|
2015-02-13 08:28:59 +03:00
|
|
|
goto out;
|
2013-04-04 20:00:17 +04:00
|
|
|
}
|
2013-12-20 07:18:07 +04:00
|
|
|
#endif
|
2013-04-03 22:57:53 +04:00
|
|
|
} else if (0 == strcmp(lim[0], "maxchildren")) {
|
2013-12-20 07:18:07 +04:00
|
|
|
#if HAVE_DECL_RLIMIT_NPROC
|
2013-11-13 19:40:34 +04:00
|
|
|
if (OPAL_SUCCESS != opal_setlimit(RLIMIT_NPROC, setlim, &value)) {
|
2013-04-04 20:00:17 +04:00
|
|
|
*errmsg = opal_show_help_string("help-opal-util.txt", "sys-limit-failed", true, "maxchildren", setlim);
|
2015-02-13 08:28:59 +03:00
|
|
|
goto out;
|
2013-04-04 20:00:17 +04:00
|
|
|
}
|
2013-11-13 19:40:34 +04:00
|
|
|
opal_sys_limits.num_procs = value;
|
2007-04-25 05:54:37 +04:00
|
|
|
#endif
|
2013-04-03 22:57:53 +04:00
|
|
|
} else {
|
2013-04-04 20:00:17 +04:00
|
|
|
*errmsg = opal_show_help_string("help-opal-util.txt", "sys-limit-unrecognized", true, lim[0], setlim);
|
2015-02-13 08:28:59 +03:00
|
|
|
goto out;
|
2007-04-23 22:53:47 +04:00
|
|
|
}
|
2015-02-16 05:05:35 +03:00
|
|
|
opal_argv_free(lim);
|
|
|
|
lim = NULL;
|
2007-04-23 22:53:47 +04:00
|
|
|
}
|
2013-04-03 22:57:53 +04:00
|
|
|
|
2015-02-13 08:28:59 +03:00
|
|
|
/* indicate we initialized the limits structure */
|
|
|
|
opal_sys_limits.initialized = true;
|
|
|
|
|
|
|
|
rc = OPAL_SUCCESS;
|
|
|
|
|
|
|
|
out:
|
2015-05-27 22:39:42 +03:00
|
|
|
opal_argv_free(lims);
|
2015-02-27 13:19:46 +03:00
|
|
|
if (NULL != lim) {
|
|
|
|
opal_argv_free(lim);
|
|
|
|
}
|
2013-04-03 22:57:53 +04:00
|
|
|
|
2015-02-13 08:28:59 +03:00
|
|
|
return rc;
|
2007-04-23 22:53:47 +04:00
|
|
|
}
|
2014-08-02 22:38:16 +04:00
|
|
|
|
|
|
|
int opal_getpagesize(void)
|
|
|
|
{
|
2017-04-05 23:39:36 +03:00
|
|
|
static int page_size = -1;
|
|
|
|
|
|
|
|
if (page_size != -1) {
|
|
|
|
// testing in a loop showed sysconf() took ~5 usec vs ~0.3 usec with it cached
|
|
|
|
return page_size;
|
|
|
|
}
|
|
|
|
|
2014-08-02 22:38:16 +04:00
|
|
|
#ifdef HAVE_GETPAGESIZE
|
2017-04-05 23:39:36 +03:00
|
|
|
return page_size = getpagesize();
|
2014-08-02 22:38:16 +04:00
|
|
|
#elif defined(_SC_PAGESIZE )
|
2017-04-05 23:39:36 +03:00
|
|
|
return page_size = sysconf(_SC_PAGESIZE);
|
2014-08-02 22:38:16 +04:00
|
|
|
#elif defined(_SC_PAGE_SIZE)
|
2017-04-05 23:39:36 +03:00
|
|
|
return page_size = sysconf(_SC_PAGE_SIZE);
|
2014-08-02 22:38:16 +04:00
|
|
|
#else
|
2017-04-05 23:39:36 +03:00
|
|
|
return page_size = 65536; /* safer to overestimate than under */
|
2014-08-02 22:38:16 +04:00
|
|
|
#endif
|
|
|
|
}
|