Skip to contentSkip to conversation

Register external threads as lcore

PATCH Superseded David Marchand 11 participants 133 messages Jun 10, 2020 – Dec 16, 2022 Arm/Amlogic Meson SoC support Page 2 of 2

lore
Share & tools
lore
#105 lore raw

PATCH v5 4/10 eal: introduce thread uninit helper

In reply to David Marchand

This is a preparation step for dynamically unregistering threads.

Since we explicitly allocate a per thread trace buffer in __rte_thread_init, add an internal helper to free this buffer.

---
Changes since v4:
- renamed rte_thread_uninit and moved to eal_private.h,
- hid freeing helper,

Changes since v2:
- added missing stub for windows tracing support,
- moved free symbol to exported (experimental) ABI as a counterpart of the alloc symbol we already had,

Changes since v1:
- rebased on master, removed Windows workaround wrt traces support,

---
 lib/librte_eal/common/eal_common_thread.c |  9 +++++
 lib/librte_eal/common/eal_common_trace.c  | 49 +++++++++++++++++++----
 lib/librte_eal/common/eal_private.h       |  5 +++
 lib/librte_eal/common/eal_trace.h         |  1 +
 lib/librte_eal/windows/eal.c              |  5 +++
 5 files changed, 62 insertions(+), 7 deletions(-)
lib/librte_eal/common/eal_common_thread.c +9 -0
index fb06f8f802..6d1c87b1c2 100644
@@ -20,6 +20,7 @@
 #include "eal_internal_cfg.h"
 #include "eal_private.h"
 #include "eal_thread.h"
+#include "eal_trace.h"
 
 RTE_DEFINE_PER_LCORE(unsigned int, _lcore_id) = LCORE_ID_ANY;
 RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
@@ -161,6 +162,14 @@ __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset)
 	__rte_trace_mem_per_thread_alloc();
 }
 
+void
+__rte_thread_uninit(void)
+{
+	trace_mem_per_thread_free();
+
+	RTE_PER_LCORE(_lcore_id) = LCORE_ID_ANY;
+}
+
 struct rte_thread_ctrl_params {
 	void *(*start_routine)(void *);
 	void *arg;
lib/librte_eal/common/eal_common_trace.c +42 -7

73 lines not on this page · raw message

lib/librte_eal/common/eal_private.h +5 -0
index 5d8b53882d..a77ac7a963 100644
@@ -709,4 +709,9 @@ eal_get_application_usage_hook(void);
  */
 void __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset);
 
+/**
+ * Uninitialize per-lcore info for current thread.
+ */
+void __rte_thread_uninit(void);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/eal_trace.h +1 -0
index 8f60616156..92c5951c3a 100644
@@ -106,6 +106,7 @@ int trace_metadata_create(void);
 void trace_metadata_destroy(void);
 int trace_mkdir(void);
 int trace_epoch_time_save(void);
+void trace_mem_free(void);
 void trace_mem_per_thread_free(void);
 
 /* EAL interface */
lib/librte_eal/windows/eal.c +5 -0
index 9f5d019e64..a11daee68b 100644
@@ -215,6 +215,11 @@ __rte_trace_mem_per_thread_alloc(void)
 {
 }
 
+void
+trace_mem_per_thread_free(void)
+{
+}
+
 void
 __rte_trace_point_emit_field(size_t sz, const char *field,
 	const char *type)
Signature · 1 line
2.23.0
#106 lore raw

PATCH v5 5/10 eal: move lcore role code

In reply to David Marchand

For consistency sake, move all lcore role code in the dedicated compilation unit / header.

---
 lib/librte_eal/common/eal_common_lcore.c  | 11 +++++++
 lib/librte_eal/common/eal_common_thread.c | 11 -------
 lib/librte_eal/include/rte_eal.h          |  9 ------
 lib/librte_eal/include/rte_lcore.h        | 37 ++++++++++++++---------
 4 files changed, 34 insertions(+), 34 deletions(-)
lib/librte_eal/common/eal_common_lcore.c +11 -0
index 5404922a87..86d32a3dd7 100644
@@ -63,6 +63,17 @@ rte_eal_lcore_role(unsigned int lcore_id)
 	return cfg->lcore_role[lcore_id];
 }
 
+int
+rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
+{
+	struct rte_config *cfg = rte_eal_get_configuration();
+
+	if (lcore_id >= RTE_MAX_LCORE)
+		return -EINVAL;
+
+	return cfg->lcore_role[lcore_id] == role;
+}
+
 int rte_lcore_is_enabled(unsigned int lcore_id)
 {
 	struct rte_config *cfg = rte_eal_get_configuration();
lib/librte_eal/common/eal_common_thread.c +0 -11
index 6d1c87b1c2..b1b69ea58c 100644
@@ -33,17 +33,6 @@ unsigned rte_socket_id(void)
 	return RTE_PER_LCORE(_socket_id);
 }
 
-int
-rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
-{
-	struct rte_config *cfg = rte_eal_get_configuration();
-
-	if (lcore_id >= RTE_MAX_LCORE)
-		return -EINVAL;
-
-	return cfg->lcore_role[lcore_id] == role;
-}
-
 static int
 eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
 {
lib/librte_eal/include/rte_eal.h +0 -9
index 2edf8c6556..0913d1947c 100644
@@ -31,15 +31,6 @@ extern "C" {
 /* Maximum thread_name length. */
 #define RTE_MAX_THREAD_NAME_LEN 16
 
-/**
- * The lcore role (used in RTE or not).
- */
-enum rte_lcore_role_t {
-	ROLE_RTE,
-	ROLE_OFF,
-	ROLE_SERVICE,
-};
-
 /**
  * The type of process in a linux, multi-process setup
  */
lib/librte_eal/include/rte_lcore.h +23 -14
index 5c1d1926e9..3968c40693 100644
@@ -24,6 +24,15 @@ extern "C" {
 
 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);  /**< Per thread "lcore id". */
 
+/**
+ * The lcore role (used in RTE or not).
+ */
+enum rte_lcore_role_t {
+	ROLE_RTE,
+	ROLE_OFF,
+	ROLE_SERVICE,
+};
+
 /**
  * Get a lcore's role.
  *
@@ -34,6 +43,20 @@ RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);  /**< Per thread "lcore id". */
  */
 enum rte_lcore_role_t rte_eal_lcore_role(unsigned int lcore_id);
 
+/**
+ * Test if the core supplied has a specific role
+ *
+ * @param lcore_id
+ *   The identifier of the lcore, which MUST be between 0 and
+ *   RTE_MAX_LCORE-1.
+ * @param role
+ *   The role to be checked against.
+ * @return
+ *   Boolean value: positive if test is true; otherwise returns 0.
+ */
+int
+rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
+
 /**
  * Return the Application thread ID of the execution unit.
  *
@@ -283,20 +306,6 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name,
 		const pthread_attr_t *attr,
 		void *(*start_routine)(void *), void *arg);
 
-/**
- * Test if the core supplied has a specific role
- *
- * @param lcore_id
- *   The identifier of the lcore, which MUST be between 0 and
- *   RTE_MAX_LCORE-1.
- * @param role
- *   The role to be checked against.
- * @return
- *   Boolean value: positive if test is true; otherwise returns 0.
- */
-int
-rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
-
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.23.0
#107 lore raw

PATCH v5 6/10 eal: register non-EAL threads as lcores

In reply to David Marchand

DPDK allows calling some part of its API from a non-EAL thread but this has some limitations.
OVS (and other applications) has its own thread management but still want to avoid such limitations by hacking RTE_PER_LCORE(_lcore_id) and faking EAL threads potentially unknown of some DPDK component.

Introduce a new API to register non-EAL thread and associate them to a free lcore with a new NON_EAL role.
This role denotes lcores that do not run DPDK mainloop and as such prevents use of rte_eal_wait_lcore() and consorts.

Multiprocess is not supported as the need for cohabitation with this new feature is unclear at the moment.

---
Changes since v4:
- returned an error when rte_thread_register fails + set rte_errno,
- prefixed unit tests logs with Error: when applicable,

Changes since v2:
- refused multiprocess init once rte_thread_register got called, and vice versa,
- added warning on multiprocess in rte_thread_register doxygen,

Changes since v1:
- moved cleanup on lcore role code in patch 5,
- added unit test,
- updated documentation,
- changed naming from "external thread" to "registered non-EAL thread"

---
 MAINTAINERS                                   |   1 +
 app/test/Makefile                             |   1 +
 app/test/autotest_data.py                     |   6 +
 app/test/meson.build                          |   2 +
 app/test/test_lcores.c                        | 143 ++++++++++++++++++
 doc/guides/howto/debug_troubleshoot.rst       |   5 +-
 .../prog_guide/env_abstraction_layer.rst      |  22 +--
 doc/guides/prog_guide/mempool_lib.rst         |   2 +-
 lib/librte_eal/common/eal_common_lcore.c      |  45 +++++-
 lib/librte_eal/common/eal_common_proc.c       |  41 +++++
 lib/librte_eal/common/eal_common_thread.c     |  49 +++++-
 lib/librte_eal/common/eal_memcfg.h            |   2 +
 lib/librte_eal/common/eal_private.h           |  28 ++++
 lib/librte_eal/freebsd/eal.c                  |   4 +
 lib/librte_eal/include/rte_lcore.h            |  28 +++-
 lib/librte_eal/linux/eal.c                    |   4 +
 lib/librte_eal/rte_eal_version.map            |   4 +
 lib/librte_mempool/rte_mempool.h              |  11 +-
 18 files changed, 375 insertions(+), 23 deletions(-)
 create mode 100644 app/test/test_lcores.c
MAINTAINERS +1 -0
index 27a7be8e64..5a0e1b48b2 100644
@@ -182,6 +182,7 @@ F: app/test/test_cycles.c
 F: app/test/test_debug.c
 F: app/test/test_eal*
 F: app/test/test_errno.c
+F: app/test/test_lcores.c
 F: app/test/test_logs.c
 F: app/test/test_memcpy*
 F: app/test/test_per_lcore.c
app/test/Makefile +1 -0
index e5440774b1..f4065271e4 100644
@@ -98,6 +98,7 @@ SRCS-$(CONFIG_RTE_LIBRTE_FLOW_CLASSIFY) += test_flow_classify.c
 endif
 
 SRCS-y += test_rwlock.c
+SRCS-y += test_lcores.c
 
 SRCS-$(CONFIG_RTE_LIBRTE_STACK) += test_stack.c
 SRCS-$(CONFIG_RTE_LIBRTE_STACK) += test_stack_perf.c
app/test/autotest_data.py +6 -0
index 238ab631b4..4b7da45e09 100644
@@ -62,6 +62,12 @@
         "Func":    rwlock_autotest,
         "Report":  None,
     },
+    {
+        "Name":    "Lcores autotest",
+        "Command": "lcores_autotest",
+        "Func":    default_autotest,
+        "Report":  None,
+    },
     {
         "Name":    "Logs autotest",
         "Command": "logs_autotest",
app/test/meson.build +2 -0
index 56591db4e0..86b4d4d490 100644
@@ -67,6 +67,7 @@ test_sources = files('commands.c',
 	'test_ipsec_perf.c',
 	'test_kni.c',
 	'test_kvargs.c',
+	'test_lcores.c',
 	'test_logs.c',
 	'test_lpm.c',
 	'test_lpm6.c',
@@ -207,6 +208,7 @@ fast_tests = [
         ['hash_autotest', true],
         ['interrupt_autotest', true],
         ['ipfrag_autotest', false],
+        ['lcores_autotest', true],
         ['logs_autotest', true],
         ['lpm_autotest', true],
         ['lpm6_autotest', true],
app/test/test_lcores.c new file +143 -0

143 lines not on this page · raw message

doc/guides/howto/debug_troubleshoot.rst +3 -2
index cef016b2fe..5a46f5fba3 100644
@@ -307,8 +307,9 @@ Custom worker function :numref:`dtg_distributor_worker`.
 
 #. Configuration issue isolation
 
-   * Identify core role using ``rte_eal_lcore_role`` to identify RTE, OFF and
-     SERVICE. Check performance functions are mapped to run on the cores.
+   * Identify core role using ``rte_eal_lcore_role`` to identify RTE, OFF,
+     SERVICE and NON_EAL. Check performance functions are mapped to run on the
+     cores.
 
    * For high-performance execution logic ensure running it on correct NUMA
      and non-master core.
doc/guides/prog_guide/env_abstraction_layer.rst +13 -9
index 48a2fec066..f64ae953d1 100644
@@ -564,9 +564,13 @@ It's also compatible with the pattern of corelist('-l') option.
 non-EAL pthread support
 ~~~~~~~~~~~~~~~~~~~~~~~
 
-It is possible to use the DPDK execution context with any user pthread (aka. Non-EAL pthreads).
-In a non-EAL pthread, the *_lcore_id* is always LCORE_ID_ANY which identifies that it is not an EAL thread with a valid, unique, *_lcore_id*.
-Some libraries will use an alternative unique ID (e.g. TID), some will not be impacted at all, and some will work but with limitations (e.g. timer and mempool libraries).
+It is possible to use the DPDK execution context with any user pthread (aka. non-EAL pthreads).
+There are two kinds of non-EAL pthreads:
+
+- a registered non-EAL pthread with a valid *_lcore_id* that was successfully assigned by calling ``rte_thread_register()``,
+- a non registered non-EAL pthread with a LCORE_ID_ANY,
+
+For non registered non-EAL pthread (with a LCORE_ID_ANY *_lcore_id*), some libraries will use an alternative unique ID (e.g. TID), some will not be impacted at all, and some will work but with limitations (e.g. timer and mempool libraries).
 
 All these impacts are mentioned in :ref:`known_issue_label` section.
 
@@ -613,9 +617,9 @@ Known Issues
 + rte_mempool
 
   The rte_mempool uses a per-lcore cache inside the mempool.
-  For non-EAL pthreads, ``rte_lcore_id()`` will not return a valid number.
-  So for now, when rte_mempool is used with non-EAL pthreads, the put/get operations will bypass the default mempool cache and there is a performance penalty because of this bypass.
-  Only user-owned external caches can be used in a non-EAL context in conjunction with ``rte_mempool_generic_put()`` and ``rte_mempool_generic_get()`` that accept an explicit cache parameter.
+  For unregistered non-EAL pthreads, ``rte_lcore_id()`` will not return a valid number.
+  So for now, when rte_mempool is used with unregistered non-EAL pthreads, the put/get operations will bypass the default mempool cache and there is a performance penalty because of this bypass.
+  Only user-owned external caches can be used in an unregistered non-EAL context in conjunction with ``rte_mempool_generic_put()`` and ``rte_mempool_generic_get()`` that accept an explicit cache parameter.
 
 + rte_ring
 
@@ -660,15 +664,15 @@ Known Issues
 
 + rte_timer
 
-  Running  ``rte_timer_manage()`` on a non-EAL pthread is not allowed. However, resetting/stopping the timer from a non-EAL pthread is allowed.
+  Running  ``rte_timer_manage()`` on an unregistered non-EAL pthread is not allowed. However, resetting/stopping the timer from a non-EAL pthread is allowed.
 
 + rte_log
 
-  In non-EAL pthreads, there is no per thread loglevel and logtype, global loglevels are used.
+  In unregistered non-EAL pthreads, there is no per thread loglevel and logtype, global loglevels are used.
 
 + misc
 
-  The debug statistics of rte_ring, rte_mempool and rte_timer are not supported in a non-EAL pthread.
+  The debug statistics of rte_ring, rte_mempool and rte_timer are not supported in an unregistered non-EAL pthread.
 
 cgroup control
 ~~~~~~~~~~~~~~
doc/guides/prog_guide/mempool_lib.rst +1 -1
index f8b430d656..e3e1f940be 100644
@@ -103,7 +103,7 @@ The maximum size of the cache is static and is defined at compilation time (CONF
 Alternatively to the internal default per-lcore local cache, an application can create and manage external caches through the ``rte_mempool_cache_create()``, ``rte_mempool_cache_free()`` and ``rte_mempool_cache_flush()`` calls.
 These user-owned caches can be explicitly passed to ``rte_mempool_generic_put()`` and ``rte_mempool_generic_get()``.
 The ``rte_mempool_default_cache()`` call returns the default internal cache if any.
-In contrast to the default caches, user-owned caches can be used by non-EAL threads too.
+In contrast to the default caches, user-owned caches can be used by unregistered non-EAL threads too.
 
 Mempool Handlers
 ------------------------
lib/librte_eal/common/eal_common_lcore.c +41 -4

57 lines not on this page · raw message

lib/librte_eal/common/eal_common_proc.c +41 -0

50 lines not on this page · raw message

lib/librte_eal/common/eal_common_thread.c +48 -1

60 lines not on this page · raw message

lib/librte_eal/common/eal_memcfg.h +2 -0
index 583fcb5953..ea013a9daf 100644
@@ -41,6 +41,8 @@ struct rte_mem_config {
 	rte_rwlock_t memory_hotplug_lock;
 	/**< Indicates whether memory hotplug request is in progress. */
 
+	uint8_t mp_status; /**< Multiprocess status. */
+
 	/* memory segments and zones */
 	struct rte_fbarray memzones; /**< Memzone descriptors. */
 
lib/librte_eal/common/eal_private.h +28 -0
index a77ac7a963..e82fb80aa0 100644
@@ -396,6 +396,24 @@ uint64_t get_tsc_freq(void);
  */
 uint64_t get_tsc_freq_arch(void);
 
+/**
+ * Allocate a free lcore to associate to a non-EAL thread.
+ *
+ * @return
+ *   - the id of a lcore with role ROLE_NON_EAL on success.
+ *   - RTE_MAX_LCORE if none was available.
+ */
+unsigned int eal_lcore_non_eal_allocate(void);
+
+/**
+ * Release the lcore used by a non-EAL thread.
+ * Counterpart of eal_lcore_non_eal_allocate().
+ *
+ * @param lcore_id
+ *   The lcore with role ROLE_NON_EAL to release.
+ */
+void eal_lcore_non_eal_release(unsigned int lcore_id);
+
 /**
  * Prepare physical memory mapping
  * i.e. hugepages on Linux and
@@ -714,4 +732,14 @@ void __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset);
  */
 void __rte_thread_uninit(void);
 
+/**
+ * Mark primary process as not supporting multi-process.
+ */
+bool eal_disable_multiprocess(void);
+
+/**
+ * Instruct primary process that a secondary process wants to attach.
+ */
+bool eal_enable_multiprocess(void);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/freebsd/eal.c +4 -0
index fd577daf44..7e5001a864 100644
@@ -400,6 +400,10 @@ rte_config_init(void)
 		}
 		if (rte_eal_config_reattach() < 0)
 			return -1;
+		if (!eal_enable_multiprocess()) {
+			RTE_LOG(ERR, EAL, "Primary process refused secondary attachment\n");
+			return -1;
+		}
 		eal_mcfg_update_internal();
 		break;
 	case RTE_PROC_AUTO:
lib/librte_eal/include/rte_lcore.h +27 -1
index 3968c40693..2fd1a03275 100644
@@ -31,6 +31,7 @@ enum rte_lcore_role_t {
 	ROLE_RTE,
 	ROLE_OFF,
 	ROLE_SERVICE,
+	ROLE_NON_EAL,
 };
 
 /**
@@ -67,7 +68,8 @@ rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
  *   to run threads with lcore IDs 0, 1, 2 and 3 on physical core 10..
  *
  * @return
- *  Logical core ID (in EAL thread) or LCORE_ID_ANY (in non-EAL thread)
+ *  Logical core ID (in EAL thread or registered non-EAL thread) or
+ *  LCORE_ID_ANY (in unregistered non-EAL thread)
  */
 static inline unsigned
 rte_lcore_id(void)
@@ -279,6 +281,30 @@ int rte_thread_setname(pthread_t id, const char *name);
 __rte_experimental
 int rte_thread_getname(pthread_t id, char *name, size_t len);
 
+/**
+ * Register current non-EAL thread as a lcore.
+ *
+ * @note This API is not compatible with the multi-process feature:
+ * - if a primary process registers a non-EAL thread, then no secondary process
+ *   will initialise.
+ * - if a secondary process initialises successfully, trying to register a
+ *   non-EAL thread from either primary or secondary processes will always end
+ *   up with the thread getting LCORE_ID_ANY as lcore.
+ *
+ * @return
+ *   On success, return 0; otherwise return -1 with rte_errno set.
+ */
+__rte_experimental
+int
+rte_thread_register(void);
+
+/**
+ * Unregister current thread and release lcore if one was associated.
+ */
+__rte_experimental
+void
+rte_thread_unregister(void);
+
 /**
  * Create a control thread.
  *
lib/librte_eal/linux/eal.c +4 -0
index bd089cdd44..f4daf46c4e 100644
@@ -492,6 +492,10 @@ rte_config_init(void)
 		}
 		if (rte_eal_config_reattach() < 0)
 			return -1;
+		if (!eal_enable_multiprocess()) {
+			RTE_LOG(ERR, EAL, "Primary process refused secondary attachment\n");
+			return -1;
+		}
 		eal_mcfg_update_internal();
 		break;
 	case RTE_PROC_AUTO:
lib/librte_eal/rte_eal_version.map +4 -0
index 0d42d44ce9..5503dd7620 100644
@@ -393,6 +393,10 @@ EXPERIMENTAL {
 	rte_trace_point_lookup;
 	rte_trace_regexp;
 	rte_trace_save;
+
+	# added in 20.08
+	rte_thread_register;
+	rte_thread_unregister;
 };
 
 INTERNAL {
lib/librte_mempool/rte_mempool.h +6 -5
index 652d19f9f1..9e0ee052b3 100644
@@ -28,9 +28,9 @@
  * rte_mempool_get() or rte_mempool_put() are designed to be called from an EAL
  * thread due to the internal per-lcore cache. Due to the lack of caching,
  * rte_mempool_get() or rte_mempool_put() performance will suffer when called
- * by non-EAL threads. Instead, non-EAL threads should call
- * rte_mempool_generic_get() or rte_mempool_generic_put() with a user cache
- * created with rte_mempool_cache_create().
+ * by unregistered non-EAL threads. Instead, unregistered non-EAL threads
+ * should call rte_mempool_generic_get() or rte_mempool_generic_put() with a
+ * user cache created with rte_mempool_cache_create().
  */
 
 #include <stdio.h>
@@ -1233,7 +1233,7 @@ void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
 /**
  * Create a user-owned mempool cache.
  *
- * This can be used by non-EAL threads to enable caching when they
+ * This can be used by unregistered non-EAL threads to enable caching when they
  * interact with a mempool.
  *
  * @param size
@@ -1264,7 +1264,8 @@ rte_mempool_cache_free(struct rte_mempool_cache *cache);
  * @param lcore_id
  *   The logical core id.
  * @return
- *   A pointer to the mempool cache or NULL if disabled or non-EAL thread.
+ *   A pointer to the mempool cache or NULL if disabled or unregistered non-EAL
+ *   thread.
  */
 static __rte_always_inline struct rte_mempool_cache *
 rte_mempool_default_cache(struct rte_mempool *mp, unsigned lcore_id)
Signature · 1 line
2.23.0
#108 lore raw

PATCH v5 7/10 eal: add lcore init callbacks

In reply to David Marchand

DPDK components and applications can have their say when a new lcore is initialized. For this, they can register a callback for initializing and releasing their private data.

---
Changes since v4:
- fixed leak on callback register failure,
- fixed nits from Konstantin and Olivier,
- prefixed unit tests logs with Error: when applicable,

Changes since v2:
- added missing test,
- fixed rollback on lcore register,

Changes since v1:
- added unit test (since missing some coverage, for v3),
- preferred callback and removed mention of notification,

---
 app/test/test_lcores.c                   | 227 +++++++++++++++++++++++
 lib/librte_eal/common/eal_common_lcore.c | 146 ++++++++++++++-
 lib/librte_eal/common/eal_private.h      |   3 +-
 lib/librte_eal/include/rte_lcore.h       |  70 +++++++
 lib/librte_eal/rte_eal_version.map       |   2 +
 5 files changed, 442 insertions(+), 6 deletions(-)
app/test/test_lcores.c +227 -0
index afb9cdd444..7df827b4e8 100644
@@ -5,6 +5,7 @@
 #include <pthread.h>
 #include <string.h>
 
+#include <rte_common.h>
 #include <rte_errno.h>
 #include <rte_lcore.h>
 
@@ -117,6 +118,226 @@ test_non_eal_lcores(unsigned int eal_threads_count)
 	return ret;
 }
 
+struct limit_lcore_context {
+	unsigned int init;
+	unsigned int max;
+	unsigned int uninit;
+};
+
+static int
+limit_lcores_init(unsigned int lcore_id __rte_unused, void *arg)
+{
+	struct limit_lcore_context *l = arg;
+
+	l->init++;
+	if (l->init > l->max)
+		return -1;
+	return 0;
+}
+
+static void
+limit_lcores_uninit(unsigned int lcore_id __rte_unused, void *arg)
+{
+	struct limit_lcore_context *l = arg;
+
+	l->uninit++;
+}
+
+static int
+test_lcores_callback(unsigned int eal_threads_count)
+{
+	struct limit_lcore_context l;
+	void *handle;
+
+	/* Refuse last lcore => callback register error. */
+	memset(&l, 0, sizeof(l));
+	l.max = eal_threads_count - 1;
+	handle = rte_lcore_callback_register("limit", limit_lcores_init,
+		limit_lcores_uninit, &l);
+	if (handle != NULL) {
+		printf("Error: lcore callback register should have failed\n");
+		goto error;
+	}
+	/* Refusal happens at the n th call to the init callback.
+	 * Besides, n - 1 were accepted, so we expect as many uninit calls when
+	 * the rollback happens.
+	 */
+	if (l.init != eal_threads_count) {
+		printf("Error: lcore callback register failed but incorrect init calls, expected %u, got %u\n",
+			eal_threads_count, l.init);
+		goto error;
+	}
+	if (l.uninit != eal_threads_count - 1) {
+		printf("Error: lcore callback register failed but incorrect uninit calls, expected %u, got %u\n",
+			eal_threads_count - 1, l.uninit);
+		goto error;
+	}
+
+	/* Accept all lcore and unregister. */
+	memset(&l, 0, sizeof(l));
+	l.max = eal_threads_count;
+	handle = rte_lcore_callback_register("limit", limit_lcores_init,
+		limit_lcores_uninit, &l);
+	if (handle == NULL) {
+		printf("Error: lcore callback register failed\n");
+		goto error;
+	}
+	if (l.uninit != 0) {
+		printf("Error: lcore callback register succeeded but incorrect uninit calls, expected 0, got %u\n",
+			l.uninit);
+		goto error;
+	}
+	rte_lcore_callback_unregister(handle);
+	handle = NULL;
+	if (l.init != eal_threads_count) {
+		printf("Error: lcore callback unregister done but incorrect init calls, expected %u, got %u\n",
+			eal_threads_count, l.init);
+		goto error;
+	}
+	if (l.uninit != eal_threads_count) {
+		printf("Error: lcore callback unregister done but incorrect uninit calls, expected %u, got %u\n",
+			eal_threads_count, l.uninit);
+		goto error;
+	}
+
+	return 0;
+
+error:
+	if (handle != NULL)
+		rte_lcore_callback_unregister(handle);
+
+	return -1;
+}
+
+static int
+test_non_eal_lcores_callback(unsigned int eal_threads_count)
+{
+	struct thread_context thread_contexts[2];
+	unsigned int non_eal_threads_count = 0;
+	struct limit_lcore_context l[2] = {};
+	unsigned int registered_count = 0;
+	struct thread_context *t;
+	void *handle[2] = {};
+	unsigned int i;
+	int ret;
+
+	/* This test requires two empty slots to be sure lcore init refusal is
+	 * because of callback execution.
+	 */
+	if (eal_threads_count + 2 >= RTE_MAX_LCORE)
+		return 0;
+
+	/* Register two callbacks:
+	 * - first one accepts any lcore,
+	 * - second one accepts all EAL lcore + one more for the first non-EAL
+	 *   thread, then refuses the next lcore.
+	 */
+	l[0].max = UINT_MAX;
+	handle[0] = rte_lcore_callback_register("no_limit", limit_lcores_init,
+		limit_lcores_uninit, &l[0]);
+	if (handle[0] == NULL) {
+		printf("Error: lcore callback [0] register failed\n");
+		goto error;
+	}
+	l[1].max = eal_threads_count + 1;
+	handle[1] = rte_lcore_callback_register("limit", limit_lcores_init,
+		limit_lcores_uninit, &l[1]);
+	if (handle[1] == NULL) {
+		printf("Error: lcore callback [1] register failed\n");
+		goto error;
+	}
+	if (l[0].init != eal_threads_count || l[1].init != eal_threads_count) {
+		printf("Error: lcore callbacks register succeeded but incorrect init calls, expected %u, %u, got %u, %u\n",
+			eal_threads_count, eal_threads_count,
+			l[0].init, l[1].init);
+		goto error;
+	}
+	if (l[0].uninit != 0 || l[1].uninit != 0) {
+		printf("Error: lcore callbacks register succeeded but incorrect uninit calls, expected 0, 1, got %u, %u\n",
+			l[0].uninit, l[1].uninit);
+		goto error;
+	}
+	/* First thread that expects a valid lcore id. */
+	t = &thread_contexts[0];
+	t->state = INIT;
+	t->registered_count = &registered_count;
+	t->lcore_id_any = false;
+	if (pthread_create(&t->id, NULL, thread_loop, t) != 0)
+		goto cleanup_threads;
+	non_eal_threads_count++;
+	while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
+			non_eal_threads_count)
+		;
+	if (l[0].init != eal_threads_count + 1 ||
+			l[1].init != eal_threads_count + 1) {
+		printf("Error: incorrect init calls, expected %u, %u, got %u, %u\n",
+			eal_threads_count + 1, eal_threads_count + 1,
+			l[0].init, l[1].init);
+		goto cleanup_threads;
+	}
+	if (l[0].uninit != 0 || l[1].uninit != 0) {
+		printf("Error: incorrect uninit calls, expected 0, 0, got %u, %u\n",
+			l[0].uninit, l[1].uninit);
+		goto cleanup_threads;
+	}
+	/* Second thread, that expects LCORE_ID_ANY because of init refusal. */
+	t = &thread_contexts[1];
+	t->state = INIT;
+	t->registered_count = &registered_count;
+	t->lcore_id_any = true;
+	if (pthread_create(&t->id, NULL, thread_loop, t) != 0)
+		goto cleanup_threads;
+	non_eal_threads_count++;
+	while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
+			non_eal_threads_count)
+		;
+	if (l[0].init != eal_threads_count + 2 ||
+			l[1].init != eal_threads_count + 2) {
+		printf("Error: incorrect init calls, expected %u, %u, got %u, %u\n",
+			eal_threads_count + 2, eal_threads_count + 2,
+			l[0].init, l[1].init);
+		goto cleanup_threads;
+	}
+	if (l[0].uninit != 1 || l[1].uninit != 0) {
+		printf("Error: incorrect uninit calls, expected 1, 0, got %u, %u\n",
+			l[0].uninit, l[1].uninit);
+		goto cleanup_threads;
+	}
+	/* Release all threads, and check their states. */
+	__atomic_store_n(&registered_count, 0, __ATOMIC_RELEASE);
+	ret = 0;
+	for (i = 0; i < non_eal_threads_count; i++) {
+		t = &thread_contexts[i];
+		pthread_join(t->id, NULL);
+		if (t->state != DONE)
+			ret = -1;
+	}
+	if (ret < 0)
+		goto error;
+	if (l[0].uninit != 2 || l[1].uninit != 1) {
+		printf("Error: threads reported having successfully registered and unregistered, but incorrect uninit calls, expected 2, 1, got %u, %u\n",
+			l[0].uninit, l[1].uninit);
+		goto error;
+	}
+	rte_lcore_callback_unregister(handle[0]);
+	rte_lcore_callback_unregister(handle[1]);
+	return 0;
+
+cleanup_threads:
+	/* Release all threads */
+	__atomic_store_n(&registered_count, 0, __ATOMIC_RELEASE);
+	for (i = 0; i < non_eal_threads_count; i++) {
+		t = &thread_contexts[i];
+		pthread_join(t->id, NULL);
+	}
+error:
+	if (handle[1] != NULL)
+		rte_lcore_callback_unregister(handle[1]);
+	if (handle[0] != NULL)
+		rte_lcore_callback_unregister(handle[0]);
+	return -1;
+}
+
 static int
 test_lcores(void)
 {
@@ -137,6 +358,12 @@ test_lcores(void)
 	if (test_non_eal_lcores(eal_threads_count) < 0)
 		return TEST_FAILED;
 
+	if (test_lcores_callback(eal_threads_count) < 0)
+		return TEST_FAILED;
+
+	if (test_non_eal_lcores_callback(eal_threads_count) < 0)
+		return TEST_FAILED;
+
 	return TEST_SUCCESS;
 }
 
lib/librte_eal/common/eal_common_lcore.c +141 -5

171 lines not on this page · raw message

lib/librte_eal/common/eal_private.h +2 -1
index e82fb80aa0..535e008474 100644
@@ -401,7 +401,8 @@ uint64_t get_tsc_freq_arch(void);
  *
  * @return
  *   - the id of a lcore with role ROLE_NON_EAL on success.
- *   - RTE_MAX_LCORE if none was available.
+ *   - RTE_MAX_LCORE if none was available or initializing was refused (see
+ *     rte_lcore_callback_register).
  */
 unsigned int eal_lcore_non_eal_allocate(void);
 
lib/librte_eal/include/rte_lcore.h +70 -0

76 lines not on this page · raw message

lib/librte_eal/rte_eal_version.map +2 -0
index 5503dd7620..c3e762c1d9 100644
@@ -395,6 +395,8 @@ EXPERIMENTAL {
 	rte_trace_save;
 
 	# added in 20.08
+	rte_lcore_callback_register;
+	rte_lcore_callback_unregister;
 	rte_thread_register;
 	rte_thread_unregister;
 };
Signature · 1 line
2.23.0
#109 lore raw

PATCH v5 8/10 eal: add lcore iterators

In reply to David Marchand

Add a helper to iterate all lcores.
The iterator callback is read-only wrt the lcores list.

Implement a dump function on top of this for debugging.

---
Changes since v2:
- added rte_lcore_dump calls in unit test, for basic check,

Changes since v1:
- introduced lcore iterators and implemented rte_lcore_dump, this iterator mechanism can then be used outside of EAL,

---
 app/test/test_lcores.c                    |  3 +
 lib/librte_eal/common/eal_common_lcore.c  | 77 ++++++++++++++++++++---
 lib/librte_eal/common/eal_common_thread.c | 16 +++--
 lib/librte_eal/common/eal_thread.h        | 13 +++-
 lib/librte_eal/freebsd/eal.c              |  2 +-
 lib/librte_eal/freebsd/eal_thread.c       |  2 +-
 lib/librte_eal/include/rte_lcore.h        | 47 +++++++++++++-
 lib/librte_eal/linux/eal.c                |  2 +-
 lib/librte_eal/linux/eal_thread.c         |  2 +-
 lib/librte_eal/rte_eal_version.map        |  2 +
 10 files changed, 143 insertions(+), 23 deletions(-)
app/test/test_lcores.c +3 -0
index 7df827b4e8..19a7ab9fce 100644
@@ -303,6 +303,7 @@ test_non_eal_lcores_callback(unsigned int eal_threads_count)
 			l[0].uninit, l[1].uninit);
 		goto cleanup_threads;
 	}
+	rte_lcore_dump(stdout);
 	/* Release all threads, and check their states. */
 	__atomic_store_n(&registered_count, 0, __ATOMIC_RELEASE);
 	ret = 0;
@@ -314,6 +315,7 @@ test_non_eal_lcores_callback(unsigned int eal_threads_count)
 	}
 	if (ret < 0)
 		goto error;
+	rte_lcore_dump(stdout);
 	if (l[0].uninit != 2 || l[1].uninit != 1) {
 		printf("Error: threads reported having successfully registered and unregistered, but incorrect uninit calls, expected 2, 1, got %u, %u\n",
 			l[0].uninit, l[1].uninit);
@@ -354,6 +356,7 @@ test_lcores(void)
 	}
 	printf("EAL threads count: %u, RTE_MAX_LCORE=%u\n", eal_threads_count,
 		RTE_MAX_LCORE);
+	rte_lcore_dump(stdout);
 
 	if (test_non_eal_lcores(eal_threads_count) < 0)
 		return TEST_FAILED;
lib/librte_eal/common/eal_common_lcore.c +67 -10

135 lines not on this page · raw message

lib/librte_eal/common/eal_common_thread.c +11 -5
index cead8ea5cf..ea09a4f3e0 100644
@@ -105,17 +105,14 @@ rte_thread_get_affinity(rte_cpuset_t *cpusetp)
 }
 
 int
-eal_thread_dump_affinity(char *str, unsigned size)
+eal_thread_dump_affinity(rte_cpuset_t *cpuset, char *str, unsigned int size)
 {
-	rte_cpuset_t cpuset;
 	unsigned cpu;
 	int ret;
 	unsigned int out = 0;
 
-	rte_thread_get_affinity(&cpuset);
-
 	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
-		if (!CPU_ISSET(cpu, &cpuset))
+		if (!CPU_ISSET(cpu, cpuset))
 			continue;
 
 		ret = snprintf(str + out,
@@ -138,6 +135,15 @@ eal_thread_dump_affinity(char *str, unsigned size)
 	return ret;
 }
 
+int
+eal_thread_dump_current_affinity(char *str, unsigned int size)
+{
+	rte_cpuset_t cpuset;
+
+	rte_thread_get_affinity(&cpuset);
+	return eal_thread_dump_affinity(&cpuset, str, size);
+}
+
 void
 __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset)
 {
lib/librte_eal/common/eal_thread.h +11 -2
index dc1fc6eb99..4a49117be8 100644
@@ -32,13 +32,15 @@ unsigned eal_cpu_socket_id(unsigned cpu_id);
 #define RTE_CPU_AFFINITY_STR_LEN            256
 
 /**
- * Dump the current pthread cpuset.
+ * Dump the cpuset as a human readable string.
  * This function is private to EAL.
  *
  * Note:
  *   If the dump size is greater than the size of given buffer,
  *   the string will be truncated and with '\0' at the end.
  *
+ * @param cpuset
+ *   The CPU affinity object to dump.
  * @param str
  *   The string buffer the cpuset will dump to.
  * @param size
@@ -47,6 +49,13 @@ unsigned eal_cpu_socket_id(unsigned cpu_id);
  *   0 for success, -1 if truncation happens.
  */
 int
-eal_thread_dump_affinity(char *str, unsigned size);
+eal_thread_dump_affinity(rte_cpuset_t *cpuset, char *str, unsigned int size);
+
+/**
+ * Dump the current thread cpuset.
+ * This is a wrapper on eal_thread_dump_affinity().
+ */
+int
+eal_thread_dump_current_affinity(char *str, unsigned int size);
 
 #endif /* EAL_THREAD_H */
lib/librte_eal/freebsd/eal.c +1 -1
index 7e5001a864..7d6f4aa09b 100644
@@ -858,7 +858,7 @@ rte_eal_init(int argc, char **argv)
 	__rte_thread_init(config->master_lcore,
 		&lcore_config[config->master_lcore].cpuset);
 
-	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
+	ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
 
 	RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
 		config->master_lcore, thread_id, cpuset,
lib/librte_eal/freebsd/eal_thread.c +1 -1
index 0788a54fe6..99b5fefc4c 100644
@@ -92,7 +92,7 @@ eal_thread_loop(__rte_unused void *arg)
 
 	__rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
 
-	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
+	ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
 	RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
 		lcore_id, thread_id, cpuset, ret == 0 ? "" : "...");
 
lib/librte_eal/include/rte_lcore.h +45 -2

59 lines not on this page · raw message

lib/librte_eal/linux/eal.c +1 -1
index f4daf46c4e..2cce19dd3d 100644
@@ -1197,7 +1197,7 @@ rte_eal_init(int argc, char **argv)
 	__rte_thread_init(config->master_lcore,
 		&lcore_config[config->master_lcore].cpuset);
 
-	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
+	ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
 	RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
 		config->master_lcore, (uintptr_t)thread_id, cpuset,
 		ret == 0 ? "" : "...");
lib/librte_eal/linux/eal_thread.c +1 -1
index e0440c0000..48a2c1124b 100644
@@ -92,7 +92,7 @@ eal_thread_loop(__rte_unused void *arg)
 
 	__rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
 
-	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
+	ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
 	RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
 		lcore_id, (uintptr_t)thread_id, cpuset, ret == 0 ? "" : "...");
 
lib/librte_eal/rte_eal_version.map +2 -0
index c3e762c1d9..3aeb5b11ab 100644
@@ -397,6 +397,8 @@ EXPERIMENTAL {
 	# added in 20.08
 	rte_lcore_callback_register;
 	rte_lcore_callback_unregister;
+	rte_lcore_dump;
+	rte_lcore_iterate;
 	rte_thread_register;
 	rte_thread_unregister;
 };
Signature · 1 line
2.23.0
#110 lore raw

PATCH v5 9/10 mempool/bucket: handle non-EAL lcores

In reply to David Marchand

Convert to new lcore API to support non-EAL lcores.

---
Changes since v3:
- addressed Andrew comments,

---
 drivers/mempool/bucket/rte_mempool_bucket.c | 130 ++++++++++++--------
 1 file changed, 81 insertions(+), 49 deletions(-)
drivers/mempool/bucket/rte_mempool_bucket.c +81 -49
index 5ce1ef16fb..8b9daa9782 100644
@@ -55,6 +55,7 @@ struct bucket_data {
 	struct rte_ring *shared_orphan_ring;
 	struct rte_mempool *pool;
 	unsigned int bucket_mem_size;
+	void *lcore_callback_handle;
 };
 
 static struct bucket_stack *
@@ -345,6 +346,23 @@ bucket_dequeue_contig_blocks(struct rte_mempool *mp, void **first_obj_table,
 	return 0;
 }
 
+struct bucket_count_per_lcore_ctx {
+	const struct bucket_data *bd;
+	unsigned int count;
+};
+
+static int
+bucket_count_per_lcore(unsigned int lcore_id, void *arg)
+{
+	struct bucket_count_per_lcore_ctx *bplc = arg;
+
+	bplc->count += bplc->bd->obj_per_bucket *
+		bplc->bd->buckets[lcore_id]->top;
+	bplc->count +=
+		rte_ring_count(bplc->bd->adoption_buffer_rings[lcore_id]);
+	return 0;
+}
+
 static void
 count_underfilled_buckets(struct rte_mempool *mp,
 			  void *opaque,
@@ -373,23 +391,64 @@ count_underfilled_buckets(struct rte_mempool *mp,
 static unsigned int
 bucket_get_count(const struct rte_mempool *mp)
 {
-	const struct bucket_data *bd = mp->pool_data;
-	unsigned int count =
-		bd->obj_per_bucket * rte_ring_count(bd->shared_bucket_ring) +
-		rte_ring_count(bd->shared_orphan_ring);
-	unsigned int i;
+	struct bucket_count_per_lcore_ctx bplc;
 
-	for (i = 0; i < RTE_MAX_LCORE; i++) {
-		if (!rte_lcore_is_enabled(i))
-			continue;
-		count += bd->obj_per_bucket * bd->buckets[i]->top +
-			rte_ring_count(bd->adoption_buffer_rings[i]);
-	}
+	bplc.bd = mp->pool_data;
+	bplc.count = bplc.bd->obj_per_bucket *
+		rte_ring_count(bplc.bd->shared_bucket_ring);
+	bplc.count += rte_ring_count(bplc.bd->shared_orphan_ring);
 
+	rte_lcore_iterate(bucket_count_per_lcore, &bplc);
 	rte_mempool_mem_iter((struct rte_mempool *)(uintptr_t)mp,
-			     count_underfilled_buckets, &count);
+			     count_underfilled_buckets, &bplc.count);
+
+	return bplc.count;
+}
+
+static int
+bucket_init_per_lcore(unsigned int lcore_id, void *arg)
+{
+	char rg_name[RTE_RING_NAMESIZE];
+	struct bucket_data *bd = arg;
+	struct rte_mempool *mp;
+	int rg_flags;
+	int rc;
+
+	mp = bd->pool;
+	bd->buckets[lcore_id] = bucket_stack_create(mp,
+		mp->size / bd->obj_per_bucket);
+	if (bd->buckets[lcore_id] == NULL)
+		goto error;
+
+	rc = snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT ".a%u",
+		mp->name, lcore_id);
+	if (rc < 0 || rc >= (int)sizeof(rg_name))
+		goto error;
+
+	rg_flags = RING_F_SC_DEQ;
+	if (mp->flags & MEMPOOL_F_SP_PUT)
+		rg_flags |= RING_F_SP_ENQ;
+	bd->adoption_buffer_rings[lcore_id] = rte_ring_create(rg_name,
+		rte_align32pow2(mp->size + 1), mp->socket_id, rg_flags);
+	if (bd->adoption_buffer_rings[lcore_id] == NULL)
+		goto error;
 
-	return count;
+	return 0;
+error:
+	rte_free(bd->buckets[lcore_id]);
+	bd->buckets[lcore_id] = NULL;
+	return -1;
+}
+
+static void
+bucket_uninit_per_lcore(unsigned int lcore_id, void *arg)
+{
+	struct bucket_data *bd = arg;
+
+	rte_ring_free(bd->adoption_buffer_rings[lcore_id]);
+	bd->adoption_buffer_rings[lcore_id] = NULL;
+	rte_free(bd->buckets[lcore_id]);
+	bd->buckets[lcore_id] = NULL;
 }
 
 static int
@@ -399,7 +458,6 @@ bucket_alloc(struct rte_mempool *mp)
 	int rc = 0;
 	char rg_name[RTE_RING_NAMESIZE];
 	struct bucket_data *bd;
-	unsigned int i;
 	unsigned int bucket_header_size;
 	size_t pg_sz;
 
@@ -429,36 +487,17 @@ bucket_alloc(struct rte_mempool *mp)
 	/* eventually this should be a tunable parameter */
 	bd->bucket_stack_thresh = (mp->size / bd->obj_per_bucket) * 4 / 3;
 
+	bd->lcore_callback_handle = rte_lcore_callback_register("bucket",
+		bucket_init_per_lcore, bucket_uninit_per_lcore, bd);
+	if (bd->lcore_callback_handle == NULL) {
+		rc = -ENOMEM;
+		goto no_mem_for_stacks;
+	}
+
 	if (mp->flags & MEMPOOL_F_SP_PUT)
 		rg_flags |= RING_F_SP_ENQ;
 	if (mp->flags & MEMPOOL_F_SC_GET)
 		rg_flags |= RING_F_SC_DEQ;
-
-	for (i = 0; i < RTE_MAX_LCORE; i++) {
-		if (!rte_lcore_is_enabled(i))
-			continue;
-		bd->buckets[i] =
-			bucket_stack_create(mp, mp->size / bd->obj_per_bucket);
-		if (bd->buckets[i] == NULL) {
-			rc = -ENOMEM;
-			goto no_mem_for_stacks;
-		}
-		rc = snprintf(rg_name, sizeof(rg_name),
-			      RTE_MEMPOOL_MZ_FORMAT ".a%u", mp->name, i);
-		if (rc < 0 || rc >= (int)sizeof(rg_name)) {
-			rc = -ENAMETOOLONG;
-			goto no_mem_for_stacks;
-		}
-		bd->adoption_buffer_rings[i] =
-			rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
-					mp->socket_id,
-					rg_flags | RING_F_SC_DEQ);
-		if (bd->adoption_buffer_rings[i] == NULL) {
-			rc = -rte_errno;
-			goto no_mem_for_stacks;
-		}
-	}
-
 	rc = snprintf(rg_name, sizeof(rg_name),
 		      RTE_MEMPOOL_MZ_FORMAT ".0", mp->name);
 	if (rc < 0 || rc >= (int)sizeof(rg_name)) {
@@ -498,11 +537,8 @@ bucket_alloc(struct rte_mempool *mp)
 	rte_ring_free(bd->shared_orphan_ring);
 cannot_create_shared_orphan_ring:
 invalid_shared_orphan_ring:
+	rte_lcore_callback_unregister(bd->lcore_callback_handle);
 no_mem_for_stacks:
-	for (i = 0; i < RTE_MAX_LCORE; i++) {
-		rte_free(bd->buckets[i]);
-		rte_ring_free(bd->adoption_buffer_rings[i]);
-	}
 	rte_free(bd);
 no_mem_for_data:
 	rte_errno = -rc;
@@ -512,16 +548,12 @@ bucket_alloc(struct rte_mempool *mp)
 static void
 bucket_free(struct rte_mempool *mp)
 {
-	unsigned int i;
 	struct bucket_data *bd = mp->pool_data;
 
 	if (bd == NULL)
 		return;
 
-	for (i = 0; i < RTE_MAX_LCORE; i++) {
-		rte_free(bd->buckets[i]);
-		rte_ring_free(bd->adoption_buffer_rings[i]);
-	}
+	rte_lcore_callback_unregister(bd->lcore_callback_handle);
 
 	rte_ring_free(bd->shared_orphan_ring);
 	rte_ring_free(bd->shared_bucket_ring);
Signature · 1 line
2.23.0
#111 lore raw

PATCH v5 10/10 eal: add multiprocess disable API

In reply to David Marchand

The multiprocess feature has been implicitly enabled so far. Applications might want to explicitly disable like when using with the non-EAL registering API.

---
 app/test/test_mp_secondary.c              |  7 +++++++
 lib/librte_eal/common/eal_common_proc.c   |  4 ++--
 lib/librte_eal/common/eal_common_thread.c |  2 +-
 lib/librte_eal/common/eal_private.h       |  5 -----
 lib/librte_eal/include/rte_eal.h          | 14 ++++++++++++++
 lib/librte_eal/rte_eal_version.map        |  1 +
 6 files changed, 25 insertions(+), 8 deletions(-)
app/test/test_mp_secondary.c +7 -0
index ac15ddbf20..3a655c3400 100644
@@ -97,6 +97,13 @@ run_secondary_instances(void)
 			(1 << rte_get_master_lcore()));
 
 	ret |= launch_proc(argv1);
+	printf("### Testing rte_mp_disable() reject:\n");
+	if (rte_mp_disable()) {
+		printf("Error: rte_mp_disable() has been accepted\n");
+		ret |= -1;
+	} else {
+		printf("# Checked rte_mp_disable() is refused\n");
+	}
 	ret |= launch_proc(argv2);
 
 	ret |= !(launch_proc(argv3));
lib/librte_eal/common/eal_common_proc.c +2 -2
index f0cf2b65d2..d35b9c0380 100644
@@ -1236,7 +1236,7 @@ rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
 
 /* Internally, the status of the mp feature is represented as a three-state:
  * - "unknown" as long as no secondary process attached to a primary process
- *   and there was no call to rte_mp_(dis|en)able yet,
+ *   and there was no call to rte_mp_disable yet,
  * - "enabled" as soon as a secondary process attaches to a primary process,
  * - "disabled" when a primary process successfully called rte_mp_disable,
  */
@@ -1263,7 +1263,7 @@ set_mp_status(enum mp_status status)
 }
 
 bool
-eal_disable_multiprocess(void)
+rte_mp_disable(void)
 {
 	return set_mp_status(MP_STATUS_DISABLED);
 }
lib/librte_eal/common/eal_common_thread.c +1 -1
index ea09a4f3e0..73a055902a 100644
@@ -261,7 +261,7 @@ rte_thread_register(void)
 		rte_errno = EINVAL;
 		return -1;
 	}
-	if (!eal_disable_multiprocess()) {
+	if (!rte_mp_disable()) {
 		RTE_LOG(ERR, EAL, "Multiprocess in use, registering non-EAL threads is not supported.\n");
 		rte_errno = EINVAL;
 		return -1;
lib/librte_eal/common/eal_private.h +0 -5
index 535e008474..a917727e41 100644
@@ -733,11 +733,6 @@ void __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset);
  */
 void __rte_thread_uninit(void);
 
-/**
- * Mark primary process as not supporting multi-process.
- */
-bool eal_disable_multiprocess(void);
-
 /**
  * Instruct primary process that a secondary process wants to attach.
  */
lib/librte_eal/include/rte_eal.h +14 -0
index 0913d1947c..fc7db2d427 100644
@@ -151,6 +151,20 @@ int rte_eal_cleanup(void);
  */
 int rte_eal_primary_proc_alive(const char *config_file_path);
 
+/**
+ * Disable multiprocess.
+ *
+ * This function can be called to indicate that multiprocess won't be used for
+ * the rest of the application life.
+ *
+ * @return
+ *   - true if called from a primary process that had no secondary processes
+ *     attached,
+ *   - false, otherwise.
+ */
+__rte_experimental
+bool rte_mp_disable(void);
+
 #define RTE_MP_MAX_FD_NUM	8    /* The max amount of fds */
 #define RTE_MP_MAX_NAME_LEN	64   /* The max length of action name */
 #define RTE_MP_MAX_PARAM_LEN	256  /* The max length of param */
lib/librte_eal/rte_eal_version.map +1 -0
index 3aeb5b11ab..d17f13f8d3 100644
@@ -399,6 +399,7 @@ EXPERIMENTAL {
 	rte_lcore_callback_unregister;
 	rte_lcore_dump;
 	rte_lcore_iterate;
+	rte_mp_disable;
 	rte_thread_register;
 	rte_thread_unregister;
 };
Signature · 1 line
2.23.0
#112 lore raw

v6 cover Register non-EAL threads as lcore

In reply to David Marchand

OVS and some other applications have been hacking into DPDK internals to fake EAL threads and avoid performance penalty of only having non-EAL threads.

This series proposes to add a new type of lcores and maps those threads to such lcores.
non-EAL threads won't run the DPDK eal mainloop.
As a consequence, part of the EAL threads API cannot work.

Having new lcores appearing during the process lifetime is not expected by some DPDK components. This is addressed by introducing init/uninit callacks invoked when hotplugging of such lcore.

There is still some work/discussion:
- refuse new lcore role in incompatible EAL threads API (or document it only as those API were already incompatible?),
- think about deprecation notices for existing RTE_FOREACH_LCORE macros and consorts, it is probably worth discussing on how to iterate over lcores,

For the interested parties, I have a patch [1] against dpdk-latest OVS branch that makes use of this series (this patch probably won't work with v5, it will be rebased once dpdk side is ready).

1: https://patchwork.ozlabs.org/project/openvswitch/patch/[email protected]/

Changes since v5:
- fixed windows build,

Changes since v4:
- added separate API to control mp feature activation,
- addressed Konstantin and Olivier comments,

Changes since v3:
- added init failure when trying to use in conjunction with multiprocess,
- addressed Andrew comments,

Changes since v2:
- fixed windows build error due to missing trace stub,
- fixed bug when rolling back on lcore register,

Changes since v1:
- rebased on master (conflicts on merged Windows series),
- separated lcore role code cleanup in a patch,
- tried to use a single naming, so kept non-EAL threads as the main notion. non-EAL threads are then distinguished between registered and unregistered non-EAL threads,
- added unit tests (still missing some coverage, marked with a FIXME),
- reworked callbacks call under a common rwlock lock which protects lcores allocations and callbacks registration,
- introduced lcore iterators and converted the bucket mempool driver,

Signature · 48 lines
David Marchand

David Marchand (10):
  eal: relocate per thread symbols to common
  eal: fix multiple definition of per lcore thread id
  eal: introduce thread init helper
  eal: introduce thread uninit helper
  eal: move lcore role code
  eal: register non-EAL threads as lcores
  eal: add lcore init callbacks
  eal: add lcore iterators
  mempool/bucket: handle non-EAL lcores
  eal: add multiprocess disable API

 MAINTAINERS                                   |   1 +
 app/test/Makefile                             |   1 +
 app/test/autotest_data.py                     |   6 +
 app/test/meson.build                          |   2 +
 app/test/test_lcores.c                        | 373 ++++++++++++++++++
 app/test/test_mp_secondary.c                  |   7 +
 doc/guides/howto/debug_troubleshoot.rst       |   5 +-
 .../prog_guide/env_abstraction_layer.rst      |  22 +-
 doc/guides/prog_guide/mempool_lib.rst         |   2 +-
 doc/guides/rel_notes/release_20_08.rst        |   6 +
 drivers/mempool/bucket/rte_mempool_bucket.c   | 130 +++---
 lib/librte_eal/common/eal_common_lcore.c      | 249 +++++++++++-
 lib/librte_eal/common/eal_common_proc.c       |  41 ++
 lib/librte_eal/common/eal_common_thread.c     | 141 +++++--
 lib/librte_eal/common/eal_common_trace.c      |  49 ++-
 lib/librte_eal/common/eal_memcfg.h            |   2 +
 lib/librte_eal/common/eal_private.h           |  39 ++
 lib/librte_eal/common/eal_thread.h            |  21 +-
 lib/librte_eal/common/eal_trace.h             |   1 +
 lib/librte_eal/freebsd/eal.c                  |  20 +-
 lib/librte_eal/freebsd/eal_thread.c           |  38 +-
 lib/librte_eal/include/rte_eal.h              |  26 +-
 lib/librte_eal/include/rte_lcore.h            | 179 ++++++++-
 lib/librte_eal/linux/eal.c                    |  21 +-
 lib/librte_eal/linux/eal_thread.c             |  38 +-
 lib/librte_eal/rte_eal_version.map            |  16 +
 lib/librte_eal/windows/eal.c                  |  16 +-
 lib/librte_eal/windows/eal_thread.c           |  14 +-
 lib/librte_mempool/rte_mempool.h              |  11 +-
 29 files changed, 1234 insertions(+), 243 deletions(-)
 create mode 100644 app/test/test_lcores.c

-- 
2.23.0
#113 lore raw

PATCH v6 1/10 eal: relocate per thread symbols to common

We have per lcore thread symbols scattered in OS implementations but common code relies on them.
Move all of them in common.

RTE_PER_LCORE(_socket_id) and RTE_PER_LCORE(_cpuset) have public accessors and are not exported through the library map, they can be made static.

---
 lib/librte_eal/common/eal_common_thread.c | 5 ++++-
 lib/librte_eal/freebsd/eal_thread.c       | 4 ----
 lib/librte_eal/include/rte_lcore.h        | 1 -
 lib/librte_eal/linux/eal_thread.c         | 4 ----
 lib/librte_eal/windows/eal_thread.c       | 4 ----
 5 files changed, 4 insertions(+), 14 deletions(-)
lib/librte_eal/common/eal_common_thread.c +4 -1
index a341070926..7be80c292e 100644
@@ -21,7 +21,10 @@
 #include "eal_private.h"
 #include "eal_thread.h"
 
-RTE_DECLARE_PER_LCORE(unsigned , _socket_id);
+RTE_DEFINE_PER_LCORE(unsigned int, _lcore_id) = LCORE_ID_ANY;
+static RTE_DEFINE_PER_LCORE(unsigned int, _socket_id) =
+	(unsigned int)SOCKET_ID_ANY;
+static RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
 
 unsigned rte_socket_id(void)
 {
lib/librte_eal/freebsd/eal_thread.c +0 -4
index b52019782a..40676d9ef5 100644
@@ -25,10 +25,6 @@
 #include "eal_private.h"
 #include "eal_thread.h"
 
-RTE_DEFINE_PER_LCORE(unsigned, _lcore_id) = LCORE_ID_ANY;
-RTE_DEFINE_PER_LCORE(unsigned, _socket_id) = (unsigned)SOCKET_ID_ANY;
-RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
-
 /*
  * Send a message to a slave lcore identified by slave_id to call a
  * function f with argument arg. Once the execution is done, the
lib/librte_eal/include/rte_lcore.h +0 -1
index 339046bc86..5c1d1926e9 100644
@@ -23,7 +23,6 @@ extern "C" {
 #define LCORE_ID_ANY     UINT32_MAX       /**< Any lcore. */
 
 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);  /**< Per thread "lcore id". */
-RTE_DECLARE_PER_LCORE(rte_cpuset_t, _cpuset); /**< Per thread "cpuset". */
 
 /**
  * Get a lcore's role.
lib/librte_eal/linux/eal_thread.c +0 -4
index cd9d6e0ebf..a52ebef3a4 100644
@@ -25,10 +25,6 @@
 #include "eal_private.h"
 #include "eal_thread.h"
 
-RTE_DEFINE_PER_LCORE(unsigned, _lcore_id) = LCORE_ID_ANY;
-RTE_DEFINE_PER_LCORE(unsigned, _socket_id) = (unsigned)SOCKET_ID_ANY;
-RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
-
 /*
  * Send a message to a slave lcore identified by slave_id to call a
  * function f with argument arg. Once the execution is done, the
lib/librte_eal/windows/eal_thread.c +0 -4
index 3dd56519c9..f12a2ec6ad 100644
@@ -16,10 +16,6 @@
 #include "eal_private.h"
 #include "eal_windows.h"
 
-RTE_DEFINE_PER_LCORE(unsigned int, _lcore_id) = LCORE_ID_ANY;
-RTE_DEFINE_PER_LCORE(unsigned int, _socket_id) = (unsigned int)SOCKET_ID_ANY;
-RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
-
 /*
  * Send a message to a slave lcore identified by slave_id to call a
  * function f with argument arg. Once the execution is done, the
Signature · 1 line
2.23.0
#114 lore raw

PATCH v6 2/10 eal: fix multiple definition of per lcore thread id

In reply to David Marchand

Because of the inline accessor + static declaration in rte_gettid(), we end up with multiple symbols for RTE_PER_LCORE(_thread_id). Each compilation unit will pay a cost when accessing this information for the first time.

$ nm build/app/dpdk-testpmd | grep per_lcore__thread_id
0000000000000054 d per_lcore__thread_id.5037
0000000000000040 d per_lcore__thread_id.5103
0000000000000048 d per_lcore__thread_id.5259
000000000000004c d per_lcore__thread_id.5259
0000000000000044 d per_lcore__thread_id.5933
0000000000000058 d per_lcore__thread_id.6261
0000000000000050 d per_lcore__thread_id.7378
000000000000005c d per_lcore__thread_id.7496
000000000000000c d per_lcore__thread_id.8016
0000000000000010 d per_lcore__thread_id.8431

Make it global as part of the DPDK_21 stable ABI.

Fixes ef76436c6834 ("eal: get unique thread id")
---
 lib/librte_eal/common/eal_common_thread.c | 1 +
 lib/librte_eal/include/rte_eal.h          | 3 ++-
 lib/librte_eal/rte_eal_version.map        | 7 +++++++
 3 files changed, 10 insertions(+), 1 deletion(-)
lib/librte_eal/common/eal_common_thread.c +1 -0
index 7be80c292e..fd13453fee 100644
@@ -22,6 +22,7 @@
 #include "eal_thread.h"
 
 RTE_DEFINE_PER_LCORE(unsigned int, _lcore_id) = LCORE_ID_ANY;
+RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
 static RTE_DEFINE_PER_LCORE(unsigned int, _socket_id) =
 	(unsigned int)SOCKET_ID_ANY;
 static RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
lib/librte_eal/include/rte_eal.h +2 -1
index 2f9ed298de..2edf8c6556 100644
@@ -447,6 +447,8 @@ enum rte_intr_mode rte_eal_vfio_intr_mode(void);
  */
 int rte_sys_gettid(void);
 
+RTE_DECLARE_PER_LCORE(int, _thread_id);
+
 /**
  * Get system unique thread id.
  *
@@ -456,7 +458,6 @@ int rte_sys_gettid(void);
  */
 static inline int rte_gettid(void)
 {
-	static RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
 	if (RTE_PER_LCORE(_thread_id) == -1)
 		RTE_PER_LCORE(_thread_id) = rte_sys_gettid();
 	return RTE_PER_LCORE(_thread_id);
lib/librte_eal/rte_eal_version.map +7 -0
index 196eef5afa..0d42d44ce9 100644
@@ -221,6 +221,13 @@ DPDK_20.0 {
 	local: *;
 };
 
+DPDK_21 {
+	global:
+
+	per_lcore__thread_id;
+
+} DPDK_20.0;
+
 EXPERIMENTAL {
 	global:
 
Signature · 1 line
2.23.0
#115 lore raw

PATCH v6 3/10 eal: introduce thread init helper

In reply to David Marchand

Introduce a helper responsible for initialising the per thread context. We can then have a unified context for EAL and non-EAL threads and remove copy/paste'd OS-specific helpers.

Per EAL thread CPU affinity setting is separated from the thread init. It is to accommodate with Windows EAL where CPU affinity is not set at the moment.
Besides, having affinity set by the master lcore in FreeBSD and Linux will make it possible to detect errors rather than panic in the child thread. But the cleanup when such an event happens is left for later.

A side-effect of this patch is that control threads can now use recursive locks (rte_gettid() was not called before).

---
Changes since v4:
- renamed rte_thread_init as __rte_thread_init and moved to eal_private.h,

Changes since v1:
- rebased on master, removed Windows workarounds wrt gettid and traces support,

---
 lib/librte_eal/common/eal_common_thread.c | 50 ++++++++++++++---------
 lib/librte_eal/common/eal_private.h       | 10 +++++
 lib/librte_eal/common/eal_thread.h        |  8 ----
 lib/librte_eal/freebsd/eal.c              | 14 ++++++-
 lib/librte_eal/freebsd/eal_thread.c       | 32 +--------------
 lib/librte_eal/linux/eal.c                | 15 ++++++-
 lib/librte_eal/linux/eal_thread.c         | 32 +--------------
 lib/librte_eal/windows/eal.c              |  3 +-
 lib/librte_eal/windows/eal_thread.c       | 10 +----
 9 files changed, 71 insertions(+), 103 deletions(-)
lib/librte_eal/common/eal_common_thread.c +30 -20
index fd13453fee..fb06f8f802 100644
@@ -71,20 +71,10 @@ eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
 	return socket_id;
 }
 
-int
-rte_thread_set_affinity(rte_cpuset_t *cpusetp)
+static void
+thread_update_affinity(rte_cpuset_t *cpusetp)
 {
-	int s;
-	unsigned lcore_id;
-	pthread_t tid;
-
-	tid = pthread_self();
-
-	s = pthread_setaffinity_np(tid, sizeof(rte_cpuset_t), cpusetp);
-	if (s != 0) {
-		RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
-		return -1;
-	}
+	unsigned int lcore_id = rte_lcore_id();
 
 	/* store socket_id in TLS for quick access */
 	RTE_PER_LCORE(_socket_id) =
@@ -94,14 +84,24 @@ rte_thread_set_affinity(rte_cpuset_t *cpusetp)
 	memmove(&RTE_PER_LCORE(_cpuset), cpusetp,
 		sizeof(rte_cpuset_t));
 
-	lcore_id = rte_lcore_id();
 	if (lcore_id != (unsigned)LCORE_ID_ANY) {
 		/* EAL thread will update lcore_config */
 		lcore_config[lcore_id].socket_id = RTE_PER_LCORE(_socket_id);
 		memmove(&lcore_config[lcore_id].cpuset, cpusetp,
 			sizeof(rte_cpuset_t));
 	}
+}
+
+int
+rte_thread_set_affinity(rte_cpuset_t *cpusetp)
+{
+	if (pthread_setaffinity_np(pthread_self(), sizeof(rte_cpuset_t),
+			cpusetp) != 0) {
+		RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
+		return -1;
+	}
 
+	thread_update_affinity(cpusetp);
 	return 0;
 }
 
@@ -147,6 +147,19 @@ eal_thread_dump_affinity(char *str, unsigned size)
 	return ret;
 }
 
+void
+__rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset)
+{
+	/* set the lcore ID in per-lcore memory area */
+	RTE_PER_LCORE(_lcore_id) = lcore_id;
+
+	/* acquire system unique id */
+	rte_gettid();
+
+	thread_update_affinity(cpuset);
+
+	__rte_trace_mem_per_thread_alloc();
+}
 
 struct rte_thread_ctrl_params {
 	void *(*start_routine)(void *);
@@ -154,7 +167,7 @@ struct rte_thread_ctrl_params {
 	pthread_barrier_t configured;
 };
 
-static void *rte_thread_init(void *arg)
+static void *ctrl_thread_init(void *arg)
 {
 	int ret;
 	struct internal_config *internal_conf =
@@ -164,8 +177,7 @@ static void *rte_thread_init(void *arg)
 	void *(*start_routine)(void *) = params->start_routine;
 	void *routine_arg = params->arg;
 
-	/* Store cpuset in TLS for quick access */
-	memmove(&RTE_PER_LCORE(_cpuset), cpuset, sizeof(rte_cpuset_t));
+	__rte_thread_init(rte_lcore_id(), cpuset);
 
 	ret = pthread_barrier_wait(&params->configured);
 	if (ret == PTHREAD_BARRIER_SERIAL_THREAD) {
@@ -173,8 +185,6 @@ static void *rte_thread_init(void *arg)
 		free(params);
 	}
 
-	__rte_trace_mem_per_thread_alloc();
-
 	return start_routine(routine_arg);
 }
 
@@ -198,7 +208,7 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name,
 
 	pthread_barrier_init(&params->configured, NULL, 2);
 
-	ret = pthread_create(thread, attr, rte_thread_init, (void *)params);
+	ret = pthread_create(thread, attr, ctrl_thread_init, (void *)params);
 	if (ret != 0) {
 		free(params);
 		return -ret;
lib/librte_eal/common/eal_private.h +10 -0
index 46bcae9305..5d8b53882d 100644
@@ -699,4 +699,14 @@ eal_get_internal_configuration(void);
 rte_usage_hook_t
 eal_get_application_usage_hook(void);
 
+/**
+ * Init per-lcore info in current thread.
+ *
+ * @param lcore_id
+ *   identifier of lcore.
+ * @param cpuset
+ *   CPU affinity for this thread.
+ */
+void __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/eal_thread.h +0 -8
index b40ed249ed..dc1fc6eb99 100644
@@ -15,14 +15,6 @@
  */
 __rte_noreturn void *eal_thread_loop(void *arg);
 
-/**
- * Init per-lcore info for master thread
- *
- * @param lcore_id
- *   identifier of master lcore
- */
-void eal_thread_init_master(unsigned lcore_id);
-
 /**
  * Get the NUMA socket id from cpu id.
  * This function is private to EAL.
lib/librte_eal/freebsd/eal.c +13 -1
index 8c75cba79a..fd577daf44 100644
@@ -845,7 +845,14 @@ rte_eal_init(int argc, char **argv)
 
 	eal_check_mem_on_local_socket();
 
-	eal_thread_init_master(config->master_lcore);
+	if (pthread_setaffinity_np(pthread_self(), sizeof(rte_cpuset_t),
+			&lcore_config[config->master_lcore].cpuset) != 0) {
+		rte_eal_init_alert("Cannot set affinity");
+		rte_errno = EINVAL;
+		return -1;
+	}
+	__rte_thread_init(config->master_lcore,
+		&lcore_config[config->master_lcore].cpuset);
 
 	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
 
@@ -876,6 +883,11 @@ rte_eal_init(int argc, char **argv)
 		snprintf(thread_name, sizeof(thread_name),
 				"lcore-slave-%d", i);
 		rte_thread_setname(lcore_config[i].thread_id, thread_name);
+
+		ret = pthread_setaffinity_np(lcore_config[i].thread_id,
+			sizeof(rte_cpuset_t), &lcore_config[i].cpuset);
+		if (ret != 0)
+			rte_panic("Cannot set affinity\n");
 	}
 
 	/*
lib/librte_eal/freebsd/eal_thread.c +1 -31
index 40676d9ef5..0788a54fe6 100644
@@ -66,29 +66,6 @@ rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
 	return rc;
 }
 
-/* set affinity for current thread */
-static int
-eal_thread_set_affinity(void)
-{
-	unsigned lcore_id = rte_lcore_id();
-
-	/* acquire system unique id  */
-	rte_gettid();
-
-	/* update EAL thread core affinity */
-	return rte_thread_set_affinity(&lcore_config[lcore_id].cpuset);
-}
-
-void eal_thread_init_master(unsigned lcore_id)
-{
-	/* set the lcore ID in per-lcore memory area */
-	RTE_PER_LCORE(_lcore_id) = lcore_id;
-
-	/* set CPU affinity */
-	if (eal_thread_set_affinity() < 0)
-		rte_panic("cannot set affinity\n");
-}
-
 /* main loop of threads */
 __rte_noreturn void *
 eal_thread_loop(__rte_unused void *arg)
@@ -113,19 +90,12 @@ eal_thread_loop(__rte_unused void *arg)
 	m2s = lcore_config[lcore_id].pipe_master2slave[0];
 	s2m = lcore_config[lcore_id].pipe_slave2master[1];
 
-	/* set the lcore ID in per-lcore memory area */
-	RTE_PER_LCORE(_lcore_id) = lcore_id;
-
-	/* set CPU affinity */
-	if (eal_thread_set_affinity() < 0)
-		rte_panic("cannot set affinity\n");
+	__rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
 
 	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
-
 	RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
 		lcore_id, thread_id, cpuset, ret == 0 ? "" : "...");
 
-	__rte_trace_mem_per_thread_alloc();
 	rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
 
 	/* read on our pipe to get commands */
lib/librte_eal/linux/eal.c +13 -2
index 3b56d14da1..bd089cdd44 100644
@@ -1184,10 +1184,16 @@ rte_eal_init(int argc, char **argv)
 
 	eal_check_mem_on_local_socket();
 
-	eal_thread_init_master(config->master_lcore);
+	if (pthread_setaffinity_np(pthread_self(), sizeof(rte_cpuset_t),
+			&lcore_config[config->master_lcore].cpuset) != 0) {
+		rte_eal_init_alert("Cannot set affinity");
+		rte_errno = EINVAL;
+		return -1;
+	}
+	__rte_thread_init(config->master_lcore,
+		&lcore_config[config->master_lcore].cpuset);
 
 	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
-
 	RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
 		config->master_lcore, (uintptr_t)thread_id, cpuset,
 		ret == 0 ? "" : "...");
@@ -1219,6 +1225,11 @@ rte_eal_init(int argc, char **argv)
 		if (ret != 0)
 			RTE_LOG(DEBUG, EAL,
 				"Cannot set name for lcore thread\n");
+
+		ret = pthread_setaffinity_np(lcore_config[i].thread_id,
+			sizeof(rte_cpuset_t), &lcore_config[i].cpuset);
+		if (ret != 0)
+			rte_panic("Cannot set affinity\n");
 	}
 
 	/*
lib/librte_eal/linux/eal_thread.c +1 -31
index a52ebef3a4..e0440c0000 100644
@@ -66,29 +66,6 @@ rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
 	return rc;
 }
 
-/* set affinity for current EAL thread */
-static int
-eal_thread_set_affinity(void)
-{
-	unsigned lcore_id = rte_lcore_id();
-
-	/* acquire system unique id  */
-	rte_gettid();
-
-	/* update EAL thread core affinity */
-	return rte_thread_set_affinity(&lcore_config[lcore_id].cpuset);
-}
-
-void eal_thread_init_master(unsigned lcore_id)
-{
-	/* set the lcore ID in per-lcore memory area */
-	RTE_PER_LCORE(_lcore_id) = lcore_id;
-
-	/* set CPU affinity */
-	if (eal_thread_set_affinity() < 0)
-		rte_panic("cannot set affinity\n");
-}
-
 /* main loop of threads */
 __rte_noreturn void *
 eal_thread_loop(__rte_unused void *arg)
@@ -113,19 +90,12 @@ eal_thread_loop(__rte_unused void *arg)
 	m2s = lcore_config[lcore_id].pipe_master2slave[0];
 	s2m = lcore_config[lcore_id].pipe_slave2master[1];
 
-	/* set the lcore ID in per-lcore memory area */
-	RTE_PER_LCORE(_lcore_id) = lcore_id;
-
-	/* set CPU affinity */
-	if (eal_thread_set_affinity() < 0)
-		rte_panic("cannot set affinity\n");
+	__rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
 
 	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
-
 	RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
 		lcore_id, (uintptr_t)thread_id, cpuset, ret == 0 ? "" : "...");
 
-	__rte_trace_mem_per_thread_alloc();
 	rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
 
 	/* read on our pipe to get commands */
lib/librte_eal/windows/eal.c +2 -1
index eb10b4ef96..9f5d019e64 100644
@@ -333,7 +333,8 @@ rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	eal_thread_init_master(config->master_lcore);
+	__rte_thread_init(config->master_lcore,
+		&lcore_config[config->master_lcore].cpuset);
 
 	bscan = rte_bus_scan();
 	if (bscan < 0) {
lib/librte_eal/windows/eal_thread.c +1 -9
index f12a2ec6ad..20889b6196 100644
@@ -53,13 +53,6 @@ rte_eal_remote_launch(lcore_function_t *f, void *arg, unsigned int slave_id)
 	return 0;
 }
 
-void
-eal_thread_init_master(unsigned int lcore_id)
-{
-	/* set the lcore ID in per-lcore memory area */
-	RTE_PER_LCORE(_lcore_id) = lcore_id;
-}
-
 /* main loop of threads */
 void *
 eal_thread_loop(void *arg __rte_unused)
@@ -84,8 +77,7 @@ eal_thread_loop(void *arg __rte_unused)
 	m2s = lcore_config[lcore_id].pipe_master2slave[0];
 	s2m = lcore_config[lcore_id].pipe_slave2master[1];
 
-	/* set the lcore ID in per-lcore memory area */
-	RTE_PER_LCORE(_lcore_id) = lcore_id;
+	__rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
 
 	RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s])\n",
 		lcore_id, (uintptr_t)thread_id, cpuset);
Signature · 1 line
2.23.0
#116 lore raw

PATCH v6 4/10 eal: introduce thread uninit helper

In reply to David Marchand

This is a preparation step for dynamically unregistering threads.

Since we explicitly allocate a per thread trace buffer in __rte_thread_init, add an internal helper to free this buffer.

---
Changes since v5:
- fixed windows build,

Changes since v4:
- renamed rte_thread_uninit and moved to eal_private.h,
- hid freeing helper,

Changes since v2:
- added missing stub for windows tracing support,
- moved free symbol to exported (experimental) ABI as a counterpart of the alloc symbol we already had,

Changes since v1:
- rebased on master, removed Windows workaround wrt traces support,

---
 lib/librte_eal/common/eal_common_thread.c |  9 +++++
 lib/librte_eal/common/eal_common_trace.c  | 49 +++++++++++++++++++----
 lib/librte_eal/common/eal_private.h       |  5 +++
 lib/librte_eal/common/eal_trace.h         |  1 +
 lib/librte_eal/windows/eal.c              |  7 +++-
 5 files changed, 63 insertions(+), 8 deletions(-)
lib/librte_eal/common/eal_common_thread.c +9 -0
index fb06f8f802..6d1c87b1c2 100644
@@ -20,6 +20,7 @@
 #include "eal_internal_cfg.h"
 #include "eal_private.h"
 #include "eal_thread.h"
+#include "eal_trace.h"
 
 RTE_DEFINE_PER_LCORE(unsigned int, _lcore_id) = LCORE_ID_ANY;
 RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
@@ -161,6 +162,14 @@ __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset)
 	__rte_trace_mem_per_thread_alloc();
 }
 
+void
+__rte_thread_uninit(void)
+{
+	trace_mem_per_thread_free();
+
+	RTE_PER_LCORE(_lcore_id) = LCORE_ID_ANY;
+}
+
 struct rte_thread_ctrl_params {
 	void *(*start_routine)(void *);
 	void *arg;
lib/librte_eal/common/eal_common_trace.c +42 -7

73 lines not on this page · raw message

lib/librte_eal/common/eal_private.h +5 -0
index 5d8b53882d..a77ac7a963 100644
@@ -709,4 +709,9 @@ eal_get_application_usage_hook(void);
  */
 void __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset);
 
+/**
+ * Uninitialize per-lcore info for current thread.
+ */
+void __rte_thread_uninit(void);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/common/eal_trace.h +1 -0
index 8f60616156..92c5951c3a 100644
@@ -106,6 +106,7 @@ int trace_metadata_create(void);
 void trace_metadata_destroy(void);
 int trace_mkdir(void);
 int trace_epoch_time_save(void);
+void trace_mem_free(void);
 void trace_mem_per_thread_free(void);
 
 /* EAL interface */
lib/librte_eal/windows/eal.c +6 -1
index 9f5d019e64..addac62ae5 100644
@@ -17,10 +17,10 @@
 #include <eal_filesystem.h>
 #include <eal_options.h>
 #include <eal_private.h>
-#include <rte_trace_point.h>
 #include <rte_vfio.h>
 
 #include "eal_hugepages.h"
+#include "eal_trace.h"
 #include "eal_windows.h"
 
 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
@@ -215,6 +215,11 @@ __rte_trace_mem_per_thread_alloc(void)
 {
 }
 
+void
+trace_mem_per_thread_free(void)
+{
+}
+
 void
 __rte_trace_point_emit_field(size_t sz, const char *field,
 	const char *type)
Signature · 1 line
2.23.0
#117 lore raw

PATCH v6 5/10 eal: move lcore role code

In reply to David Marchand

For consistency sake, move all lcore role code in the dedicated compilation unit / header.

---
 lib/librte_eal/common/eal_common_lcore.c  | 11 +++++++
 lib/librte_eal/common/eal_common_thread.c | 11 -------
 lib/librte_eal/include/rte_eal.h          |  9 ------
 lib/librte_eal/include/rte_lcore.h        | 37 ++++++++++++++---------
 4 files changed, 34 insertions(+), 34 deletions(-)
lib/librte_eal/common/eal_common_lcore.c +11 -0
index 5404922a87..86d32a3dd7 100644
@@ -63,6 +63,17 @@ rte_eal_lcore_role(unsigned int lcore_id)
 	return cfg->lcore_role[lcore_id];
 }
 
+int
+rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
+{
+	struct rte_config *cfg = rte_eal_get_configuration();
+
+	if (lcore_id >= RTE_MAX_LCORE)
+		return -EINVAL;
+
+	return cfg->lcore_role[lcore_id] == role;
+}
+
 int rte_lcore_is_enabled(unsigned int lcore_id)
 {
 	struct rte_config *cfg = rte_eal_get_configuration();
lib/librte_eal/common/eal_common_thread.c +0 -11
index 6d1c87b1c2..b1b69ea58c 100644
@@ -33,17 +33,6 @@ unsigned rte_socket_id(void)
 	return RTE_PER_LCORE(_socket_id);
 }
 
-int
-rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role)
-{
-	struct rte_config *cfg = rte_eal_get_configuration();
-
-	if (lcore_id >= RTE_MAX_LCORE)
-		return -EINVAL;
-
-	return cfg->lcore_role[lcore_id] == role;
-}
-
 static int
 eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
 {
lib/librte_eal/include/rte_eal.h +0 -9
index 2edf8c6556..0913d1947c 100644
@@ -31,15 +31,6 @@ extern "C" {
 /* Maximum thread_name length. */
 #define RTE_MAX_THREAD_NAME_LEN 16
 
-/**
- * The lcore role (used in RTE or not).
- */
-enum rte_lcore_role_t {
-	ROLE_RTE,
-	ROLE_OFF,
-	ROLE_SERVICE,
-};
-
 /**
  * The type of process in a linux, multi-process setup
  */
lib/librte_eal/include/rte_lcore.h +23 -14
index 5c1d1926e9..3968c40693 100644
@@ -24,6 +24,15 @@ extern "C" {
 
 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);  /**< Per thread "lcore id". */
 
+/**
+ * The lcore role (used in RTE or not).
+ */
+enum rte_lcore_role_t {
+	ROLE_RTE,
+	ROLE_OFF,
+	ROLE_SERVICE,
+};
+
 /**
  * Get a lcore's role.
  *
@@ -34,6 +43,20 @@ RTE_DECLARE_PER_LCORE(unsigned, _lcore_id);  /**< Per thread "lcore id". */
  */
 enum rte_lcore_role_t rte_eal_lcore_role(unsigned int lcore_id);
 
+/**
+ * Test if the core supplied has a specific role
+ *
+ * @param lcore_id
+ *   The identifier of the lcore, which MUST be between 0 and
+ *   RTE_MAX_LCORE-1.
+ * @param role
+ *   The role to be checked against.
+ * @return
+ *   Boolean value: positive if test is true; otherwise returns 0.
+ */
+int
+rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
+
 /**
  * Return the Application thread ID of the execution unit.
  *
@@ -283,20 +306,6 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name,
 		const pthread_attr_t *attr,
 		void *(*start_routine)(void *), void *arg);
 
-/**
- * Test if the core supplied has a specific role
- *
- * @param lcore_id
- *   The identifier of the lcore, which MUST be between 0 and
- *   RTE_MAX_LCORE-1.
- * @param role
- *   The role to be checked against.
- * @return
- *   Boolean value: positive if test is true; otherwise returns 0.
- */
-int
-rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
-
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.23.0
#118 lore raw

PATCH v6 6/10 eal: register non-EAL threads as lcores

In reply to David Marchand

DPDK allows calling some part of its API from a non-EAL thread but this has some limitations.
OVS (and other applications) has its own thread management but still want to avoid such limitations by hacking RTE_PER_LCORE(_lcore_id) and faking EAL threads potentially unknown of some DPDK component.

Introduce a new API to register non-EAL thread and associate them to a free lcore with a new NON_EAL role.
This role denotes lcores that do not run DPDK mainloop and as such prevents use of rte_eal_wait_lcore() and consorts.

Multiprocess is not supported as the need for cohabitation with this new feature is unclear at the moment.

---
Changes since v5:
- added release notes,
- fixed windows build,

Changes since v4:
- returned an error when rte_thread_register fails + set rte_errno,
- prefixed unit tests logs with Error: when applicable,

Changes since v2:
- refused multiprocess init once rte_thread_register got called, and vice versa,
- added warning on multiprocess in rte_thread_register doxygen,

Changes since v1:
- moved cleanup on lcore role code in patch 5,
- added unit test,
- updated documentation,
- changed naming from "external thread" to "registered non-EAL thread"

---
 MAINTAINERS                                   |   1 +
 app/test/Makefile                             |   1 +
 app/test/autotest_data.py                     |   6 +
 app/test/meson.build                          |   2 +
 app/test/test_lcores.c                        | 143 ++++++++++++++++++
 doc/guides/howto/debug_troubleshoot.rst       |   5 +-
 .../prog_guide/env_abstraction_layer.rst      |  22 +--
 doc/guides/prog_guide/mempool_lib.rst         |   2 +-
 doc/guides/rel_notes/release_20_08.rst        |   6 +
 lib/librte_eal/common/eal_common_lcore.c      |  45 +++++-
 lib/librte_eal/common/eal_common_proc.c       |  42 +++++
 lib/librte_eal/common/eal_common_thread.c     |  49 +++++-
 lib/librte_eal/common/eal_memcfg.h            |   2 +
 lib/librte_eal/common/eal_private.h           |  28 ++++
 lib/librte_eal/freebsd/eal.c                  |   4 +
 lib/librte_eal/include/rte_lcore.h            |  28 +++-
 lib/librte_eal/linux/eal.c                    |   4 +
 lib/librte_eal/rte_eal_version.map            |   4 +
 lib/librte_eal/windows/eal.c                  |   6 +
 lib/librte_mempool/rte_mempool.h              |  11 +-
 20 files changed, 388 insertions(+), 23 deletions(-)
 create mode 100644 app/test/test_lcores.c
MAINTAINERS +1 -0
index 27a7be8e64..5a0e1b48b2 100644
@@ -182,6 +182,7 @@ F: app/test/test_cycles.c
 F: app/test/test_debug.c
 F: app/test/test_eal*
 F: app/test/test_errno.c
+F: app/test/test_lcores.c
 F: app/test/test_logs.c
 F: app/test/test_memcpy*
 F: app/test/test_per_lcore.c
app/test/Makefile +1 -0
index e5440774b1..f4065271e4 100644
@@ -98,6 +98,7 @@ SRCS-$(CONFIG_RTE_LIBRTE_FLOW_CLASSIFY) += test_flow_classify.c
 endif
 
 SRCS-y += test_rwlock.c
+SRCS-y += test_lcores.c
 
 SRCS-$(CONFIG_RTE_LIBRTE_STACK) += test_stack.c
 SRCS-$(CONFIG_RTE_LIBRTE_STACK) += test_stack_perf.c
app/test/autotest_data.py +6 -0
index 238ab631b4..4b7da45e09 100644
@@ -62,6 +62,12 @@
         "Func":    rwlock_autotest,
         "Report":  None,
     },
+    {
+        "Name":    "Lcores autotest",
+        "Command": "lcores_autotest",
+        "Func":    default_autotest,
+        "Report":  None,
+    },
     {
         "Name":    "Logs autotest",
         "Command": "logs_autotest",
app/test/meson.build +2 -0
index 56591db4e0..86b4d4d490 100644
@@ -67,6 +67,7 @@ test_sources = files('commands.c',
 	'test_ipsec_perf.c',
 	'test_kni.c',
 	'test_kvargs.c',
+	'test_lcores.c',
 	'test_logs.c',
 	'test_lpm.c',
 	'test_lpm6.c',
@@ -207,6 +208,7 @@ fast_tests = [
         ['hash_autotest', true],
         ['interrupt_autotest', true],
         ['ipfrag_autotest', false],
+        ['lcores_autotest', true],
         ['logs_autotest', true],
         ['lpm_autotest', true],
         ['lpm6_autotest', true],
app/test/test_lcores.c new file +143 -0

143 lines not on this page · raw message

doc/guides/howto/debug_troubleshoot.rst +3 -2
index cef016b2fe..5a46f5fba3 100644
@@ -307,8 +307,9 @@ Custom worker function :numref:`dtg_distributor_worker`.
 
 #. Configuration issue isolation
 
-   * Identify core role using ``rte_eal_lcore_role`` to identify RTE, OFF and
-     SERVICE. Check performance functions are mapped to run on the cores.
+   * Identify core role using ``rte_eal_lcore_role`` to identify RTE, OFF,
+     SERVICE and NON_EAL. Check performance functions are mapped to run on the
+     cores.
 
    * For high-performance execution logic ensure running it on correct NUMA
      and non-master core.
doc/guides/prog_guide/env_abstraction_layer.rst +13 -9
index 48a2fec066..f64ae953d1 100644
@@ -564,9 +564,13 @@ It's also compatible with the pattern of corelist('-l') option.
 non-EAL pthread support
 ~~~~~~~~~~~~~~~~~~~~~~~
 
-It is possible to use the DPDK execution context with any user pthread (aka. Non-EAL pthreads).
-In a non-EAL pthread, the *_lcore_id* is always LCORE_ID_ANY which identifies that it is not an EAL thread with a valid, unique, *_lcore_id*.
-Some libraries will use an alternative unique ID (e.g. TID), some will not be impacted at all, and some will work but with limitations (e.g. timer and mempool libraries).
+It is possible to use the DPDK execution context with any user pthread (aka. non-EAL pthreads).
+There are two kinds of non-EAL pthreads:
+
+- a registered non-EAL pthread with a valid *_lcore_id* that was successfully assigned by calling ``rte_thread_register()``,
+- a non registered non-EAL pthread with a LCORE_ID_ANY,
+
+For non registered non-EAL pthread (with a LCORE_ID_ANY *_lcore_id*), some libraries will use an alternative unique ID (e.g. TID), some will not be impacted at all, and some will work but with limitations (e.g. timer and mempool libraries).
 
 All these impacts are mentioned in :ref:`known_issue_label` section.
 
@@ -613,9 +617,9 @@ Known Issues
 + rte_mempool
 
   The rte_mempool uses a per-lcore cache inside the mempool.
-  For non-EAL pthreads, ``rte_lcore_id()`` will not return a valid number.
-  So for now, when rte_mempool is used with non-EAL pthreads, the put/get operations will bypass the default mempool cache and there is a performance penalty because of this bypass.
-  Only user-owned external caches can be used in a non-EAL context in conjunction with ``rte_mempool_generic_put()`` and ``rte_mempool_generic_get()`` that accept an explicit cache parameter.
+  For unregistered non-EAL pthreads, ``rte_lcore_id()`` will not return a valid number.
+  So for now, when rte_mempool is used with unregistered non-EAL pthreads, the put/get operations will bypass the default mempool cache and there is a performance penalty because of this bypass.
+  Only user-owned external caches can be used in an unregistered non-EAL context in conjunction with ``rte_mempool_generic_put()`` and ``rte_mempool_generic_get()`` that accept an explicit cache parameter.
 
 + rte_ring
 
@@ -660,15 +664,15 @@ Known Issues
 
 + rte_timer
 
-  Running  ``rte_timer_manage()`` on a non-EAL pthread is not allowed. However, resetting/stopping the timer from a non-EAL pthread is allowed.
+  Running  ``rte_timer_manage()`` on an unregistered non-EAL pthread is not allowed. However, resetting/stopping the timer from a non-EAL pthread is allowed.
 
 + rte_log
 
-  In non-EAL pthreads, there is no per thread loglevel and logtype, global loglevels are used.
+  In unregistered non-EAL pthreads, there is no per thread loglevel and logtype, global loglevels are used.
 
 + misc
 
-  The debug statistics of rte_ring, rte_mempool and rte_timer are not supported in a non-EAL pthread.
+  The debug statistics of rte_ring, rte_mempool and rte_timer are not supported in an unregistered non-EAL pthread.
 
 cgroup control
 ~~~~~~~~~~~~~~
doc/guides/prog_guide/mempool_lib.rst +1 -1
index f8b430d656..e3e1f940be 100644
@@ -103,7 +103,7 @@ The maximum size of the cache is static and is defined at compilation time (CONF
 Alternatively to the internal default per-lcore local cache, an application can create and manage external caches through the ``rte_mempool_cache_create()``, ``rte_mempool_cache_free()`` and ``rte_mempool_cache_flush()`` calls.
 These user-owned caches can be explicitly passed to ``rte_mempool_generic_put()`` and ``rte_mempool_generic_get()``.
 The ``rte_mempool_default_cache()`` call returns the default internal cache if any.
-In contrast to the default caches, user-owned caches can be used by non-EAL threads too.
+In contrast to the default caches, user-owned caches can be used by unregistered non-EAL threads too.
 
 Mempool Handlers
 ------------------------
doc/guides/rel_notes/release_20_08.rst +6 -0
index c0f81d8d40..1cb44be626 100644
@@ -56,6 +56,12 @@ New Features
      Also, make sure to start the actual text at the margin.
      =========================================================
 
+* **Added non-EAL threads registration API.**
+
+  Added a new API to register non-EAL threads as lcores. This can be used by
+  applications to have its threads known of DPDK without suffering from the
+  non-EAL previous limitations in terms of performance.
+
 * **Updated PCAP driver.**
 
   Updated PCAP driver with new features and improvements, including:
lib/librte_eal/common/eal_common_lcore.c +41 -4

57 lines not on this page · raw message

lib/librte_eal/common/eal_common_proc.c +42 -0

51 lines not on this page · raw message

lib/librte_eal/common/eal_common_thread.c +48 -1

60 lines not on this page · raw message

lib/librte_eal/common/eal_memcfg.h +2 -0
index 583fcb5953..ea013a9daf 100644
@@ -41,6 +41,8 @@ struct rte_mem_config {
 	rte_rwlock_t memory_hotplug_lock;
 	/**< Indicates whether memory hotplug request is in progress. */
 
+	uint8_t mp_status; /**< Multiprocess status. */
+
 	/* memory segments and zones */
 	struct rte_fbarray memzones; /**< Memzone descriptors. */
 
lib/librte_eal/common/eal_private.h +28 -0
index a77ac7a963..e82fb80aa0 100644
@@ -396,6 +396,24 @@ uint64_t get_tsc_freq(void);
  */
 uint64_t get_tsc_freq_arch(void);
 
+/**
+ * Allocate a free lcore to associate to a non-EAL thread.
+ *
+ * @return
+ *   - the id of a lcore with role ROLE_NON_EAL on success.
+ *   - RTE_MAX_LCORE if none was available.
+ */
+unsigned int eal_lcore_non_eal_allocate(void);
+
+/**
+ * Release the lcore used by a non-EAL thread.
+ * Counterpart of eal_lcore_non_eal_allocate().
+ *
+ * @param lcore_id
+ *   The lcore with role ROLE_NON_EAL to release.
+ */
+void eal_lcore_non_eal_release(unsigned int lcore_id);
+
 /**
  * Prepare physical memory mapping
  * i.e. hugepages on Linux and
@@ -714,4 +732,14 @@ void __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset);
  */
 void __rte_thread_uninit(void);
 
+/**
+ * Mark primary process as not supporting multi-process.
+ */
+bool eal_disable_multiprocess(void);
+
+/**
+ * Instruct primary process that a secondary process wants to attach.
+ */
+bool eal_enable_multiprocess(void);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/freebsd/eal.c +4 -0
index fd577daf44..7e5001a864 100644
@@ -400,6 +400,10 @@ rte_config_init(void)
 		}
 		if (rte_eal_config_reattach() < 0)
 			return -1;
+		if (!eal_enable_multiprocess()) {
+			RTE_LOG(ERR, EAL, "Primary process refused secondary attachment\n");
+			return -1;
+		}
 		eal_mcfg_update_internal();
 		break;
 	case RTE_PROC_AUTO:
lib/librte_eal/include/rte_lcore.h +27 -1
index 3968c40693..2fd1a03275 100644
@@ -31,6 +31,7 @@ enum rte_lcore_role_t {
 	ROLE_RTE,
 	ROLE_OFF,
 	ROLE_SERVICE,
+	ROLE_NON_EAL,
 };
 
 /**
@@ -67,7 +68,8 @@ rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role);
  *   to run threads with lcore IDs 0, 1, 2 and 3 on physical core 10..
  *
  * @return
- *  Logical core ID (in EAL thread) or LCORE_ID_ANY (in non-EAL thread)
+ *  Logical core ID (in EAL thread or registered non-EAL thread) or
+ *  LCORE_ID_ANY (in unregistered non-EAL thread)
  */
 static inline unsigned
 rte_lcore_id(void)
@@ -279,6 +281,30 @@ int rte_thread_setname(pthread_t id, const char *name);
 __rte_experimental
 int rte_thread_getname(pthread_t id, char *name, size_t len);
 
+/**
+ * Register current non-EAL thread as a lcore.
+ *
+ * @note This API is not compatible with the multi-process feature:
+ * - if a primary process registers a non-EAL thread, then no secondary process
+ *   will initialise.
+ * - if a secondary process initialises successfully, trying to register a
+ *   non-EAL thread from either primary or secondary processes will always end
+ *   up with the thread getting LCORE_ID_ANY as lcore.
+ *
+ * @return
+ *   On success, return 0; otherwise return -1 with rte_errno set.
+ */
+__rte_experimental
+int
+rte_thread_register(void);
+
+/**
+ * Unregister current thread and release lcore if one was associated.
+ */
+__rte_experimental
+void
+rte_thread_unregister(void);
+
 /**
  * Create a control thread.
  *
lib/librte_eal/linux/eal.c +4 -0
index bd089cdd44..f4daf46c4e 100644
@@ -492,6 +492,10 @@ rte_config_init(void)
 		}
 		if (rte_eal_config_reattach() < 0)
 			return -1;
+		if (!eal_enable_multiprocess()) {
+			RTE_LOG(ERR, EAL, "Primary process refused secondary attachment\n");
+			return -1;
+		}
 		eal_mcfg_update_internal();
 		break;
 	case RTE_PROC_AUTO:
lib/librte_eal/rte_eal_version.map +4 -0
index 0d42d44ce9..5503dd7620 100644
@@ -393,6 +393,10 @@ EXPERIMENTAL {
 	rte_trace_point_lookup;
 	rte_trace_regexp;
 	rte_trace_save;
+
+	# added in 20.08
+	rte_thread_register;
+	rte_thread_unregister;
 };
 
 INTERNAL {
lib/librte_eal/windows/eal.c +6 -0
index addac62ae5..9fc831fe6f 100644
@@ -66,6 +66,12 @@ eal_proc_type_detect(void)
 	return ptype;
 }
 
+bool
+eal_disable_multiprocess(void)
+{
+	return true;
+}
+
 /* display usage */
 static void
 eal_usage(const char *prgname)
lib/librte_mempool/rte_mempool.h +6 -5
index 652d19f9f1..9e0ee052b3 100644
@@ -28,9 +28,9 @@
  * rte_mempool_get() or rte_mempool_put() are designed to be called from an EAL
  * thread due to the internal per-lcore cache. Due to the lack of caching,
  * rte_mempool_get() or rte_mempool_put() performance will suffer when called
- * by non-EAL threads. Instead, non-EAL threads should call
- * rte_mempool_generic_get() or rte_mempool_generic_put() with a user cache
- * created with rte_mempool_cache_create().
+ * by unregistered non-EAL threads. Instead, unregistered non-EAL threads
+ * should call rte_mempool_generic_get() or rte_mempool_generic_put() with a
+ * user cache created with rte_mempool_cache_create().
  */
 
 #include <stdio.h>
@@ -1233,7 +1233,7 @@ void rte_mempool_dump(FILE *f, struct rte_mempool *mp);
 /**
  * Create a user-owned mempool cache.
  *
- * This can be used by non-EAL threads to enable caching when they
+ * This can be used by unregistered non-EAL threads to enable caching when they
  * interact with a mempool.
  *
  * @param size
@@ -1264,7 +1264,8 @@ rte_mempool_cache_free(struct rte_mempool_cache *cache);
  * @param lcore_id
  *   The logical core id.
  * @return
- *   A pointer to the mempool cache or NULL if disabled or non-EAL thread.
+ *   A pointer to the mempool cache or NULL if disabled or unregistered non-EAL
+ *   thread.
  */
 static __rte_always_inline struct rte_mempool_cache *
 rte_mempool_default_cache(struct rte_mempool *mp, unsigned lcore_id)
Signature · 1 line
2.23.0
#119 lore raw

RE: [dpdk-dev] [PATCH v6 06/10] eal: register non-EAL threads as lcores

Quoted 14 lines

From: dev [mailto:[email protected]] On Behalf Of David Marchand
Sent: Monday, 6 July 2020 22.53

DPDK allows calling some part of its API from a non-EAL thread but this has some limitations.
OVS (and other applications) has its own thread management but still want to avoid such limitations by hacking RTE_PER_LCORE(_lcore_id) and faking EAL threads potentially unknown of some DPDK component.

Introduce a new API to register non-EAL thread and associate them to a free lcore with a new NON_EAL role.
This role denotes lcores that do not run DPDK mainloop and as such prevents use of rte_eal_wait_lcore() and consorts.

Quoted 8 lines

Multiprocess is not supported as the need for cohabitation with this new
feature is unclear at the moment.

---

Following up on a two+ years old patch series, triggered by the discussion [1] about statistics counters in the mempool library...

The rte_lcore_role_t enum values are missing the RTE_ prefix, and the meanings of the four values should be documented with comments in the source code.

Also, the documentation [2] could benefit from some elaboration about the lcore roles. I had to read the source code in depth to understand that each "registered non-EAL thread" has a unique lcore_id, which is not used by any other EAL or non-EAL thread.

[1]: http://inbox.dpdk.org/dev/[email protected]/T/#u
[2]: http://doc.dpdk.org/guides/prog_guide/env_abstraction_layer.html#multiple-pthread

#120 lore raw

Re: [dpdk-dev] [PATCH v6 06/10] eal: register non-EAL threads as lcores

03/11/2022 10:02, Morten Brørup:

Quoted 23 lines
Quoted 14 lines

From: dev [mailto:[email protected]] On Behalf Of David Marchand
Sent: Monday, 6 July 2020 22.53

DPDK allows calling some part of its API from a non-EAL thread but this has some limitations.
OVS (and other applications) has its own thread management but still want to avoid such limitations by hacking RTE_PER_LCORE(_lcore_id) and faking EAL threads potentially unknown of some DPDK component.

Introduce a new API to register non-EAL thread and associate them to a free lcore with a new NON_EAL role.
This role denotes lcores that do not run DPDK mainloop and as such prevents use of rte_eal_wait_lcore() and consorts.

Quoted 8 lines

Multiprocess is not supported as the need for cohabitation with this new
feature is unclear at the moment.

---

Quoted 5 lines

Following up on a two+ years old patch series, triggered by the discussion [1] about statistics counters in the mempool library...

The rte_lcore_role_t enum values are missing the RTE_ prefix, and the meanings of the four values should be documented with comments in the source code.

Also, the documentation [2] could benefit from some elaboration about the lcore roles. I had to read the source code in depth to understand that each "registered non-EAL thread" has a unique lcore_id, which is not used by any other EAL or non-EAL thread.

You probably have the best point of view to improve the documentation. Feel free to work on a patch.

Quoted 2 lines

[1]: http://inbox.dpdk.org/dev/[email protected]/T/#u
[2]: http://doc.dpdk.org/guides/prog_guide/env_abstraction_layer.html#multiple-pthread

#121 lore raw

PATCH v6 7/10 eal: add lcore init callbacks

In reply to David Marchand

DPDK components and applications can have their say when a new lcore is initialized. For this, they can register a callback for initializing and releasing their private data.

---
Changes since v4:
- fixed leak on callback register failure,
- fixed nits from Konstantin and Olivier,
- prefixed unit tests logs with Error: when applicable,

Changes since v2:
- added missing test,
- fixed rollback on lcore register,

Changes since v1:
- added unit test (since missing some coverage, for v3),
- preferred callback and removed mention of notification,

---
 app/test/test_lcores.c                   | 227 +++++++++++++++++++++++
 lib/librte_eal/common/eal_common_lcore.c | 146 ++++++++++++++-
 lib/librte_eal/common/eal_private.h      |   3 +-
 lib/librte_eal/include/rte_lcore.h       |  70 +++++++
 lib/librte_eal/rte_eal_version.map       |   2 +
 5 files changed, 442 insertions(+), 6 deletions(-)
app/test/test_lcores.c +227 -0
index afb9cdd444..7df827b4e8 100644
@@ -5,6 +5,7 @@
 #include <pthread.h>
 #include <string.h>
 
+#include <rte_common.h>
 #include <rte_errno.h>
 #include <rte_lcore.h>
 
@@ -117,6 +118,226 @@ test_non_eal_lcores(unsigned int eal_threads_count)
 	return ret;
 }
 
+struct limit_lcore_context {
+	unsigned int init;
+	unsigned int max;
+	unsigned int uninit;
+};
+
+static int
+limit_lcores_init(unsigned int lcore_id __rte_unused, void *arg)
+{
+	struct limit_lcore_context *l = arg;
+
+	l->init++;
+	if (l->init > l->max)
+		return -1;
+	return 0;
+}
+
+static void
+limit_lcores_uninit(unsigned int lcore_id __rte_unused, void *arg)
+{
+	struct limit_lcore_context *l = arg;
+
+	l->uninit++;
+}
+
+static int
+test_lcores_callback(unsigned int eal_threads_count)
+{
+	struct limit_lcore_context l;
+	void *handle;
+
+	/* Refuse last lcore => callback register error. */
+	memset(&l, 0, sizeof(l));
+	l.max = eal_threads_count - 1;
+	handle = rte_lcore_callback_register("limit", limit_lcores_init,
+		limit_lcores_uninit, &l);
+	if (handle != NULL) {
+		printf("Error: lcore callback register should have failed\n");
+		goto error;
+	}
+	/* Refusal happens at the n th call to the init callback.
+	 * Besides, n - 1 were accepted, so we expect as many uninit calls when
+	 * the rollback happens.
+	 */
+	if (l.init != eal_threads_count) {
+		printf("Error: lcore callback register failed but incorrect init calls, expected %u, got %u\n",
+			eal_threads_count, l.init);
+		goto error;
+	}
+	if (l.uninit != eal_threads_count - 1) {
+		printf("Error: lcore callback register failed but incorrect uninit calls, expected %u, got %u\n",
+			eal_threads_count - 1, l.uninit);
+		goto error;
+	}
+
+	/* Accept all lcore and unregister. */
+	memset(&l, 0, sizeof(l));
+	l.max = eal_threads_count;
+	handle = rte_lcore_callback_register("limit", limit_lcores_init,
+		limit_lcores_uninit, &l);
+	if (handle == NULL) {
+		printf("Error: lcore callback register failed\n");
+		goto error;
+	}
+	if (l.uninit != 0) {
+		printf("Error: lcore callback register succeeded but incorrect uninit calls, expected 0, got %u\n",
+			l.uninit);
+		goto error;
+	}
+	rte_lcore_callback_unregister(handle);
+	handle = NULL;
+	if (l.init != eal_threads_count) {
+		printf("Error: lcore callback unregister done but incorrect init calls, expected %u, got %u\n",
+			eal_threads_count, l.init);
+		goto error;
+	}
+	if (l.uninit != eal_threads_count) {
+		printf("Error: lcore callback unregister done but incorrect uninit calls, expected %u, got %u\n",
+			eal_threads_count, l.uninit);
+		goto error;
+	}
+
+	return 0;
+
+error:
+	if (handle != NULL)
+		rte_lcore_callback_unregister(handle);
+
+	return -1;
+}
+
+static int
+test_non_eal_lcores_callback(unsigned int eal_threads_count)
+{
+	struct thread_context thread_contexts[2];
+	unsigned int non_eal_threads_count = 0;
+	struct limit_lcore_context l[2] = {};
+	unsigned int registered_count = 0;
+	struct thread_context *t;
+	void *handle[2] = {};
+	unsigned int i;
+	int ret;
+
+	/* This test requires two empty slots to be sure lcore init refusal is
+	 * because of callback execution.
+	 */
+	if (eal_threads_count + 2 >= RTE_MAX_LCORE)
+		return 0;
+
+	/* Register two callbacks:
+	 * - first one accepts any lcore,
+	 * - second one accepts all EAL lcore + one more for the first non-EAL
+	 *   thread, then refuses the next lcore.
+	 */
+	l[0].max = UINT_MAX;
+	handle[0] = rte_lcore_callback_register("no_limit", limit_lcores_init,
+		limit_lcores_uninit, &l[0]);
+	if (handle[0] == NULL) {
+		printf("Error: lcore callback [0] register failed\n");
+		goto error;
+	}
+	l[1].max = eal_threads_count + 1;
+	handle[1] = rte_lcore_callback_register("limit", limit_lcores_init,
+		limit_lcores_uninit, &l[1]);
+	if (handle[1] == NULL) {
+		printf("Error: lcore callback [1] register failed\n");
+		goto error;
+	}
+	if (l[0].init != eal_threads_count || l[1].init != eal_threads_count) {
+		printf("Error: lcore callbacks register succeeded but incorrect init calls, expected %u, %u, got %u, %u\n",
+			eal_threads_count, eal_threads_count,
+			l[0].init, l[1].init);
+		goto error;
+	}
+	if (l[0].uninit != 0 || l[1].uninit != 0) {
+		printf("Error: lcore callbacks register succeeded but incorrect uninit calls, expected 0, 1, got %u, %u\n",
+			l[0].uninit, l[1].uninit);
+		goto error;
+	}
+	/* First thread that expects a valid lcore id. */
+	t = &thread_contexts[0];
+	t->state = INIT;
+	t->registered_count = &registered_count;
+	t->lcore_id_any = false;
+	if (pthread_create(&t->id, NULL, thread_loop, t) != 0)
+		goto cleanup_threads;
+	non_eal_threads_count++;
+	while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
+			non_eal_threads_count)
+		;
+	if (l[0].init != eal_threads_count + 1 ||
+			l[1].init != eal_threads_count + 1) {
+		printf("Error: incorrect init calls, expected %u, %u, got %u, %u\n",
+			eal_threads_count + 1, eal_threads_count + 1,
+			l[0].init, l[1].init);
+		goto cleanup_threads;
+	}
+	if (l[0].uninit != 0 || l[1].uninit != 0) {
+		printf("Error: incorrect uninit calls, expected 0, 0, got %u, %u\n",
+			l[0].uninit, l[1].uninit);
+		goto cleanup_threads;
+	}
+	/* Second thread, that expects LCORE_ID_ANY because of init refusal. */
+	t = &thread_contexts[1];
+	t->state = INIT;
+	t->registered_count = &registered_count;
+	t->lcore_id_any = true;
+	if (pthread_create(&t->id, NULL, thread_loop, t) != 0)
+		goto cleanup_threads;
+	non_eal_threads_count++;
+	while (__atomic_load_n(&registered_count, __ATOMIC_ACQUIRE) !=
+			non_eal_threads_count)
+		;
+	if (l[0].init != eal_threads_count + 2 ||
+			l[1].init != eal_threads_count + 2) {
+		printf("Error: incorrect init calls, expected %u, %u, got %u, %u\n",
+			eal_threads_count + 2, eal_threads_count + 2,
+			l[0].init, l[1].init);
+		goto cleanup_threads;
+	}
+	if (l[0].uninit != 1 || l[1].uninit != 0) {
+		printf("Error: incorrect uninit calls, expected 1, 0, got %u, %u\n",
+			l[0].uninit, l[1].uninit);
+		goto cleanup_threads;
+	}
+	/* Release all threads, and check their states. */
+	__atomic_store_n(&registered_count, 0, __ATOMIC_RELEASE);
+	ret = 0;
+	for (i = 0; i < non_eal_threads_count; i++) {
+		t = &thread_contexts[i];
+		pthread_join(t->id, NULL);
+		if (t->state != DONE)
+			ret = -1;
+	}
+	if (ret < 0)
+		goto error;
+	if (l[0].uninit != 2 || l[1].uninit != 1) {
+		printf("Error: threads reported having successfully registered and unregistered, but incorrect uninit calls, expected 2, 1, got %u, %u\n",
+			l[0].uninit, l[1].uninit);
+		goto error;
+	}
+	rte_lcore_callback_unregister(handle[0]);
+	rte_lcore_callback_unregister(handle[1]);
+	return 0;
+
+cleanup_threads:
+	/* Release all threads */
+	__atomic_store_n(&registered_count, 0, __ATOMIC_RELEASE);
+	for (i = 0; i < non_eal_threads_count; i++) {
+		t = &thread_contexts[i];
+		pthread_join(t->id, NULL);
+	}
+error:
+	if (handle[1] != NULL)
+		rte_lcore_callback_unregister(handle[1]);
+	if (handle[0] != NULL)
+		rte_lcore_callback_unregister(handle[0]);
+	return -1;
+}
+
 static int
 test_lcores(void)
 {
@@ -137,6 +358,12 @@ test_lcores(void)
 	if (test_non_eal_lcores(eal_threads_count) < 0)
 		return TEST_FAILED;
 
+	if (test_lcores_callback(eal_threads_count) < 0)
+		return TEST_FAILED;
+
+	if (test_non_eal_lcores_callback(eal_threads_count) < 0)
+		return TEST_FAILED;
+
 	return TEST_SUCCESS;
 }
 
lib/librte_eal/common/eal_common_lcore.c +141 -5

171 lines not on this page · raw message

lib/librte_eal/common/eal_private.h +2 -1
index e82fb80aa0..535e008474 100644
@@ -401,7 +401,8 @@ uint64_t get_tsc_freq_arch(void);
  *
  * @return
  *   - the id of a lcore with role ROLE_NON_EAL on success.
- *   - RTE_MAX_LCORE if none was available.
+ *   - RTE_MAX_LCORE if none was available or initializing was refused (see
+ *     rte_lcore_callback_register).
  */
 unsigned int eal_lcore_non_eal_allocate(void);
 
lib/librte_eal/include/rte_lcore.h +70 -0

76 lines not on this page · raw message

lib/librte_eal/rte_eal_version.map +2 -0
index 5503dd7620..c3e762c1d9 100644
@@ -395,6 +395,8 @@ EXPERIMENTAL {
 	rte_trace_save;
 
 	# added in 20.08
+	rte_lcore_callback_register;
+	rte_lcore_callback_unregister;
 	rte_thread_register;
 	rte_thread_unregister;
 };
Signature · 1 line
2.23.0
#122 lore raw

RE: [dpdk-dev] [PATCH v6 07/10] eal: add lcore init callbacks

Quoted 10 lines

From: dev [mailto:[email protected]] On Behalf Of David Marchand
Sent: Monday, 6 July 2020 22.53

DPDK components and applications can have their say when a new lcore is initialized. For this, they can register a callback for initializing and
releasing their private data.

---

David,

Shouldn't these callbacks be called from the EAL threads too, e.g. from eal_thread_loop()?

I looks like they are only called from eal_lcore_non_eal_allocate(), which is only called from rte_thread_register().

-Morten

#123 lore raw

Re: [dpdk-dev] [PATCH v6 07/10] eal: add lcore init callbacks

Quoted 9 lines · Morten Brørup
Quoted 9 lines

From: dev [mailto:[email protected]] On Behalf Of David Marchand
Sent: Monday, 6 July 2020 22.53

DPDK components and applications can have their say when a new lcore is initialized. For this, they can register a callback for initializing and
releasing their private data.

Quoted 8 lines
Quoted 2 lines

---

David,

Shouldn't these callbacks be called from the EAL threads too, e.g. from eal_thread_loop()?

I looks like they are only called from eal_lcore_non_eal_allocate(), which is only called from rte_thread_register().

These should be called for already present lcores on callback registration. See rte_lcore_callback_register().

Signature · 1 line
David Marchand
#124 lore raw

RE: [dpdk-dev] [PATCH v6 07/10] eal: add lcore init callbacks

+CC Mattias, I forgot to put you on this discussion

Quoted 2 lines

From: David Marchand [mailto:[email protected]]
Sent: Thursday, 15 December 2022 10.09

llbacks

Quoted 20 lines
Quoted 5 lines · Morten Brørup
Quoted 5 lines

From: dev [mailto:[email protected]] On Behalf Of David Marchand
Sent: Monday, 6 July 2020 22.53

DPDK components and applications can have their say when a new

lcore is

Quoted 1 line
Quoted 1 line

initialized. For this, they can register a callback for

initializing

Quoted 9 lines
Quoted 5 lines

and
releasing their private data.

---

David,

Shouldn't these callbacks be called from the EAL threads too, e.g.

Quoted 8 lines

from eal_thread_loop()?

Quoted 2 lines

I looks like they are only called from eal_lcore_non_eal_allocate(),

which is only called from rte_thread_register().

These should be called for already present lcores on callback registration.
See rte_lcore_callback_register().

That is completely useless! They need to be called from the thread itself, so they have the correct environment, thread local storage, thread id, and similar context.

Here is an example why:

http://inbox.dpdk.org/dev/DM4PR18MB4368EEE47C1FE95DD73CE3FCD2E39@DM4PR18MB4368.namprd18.prod.outlook.com/T/#m3f6bb60ce19bdbba594e1212e8f17358ac1f9d3e

When I saw them called from rte_lcore_callback_register(), I thought it was for some sort of pre-initialization or validation purposes.

#125 lore raw

Re: [dpdk-dev] [PATCH v6 07/10] eal: add lcore init callbacks

Morten,

Quoted 11 lines · Morten Brørup
Quoted 9 lines
Quoted 1 line

Shouldn't these callbacks be called from the EAL threads too, e.g.

from eal_thread_loop()?

Quoted 2 lines

I looks like they are only called from eal_lcore_non_eal_allocate(),

which is only called from rte_thread_register().

These should be called for already present lcores on callback registration.
See rte_lcore_callback_register().

That is completely useless! They need to be called from the thread itself, so they have the correct environment, thread local storage, thread id, and similar context.

If it is broken, please fix it or come with a better implementation.

At the time, existing modules were storing their per lcore/thread private info using a local array and dereferencing it using rte_lcore_id() or a criteria of their liking.
The callbacks have to be carefully written to explicitly initialise per lcore/thread private info.
I thought it was enough, plus nobody objected.

Now, some things to consider for this requirement you want to add.

If we want an application to be able to register callbacks after rte_eal_init(), we need to wake/interrupt all EAL threads from what they are doing.
Once EAL threads enter some job the application gave, they won't reread anything from EAL.

Or we could require that callbacks are registered before rte_eal_init(), the init() callback could be called as the last step once all DPDK modules are initialised but before the application calls rte_eal_remote_launch().

But the problem of needing the break EAL threads from what they are doing is also present when we want to call the uninit() callback. And there, I have no good idea.

Signature · 1 line
David Marchand
#126 lore raw

RE: [dpdk-dev] [PATCH v6 07/10] eal: add lcore init callbacks

Quoted 23 lines

From: David Marchand [mailto:[email protected]]
Sent: Friday, 16 December 2022 09.09

Morten,

Quoted 1 line · Morten Brørup
Quoted 1 line
Quoted 1 line

Shouldn't these callbacks be called from the EAL threads too,

e.g.

Quoted 3 lines
Quoted 3 lines

from eal_thread_loop()?

Quoted 2 lines

I looks like they are only called from

eal_lcore_non_eal_allocate(),

Quoted 7 lines
Quoted 5 lines

which is only called from rte_thread_register().

These should be called for already present lcores on callback registration.
See rte_lcore_callback_register().

That is completely useless! They need to be called from the thread

itself, so they have the correct environment, thread local storage, thread id, and similar context.

Quoted 8 lines

If it is broken, please fix it or come with a better implementation.

At the time, existing modules were storing their per lcore/thread private info using a local array and dereferencing it using rte_lcore_id() or a criteria of their liking.
The callbacks have to be carefully written to explicitly initialise per lcore/thread private info.
I thought it was enough, plus nobody objected.

Apparently, you were not alone thinking it was enough. :-)

Quoted 10 lines

Now, some things to consider for this requirement you want to add.

If we want an application to be able to register callbacks after rte_eal_init(), we need to wake/interrupt all EAL threads from what they are doing.
Once EAL threads enter some job the application gave, they won't reread anything from EAL.

Quoted 8 lines

Or we could require that callbacks are registered before rte_eal_init(), the init() callback could be called as the last step once all DPDK modules are initialised but before the application calls rte_eal_remote_launch().

But the problem of needing the break EAL threads from what they are doing is also present when we want to call the uninit() callback. And there, I have no good idea.

Thank you for sharing your thoughts on this, David. Very valuable insights!

I will try to determine the relevant scope and come up with a rough RFC, hoping to meet the December 25th proposal deadline for version 23.03.

-Morten

#127 lore raw

PATCH v6 8/10 eal: add lcore iterators

In reply to David Marchand

Add a helper to iterate all lcores.
The iterator callback is read-only wrt the lcores list.

Implement a dump function on top of this for debugging.

---
Changes since v2:
- added rte_lcore_dump calls in unit test, for basic check,

Changes since v1:
- introduced lcore iterators and implemented rte_lcore_dump, this iterator mechanism can then be used outside of EAL,

---
 app/test/test_lcores.c                    |  3 +
 lib/librte_eal/common/eal_common_lcore.c  | 77 ++++++++++++++++++++---
 lib/librte_eal/common/eal_common_thread.c | 16 +++--
 lib/librte_eal/common/eal_thread.h        | 13 +++-
 lib/librte_eal/freebsd/eal.c              |  2 +-
 lib/librte_eal/freebsd/eal_thread.c       |  2 +-
 lib/librte_eal/include/rte_lcore.h        | 47 +++++++++++++-
 lib/librte_eal/linux/eal.c                |  2 +-
 lib/librte_eal/linux/eal_thread.c         |  2 +-
 lib/librte_eal/rte_eal_version.map        |  2 +
 10 files changed, 143 insertions(+), 23 deletions(-)
app/test/test_lcores.c +3 -0
index 7df827b4e8..19a7ab9fce 100644
@@ -303,6 +303,7 @@ test_non_eal_lcores_callback(unsigned int eal_threads_count)
 			l[0].uninit, l[1].uninit);
 		goto cleanup_threads;
 	}
+	rte_lcore_dump(stdout);
 	/* Release all threads, and check their states. */
 	__atomic_store_n(&registered_count, 0, __ATOMIC_RELEASE);
 	ret = 0;
@@ -314,6 +315,7 @@ test_non_eal_lcores_callback(unsigned int eal_threads_count)
 	}
 	if (ret < 0)
 		goto error;
+	rte_lcore_dump(stdout);
 	if (l[0].uninit != 2 || l[1].uninit != 1) {
 		printf("Error: threads reported having successfully registered and unregistered, but incorrect uninit calls, expected 2, 1, got %u, %u\n",
 			l[0].uninit, l[1].uninit);
@@ -354,6 +356,7 @@ test_lcores(void)
 	}
 	printf("EAL threads count: %u, RTE_MAX_LCORE=%u\n", eal_threads_count,
 		RTE_MAX_LCORE);
+	rte_lcore_dump(stdout);
 
 	if (test_non_eal_lcores(eal_threads_count) < 0)
 		return TEST_FAILED;
lib/librte_eal/common/eal_common_lcore.c +67 -10

135 lines not on this page · raw message

lib/librte_eal/common/eal_common_thread.c +11 -5
index cead8ea5cf..ea09a4f3e0 100644
@@ -105,17 +105,14 @@ rte_thread_get_affinity(rte_cpuset_t *cpusetp)
 }
 
 int
-eal_thread_dump_affinity(char *str, unsigned size)
+eal_thread_dump_affinity(rte_cpuset_t *cpuset, char *str, unsigned int size)
 {
-	rte_cpuset_t cpuset;
 	unsigned cpu;
 	int ret;
 	unsigned int out = 0;
 
-	rte_thread_get_affinity(&cpuset);
-
 	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
-		if (!CPU_ISSET(cpu, &cpuset))
+		if (!CPU_ISSET(cpu, cpuset))
 			continue;
 
 		ret = snprintf(str + out,
@@ -138,6 +135,15 @@ eal_thread_dump_affinity(char *str, unsigned size)
 	return ret;
 }
 
+int
+eal_thread_dump_current_affinity(char *str, unsigned int size)
+{
+	rte_cpuset_t cpuset;
+
+	rte_thread_get_affinity(&cpuset);
+	return eal_thread_dump_affinity(&cpuset, str, size);
+}
+
 void
 __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset)
 {
lib/librte_eal/common/eal_thread.h +11 -2
index dc1fc6eb99..4a49117be8 100644
@@ -32,13 +32,15 @@ unsigned eal_cpu_socket_id(unsigned cpu_id);
 #define RTE_CPU_AFFINITY_STR_LEN            256
 
 /**
- * Dump the current pthread cpuset.
+ * Dump the cpuset as a human readable string.
  * This function is private to EAL.
  *
  * Note:
  *   If the dump size is greater than the size of given buffer,
  *   the string will be truncated and with '\0' at the end.
  *
+ * @param cpuset
+ *   The CPU affinity object to dump.
  * @param str
  *   The string buffer the cpuset will dump to.
  * @param size
@@ -47,6 +49,13 @@ unsigned eal_cpu_socket_id(unsigned cpu_id);
  *   0 for success, -1 if truncation happens.
  */
 int
-eal_thread_dump_affinity(char *str, unsigned size);
+eal_thread_dump_affinity(rte_cpuset_t *cpuset, char *str, unsigned int size);
+
+/**
+ * Dump the current thread cpuset.
+ * This is a wrapper on eal_thread_dump_affinity().
+ */
+int
+eal_thread_dump_current_affinity(char *str, unsigned int size);
 
 #endif /* EAL_THREAD_H */
lib/librte_eal/freebsd/eal.c +1 -1
index 7e5001a864..7d6f4aa09b 100644
@@ -858,7 +858,7 @@ rte_eal_init(int argc, char **argv)
 	__rte_thread_init(config->master_lcore,
 		&lcore_config[config->master_lcore].cpuset);
 
-	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
+	ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
 
 	RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
 		config->master_lcore, thread_id, cpuset,
lib/librte_eal/freebsd/eal_thread.c +1 -1
index 0788a54fe6..99b5fefc4c 100644
@@ -92,7 +92,7 @@ eal_thread_loop(__rte_unused void *arg)
 
 	__rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
 
-	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
+	ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
 	RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
 		lcore_id, thread_id, cpuset, ret == 0 ? "" : "...");
 
lib/librte_eal/include/rte_lcore.h +45 -2

59 lines not on this page · raw message

lib/librte_eal/linux/eal.c +1 -1
index f4daf46c4e..2cce19dd3d 100644
@@ -1197,7 +1197,7 @@ rte_eal_init(int argc, char **argv)
 	__rte_thread_init(config->master_lcore,
 		&lcore_config[config->master_lcore].cpuset);
 
-	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
+	ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
 	RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
 		config->master_lcore, (uintptr_t)thread_id, cpuset,
 		ret == 0 ? "" : "...");
lib/librte_eal/linux/eal_thread.c +1 -1
index e0440c0000..48a2c1124b 100644
@@ -92,7 +92,7 @@ eal_thread_loop(__rte_unused void *arg)
 
 	__rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
 
-	ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
+	ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
 	RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
 		lcore_id, (uintptr_t)thread_id, cpuset, ret == 0 ? "" : "...");
 
lib/librte_eal/rte_eal_version.map +2 -0
index c3e762c1d9..3aeb5b11ab 100644
@@ -397,6 +397,8 @@ EXPERIMENTAL {
 	# added in 20.08
 	rte_lcore_callback_register;
 	rte_lcore_callback_unregister;
+	rte_lcore_dump;
+	rte_lcore_iterate;
 	rte_thread_register;
 	rte_thread_unregister;
 };
Signature · 1 line
2.23.0
#128 lore raw

PATCH v6 9/10 mempool/bucket: handle non-EAL lcores

In reply to David Marchand

Convert to new lcore API to support non-EAL lcores.

---
Changes since v3:
- addressed Andrew comments,

---
 drivers/mempool/bucket/rte_mempool_bucket.c | 130 ++++++++++++--------
 1 file changed, 81 insertions(+), 49 deletions(-)
drivers/mempool/bucket/rte_mempool_bucket.c +81 -49
index 5ce1ef16fb..8b9daa9782 100644
@@ -55,6 +55,7 @@ struct bucket_data {
 	struct rte_ring *shared_orphan_ring;
 	struct rte_mempool *pool;
 	unsigned int bucket_mem_size;
+	void *lcore_callback_handle;
 };
 
 static struct bucket_stack *
@@ -345,6 +346,23 @@ bucket_dequeue_contig_blocks(struct rte_mempool *mp, void **first_obj_table,
 	return 0;
 }
 
+struct bucket_count_per_lcore_ctx {
+	const struct bucket_data *bd;
+	unsigned int count;
+};
+
+static int
+bucket_count_per_lcore(unsigned int lcore_id, void *arg)
+{
+	struct bucket_count_per_lcore_ctx *bplc = arg;
+
+	bplc->count += bplc->bd->obj_per_bucket *
+		bplc->bd->buckets[lcore_id]->top;
+	bplc->count +=
+		rte_ring_count(bplc->bd->adoption_buffer_rings[lcore_id]);
+	return 0;
+}
+
 static void
 count_underfilled_buckets(struct rte_mempool *mp,
 			  void *opaque,
@@ -373,23 +391,64 @@ count_underfilled_buckets(struct rte_mempool *mp,
 static unsigned int
 bucket_get_count(const struct rte_mempool *mp)
 {
-	const struct bucket_data *bd = mp->pool_data;
-	unsigned int count =
-		bd->obj_per_bucket * rte_ring_count(bd->shared_bucket_ring) +
-		rte_ring_count(bd->shared_orphan_ring);
-	unsigned int i;
+	struct bucket_count_per_lcore_ctx bplc;
 
-	for (i = 0; i < RTE_MAX_LCORE; i++) {
-		if (!rte_lcore_is_enabled(i))
-			continue;
-		count += bd->obj_per_bucket * bd->buckets[i]->top +
-			rte_ring_count(bd->adoption_buffer_rings[i]);
-	}
+	bplc.bd = mp->pool_data;
+	bplc.count = bplc.bd->obj_per_bucket *
+		rte_ring_count(bplc.bd->shared_bucket_ring);
+	bplc.count += rte_ring_count(bplc.bd->shared_orphan_ring);
 
+	rte_lcore_iterate(bucket_count_per_lcore, &bplc);
 	rte_mempool_mem_iter((struct rte_mempool *)(uintptr_t)mp,
-			     count_underfilled_buckets, &count);
+			     count_underfilled_buckets, &bplc.count);
+
+	return bplc.count;
+}
+
+static int
+bucket_init_per_lcore(unsigned int lcore_id, void *arg)
+{
+	char rg_name[RTE_RING_NAMESIZE];
+	struct bucket_data *bd = arg;
+	struct rte_mempool *mp;
+	int rg_flags;
+	int rc;
+
+	mp = bd->pool;
+	bd->buckets[lcore_id] = bucket_stack_create(mp,
+		mp->size / bd->obj_per_bucket);
+	if (bd->buckets[lcore_id] == NULL)
+		goto error;
+
+	rc = snprintf(rg_name, sizeof(rg_name), RTE_MEMPOOL_MZ_FORMAT ".a%u",
+		mp->name, lcore_id);
+	if (rc < 0 || rc >= (int)sizeof(rg_name))
+		goto error;
+
+	rg_flags = RING_F_SC_DEQ;
+	if (mp->flags & MEMPOOL_F_SP_PUT)
+		rg_flags |= RING_F_SP_ENQ;
+	bd->adoption_buffer_rings[lcore_id] = rte_ring_create(rg_name,
+		rte_align32pow2(mp->size + 1), mp->socket_id, rg_flags);
+	if (bd->adoption_buffer_rings[lcore_id] == NULL)
+		goto error;
 
-	return count;
+	return 0;
+error:
+	rte_free(bd->buckets[lcore_id]);
+	bd->buckets[lcore_id] = NULL;
+	return -1;
+}
+
+static void
+bucket_uninit_per_lcore(unsigned int lcore_id, void *arg)
+{
+	struct bucket_data *bd = arg;
+
+	rte_ring_free(bd->adoption_buffer_rings[lcore_id]);
+	bd->adoption_buffer_rings[lcore_id] = NULL;
+	rte_free(bd->buckets[lcore_id]);
+	bd->buckets[lcore_id] = NULL;
 }
 
 static int
@@ -399,7 +458,6 @@ bucket_alloc(struct rte_mempool *mp)
 	int rc = 0;
 	char rg_name[RTE_RING_NAMESIZE];
 	struct bucket_data *bd;
-	unsigned int i;
 	unsigned int bucket_header_size;
 	size_t pg_sz;
 
@@ -429,36 +487,17 @@ bucket_alloc(struct rte_mempool *mp)
 	/* eventually this should be a tunable parameter */
 	bd->bucket_stack_thresh = (mp->size / bd->obj_per_bucket) * 4 / 3;
 
+	bd->lcore_callback_handle = rte_lcore_callback_register("bucket",
+		bucket_init_per_lcore, bucket_uninit_per_lcore, bd);
+	if (bd->lcore_callback_handle == NULL) {
+		rc = -ENOMEM;
+		goto no_mem_for_stacks;
+	}
+
 	if (mp->flags & MEMPOOL_F_SP_PUT)
 		rg_flags |= RING_F_SP_ENQ;
 	if (mp->flags & MEMPOOL_F_SC_GET)
 		rg_flags |= RING_F_SC_DEQ;
-
-	for (i = 0; i < RTE_MAX_LCORE; i++) {
-		if (!rte_lcore_is_enabled(i))
-			continue;
-		bd->buckets[i] =
-			bucket_stack_create(mp, mp->size / bd->obj_per_bucket);
-		if (bd->buckets[i] == NULL) {
-			rc = -ENOMEM;
-			goto no_mem_for_stacks;
-		}
-		rc = snprintf(rg_name, sizeof(rg_name),
-			      RTE_MEMPOOL_MZ_FORMAT ".a%u", mp->name, i);
-		if (rc < 0 || rc >= (int)sizeof(rg_name)) {
-			rc = -ENAMETOOLONG;
-			goto no_mem_for_stacks;
-		}
-		bd->adoption_buffer_rings[i] =
-			rte_ring_create(rg_name, rte_align32pow2(mp->size + 1),
-					mp->socket_id,
-					rg_flags | RING_F_SC_DEQ);
-		if (bd->adoption_buffer_rings[i] == NULL) {
-			rc = -rte_errno;
-			goto no_mem_for_stacks;
-		}
-	}
-
 	rc = snprintf(rg_name, sizeof(rg_name),
 		      RTE_MEMPOOL_MZ_FORMAT ".0", mp->name);
 	if (rc < 0 || rc >= (int)sizeof(rg_name)) {
@@ -498,11 +537,8 @@ bucket_alloc(struct rte_mempool *mp)
 	rte_ring_free(bd->shared_orphan_ring);
 cannot_create_shared_orphan_ring:
 invalid_shared_orphan_ring:
+	rte_lcore_callback_unregister(bd->lcore_callback_handle);
 no_mem_for_stacks:
-	for (i = 0; i < RTE_MAX_LCORE; i++) {
-		rte_free(bd->buckets[i]);
-		rte_ring_free(bd->adoption_buffer_rings[i]);
-	}
 	rte_free(bd);
 no_mem_for_data:
 	rte_errno = -rc;
@@ -512,16 +548,12 @@ bucket_alloc(struct rte_mempool *mp)
 static void
 bucket_free(struct rte_mempool *mp)
 {
-	unsigned int i;
 	struct bucket_data *bd = mp->pool_data;
 
 	if (bd == NULL)
 		return;
 
-	for (i = 0; i < RTE_MAX_LCORE; i++) {
-		rte_free(bd->buckets[i]);
-		rte_ring_free(bd->adoption_buffer_rings[i]);
-	}
+	rte_lcore_callback_unregister(bd->lcore_callback_handle);
 
 	rte_ring_free(bd->shared_orphan_ring);
 	rte_ring_free(bd->shared_bucket_ring);
Signature · 1 line
2.23.0
#129 lore raw

PATCH v6 10/10 eal: add multiprocess disable API

In reply to David Marchand

The multiprocess feature has been implicitly enabled so far. Applications might want to explicitly disable like when using the non-EAL threads registration API.

---
Changes since v5:
- fixed windows build,

---
 app/test/test_mp_secondary.c              |  7 +++++++
 lib/librte_eal/common/eal_common_proc.c   |  7 +++----
 lib/librte_eal/common/eal_common_thread.c |  2 +-
 lib/librte_eal/common/eal_private.h       |  5 -----
 lib/librte_eal/include/rte_eal.h          | 14 ++++++++++++++
 lib/librte_eal/rte_eal_version.map        |  1 +
 lib/librte_eal/windows/eal.c              |  2 +-
 7 files changed, 27 insertions(+), 11 deletions(-)
app/test/test_mp_secondary.c +7 -0
index ac15ddbf20..3a655c3400 100644
@@ -97,6 +97,13 @@ run_secondary_instances(void)
 			(1 << rte_get_master_lcore()));
 
 	ret |= launch_proc(argv1);
+	printf("### Testing rte_mp_disable() reject:\n");
+	if (rte_mp_disable()) {
+		printf("Error: rte_mp_disable() has been accepted\n");
+		ret |= -1;
+	} else {
+		printf("# Checked rte_mp_disable() is refused\n");
+	}
 	ret |= launch_proc(argv2);
 
 	ret |= !(launch_proc(argv3));
lib/librte_eal/common/eal_common_proc.c +3 -4
index 10142683f8..d35b9c0380 100644
@@ -1236,10 +1236,9 @@ rte_mp_reply(struct rte_mp_msg *msg, const char *peer)
 
 /* Internally, the status of the mp feature is represented as a three-state:
  * - "unknown" as long as no secondary process attached to a primary process
- *   and there was no call to eal_disable_multiprocess yet,
+ *   and there was no call to rte_mp_disable yet,
  * - "enabled" as soon as a secondary process attaches to a primary process,
- * - "disabled" when a primary process successfully called
- *   eal_disable_multiprocess,
+ * - "disabled" when a primary process successfully called rte_mp_disable,
  */
 enum mp_status {
 	MP_STATUS_UNKNOWN,
@@ -1264,7 +1263,7 @@ set_mp_status(enum mp_status status)
 }
 
 bool
-eal_disable_multiprocess(void)
+rte_mp_disable(void)
 {
 	return set_mp_status(MP_STATUS_DISABLED);
 }
lib/librte_eal/common/eal_common_thread.c +1 -1
index ea09a4f3e0..73a055902a 100644
@@ -261,7 +261,7 @@ rte_thread_register(void)
 		rte_errno = EINVAL;
 		return -1;
 	}
-	if (!eal_disable_multiprocess()) {
+	if (!rte_mp_disable()) {
 		RTE_LOG(ERR, EAL, "Multiprocess in use, registering non-EAL threads is not supported.\n");
 		rte_errno = EINVAL;
 		return -1;
lib/librte_eal/common/eal_private.h +0 -5
index 535e008474..a917727e41 100644
@@ -733,11 +733,6 @@ void __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset);
  */
 void __rte_thread_uninit(void);
 
-/**
- * Mark primary process as not supporting multi-process.
- */
-bool eal_disable_multiprocess(void);
-
 /**
  * Instruct primary process that a secondary process wants to attach.
  */
lib/librte_eal/include/rte_eal.h +14 -0
index 0913d1947c..fc7db2d427 100644
@@ -151,6 +151,20 @@ int rte_eal_cleanup(void);
  */
 int rte_eal_primary_proc_alive(const char *config_file_path);
 
+/**
+ * Disable multiprocess.
+ *
+ * This function can be called to indicate that multiprocess won't be used for
+ * the rest of the application life.
+ *
+ * @return
+ *   - true if called from a primary process that had no secondary processes
+ *     attached,
+ *   - false, otherwise.
+ */
+__rte_experimental
+bool rte_mp_disable(void);
+
 #define RTE_MP_MAX_FD_NUM	8    /* The max amount of fds */
 #define RTE_MP_MAX_NAME_LEN	64   /* The max length of action name */
 #define RTE_MP_MAX_PARAM_LEN	256  /* The max length of param */
lib/librte_eal/rte_eal_version.map +1 -0
index 3aeb5b11ab..d17f13f8d3 100644
@@ -399,6 +399,7 @@ EXPERIMENTAL {
 	rte_lcore_callback_unregister;
 	rte_lcore_dump;
 	rte_lcore_iterate;
+	rte_mp_disable;
 	rte_thread_register;
 	rte_thread_unregister;
 };
lib/librte_eal/windows/eal.c +1 -1
index 9fc831fe6f..e50601dd36 100644
@@ -67,7 +67,7 @@ eal_proc_type_detect(void)
 }
 
 bool
-eal_disable_multiprocess(void)
+rte_mp_disable(void)
 {
 	return true;
 }
Signature · 1 line
2.23.0
#130 lore raw

Re: [dpdk-dev] [PATCH v6 00/10] Register non-EAL threads as lcore

In reply to David Marchand

Hi David,

Quoted 41 lines

OVS and some other applications have been hacking into DPDK internals to fake EAL threads and avoid performance penalty of only having non-EAL threads.

This series proposes to add a new type of lcores and maps those threads to such lcores.
non-EAL threads won't run the DPDK eal mainloop.
As a consequence, part of the EAL threads API cannot work.

Having new lcores appearing during the process lifetime is not expected by some DPDK components. This is addressed by introducing init/uninit callacks invoked when hotplugging of such lcore.

There is still some work/discussion:
- refuse new lcore role in incompatible EAL threads API (or document it only as those API were already incompatible?),
- think about deprecation notices for existing RTE_FOREACH_LCORE macros and consorts, it is probably worth discussing on how to iterate over lcores,

For the interested parties, I have a patch [1] against dpdk-latest OVS branch that makes use of this series (this patch probably won't work with v5, it will be rebased once dpdk side is ready).

1: https://patchwork.ozlabs.org/project/openvswitch/patch/[email protected]/

Changes since v5:
- fixed windows build,

Changes since v4:
- added separate API to control mp feature activation,
- addressed Konstantin and Olivier comments,

Changes since v3:
- added init failure when trying to use in conjunction with multiprocess,
- addressed Andrew comments,

Changes since v2:
- fixed windows build error due to missing trace stub,
- fixed bug when rolling back on lcore register,

Quoted 11 lines

Changes since v1:
- rebased on master (conflicts on merged Windows series),
- separated lcore role code cleanup in a patch,
- tried to use a single naming, so kept non-EAL threads as the main notion. non-EAL threads are then distinguished between registered and unregistered non-EAL threads,
- added unit tests (still missing some coverage, marked with a FIXME),
- reworked callbacks call under a common rwlock lock which protects lcores allocations and callbacks registration,
- introduced lcore iterators and converted the bucket mempool driver,

LGTM, just 2 nits see below.
Apart from that:
Series Acked-by: Konstantin Ananyev <[email protected]>

1.
+void
+rte_lcore_callback_unregister(void *handle)
+{

+       struct rte_config *cfg = rte_eal_get_configuration();
+       struct lcore_callback *callback = handle;
+       unsigned int lcore_id;

Seems like forgot to add formal parameter check here:
if (callback == NULL) ...

+
+ rte_rwlock_write_lock(&lcore_lock);
+ if (callback->uninit == NULL)

2.

+bool
+rte_mp_disable(void)
+{
+ return set_mp_status(MP_STATUS_DISABLED);
+}

Probably name it rte_eal_multiprocess_enable (or so) to make it clear from naming and follow more closely our own name convention.

+
+bool
+eal_enable_multiprocess(void)
+{
+ return set_mp_status(MP_STATUS_ENABLED);
+}

Might be worth to make that function public too.
Then user will have a proper pair to use:
rte_eal_multiprocess_(enable|disable).

#131 lore raw

Re: [dpdk-dev] [PATCH v6 00/10] Register non-EAL threads as lcore

Quoted 53 lines · Konstantin
Quoted 41 lines

OVS and some other applications have been hacking into DPDK internals to fake EAL threads and avoid performance penalty of only having non-EAL threads.

This series proposes to add a new type of lcores and maps those threads to such lcores.
non-EAL threads won't run the DPDK eal mainloop.
As a consequence, part of the EAL threads API cannot work.

Having new lcores appearing during the process lifetime is not expected by some DPDK components. This is addressed by introducing init/uninit callacks invoked when hotplugging of such lcore.

There is still some work/discussion:
- refuse new lcore role in incompatible EAL threads API (or document it only as those API were already incompatible?),
- think about deprecation notices for existing RTE_FOREACH_LCORE macros and consorts, it is probably worth discussing on how to iterate over lcores,

For the interested parties, I have a patch [1] against dpdk-latest OVS branch that makes use of this series (this patch probably won't work with v5, it will be rebased once dpdk side is ready).

1: https://patchwork.ozlabs.org/project/openvswitch/patch/[email protected]/

Changes since v5:
- fixed windows build,

Changes since v4:
- added separate API to control mp feature activation,
- addressed Konstantin and Olivier comments,

Changes since v3:
- added init failure when trying to use in conjunction with multiprocess,
- addressed Andrew comments,

Changes since v2:
- fixed windows build error due to missing trace stub,
- fixed bug when rolling back on lcore register,

Quoted 11 lines

Changes since v1:
- rebased on master (conflicts on merged Windows series),
- separated lcore role code cleanup in a patch,
- tried to use a single naming, so kept non-EAL threads as the main notion. non-EAL threads are then distinguished between registered and unregistered non-EAL threads,
- added unit tests (still missing some coverage, marked with a FIXME),
- reworked callbacks call under a common rwlock lock which protects lcores allocations and callbacks registration,
- introduced lcore iterators and converted the bucket mempool driver,

Quoted 3 lines

LGTM, just 2 nits see below.
Apart from that:
Series Acked-by: Konstantin Ananyev <[email protected]>

Thanks for the review.

Quoted 11 lines

1.

(quoted diff) +6 -0
+void
+rte_lcore_callback_unregister(void *handle)
+{
+       struct rte_config *cfg = rte_eal_get_configuration();
+       struct lcore_callback *callback = handle;
+       unsigned int lcore_id;

Seems like forgot to add formal parameter check here:
if (callback == NULL) ...

Indeed, fixed.

Quoted 13 lines
(quoted diff) +3 -0
+
+       rte_rwlock_write_lock(&lcore_lock);
+       if (callback->uninit == NULL)

2.

(quoted diff) +5 -0
+bool
+rte_mp_disable(void)
+{
+       return set_mp_status(MP_STATUS_DISABLED);
+}
Quoted 4 lines

Probably name it rte_eal_multiprocess_enable (or so) to make it clear from naming and follow more closely our own name convention.

Apis for mp have the rte_mp_ prefix.

lib/librte_eal/rte_eal_version.map:     rte_mp_action_register;
lib/librte_eal/rte_eal_version.map:     rte_mp_action_unregister;
lib/librte_eal/rte_eal_version.map:     rte_mp_reply;
lib/librte_eal/rte_eal_version.map:     rte_mp_sendmsg;
lib/librte_eal/rte_eal_version.map:     rte_mp_request_async;
lib/librte_eal/rte_eal_version.map:     rte_mp_request_sync;

I prefer to stick to it.

Review

Quoted 6 lines
(quoted diff) +6 -0
+
+bool
+eal_enable_multiprocess(void)
+{
+       return set_mp_status(MP_STATUS_ENABLED);
+}

I will go with __rte_mp_ to indicate the private aspect.

Quoted 5 lines

Might be worth to make that function public too.
Then user will have a proper pair to use:
rte_eal_multiprocess_(enable|disable).

I don't see the need for now.
If you feel strong about it, I can send a followup patch later.

Passed the checks again and pushed to the main branch.

Signature · 1 line
David Marchand
#132 lore raw

Re: [dpdk-dev] [PATCH v6 00/10] Register non-EAL threads as lcore

In reply to David Marchand
Quoted 21 lines · David Marchand

OVS and some other applications have been hacking into DPDK internals to fake EAL threads and avoid performance penalty of only having non-EAL threads.

This series proposes to add a new type of lcores and maps those threads to such lcores.
non-EAL threads won't run the DPDK eal mainloop.
As a consequence, part of the EAL threads API cannot work.

Having new lcores appearing during the process lifetime is not expected by some DPDK components. This is addressed by introducing init/uninit callacks invoked when hotplugging of such lcore.

There is still some work/discussion:
- refuse new lcore role in incompatible EAL threads API (or document it only as those API were already incompatible?),
- think about deprecation notices for existing RTE_FOREACH_LCORE macros and consorts, it is probably worth discussing on how to iterate over lcores,

Quoted 5 lines

For the interested parties, I have a patch [1] against dpdk-latest OVS branch that makes use of this series (this patch probably won't work with v5, it will be rebased once dpdk side is ready).

1: https://patchwork.ozlabs.org/project/openvswitch/patch/[email protected]/

Series applied with last comments from Konstantin.

Signature · 1 line
David Marchand