ticketlock: ticket based to improve fairness
PATCH 1/1 ticketlock: ticket based to improve fairness
From: Joyce Kong <[email protected]>
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time. As shown in the following test, within same period of time, there are threads taking locks much more times than the others.
The ticketlock gives each waiting thread a ticket and they can take the lock one by one, first come, first serviced, this avoids starvation for too long time and is more predictable.
*** spinlock_autotest without this patch ***
Core [0] count = 89
Core [1] count = 84
Core [2] count = 94
...
Core [208] count = 171
Core [209] count = 152
Core [210] count = 161
Core [211] count = 187
*** spinlock_autotest with this patch ***
Core [0] count = 534
Core [1] count = 533
Core [2] count = 542
...
Core [208] count = 554
Core [209] count = 556
Core [210] count = 555
Core [211] count = 551
--- .../common/include/generic/rte_ticketlock.h | 203 +++++++++++++++++++++ lib/librte_eal/rte_eal_version.map | 8 + 2 files changed, 211 insertions(+) create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +203 -0
@@ -0,0 +1,203 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2018-2019 Arm Corporation +*/ + +#ifndef _RTE_TICKETLOCK_H_ +#define _RTE_TICKETLOCK_H_ + +/** +* @file +* +* RTE ticketlocks +* +* This file defines an API for read-write locks, which are implemented +* in an architecture-specific way. This kind of lock simply waits in +* a loop repeatedly checking until the lock becomes available. +* +* All locks must be initialised before use, and only initialised once. +* +*/ + +#include <rte_lcore.h> +#include <rte_common.h> +#include <rte_pause.h> + +/** +* The rte_ticketlock_t type. +*/ +typedef struct { +uint16_t current; +uint16_t next; +}rte_ticketlock_t ; + +/** +* A static ticketlock initializer. +*/ +#define RTE_TICKETLOCK_INITIALIZER { 0 } + +/** +* Initialize the ticketlock to an unlocked state. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_init (rte_ticketlock_t *tl) +{ +__atomic_store_n (&tl->current,0 , __ATOMIC_RELAXED); +__atomic_store_n (&tl->next,0 , __ATOMIC_RELAXED); +} + +/** +* Take the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_lock (rte_ticketlock_t *tl); + +static inline __rte_experimental void +rte_ticketlock_lock (rte_ticketlock_t *tl) +{ +uint16_t me =__atomic_fetch_add (&tl->next,1 , __ATOMIC_RELAXED); +while (__atomic_load_n (&tl->current, __ATOMIC_ACQUIRE) != me) +rte_pause (); +} + +/** +* Release the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_unlock (rte_ticketlock_t *tl); + +static inline __rte_experimental void +rte_ticketlock_unlock (rte_ticketlock_t *tl) +{ +uint16_t i =__atomic_load_n (&tl->current, __ATOMIC_RELAXED); + i++; +__atomic_store_n (&tl->current, i, __ATOMIC_RELEASE); +} + +/** +* Try to take the lock. +* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock is successfully taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_trylock (rte_ticketlock_t *tl) +{ +uint16_t me =__atomic_fetch_add (&tl->next,1 , __ATOMIC_RELAXED); +while (__atomic_load_n (&tl->current, __ATOMIC_RELAXED) != me) { +__atomic_sub_fetch (&tl->next,1 , __ATOMIC_RELAXED); +return 0 ; + } + +return 1 ; +} + +/** +* Test if the lock is taken. +* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock icurrently taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_is_locked (rte_ticketlock_t *tl) +{ +return (__atomic_load_n (&tl->current, __ATOMIC_RELAXED) != +__atomic_load_n (&tl->next, __ATOMIC_RELAXED)); +} + +/** +* The rte_ticketlock_recursive_t type. +*/ +typedef struct { +rte_ticketlock_t tl;/**< the actual ticketlock */ +volatile int user;/**< core id using lock, -1 for unused */ +volatile int count;/**< count of time this lock has been called */ +}rte_ticketlock_recursive_t ; + +/** +* A static recursive ticketlock initializer. +*/ +#define RTE_TICKETLOCK_RECURSIVE_INITIALIZER {RTE_TICKETLOCK_INITIALIZER, -1, 0} + +/** +* Initialize the recursive ticketlock to an unlocked state. +* +* @param tlr +* A pointer to the recursive ticketlock. +*/ +static inline __rte_experimental void rte_ticketlock_recursive_init( +rte_ticketlock_recursive_t *tlr) +{ +rte_ticketlock_init (&tlr->tl); + tlr->user =-1 ; + tlr->count =0 ; +} + +/** +* Take the recursive ticketlock. +* +* @param tlr +* A pointer to the recursive ticketlock. +*/ +static inline __rte_experimental void rte_ticketlock_recursive_lock( +rte_ticketlock_recursive_t *tlr) +{ +int id =rte_gettid (); + +if (tlr->user != id) { +rte_ticketlock_lock (&tlr->tl); + tlr->user = id; + } + tlr->count++; +} +/** +* Release the recursive ticketlock. +* +* @param tlr +* A pointer to the recursive ticketlock. +*/ +static inline __rte_experimental void rte_ticketlock_recursive_unlock( +rte_ticketlock_recursive_t *tlr) +{ +if (--(tlr->count) ==0 ) { + tlr->user =-1 ; +rte_ticketlock_unlock (&tlr->tl); + } + +} + +/** +* Try to take the recursive lock. +* +* @param tlr +* A pointer to the recursive ticketlock. +* @return +* 1 if the lock is successfully taken; 0 otherwise. +*/ +static inline __rte_experimental int rte_ticketlock_recursive_trylock( +rte_ticketlock_recursive_t *tlr) +{ +int id =rte_gettid (); + +if (tlr->user != id) { +if (rte_ticketlock_trylock (&tlr->tl) ==0 ) +return 0 ; + tlr->user = id; + } + tlr->count++; +return 1 ; +} + +#endif /* _RTE_TICKETLOCK_H_ */
lib/librte_eal/rte_eal_version.map +8 -0
@@ -364,4 +364,12 @@ EXPERIMENTAL { rte_service_may_be_active; rte_socket_count; rte_socket_id_by_idx; + rte_ticketlock_lock; + rte_ticketlock_unlock; + rte_ticketlock_islocked; + rte_ticketlock_trylock; + rte_ticketlock_recurrsive_lock; + rte_ticketlock_recurrsive_unlock; + rte_ticketlock_recurrsive_islocked; + rte_ticketlock_recurrsive_trylock; };
Signature · 1 line
2.11.0
Quoted 26 lines · Gavin Hu
-------------------------------------------------------------------
---
From: Joyce Kong <[email protected]>The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time. As
shown in the following test, within same period of time, there are threads taking locks much more times than the others.The ticketlock gives each waiting thread a ticket and they can take the
lock one by one, first come, first serviced, this avoids starvation for
too long time and is more predictable.***
spinlock_autotestwithout this patch ***
Core [0] count = 89
Core [1] count = 84
Core [2] count = 94
...
Core [208] count = 171
Core [209] count = 152
Core [210] count = 161
Core [211] count = 187
Quoted 1 line
***
spinlock_autotestwith this patch ***
This comment needs to be updated as it is new API now and we need add test case for the same. I think, making modification to exiting spinlock test case would be fine.
Quoted 13 lines
Core [0] count = 534
Core [1] count = 533
Core [2] count = 542
...
Core [208] count = 554
Core [209] count = 556
Core [210] count = 555
Core [211] count = 551Signed-off-by Joyce Kong <[email protected]>Signed-off-by Gavin Hu <[email protected]>---
Quoted 40 lines
-----Original Message-----
From: Jerin Jacob Kollanukkaran <[email protected]>
Sent: Monday, January 14, 2019 4:00 PM
To: Gavin Hu (Arm Technology China) <[email protected]>; [email protected]
Cc: [email protected]; Honnappa Nagarahalli <[email protected]>; [email protected]; nd <[email protected]>; Joyce Kong (Arm Technology China) <[email protected]>; [email protected]
Subject: Re: [EXT] [PATCH v1] ticketlock: ticket based to improve fairnessQuoted 26 lines · Gavin Hu
-------------------------------------------------------------------
---
From: Joyce Kong <[email protected]>The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time. As
shown in the following test, within same period of time, there are threads taking locks much more times than the others.The ticketlock gives each waiting thread a ticket and they can take the
lock one by one, first come, first serviced, this avoids starvation for
too long time and is more predictable.***
spinlock_autotestwithout this patch ***
Core [0] count = 89
Core [1] count = 84
Core [2] count = 94
...
Core [208] count = 171
Core [209] count = 152
Core [210] count = 161
Core [211] count = 187
Quoted 5 lines
Quoted 1 line
***
spinlock_autotestwith this patch ***This comment needs to be updated as it is new API now and we need add test case for the same. I think, making modification to exiting spinlock test case would be fine.
Thanks for you comments, we will do it and send a v2.
Quoted 43 lines
Subject: [PATCH v1] ticketlock: ticket based to improve fairness
From: Joyce Kong <[email protected]>
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time. As shown in the following test, within same period of time, there are threads taking locks much more times than the others.
The ticketlock gives each waiting thread a ticket and they can take the lock one by one, first come, first serviced, this avoids starvation for too long time and is more predictable.
***
spinlock_autotestwithout this patch *** Core [0] count = 89 Core [1] count = 84 Core [2] count = 94 ...
Core [208] count = 171
Core [209] count = 152
Core [210] count = 161
Core [211] count = 187***
spinlock_autotestwith this patch *** Core [0] count = 534 Core [1] count = 533 Core [2] count = 542 ...
Core [208] count = 554
Core [209] count = 556
Core [210] count = 555
Core [211] count = 551Signed-off-by Joyce Kong <[email protected]>Signed-off-by Gavin Hu <[email protected]>--- .../common/include/generic/rte_ticketlock.h | 203 +++++++++++++++++++++ lib/librte_eal/rte_eal_version.map | 8 + 2 files changed, 211 insertions(+) create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.hlib/librte_eal/common/include/generic/rte_ticketlock.h new file +0
-0
lib/librte_eal/common/include/generic/rte_ticketlock.h line 2
Quoted 2 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +2
-0@@ -0,0 +1,203 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2018-2019 Arm Corporation */
^^^^^^^^^^
Should be 'Limited'
lib/librte_eal/common/include/generic/rte_ticketlock.h line 15
Quoted 12 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +12
-0+ +#ifndef _RTE_TICKETLOCK_H_ +#define _RTE_TICKETLOCK_H_ + +/** +* @file +* +* RTE ticketlocks +* +* This file defines an API for read-write locks, which are implemented +* in an architecture-specific way. This kind of lock simply waits in +* a loop repeatedly checking until the lock becomes available.
Needs update for ticket lock
Quoted 5 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +5
-0+* +* All locks must be initialised before use, and only initialised once. +* +*/ +
lib/librte_eal/common/include/generic/rte_ticketlock.h line 30
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+#include <rte_lcore.h> +#include <rte_common.h> +#include <rte_pause.h> + +/** +* The rte_ticketlock_t type. +*/ +typedef struct { + uint16_t current; + uint16_t next;
Any reason these are 16b?
Use 'volatile' for consistency with rte_spinlock_t? (though I do not prefer to use 'volatile' in general)
Quoted 23 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +23
-0+} rte_ticketlock_t; + +/** +* A static ticketlock initializer. +*/ +#define RTE_TICKETLOCK_INITIALIZER { 0 } + +/** +* Initialize the ticketlock to an unlocked state. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_init (rte_ticketlock_t *tl) { +__atomic_store_n (&tl->current,0 , __ATOMIC_RELAXED); +__atomic_store_n (&tl->next,0 , __ATOMIC_RELAXED); } + +/** +* Take the ticketlock. +* +* @param tl +* A pointer to the ticketlock.
lib/librte_eal/common/include/generic/rte_ticketlock.h line 66
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+*/ +static inline __rte_experimental void +rte_ticketlock_lock (rte_ticketlock_t *tl); + +static inline __rte_experimental void +rte_ticketlock_lock (rte_ticketlock_t *tl) { +uint16_t me =__atomic_fetch_add (&tl->next,1 , __ATOMIC_RELAXED); +while (__atomic_load_n (&tl->current, __ATOMIC_ACQUIRE) != me) +rte_pause (); +}
Do we need to place the APIs under RTE_FORCE_INTRINSICS flag? I see that x86 has implementation which does not use intrinsics (lib/librte_eal/common/include/arch/x86/rte_spinlock.h).
Quoted 18 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +18
-0+ +/** +* Release the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_unlock (rte_ticketlock_t *tl); + +static inline __rte_experimental void +rte_ticketlock_unlock (rte_ticketlock_t *tl) { +uint16_t i =__atomic_load_n (&tl->current, __ATOMIC_RELAXED); + i++; +__atomic_store_n (&tl->current, i, __ATOMIC_RELEASE); } + +/** +* Try to take the lock.
lib/librte_eal/common/include/generic/rte_ticketlock.h line 97
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock is successfully taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_trylock (rte_ticketlock_t *tl) { +uint16_t me =__atomic_fetch_add (&tl->next,1 , __ATOMIC_RELAXED); +while (__atomic_load_n (&tl->current, __ATOMIC_RELAXED) != me) {
What happens if other threads incremented 'tl->next' here?
lib/librte_eal/common/include/generic/rte_ticketlock.h line 103
Quoted 6 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +6
-0+__atomic_sub_fetch (&tl->next,1 , __ATOMIC_RELAXED); +return 0 ; + } + +return 1 ; +}
IMO, this implementation needs to be changed. We should try to take the lock if tl->next and tl->current are equal. Then tl->next should be incremented using a CAS (which makes sure no one else has taken the lock in the meanwhile)
lib/librte_eal/common/include/generic/rte_ticketlock.h line 116
Quoted 12 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +12
-0+ +/** +* Test if the lock is taken. +* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock icurrently taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_is_locked (rte_ticketlock_t *tl) { + return (__atomic_load_n(&tl->current, __ATOMIC_RELAXED) !=
Load of tl->current should be __ATOMIC_ACQUIRE
lib/librte_eal/common/include/generic/rte_ticketlock.h
Quoted 9 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +9
-0+ __atomic_load_n(&tl->next, __ATOMIC_RELAXED)); } + +/** +* The rte_ticketlock_recursive_t type. +*/ +typedef struct { +rte_ticketlock_t tl;/**< the actual ticketlock */ +volatile int user;/**< core id using lock, -1 for unused */ + volatile int count; /**< count of time this lock has been called */ }
'count' can be 'unsigned int'?
Quoted 43 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +43
-0+rte_ticketlock_recursive_t; + +/** +* A static recursive ticketlock initializer. +*/ +#define RTE_TICKETLOCK_RECURSIVE_INITIALIZER +{RTE_TICKETLOCK_INITIALIZER, -1, 0} + +/** +* Initialize the recursive ticketlock to an unlocked state. +* +* @param tlr +* A pointer to the recursive ticketlock. +*/ +static inline __rte_experimental void rte_ticketlock_recursive_init( +rte_ticketlock_recursive_t *tlr) +{ +rte_ticketlock_init (&tlr->tl); + tlr->user =-1 ; + tlr->count =0 ; +} + +/** +* Take the recursive ticketlock. +* +* @param tlr +* A pointer to the recursive ticketlock. +*/ +static inline __rte_experimental void rte_ticketlock_recursive_lock( +rte_ticketlock_recursive_t *tlr) +{ +int id =rte_gettid (); + +if (tlr->user != id) { +rte_ticketlock_lock (&tlr->tl); + tlr->user = id; + } + tlr->count++; +} +/** +* Release the recursive ticketlock. +* +* @param tlr
lib/librte_eal/common/include/generic/rte_ticketlock.h line 178
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+* A pointer to the recursive ticketlock. +*/ +static inline __rte_experimental void rte_ticketlock_recursive_unlock( +rte_ticketlock_recursive_t *tlr) +{ +if (--(tlr->count) ==0 ) { + tlr->user =-1 ; +rte_ticketlock_unlock (&tlr->tl); + } +
Minor comment. Extra line.
Quoted 32 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +25
-0+} + +/** +* Try to take the recursive lock. +* +* @param tlr +* A pointer to the recursive ticketlock. +* @return +* 1 if the lock is successfully taken; 0 otherwise. +*/ +static inline __rte_experimental int rte_ticketlock_recursive_trylock( +rte_ticketlock_recursive_t *tlr) +{ +int id =rte_gettid (); + +if (tlr->user != id) { +if (rte_ticketlock_trylock (&tlr->tl) ==0 ) +return 0 ; + tlr->user = id; + } + tlr->count++; +return 1 ; +} + +#endif /* _RTE_TICKETLOCK_H_ */ lib/librte_eal/rte_eal_version.map +0
-0@@ -364,4 +364,12 @@ EXPERIMENTAL { rte_service_may_be_active;
lib/librte_eal/rte_eal_version.map EXPERIMENTAL { · line 374
Quoted 10 lines
lib/librte_eal/rte_eal_version.map +8
-0rte_socket_count; rte_socket_id_by_idx; + rte_ticketlock_lock; + rte_ticketlock_unlock; + rte_ticketlock_islocked; + rte_ticketlock_trylock; + rte_ticketlock_recurrsive_lock; + rte_ticketlock_recurrsive_unlock; + rte_ticketlock_recurrsive_islocked; + rte_ticketlock_recurrsive_trylock;
These are all 'inline' functions, they do not need to be versioned.
Quoted 3 lines
lib/librte_eal/rte_eal_version.map +0
-1}; --2.11.0
PATCH v2 1/2 ticketlock: ticket based to improve fairness
In reply to Gavin HuThe spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time. As shown in the following test, within same period of time, there are threads taking locks much more times than the others.
The ticketlock gives each waiting thread a ticket and they can take the lock one by one, first come, first serviced, this avoids starvation for too long time and is more predictable.
*** ticketlock_autotest with this patch ***
Core [0] count = 496
Core [1] count = 495
Core [2] count = 498
...
Core [209] count = 488
Core [210] count = 490
Core [211] count = 474--- doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/generic/rte_ticketlock.h | 198 +++++++++++++++++++++ lib/librte_eal/common/meson.build | 1 + 4 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
doc/api/doxy-api-index.md +1 -0
@@ -65,6 +65,7 @@ The public API headers are grouped by topics: [atomic] (@ref rte_atomic.h), [rwlock] (@ref rte_rwlock.h), [spinlock] (@ref rte_spinlock.h) + [ticketlock] (@ref rte_ticketlock.h) - **CPU arch**: [branch prediction] (@ref rte_branch_prediction.h),
lib/librte_eal/common/Makefile +1 -1
@@ -20,7 +20,7 @@ INC += rte_bitmap.h rte_vfio.h rte_hypervisor.h rte_test.h INC += rte_reciprocal.h rte_fbarray.h rte_uuid.h GENERIC_INC := rte_atomic.h rte_byteorder.h rte_cycles.h rte_prefetch.h -GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h +GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h rte_ticketlock.h GENERIC_INC += rte_vect.h rte_pause.h rte_io.h # defined in mk/arch/$(RTE_ARCH)/rte.vars.mk
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +198 -0
198 lines not on this page · raw message
lib/librte_eal/common/meson.build +1 -0
@@ -98,6 +98,7 @@ generic_headers = files( 'include/generic/rte_prefetch.h', 'include/generic/rte_rwlock.h', 'include/generic/rte_spinlock.h', + 'include/generic/rte_ticketlock.h', 'include/generic/rte_vect.h') install_headers(generic_headers, subdir: 'generic')
Signature · 1 line
2.7.4
PATCH v2 2/2 test/ticketlock: add ticket lock test case
Add test cases for ticket lock, recursive ticket lock, and ticket lock performance.
--- test/test/Makefile | 1 + test/test/autotest_data.py | 6 + test/test/meson.build | 1 + test/test/test_ticketlock.c | 311 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 319 insertions(+) create mode 100644 test/test/test_ticketlock.c
test/test/Makefile +1 -0
@@ -65,6 +65,7 @@ SRCS-y += test_barrier.c SRCS-y += test_malloc.c SRCS-y += test_cycles.c SRCS-y += test_spinlock.c +SRCS-y += test_ticketlock.c SRCS-y += test_memory.c SRCS-y += test_memzone.c SRCS-y += test_bitmap.c
test/test/autotest_data.py +6 -0
@@ -171,6 +171,12 @@ "Report": None, }, { + "Name": "Ticketlock_autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + }, + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,
test/test/meson.build +1 -0
@@ -103,6 +103,7 @@ test_sources = files('commands.c', 'test_timer.c', 'test_timer_perf.c', 'test_timer_racecond.c', + 'test_ticketlock.c', 'test_version.c', 'virtual_pmd.c' )
test/test/test_ticketlock.c new file +311 -0
311 lines not on this page · raw message
Signature · 1 line
2.7.4
v3 cover ticketlock: implement ticketlock and add test case
In reply to Joyce KongV3:
1.Update ticketlock intrduction(suggested by Honnappa Nagarahalli).
2.Change the implementation of rte_ticketlock_trylock to CAS(suggested by Honnappa Nagarahalli).
V2:
1.Update commit message(suggested by Jerin Jacob). 2.Add ticketlock test cases(suggested by Jerin Jacob).
V1:
Implement ticket lock to improve lock fairness and prdictability.
As shown on thundex-2 platform:
*** ticketlock_autotest with this patch ***
Core [0] count = 496
Core [1] count = 495
Core [2] count = 498
...
Core [209] count = 488
Core [210] count = 490
Core [211] count = 474Joyce Kong (2):
ticketlock: ticket based to improve fairness
test/ticketlock: add ticket lock test case
MAINTAINERS | 5 + doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/generic/rte_ticketlock.h | 201 +++++++++++++ lib/librte_eal/common/meson.build | 1 + test/test/Makefile | 1 + test/test/autotest_data.py | 6 + test/test/meson.build | 1 + test/test/test_ticketlock.c | 311 +++++++++++++++++++++ 9 files changed, 528 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h create mode 100644 test/test/test_ticketlock.c
Signature · 1 line
2.7.4
PATCH v3 1/2 ticketlock: ticket based to improve fairness
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time.
This patch introduces ticketlock which gives each waiting thread a ticket and they can take the lock one by one. First come, first serviced. This avoids starvation for too long time and is more predictable.
--- MAINTAINERS | 4 + doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/generic/rte_ticketlock.h | 201 +++++++++++++++++++++ lib/librte_eal/common/meson.build | 1 + 5 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
MAINTAINERS +4 -0
@@ -212,6 +212,10 @@ M: Cristian Dumitrescu <[email protected]> F: lib/librte_eal/common/include/rte_bitmap.h F: test/test/test_bitmap.c +Ticketlock +M: Joyce Kong <[email protected]> +F: lib/librte_eal/common/include/generic/rte_ticketlock.h + ARM v7 M: Jan Viktorin <[email protected]> M: Gavin Hu <[email protected]>
doc/api/doxy-api-index.md +1 -0
@@ -65,6 +65,7 @@ The public API headers are grouped by topics: [atomic] (@ref rte_atomic.h), [rwlock] (@ref rte_rwlock.h), [spinlock] (@ref rte_spinlock.h) + [ticketlock] (@ref rte_ticketlock.h) - **CPU arch**: [branch prediction] (@ref rte_branch_prediction.h),
lib/librte_eal/common/Makefile +1 -1
@@ -20,7 +20,7 @@ INC += rte_bitmap.h rte_vfio.h rte_hypervisor.h rte_test.h INC += rte_reciprocal.h rte_fbarray.h rte_uuid.h GENERIC_INC := rte_atomic.h rte_byteorder.h rte_cycles.h rte_prefetch.h -GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h +GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h rte_ticketlock.h GENERIC_INC += rte_vect.h rte_pause.h rte_io.h # defined in mk/arch/$(RTE_ARCH)/rte.vars.mk
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +201 -0
201 lines not on this page · raw message
lib/librte_eal/common/meson.build +1 -0
@@ -98,6 +98,7 @@ generic_headers = files( 'include/generic/rte_prefetch.h', 'include/generic/rte_rwlock.h', 'include/generic/rte_spinlock.h', + 'include/generic/rte_ticketlock.h', 'include/generic/rte_vect.h') install_headers(generic_headers, subdir: 'generic')
Signature · 1 line
2.7.4
PATCH v3 2/2 test/ticketlock: add ticket lock test case
In reply to Joyce KongAdd test cases for ticket lock, recursive ticket lock, and ticket lock performance.
--- MAINTAINERS | 1 + test/test/Makefile | 1 + test/test/autotest_data.py | 6 + test/test/meson.build | 1 + test/test/test_ticketlock.c | 311 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 320 insertions(+) create mode 100644 test/test/test_ticketlock.c
MAINTAINERS +1 -0
@@ -215,6 +215,7 @@ F: test/test/test_bitmap.c Ticketlock M: Joyce Kong <[email protected]> F: lib/librte_eal/common/include/generic/rte_ticketlock.h +F: test/test/test_ticketlock.c ARM v7 M: Jan Viktorin <[email protected]>
test/test/Makefile +1 -0
@@ -65,6 +65,7 @@ SRCS-y += test_barrier.c SRCS-y += test_malloc.c SRCS-y += test_cycles.c SRCS-y += test_spinlock.c +SRCS-y += test_ticketlock.c SRCS-y += test_memory.c SRCS-y += test_memzone.c SRCS-y += test_bitmap.c
test/test/autotest_data.py +6 -0
@@ -171,6 +171,12 @@ "Report": None, }, { + "Name": "Ticketlock_autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + }, + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,
test/test/meson.build +1 -0
@@ -107,6 +107,7 @@ test_sources = files('commands.c', 'test_timer.c', 'test_timer_perf.c', 'test_timer_racecond.c', + 'test_ticketlock.c', 'test_version.c', 'virtual_pmd.c' )
test/test/test_ticketlock.c new file +311 -0
311 lines not on this page · raw message
Signature · 1 line
2.7.4
v4 cover ticketlock: implement ticketlock and add test case
In reply to Joyce Kongv4:
Change some assignment operation in recursive ticket lock to __atomic.
V3:
1.Update ticketlock intrduction(suggested by Honnappa Nagarahalli).
2.Change the implementation of rte_ticketlock_trylock to CAS(suggested by Honnappa Nagarahalli).
V2:
1.Update commit message(suggested by Jerin Jacob). 2.Add ticketlock test cases(suggested by Jerin Jacob).
V1:
Implement ticket lock to improve lock fairness and prdictability.
As shown on thundex-2 platform:
*** ticketlock_autotest with this patch ***
Core [0] count = 496
Core [1] count = 495
Core [2] count = 498
...
Core [209] count = 488
Core [210] count = 490
Core [211] count = 474Joyce Kong (2):
ticketlock: ticket based to improve fairness
test/ticketlock: add ticket lock test case
MAINTAINERS | 4 + doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/generic/rte_ticketlock.h | 201 +++++++++++++ lib/librte_eal/common/meson.build | 1 + test/test/Makefile | 1 + test/test/autotest_data.py | 6 + test/test/meson.build | 1 + test/test/test_ticketlock.c | 311 +++++++++++++++++++++ 9 files changed, 527 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h create mode 100644 test/test/test_ticketlock.c
Signature · 1 line
2.7.4
PATCH v4 1/2 ticketlock: ticket based to improve fairness
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time.
This patch introduces ticketlock which gives each waiting thread a ticket and they can take the lock one by one. First come, first serviced. This avoids starvation for too long time and is more predictable.
--- MAINTAINERS | 4 + doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/generic/rte_ticketlock.h | 201 +++++++++++++++++++++ lib/librte_eal/common/meson.build | 1 + 5 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
MAINTAINERS +4 -0
@@ -212,6 +212,10 @@ M: Cristian Dumitrescu <[email protected]> F: lib/librte_eal/common/include/rte_bitmap.h F: test/test/test_bitmap.c +Ticketlock +M: Joyce Kong <[email protected]> +F: lib/librte_eal/common/include/generic/rte_ticketlock.h + ARM v7 M: Jan Viktorin <[email protected]> M: Gavin Hu <[email protected]>
doc/api/doxy-api-index.md +1 -0
@@ -65,6 +65,7 @@ The public API headers are grouped by topics: [atomic] (@ref rte_atomic.h), [rwlock] (@ref rte_rwlock.h), [spinlock] (@ref rte_spinlock.h) + [ticketlock] (@ref rte_ticketlock.h) - **CPU arch**: [branch prediction] (@ref rte_branch_prediction.h),
lib/librte_eal/common/Makefile +1 -1
@@ -20,7 +20,7 @@ INC += rte_bitmap.h rte_vfio.h rte_hypervisor.h rte_test.h INC += rte_reciprocal.h rte_fbarray.h rte_uuid.h GENERIC_INC := rte_atomic.h rte_byteorder.h rte_cycles.h rte_prefetch.h -GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h +GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h rte_ticketlock.h GENERIC_INC += rte_vect.h rte_pause.h rte_io.h # defined in mk/arch/$(RTE_ARCH)/rte.vars.mk
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +201 -0
201 lines not on this page · raw message
lib/librte_eal/common/meson.build +1 -0
@@ -98,6 +98,7 @@ generic_headers = files( 'include/generic/rte_prefetch.h', 'include/generic/rte_rwlock.h', 'include/generic/rte_spinlock.h', + 'include/generic/rte_ticketlock.h', 'include/generic/rte_vect.h') install_headers(generic_headers, subdir: 'generic')
Signature · 1 line
2.7.4
PATCH v4 2/2 test/ticketlock: add ticket lock test case
In reply to Joyce KongAdd test cases for ticket lock, recursive ticket lock, and ticket lock performance.
--- test/test/Makefile | 1 + test/test/autotest_data.py | 6 + test/test/meson.build | 1 + test/test/test_ticketlock.c | 311 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 319 insertions(+) create mode 100644 test/test/test_ticketlock.c
test/test/Makefile +1 -0
@@ -65,6 +65,7 @@ SRCS-y += test_barrier.c SRCS-y += test_malloc.c SRCS-y += test_cycles.c SRCS-y += test_spinlock.c +SRCS-y += test_ticketlock.c SRCS-y += test_memory.c SRCS-y += test_memzone.c SRCS-y += test_bitmap.c
test/test/autotest_data.py +6 -0
@@ -171,6 +171,12 @@ "Report": None, }, { + "Name": "Ticketlock_autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + }, + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,
test/test/meson.build +1 -0
@@ -107,6 +107,7 @@ test_sources = files('commands.c', 'test_timer.c', 'test_timer_perf.c', 'test_timer_racecond.c', + 'test_ticketlock.c', 'test_version.c', 'virtual_pmd.c' )
test/test/test_ticketlock.c new file +311 -0
311 lines not on this page · raw message
Signature · 1 line
2.7.4
v5 cover ticketlock: implement ticketlock and add test case
In reply to Joyce Kongv5:
Change the variants inside rte_ticket_lock from unint to uint16_t for binary compatibility with the plain spin lock.
v4:
Change some assignment operation in recursive ticket lock to __atomic.
V3:
1.Update ticketlock intrduction(suggested by Honnappa Nagarahalli).
2.Change the implementation of rte_ticketlock_trylock to CAS(suggested by Honnappa Nagarahalli).
V2:
1.Update commit message(suggested by Jerin Jacob). 2.Add ticketlock test cases(suggested by Jerin Jacob).
V1:
Implement ticket lock to improve lock fairness and prdictability.
As shown on thundex-2 platform:
*** ticketlock_autotest with this patch ***
Core [0] count = 496
Core [1] count = 495
Core [2] count = 498
...
Core [209] count = 488
Core [210] count = 490
Core [211] count = 474Joyce Kong (2):
eal/ticketlock: ticket based to improve fairness test/ticketlock: add ticket lock test case
MAINTAINERS | 4 + app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 311 +++++++++++++++++++++ doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/generic/rte_ticketlock.h | 203 ++++++++++++++ lib/librte_eal/common/meson.build | 1 + 9 files changed, 529 insertions(+), 1 deletion(-) create mode 100644 app/test/test_ticketlock.c create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
Signature · 1 line
2.7.4
PATCH v5 1/2 eal/ticketlock: ticket based to improve fairness
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time.
This patch introduces ticketlock which gives each waiting thread a ticket and they can take the lock one by one. First come, first serviced. This avoids starvation for too long time and is more predictable.
--- MAINTAINERS | 4 + doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/generic/rte_ticketlock.h | 203 +++++++++++++++++++++ lib/librte_eal/common/meson.build | 1 + 5 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
MAINTAINERS +4 -0
@@ -210,6 +210,10 @@ M: Cristian Dumitrescu <[email protected]> F: lib/librte_eal/common/include/rte_bitmap.h F: app/test/test_bitmap.c +Ticketlock +M: Joyce Kong <[email protected]> +F: lib/librte_eal/common/include/generic/rte_ticketlock.h + ARM v7 M: Jan Viktorin <[email protected]> M: Gavin Hu <[email protected]>
doc/api/doxy-api-index.md +1 -0
@@ -65,6 +65,7 @@ The public API headers are grouped by topics: [atomic] (@ref rte_atomic.h), [rwlock] (@ref rte_rwlock.h), [spinlock] (@ref rte_spinlock.h) + [ticketlock] (@ref rte_ticketlock.h) - **CPU arch**: [branch prediction] (@ref rte_branch_prediction.h),
lib/librte_eal/common/Makefile +1 -1
@@ -20,7 +20,7 @@ INC += rte_bitmap.h rte_vfio.h rte_hypervisor.h rte_test.h INC += rte_reciprocal.h rte_fbarray.h rte_uuid.h GENERIC_INC := rte_atomic.h rte_byteorder.h rte_cycles.h rte_prefetch.h -GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h +GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h rte_ticketlock.h GENERIC_INC += rte_vect.h rte_pause.h rte_io.h # defined in mk/arch/$(RTE_ARCH)/rte.vars.mk
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +203 -0
203 lines not on this page · raw message
lib/librte_eal/common/meson.build +1 -0
@@ -99,6 +99,7 @@ generic_headers = files( 'include/generic/rte_prefetch.h', 'include/generic/rte_rwlock.h', 'include/generic/rte_spinlock.h', + 'include/generic/rte_ticketlock.h', 'include/generic/rte_vect.h') install_headers(generic_headers, subdir: 'generic')
Signature · 1 line
2.7.4
Re: [PATCH v5 1/2] eal/ticketlock: ticket based to improve fairness
Quoted 24 lines · Joyce Kong
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time.
This patch introduces ticketlock which gives each waiting thread a ticket and they can take the lock one by one. First come, first serviced.
This avoids starvation for too long time and is more predictable.Suggested-by Jerin Jacob <[email protected]>Signed-off-by Joyce kong <[email protected]>Reviewed-by Gavin Hu <[email protected]>Reviewed-by Ola Liljedahl <[email protected]>Reviewed-by Honnappa Nagarahalli <[email protected]>---
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +4
-0+static inline __rte_experimental int +rte_ticketlock_trylock (rte_ticketlock_t *tl) +{ + unsigned int next = __atomic_load_n(&tl->next,
__ATOMIC_RELAXED);(quoted diff) +1
-0+ unsigned int cur = __atomic_load_n(&tl->current,
__ATOMIC_RELAXED);(quoted diff) +2
-0+ if (next == cur) { + if (__atomic_compare_exchange_n(&tl->next, &next,next+1,
lib/librte_eal/common/include/generic/rte_ticketlock.h line 94
Quoted 1 line
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +1
-0+ 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
gcc 8.2 emits the following compilation error.
/export/dpdk.org/build/include/generic/rte_ticketlock.h:93:46: error:
incompatible pointer types passing 'unsigned int *' to parameter of type 'uint16_t *' (aka 'unsigned short *') [-Werror,-Wincompatible- pointer-types]
if (__atomic_compare_exchange_n(&tl->next, &next,
next+1,
Quoted 7 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +6
-0+return 1 ; + } + +return 0 ; +} +
Re: [PATCH v5 1/2] eal/ticketlock: ticket based to improve fairness
Quoted 40 lines
-----Original Message-----
From: Jerin Jacob Kollanukkaran <[email protected]>
Sent: Wednesday, March 13, 2019 5:41 PM
To: Joyce Kong (Arm Technology China) <[email protected]>; [email protected]
Cc: [email protected]; Honnappa Nagarahalli <[email protected]>; [email protected]; nd <[email protected]>; [email protected]; Gavin Hu (Arm Technology China) <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v5 1/2] eal/ticketlock: ticket based to improve fairnessQuoted 23 lines · Joyce Kong
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time.
This patch introduces ticketlock which gives each waiting thread a ticket and they can take the lock one by one. First come, first serviced.
This avoids starvation for too long time and is more predictable.Suggested-by Jerin Jacob <[email protected]>Signed-off-by Joyce kong <[email protected]>Reviewed-by Gavin Hu <[email protected]>Reviewed-by Ola Liljedahl <[email protected]>Reviewed-by Honnappa Nagarahalli <[email protected]>---
(quoted diff) +3
-0+static inline __rte_experimental int +rte_ticketlock_trylock(rte_ticketlock_t *tl) { + unsigned int next = __atomic_load_n(&tl->next,
__ATOMIC_RELAXED);(quoted diff) +1
-0+ unsigned int cur = __atomic_load_n(&tl->current,
__ATOMIC_RELAXED);(quoted diff) +2
-0+ if (next == cur) { + if (__atomic_compare_exchange_n(&tl->next, &next,next+1,
lib/librte_eal/common/include/generic/rte_ticketlock.h line 94
Quoted 1 line
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +1
-0+ 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))gcc 8.2 emits the following compilation error.
Quoted 6 lines
/export/dpdk.org/build/include/generic/rte_ticketlock.h:93:46: error:
incompatible pointer types passing 'unsigned int *' to parameter of type 'uint16_t*' (aka 'unsigned short *') [-Werror,-Wincompatible- pointer-types]if (__atomic_compare_exchange_n(&tl->next, &next,next+1,
Fix the error by changing next and cur from unsigned int to unint16_t in V6.
Quoted 8 lines
Quoted 8 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +6
-0+return 1 ; + } + +return 0 ; +} +
Re: [PATCH v5 1/2] eal/ticketlock: ticket based to improve fairness
In reply to Joyce KongQuoted 23 lines · Joyce Kong
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time.
This patch introduces ticketlock which gives each waiting thread a ticket and they can take the lock one by one. First come, first serviced.
This avoids starvation for too long time and is more predictable.Suggested-by Jerin Jacob <[email protected]>Signed-off-by Joyce kong <[email protected]>Reviewed-by Gavin Hu <[email protected]>Reviewed-by Ola Liljedahl <[email protected]>Reviewed-by Honnappa Nagarahalli <[email protected]>---
MAINTAINERS +0
-0@@ -210,6 +210,10 @@ M: Cristian Dumitrescu <[email protected]>
F:lib/librte_eal/common/include/rte_bitmap.h
F:app/test/test_bitmap.c
MAINTAINERS M: Cristian Dumitrescu <[email protected]> · line 215
Quoted 3 lines
MAINTAINERS +3
-0+Ticketlock +M: Joyce Kong <[email protected]> +F: lib/librte_eal/common/include/generic/rte_ticketlock.h
Add F: app/test/test_ticketlock.c in the next patch
MAINTAINERS
Quoted 4 lines
MAINTAINERS +3
-0+#include <rte_lcore.h> +#include <rte_common.h> +#include <rte_pause.h>
Sort the header in alphabetical order.
Quoted 11 lines
MAINTAINERS +10
-0+ +/** + * The rte_ticketlock_t type. + */ +typedef struct { + uint16_t current; + uint16_t next; +} rte_ticketlock_t; + +
MAINTAINERS
Quoted 10 lines
MAINTAINERS +10
-0+/** + * Take the ticketlock. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_lock(rte_ticketlock_t *tl) +{ + unsigned int me = __atomic_fetch_add(&tl->next, 1,
If current, next is uint16_t why "me" as unsigned int.
Quoted 16 lines
__ATOMIC_RELAXED);MAINTAINERS +14
-0+ while (__atomic_load_n(&tl->current, __ATOMIC_ACQUIRE) != me) + rte_pause(); +} + +/** + * Release the ticketlock. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_unlock(rte_ticketlock_t *tl) +{ + unsigned int i = __atomic_load_n(&tl->current,
__ATOMIC_RELAXED);
MAINTAINERS
Quoted 1 line
MAINTAINERS +1
-0+ i++;
You can save this line by making __atomic_store_n(&tl->current, i + 1, __ATOMIC_RELEASE);
The code looks good. Please check the above comments and earlier reported compilation issue with clang 7.1
Re: [PATCH v5 1/2] eal/ticketlock: ticket based to improve fairness
Quoted 35 lines
-----Original Message-----
From: Jerin Jacob Kollanukkaran <[email protected]>
Sent: Wednesday, March 13, 2019 11:36 PM
To: Joyce Kong (Arm Technology China) <[email protected]>; [email protected]
Cc: [email protected]; Honnappa Nagarahalli <[email protected]>; [email protected]; nd <[email protected]>; [email protected]; Gavin Hu (Arm Technology China) <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v5 1/2] eal/ticketlock: ticket based to improve fairnessQuoted 22 lines · Joyce Kong
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time.
This patch introduces ticketlock which gives each waiting thread a ticket and they can take the lock one by one. First come, first serviced.
This avoids starvation for too long time and is more predictable.Suggested-by Jerin Jacob <[email protected]>Signed-off-by Joyce kong <[email protected]>Reviewed-by Gavin Hu <[email protected]>Reviewed-by Ola Liljedahl <[email protected]>Reviewed-by Honnappa Nagarahalli <[email protected]>---
MAINTAINERS +0
-0@@ -210,6 +210,10 @@ M: Cristian Dumitrescu <[email protected]>
F:lib/librte_eal/common/include/rte_bitmap.h
F:app/test/test_bitmap.c
Quoted 7 lines
MAINTAINERS M: Cristian Dumitrescu <[email protected]> · line 215
Quoted 3 lines
MAINTAINERS +3
-0+Ticketlock +M: Joyce Kong <[email protected]> +F: lib/librte_eal/common/include/generic/rte_ticketlock.hAdd F:
app/test/test_ticketlock.cin the next patch
Done in V6.
Quoted 7 lines
MAINTAINERS
Quoted 4 lines
MAINTAINERS +3
-0+#include <rte_lcore.h> +#include <rte_common.h> +#include <rte_pause.h>Sort the header in alphabetical order.
Done in v6.
Quoted 10 lines
Quoted 10 lines
MAINTAINERS +9
-0+ +/** + * The rte_ticketlock_t type. + */ +typedef struct { + uint16_t current; + uint16_t next; +} rte_ticketlock_t; +
Quoted 13 lines
MAINTAINERS
Quoted 10 lines
MAINTAINERS +10
-0+ +/** + * Take the ticketlock. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_lock(rte_ticketlock_t *tl) { + unsigned int me = __atomic_fetch_add(&tl->next, 1,If current, next is
uint16_twhy "me" as unsigned int.
Change "me" to uint16_t to match current and next in v6.
Quoted 11 lines
Quoted 11 lines
__ATOMIC_RELAXED);MAINTAINERS +10
-0+ while (__atomic_load_n(&tl->current, __ATOMIC_ACQUIRE) != me) + rte_pause(); +} + +/** + * Release the ticketlock. + * + * @param tl + * A pointer to the ticketlock. + */
Quoted 9 lines
MAINTAINERS
Quoted 5 lines
MAINTAINERS +3
-0+static inline __rte_experimental void +rte_ticketlock_unlock(rte_ticketlock_t *tl) { + unsigned int i = __atomic_load_n(&tl->current,
__ATOMIC_RELAXED);MAINTAINERS +1
-0+ i++;You can save this line by making
__atomic_store_n(&tl->current, i + 1, __ATOMIC_RELEASE);
Done in V6.
Quoted 3 lines
The code looks good. Please check the above comments and earlier reported compilation issue with clang 7.1
PATCH v5 2/2 test/ticketlock: add ticket lock test case
In reply to Joyce KongAdd test cases for ticket lock, recursive ticket lock, and ticket lock performance.
--- app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 311 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 319 insertions(+) create mode 100644 app/test/test_ticketlock.c
app/test/Makefile +1 -0
@@ -65,6 +65,7 @@ SRCS-y += test_barrier.c SRCS-y += test_malloc.c SRCS-y += test_cycles.c SRCS-y += test_spinlock.c +SRCS-y += test_ticketlock.c SRCS-y += test_memory.c SRCS-y += test_memzone.c SRCS-y += test_bitmap.c
app/test/autotest_data.py +6 -0
@@ -171,6 +171,12 @@ "Report": None, }, { + "Name": "Ticketlock autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + } + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,
app/test/meson.build +1 -0
@@ -107,6 +107,7 @@ test_sources = files('commands.c', 'test_timer.c', 'test_timer_perf.c', 'test_timer_racecond.c', + 'test_ticketlock.c', 'test_version.c', 'virtual_pmd.c' )
app/test/test_ticketlock.c new file +311 -0
311 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH v5 2/2] test/ticketlock: add ticket lock test case
Quoted 20 lines · Joyce Kong
Add test cases for ticket lock, recursive ticket lock, and ticket lock performance.
Signed-off-by Joyce Kong <[email protected]>Reviewed-by Gavin Hu <[email protected]>Reviewed-by Phil Yang <[email protected]>---
index 0000000..dfa818capp/test/test_ticketlock.c new file +9
-0@@ -0,0 +1,311 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2018-2019 Arm Limited +*/ + +#include <stdio.h> +#include <stdint.h> +#include <inttypes.h> +#include <string.h> +#include <unistd.h>
app/test/test_ticketlock.c line 19
Quoted 10 lines
app/test/test_ticketlock.c new file +10
-0+#include <sys/queue.h> + +#include <rte_common.h> +#include <rte_memory.h> +#include <rte_per_lcore.h> +#include <rte_launch.h> +#include <rte_eal.h> +#include <rte_lcore.h> +#include <rte_cycles.h> +#include <generic/rte_ticketlock.h>
it should be just <rte_ticketlock.h>
app/test/test_ticketlock.c line 20
Quoted 1 line
app/test/test_ticketlock.c new file +1
-0+#include <rte_atomic.h>
Please sort this in alphabetical order.
Quoted 1 line
Re: [PATCH v5 2/2] test/ticketlock: add ticket lock test case
Quoted 36 lines
-----Original Message-----
From: Jerin Jacob Kollanukkaran <[email protected]>
Sent: Wednesday, March 13, 2019 9:31 PM
To: Joyce Kong (Arm Technology China) <[email protected]>; [email protected]
Cc: [email protected]; Honnappa Nagarahalli <[email protected]>; [email protected]; nd <[email protected]>; [email protected]; Gavin Hu (Arm Technology China) <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v5 2/2] test/ticketlock: add ticket lock test caseQuoted 23 lines · Joyce Kong
Add test cases for ticket lock, recursive ticket lock, and ticket lock performance.
Signed-off-by Joyce Kong <[email protected]>Reviewed-by Gavin Hu <[email protected]>Reviewed-by Phil Yang <[email protected]>---
index 0000000..dfa818capp/test/test_ticketlock.c new file +12
-0@@ -0,0 +1,311 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2018-2019 Arm Limited */ + +#include <stdio.h> +#include <stdint.h> +#include <inttypes.h> +#include <string.h> +#include <unistd.h> +#include <sys/queue.h> + +#include <rte_common.h> +#include <rte_memory.h>
Quoted 9 lines
app/test/test_ticketlock.c line 19
Quoted 6 lines
app/test/test_ticketlock.c new file +6
-0+#include <rte_per_lcore.h> +#include <rte_launch.h> +#include <rte_eal.h> +#include <rte_lcore.h> +#include <rte_cycles.h> +#include <generic/rte_ticketlock.h> it should be just <
rte_ticketlock.h>
Change this to <rte_ticketlock.h> by adding a new rte_ticketlock.h file in lib/librte_eal/common/include/arch/arm in V6.
Quoted 4 lines
app/test/test_ticketlock.c line 20
Quoted 1 line
app/test/test_ticketlock.c new file +1
-0+#include <rte_atomic.h> Please sort this in alphabetical order.
Do this in v6.
v6 cover ticketlock: implement ticketlock and add test case
In reply to Joyce Kongv6:
Add rte_ticketlock.h in lib/librte_eal/common/include/arch/arm/. Sort header inside ticketlock files by alphabetical order.
v5:
Change the variants inside rte_ticket_lock from unint to uint16_t for binary compatibility with the plain spin lock(suggested by Honnappa Nagarahalli)).
v4:
Change some assignment operation in recursive ticket lock to __atomic.
V3:
1.Update ticketlock intrduction(suggested by Honnappa Nagarahalli).
2.Change the implementation of rte_ticketlock_trylock to CAS(suggested by Honnappa Nagarahalli).
V2:
1.Update commit message(suggested by Jerin Jacob). 2.Add ticketlock test cases(suggested by Jerin Jacob).
V1:
Implement ticket lock to improve lock fairness and prdictability.
As shown on thundex-2 platform:
*** ticketlock_autotest with this patch ***
Core [0] count = 496
Core [1] count = 495
Core [2] count = 498
...
Core [209] count = 488
Core [210] count = 490
Core [211] count = 474Joyce Kong (2):
eal/ticketlock: ticket based to improve fairness test/ticketlock: add ticket lock test case
MAINTAINERS | 6 + app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 311 +++++++++++++++++++++ doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/arch/arm/rte_ticketlock.h | 64 +++++ .../common/include/generic/rte_ticketlock.h | 308 ++++++++++++++++++++ lib/librte_eal/common/meson.build | 1 + 10 files changed, 700 insertions(+), 1 deletion(-) create mode 100644 app/test/test_ticketlock.c create mode 100644 lib/librte_eal/common/include/arch/arm/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
Signature · 1 line
2.7.4
PATCH v6 1/2 eal/ticketlock: ticket based to improve fairness
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time.
This patch introduces ticketlock which gives each waiting thread a ticket and they can take the lock one by one. First come, first serviced. This avoids starvation for too long time and is more predictable.
--- MAINTAINERS | 5 + doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/arch/arm/rte_ticketlock.h | 64 +++++ .../common/include/generic/rte_ticketlock.h | 308 +++++++++++++++++++++ lib/librte_eal/common/meson.build | 1 + 6 files changed, 380 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/include/arch/arm/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
MAINTAINERS +5 -0
@@ -210,6 +210,11 @@ M: Cristian Dumitrescu <[email protected]> F: lib/librte_eal/common/include/rte_bitmap.h F: app/test/test_bitmap.c +Ticketlock +M: Joyce Kong <[email protected]> +F: lib/librte_eal/common/include/generic/rte_ticketlock.h +F: lib/librte_eal/common/include/arch/arm/rte_ticketlock.h + ARM v7 M: Jan Viktorin <[email protected]> M: Gavin Hu <[email protected]>
doc/api/doxy-api-index.md +1 -0
@@ -65,6 +65,7 @@ The public API headers are grouped by topics: [atomic] (@ref rte_atomic.h), [rwlock] (@ref rte_rwlock.h), [spinlock] (@ref rte_spinlock.h) + [ticketlock] (@ref rte_ticketlock.h) - **CPU arch**: [branch prediction] (@ref rte_branch_prediction.h),
lib/librte_eal/common/Makefile +1 -1
@@ -20,7 +20,7 @@ INC += rte_bitmap.h rte_vfio.h rte_hypervisor.h rte_test.h INC += rte_reciprocal.h rte_fbarray.h rte_uuid.h GENERIC_INC := rte_atomic.h rte_byteorder.h rte_cycles.h rte_prefetch.h -GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h +GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h rte_ticketlock.h GENERIC_INC += rte_vect.h rte_pause.h rte_io.h # defined in mk/arch/$(RTE_ARCH)/rte.vars.mk
lib/librte_eal/common/include/arch/arm/rte_ticketlock.h new file +64 -0
64 lines not on this page · raw message
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +308 -0
308 lines not on this page · raw message
lib/librte_eal/common/meson.build +1 -0
@@ -99,6 +99,7 @@ generic_headers = files( 'include/generic/rte_prefetch.h', 'include/generic/rte_rwlock.h', 'include/generic/rte_spinlock.h', + 'include/generic/rte_ticketlock.h', 'include/generic/rte_vect.h') install_headers(generic_headers, subdir: 'generic')
Signature · 1 line
2.7.4
Re: [PATCH v6 1/2] eal/ticketlock: ticket based to improve fairness
Hi,
Quoted 91 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +85
-0@@ -0,0 +1,308 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_H_ +#define _RTE_TICKETLOCK_H_ + +/** + * @file + * + * RTE ticket locks + * + * This file defines an API for ticket locks, which give each waiting + * thread a ticket and take the lock one by one, first come, first + * serviced. + * + * All locks must be initialised before use, and only initialised once. + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <rte_common.h> +#include <rte_lcore.h> +#include <rte_pause.h> + +/** + * The rte_ticketlock_t type. + */ +typedef struct { + uint16_t current; + uint16_t next; +} rte_ticketlock_t; + +/** + * A static ticketlock initializer. + */ +#define RTE_TICKETLOCK_INITIALIZER { 0 } + +/** + * Initialize the ticketlock to an unlocked state. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_init(rte_ticketlock_t *tl) +{ + __atomic_store_n(&tl->current, 0, __ATOMIC_RELAXED); + __atomic_store_n(&tl->next, 0, __ATOMIC_RELAXED); +} + +/** + * Take the ticketlock. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_lock(rte_ticketlock_t *tl) +{ + uint16_t me = __atomic_fetch_add(&tl->next, 1, __ATOMIC_RELAXED); + while (__atomic_load_n(&tl->current, __ATOMIC_ACQUIRE) != me) + rte_pause(); +} + +/** + * Release the ticketlock. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_unlock(rte_ticketlock_t *tl) +{ + uint16_t i = __atomic_load_n(&tl->current, __ATOMIC_RELAXED); + __atomic_store_n(&tl->current, i+1, __ATOMIC_RELEASE); +} + +/** + * Try to take the lock. + * + * @param tl
lib/librte_eal/common/include/generic/rte_ticketlock.h line 95
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+* A pointer to the ticketlock. +* @return +* 1 if the lock is successfully taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_trylock (rte_ticketlock_t *tl) +{ +uint16_t next =__atomic_load_n (&tl->next, __ATOMIC_RELAXED); +uint16_t cur =__atomic_load_n (&tl->current, __ATOMIC_RELAXED); +if (next == cur) {
Probably a naïve one:
Suppose next==cur==1 here, then this thread will experience really long context switch,
so next time it continues its execution tl->next value will wrap-up and will be 1 again, and tl->current==0 (lock held). I suppose this function will set tl->next=2 and will return a success?
Wouldn't be better here and in _is_locked_ to do load/store for next/current values in one go
(using 32bit op)?
Konstantin
Quoted 23 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +23
-0+if (__atomic_compare_exchange_n (&tl->next, &next, next+1 , +0 , __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) +return 1 ; + } + +return 0 ; +} + +/** +* Test if the lock is taken. +* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock icurrently taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_is_locked (rte_ticketlock_t *tl) +{ +return (__atomic_load_n (&tl->current, __ATOMIC_ACQUIRE) != +__atomic_load_n (&tl->next, __ATOMIC_ACQUIRE)); +} +
Re: [PATCH v6 1/2] eal/ticketlock: ticket based to improve fairness
Hi Konstantin,
Quoted 113 lines
-----Original Message-----
From: Ananyev, Konstantin <[email protected]>
Sent: Friday, March 15, 2019 8:56 PM
To: Joyce Kong (Arm Technology China) <[email protected]>; [email protected]
Cc: nd <[email protected]>; [email protected]; [email protected]; [email protected]; Honnappa Nagarahalli <[email protected]>; Gavin Hu (Arm Technology China) <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v6 1/2] eal/ticketlock: ticket based to improve fairnessHi,
Review
Quoted 1 line
(quoted diff) +0
-0
b/lib/librte_eal/common/include/generic/rte_ticketlock.hQuoted 59 lines
new file mode 100644
index 0000000..d63aaaalib/librte_eal/common/include/generic/rte_ticketlock.h new file +54
-0@@ -0,0 +1,308 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_H_ +#define _RTE_TICKETLOCK_H_ + +/** + * @file + * + * RTE ticket locks + * + * This file defines an API for ticket locks, which give each waiting + * thread a ticket and take the lock one by one, first come, first + * serviced. + * + * All locks must be initialised before use, and only initialised once. + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <rte_common.h> +#include <rte_lcore.h> +#include <rte_pause.h> + +/** + * The rte_ticketlock_t type. + */ +typedef struct { + uint16_t current; + uint16_t next; +} rte_ticketlock_t; + +/** + * A static ticketlock initializer. + */ +#define RTE_TICKETLOCK_INITIALIZER { 0 } + +/** + * Initialize the ticketlock to an unlocked state. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_init(rte_ticketlock_t *tl) +{ + __atomic_store_n(&tl->current, 0, __ATOMIC_RELAXED); + __atomic_store_n(&tl->next, 0, __ATOMIC_RELAXED); +} +lib/librte_eal/common/include/generic/rte_ticketlock.h line 64
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+/** +* Take the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_lock (rte_ticketlock_t *tl) +{ + uint16_t me = __atomic_fetch_add(&tl->next, 1,
__ATOMIC_RELAXED);Quoted 27 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +27
-0+while (__atomic_load_n (&tl->current, __ATOMIC_ACQUIRE) != me) +rte_pause (); +} + +/** +* Release the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_unlock (rte_ticketlock_t *tl) +{ +uint16_t i =__atomic_load_n (&tl->current, __ATOMIC_RELAXED); +__atomic_store_n (&tl->current, i+1 , __ATOMIC_RELEASE); +} + +/** +* Try to take the lock. +* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock is successfully taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_trylock (rte_ticketlock_t *tl)
Quoted 8 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h line 95
Quoted 4 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +4
-0+{ +uint16_t next =__atomic_load_n (&tl->next, __ATOMIC_RELAXED); +uint16_t cur =__atomic_load_n (&tl->current, __ATOMIC_RELAXED); +if (next == cur) {Probably a naïve one:
Suppose next==cur==1 here, then this thread will experience really long context switch,
By saying context switch, do you mean running to here, it is out of CPU time and starving for CPU?
Quoted 3 lines
so next time it continues its execution
tl->nextvalue will wrap-up and will be 1 again, and tl->current==0 (lock held).
I suppose this function will set tl->next=2 and will return a success?
If this thread was swapped out and another thread took/attempted to take the lock, yes, tl->next == 2 here, But as next == 1 unchanged, so it would not return a success.
Quoted 4 lines
Wouldn't be better here and in _is_locked_ to do load/store for next/current values in one go
(using 32bit op)?
Konstantin
To load both in one go is feasible, but no necessary as we need to compare them.
We don't need store both as in this function tl->current is read only.tl->next is read-update-store, I ever thought of combining the two if-statements to one __atomic_compare_exchange_n(&(&tl->next,&tl->current, tl->next+1, ...), but tl->next+1 is out of atomicity and may be the old value and corrupt the ticket lock waiting chain.
The current code works ok except it may fail spuriously(in case during context switch, the lock was taken and released by other threads, moving tl->next forward, in this case The lock is available but not obtained by this trylock). Anyway, as the name suggests, it is a try/attempt, a spurious fail is not a big deal? And in most cases, dpdk running on dedicated cores, the context switch will not happen at all.
Any more comments are welcome!
Quoted 25 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h line 96
Quoted 2 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +1
-0+ if (__atomic_compare_exchange_n(&tl->next, &next,next+1,
Quoted 22 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +22
-0+ 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) +return 1 ; + } + +return 0 ; +} + +/** +* Test if the lock is taken. +* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock icurrently taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_is_locked (rte_ticketlock_t *tl) +{ +return (__atomic_load_n (&tl->current, __ATOMIC_ACQUIRE) != +__atomic_load_n (&tl->next, __ATOMIC_ACQUIRE)); +} +
Re: [PATCH v6 1/2] eal/ticketlock: ticket based to improve fairness
Hi Gavin,
Quoted 109 lines
Quoted 100 lines
Review
Quoted 2 lines
(quoted diff) +0
-0
b/lib/librte_eal/common/include/generic/rte_ticketlock.hQuoted 59 lines
new file mode 100644
index 0000000..d63aaaalib/librte_eal/common/include/generic/rte_ticketlock.h new file +54
-0@@ -0,0 +1,308 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_H_ +#define _RTE_TICKETLOCK_H_ + +/** + * @file + * + * RTE ticket locks + * + * This file defines an API for ticket locks, which give each waiting + * thread a ticket and take the lock one by one, first come, first + * serviced. + * + * All locks must be initialised before use, and only initialised once. + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <rte_common.h> +#include <rte_lcore.h> +#include <rte_pause.h> + +/** + * The rte_ticketlock_t type. + */ +typedef struct { + uint16_t current; + uint16_t next; +} rte_ticketlock_t; + +/** + * A static ticketlock initializer. + */ +#define RTE_TICKETLOCK_INITIALIZER { 0 } + +/** + * Initialize the ticketlock to an unlocked state. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_init(rte_ticketlock_t *tl) +{ + __atomic_store_n(&tl->current, 0, __ATOMIC_RELAXED); + __atomic_store_n(&tl->next, 0, __ATOMIC_RELAXED); +} +lib/librte_eal/common/include/generic/rte_ticketlock.h line 64
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+/** +* Take the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_lock (rte_ticketlock_t *tl) +{ + uint16_t me = __atomic_fetch_add(&tl->next, 1,
__ATOMIC_RELAXED);Quoted 27 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +27
-0+while (__atomic_load_n (&tl->current, __ATOMIC_ACQUIRE) != me) +rte_pause (); +} + +/** +* Release the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_unlock (rte_ticketlock_t *tl) +{ +uint16_t i =__atomic_load_n (&tl->current, __ATOMIC_RELAXED); +__atomic_store_n (&tl->current, i+1 , __ATOMIC_RELEASE); +} + +/** +* Try to take the lock. +* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock is successfully taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_trylock (rte_ticketlock_t *tl)Quoted 8 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h line 95
Quoted 4 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +4
-0+{ +uint16_t next =__atomic_load_n (&tl->next, __ATOMIC_RELAXED); +uint16_t cur =__atomic_load_n (&tl->current, __ATOMIC_RELAXED); +if (next == cur) {Probably a naïve one:
Suppose next==cur==1 here, then this thread will experience really long context switch,
Quoted 1 line
By saying context switch, do you mean running to here, it is out of CPU time and starving for CPU?
Yes.
Quoted 7 lines
Quoted 4 lines
so next time it continues its execution
tl->nextvalue will wrap-up and will be 1 again, and tl->current==0 (lock held).
I suppose this function will set tl->next=2 and will return a success?If this thread was swapped out and another thread took/attempted to take the lock, yes,
tl->next== 2 here, But as next == 1 unchanged, so it would not return a success.
I am not talking about situation when tl->next == 2,tl->current==1 (just one lock() was executed by different thread). I am talking about situation when this thread was out of cpu for significant amount of cycles,
and in that period tl->next and tl->current were wrapped around (they both reached UINT16_MAX, then 0). i.e. UINT16_MAX lock/unlock were executed while this thread was away from cpu.
After that another thread just did successful lock(), so tl->next==1 and tl->current==0.
Now this thread wakeups and continues with:__atomic_compare_exchange_n(&tl->next, &next, next+1, ...)
As both tl->next==1 and next==1, it will succeed.
So we have 2 threads assuming they grabbed the lock successfully.
Konstantin
Quoted 44 lines
Quoted 5 lines
Wouldn't be better here and in _is_locked_ to do load/store for next/current values in one go
(using 32bit op)?
KonstantinTo load both in one go is feasible, but no necessary as we need to compare them.
We don't need store both as in this functiontl->currentis read only.tl->nextis read-update-store, I ever thought of combining the two if-statements to one __atomic_compare_exchange_n(&(&tl->next,&tl-Quoted 1 line
current, tl->next+1, ...),
but tl->next+1 is out of atomicity and may be the old value and corrupt the ticket lock waiting chain.
The current code works ok except it may fail spuriously(in case during context switch, the lock was taken and released by other threads, moving
tl->nextforward, in this case
The lock is available but not obtained by this trylock). Anyway, as the name suggests, it is a try/attempt, a spurious fail is not a big deal? And in most cases, dpdk running on dedicated cores,
the context switch will not happen at all.Any more comments are welcome!
Quoted 25 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h line 96
Quoted 2 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +1
-0+ if (__atomic_compare_exchange_n(&tl->next, &next,next+1,
Quoted 22 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +22
-0+ 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) +return 1 ; + } + +return 0 ; +} + +/** +* Test if the lock is taken. +* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock icurrently taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_is_locked (rte_ticketlock_t *tl) +{ +return (__atomic_load_n (&tl->current, __ATOMIC_ACQUIRE) != +__atomic_load_n (&tl->next, __ATOMIC_ACQUIRE)); +} +
Re: [PATCH v6 1/2] eal/ticketlock: ticket based to improve fairness
Hi Konstantin,
Quoted 146 lines
-----Original Message-----
From: Ananyev, Konstantin <[email protected]>
Sent: Tuesday, March 19, 2019 6:15 PM
To: Gavin Hu (Arm Technology China) <[email protected]>; [email protected]
Cc: nd <[email protected]>; [email protected]; [email protected]; [email protected]; Honnappa Nagarahalli <[email protected]>; Joyce Kong (Arm Technology China) <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v6 1/2] eal/ticketlock: ticket based to improve fairnessHi Gavin,
Quoted 109 lines
Quoted 100 lines
Review
Quoted 2 lines
(quoted diff) +0
-0
b/lib/librte_eal/common/include/generic/rte_ticketlock.hQuoted 59 lines
new file mode 100644
index 0000000..d63aaaalib/librte_eal/common/include/generic/rte_ticketlock.h new file +54
-0@@ -0,0 +1,308 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_H_ +#define _RTE_TICKETLOCK_H_ + +/** + * @file + * + * RTE ticket locks + * + * This file defines an API for ticket locks, which give each waiting + * thread a ticket and take the lock one by one, first come, first + * serviced. + * + * All locks must be initialised before use, and only initialised once. + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <rte_common.h> +#include <rte_lcore.h> +#include <rte_pause.h> + +/** + * The rte_ticketlock_t type. + */ +typedef struct { + uint16_t current; + uint16_t next; +} rte_ticketlock_t; + +/** + * A static ticketlock initializer. + */ +#define RTE_TICKETLOCK_INITIALIZER { 0 } + +/** + * Initialize the ticketlock to an unlocked state. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_init(rte_ticketlock_t *tl) +{ + __atomic_store_n(&tl->current, 0, __ATOMIC_RELAXED); + __atomic_store_n(&tl->next, 0, __ATOMIC_RELAXED); +} +lib/librte_eal/common/include/generic/rte_ticketlock.h line 64
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+/** +* Take the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_lock (rte_ticketlock_t *tl) +{ + uint16_t me = __atomic_fetch_add(&tl->next, 1,
__ATOMIC_RELAXED);Quoted 27 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +27
-0+while (__atomic_load_n (&tl->current, __ATOMIC_ACQUIRE) != me) +rte_pause (); +} + +/** +* Release the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_unlock (rte_ticketlock_t *tl) +{ +uint16_t i =__atomic_load_n (&tl->current, __ATOMIC_RELAXED); +__atomic_store_n (&tl->current, i+1 , __ATOMIC_RELEASE); +} + +/** +* Try to take the lock. +* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock is successfully taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_trylock (rte_ticketlock_t *tl)Quoted 8 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h line 95
Quoted 4 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +4
-0+{ +uint16_t next =__atomic_load_n (&tl->next, __ATOMIC_RELAXED); +uint16_t cur =__atomic_load_n (&tl->current, __ATOMIC_RELAXED); +if (next == cur) {Probably a naïve one:
Suppose next==cur==1 here, then this thread will experience really long context switch,Quoted 1 line
By saying context switch, do you mean running to here, it is out of CPU time
and starving for CPU?
Yes.
Quoted 6 lines
Quoted 4 lines
so next time it continues its execution
tl->nextvalue will wrap-up and will be 1 again, and tl->current==0 (lock held).
I suppose this function will set tl->next=2 and will return a success?If this thread was swapped out and another thread took/attempted to take
the lock, yes,
tl->next== 2 here,Quoted 1 line
But as next == 1 unchanged, so it would not return a success.
I am not talking about situation when
tl->next== 2,tl->current==1 (just onelock()was executed by different thread).
I am talking about situation when this thread was out of cpu for significant amount of cycles,
and in that periodtl->nextandtl->currentwere wrapped around (they both reachedUINT16_MAX, then 0).
i.e.UINT16_MAXlock/unlock were executed while this thread was away from cpu.
After that another thread just did successfullock(), so tl->next==1 and tl-
Quoted 7 lines
Quoted 1 line
current==0.
Now this thread wakeups and continues with:
__atomic_compare_exchange_n(&tl->next, &next, next+1, ...)As both tl->next==1 and next==1, it will succeed.
So we have 2 threads assuming they grabbed the lock successfully. Konstantin
Now I understood your points, but not sure if it is a rare or even impossible case for this thread stalls for CPU and during this time, the other threads have taken the lock for 2^16 times, to wrap up.
Anyway I made a patch, currently in internal review to fix this issue, the basic idea is to compare not only the next, but also the current, and update the next(+1 and take the lock) only if both of them were not changed(or wrapped up and the lock released). I will submit the patch after internal review approved. Please let me know if you have more comments.
Quoted 49 lines
Quoted 7 lines
Quoted 5 lines
Wouldn't be better here and in _is_locked_ to do load/store for next/current values in one go
(using 32bit op)?
KonstantinTo load both in one go is feasible, but no necessary as we need to compare
them.
Quoted 2 lines
We don't need store both as in this function
tl->currentis read only.tl->nextis read-update-store, I ever thought of combining the two if-statements to one __atomic_compare_exchange_n(&(&tl->next,&tl-
Quoted 2 lines
Quoted 1 line
current, tl->next+1, ...),
but tl->next+1 is out of atomicity and may be the old value and corrupt the
ticket lock waiting chain.
Quoted 2 lines
The current code works ok except it may fail spuriously(in case during
context switch, the lock was taken and released by other threads,
Quoted 2 lines
moving
tl->nextforward, in this case
The lock is available but not obtained by this trylock). Anyway, as the namesuggests, it is a try/attempt, a spurious fail is not a big deal?
Quoted 29 lines
And in most cases, dpdk running on dedicated cores, the context switch will not happen at all.
Any more comments are welcome!
Quoted 25 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h line 96
Quoted 2 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +1
-0+ if (__atomic_compare_exchange_n(&tl->next, &next,next+1,
Quoted 22 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +22
-0+ 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) +return 1 ; + } + +return 0 ; +} + +/** +* Test if the lock is taken. +* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock icurrently taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_is_locked (rte_ticketlock_t *tl) +{ +return (__atomic_load_n (&tl->current, __ATOMIC_ACQUIRE) != +__atomic_load_n (&tl->next, __ATOMIC_ACQUIRE)); +} +
Re: [PATCH v6 1/2] eal/ticketlock: ticket based to improve fairness
Hi Gavin,
Quoted 133 lines
Quoted 133 lines
Quoted 109 lines
Quoted 100 lines
Review
Quoted 2 lines
(quoted diff) +0
-0
b/lib/librte_eal/common/include/generic/rte_ticketlock.hQuoted 59 lines
new file mode 100644
index 0000000..d63aaaalib/librte_eal/common/include/generic/rte_ticketlock.h new file +54
-0@@ -0,0 +1,308 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_H_ +#define _RTE_TICKETLOCK_H_ + +/** + * @file + * + * RTE ticket locks + * + * This file defines an API for ticket locks, which give each waiting + * thread a ticket and take the lock one by one, first come, first + * serviced. + * + * All locks must be initialised before use, and only initialised once. + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <rte_common.h> +#include <rte_lcore.h> +#include <rte_pause.h> + +/** + * The rte_ticketlock_t type. + */ +typedef struct { + uint16_t current; + uint16_t next; +} rte_ticketlock_t; + +/** + * A static ticketlock initializer. + */ +#define RTE_TICKETLOCK_INITIALIZER { 0 } + +/** + * Initialize the ticketlock to an unlocked state. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_init(rte_ticketlock_t *tl) +{ + __atomic_store_n(&tl->current, 0, __ATOMIC_RELAXED); + __atomic_store_n(&tl->next, 0, __ATOMIC_RELAXED); +} +lib/librte_eal/common/include/generic/rte_ticketlock.h line 64
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+/** +* Take the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_lock (rte_ticketlock_t *tl) +{ + uint16_t me = __atomic_fetch_add(&tl->next, 1,
__ATOMIC_RELAXED);Quoted 27 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +27
-0+while (__atomic_load_n (&tl->current, __ATOMIC_ACQUIRE) != me) +rte_pause (); +} + +/** +* Release the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_unlock (rte_ticketlock_t *tl) +{ +uint16_t i =__atomic_load_n (&tl->current, __ATOMIC_RELAXED); +__atomic_store_n (&tl->current, i+1 , __ATOMIC_RELEASE); +} + +/** +* Try to take the lock. +* +* @param tl +* A pointer to the ticketlock. +* @return +* 1 if the lock is successfully taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_trylock (rte_ticketlock_t *tl)Quoted 8 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h line 95
Quoted 4 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +4
-0+{ +uint16_t next =__atomic_load_n (&tl->next, __ATOMIC_RELAXED); +uint16_t cur =__atomic_load_n (&tl->current, __ATOMIC_RELAXED); +if (next == cur) {Probably a naïve one:
Suppose next==cur==1 here, then this thread will experience really long context switch,Quoted 1 line
By saying context switch, do you mean running to here, it is out of CPU time
and starving for CPU?
Yes.
Quoted 6 lines
Quoted 4 lines
so next time it continues its execution
tl->nextvalue will wrap-up and will be 1 again, and tl->current==0 (lock held).
I suppose this function will set tl->next=2 and will return a success?If this thread was swapped out and another thread took/attempted to take
the lock, yes,
tl->next== 2 here,Quoted 1 line
But as next == 1 unchanged, so it would not return a success.
I am not talking about situation when
tl->next== 2,tl->current==1 (just onelock()was executed by different thread).
I am talking about situation when this thread was out of cpu for significant amount of cycles,
and in that periodtl->nextandtl->currentwere wrapped around (they both reachedUINT16_MAX, then 0).
i.e.UINT16_MAXlock/unlock were executed while this thread was away from cpu.
After that another thread just did successfullock(), so tl->next==1 and tl-Quoted 1 line
current==0.
Quoted 8 lines
Quoted 6 lines
Now this thread wakeups and continues with:
__atomic_compare_exchange_n(&tl->next, &next, next+1, ...)As both tl->next==1 and next==1, it will succeed.
So we have 2 threads assuming they grabbed the lock successfully. KonstantinNow I understood your points, but not sure if it is a rare or even impossible case for this thread stalls for CPU and during this time, the other threads have taken the lock for 2^16 times, to wrap up.
I am agree it should be very rare, but I am not sure it is impossible. Let say thread is doing lock/unlock in a loop, with one iteration ~100 cycles. Then it would wrap around in ~6.5M cycles (~3ms on modern cpus).
Quoted 4 lines
Anyway I made a patch, currently in internal review to fix this issue, the basic idea is to compare not only the next, but also the current, and update the next(+1 and take the lock) only if both of them were not changed(or wrapped up and the lock released).
I will submit the patch after internal review approved. Please let me know if you have more comments.
Ok, thanks
Konstantin
Re: [PATCH v6 1/2] eal/ticketlock: ticket based to improve fairness
Hi Konstantin,
Quoted 162 lines
-----Original Message-----
From: Ananyev, Konstantin <[email protected]>
Sent: Wednesday, March 20, 2019 5:47 PM
To: Gavin Hu (Arm Technology China) <[email protected]>; [email protected]
Cc: nd <[email protected]>; [email protected]; [email protected]; [email protected]; Honnappa Nagarahalli <[email protected]>; Joyce Kong (Arm Technology China) <[email protected]>
Subject: RE: [dpdk-dev] [PATCH v6 1/2] eal/ticketlock: ticket based to improve fairnessHi Gavin,
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
Quoted 73 lines
Review
Quoted 2 lines
(quoted diff) +0
-0
b/lib/librte_eal/common/include/generic/rte_ticketlock.hQuoted 59 lines
new file mode 100644
index 0000000..d63aaaalib/librte_eal/common/include/generic/rte_ticketlock.h new file +54
-0@@ -0,0 +1,308 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_H_ +#define _RTE_TICKETLOCK_H_ + +/** + * @file + * + * RTE ticket locks + * + * This file defines an API for ticket locks, which give each waiting + * thread a ticket and take the lock one by one, first come, first + * serviced. + * + * All locks must be initialised before use, and only initialised once. + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <rte_common.h> +#include <rte_lcore.h> +#include <rte_pause.h> + +/** + * The rte_ticketlock_t type. + */ +typedef struct { + uint16_t current; + uint16_t next; +} rte_ticketlock_t; + +/** + * A static ticketlock initializer. + */ +#define RTE_TICKETLOCK_INITIALIZER { 0 } + +/** + * Initialize the ticketlock to an unlocked state. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_init(rte_ticketlock_t *tl) +{ + __atomic_store_n(&tl->current, 0, __ATOMIC_RELAXED); + __atomic_store_n(&tl->next, 0, __ATOMIC_RELAXED); +} +lib/librte_eal/common/include/generic/rte_ticketlock.h line 64
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+/** +* Take the ticketlock. +* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_lock (rte_ticketlock_t *tl) +{ + uint16_t me = __atomic_fetch_add(&tl->next, 1,
__ATOMIC_RELAXED);Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
lib/librte_eal/common/include/generic/rte_ticketlock.h line 65
Quoted 1 line
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +1
-0+ while (__atomic_load_n(&tl->current,
__ATOMIC_ACQUIRE) != me)Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +5
-0+rte_pause (); +} + +/** +* Release the ticketlock. Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h
Quoted 8 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +8
-0+* +* @param tl +* A pointer to the ticketlock. +*/ +static inline __rte_experimental void +rte_ticketlock_unlock (rte_ticketlock_t *tl) +{ + uint16_t i = __atomic_load_n(&tl->current,
__ATOMIC_RELAXED);Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +7
-0+__atomic_store_n (&tl->current, i+1 , __ATOMIC_RELEASE); +} + +/** +* Try to take the lock. +* +* @param tl Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h
Quoted 8 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +8
-0+* A pointer to the ticketlock. +* @return +* 1 if the lock is successfully taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_trylock (rte_ticketlock_t *tl) +{ + uint16_t next = __atomic_load_n(&tl->next,
__ATOMIC_RELAXED);Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
lib/librte_eal/common/include/generic/rte_ticketlock.h line 94
Quoted 1 line
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +1
-0+ uint16_t cur = __atomic_load_n(&tl->current,
__ATOMIC_RELAXED);Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h line 95
Quoted 1 line
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +1
-0+if (next == cur) {Probably a naïve one:
Suppose next==cur==1 here, then this thread will experience reallylong
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
context switch,
By saying context switch, do you mean running to here, it is out of CPU
time
Quoted 6 lines
Quoted 6 lines
and starving for CPU?
Yes.
Quoted 2 lines
Quoted 2 lines
so next time it continues its execution
tl->nextvalue will wrap-up andwill
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
be 1 again, and tl->current==0 (lock held).
I suppose this function will set tl->next=2 and will return a success?If this thread was swapped out and another thread took/attempted to
take
Quoted 4 lines
Quoted 4 lines
the lock, yes,
tl->next== 2 here,Quoted 1 line
But as next == 1 unchanged, so it would not return a success.
I am not talking about situation when
tl->next== 2,tl->current==1 (justone
Quoted 4 lines
Quoted 4 lines
lock()was executed by different thread).
I am talking about situation when this thread was out of cpu for significant amount of cycles,
and in that periodtl->nextandtl->currentwere wrapped around (theyboth
Quoted 2 lines
Quoted 2 lines
reached
UINT16_MAX, then 0).
i.e.UINT16_MAXlock/unlock were executed while this thread was awayfrom
Quoted 9 lines
Quoted 9 lines
cpu.
After that another thread just did successfullock(), so tl->next==1 and tl-Quoted 1 line
current==0.
Now this thread wakeups and continues with:
__atomic_compare_exchange_n(&tl->next, &next, next+1, ...)As both tl->next==1 and next==1, it will succeed.
So we have 2 threads assuming they grabbed the lock successfully. Konstantin
Quoted 8 lines
Quoted 1 line
Now I understood your points, but not sure if it is a rare or even impossible
case for this thread stalls for CPU and during this time, the other
Quoted 1 line
threads have taken the lock for 2^16 times, to wrap up.
I am agree it should be very rare, but I am not sure it is impossible. Let say thread is doing lock/unlock in a loop, with one iteration ~100 cycles. Then it would wrap around in ~6.5M cycles (~3ms on modern cpus).
Thanks, agree, your quantitative way of analysis helped me understand the issue.
Quoted 10 lines
Quoted 2 lines
Anyway I made a patch, currently in internal review to fix this issue, the
basic idea is to compare not only the next, but also the current, and
Quoted 1 line
update the next(+1 and take the lock) only if both of them were not
changed(or wrapped up and the lock released).
Quoted 1 line
I will submit the patch after internal review approved. Please let me know if
you have more comments.
Ok, thanks
Konstantin
The fix was submitted into v7, 1/3, could you help review?
Thanks!
Gavin
PATCH v6 2/2 test/ticketlock: add ticket lock test case
In reply to Joyce KongAdd test cases for ticket lock, recursive ticket lock, and ticket lock performance.
--- MAINTAINERS | 1 + app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 311 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 320 insertions(+) create mode 100644 app/test/test_ticketlock.c
MAINTAINERS +1 -0
@@ -214,6 +214,7 @@ Ticketlock M: Joyce Kong <[email protected]> F: lib/librte_eal/common/include/generic/rte_ticketlock.h F: lib/librte_eal/common/include/arch/arm/rte_ticketlock.h +F: app/test/test_ticketlock.c ARM v7 M: Jan Viktorin <[email protected]>
app/test/Makefile +1 -0
@@ -65,6 +65,7 @@ SRCS-y += test_barrier.c SRCS-y += test_malloc.c SRCS-y += test_cycles.c SRCS-y += test_spinlock.c +SRCS-y += test_ticketlock.c SRCS-y += test_memory.c SRCS-y += test_memzone.c SRCS-y += test_bitmap.c
app/test/autotest_data.py +6 -0
@@ -171,6 +171,12 @@ "Report": None, }, { + "Name": "Ticketlock autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + } + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,
app/test/meson.build +1 -0
@@ -107,6 +107,7 @@ test_sources = files('commands.c', 'test_timer.c', 'test_timer_perf.c', 'test_timer_racecond.c', + 'test_ticketlock.c', 'test_version.c', 'virtual_pmd.c' )
app/test/test_ticketlock.c new file +311 -0
311 lines not on this page · raw message
Signature · 1 line
2.7.4
v7 cover ticketlock: implement ticketlock and add test case
In reply to Joyce Kongv7:
1. Modify trylock to compare both current and next fields to gurantee the lock being obtained only if tl->current==1(lock is free).
As within the trylock function, suppose next==curr=1, then this thread will experience really long context switch, and next time it continues its execution and tl->next wraps around to 1 again and tl_couurent==0(lock held by another thread),this trylock will return a success, that means two threads holding the lock. (Suggested by Konstantin Ananyev)
2. Let all archs use generic ticketlock implementation.
v6:
Add rte_ticketlock.h in lib/librte_eal/common/include/arch/arm/. Sort header inside ticketlock files by alphabetical order.
v5:
Change the variants inside rte_ticket_lock from unint to uint16_t for binary compatibility with the plain spin lock(suggested by Honnappa Nagarahalli)).
v4:
Change some assignment operation in recursive ticket lock to __atomic.
V3:
1.Update ticketlock intrduction(suggested by Honnappa Nagarahalli).
2.Change the implementation of rte_ticketlock_trylock to CAS(suggested by Honnappa Nagarahalli).
V2:
1.Update commit message(suggested by Jerin Jacob). 2.Add ticketlock test cases(suggested by Jerin Jacob).
V1:
Implement ticket lock to improve lock fairness and prdictability.
As shown on thundex-2 platform:
*** ticketlock_autotest with this patch ***
Core [0] count = 496
Core [1] count = 495
Core [2] count = 498
...
Core [209] count = 488
Core [210] count = 490
Core [211] count = 474Joyce Kong (3):
eal/ticketlock: ticket based to improve fairness eal/ticketlock: enable generic ticketlock on all arch test/ticketlock: add ticket lock test case
MAINTAINERS | 5 + app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 311 +++++++++++++++++++++ doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/arch/arm/rte_ticketlock.h | 22 ++ .../common/include/arch/ppc_64/rte_ticketlock.h | 18 ++ .../common/include/arch/x86/rte_ticketlock.h | 18 ++ .../common/include/generic/rte_ticketlock.h | 215 ++++++++++++++ lib/librte_eal/common/meson.build | 1 + 12 files changed, 600 insertions(+), 1 deletion(-) create mode 100644 app/test/test_ticketlock.c create mode 100644 lib/librte_eal/common/include/arch/arm/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/ppc_64/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/x86/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
Signature · 1 line
2.7.4
PATCH v7 2/3 eal/ticketlock: enable generic ticketlock on all arch
Let all architectures use generic ticketlock implementation.
--- .../common/include/arch/arm/rte_ticketlock.h | 22 ++++++++++++++++++++++ .../common/include/arch/ppc_64/rte_ticketlock.h | 18 ++++++++++++++++++ .../common/include/arch/x86/rte_ticketlock.h | 18 ++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 lib/librte_eal/common/include/arch/arm/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/ppc_64/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/x86/rte_ticketlock.h
lib/librte_eal/common/include/arch/arm/rte_ticketlock.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_ARM_H_ +#define _RTE_TICKETLOCK_ARM_H_ + +#ifndef RTE_FORCE_INTRINSICS +# error Platform must be built with CONFIG_RTE_FORCE_INTRINSICS +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#include "generic/rte_ticketlock.h" + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_TICKETLOCK_ARM_H_ */
lib/librte_eal/common/include/arch/ppc_64/rte_ticketlock.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_PPC_64_H_ +#define _RTE_TICKETLOCK_PPC_64_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "generic/rte_ticketlock.h" + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_TICKETLOCK_PPC_64_H_ */
lib/librte_eal/common/include/arch/x86/rte_ticketlock.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_X86_64_H_ +#define _RTE_TICKETLOCK_X86_64_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "generic/rte_ticketlock.h" + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_TICKETLOCK_X86_64_H_ */
Signature · 1 line
2.7.4
PATCH v7 3/3 test/ticketlock: add ticket lock test case
In reply to Joyce KongAdd test cases for ticket lock, recursive ticket lock, and ticket lock performance.
--- MAINTAINERS | 1 + app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 311 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 320 insertions(+) create mode 100644 app/test/test_ticketlock.c
MAINTAINERS +1 -0
@@ -213,6 +213,7 @@ F: app/test/test_bitmap.c Ticketlock M: Joyce Kong <[email protected]> F: lib/librte_eal/common/include/generic/rte_ticketlock.h +F: app/test/test_ticketlock.c ARM v7 M: Jan Viktorin <[email protected]>
app/test/Makefile +1 -0
@@ -65,6 +65,7 @@ SRCS-y += test_barrier.c SRCS-y += test_malloc.c SRCS-y += test_cycles.c SRCS-y += test_spinlock.c +SRCS-y += test_ticketlock.c SRCS-y += test_memory.c SRCS-y += test_memzone.c SRCS-y += test_bitmap.c
app/test/autotest_data.py +6 -0
@@ -171,6 +171,12 @@ "Report": None, }, { + "Name": "Ticketlock autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + } + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,
app/test/meson.build +1 -0
@@ -107,6 +107,7 @@ test_sources = files('commands.c', 'test_timer.c', 'test_timer_perf.c', 'test_timer_racecond.c', + 'test_ticketlock.c', 'test_version.c', 'virtual_pmd.c' )
app/test/test_ticketlock.c new file +311 -0
311 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [PATCH v7 3/3] test/ticketlock: add ticket lock test case
Quoted 133 lines
Add test cases for ticket lock, recursive ticket lock, and ticket lock performance.
Signed-off-by Joyce Kong <[email protected]>Reviewed-by Gavin Hu <[email protected]>Reviewed-by Ruifeng Wang <[email protected]>--- MAINTAINERS | 1 + app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 311 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 320 insertions(+) create mode 100644 app/test/test_ticketlock.cMAINTAINERS +1
-0@@ -213,6 +213,7 @@ F: app/test/test_bitmap.c Ticketlock M: Joyce Kong <[email protected]> F: lib/librte_eal/common/include/generic/rte_ticketlock.h +F: app/test/test_ticketlock.c ARM v7 M: Jan Viktorin <[email protected]>app/test/Makefile +1
-0@@ -65,6 +65,7 @@ SRCS-y += test_barrier.c SRCS-y += test_malloc.c SRCS-y += test_cycles.c SRCS-y += test_spinlock.c +SRCS-y += test_ticketlock.c SRCS-y += test_memory.c SRCS-y += test_memzone.c SRCS-y += test_bitmap.capp/test/autotest_data.py +6
-0@@ -171,6 +171,12 @@ "Report": None, }, { + "Name": "Ticketlock autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + } + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,app/test/meson.build +1
-0@@ -107,6 +107,7 @@ test_sources = files('commands.c', 'test_timer.c', 'test_timer_perf.c', 'test_timer_racecond.c', + 'test_ticketlock.c', 'test_version.c', 'virtual_pmd.c' )app/test/test_ticketlock.c new file +56
-0@@ -0,0 +1,311 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2018-2019 Arm Limited +*/ + +#include <inttypes.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <sys/queue.h> +#include <unistd.h> + +#include <rte_atomic.h> +#include <rte_common.h> +#include <rte_cycles.h> +#include <rte_eal.h> +#include <rte_launch.h> +#include <rte_lcore.h> +#include <rte_memory.h> +#include <rte_per_lcore.h> +#include <rte_ticketlock.h> + +#include "test.h" + +/* +* Ticketlock test +* ============= +* +* - There is a global ticketlock and a table of ticketlocks (one per lcore). +* +* - The test function takes all of these locks and launches the +* ``test_ticketlock_per_core()`` function on each core (except the master). +* +* - The function takes the global lock, display something, then releases +* the global lock. +* - The function takes the per-lcore lock, display something, then releases +* the per-core lock. +* +* - The main function unlocks the per-lcore locks sequentially and +* waits between each lock. This triggers the display of a message +* for each core, in the correct order. The autotest script checks that +* this order is correct. +* +* - A load test is carried out, with all cores attempting to lock a single lock +* multiple times +*/ + +static rte_ticketlock_t tl, tl_try; +static rte_ticketlock_t tl_tab[RTE_MAX_LCORE ]; +static rte_ticketlock_recursive_t tlr; +static unsigned int count; + +static rte_atomic32_t synchro; + +static int +test_ticketlock_per_core (__attribute__((unused))void *arg) +{
app/test/test_ticketlock.c line 66
Quoted 10 lines
app/test/test_ticketlock.c new file +10
-0+rte_ticketlock_lock (&tl); +printf ("Global lock taken on core %u\n" ,rte_lcore_id ()); +rte_ticketlock_unlock (&tl); + +rte_ticketlock_lock (&tl_tab[rte_lcore_id ()]); +printf ("Hello from core %u !\n" ,rte_lcore_id ()); +rte_ticketlock_unlock (&tl_tab[rte_lcore_id ()]); + +return 0 ; +}
I think that's probably no enough for functional testing.
Need something extra to ensure that it provides correct locking in MT env. Probably extend the perf test below to do both?
Something like that:
static uint64_t lcount __rte_cache_aligned;
static uint64_t lcore_count[RTE_MAX_LCORE] __rte_cache_aligned;
...
load_loop_fn(...)
{
...
rte_ticketlock_lock(&lk);
lcount++;
rte_ticketlock_unlock(&lk);
lcore_count[current_lcore]++;
}
Then in test_ticketlock_perf() make sure that sum of al lcore_count[] values equals to lcount value:
tcount = 0;
for (i = 0; i != RTE_DIM(lcore_count); i++)
tcount += lcore_count[i];
if (tcount != lcount)
<error>
Same thought for trylock.
Konstantin
Quoted 48 lines
app/test/test_ticketlock.c new file +48
-0+ +static int +test_ticketlock_recursive_per_core (__attribute__((unused))void *arg) +{ +unsigned int id =rte_lcore_id (); + +rte_ticketlock_recursive_lock (&tlr); +printf ("Global recursive lock taken on core %u - count = %d\n" , + id, tlr.count); +rte_ticketlock_recursive_lock (&tlr); +printf ("Global recursive lock taken on core %u - count = %d\n" , + id, tlr.count); +rte_ticketlock_recursive_lock (&tlr); +printf ("Global recursive lock taken on core %u - count = %d\n" , + id, tlr.count); + +printf ("Hello from within recursive locks from core %u !\n" , id); + +rte_ticketlock_recursive_unlock (&tlr); +printf ("Global recursive lock released on core %u - count = %d\n" , + id, tlr.count); +rte_ticketlock_recursive_unlock (&tlr); +printf ("Global recursive lock released on core %u - count = %d\n" , + id, tlr.count); +rte_ticketlock_recursive_unlock (&tlr); +printf ("Global recursive lock released on core %u - count = %d\n" , + id, tlr.count); + +return 0 ; +} + +static rte_ticketlock_t lk =RTE_TICKETLOCK_INITIALIZER ; +static uint64_t lock_count[RTE_MAX_LCORE ] = {0 }; + +#define TIME_MS 100 + +static int +load_loop_fn (void *func_param) +{ +uint64_t time_diff =0 , begin; +uint64_t hz =rte_get_timer_hz (); +uint64_t lcount =0 ; +const int use_lock = *(int *)func_param; +const unsigned int lcore =rte_lcore_id (); + +/* wait synchro for slaves */ +if (lcore !=rte_get_master_lcore ()) +while (rte_atomic32_read (&synchro) ==0 )
app/test/test_ticketlock.c line 124
Quoted 10 lines
app/test/test_ticketlock.c new file +10
-0+ ; + + begin =rte_get_timer_cycles (); +while (time_diff < hz *TIME_MS /1000 ) { +if (use_lock) +rte_ticketlock_lock (&lk); + lcount++; +if (use_lock) +rte_ticketlock_unlock (&lk); +/* delay to make lock duty cycle slighlty realistic */
Probably better to do here the same as in test spinlock patches:
- remove delay_us()
- move
time_diff = rte_get_timer_cycles() - begin;
out of the loop and report aggregate cycles.
Quoted 189 lines
app/test/test_ticketlock.c new file +187
-1+rte_delay_us (1 ); + time_diff =rte_get_timer_cycles () - begin; + } + lock_count[lcore] = lcount; +return 0 ; +} + +static int +test_ticketlock_perf (void ) +{ +unsigned int i; +uint64_t total =0 ; +int lock =0 ; +const unsigned int lcore =rte_lcore_id (); + +printf ("\nTest with no lock on single core...\n" ); +load_loop_fn (&lock); +printf ("Core [%u] count = %" PRIu64"\n" , lcore, lock_count[lcore]); +memset (lock_count,0 ,sizeof (lock_count)); + +printf ("\nTest with lock on single core...\n" ); + lock =1 ; +load_loop_fn (&lock); +printf ("Core [%u] count = %" PRIu64"\n" , lcore, lock_count[lcore]); +memset (lock_count,0 ,sizeof (lock_count)); + +printf ("\nTest with lock on %u cores...\n" ,rte_lcore_count ()); + +/* Clear synchro and start slaves */ +rte_atomic32_set (&synchro,0 ); +rte_eal_mp_remote_launch (load_loop_fn, &lock,SKIP_MASTER ); + +/* start synchro and launch test on master */ +rte_atomic32_set (&synchro,1 ); +load_loop_fn (&lock); + +rte_eal_mp_wait_lcore (); + +RTE_LCORE_FOREACH (i) { +printf ("Core [%u] count = %" PRIu64"\n" , i, lock_count[i]); + total += lock_count[i]; + } + +printf ("Total count = %" PRIu64"\n" , total); + +return 0 ; +} + +/* +* Use rte_ticketlock_trylock() to trylock a ticketlock object, +* If it could not lock the object successfully, it would +* return immediately and the variable of "count" would be +* increased by one per times. the value of "count" could be +* checked as the result later. +*/ +static int +test_ticketlock_try (__attribute__((unused))void *arg) +{ +if (rte_ticketlock_trylock (&tl_try) ==0 ) { +rte_ticketlock_lock (&tl); + count++; +rte_ticketlock_unlock (&tl); + } + +return 0 ; +} + + +/* +* Test rte_eal_get_lcore_state() in addition to ticketlocks +* as we have "waiting" then "running" lcores. +*/ +static int +test_ticketlock (void ) +{ +int ret =0 ; +int i; + +/* slave cores should be waiting: print it */ +RTE_LCORE_FOREACH_SLAVE (i) { +printf ("lcore %d state: %d\n" , i, + (int )rte_eal_get_lcore_state (i)); + } + +rte_ticketlock_init (&tl); +rte_ticketlock_init (&tl_try); +rte_ticketlock_recursive_init (&tlr); +RTE_LCORE_FOREACH_SLAVE (i) { +rte_ticketlock_init (&tl_tab[i]); + } + +rte_ticketlock_lock (&tl); + +RTE_LCORE_FOREACH_SLAVE (i) { +rte_ticketlock_lock (&tl_tab[i]); +rte_eal_remote_launch (test_ticketlock_per_core,NULL , i); + } + +/* slave cores should be busy: print it */ +RTE_LCORE_FOREACH_SLAVE (i) { +printf ("lcore %d state: %d\n" , i, + (int )rte_eal_get_lcore_state (i)); + } +rte_ticketlock_unlock (&tl); + +RTE_LCORE_FOREACH_SLAVE (i) { +rte_ticketlock_unlock (&tl_tab[i]); +rte_delay_ms (10 ); + } + +rte_eal_mp_wait_lcore (); + +rte_ticketlock_recursive_lock (&tlr); + +/* +* Try to acquire a lock that we already own +*/ +if (!rte_ticketlock_recursive_trylock (&tlr)) { +printf ("rte_ticketlock_recursive_trylock failed on a lock that " +"we already own\n" ); + ret =-1 ; + }else +rte_ticketlock_recursive_unlock (&tlr); + +RTE_LCORE_FOREACH_SLAVE (i) { +rte_eal_remote_launch (test_ticketlock_recursive_per_core, +NULL , i); + } +rte_ticketlock_recursive_unlock (&tlr); +rte_eal_mp_wait_lcore (); + +/* +* Test if it could return immediately from try-locking a locked object. +* Here it will lock the ticketlock object first, then launch all the +* slave lcores to trylock the same ticketlock object. +* All the slave lcores should give up try-locking a locked object and +* return immediately, and then increase the "count" initialized with +* zero by one per times. +* We can check if the "count" is finally equal to the number of all +* slave lcores to see if the behavior of try-locking a locked +* ticketlock object is correct. +*/ +if (rte_ticketlock_trylock (&tl_try) ==0 ) +return -1 ; + + count =0 ; +RTE_LCORE_FOREACH_SLAVE (i) { +rte_eal_remote_launch (test_ticketlock_try,NULL , i); + } +rte_eal_mp_wait_lcore (); +rte_ticketlock_unlock (&tl_try); +if (rte_ticketlock_is_locked (&tl)) { +printf ("ticketlock is locked but it should not be\n" ); +return -1 ; + } +rte_ticketlock_lock (&tl); +if (count != (rte_lcore_count () -1 )) + ret =-1 ; + +rte_ticketlock_unlock (&tl); + +/* +* Test if it can trylock recursively. +* Use rte_ticketlock_recursive_trylock() to check if it can lock +* a ticketlock object recursively. Here it will try to lock a +* ticketlock object twice. +*/ +if (rte_ticketlock_recursive_trylock (&tlr) ==0 ) { +printf ("It failed to do the first ticketlock_recursive_trylock " +"but it should able to do\n" ); +return -1 ; + } +if (rte_ticketlock_recursive_trylock (&tlr) ==0 ) { +printf ("It failed to do the second ticketlock_recursive_trylock " +"but it should able to do\n" ); +return -1 ; + } +rte_ticketlock_recursive_unlock (&tlr); +rte_ticketlock_recursive_unlock (&tlr); + +if (test_ticketlock_perf () <0 ) +return -1 ; + +return ret; +} + +REGISTER_TEST_COMMAND (ticketlock_autotest, test_ticketlock); --2.7.4
Re: [PATCH v7 3/3] test/ticketlock: add ticket lock test case
Hi Konstantin,
Quoted 173 lines
-----Original Message-----
From: Ananyev, Konstantin <[email protected]>
Sent: Friday, March 22, 2019 7:39 PM
To: Joyce Kong (Arm Technology China) <[email protected]>; [email protected]
Cc: nd <[email protected]>; [email protected]; [email protected]; [email protected]; Honnappa Nagarahalli <[email protected]>; Gavin Hu (Arm Technology China) <[email protected]>
Subject: RE: [PATCH v7 3/3] test/ticketlock: add ticket lock test caseQuoted 94 lines
Add test cases for ticket lock, recursive ticket lock, and ticket lock performance.
Signed-off-by Joyce Kong <[email protected]>Reviewed-by Gavin Hu <[email protected]>Reviewed-by Ruifeng Wang <[email protected]>--- MAINTAINERS | 1 + app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 311 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 320 insertions(+) create mode 100644 app/test/test_ticketlock.cMAINTAINERS +1
-0@@ -213,6 +213,7 @@ F: app/test/test_bitmap.c Ticketlock M: Joyce Kong <[email protected]> F: lib/librte_eal/common/include/generic/rte_ticketlock.h +F: app/test/test_ticketlock.c ARM v7 M: Jan Viktorin <[email protected]> diff --gita/app/test/Makefile b/app/test/Makefile index 89949c2..d6aa28b 100644
app/test/Makefile +0
-0@@ -65,6 +65,7 @@ SRCS-y += test_barrier.c SRCS-y += test_malloc.cSRCS-y +=
test_cycles.cSRCS-y +=test_spinlock.capp/test/Makefile +1
-0+SRCS-y += test_ticketlock.c SRCS-y += test_memory.c SRCS-y += test_memzone.c SRCS-y += test_bitmap.capp/test/autotest_data.py +6
-0@@ -171,6 +171,12 @@ "Report": None, }, { + "Name": "Ticketlock autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + } + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,app/test/meson.build index renamed from app/test/meson.build +0
-005e5dde..ddb4d09 100644
app/test/meson.build +1
-0@@ -107,6 +107,7 @@ test_sources = files('commands.c', 'test_timer.c', 'test_timer_perf.c', 'test_timer_racecond.c', + 'test_ticketlock.c', 'test_version.c', 'virtual_pmd.c' )app/test/test_ticketlock.c new file +24
-0@@ -0,0 +1,311 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2018-2019 Arm Limited */ + +#include <inttypes.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <sys/queue.h> +#include <unistd.h> + +#include <rte_atomic.h> +#include <rte_common.h> +#include <rte_cycles.h> +#include <rte_eal.h> +#include <rte_launch.h> +#include <rte_lcore.h> +#include <rte_memory.h> +#include <rte_per_lcore.h> +#include <rte_ticketlock.h> + +#include "test.h" + +/* +* Ticketlock test app/test/test_ticketlock.c line 34
Quoted 10 lines
app/test/test_ticketlock.c new file +10
-0+* ============= +* +* - There is a global ticketlock and a table of ticketlocks (one per lcore). +* +* - The test function takes all of these locks and launches the +* ``test_ticketlock_per_core()`` function on each core (except the master). +* +* - The function takes the global lock, display something, then releases +* the global lock. +* - The function takes the per-lcore lock, display something, then releases
Quoted 19 lines
app/test/test_ticketlock.c new file +19
-0+* the per-core lock. +* +* - The main function unlocks the per-lcore locks sequentially and +* waits between each lock. This triggers the display of a message +* for each core, in the correct order. The autotest script checks that +* this order is correct. +* +* - A load test is carried out, with all cores attempting to lock a single lock +* multiple times +*/ + +static rte_ticketlock_t tl, tl_try; +static rte_ticketlock_t tl_tab[RTE_MAX_LCORE ];static +rte_ticketlock_recursive_t tlr;static unsigned int count; + +static rte_atomic32_t synchro; + +static int +test_ticketlock_per_core (__attribute__((unused))void *arg) {app/test/test_ticketlock.c line 66
Quoted 10 lines
app/test/test_ticketlock.c new file +10
-0+rte_ticketlock_lock (&tl); +printf ("Global lock taken on core %u\n" ,rte_lcore_id ()); +rte_ticketlock_unlock (&tl); + +rte_ticketlock_lock (&tl_tab[rte_lcore_id ()]); +printf ("Hello from core %u !\n" ,rte_lcore_id ()); +rte_ticketlock_unlock (&tl_tab[rte_lcore_id ()]); + +return 0 ; +}I think that's probably no enough for functional testing.
Need something extra to ensure that it provides correct locking in MT env. Probably extend the perf test below to do both?
Something like that:static
uint64_tlcount__rte_cache_aligned; staticuint64_tlcore_count[RTE_MAX_LCORE]__rte_cache_aligned;...
load_loop_fn(...){ ... rte_ticketlock_lock(&lk); lcount++; rte_ticketlock_unlock(&lk); lcore_count[current_lcore]++; }Then in
test_ticketlock_perf()make sure that sum of al lcore_count[] values equals to lcount value:
tcount = 0;
for (i = 0; i !=RTE_DIM(lcore_count); i++)
tcount += lcore_count[i];
Quoted 6 lines
if (tcount != lcount)
<error>Same thought for trylock.
Konstantin
Got your opinion and will do this in next version.
Quoted 56 lines
Quoted 56 lines
app/test/test_ticketlock.c new file +56
-0+ +static int +test_ticketlock_recursive_per_core (__attribute__((unused))void *arg) +{ +unsigned int id =rte_lcore_id (); + +rte_ticketlock_recursive_lock (&tlr); +printf ("Global recursive lock taken on core %u - count = %d\n" , + id, tlr.count); +rte_ticketlock_recursive_lock (&tlr); +printf ("Global recursive lock taken on core %u - count = %d\n" , + id, tlr.count); +rte_ticketlock_recursive_lock (&tlr); +printf ("Global recursive lock taken on core %u - count = %d\n" , + id, tlr.count); + +printf ("Hello from within recursive locks from core %u !\n" , id); + +rte_ticketlock_recursive_unlock (&tlr); +printf ("Global recursive lock released on core %u - count = %d\n" , + id, tlr.count); +rte_ticketlock_recursive_unlock (&tlr); +printf ("Global recursive lock released on core %u - count = %d\n" , + id, tlr.count); +rte_ticketlock_recursive_unlock (&tlr); +printf ("Global recursive lock released on core %u - count = %d\n" , + id, tlr.count); + +return 0 ; +} + +static rte_ticketlock_t lk =RTE_TICKETLOCK_INITIALIZER ;static +uint64_t lock_count[RTE_MAX_LCORE ] = {0 }; + +#define TIME_MS 100 + +static int +load_loop_fn (void *func_param) +{ +uint64_t time_diff =0 , begin; +uint64_t hz =rte_get_timer_hz (); +uint64_t lcount =0 ; +const int use_lock = *(int *)func_param; +const unsigned int lcore =rte_lcore_id (); + +/* wait synchro for slaves */ +if (lcore !=rte_get_master_lcore ()) +while (rte_atomic32_read (&synchro) ==0 ) + ; + + begin =rte_get_timer_cycles (); +while (time_diff < hz *TIME_MS /1000 ) { +if (use_lock) +rte_ticketlock_lock (&lk); + lcount++; +if (use_lock)
Quoted 9 lines
app/test/test_ticketlock.c line 124
Quoted 2 lines
app/test/test_ticketlock.c new file +2
-0+rte_ticketlock_unlock (&lk); +/* delay to make lock duty cycle slighlty realistic */ Probably better to do here the same as in test spinlock patches:
- removedelay_us()
- move
time_diff=rte_get_timer_cycles()- begin; out of the loop and report
aggregate cycles.
Will do the same as test spinlock patches in next version.
Quoted 189 lines
Quoted 163 lines
app/test/test_ticketlock.c new file +163
-0+rte_delay_us (1 ); + time_diff =rte_get_timer_cycles () - begin; + } + lock_count[lcore] = lcount; +return 0 ; +} + +static int +test_ticketlock_perf (void ) +{ +unsigned int i; +uint64_t total =0 ; +int lock =0 ; +const unsigned int lcore =rte_lcore_id (); + +printf ("\nTest with no lock on single core...\n" ); +load_loop_fn (&lock); +printf ("Core [%u] count = %" PRIu64"\n" , lcore, lock_count[lcore]); +memset (lock_count,0 ,sizeof (lock_count)); + +printf ("\nTest with lock on single core...\n" ); + lock =1 ; +load_loop_fn (&lock); +printf ("Core [%u] count = %" PRIu64"\n" , lcore, lock_count[lcore]); +memset (lock_count,0 ,sizeof (lock_count)); + +printf ("\nTest with lock on %u cores...\n" ,rte_lcore_count ()); + +/* Clear synchro and start slaves */ +rte_atomic32_set (&synchro,0 ); +rte_eal_mp_remote_launch (load_loop_fn, &lock,SKIP_MASTER ); + +/* start synchro and launch test on master */ +rte_atomic32_set (&synchro,1 ); +load_loop_fn (&lock); + +rte_eal_mp_wait_lcore (); + +RTE_LCORE_FOREACH (i) { +printf ("Core [%u] count = %" PRIu64"\n" , i, lock_count[i]); + total += lock_count[i]; + } + +printf ("Total count = %" PRIu64"\n" , total); + +return 0 ; +} + +/* +* Use rte_ticketlock_trylock() to trylock a ticketlock object, +* If it could not lock the object successfully, it would +* return immediately and the variable of "count" would be +* increased by one per times. the value of "count" could be +* checked as the result later. +*/ +static int +test_ticketlock_try (__attribute__((unused))void *arg) { +if (rte_ticketlock_trylock (&tl_try) ==0 ) { +rte_ticketlock_lock (&tl); + count++; +rte_ticketlock_unlock (&tl); + } + +return 0 ; +} + + +/* +* Test rte_eal_get_lcore_state() in addition to ticketlocks +* as we have "waiting" then "running" lcores. +*/ +static int +test_ticketlock (void ) +{ +int ret =0 ; +int i; + +/* slave cores should be waiting: print it */ +RTE_LCORE_FOREACH_SLAVE (i) { +printf ("lcore %d state: %d\n" , i, + (int )rte_eal_get_lcore_state (i)); + } + +rte_ticketlock_init (&tl); +rte_ticketlock_init (&tl_try); +rte_ticketlock_recursive_init (&tlr); +RTE_LCORE_FOREACH_SLAVE (i) { +rte_ticketlock_init (&tl_tab[i]); + } + +rte_ticketlock_lock (&tl); + +RTE_LCORE_FOREACH_SLAVE (i) { +rte_ticketlock_lock (&tl_tab[i]); +rte_eal_remote_launch (test_ticketlock_per_core,NULL , i); + } + +/* slave cores should be busy: print it */ +RTE_LCORE_FOREACH_SLAVE (i) { +printf ("lcore %d state: %d\n" , i, + (int )rte_eal_get_lcore_state (i)); + } +rte_ticketlock_unlock (&tl); + +RTE_LCORE_FOREACH_SLAVE (i) { +rte_ticketlock_unlock (&tl_tab[i]); +rte_delay_ms (10 ); + } + +rte_eal_mp_wait_lcore (); + +rte_ticketlock_recursive_lock (&tlr); + +/* +* Try to acquire a lock that we already own +*/ +if (!rte_ticketlock_recursive_trylock (&tlr)) { +printf ("rte_ticketlock_recursive_trylock failed on a lock that " +"we already own\n" ); + ret =-1 ; + }else +rte_ticketlock_recursive_unlock (&tlr); + +RTE_LCORE_FOREACH_SLAVE (i) { +rte_eal_remote_launch (test_ticketlock_recursive_per_core, +NULL , i); + } +rte_ticketlock_recursive_unlock (&tlr); +rte_eal_mp_wait_lcore (); + +/* +* Test if it could return immediately from try-locking a locked object. +* Here it will lock the ticketlock object first, then launch all the +* slave lcores to trylock the same ticketlock object. +* All the slave lcores should give up try-locking a locked object and +* return immediately, and then increase the "count" initialized with +* zero by one per times. +* We can check if the "count" is finally equal to the number of all +* slave lcores to see if the behavior of try-locking a locked +* ticketlock object is correct. +*/ +if (rte_ticketlock_trylock (&tl_try) ==0 ) +return -1 ; + + count =0 ; +RTE_LCORE_FOREACH_SLAVE (i) { +rte_eal_remote_launch (test_ticketlock_try,NULL , i); + } +rte_eal_mp_wait_lcore (); +rte_ticketlock_unlock (&tl_try); +if (rte_ticketlock_is_locked (&tl)) { +printf ("ticketlock is locked but it should not be\n" ); +return -1 ; + } +rte_ticketlock_lock (&tl); +if (count != (rte_lcore_count () -1 )) + ret =-1 ; + +rte_ticketlock_unlock (&tl); + +/* +* Test if it can trylock recursively. +* Use rte_ticketlock_recursive_trylock() to check if it can lock app/test/test_ticketlock.c
Quoted 10 lines
app/test/test_ticketlock.c new file +10
-0+* a ticketlock object recursively. Here it will try to lock a +* ticketlock object twice. +*/ +if (rte_ticketlock_recursive_trylock (&tlr) ==0 ) { +printf ("It failed to do the first ticketlock_recursive_trylock " +"but it should able to do\n" ); +return -1 ; + } +if (rte_ticketlock_recursive_trylock (&tlr) ==0 ) { + printf("It failed to do the second ticketlock_recursive_trylock"
Quoted 15 lines
app/test/test_ticketlock.c new file +13
-1+ "but it should able to do\n"); +return -1 ; + } +rte_ticketlock_recursive_unlock (&tlr); +rte_ticketlock_recursive_unlock (&tlr); + +if (test_ticketlock_perf () <0 ) +return -1 ; + +return ret; +} + +REGISTER_TEST_COMMAND (ticketlock_autotest, test_ticketlock); --2.7.4
PATCH v7 1/3 eal/ticketlock: ticket based to improve fairness
In reply to Joyce KongThe spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time.
This patch introduces ticketlock which gives each waiting thread a ticket and they can take the lock one by one. First come, first serviced. This avoids starvation for too long time and is more predictable.
--- MAINTAINERS | 4 + doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/generic/rte_ticketlock.h | 215 +++++++++++++++++++++ lib/librte_eal/common/meson.build | 1 + 5 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
MAINTAINERS +4 -0
@@ -210,6 +210,10 @@ M: Cristian Dumitrescu <[email protected]> F: lib/librte_eal/common/include/rte_bitmap.h F: app/test/test_bitmap.c +Ticketlock +M: Joyce Kong <[email protected]> +F: lib/librte_eal/common/include/generic/rte_ticketlock.h + ARM v7 M: Jan Viktorin <[email protected]> M: Gavin Hu <[email protected]>
doc/api/doxy-api-index.md +1 -0
@@ -65,6 +65,7 @@ The public API headers are grouped by topics: [atomic] (@ref rte_atomic.h), [rwlock] (@ref rte_rwlock.h), [spinlock] (@ref rte_spinlock.h) + [ticketlock] (@ref rte_ticketlock.h) - **CPU arch**: [branch prediction] (@ref rte_branch_prediction.h),
lib/librte_eal/common/Makefile +1 -1
@@ -20,7 +20,7 @@ INC += rte_bitmap.h rte_vfio.h rte_hypervisor.h rte_test.h INC += rte_reciprocal.h rte_fbarray.h rte_uuid.h GENERIC_INC := rte_atomic.h rte_byteorder.h rte_cycles.h rte_prefetch.h -GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h +GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h rte_ticketlock.h GENERIC_INC += rte_vect.h rte_pause.h rte_io.h # defined in mk/arch/$(RTE_ARCH)/rte.vars.mk
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +215 -0
215 lines not on this page · raw message
lib/librte_eal/common/meson.build +1 -0
@@ -99,6 +99,7 @@ generic_headers = files( 'include/generic/rte_prefetch.h', 'include/generic/rte_rwlock.h', 'include/generic/rte_spinlock.h', + 'include/generic/rte_ticketlock.h', 'include/generic/rte_vect.h') install_headers(generic_headers, subdir: 'generic')
Signature · 1 line
2.7.4
Re: [PATCH v7 1/3] eal/ticketlock: ticket based to improve fairness
Hi Joyce,
Quoted 155 lines
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time.
This patch introduces ticketlock which gives each waiting thread a ticket and they can take the lock one by one. First come, first serviced. This avoids starvation for too long time and is more predictable.
Suggested-by Jerin Jacob <[email protected]>Signed-off-by Joyce Kong <[email protected]>Reviewed-by Gavin Hu <[email protected]>Reviewed-by Ola Liljedahl <[email protected]>Reviewed-by Honnappa Nagarahalli <[email protected]>--- MAINTAINERS | 4 + doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/generic/rte_ticketlock.h | 215 +++++++++++++++++++++ lib/librte_eal/common/meson.build | 1 + 5 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.hMAINTAINERS +4
-0@@ -210,6 +210,10 @@ M: Cristian Dumitrescu <[email protected]> F: lib/librte_eal/common/include/rte_bitmap.h F: app/test/test_bitmap.c +Ticketlock +M: Joyce Kong <[email protected]> +F: lib/librte_eal/common/include/generic/rte_ticketlock.h + ARM v7 M: Jan Viktorin <[email protected]> M: Gavin Hu <[email protected]>doc/api/doxy-api-index.md +1
-0@@ -65,6 +65,7 @@ The public API headers are grouped by topics: [atomic] (@ref rte_atomic.h), [rwlock] (@ref rte_rwlock.h), [spinlock] (@ref rte_spinlock.h) + [ticketlock] (@ref rte_ticketlock.h) - **CPU arch**: [branch prediction] (@ref rte_branch_prediction.h),lib/librte_eal/common/Makefile +1
-1@@ -20,7 +20,7 @@ INC += rte_bitmap.h rte_vfio.h rte_hypervisor.h rte_test.h INC += rte_reciprocal.h rte_fbarray.h rte_uuid.h GENERIC_INC := rte_atomic.h rte_byteorder.h rte_cycles.h rte_prefetch.h -GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h +GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h rte_ticketlock.h GENERIC_INC += rte_vect.h rte_pause.h rte_io.h # defined in mk/arch/$(RTE_ARCH)/rte.vars.mklib/librte_eal/common/include/generic/rte_ticketlock.h new file +87
-0@@ -0,0 +1,215 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_H_ +#define _RTE_TICKETLOCK_H_ + +/** + * @file + * + * RTE ticket locks + * + * This file defines an API for ticket locks, which give each waiting + * thread a ticket and take the lock one by one, first come, first + * serviced. + * + * All locks must be initialised before use, and only initialised once. + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <rte_common.h> +#include <rte_lcore.h> +#include <rte_pause.h> + +/** + * The rte_ticketlock_t type. + */ +typedef union { + uint32_t tickets; + struct { + uint16_t current; + uint16_t next; + } s; +} rte_ticketlock_t; + +/** + * A static ticketlock initializer. + */ +#define RTE_TICKETLOCK_INITIALIZER { 0 } + +/** + * Initialize the ticketlock to an unlocked state. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_init(rte_ticketlock_t *tl) +{ + __atomic_store_n(&tl->tickets, 0, __ATOMIC_RELAXED); +} + +/** + * Take the ticketlock. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_lock(rte_ticketlock_t *tl) +{ + uint16_t me = __atomic_fetch_add(&tl->s.next, 1, __ATOMIC_RELAXED); + while (__atomic_load_n(&tl->s.current, __ATOMIC_ACQUIRE) != me) + rte_pause(); +} + +/** + * Release the ticketlock. + * + * @param tl + * A pointer to the ticketlock. + */ +static inline __rte_experimental void +rte_ticketlock_unlock(rte_ticketlock_t *tl) +{ + uint16_t i = __atomic_load_n(&tl->s.current, __ATOMIC_RELAXED); + __atomic_store_n(&tl->s.current, i + 1, __ATOMIC_RELEASE); +} + +/** + * Try to take the lock. + * + * @param tl
lib/librte_eal/common/include/generic/rte_ticketlock.h line 97
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+* A pointer to the ticketlock. +* @return +* 1 if the lock is successfully taken; 0 otherwise. +*/ +static inline __rte_experimental int +rte_ticketlock_trylock (rte_ticketlock_t *tl) +{ +rte_ticketlock_t old, new; + old.tickets =__atomic_load_n (&tl->tickets, __ATOMIC_RELAXED); + new.tickets =__atomic_load_n (&tl->tickets, __ATOMIC_RELAXED);
As a nit: from my point, there is no need for second load, we can just do:
new.tickets = old.tickets;
Apart from that:
Quoted 10 lines
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +10
-0+ new.s.next++; +if (old.s.next == old.s.current) { +if (__atomic_compare_exchange_n (&tl->tickets, &old.tickets, + new.tickets,0 , __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) +return 1 ; + } + +return 0 ; +} +
v8 cover ticketlock: implement ticketlock and add test case
In reply to Joyce Kongv8:
1. Enhance functional test for ticketlock by adding extra verification to ensure correct locking in MT env.
2. Amortize the cost of getting time in test case.
(Suggested by Konstantin Ananyev)
v7:
1. Modify trylock to compare both current and next fields to gurantee the lock being obtained only if tl->current==1(lock is free).
As within the trylock function, suppose next==curr=1, then this thread will experience really long context switch, and next time it continues its execution and tl->next wraps around to 1 again and tl_couurent==0(lock held by another thread),this trylock will return a success, that means two threads holding the lock. (Suggested by Konstantin Ananyev)
2. Let all archs use generic ticketlock implementation.
v6:
Add rte_ticketlock.h in lib/librte_eal/common/include/arch/arm/. Sort header inside ticketlock files by alphabetical order.
v5:
Change the variants inside rte_ticket_lock from unint to uint16_t for binary compatibility with the plain spin lock(suggested by Honnappa Nagarahalli)).
v4:
Change some assignment operation in recursive ticket lock to __atomic.
V3:
1.Update ticketlock intrduction(suggested by Honnappa Nagarahalli).
2.Change the implementation of rte_ticketlock_trylock to CAS(suggested by Honnappa Nagarahalli).
V2:
1.Update commit message(suggested by Jerin Jacob). 2.Add ticketlock test cases(suggested by Jerin Jacob).
V1:
Implement ticket lock to improve lock fairness and prdictability.
As shown on thundex-2 platform:
*** ticketlock_autotest with this patch ***
Core [0] count = 496
Core [1] count = 495
Core [2] count = 498
...
Core [209] count = 488
Core [210] count = 490
Core [211] count = 474Joyce Kong (3):
eal/ticketlock: ticket based to improve fairness eal/ticketlock: enable generic ticketlock on all arch test/ticketlock: add ticket lock test case
MAINTAINERS | 5 + app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 319 +++++++++++++++++++++ doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/arch/arm/rte_ticketlock.h | 22 ++ .../common/include/arch/ppc_64/rte_ticketlock.h | 18 ++ .../common/include/arch/x86/rte_ticketlock.h | 18 ++ .../common/include/generic/rte_ticketlock.h | 215 ++++++++++++++ lib/librte_eal/common/meson.build | 1 + 12 files changed, 608 insertions(+), 1 deletion(-) create mode 100644 app/test/test_ticketlock.c create mode 100644 lib/librte_eal/common/include/arch/arm/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/ppc_64/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/x86/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
Signature · 1 line
2.7.4
PATCH v8 1/3 eal/ticketlock: ticket based to improve fairness
The spinlock implementation is unfair, some threads may take locks aggressively while leaving the other threads starving for long time.
This patch introduces ticketlock which gives each waiting thread a ticket and they can take the lock one by one. First come, first serviced. This avoids starvation for too long time and is more predictable.
--- MAINTAINERS | 4 + doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/generic/rte_ticketlock.h | 215 +++++++++++++++++++++ lib/librte_eal/common/meson.build | 1 + 5 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
MAINTAINERS +4 -0
@@ -210,6 +210,10 @@ M: Cristian Dumitrescu <[email protected]> F: lib/librte_eal/common/include/rte_bitmap.h F: app/test/test_bitmap.c +Ticketlock +M: Joyce Kong <[email protected]> +F: lib/librte_eal/common/include/generic/rte_ticketlock.h + ARM v7 M: Jan Viktorin <[email protected]> M: Gavin Hu <[email protected]>
doc/api/doxy-api-index.md +1 -0
@@ -65,6 +65,7 @@ The public API headers are grouped by topics: [atomic] (@ref rte_atomic.h), [rwlock] (@ref rte_rwlock.h), [spinlock] (@ref rte_spinlock.h) + [ticketlock] (@ref rte_ticketlock.h) - **CPU arch**: [branch prediction] (@ref rte_branch_prediction.h),
lib/librte_eal/common/Makefile +1 -1
@@ -20,7 +20,7 @@ INC += rte_bitmap.h rte_vfio.h rte_hypervisor.h rte_test.h INC += rte_reciprocal.h rte_fbarray.h rte_uuid.h GENERIC_INC := rte_atomic.h rte_byteorder.h rte_cycles.h rte_prefetch.h -GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h +GENERIC_INC += rte_spinlock.h rte_memcpy.h rte_cpuflags.h rte_rwlock.h rte_ticketlock.h GENERIC_INC += rte_vect.h rte_pause.h rte_io.h # defined in mk/arch/$(RTE_ARCH)/rte.vars.mk
lib/librte_eal/common/include/generic/rte_ticketlock.h new file +215 -0
215 lines not on this page · raw message
lib/librte_eal/common/meson.build +1 -0
@@ -99,6 +99,7 @@ generic_headers = files( 'include/generic/rte_prefetch.h', 'include/generic/rte_rwlock.h', 'include/generic/rte_spinlock.h', + 'include/generic/rte_ticketlock.h', 'include/generic/rte_vect.h') install_headers(generic_headers, subdir: 'generic')
Signature · 1 line
2.7.4
PATCH v8 2/3 eal/ticketlock: enable generic ticketlock on all arch
In reply to Joyce KongLet all architectures use generic ticketlock implementation.
--- .../common/include/arch/arm/rte_ticketlock.h | 22 ++++++++++++++++++++++ .../common/include/arch/ppc_64/rte_ticketlock.h | 18 ++++++++++++++++++ .../common/include/arch/x86/rte_ticketlock.h | 18 ++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 lib/librte_eal/common/include/arch/arm/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/ppc_64/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/x86/rte_ticketlock.h
lib/librte_eal/common/include/arch/arm/rte_ticketlock.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_ARM_H_ +#define _RTE_TICKETLOCK_ARM_H_ + +#ifndef RTE_FORCE_INTRINSICS +# error Platform must be built with CONFIG_RTE_FORCE_INTRINSICS +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#include "generic/rte_ticketlock.h" + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_TICKETLOCK_ARM_H_ */
lib/librte_eal/common/include/arch/ppc_64/rte_ticketlock.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_PPC_64_H_ +#define _RTE_TICKETLOCK_PPC_64_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "generic/rte_ticketlock.h" + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_TICKETLOCK_PPC_64_H_ */
lib/librte_eal/common/include/arch/x86/rte_ticketlock.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: BSD-3-Clause +* Copyright(c) 2019 Arm Limited +*/ + +#ifndef _RTE_TICKETLOCK_X86_64_H_ +#define _RTE_TICKETLOCK_X86_64_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "generic/rte_ticketlock.h" + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_TICKETLOCK_X86_64_H_ */
Signature · 1 line
2.7.4
PATCH v8 3/3 test/ticketlock: add ticket lock test case
In reply to Joyce KongAdd test cases for ticket lock, recursive ticket lock, and ticket lock performance.
--- MAINTAINERS | 1 + app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 319 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 328 insertions(+) create mode 100644 app/test/test_ticketlock.c
MAINTAINERS +1 -0
@@ -213,6 +213,7 @@ F: app/test/test_bitmap.c Ticketlock M: Joyce Kong <[email protected]> F: lib/librte_eal/common/include/generic/rte_ticketlock.h +F: app/test/test_ticketlock.c ARM v7 M: Jan Viktorin <[email protected]>
app/test/Makefile +1 -0
@@ -65,6 +65,7 @@ SRCS-y += test_barrier.c SRCS-y += test_malloc.c SRCS-y += test_cycles.c SRCS-y += test_spinlock.c +SRCS-y += test_ticketlock.c SRCS-y += test_memory.c SRCS-y += test_memzone.c SRCS-y += test_bitmap.c
app/test/autotest_data.py +6 -0
@@ -171,6 +171,12 @@ "Report": None, }, { + "Name": "Ticketlock autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + } + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,
app/test/meson.build +1 -0
@@ -107,6 +107,7 @@ test_sources = files('commands.c', 'test_timer.c', 'test_timer_perf.c', 'test_timer_racecond.c', + 'test_ticketlock.c', 'test_version.c', 'virtual_pmd.c' )
app/test/test_ticketlock.c new file +319 -0
319 lines not on this page · raw message
Signature · 1 line
2.7.4
Re: [dpdk-dev] [PATCH v8 3/3] test/ticketlock: add ticket lock test case
Quoted 8 lines · Joyce Kong
app/test/autotest_data.py +0
-0@@ -171,6 +171,12 @@ "Report": None, },
app/test/autotest_data.py line 182
Quoted 10 lines
app/test/autotest_data.py +6
-0{ + "Name": "Ticketlock autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + } + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,
Please, can you fix this ?
We are missing a , and ticketlock_autotest has no implementation in python afaics.
Signature · 1 line
David Marchand
Re: [dpdk-dev] [PATCH v8 3/3] test/ticketlock: add ticket lock test case
08/04/2019 22:18, David Marchand:
Quoted 20 lines · Joyce Kong
Quoted 6 lines
app/test/autotest_data.py +0
-0@@ -171,6 +171,12 @@ "Report": None, },app/test/autotest_data.py line 182
Quoted 10 lines
app/test/autotest_data.py +6
-0{ + "Name": "Ticketlock autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + } + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,Please, can you fix this ?
Quoted 2 lines
We are missing a , and
ticketlock_autotesthas no implementation in python afaics.
Please, send a fix ASAP.
Re: [dpdk-dev] [PATCH v8 3/3] test/ticketlock: add ticket lock test case
Quoted 35 lines
-----Original Message-----
From: Thomas Monjalon <[email protected]>
Sent: Monday, April 15, 2019 4:38 AM
To: Joyce Kong (Arm Technology China) <[email protected]>
Cc: [email protected]; David Marchand <[email protected]>; nd <[email protected]>; Stephen Hemminger <[email protected]>; Jerin Jacob <[email protected]>; Ananyev, Konstantin <[email protected]>; Honnappa Nagarahalli <[email protected]>; Gavin Hu (Arm Technology China) <[email protected]>
Subject: Re: [dpdk-dev] [PATCH v8 3/3] test/ticketlock: add ticket lock test case08/04/2019 22:18, David Marchand:
Quoted 1 line
On Mon, Mar 25, 2019 at 12:12 PM Joyce Kong <[email protected]>
Quoted 19 lines · wrote:
Quoted 6 lines
app/test/autotest_data.py +0
-0@@ -171,6 +171,12 @@ "Report": None, },app/test/autotest_data.py line 182
Quoted 10 lines
app/test/autotest_data.py +6
-0{ + "Name": "Ticketlock autotest", + "Command": "ticketlock_autotest", + "Func": ticketlock_autotest, + "Report": None, + } + { "Name": "Byte order autotest", "Command": "byteorder_autotest", "Func": default_autotest,Please, can you fix this ?
Quoted 4 lines
Quoted 2 lines
We are missing a , and
ticketlock_autotesthas no implementation in python afaics.Please, send a fix ASAP.
Fix in the patch test/ticketlock: implement ticketlock autotest .
Re: [PATCH v8 0/3] ticketlock: implement ticketlock and add test case
In reply to Joyce KongQuoted 73 lines
v8:
1. Enhance functional test for ticketlock by adding extra verification to ensure correct locking in MT env.
2. Amortize the cost of getting time in test case.
(Suggested by Konstantin Ananyev)v7:
1. Modify trylock to compare both current and next fields to gurantee the lock being obtained only if tl->current==1(lock is free).
As within the trylock function, suppose next==curr=1, then this thread will experience really long context switch, and next time it continues its execution andtl->nextwraps around to 1 again and tl_couurent==0(lock held by another thread),this trylock will return a success, that means two threads holding the lock. (Suggested by Konstantin Ananyev)
2. Let all archs use generic ticketlock implementation.v6:
Addrte_ticketlock.hinlib/librte_eal/common/include/arch/arm/. Sort header inside ticketlock files by alphabetical order.v5:
Change the variants insiderte_ticket_lockfrom unint touint16_tfor binary compatibility with the plain spin lock(suggested by Honnappa Nagarahalli)).v4:
Change some assignment operation in recursive ticket lock to__atomic.V3:
1.Update ticketlock intrduction(suggested by Honnappa Nagarahalli).
2.Change the implementation ofrte_ticketlock_trylockto CAS(suggested by Honnappa Nagarahalli).V2:
1.Update commit message(suggested by Jerin Jacob). 2.Add ticketlock test cases(suggested by Jerin Jacob).V1:
Implement ticket lock to improve lock fairness and prdictability. As shown on thundex-2 platform: *** ticketlock_autotest with this patch *** Core [0] count = 496 Core [1] count = 495 Core [2] count = 498 ... Core [209] count = 488 Core [210] count = 490 Core [211] count = 474Joyce Kong (3):
eal/ticketlock: ticket based to improve fairness eal/ticketlock: enable generic ticketlock on all arch test/ticketlock: add ticket lock test caseMAINTAINERS | 5 + app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 319 +++++++++++++++++++++ doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/arch/arm/rte_ticketlock.h | 22 ++ .../common/include/arch/ppc_64/rte_ticketlock.h | 18 ++ .../common/include/arch/x86/rte_ticketlock.h | 18 ++ .../common/include/generic/rte_ticketlock.h | 215 ++++++++++++++ lib/librte_eal/common/meson.build | 1 + 12 files changed, 608 insertions(+), 1 deletion(-) create mode 100644 app/test/test_ticketlock.c create mode 100644 lib/librte_eal/common/include/arch/arm/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/ppc_64/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/x86/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
Quoted 1 line
--
Quoted 1 line
2.7.4
Thomas Monjalon
AppliedRe: [PATCH v8 0/3] ticketlock: implement ticketlock and add test case
Quoted 24 lines
Quoted 15 lines
Joyce Kong (3):
eal/ticketlock: ticket based to improve fairness eal/ticketlock: enable generic ticketlock on all arch test/ticketlock: add ticket lock test caseMAINTAINERS | 5 + app/test/Makefile | 1 + app/test/autotest_data.py | 6 + app/test/meson.build | 1 + app/test/test_ticketlock.c | 319 +++++++++++++++++++++ doc/api/doxy-api-index.md | 1 + lib/librte_eal/common/Makefile | 2 +- .../common/include/arch/arm/rte_ticketlock.h | 22 ++ .../common/include/arch/ppc_64/rte_ticketlock.h | 18 ++ .../common/include/arch/x86/rte_ticketlock.h | 18 ++Quoted 8 lines
.../common/include/generic/rte_ticketlock.h | 215 ++++++++++++++ lib/librte_eal/common/meson.build | 1 + 12 files changed, 608 insertions(+), 1 deletion(-) create mode 100644 app/test/test_ticketlock.c create mode 100644 lib/librte_eal/common/include/arch/arm/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/ppc_64/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/arch/x86/rte_ticketlock.h create mode 100644 lib/librte_eal/common/include/generic/rte_ticketlock.h
Quoted 1 line
Applied, thanks