[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: RFC: Lightweight synchronization mechanism for gnumach
From: |
Agustina Arzille |
Subject: |
Re: RFC: Lightweight synchronization mechanism for gnumach |
Date: |
Sun, 28 Feb 2016 18:52:26 -0300 |
On 02/28/2016 06:32 PM, Samuel Thibault wrote:
Hello,
Agustina Arzille, on Sun 28 Feb 2016 14:22:03 -0300, wrote:
Here's a rough comparison of how locking is implemented in libpthread,
and the futex prototype I posted.
Did you make measurements? It'd be interesting to post them here.
Samuel
Hello, Samuel.
Here's the benchmarking program I used to compare pthread mutexes and
futexes:
=================================
#include <stdio.h>
#include <pthread.h>
#ifdef USE_FUTEX
#include "futex.h"
# define lock_t unsigned int
# define LOCK_INITIALIZER 0
# define lock_acq(Lp) futex_lock ((Lp), 0)
# define lock_rel(Lp) futex_unlock ((Lp), 0)
#else
# define lock_t pthread_mutex_t
# define LOCK_INITIALIZER PTHREAD_MUTEX_INITIALIZER
# define lock_acq(Lp) pthread_mutex_lock (Lp)
# define lock_rel(Lp) pthread_mutex_unlock (Lp)
#endif
static lock_t lock = LOCK_INITIALIZER;
static volatile int value = 0;
#define LIMIT 1000000
void* thrfct (void *argp)
{
int i, off = argp ? 1 : -1;
for (i = 0; i < LIMIT; ++i)
{
lock_acq (&lock);
value += off;
lock_rel (&lock);
}
return (0);
}
#define NTHREADS 5
int main (void)
{
pthread_t thrs[NTHREADS];
int i;
for (i = 0; i < NTHREADS; ++i)
pthread_create (&thrs[i], NULL, thrfct, (void *)(i & 1));
thrfct ((void *)(NTHREADS & 1));
for (i = 0; i < NTHREADS; ++i)
pthread_join (thrs[i], NULL);
printf ("RESULT: %d\n", value);
return (0);
}
=================================
The file "futex.h" simply contains the wrappers as posted in:
http://lists.gnu.org/archive/html/bug-hurd/2016-02/msg00138.html
To test this, I compiled 2 versions of the program: One that uses
pthread and
the other that uses futexes:
gcc -O2 -DUSE_FUTEX benchmark.c -o futexes -lpthread
gcc -O2 benchmark.c -o pthreads -lpthread
Here are the times I got from running "time ./pthreads"
I'm using gcc 5.2.1 and running Hurd with KVM.
real: 0m1.340s
user: 0
sys : 0
real: 0m3.030s
user: 0
sys : 0
real: 0m0.390s
user: 0
sys : 0
real: 0m0.380s
user: 0
sys : 0
real: 0m3.010s
user: 0
sys : 0
And here are the times for "time ./futexes"
real: 0m0.160s
user: 0
sys : 0
real: 0m0.160s
user: 0
sys : 0
real: 0m0.270s
user: 0
sys : 0
real: 0m0.150s
user: 0
sys : 0
real: 0m0.150s
user: 0
sys : 0
Now, the benchmark I used isn't exactly representative of real-life
programs (At least, I hope it isn't), but I think it serves to illustrate
the performance of each implementation under heavy contention.
Re: RFC: Lightweight synchronization mechanism for gnumach, Richard Braun, 2016/02/28
Re: RFC: Lightweight synchronization mechanism for gnumach, Richard Braun, 2016/02/28
Re: RFC: Lightweight synchronization mechanism for gnumach, Samuel Thibault, 2016/02/28