[v7] eal: allow worker lcore stacks to be allocated from hugepage memory

Message ID 20220623112127.43088-1-donw@xsightlabs.com (mailing list archive)
State Accepted, archived
Delegated to: David Marchand
Headers
Series [v7] eal: allow worker lcore stacks to be allocated from hugepage memory |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-abi-testing warning Testing issues

Commit Message

Don Wallwork June 23, 2022, 11:21 a.m. UTC
Add support for using hugepages for worker lcore stack memory.  The
intent is to improve performance by reducing stack memory related TLB
misses and also by using memory local to the NUMA node of each lcore.

EAL option '--huge-worker-stack [stack-size-in-kbytes]' is added to allow
the feature to be enabled at runtime.  If the size is not specified,
the system pthread stack size will be used.

Signed-off-by: Don Wallwork <donw@xsightlabs.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
---
 app/test/test_eal_flags.c                     |  36 ++++++
 doc/guides/linux_gsg/eal_args.include.rst     |   6 +
 .../prog_guide/env_abstraction_layer.rst      |  19 ++++
 lib/eal/common/eal_common_options.c           |   7 ++
 lib/eal/common/eal_internal_cfg.h             |   1 +
 lib/eal/common/eal_options.h                  |   2 +
 lib/eal/linux/eal.c                           | 105 +++++++++++++++++-
 7 files changed, 174 insertions(+), 2 deletions(-)
  

Comments

David Marchand June 23, 2022, 8:32 p.m. UTC | #1
On Thu, Jun 23, 2022 at 1:21 PM Don Wallwork <donw@xsightlabs.com> wrote:
>
> Add support for using hugepages for worker lcore stack memory.  The
> intent is to improve performance by reducing stack memory related TLB
> misses and also by using memory local to the NUMA node of each lcore.
>
> EAL option '--huge-worker-stack[=stack-size-in-kbytes]' is added to allow
> the feature to be enabled at runtime.  If the size is not specified,
> the system pthread stack size will be used.
>
> Signed-off-by: Don Wallwork <donw@xsightlabs.com>
> Acked-by: Morten Brørup <mb@smartsharesystems.com>
> Acked-by: Chengwen Feng <fengchengwen@huawei.com>

Applied, thanks Don.
  

Patch

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index 68deb86aab..624379d49e 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -859,6 +859,14 @@  test_no_huge_flag(void)
 	/* With --no-huge, -m and --socket-mem */
 	const char *argv4[] = {prgname, prefix, no_huge,
 			"-m", DEFAULT_MEM_SIZE, "--socket-mem=" DEFAULT_MEM_SIZE};
+
+	/* With --no-huge and --huge-worker-stack (should fail) */
+	const char * const argv5[] = {prgname, prefix, no_huge,
+			"--huge-worker-stack"};
+	/* With --no-huge and --huge-worker-stack=512 (should fail) */
+	const char * const argv6[] = {prgname, prefix, no_huge,
+			"--huge-worker-stack=512"};
+
 	if (launch_proc(argv1) != 0) {
 		printf("Error - process did not run ok with --no-huge flag\n");
 		return -1;
@@ -882,6 +890,16 @@  test_no_huge_flag(void)
 				"--socket-mem flags\n");
 		return -1;
 	}
+	if (launch_proc(argv5) == 0) {
+		printf("Error - process run ok with --no-huge and "
+		       "--huge-worker-stack flags");
+		return -1;
+	}
+	if (launch_proc(argv6) == 0) {
+		printf("Error - process run ok with --no-huge and "
+		       "--huge-worker-stack=size flags");
+		return -1;
+	}
 	return 0;
 }
 
@@ -1031,6 +1049,14 @@  test_misc_flags(void)
 	const char * const argv20[] = {prgname, "--file-prefix=uiodev",
 			"--create-uio-dev"};
 
+	/* Try running with --huge-worker-stack flag */
+	const char * const argv21[] = {prgname, prefix, mp_flag,
+				       "--huge-worker-stack"};
+
+	/* Try running with --huge-worker-stack=512 flag */
+	const char * const argv22[] = {prgname, prefix, mp_flag,
+				       "--huge-worker-stack=512"};
+
 	/* run all tests also applicable to FreeBSD first */
 
 	if (launch_proc(argv0) == 0) {
@@ -1130,6 +1156,16 @@  test_misc_flags(void)
 				"--create-uio-dev parameter\n");
 		goto fail;
 	}
+	if (launch_proc(argv21) != 0) {
+		printf("Error - process did not run ok with "
+				"--huge-worker-stack parameter\n");
+		goto fail;
+	}
+	if (launch_proc(argv22) != 0) {
+		printf("Error - process did not run ok with "
+				"--huge-worker-stack=size parameter\n");
+		goto fail;
+	}
 
 	rmdir(hugepath_dir3);
 	rmdir(hugepath_dir2);
diff --git a/doc/guides/linux_gsg/eal_args.include.rst b/doc/guides/linux_gsg/eal_args.include.rst
index 3549a0cf56..9cfbf7de84 100644
--- a/doc/guides/linux_gsg/eal_args.include.rst
+++ b/doc/guides/linux_gsg/eal_args.include.rst
@@ -116,6 +116,12 @@  Memory-related options
 
     Force IOVA mode to a specific value.
 
+*   ``--huge-worker-stack[=size]``
+
+    Allocate worker stack memory from hugepage memory. Stack size defaults
+    to system pthread stack size unless the optional size (in kbytes) is
+    specified.
+
 Debugging options
 ~~~~~~~~~~~~~~~~~
 
diff --git a/doc/guides/prog_guide/env_abstraction_layer.rst b/doc/guides/prog_guide/env_abstraction_layer.rst
index 5f0748fba1..42def41e61 100644
--- a/doc/guides/prog_guide/env_abstraction_layer.rst
+++ b/doc/guides/prog_guide/env_abstraction_layer.rst
@@ -329,6 +329,25 @@  Another option is to use bigger page sizes. Since fewer pages are required to
 cover the same memory area, fewer file descriptors will be stored internally
 by EAL.
 
+Hugepage Worker Stacks
+^^^^^^^^^^^^^^^^^^^^^^
+
+When the ``--huge-worker-stack[=size]`` EAL option is specified, worker
+thread stacks are allocated from hugepage memory local to the NUMA node
+of the thread. Worker stack size defaults to system pthread stack size
+if the optional size parameter is not specified.
+
+.. warning::
+    Stacks allocated from hugepage memory are not protected by guard
+    pages. Worker stacks must be sufficiently sized to prevent stack
+    overflow when this option is used.
+
+    As with normal thread stacks, hugepage worker thread stack size is
+    fixed and is not dynamically resized. Therefore, an application that
+    is free of stack page faults under a given load should be safe with
+    hugepage worker thread stacks given the same thread stack size and
+    loading conditions.
+
 Support for Externally Allocated Memory
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
diff --git a/lib/eal/common/eal_common_options.c b/lib/eal/common/eal_common_options.c
index f247a42455..726ee5a5d5 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -103,6 +103,7 @@  eal_long_options[] = {
 	{OPT_TELEMETRY,         0, NULL, OPT_TELEMETRY_NUM        },
 	{OPT_NO_TELEMETRY,      0, NULL, OPT_NO_TELEMETRY_NUM     },
 	{OPT_FORCE_MAX_SIMD_BITWIDTH, 1, NULL, OPT_FORCE_MAX_SIMD_BITWIDTH_NUM},
+	{OPT_HUGE_WORKER_STACK, 2, NULL, OPT_HUGE_WORKER_STACK_NUM     },
 
 	{0,                     0, NULL, 0                        }
 };
@@ -2079,6 +2080,12 @@  eal_check_common_options(struct internal_config *internal_cfg)
 			"be specified together with --"OPT_NO_HUGE"\n");
 		return -1;
 	}
+	if (internal_cfg->no_hugetlbfs &&
+	    internal_cfg->huge_worker_stack_size != 0) {
+		RTE_LOG(ERR, EAL, "Option --"OPT_HUGE_WORKER_STACK" cannot "
+			"be specified together with --"OPT_NO_HUGE"\n");
+		return -1;
+	}
 	if (internal_conf->force_socket_limits && internal_conf->legacy_mem) {
 		RTE_LOG(ERR, EAL, "Option --"OPT_SOCKET_LIMIT
 			" is only supported in non-legacy memory mode\n");
diff --git a/lib/eal/common/eal_internal_cfg.h b/lib/eal/common/eal_internal_cfg.h
index b71faadd18..167ec501fa 100644
--- a/lib/eal/common/eal_internal_cfg.h
+++ b/lib/eal/common/eal_internal_cfg.h
@@ -102,6 +102,7 @@  struct internal_config {
 	unsigned int no_telemetry; /**< true to disable Telemetry */
 	struct simd_bitwidth max_simd_bitwidth;
 	/**< max simd bitwidth path to use */
+	size_t huge_worker_stack_size; /**< worker thread stack size */
 };
 
 void eal_reset_internal_config(struct internal_config *internal_cfg);
diff --git a/lib/eal/common/eal_options.h b/lib/eal/common/eal_options.h
index 8e4f7202a2..3cc9cb6412 100644
--- a/lib/eal/common/eal_options.h
+++ b/lib/eal/common/eal_options.h
@@ -87,6 +87,8 @@  enum {
 	OPT_NO_TELEMETRY_NUM,
 #define OPT_FORCE_MAX_SIMD_BITWIDTH  "force-max-simd-bitwidth"
 	OPT_FORCE_MAX_SIMD_BITWIDTH_NUM,
+#define OPT_HUGE_WORKER_STACK  "huge-worker-stack"
+	OPT_HUGE_WORKER_STACK_NUM,
 
 	OPT_LONG_MAX_NUM
 };
diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c
index 1ef263434a..40e3400d30 100644
--- a/lib/eal/linux/eal.c
+++ b/lib/eal/linux/eal.c
@@ -451,6 +451,11 @@  eal_usage(const char *prgname)
 	       "  --"OPT_LEGACY_MEM"        Legacy memory mode (no dynamic allocation, contiguous segments)\n"
 	       "  --"OPT_SINGLE_FILE_SEGMENTS" Put all hugepage memory in single files\n"
 	       "  --"OPT_MATCH_ALLOCATIONS" Free hugepages exactly as allocated\n"
+	       "  --"OPT_HUGE_WORKER_STACK"[=size]\n"
+	       "                      Allocate worker thread stacks from\n"
+	       "                      hugepage memory. Size is in units of\n"
+	       "                      kbytes and defaults to system thread\n"
+	       "                      stack size if not specified.\n"
 	       "\n");
 	/* Allow the application to print its usage message too if hook is set */
 	if (hook) {
@@ -579,6 +584,47 @@  eal_log_level_parse(int argc, char **argv)
 	optarg = old_optarg;
 }
 
+static int
+eal_parse_huge_worker_stack(const char *arg)
+{
+	struct internal_config *cfg = eal_get_internal_configuration();
+
+	if (arg == NULL || arg[0] == '\0') {
+		pthread_attr_t attr;
+		int ret;
+
+		if (pthread_attr_init(&attr) != 0) {
+			RTE_LOG(ERR, EAL,
+				"Could not retrieve default stack size\n");
+			return -1;
+		}
+		ret = pthread_attr_getstacksize(&attr,
+						&cfg->huge_worker_stack_size);
+		pthread_attr_destroy(&attr);
+		if (ret != 0) {
+			RTE_LOG(ERR, EAL,
+				"Could not retrieve default stack size\n");
+			return -1;
+		}
+	} else {
+		unsigned long stack_size;
+		char *end;
+
+		errno = 0;
+		stack_size = strtoul(arg, &end, 10);
+		if (errno || end == NULL || stack_size == 0 ||
+		    stack_size >= (size_t)-1 / 1024)
+			return -1;
+
+		cfg->huge_worker_stack_size = stack_size * 1024;
+	}
+
+	RTE_LOG(DEBUG, EAL,
+		"Each worker thread will use %zu kB of DPDK memory as stack\n",
+		cfg->huge_worker_stack_size / 1024);
+	return 0;
+}
+
 /* Parse the argument given in the command line of the application */
 static int
 eal_parse_args(int argc, char **argv)
@@ -716,6 +762,14 @@  eal_parse_args(int argc, char **argv)
 			internal_conf->match_allocations = 1;
 			break;
 
+		case OPT_HUGE_WORKER_STACK_NUM:
+			if (eal_parse_huge_worker_stack(optarg) < 0) {
+				RTE_LOG(ERR, EAL, "invalid parameter for --"
+					OPT_HUGE_WORKER_STACK"\n");
+				return -1;
+			}
+			break;
+
 		default:
 			if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
 				RTE_LOG(ERR, EAL, "Option %c is not supported "
@@ -857,6 +911,54 @@  is_iommu_enabled(void)
 	return n > 2;
 }
 
+static int
+eal_worker_thread_create(unsigned int lcore_id)
+{
+	pthread_attr_t *attrp = NULL;
+	void *stack_ptr = NULL;
+	pthread_attr_t attr;
+	size_t stack_size;
+	int ret = -1;
+
+	stack_size = eal_get_internal_configuration()->huge_worker_stack_size;
+	if (stack_size != 0) {
+		/* Allocate NUMA aware stack memory and set pthread attributes */
+		stack_ptr = rte_zmalloc_socket("lcore_stack",
+					       stack_size,
+					       RTE_CACHE_LINE_SIZE,
+					       rte_lcore_to_socket_id(lcore_id));
+		if (stack_ptr == NULL) {
+			rte_eal_init_alert("Cannot allocate worker lcore stack memory");
+			rte_errno = ENOMEM;
+			goto out;
+		}
+
+		if (pthread_attr_init(&attr) != 0) {
+			rte_eal_init_alert("Cannot init pthread attributes");
+			rte_errno = EFAULT;
+			goto out;
+		}
+		attrp = &attr;
+
+		if (pthread_attr_setstack(attrp, stack_ptr, stack_size) != 0) {
+			rte_eal_init_alert("Cannot set pthread stack attributes");
+			rte_errno = EFAULT;
+			goto out;
+		}
+	}
+
+	if (pthread_create(&lcore_config[lcore_id].thread_id, attrp,
+			   eal_thread_loop, (void *)(uintptr_t)lcore_id) == 0)
+		ret = 0;
+
+out:
+	if (ret != 0)
+		rte_free(stack_ptr);
+	if (attrp != NULL)
+		pthread_attr_destroy(attrp);
+	return ret;
+}
+
 /* Launch threads, called at application init(). */
 int
 rte_eal_init(int argc, char **argv)
@@ -1144,8 +1246,7 @@  rte_eal_init(int argc, char **argv)
 		lcore_config[i].state = WAIT;
 
 		/* create a thread for each lcore */
-		ret = pthread_create(&lcore_config[i].thread_id, NULL,
-				     eal_thread_loop, (void *)(uintptr_t)i);
+		ret = eal_worker_thread_create(i);
 		if (ret != 0)
 			rte_panic("Cannot create thread\n");