[dpdk-dev] [PATCH v4 6/9] eal: register non-EAL threads as lcores
Ananyev, Konstantin
konstantin.ananyev at intel.com
Mon Jun 29 16:27:18 CEST 2020
> diff --git a/lib/librte_eal/common/eal_common_lcore.c b/lib/librte_eal/common/eal_common_lcore.c
> index 86d32a3dd7..a61824a779 100644
> --- a/lib/librte_eal/common/eal_common_lcore.c
> +++ b/lib/librte_eal/common/eal_common_lcore.c
> @@ -6,13 +6,15 @@
> #include <limits.h>
> #include <string.h>
>
> -#include <rte_errno.h>
> -#include <rte_log.h>
> -#include <rte_eal.h>
> -#include <rte_lcore.h>
> #include <rte_common.h>
> #include <rte_debug.h>
> +#include <rte_eal.h>
> +#include <rte_errno.h>
> +#include <rte_lcore.h>
> +#include <rte_log.h>
> +#include <rte_spinlock.h>
>
> +#include "eal_memcfg.h"
> #include "eal_private.h"
> #include "eal_thread.h"
>
> @@ -220,3 +222,43 @@ rte_socket_id_by_idx(unsigned int idx)
> }
> return config->numa_nodes[idx];
> }
> +
> +static rte_spinlock_t lcore_lock = RTE_SPINLOCK_INITIALIZER;
> +
> +unsigned int
> +eal_lcore_non_eal_allocate(void)
> +{
> + struct rte_config *cfg = rte_eal_get_configuration();
> + unsigned int lcore_id;
> +
> + if (cfg->process_type == RTE_PROC_SECONDARY ||
> + !eal_mcfg_forbid_multiprocess()) {
> + RTE_LOG(ERR, EAL, "Multiprocess in use, cannot allocate new lcore.\n");
> + return RTE_MAX_LCORE;
> + }
> + rte_spinlock_lock(&lcore_lock);
> + for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
> + if (cfg->lcore_role[lcore_id] != ROLE_OFF)
> + continue;
> + cfg->lcore_role[lcore_id] = ROLE_NON_EAL;
> + cfg->lcore_count++;
> + break;
> + }
> + if (lcore_id == RTE_MAX_LCORE)
> + RTE_LOG(DEBUG, EAL, "No lcore available.\n");
> + rte_spinlock_unlock(&lcore_lock);
> + return lcore_id;
> +}
> +
> +void
> +eal_lcore_non_eal_release(unsigned int lcore_id)
> +{
> + struct rte_config *cfg = rte_eal_get_configuration();
> +
> + rte_spinlock_lock(&lcore_lock);
> + if (cfg->lcore_role[lcore_id] == ROLE_NON_EAL) {
> + cfg->lcore_role[lcore_id] = ROLE_OFF;
> + cfg->lcore_count--;
> + }
> + rte_spinlock_unlock(&lcore_lock);
> +}
> diff --git a/lib/librte_eal/common/eal_common_mcfg.c b/lib/librte_eal/common/eal_common_mcfg.c
> index 49d3ed0ce5..5b42d454e2 100644
> --- a/lib/librte_eal/common/eal_common_mcfg.c
> +++ b/lib/librte_eal/common/eal_common_mcfg.c
> @@ -44,6 +44,42 @@ eal_mcfg_check_version(void)
> return 0;
> }
>
> +enum mp_status {
> + MP_UNKNOWN,
> + MP_FORBIDDEN,
> + MP_ENABLED,
> +};
> +
> +static bool
> +eal_mcfg_set_mp_status(enum mp_status status)
> +{
> + struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
> + uint8_t expected;
> + uint8_t desired;
> +
> + expected = MP_UNKNOWN;
> + desired = status;
> + if (__atomic_compare_exchange_n(&mcfg->mp_status, &expected, desired,
> + false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
> + return true;
> +
> + return __atomic_load_n(&mcfg->mp_status, __ATOMIC_RELAXED) == desired;
> +}
> +
> +bool
> +eal_mcfg_forbid_multiprocess(void)
> +{
> + assert(rte_eal_get_configuration()->process_type == RTE_PROC_PRIMARY);
> + return eal_mcfg_set_mp_status(MP_FORBIDDEN);
> +}
> +
> +bool
> +eal_mcfg_enable_multiprocess(void)
> +{
> + assert(rte_eal_get_configuration()->process_type == RTE_PROC_SECONDARY);
> + return eal_mcfg_set_mp_status(MP_ENABLED);
> +}
I still don't think it is a good idea to allow to change primary proc behaviour
(allow/forbid secondary procs to attach) on the fly.
Imagine the situation - there is a primary proc (supposed to run forever)
that does rte_thread_register/rte_thread_unregister during its lifetime.
Plus from time to time user runs some secondary process to collect stats/debug
the primary one (proc-info or so).
Now behaviour of such system will be completely non-deterministic:
In some runs primary proc will do rte_thread_register() first, and then secondary
proc will be never able to attach.
In other cases - secondary will win the race, and then for primary
eal_lcore_non_eal_allocate() will always fail.
Which means different behaviour, significantly varying performance, etc.
I am not big fun to introduce such workaround at all, but at least startup flag,
will guarantee consistent behaviour: secondary proc will always fail to attach
and eal_lcore_non_eal_allocate() will always succeed
(as long as there are free lcore_ids off-course).
>From your previous mail:
> A EAL flag is a stable API from the start, as there is nothing
> describing how we can remove one.
> So a new EAL flag for an experimental API/feature seems contradictory.
Hm, yes there is a gap, but why eal flag can't also be an experimental one?
What will be the difference between flag and API call here?
We can still reserve the right to remove/change it at any time.
As another thought about startup parameters -
would it make sense to have new one: --lcore-allow-list=...?
That would limit lcore_ids available for the process.
Without this new parameter specified -
lcore_allowed_list would be equal to startup lcore list (static ones),
and no dynamic lcore allocations will be allowed.
As an example:
dpdk_app --lcores=6,7 --lcore-allow=0-100
will reserve lcore_ids 6,7 at startup (same as we do now),
and leave [0-5] and [8-100] available for dynamic usage.
> void
> eal_mcfg_update_internal(void)
> {
> diff --git a/lib/librte_eal/common/eal_common_thread.c b/lib/librte_eal/common/eal_common_thread.c
> index a7ae0691bf..1cbddc4b5b 100644
> --- a/lib/librte_eal/common/eal_common_thread.c
> +++ b/lib/librte_eal/common/eal_common_thread.c
> @@ -236,3 +236,36 @@ rte_ctrl_thread_create(pthread_t *thread, const char *name,
> pthread_join(*thread, NULL);
> return -ret;
> }
> +
> +void
> +rte_thread_register(void)
> +{
> + unsigned int lcore_id;
> + rte_cpuset_t cpuset;
> +
> + /* EAL init flushes all lcores, we can't register before. */
> + assert(internal_config.init_complete == 1);
> + if (pthread_getaffinity_np(pthread_self(), sizeof(cpuset),
> + &cpuset) != 0)
> + CPU_ZERO(&cpuset);
> + lcore_id = eal_lcore_non_eal_allocate();
> + if (lcore_id >= RTE_MAX_LCORE)
> + lcore_id = LCORE_ID_ANY;
> + rte_thread_init(lcore_id, &cpuset);
> + if (lcore_id != LCORE_ID_ANY)
> + RTE_LOG(DEBUG, EAL, "Registered non-EAL thread as lcore %u.\n",
> + lcore_id);
> +}
> +
> +void
> +rte_thread_unregister(void)
> +{
> + unsigned int lcore_id = rte_lcore_id();
> +
> + if (lcore_id != LCORE_ID_ANY)
> + eal_lcore_non_eal_release(lcore_id);
> + rte_thread_uninit();
> + if (lcore_id != LCORE_ID_ANY)
> + RTE_LOG(DEBUG, EAL, "Unregistered non-EAL thread (was lcore %u).\n",
> + lcore_id);
> +}
> diff --git a/lib/librte_eal/common/eal_memcfg.h b/lib/librte_eal/common/eal_memcfg.h
> index 583fcb5953..340e523c6a 100644
> --- a/lib/librte_eal/common/eal_memcfg.h
> +++ b/lib/librte_eal/common/eal_memcfg.h
> @@ -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; /**< Indicates whether multiprocess can be used. */
> +
> /* memory segments and zones */
> struct rte_fbarray memzones; /**< Memzone descriptors. */
>
> @@ -91,6 +93,14 @@ eal_mcfg_wait_complete(void);
> int
> eal_mcfg_check_version(void);
>
> +/* mark primary process as not supporting multi-process. */
> +bool
> +eal_mcfg_forbid_multiprocess(void);
> +
> +/* instruct primary process that a secondary process attached once. */
> +bool
> +eal_mcfg_enable_multiprocess(void);
> +
> /* set mem config as complete */
> void
> eal_mcfg_complete(void);
> diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h
> index 0592fcd694..73238ff157 100644
> --- a/lib/librte_eal/common/eal_private.h
> +++ b/lib/librte_eal/common/eal_private.h
> @@ -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
> diff --git a/lib/librte_eal/freebsd/eal.c b/lib/librte_eal/freebsd/eal.c
> index 13e5de006f..32a3d999b8 100644
> --- a/lib/librte_eal/freebsd/eal.c
> +++ b/lib/librte_eal/freebsd/eal.c
> @@ -424,6 +424,10 @@ rte_config_init(void)
> }
> if (rte_eal_config_reattach() < 0)
> return -1;
> + if (!eal_mcfg_enable_multiprocess()) {
> + RTE_LOG(ERR, EAL, "Primary process refused secondary attachment\n");
> + return -1;
> + }
> eal_mcfg_update_internal();
> break;
> case RTE_PROC_AUTO:
> diff --git a/lib/librte_eal/include/rte_lcore.h b/lib/librte_eal/include/rte_lcore.h
> index 3968c40693..43747e88df 100644
> --- a/lib/librte_eal/include/rte_lcore.h
> +++ b/lib/librte_eal/include/rte_lcore.h
> @@ -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,27 @@ 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.
> + */
> +__rte_experimental
> +void
> +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.
> *
> diff --git a/lib/librte_eal/linux/eal.c b/lib/librte_eal/linux/eal.c
> index 8894cea50a..1d90d1c0e3 100644
> --- a/lib/librte_eal/linux/eal.c
> +++ b/lib/librte_eal/linux/eal.c
> @@ -514,6 +514,10 @@ rte_config_init(void)
> }
> if (rte_eal_config_reattach() < 0)
> return -1;
> + if (!eal_mcfg_enable_multiprocess()) {
> + RTE_LOG(ERR, EAL, "Primary process refused secondary attachment\n");
> + return -1;
> + }
> eal_mcfg_update_internal();
> break;
> case RTE_PROC_AUTO:
> diff --git a/lib/librte_eal/rte_eal_version.map b/lib/librte_eal/rte_eal_version.map
> index 5831eea4b0..39c41d445d 100644
> --- a/lib/librte_eal/rte_eal_version.map
> +++ b/lib/librte_eal/rte_eal_version.map
> @@ -396,6 +396,8 @@ EXPERIMENTAL {
>
> # added in 20.08
> __rte_trace_mem_per_thread_free;
> + rte_thread_register;
> + rte_thread_unregister;
> };
>
> INTERNAL {
> diff --git a/lib/librte_mempool/rte_mempool.h b/lib/librte_mempool/rte_mempool.h
> index 652d19f9f1..9e0ee052b3 100644
> --- a/lib/librte_mempool/rte_mempool.h
> +++ b/lib/librte_mempool/rte_mempool.h
> @@ -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)
> --
> 2.23.0
More information about the dev
mailing list