From: Cyrill Gorcunov <gorcunov@gmail.com> To: tml <tarantool-patches@freelists.org> Cc: Vladimir Davydov <vdavydov.dev@gmail.com>, Cyrill Gorcunov <gorcunov@gmail.com> Subject: [PATCH 2/2] lib/core/fiber: Allow to extend default stack size Date: Tue, 19 Mar 2019 22:38:45 +0300 [thread overview] Message-ID: <20190319193845.31221-3-gorcunov@gmail.com> (raw) In-Reply-To: <20190319193845.31221-1-gorcunov@gmail.com> Recently we found that 64K stack we've been using for years is not longer enough due to third party libraries, so we extended its size. Still there is no guarantee that even new size will lasts forever and users might face same problem again, waiting for new build where stack capacity increased. Instead of this ping-pong game lets allow a user to setup default stack size via FIBER_STACK_SIZE_DEFAULT environment variable. Note we fetch this value once on program startup and because it is early init stage we can't use box config engine. --- src/lib/core/fiber.c | 78 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 10 deletions(-) diff --git a/src/lib/core/fiber.c b/src/lib/core/fiber.c index 922a0bfe8..d3cb18a15 100644 --- a/src/lib/core/fiber.c +++ b/src/lib/core/fiber.c @@ -107,18 +107,25 @@ pthread_t main_thread_id; static size_t page_size; static bool stack_growsdown; +/** Indices in fiber stack sizes (FSS) */ enum { + FSS_IDX_MINIMAL, + FSS_IDX_WATERMARK, + FSS_IDX_DEFAULT, +}; + +/** Fiber stack sizes */ +static size_t fiber_stack_size[] = { /* The minimum allowable fiber stack size in bytes */ - FIBER_STACK_SIZE_MINIMAL = 16384, + [FSS_IDX_MINIMAL] = 16384, + /* Stack size watermark in bytes */ + [FSS_IDX_WATERMARK] = 65536, /* Default fiber stack size in bytes */ - FIBER_STACK_SIZE_DEFAULT = 524288, - /* Stack size watermark in bytes. */ - FIBER_STACK_SIZE_WATERMARK = 65536, + [FSS_IDX_DEFAULT] = 524288, }; /** Default fiber attributes */ -static const struct fiber_attr fiber_attr_default = { - .stack_size = FIBER_STACK_SIZE_DEFAULT, +static struct fiber_attr fiber_attr_default = { .flags = FIBER_DEFAULT_FLAGS }; @@ -172,13 +179,13 @@ fiber_attr_delete(struct fiber_attr *fiber_attr) int fiber_attr_setstacksize(struct fiber_attr *fiber_attr, size_t stack_size) { - if (stack_size < FIBER_STACK_SIZE_MINIMAL) { + if (stack_size < fiber_stack_size[FSS_IDX_MINIMAL]) { errno = EINVAL; diag_set(SystemError, "stack size is too small"); return -1; } fiber_attr->stack_size = stack_size; - if (stack_size != FIBER_STACK_SIZE_DEFAULT) { + if (stack_size != fiber_stack_size[FSS_IDX_DEFAULT]) { fiber_attr->flags |= FIBER_CUSTOM_STACK; } else { fiber_attr->flags &= ~FIBER_CUSTOM_STACK; @@ -844,11 +851,11 @@ fiber_stack_watermark_create(struct fiber *fiber) size_t offset = rand() % POISON_OFF * sizeof(poison_pool[0]); if (stack_growsdown) { fiber->stack_watermark = fiber->stack + fiber->stack_size; - fiber->stack_watermark -= FIBER_STACK_SIZE_WATERMARK; + fiber->stack_watermark -= fiber_stack_size[FSS_IDX_WATERMARK]; fiber->stack_watermark += offset; } else { fiber->stack_watermark = fiber->stack; - fiber->stack_watermark += FIBER_STACK_SIZE_WATERMARK; + fiber->stack_watermark += fiber_stack_size[FSS_IDX_WATERMARK]; fiber->stack_watermark -= page_size; fiber->stack_watermark += offset; } @@ -1383,6 +1390,56 @@ cord_slab_cache(void) return &cord()->slabc; } +/** + * Initialize early parameters of the stack + * and setup default variables. + */ +static void +fiber_stack_init(void) +{ + static const char default_size_name[] = "FIBER_STACK_SIZE_DEFAULT"; + char *env; + size_t i; + + /* + * Make sure sizes are ordered, we rely on this + * to minimize rss pressure when releasing pages. + */ + for (i = 1; i < lengthof(fiber_stack_size); i++) + assert(fiber_stack_size[i-1] < fiber_stack_size[i]); + + env = getenv(default_size_name); + if (env) { + size_t value = atol(env); + if (!value) { + say_error("invalid value in env variable %s, ignored", + default_size_name); + goto out; + } + + /* + * Allow extend stack or shrink it down close + * to watermark value. The reason why we don't + * allow to shrink down to minimal stack size + * is our shrinker engine which we try to keep + * as simple as possible. + */ + if (value > fiber_stack_size[FSS_IDX_DEFAULT] || + value > fiber_stack_size[FSS_IDX_WATERMARK]) { + value = (size_t)page_align_up((void *)value); + fiber_stack_size[FSS_IDX_DEFAULT] = value; + } + } + +out: + /* + * Once default stack size is known we should + * update default attribute since it is not + * a constant anymore. + */ + fiber_attr_default.stack_size = fiber_stack_size[FSS_IDX_DEFAULT]; +} + static NOINLINE int is_stack_growsdown(void *prev_stack_frame) { @@ -1394,6 +1451,7 @@ fiber_init(int (*invoke)(fiber_func f, va_list ap)) { page_size = sysconf(_SC_PAGESIZE); stack_growsdown = is_stack_growsdown(__builtin_frame_address(0)); + fiber_stack_init(); fiber_invoke = invoke; main_thread_id = pthread_self(); main_cord.loop = ev_default_loop(EVFLAG_AUTO | EVFLAG_ALLOCFD); -- 2.20.1
next prev parent reply other threads:[~2019-03-19 19:38 UTC|newest] Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-03-19 19:38 [RFC 0/2] lib/core/fiber: Allow to extend stack size via env variable Cyrill Gorcunov 2019-03-19 19:38 ` [PATCH 1/2] lib/core/fiber: Rename stack_direction to stack_growsdown Cyrill Gorcunov 2019-03-27 9:35 ` Vladimir Davydov 2019-03-27 9:48 ` Cyrill Gorcunov 2019-03-27 10:20 ` Vladimir Davydov 2019-03-27 10:30 ` Cyrill Gorcunov 2019-03-19 19:38 ` Cyrill Gorcunov [this message] 2019-03-27 9:35 ` [PATCH 2/2] lib/core/fiber: Allow to extend default stack size Vladimir Davydov 2019-03-27 9:46 ` Cyrill Gorcunov 2019-03-27 10:15 ` Vladimir Davydov 2019-03-27 10:23 ` Cyrill Gorcunov 2019-04-01 17:41 ` Cyrill Gorcunov 2019-04-01 18:58 ` Konstantin Osipov 2019-04-01 19:19 ` Cyrill Gorcunov 2019-04-01 20:51 ` Konstantin Osipov 2019-04-01 22:05 ` Cyrill Gorcunov 2019-04-02 7:14 ` Konstantin Osipov 2019-04-02 8:09 ` Cyrill Gorcunov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20190319193845.31221-3-gorcunov@gmail.com \ --to=gorcunov@gmail.com \ --cc=tarantool-patches@freelists.org \ --cc=vdavydov.dev@gmail.com \ --subject='Re: [PATCH 2/2] lib/core/fiber: Allow to extend default stack size' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox