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.
|
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* 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.
|
2013-04-03 22:57:53 +04:00
|
|
|
* Copyright (c) 2013 Los Alamos National Security, LLC.
|
|
|
|
* All rights reserved.
|
2014-08-02 22:38:16 +04:00
|
|
|
* Copyright (c) 2014 Intel, Inc. All rights reserved.
|
2007-04-23 22:53:47 +04:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opal_config.h"
|
|
|
|
|
2009-04-29 04:49:23 +04:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
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) {
|
2013-04-03 22:57:53 +04:00
|
|
|
rlim_set.rlim_cur = rlim.rlim_cur;
|
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;
|
2013-04-03 22:57:53 +04:00
|
|
|
int i;
|
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, ',');
|
|
|
|
|
|
|
|
/* 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");
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
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");
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
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");
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
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);
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
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);
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
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);
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
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);
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
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);
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
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);
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
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);
|
|
|
|
return OPAL_ERROR;
|
2007-04-23 22:53:47 +04:00
|
|
|
}
|
|
|
|
}
|
2013-04-03 22:57:53 +04:00
|
|
|
|
|
|
|
if (NULL != lim) {
|
|
|
|
opal_argv_free(lim);
|
|
|
|
}
|
|
|
|
if (NULL != lims) {
|
|
|
|
opal_argv_free(lims);
|
|
|
|
}
|
|
|
|
|
2007-04-23 22:53:47 +04:00
|
|
|
/* indicate we initialized the limits structure */
|
|
|
|
opal_sys_limits.initialized = true;
|
|
|
|
|
2013-04-03 22:57:53 +04:00
|
|
|
return OPAL_SUCCESS;
|
2007-04-23 22:53:47 +04:00
|
|
|
}
|
2014-08-02 22:38:16 +04:00
|
|
|
|
|
|
|
int opal_getpagesize(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_GETPAGESIZE
|
|
|
|
return getpagesize();
|
|
|
|
#elif defined(_SC_PAGESIZE )
|
|
|
|
return sysconf(_SC_PAGESIZE);
|
|
|
|
#elif defined(_SC_PAGE_SIZE)
|
|
|
|
return sysconf(_SC_PAGE_SIZE);
|
|
|
|
#else
|
|
|
|
return 65536; /* safer to overestimate than under */
|
|
|
|
#endif
|
|
|
|
}
|