From patchwork Wed Jan 6 19:45:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tal Shnaiderman X-Patchwork-Id: 86072 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (xvm-189-124.dc0.ghst.net [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 03FBAA09FF; Wed, 6 Jan 2021 20:46:12 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 79F16140E32; Wed, 6 Jan 2021 20:46:06 +0100 (CET) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by mails.dpdk.org (Postfix) with ESMTP id 14B66140E1C for ; Wed, 6 Jan 2021 20:46:03 +0100 (CET) Received: from Internal Mail-Server by MTLPINE1 (envelope-from talshn@nvidia.com) with SMTP; 6 Jan 2021 21:45:59 +0200 Received: from nvidia.com (l-wincomp04-vm.mtl.labs.mlnx [10.237.1.5]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id 106Jjxt0002786; Wed, 6 Jan 2021 21:45:59 +0200 From: Tal Shnaiderman To: dev@dpdk.org Cc: thomas@monjalon.net, pallavi.kadam@intel.com, dmitry.kozliuk@gmail.com, navasile@linux.microsoft.com, dmitrym@microsoft.com, david.marchand@redhat.com Date: Wed, 6 Jan 2021 21:45:42 +0200 Message-Id: <20210106194543.14024-2-talshn@nvidia.com> X-Mailer: git-send-email 2.16.1.windows.4 In-Reply-To: <20210106194543.14024-1-talshn@nvidia.com> References: <20210105170635.6212-2-talshn@nvidia.com> <20210106194543.14024-1-talshn@nvidia.com> Subject: [dpdk-dev] [PATCH v8 1/2] eal: move thread affinity functions to new file X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Move the definition of the functions rte_thread_set_affinity and rte_thread_get_affinity to new file, rte_thread.h The file will implement generic threading functionality and will only host threading functions which do not reference pthread API. Signed-off-by: Tal Shnaiderman --- lib/librte_eal/include/meson.build | 1 + lib/librte_eal/include/rte_lcore.h | 22 +---------------- lib/librte_eal/include/rte_thread.h | 47 +++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 lib/librte_eal/include/rte_thread.h diff --git a/lib/librte_eal/include/meson.build b/lib/librte_eal/include/meson.build index dc007084ff..0dea342e1d 100644 --- a/lib/librte_eal/include/meson.build +++ b/lib/librte_eal/include/meson.build @@ -40,6 +40,7 @@ headers += files( 'rte_service_component.h', 'rte_string_fns.h', 'rte_tailq.h', + 'rte_thread.h', 'rte_time.h', 'rte_trace.h', 'rte_trace_point.h', diff --git a/lib/librte_eal/include/rte_lcore.h b/lib/librte_eal/include/rte_lcore.h index 48b87e253a..0fe0bd839c 100644 --- a/lib/librte_eal/include/rte_lcore.h +++ b/lib/librte_eal/include/rte_lcore.h @@ -15,6 +15,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -357,27 +358,6 @@ __rte_experimental void rte_lcore_dump(FILE *f); -/** - * Set core affinity of the current thread. - * Support both EAL and non-EAL thread and update TLS. - * - * @param cpusetp - * Point to cpu_set_t for setting current thread affinity. - * @return - * On success, return 0; otherwise return -1; - */ -int rte_thread_set_affinity(rte_cpuset_t *cpusetp); - -/** - * Get core affinity of the current thread. - * - * @param cpusetp - * Point to cpu_set_t for getting current thread cpu affinity. - * It presumes input is not NULL, otherwise it causes panic. - * - */ -void rte_thread_get_affinity(rte_cpuset_t *cpusetp); - /** * Set thread names. * diff --git a/lib/librte_eal/include/rte_thread.h b/lib/librte_eal/include/rte_thread.h new file mode 100644 index 0000000000..43bf568d59 --- /dev/null +++ b/lib/librte_eal/include/rte_thread.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2021 Mellanox Technologies, Ltd + */ + +#include + +#ifndef _RTE_THREAD_H_ +#define _RTE_THREAD_H_ + +/** + * @file + * + * Threading functions + * + * Simple threads functionality supplied by EAL. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Set core affinity of the current thread. + * Support both EAL and non-EAL thread and update TLS. + * + * @param cpusetp + * Point to cpu_set_t for setting current thread affinity. + * @return + * On success, return 0; otherwise return -1; + */ +int rte_thread_set_affinity(rte_cpuset_t *cpusetp); + +/** + * Get core affinity of the current thread. + * + * @param cpusetp + * Point to cpu_set_t for getting current thread cpu affinity. + * It presumes input is not NULL, otherwise it causes panic. + * + */ +void rte_thread_get_affinity(rte_cpuset_t *cpusetp); + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_THREAD_H_ */ From patchwork Wed Jan 6 19:45:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tal Shnaiderman X-Patchwork-Id: 86071 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (xvm-189-124.dc0.ghst.net [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id CAB3EA09FF; Wed, 6 Jan 2021 20:46:05 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 5795B140E1C; Wed, 6 Jan 2021 20:46:05 +0100 (CET) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by mails.dpdk.org (Postfix) with ESMTP id 0DED240FA7 for ; Wed, 6 Jan 2021 20:46:03 +0100 (CET) Received: from Internal Mail-Server by MTLPINE1 (envelope-from talshn@nvidia.com) with SMTP; 6 Jan 2021 21:45:59 +0200 Received: from nvidia.com (l-wincomp04-vm.mtl.labs.mlnx [10.237.1.5]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id 106Jjxt1002786; Wed, 6 Jan 2021 21:45:59 +0200 From: Tal Shnaiderman To: dev@dpdk.org Cc: thomas@monjalon.net, pallavi.kadam@intel.com, dmitry.kozliuk@gmail.com, navasile@linux.microsoft.com, dmitrym@microsoft.com, david.marchand@redhat.com Date: Wed, 6 Jan 2021 21:45:43 +0200 Message-Id: <20210106194543.14024-3-talshn@nvidia.com> X-Mailer: git-send-email 2.16.1.windows.4 In-Reply-To: <20210106194543.14024-1-talshn@nvidia.com> References: <20210105170635.6212-2-talshn@nvidia.com> <20210106194543.14024-1-talshn@nvidia.com> Subject: [dpdk-dev] [PATCH v8 2/2] eal: add generic thread-local-storage functions X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Add support for TLS functionality in EAL. The following functions are added: rte_thread_tls_key_create - create a TLS data key. rte_thread_tls_key_delete - delete a TLS data key. rte_thread_tls_value_set - set value bound to the TLS key rte_thread_tls_value_get - get value bound to the TLS key TLS key is defined by the new type rte_tls_key. The API allocates the thread local storage (TLS) key. Any thread of the process can subsequently use this key to store and retrieve values that are local to the thread. Those functions are added in addition to TLS capability in rte_per_lcore.h to allow abstraction of the pthread layer for all operating systems. Windows implementation is under librte_eal/windows and implemented using WIN32 API for Windows only. Unix implementation is under librte_eal/unix and implemented using pthread for UNIX compilation. Signed-off-by: Tal Shnaiderman --- v3: switch from pthread shim to generic eal implementation [DmitryK] v4: modify file names, function names, move unix code to common for future external pthreads support [DmitryK] v5: rename var used for external pthreads, add description in meson_options.txt. [DmitryK] v6: remove external_pthread support as it collide with pthread shim implementation [DmitryK] v7: move unix code to unix instead of common, rename functions add more documentation [Thomas] --- lib/librte_eal/include/rte_thread.h | 66 ++++++++++++++++++++++++++++ lib/librte_eal/rte_eal_exports.def | 5 +++ lib/librte_eal/unix/meson.build | 1 + lib/librte_eal/unix/rte_thread.c | 86 +++++++++++++++++++++++++++++++++++++ lib/librte_eal/version.map | 6 +++ lib/librte_eal/windows/meson.build | 1 + lib/librte_eal/windows/rte_thread.c | 83 +++++++++++++++++++++++++++++++++++ 7 files changed, 248 insertions(+) create mode 100644 lib/librte_eal/unix/rte_thread.c create mode 100644 lib/librte_eal/windows/rte_thread.c diff --git a/lib/librte_eal/include/rte_thread.h b/lib/librte_eal/include/rte_thread.h index 43bf568d59..ea1a1847de 100644 --- a/lib/librte_eal/include/rte_thread.h +++ b/lib/librte_eal/include/rte_thread.h @@ -19,6 +19,11 @@ extern "C" { #endif +/** + * TLS key type, an opaque pointer. + */ +typedef struct eal_tls_key *rte_tls_key; + /** * Set core affinity of the current thread. * Support both EAL and non-EAL thread and update TLS. @@ -40,6 +45,67 @@ int rte_thread_set_affinity(rte_cpuset_t *cpusetp); */ void rte_thread_get_affinity(rte_cpuset_t *cpusetp); +/** + * Create a TLS data key visible to all threads in the process. + * the created key is later used to get/set a value. + * and optional destructor can be set to be called when a thread exits. + * + * @param key + * Pointer to store the allocated rte_tls_key + * @param destructor + * The function to be called when the thread exits. + * Ignored on Windows OS. + * + * @return + * On success, zero. + * On failure, a negative number. + */ + +__rte_experimental +int rte_thread_tls_key_create(rte_tls_key *key, void (*destructor)(void *)); + +/** + * Delete a TLS data key visible to all threads in the process + * rte_tls_key is the opaque pointer allocated by rte_thread_tls_key_create. + * + * @param key + * The rte_tls_key allocated by rte_thread_tls_key_create(). + * + * @return + * On success, zero. + * On failure, a negative number. + */ +__rte_experimental +int rte_thread_tls_key_delete(rte_tls_key key); + +/** + * Set value bound to the TLS key on behalf of the calling thread + * + * @param key + * The rte_tls_key key allocated by rte_thread_tls_key_create. + * @param value + * The value bound to the rte_tls_key key for the calling thread. + * + * @return + * On success, zero. + * On failure, a negative number. + */ +__rte_experimental +int rte_thread_tls_value_set(rte_tls_key key, const void *value); + +/** + * Get value bound to the TLS key on behalf of the calling thread + * + * @param key + * The rte_tls_key key allocated by rte_thread_tls_key_create. + * + * @return + * On success, value data pointer (can also be NULL). + * On failure, NULL and an error number is set in rte_errno. + */ +__rte_experimental +void *rte_thread_tls_value_get(rte_tls_key key); + #ifdef __cplusplus } #endif diff --git a/lib/librte_eal/rte_eal_exports.def b/lib/librte_eal/rte_eal_exports.def index 6a6be1cfa6..a11b078ac6 100644 --- a/lib/librte_eal/rte_eal_exports.def +++ b/lib/librte_eal/rte_eal_exports.def @@ -306,6 +306,11 @@ EXPORTS rte_vect_get_max_simd_bitwidth rte_vect_set_max_simd_bitwidth + rte_thread_tls_key_create + rte_thread_tls_key_delete + rte_thread_tls_value_set + rte_thread_tls_value_get + rte_mem_lock rte_mem_map rte_mem_page_size diff --git a/lib/librte_eal/unix/meson.build b/lib/librte_eal/unix/meson.build index d3af6b6fe2..71221b84a4 100644 --- a/lib/librte_eal/unix/meson.build +++ b/lib/librte_eal/unix/meson.build @@ -5,4 +5,5 @@ sources += files( 'eal_file.c', 'eal_unix_memory.c', 'eal_unix_timer.c', + 'rte_thread.c', ) diff --git a/lib/librte_eal/unix/rte_thread.c b/lib/librte_eal/unix/rte_thread.c new file mode 100644 index 0000000000..06c8093d78 --- /dev/null +++ b/lib/librte_eal/unix/rte_thread.c @@ -0,0 +1,86 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2021 Mellanox Technologies, Ltd + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +struct eal_tls_key { + pthread_key_t thread_index; +}; + +int +rte_thread_tls_key_create(rte_tls_key *key, void (*destructor)(void *)) +{ + int err; + + *key = malloc(sizeof(*key)); + if ((*key) == NULL) { + RTE_LOG(DEBUG, EAL, "Cannot allocate TLS key."); + return -1; + } + err = pthread_key_create(&((*key)->thread_index), destructor); + if (err) { + RTE_LOG(DEBUG, EAL, "pthread_key_create failed: %s\n", + strerror(err)); + free(*key); + return -1; + } + return 0; +} + +int +rte_thread_tls_key_delete(rte_tls_key key) +{ + int err; + + if (!key) { + RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); + return -1; + } + err = pthread_key_delete(key->thread_index); + if (err) { + RTE_LOG(DEBUG, EAL, "pthread_key_delete failed: %s\n", + strerror(err)); + free(key); + return -1; + } + free(key); + return 0; +} + +int +rte_thread_tls_value_set(rte_tls_key key, const void *value) +{ + int err; + + if (!key) { + RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); + return -1; + } + err = pthread_setspecific(key->thread_index, value); + if (err) { + RTE_LOG(DEBUG, EAL, "pthread_setspecific failed: %s\n", + strerror(err)); + return -1; + } + return 0; +} + +void * +rte_thread_tls_value_get(rte_tls_key key) +{ + if (!key) { + RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); + rte_errno = EINVAL; + return NULL; + } + return pthread_getspecific(key->thread_index); +} diff --git a/lib/librte_eal/version.map b/lib/librte_eal/version.map index 354c068f31..272db0504a 100644 --- a/lib/librte_eal/version.map +++ b/lib/librte_eal/version.map @@ -403,6 +403,12 @@ EXPERIMENTAL { rte_service_lcore_may_be_active; rte_vect_get_max_simd_bitwidth; rte_vect_set_max_simd_bitwidth; + + # added in 21.02 + rte_thread_tls_key_create; + rte_thread_tls_key_delete; + rte_thread_tls_value_set; + rte_thread_tls_value_get; }; INTERNAL { diff --git a/lib/librte_eal/windows/meson.build b/lib/librte_eal/windows/meson.build index 3b2faf29eb..42ff5c2d59 100644 --- a/lib/librte_eal/windows/meson.build +++ b/lib/librte_eal/windows/meson.build @@ -19,6 +19,7 @@ sources += files( 'eal_timer.c', 'fnmatch.c', 'getopt.c', + 'rte_thread.c', ) dpdk_conf.set10('RTE_EAL_NUMA_AWARE_HUGEPAGES', true) diff --git a/lib/librte_eal/windows/rte_thread.c b/lib/librte_eal/windows/rte_thread.c new file mode 100644 index 0000000000..4608bfcc5e --- /dev/null +++ b/lib/librte_eal/windows/rte_thread.c @@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright 2021 Mellanox Technologies, Ltd + */ + +#include +#include +#include +#include + +struct eal_tls_key { + DWORD thread_index; +}; + +int +rte_thread_tls_key_create(rte_tls_key *key, + __rte_unused void (*destructor)(void *)) +{ + *key = malloc(sizeof(*key)); + if ((*key) == NULL) { + RTE_LOG(DEBUG, EAL, "Cannot allocate TLS key."); + return -1; + } + (*key)->thread_index = TlsAlloc(); + if ((*key)->thread_index == TLS_OUT_OF_INDEXES) { + RTE_LOG_WIN32_ERR("TlsAlloc()"); + free(*key); + return -1; + } + return 0; +} + +int +rte_thread_tls_key_delete(rte_tls_key key) +{ + if (!key) { + RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); + return -1; + } + if (!TlsFree(key->thread_index)) { + RTE_LOG_WIN32_ERR("TlsFree()"); + free(key); + return -1; + } + free(key); + return 0; +} + +int +rte_thread_tls_value_set(rte_tls_key key, const void *value) +{ + char *p; + + if (!key) { + RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); + return -1; + } + /* discard const qualifier */ + p = (char *) (uintptr_t) value; + if (!TlsSetValue(key->thread_index, p)) { + RTE_LOG_WIN32_ERR("TlsSetValue()"); + return -1; + } + return 0; +} + +void * +rte_thread_tls_value_get(rte_tls_key key) +{ + void *output; + + if (!key) { + RTE_LOG(DEBUG, EAL, "Invalid TLS key.\n"); + rte_errno = EINVAL; + return NULL; + } + output = TlsGetValue(key->thread_index); + if (GetLastError() != ERROR_SUCCESS) { + RTE_LOG_WIN32_ERR("TlsGetValue()"); + rte_errno = ENOEXEC; + return NULL; + } + return output; +}