Re: [LAU] Simple, easy multithreaded circular buffer library for Linux?

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Paul Davis <paul@...>
Cc: <linux-audio-user@...>, Kjetil S. Matheussen <k.s.matheussen@...>
Date: Friday, October 17, 2008 - 12:13 pm

Olivier Guilyardi wrote:

Okay, I wrote such a test. It fails with Jack's ringbuffer (jack1 r3004) but
succeeds with Portaudio's one (r1240).

It's a very simple test. I'm starting two threads, writing incremented numbers
into a ringbuffer, and checking that they follow each other on the reading end.

The failure frequency depends on the chunk size / buffer size ratio. With
relative short runs, I'm getting about 2 failures / million, for both 256 / 512
and 256 / 1024. I'm around 0.5 failure / million with a 256 / 4096 ratio. But
longer runs would be necessary for exact statistics.

There is no such failure with Portaudio's ringbuffer, even if I deactivate its
memory barriers. But I just can't say if this bug is caused by the lack of
memory barrier or not in Jack, because the two implementations are different,
and all of this seems to depend on the CPU architecture, the movements of the
clouds, the color of my coffee, etc.. ;)

I'm running these tests on a Intel Core2 Quad Q6600 CPU, and making sure the two
threads run on a different CPU by looking at the program startup output. I
didn't test on a single-cpu architecture.

The Portaudio code looks more and more robust to me. It's also surprisingly
short. Maybe that the best would be to replace jack's ringbuffer with it? I
think it should be possible to keep the jack_ringbuffer api unchanged.

My test code is below. It expects the buffer size as first argument. Try with
512, 1024, 2048,... With a buffer size of 4096 bytes, I sometimes need to wait a
minute or so for the first failure to appear.

When saved in jack1 svn working dir, I compile it with something like:
gcc -I. -o testrb -Wall testrb.c libjack/ringbuffer.c -lpthread

#include
#include
#include
#include
#include

#define ARRAY_SIZE 64
#define MAX_VALUE 0x10000

jack_ringbuffer_t *rb;

static int
fill_int_array (int *array, int start, int count)
{
int i, j = start;
for (i = 0; i < count; i++)
{
array[i] = j;
j = (j + 1) % MAX_VALUE;
}
return j;
}

static int
cmp_array (int *array1, int *array2, int count)
{
int i;
for (i = 0; i < count; i++)
if (array1[i] != array2[i])
{
printf("%d != %d at offset %d\n", array1[i], array2[i], i);
return 0;
}

return 1;
}

static void *
reader_start (void * arg)
{
int i = 0, a[ARRAY_SIZE], b[ARRAY_SIZE];
unsigned long j = 0, nfailures = 0;
printf("reader started on cpu %d\n", sched_getcpu());
i = fill_int_array (a, i, ARRAY_SIZE);
while (1)
{
if (jack_ringbuffer_read_space (rb) >= ARRAY_SIZE * sizeof (int))
{
if (jack_ringbuffer_read (rb, (char *) b, ARRAY_SIZE * sizeof (int)))
{
if (!cmp_array (a, b, ARRAY_SIZE))
{
nfailures++;
printf("failure in chunk %lu - probability: %lu/%lu = %.3f per
million\n",
j, nfailures, j, (float) nfailures / (j + 1) * 1000000);
i = (b[0] + ARRAY_SIZE) % MAX_VALUE;
}
i = fill_int_array (a, i, ARRAY_SIZE);
j++;
}
}
}

return NULL;
}

static void *
writer_start (void * arg)
{
int i = 0, a[ARRAY_SIZE];
printf("writer started on cpu: %d\n", sched_getcpu());

i = fill_int_array (a, i, ARRAY_SIZE);

while (1)
{
if (jack_ringbuffer_write_space (rb) >= ARRAY_SIZE * sizeof (int))
{
if (jack_ringbuffer_write (rb, (char *) a, ARRAY_SIZE * sizeof (int)))
{
i = fill_int_array (a, i, ARRAY_SIZE);
}
}
}

return NULL;
}

int main(int argc, char *argv[])
{
int size;
printf("starting ringbuffer stress test\n");
sscanf(argv[1], "%d", &size);
printf("buffer size (bytes): %d\n", size);
printf("array size (bytes): %d\n", sizeof(int) * ARRAY_SIZE);
rb = jack_ringbuffer_create(size);
pthread_t reader_thread, writer_thread;
pthread_create (&reader_thread, NULL, reader_start, NULL);
pthread_create (&writer_thread, NULL, writer_start, NULL);
while (1)
sleep(1);
return 0;
}

--
Olivier Guilyardi / Samalyse
_______________________________________________
Linux-audio-user mailing list
Linux-audio-user@lists.linuxaudio.org
http://lists.linuxaudio.org/mailman/listinfo/linux-audio-user

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Re: [LAU] Simple, easy multithreaded circular buffer library..., Kjetil S. Matheussen, (Tue Oct 14, 4:39 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Olivier Guilyardi, (Tue Oct 14, 9:01 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Olivier Guilyardi, (Wed Oct 15, 11:03 am)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Olivier Guilyardi, (Wed Oct 15, 11:49 am)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Olivier Guilyardi, (Fri Oct 17, 12:13 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Olivier Guilyardi, (Fri Oct 17, 5:20 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Joern Nettingsmeier, (Fri Oct 17, 6:02 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Joern Nettingsmeier, (Sat Oct 18, 5:08 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Joern Nettingsmeier, (Fri Oct 17, 7:22 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Joern Nettingsmeier, (Fri Oct 17, 10:32 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Olivier Guilyardi, (Fri Oct 17, 6:37 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Olivier Guilyardi, (Fri Oct 17, 5:45 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Olivier Guilyardi, (Fri Oct 17, 2:39 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Olivier Guilyardi, (Sun Oct 19, 11:03 am)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Olivier Guilyardi, (Fri Oct 17, 5:35 pm)
[LAU] [LAA] gxtuner-1.6 update, hermann, (Sun Dec 11, 8:20 am)
Re: [LAU] [LAD] gxtuner-1.6 update, hermann, (Wed Dec 14, 1:29 pm)
[LAU] gxtuner-1.5 released, hermann, (Fri Dec 9, 11:17 am)
Re: [LAU] [LAD] gxtuner-1.5 released, Jostein Chr. Andersen, (Fri Dec 9, 2:43 pm)
Re: [LAU] [LAD] gxtuner-1.5 released, hermann, (Fri Dec 9, 3:30 pm)
Re: [LAU] [LAD] gxtuner-1.5 released, Jostein Chr. Andersen, (Fri Dec 9, 8:28 pm)
Re: [LAU] [LAD] gxtuner-1.5 released, Gerhard Zintel, (Sat Dec 10, 12:17 pm)
Re: [LAU] [LAD] gxtuner-1.5 released, Jostein Chr. Andersen, (Sat Dec 10, 11:14 pm)
Re: [LAU] [LAD] gxtuner-1.5 released, Jostein Chr. Andersen, (Sat Dec 10, 11:07 pm)
Re: [LAU] [LAD] gxtuner-1.5 released, hermann, (Fri Dec 9, 9:29 pm)
Re: [LAU] [LAA] [ANN] guitarix release guitarix2-0.18.0, Jeremy Jongepier, (Tue Sep 6, 11:23 am)
[LAU] update release gxtuner-1.3, hermann, (Thu Jul 28, 7:56 am)
[LAU] [LAA] [ANN] release gxtuner-1.0, hermann, (Sun Jul 24, 11:12 am)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, R.Wolff, (Mon Jul 25, 11:12 am)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, hermann, (Mon Jul 25, 5:54 pm)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, allcoms, (Wed Jul 27, 10:08 am)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, hermann, (Wed Jul 27, 11:06 am)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, allcoms, (Wed Jul 27, 2:33 pm)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, hermann, (Wed Jul 27, 6:04 pm)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, hermann, (Wed Jul 27, 2:54 pm)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, Fons Adriaensen, (Wed Jul 27, 10:13 am)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, hermann, (Wed Jul 27, 11:19 am)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, Fons Adriaensen, (Wed Jul 27, 11:58 am)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, hermann, (Wed Jul 27, 12:49 pm)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, Fons Adriaensen, (Wed Jul 27, 2:21 pm)
Re: [LAU] [LAA] [ANN] release gxtuner-1.0, hermann, (Wed Jul 27, 3:14 pm)
Re: [LAU] [LAA] [ANN] guitarix release guitarix2-0.17.0, Alexandre Prokoudine, (Sat Jul 9, 6:50 pm)
[LAU] [LAA] [ANN] guitarix released gx_head, hermann, (Fri Jan 14, 8:36 pm)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, hermann, (Mon Jan 17, 11:13 am)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, Mark Knecht, (Sun Jan 16, 6:31 pm)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, hermann, (Sun Jan 16, 8:22 pm)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, Ivan Tarozzi, (Sun Jan 16, 7:04 pm)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, Ivan Tarozzi, (Sun Jan 16, 6:18 pm)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, Ivan Tarozzi, (Mon Jan 17, 1:35 pm)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, hermann, (Tue Jan 18, 6:53 am)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, hermann, (Sun Jan 16, 8:04 pm)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, Jeremy Jongepier, (Sun Jan 16, 10:08 am)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, hermann, (Sun Jan 16, 11:48 am)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, hermann, (Sun Jan 16, 8:08 pm)
Re: [LAU] [LAA] [ANN] guitarix released gx_head, hermann, (Sun Jan 16, 8:25 pm)
[LAU] [LAA] [ANN] guitarix-0.10.0 released, hermann, (Sun Jul 11, 4:45 am)
[LAU] guitarix-0.08.0 release 'Rockers', hermann, (Fri May 14, 2:44 pm)
[LAU] [LAA] [ANN] guitarix-0.06.0 bugfix release, hermann, (Thu Feb 18, 8:08 pm)
[LAU] [LAA] [ANN] guitarix-0.05.9-1 release, hermann, (Thu Feb 18, 6:11 am)
[LAU] [LAA] [ANN] guitarix-0.05.6-1 released, hermann, (Fri Jan 1, 11:44 am)
[LAU] [LAA] [ANN] guitarix-0.05.2-1 released, hermann, (Sun Dec 13, 10:45 am)
[LAU] [LAA] [ANN] guitarix-0.05.2-1 released, hermann, (Mon Nov 30, 12:44 pm)
Re: [LAU] [LAA] [ANN] guitarix-0.05.2-1 released, Hartmut Noack, (Thu Dec 3, 1:05 pm)
[LAU] [ANN] guitarix-0.05.1-1 released, hermann, (Mon Oct 26, 7:52 am)
[LAU] [LAA] [ANN] guitarix-0.05.0-1 released, hermann, (Mon Jul 27, 8:14 am)
[LAU] [LAA] [ANN] guitarix-0.04.6-1 release, hermann meyer, (Wed Jun 24, 3:03 pm)
Re: [LAU] [LAA] [ANN] guitarix-0.04.6-1 release, Dragan Noveski, (Wed Jun 24, 3:49 pm)
Re: [LAU] [LAA] [ANN] guitarix-0.04.6-1 release, hermann meyer, (Wed Jun 24, 4:22 pm)
[LAU] [ANN] guitarix-0.03.9-1 bugfix release, hermann meyer, (Thu Mar 26, 7:52 pm)
[LAU] [ANN] guitarix release version 0.03.8-1, hermann meyer, (Wed Mar 18, 7:00 pm)
[LAU] [ANN] guitarix third release, hermann meyer, (Sun Nov 23, 5:19 pm)
Re: [LAU] [ANN] guitarix third release, Dragan Noveski, (Tue Nov 25, 1:18 pm)
Re: [LAU] [ANN] guitarix third release, hermann meyer, (Tue Nov 25, 2:34 pm)
Re: [LAU] [ANN] guitarix third release, Dragan Noveski, (Wed Nov 26, 2:07 am)
[LAU] [ANN] guitarix second release, hermann meyer, (Sun Nov 2, 3:35 pm)
Re: [LAU] [ANN] guitarix second release, David Baron, (Sun Nov 2, 6:39 pm)
Re: [LAU] [ANN] guitarix second release, Fons Adriaensen, (Sun Nov 2, 8:18 pm)
Re: [LAU] [ANN] guitarix second release, hermann meyer, (Sun Nov 2, 6:52 pm)
Re: [LAU] [ANN] guitarix second release, Dragan Noveski, (Sun Nov 2, 4:41 pm)
Re: [LAU] [ANN] guitarix second release, hermann meyer, (Sun Nov 2, 4:58 pm)
Re: [LAU] [ANN] guitarix second release, Dragan Noveski, (Sun Nov 2, 5:44 pm)
Re: [LAU] [ANN] guitarix second release, Julien Claassen, (Sun Nov 2, 3:59 pm)
Re: [LAU] [ANN] guitarix second release, hermann meyer, (Sun Nov 2, 4:35 pm)
Re: [LAU] [ANN] guitarix second release, Julien Claassen, (Sun Nov 2, 6:30 pm)
Re: [LAU] [ANN] guitarix second release, Darren Landrum, (Sun Nov 2, 3:53 pm)
Re: [LAU] [ANN] guitarix second release, hermann meyer, (Sun Nov 2, 4:30 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Joern Nettingsmeier, (Fri Oct 17, 3:38 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Kjetil S. Matheussen, (Fri Oct 17, 3:01 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Olivier Guilyardi, (Fri Oct 17, 3:16 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Kjetil S. Matheussen, (Fri Oct 17, 3:31 pm)
Re: [LAU] Simple, easy multithreaded circular buffer library..., Kjetil S. Matheussen, (Fri Oct 17, 12:26 pm)