Re: [Mesa-dev] [PATCH] mesa: Add new fast mtx_t mutex type for basic use cases
On 29/01/15 17:14, Kristian Høgsberg wrote: On Thu, Jan 29, 2015 at 6:36 AM, Emil Velikov emil.l.veli...@gmail.com wrote: On 28/01/15 05:08, Kristian Høgsberg wrote: While modern pthread mutexes are very fast, they still incur a call to an external DSO and overhead of the generality and features of pthread mutexes. Most mutexes in mesa only needs lock/unlock, and the idea here is that we can inline the atomic operation and make the fast case just two intructions. Mutexes are subtle and finicky to implement, so we carefully copy the implementation from Ulrich Dreppers well-written and well-reviewed paper: Futexes Are Tricky https://urldefense.proofpoint.com/v2/url?u=http-3A__www.akkadia.org_drepper_futex.pdfd=AwIGaQc=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEsr=zfmBZnnVGHeYde45pMKNnVyzeaZbdIqVLprmZCM2zzEm=NS0xLkqIj43l--WADuy3EQa3yVe4rItSr1sBgtCZJ28s=jUMBbUUMfsjTAo4ye4aoY9kqeuG10NtNEuSLKRsxPoce= We implement mutex3, which gives us a mutex that has no syscalls on uncontended lock or unlock. Further, the uncontended case boils down to a cmpxchg and an untaken branch and the uncontended unlock is just a locked decr and an untaken branch. We use __builtin_expect() to indicate that contention is unlikely so that gcc will put the contention code out of the main code flow. I don't oppose the idea of a faster mutex. But do you have some performance figures with this patch? (It doesn't need to be a real-life app -- an artificial demo/benchmark would suffice. What I'd like to know is, is the performance improvement significant enough to at least justify the complexity of maintaining a multiple mutex type across our code? Because I never had the impression that mutexes were a bottleneck. Atomic reference counting is probably more of an problem. A fast mutex only supports lock/unlock, can't be recursive or used with condition variables. We keep the pthread mutex implementation around as full_mtx_t for the few places where we use condition variables or recursive locking. For platforms or compilers where futex and atomics aren't available, mtx_t falls back to the pthread mutex. The pthread mutex lock/unlock overhead shows up on benchmarks for CPU bound applications. Most CPU bound cases are helped and some of our internal bind_buffer_object heavy benchmarks gain up to 10%. Hi Kristian, Can I humbly ask that you split this into two patches - one that introduces the new functions/struct and another one that uses them ? This way it'll be easier if/when things go crazy. Also the patch seems to wonder between posix and win32 + typedef full_mtx_t mtx_t; and + typedef mtx_t fast_mtx_t; Looks like a left over from the should I rename XX variables to fast* or just one to full* moment :) Yeah, that's how it progressed :) At first I called it fast_mtx_t and planned on replacing simple uses of mtx_t one by one. Jordan suggested that it'd be easier to make the regular mutex fast and then rename the couple of places that use more feature than we provide. I'm however strongly against having a non-standard mutex using a standard name like `mtx_t`. The point of using C11 names for threading primitives was to enable us to implement Mesa using standard-looking C code. The idea was that at one point we'd only use our C11 threads.h emulation where needed. Please keep in mind that if/when platforms start providing C11 threads.h we might be forced to use them instead of our own, as system/3rd party headers might start depending on them on their ABIs. It is imperative that any non-standard mutexes use names that don't collide with C11 threads names. Jose ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev
Re: [Mesa-dev] [PATCH] mesa: Add new fast mtx_t mutex type for basic use cases
On Thu, Jan 29, 2015 at 6:36 AM, Emil Velikov emil.l.veli...@gmail.com wrote: On 28/01/15 05:08, Kristian Høgsberg wrote: While modern pthread mutexes are very fast, they still incur a call to an external DSO and overhead of the generality and features of pthread mutexes. Most mutexes in mesa only needs lock/unlock, and the idea here is that we can inline the atomic operation and make the fast case just two intructions. Mutexes are subtle and finicky to implement, so we carefully copy the implementation from Ulrich Dreppers well-written and well-reviewed paper: Futexes Are Tricky http://www.akkadia.org/drepper/futex.pdf We implement mutex3, which gives us a mutex that has no syscalls on uncontended lock or unlock. Further, the uncontended case boils down to a cmpxchg and an untaken branch and the uncontended unlock is just a locked decr and an untaken branch. We use __builtin_expect() to indicate that contention is unlikely so that gcc will put the contention code out of the main code flow. A fast mutex only supports lock/unlock, can't be recursive or used with condition variables. We keep the pthread mutex implementation around as full_mtx_t for the few places where we use condition variables or recursive locking. For platforms or compilers where futex and atomics aren't available, mtx_t falls back to the pthread mutex. The pthread mutex lock/unlock overhead shows up on benchmarks for CPU bound applications. Most CPU bound cases are helped and some of our internal bind_buffer_object heavy benchmarks gain up to 10%. Hi Kristian, Can I humbly ask that you split this into two patches - one that introduces the new functions/struct and another one that uses them ? This way it'll be easier if/when things go crazy. Also the patch seems to wonder between posix and win32 + typedef full_mtx_t mtx_t; and + typedef mtx_t fast_mtx_t; Looks like a left over from the should I rename XX variables to fast* or just one to full* moment :) Yeah, that's how it progressed :) At first I called it fast_mtx_t and planned on replacing simple uses of mtx_t one by one. Jordan suggested that it'd be easier to make the regular mutex fast and then rename the couple of places that use more feature than we provide. I just botched the windows side when I did that. However, given that the patch now reimplements mtx_t, there's really no good way to split up introducing the fast mutex and making use of it in multiple patches. I guess we can add full_mtx_t in a separate patch, use it in a second patch and then finally reimplement mtx_t as a fast mutex. How does that sound? Kristian ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev
Re: [Mesa-dev] [PATCH] mesa: Add new fast mtx_t mutex type for basic use cases
On 28/01/15 05:08, Kristian Høgsberg wrote: While modern pthread mutexes are very fast, they still incur a call to an external DSO and overhead of the generality and features of pthread mutexes. Most mutexes in mesa only needs lock/unlock, and the idea here is that we can inline the atomic operation and make the fast case just two intructions. Mutexes are subtle and finicky to implement, so we carefully copy the implementation from Ulrich Dreppers well-written and well-reviewed paper: Futexes Are Tricky http://www.akkadia.org/drepper/futex.pdf We implement mutex3, which gives us a mutex that has no syscalls on uncontended lock or unlock. Further, the uncontended case boils down to a cmpxchg and an untaken branch and the uncontended unlock is just a locked decr and an untaken branch. We use __builtin_expect() to indicate that contention is unlikely so that gcc will put the contention code out of the main code flow. A fast mutex only supports lock/unlock, can't be recursive or used with condition variables. We keep the pthread mutex implementation around as full_mtx_t for the few places where we use condition variables or recursive locking. For platforms or compilers where futex and atomics aren't available, mtx_t falls back to the pthread mutex. The pthread mutex lock/unlock overhead shows up on benchmarks for CPU bound applications. Most CPU bound cases are helped and some of our internal bind_buffer_object heavy benchmarks gain up to 10%. Hi Kristian, Can I humbly ask that you split this into two patches - one that introduces the new functions/struct and another one that uses them ? This way it'll be easier if/when things go crazy. Also the patch seems to wonder between posix and win32 + typedef full_mtx_t mtx_t; and + typedef mtx_t fast_mtx_t; Looks like a left over from the should I rename XX variables to fast* or just one to full* moment :) Thanks Emil ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev
Re: [Mesa-dev] [PATCH] mesa: Add new fast mtx_t mutex type for basic use cases
On 29/01/15 17:14, Kristian Høgsberg wrote: On Thu, Jan 29, 2015 at 6:36 AM, Emil Velikov emil.l.veli...@gmail.com wrote: On 28/01/15 05:08, Kristian Høgsberg wrote: While modern pthread mutexes are very fast, they still incur a call to an external DSO and overhead of the generality and features of pthread mutexes. Most mutexes in mesa only needs lock/unlock, and the idea here is that we can inline the atomic operation and make the fast case just two intructions. Mutexes are subtle and finicky to implement, so we carefully copy the implementation from Ulrich Dreppers well-written and well-reviewed paper: Futexes Are Tricky http://www.akkadia.org/drepper/futex.pdf We implement mutex3, which gives us a mutex that has no syscalls on uncontended lock or unlock. Further, the uncontended case boils down to a cmpxchg and an untaken branch and the uncontended unlock is just a locked decr and an untaken branch. We use __builtin_expect() to indicate that contention is unlikely so that gcc will put the contention code out of the main code flow. A fast mutex only supports lock/unlock, can't be recursive or used with condition variables. We keep the pthread mutex implementation around as full_mtx_t for the few places where we use condition variables or recursive locking. For platforms or compilers where futex and atomics aren't available, mtx_t falls back to the pthread mutex. The pthread mutex lock/unlock overhead shows up on benchmarks for CPU bound applications. Most CPU bound cases are helped and some of our internal bind_buffer_object heavy benchmarks gain up to 10%. Hi Kristian, Can I humbly ask that you split this into two patches - one that introduces the new functions/struct and another one that uses them ? This way it'll be easier if/when things go crazy. Also the patch seems to wonder between posix and win32 + typedef full_mtx_t mtx_t; and + typedef mtx_t fast_mtx_t; Looks like a left over from the should I rename XX variables to fast* or just one to full* moment :) Yeah, that's how it progressed :) At first I called it fast_mtx_t and planned on replacing simple uses of mtx_t one by one. Jordan suggested that it'd be easier to make the regular mutex fast and then rename the couple of places that use more feature than we provide. I just botched the windows side when I did that. However, given that the patch now reimplements mtx_t, there's really no good way to split up introducing the fast mutex and making use of it in multiple patches. I guess we can add full_mtx_t in a separate patch, use it in a second patch and then finally reimplement mtx_t as a fast mutex. How does that sound? That sounds great. Thank you. -Emil ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev
Re: [Mesa-dev] [PATCH] mesa: Add new fast mtx_t mutex type for basic use cases
On Tue, Jan 27, 2015 at 11:24 PM, Jonathan Gray j...@jsg.id.au wrote: Including linux/futex.h under __GNUC__ is going to break the build of Mesa on everything compiled with clang/gcc that isn't Linux. Good point, I'll add a configure check for futex. thanks, Kristian ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev
Re: [Mesa-dev] [PATCH] mesa: Add new fast mtx_t mutex type for basic use cases
Including linux/futex.h under __GNUC__ is going to break the build of Mesa on everything compiled with clang/gcc that isn't Linux. ___ mesa-dev mailing list mesa-dev@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/mesa-dev
[Mesa-dev] [PATCH] mesa: Add new fast mtx_t mutex type for basic use cases
While modern pthread mutexes are very fast, they still incur a call to an external DSO and overhead of the generality and features of pthread mutexes. Most mutexes in mesa only needs lock/unlock, and the idea here is that we can inline the atomic operation and make the fast case just two intructions. Mutexes are subtle and finicky to implement, so we carefully copy the implementation from Ulrich Dreppers well-written and well-reviewed paper: Futexes Are Tricky http://www.akkadia.org/drepper/futex.pdf We implement mutex3, which gives us a mutex that has no syscalls on uncontended lock or unlock. Further, the uncontended case boils down to a cmpxchg and an untaken branch and the uncontended unlock is just a locked decr and an untaken branch. We use __builtin_expect() to indicate that contention is unlikely so that gcc will put the contention code out of the main code flow. A fast mutex only supports lock/unlock, can't be recursive or used with condition variables. We keep the pthread mutex implementation around as full_mtx_t for the few places where we use condition variables or recursive locking. For platforms or compilers where futex and atomics aren't available, mtx_t falls back to the pthread mutex. The pthread mutex lock/unlock overhead shows up on benchmarks for CPU bound applications. Most CPU bound cases are helped and some of our internal bind_buffer_object heavy benchmarks gain up to 10%. Signed-off-by: Kristian Høgsberg k...@bitplanet.net --- include/c11/threads_posix.h | 145 --- include/c11/threads_win32.h | 25 ++ src/gallium/auxiliary/os/os_thread.h | 10 +-- src/mesa/main/mtypes.h | 4 +- src/mesa/main/shared.c | 4 +- src/mesa/main/texobj.c | 4 +- src/mesa/main/texobj.h | 4 +- 7 files changed, 171 insertions(+), 25 deletions(-) diff --git a/include/c11/threads_posix.h b/include/c11/threads_posix.h index f9c165d..3da4742 100644 --- a/include/c11/threads_posix.h +++ b/include/c11/threads_posix.h @@ -59,13 +59,15 @@ Configuration macro: #endif // FIXME: temporary non-standard hack to ease transition -#define _MTX_INITIALIZER_NP PTHREAD_MUTEX_INITIALIZER +#define _FULL_MTX_INITIALIZER_NP PTHREAD_MUTEX_INITIALIZER + +#define _MTX_INITIALIZER_NP { 0 } /* types */ typedef pthread_cond_t cnd_t; typedef pthread_t thrd_t; typedef pthread_key_t tss_t; -typedef pthread_mutex_t mtx_t; +typedef pthread_mutex_t full_mtx_t; typedef pthread_once_t once_flag; @@ -135,7 +137,7 @@ cnd_signal(cnd_t *cond) // 7.25.3.5 static inline int -cnd_timedwait(cnd_t *cond, mtx_t *mtx, const xtime *xt) +cnd_timedwait(cnd_t *cond, full_mtx_t *mtx, const xtime *xt) { struct timespec abs_time; int rt; @@ -148,7 +150,7 @@ cnd_timedwait(cnd_t *cond, mtx_t *mtx, const xtime *xt) // 7.25.3.6 static inline int -cnd_wait(cnd_t *cond, mtx_t *mtx) +cnd_wait(cnd_t *cond, full_mtx_t *mtx) { if (!cond || !mtx) return thrd_error; pthread_cond_wait(cond, mtx); @@ -159,7 +161,7 @@ cnd_wait(cnd_t *cond, mtx_t *mtx) /* 7.25.4 Mutex functions */ // 7.25.4.1 static inline void -mtx_destroy(mtx_t *mtx) +full_mtx_destroy(full_mtx_t *mtx) { assert(mtx); pthread_mutex_destroy(mtx); @@ -167,7 +169,7 @@ mtx_destroy(mtx_t *mtx) // 7.25.4.2 static inline int -mtx_init(mtx_t *mtx, int type) +full_mtx_init(full_mtx_t *mtx, int type) { pthread_mutexattr_t attr; if (!mtx) return thrd_error; @@ -191,7 +193,7 @@ mtx_init(mtx_t *mtx, int type) // 7.25.4.3 static inline int -mtx_lock(mtx_t *mtx) +full_mtx_lock(full_mtx_t *mtx) { if (!mtx) return thrd_error; pthread_mutex_lock(mtx); @@ -199,14 +201,14 @@ mtx_lock(mtx_t *mtx) } static inline int -mtx_trylock(mtx_t *mtx); +full_mtx_trylock(full_mtx_t *mtx); static inline void thrd_yield(void); // 7.25.4.4 static inline int -mtx_timedlock(mtx_t *mtx, const xtime *xt) +full_mtx_timedlock(full_mtx_t *mtx, const xtime *xt) { if (!mtx || !xt) return thrd_error; { @@ -222,7 +224,7 @@ mtx_timedlock(mtx_t *mtx, const xtime *xt) #else time_t expire = time(NULL); expire += xt-sec; -while (mtx_trylock(mtx) != thrd_success) { +while (full_mtx_trylock(mtx) != thrd_success) { time_t now = time(NULL); if (expire now) return thrd_busy; @@ -236,7 +238,7 @@ mtx_timedlock(mtx_t *mtx, const xtime *xt) // 7.25.4.5 static inline int -mtx_trylock(mtx_t *mtx) +full_mtx_trylock(full_mtx_t *mtx) { if (!mtx) return thrd_error; return (pthread_mutex_trylock(mtx) == 0) ? thrd_success : thrd_busy; @@ -244,13 +246,132 @@ mtx_trylock(mtx_t *mtx) // 7.25.4.6 static inline int -mtx_unlock(mtx_t *mtx) +full_mtx_unlock(full_mtx_t *mtx) { if (!mtx) return thrd_error; pthread_mutex_unlock(mtx); return thrd_success;