aboutsummaryrefslogtreecommitdiffstats
path: root/erts/lib_src
diff options
context:
space:
mode:
Diffstat (limited to 'erts/lib_src')
-rw-r--r--erts/lib_src/common/erl_misc_utils.c19
-rw-r--r--erts/lib_src/common/erl_printf.c12
-rw-r--r--erts/lib_src/common/erl_printf_format.c4
-rw-r--r--erts/lib_src/common/ethr_aux.c66
-rw-r--r--erts/lib_src/pthread/ethread.c27
-rw-r--r--erts/lib_src/win/ethread.c27
6 files changed, 121 insertions, 34 deletions
diff --git a/erts/lib_src/common/erl_misc_utils.c b/erts/lib_src/common/erl_misc_utils.c
index 8186463b9c..55fac13e95 100644
--- a/erts/lib_src/common/erl_misc_utils.c
+++ b/erts/lib_src/common/erl_misc_utils.c
@@ -1,7 +1,7 @@
/*
* %CopyrightBegin%
*
- * Copyright Ericsson AB 2006-2016. All Rights Reserved.
+ * Copyright Ericsson AB 2006-2017. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -842,7 +842,7 @@ read_topology(erts_cpu_info_t *cpuinfo)
cpuinfo->topology[ix].logical = -1;
}
- ix = -1;
+ ix = 0;
if (realpath(ERTS_SYS_NODE_PATH, npath)) {
ndir = opendir(npath);
@@ -886,6 +886,7 @@ read_topology(erts_cpu_info_t *cpuinfo)
cdir = NULL;
break;
}
+
if (sscanf(cde->d_name, "cpu%d", &cpu_id) == 1) {
char buf[50]; /* Much more than enough for an integer */
int processor_id, core_id;
@@ -906,23 +907,33 @@ read_topology(erts_cpu_info_t *cpuinfo)
if (sscanf(buf, "%d", &core_id) != 1)
continue;
+ /*
+ * The number of CPUs that proc fs presents is greater
+ * then the number of CPUs configured in sysconf.
+ * This has been known to happen in docker. When this
+ * happens we refuse to give a CPU topology.
+ */
+ if (ix >= cpuinfo->configured)
+ goto error;
+
/*
* We now know node id, processor id, and
* core id of the logical processor with
* the cpu id 'cpu_id'.
*/
- ix++;
cpuinfo->topology[ix].node = node_id;
cpuinfo->topology[ix].processor = processor_id;
cpuinfo->topology[ix].processor_node = -1; /* Fixed later */
cpuinfo->topology[ix].core = core_id;
cpuinfo->topology[ix].thread = 0; /* we'll numerate later */
cpuinfo->topology[ix].logical = cpu_id;
+ ix++;
+
}
}
} while (got_nodes);
- res = ix+1;
+ res = ix;
if (!res || res < cpuinfo->online)
res = 0;
diff --git a/erts/lib_src/common/erl_printf.c b/erts/lib_src/common/erl_printf.c
index 7781fc2196..3b073bcd1b 100644
--- a/erts/lib_src/common/erl_printf.c
+++ b/erts/lib_src/common/erl_printf.c
@@ -147,8 +147,8 @@ write_f_add_cr(void *vfp, char* buf, size_t len)
return len;
}
-static int
-write_f(void *vfp, char* buf, size_t len)
+int
+erts_write_fp(void *vfp, char* buf, size_t len)
{
ASSERT(vfp);
#ifdef PUTC_ON_SMALL_WRITES
@@ -257,7 +257,7 @@ erts_printf(const char *format, ...)
FLOCKFILE(stdout);
res = erts_printf_format(erts_printf_add_cr_to_stdout
? write_f_add_cr
- : write_f,
+ : erts_write_fp,
(void *) stdout,
(char *) format,
arglist);
@@ -285,7 +285,7 @@ erts_fprintf(FILE *filep, const char *format, ...)
else if (erts_printf_add_cr_to_stderr && filep == stderr)
fmt_f = write_f_add_cr;
else
- fmt_f = write_f;
+ fmt_f = erts_write_fp;
FLOCKFILE(filep);
res = erts_printf_format(fmt_f,(void *)filep,(char *)format,arglist);
FUNLOCKFILE(filep);
@@ -390,7 +390,7 @@ erts_vprintf(const char *format, va_list arglist)
errno = 0;
res = erts_printf_format(erts_printf_add_cr_to_stdout
? write_f_add_cr
- : write_f,
+ : erts_write_fp,
(void *) stdout,
(char *) format,
arglist);
@@ -414,7 +414,7 @@ erts_vfprintf(FILE *filep, const char *format, va_list arglist)
else if (erts_printf_add_cr_to_stderr && filep == stderr)
fmt_f = write_f_add_cr;
else
- fmt_f = write_f;
+ fmt_f = erts_write_fp;
res = erts_printf_format(fmt_f,(void *)filep,(char *)format,arglist);
}
return res;
diff --git a/erts/lib_src/common/erl_printf_format.c b/erts/lib_src/common/erl_printf_format.c
index 3daa066fd3..5a680d6f9d 100644
--- a/erts/lib_src/common/erl_printf_format.c
+++ b/erts/lib_src/common/erl_printf_format.c
@@ -326,12 +326,12 @@ static int fmt_double(fmtfn_t fn,void*arg,double val,
{
int res;
int fi = 0;
- char format_str[7];
+ char format_str[8];
char sbuf[32];
char *bufp = sbuf;
double dexp;
int exp;
- size_t max_size = 1;
+ size_t max_size = 2; /* including possible sign */
int size;
int new_fmt = fmt;
int fpe_was_unmasked;
diff --git a/erts/lib_src/common/ethr_aux.c b/erts/lib_src/common/ethr_aux.c
index 3501fe335a..7b156fe01a 100644
--- a/erts/lib_src/common/ethr_aux.c
+++ b/erts/lib_src/common/ethr_aux.c
@@ -1,7 +1,7 @@
/*
* %CopyrightBegin%
*
- * Copyright Ericsson AB 2010-2016. All Rights Reserved.
+ * Copyright Ericsson AB 2010-2017. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -68,6 +68,8 @@ size_t ethr_max_stack_size__; /* kilo words */
ethr_rwmutex xhndl_rwmtx;
ethr_xhndl_list *xhndl_list;
+static ethr_tsd_key ethr_stacklimit_key__;
+
static int main_threads;
static int init_ts_event_alloc(void);
@@ -237,13 +239,11 @@ ethr_init_common__(ethr_init_data *id)
#endif
ethr_min_stack_size__ = ETHR_PAGE_ALIGN(ethr_min_stack_size__);
- ethr_min_stack_size__ = ETHR_B2KW(ethr_min_stack_size__);
-
ethr_max_stack_size__ = 32*1024*1024;
#if SIZEOF_VOID_P == 8
ethr_max_stack_size__ *= 2;
#endif
- ethr_max_stack_size__ = ETHR_B2KW(ethr_max_stack_size__);
+ ethr_max_stack_size__ = ETHR_PAGE_ALIGN(ethr_max_stack_size__);
res = ethr_init_atomics();
if (res != 0)
@@ -253,6 +253,10 @@ ethr_init_common__(ethr_init_data *id)
if (res != 0)
return res;
+ res = ethr_tsd_key_create(&ethr_stacklimit_key__, "stacklimit");
+ if (res != 0)
+ return res;
+
xhndl_list = NULL;
return 0;
@@ -313,6 +317,60 @@ ethr_late_init_common__(ethr_late_init_data *lid)
return 0;
}
+/*
+ * Stack limit
+ */
+
+void *ethr_get_stacklimit(void)
+{
+ return ethr_tsd_get(ethr_stacklimit_key__);
+}
+
+int ethr_set_stacklimit(void *limit)
+{
+ void *prev = ethr_tsd_get(ethr_stacklimit_key__);
+ if (prev)
+ return EACCES;
+ if (!limit)
+ return EINVAL;
+ return ethr_tsd_set(ethr_stacklimit_key__, limit);
+}
+
+/* internal stacklimit (thread creation) */
+
+void
+ethr_set_stacklimit__(char *prev_c, size_t stacksize)
+{
+ /*
+ * We *don't* want this function inlined, i.e., it is
+ * risky to call this function from another function
+ * in ethr_aux.c
+ */
+ void *limit = NULL;
+ char c;
+ int res;
+
+ if (stacksize) {
+ char *start;
+ if (&c > prev_c) {
+ start = (char *) ((((ethr_uint_t) prev_c)
+ / ethr_pagesize__)
+ * ethr_pagesize__);
+ limit = start + stacksize;
+ }
+ else {
+ start = (char *) (((((ethr_uint_t) prev_c) - 1)
+ / ethr_pagesize__ + 1)
+ * ethr_pagesize__);
+ limit = start - stacksize;
+ }
+ }
+
+ res = ethr_tsd_set(ethr_stacklimit_key__, limit);
+ if (res != 0)
+ ethr_abort__();
+}
+
int
ethr_install_exit_handler(void (*funcp)(void))
{
diff --git a/erts/lib_src/pthread/ethread.c b/erts/lib_src/pthread/ethread.c
index 29bffa7e12..b4b12fcd86 100644
--- a/erts/lib_src/pthread/ethread.c
+++ b/erts/lib_src/pthread/ethread.c
@@ -1,7 +1,7 @@
/*
* %CopyrightBegin%
*
- * Copyright Ericsson AB 2010-2016. All Rights Reserved.
+ * Copyright Ericsson AB 2010-2017. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -81,6 +81,7 @@ typedef struct {
void *(*thr_func)(void *);
void *arg;
void *prep_func_res;
+ size_t stacksize;
char *name;
char name_buff[16];
} ethr_thr_wrap_data__;
@@ -88,12 +89,15 @@ typedef struct {
static void *thr_wrapper(void *vtwd)
{
ethr_sint32_t result;
+ char c;
void *res;
ethr_thr_wrap_data__ *twd = (ethr_thr_wrap_data__ *) vtwd;
void *(*thr_func)(void *) = twd->thr_func;
void *arg = twd->arg;
ethr_ts_event *tsep = NULL;
+ ethr_set_stacklimit__(&c, twd->stacksize);
+
result = (ethr_sint32_t) ethr_make_ts_event__(&tsep);
if (result == 0) {
@@ -330,6 +334,7 @@ ethr_thr_create(ethr_tid *tid, void * (*func)(void *), void *arg,
twd.tse = ethr_get_ts_event();
twd.thr_func = func;
twd.arg = arg;
+ twd.stacksize = 0;
if (opts && opts->name) {
snprintf(twd.name_buff, 16, "%s", opts->name);
@@ -354,18 +359,24 @@ ethr_thr_create(ethr_tid *tid, void * (*func)(void *), void *arg,
#ifdef ETHR_DEBUG
suggested_stack_size /= 2; /* Make sure we got margin */
#endif
+ stack_size = ETHR_KW2B(suggested_stack_size);
+ stack_size = ETHR_PAGE_ALIGN(stack_size);
+ stack_size += ethr_pagesize__; /* For possible system usage */
#ifdef ETHR_STACK_GUARD_SIZE
/* The guard is at least on some platforms included in the stack size
passed when creating threads */
- suggested_stack_size += ETHR_B2KW(ETHR_STACK_GUARD_SIZE);
+ stack_size += ETHR_STACK_GUARD_SIZE;
#endif
- if (suggested_stack_size < ethr_min_stack_size__)
- stack_size = ETHR_KW2B(ethr_min_stack_size__);
- else if (suggested_stack_size > ethr_max_stack_size__)
- stack_size = ETHR_KW2B(ethr_max_stack_size__);
- else
- stack_size = ETHR_PAGE_ALIGN(ETHR_KW2B(suggested_stack_size));
+ if (stack_size < ethr_min_stack_size__)
+ stack_size = ethr_min_stack_size__;
+ else if (stack_size > ethr_max_stack_size__)
+ stack_size = ethr_max_stack_size__;
(void) pthread_attr_setstacksize(&attr, stack_size);
+ twd.stacksize = stack_size;
+ twd.stacksize -= ethr_pagesize__; /* For possible system usage */
+#ifdef ETHR_STACK_GUARD_SIZE
+ twd.stacksize -= ETHR_STACK_GUARD_SIZE;
+#endif
}
#ifdef ETHR_STACK_GUARD_SIZE
diff --git a/erts/lib_src/win/ethread.c b/erts/lib_src/win/ethread.c
index e0f19f7ba1..aa43e03435 100644
--- a/erts/lib_src/win/ethread.c
+++ b/erts/lib_src/win/ethread.c
@@ -1,7 +1,7 @@
/*
* %CopyrightBegin%
*
- * Copyright Ericsson AB 2010-2016. All Rights Reserved.
+ * Copyright Ericsson AB 2010-2017. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,6 +56,7 @@ typedef struct {
void *(*thr_func)(void *);
void *arg;
void *prep_func_res;
+ size_t stacksize;
} ethr_thr_wrap_data__;
#define ETHR_INVALID_TID_ID -1
@@ -94,6 +95,7 @@ static void thr_exit_cleanup(ethr_tid *tid, void *res)
static unsigned __stdcall thr_wrapper(LPVOID vtwd)
{
+ char c;
ethr_tid my_tid;
ethr_sint32_t result;
void *res;
@@ -102,6 +104,8 @@ static unsigned __stdcall thr_wrapper(LPVOID vtwd)
void *arg = twd->arg;
ethr_ts_event *tsep = NULL;
+ ethr_set_stacklimit__(&c, twd->stacksize);
+
result = (ethr_sint32_t) ethr_make_ts_event__(&tsep);
if (result == 0) {
@@ -199,6 +203,8 @@ ethr_init(ethr_init_data *id)
if (!ethr_not_inited__)
return EINVAL;
+ ethr_not_inited__ = 0;
+
#ifdef _WIN32_WINNT
os_version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&os_version);
@@ -234,8 +240,6 @@ ethr_init(ethr_init_data *id)
if (erts_get_cpu_configured(ethr_cpu_info__) == 1)
child_wait_spin_count = 0;
- ethr_not_inited__ = 0;
-
return 0;
error:
@@ -305,18 +309,21 @@ ethr_thr_create(ethr_tid *tid, void * (*func)(void *), void *arg,
tid->jdata->res = NULL;
}
+ twd.stacksize = 0;
+
if (use_stack_size >= 0) {
size_t suggested_stack_size = (size_t) use_stack_size;
#ifdef ETHR_DEBUG
suggested_stack_size /= 2; /* Make sure we got margin */
#endif
- if (suggested_stack_size < ethr_min_stack_size__)
- stack_size = (unsigned) ETHR_KW2B(ethr_min_stack_size__);
- else if (suggested_stack_size > ethr_max_stack_size__)
- stack_size = (unsigned) ETHR_KW2B(ethr_max_stack_size__);
- else
- stack_size = (unsigned)
- ETHR_PAGE_ALIGN(ETHR_KW2B(suggested_stack_size));
+ stack_size = (unsigned) ETHR_PAGE_ALIGN(ETHR_KW2B(suggested_stack_size));
+
+ if (stack_size < (unsigned) ethr_min_stack_size__)
+ stack_size = (unsigned) ethr_min_stack_size__;
+ else if (stack_size > (unsigned) ethr_max_stack_size__)
+ stack_size = (unsigned) ethr_max_stack_size__;
+
+ twd.stacksize = stack_size;
}
ethr_atomic32_init(&twd.result, -1);