summaryrefslogtreecommitdiffstats
path: root/lf_ring_buffer_test.c
blob: 31d4810c2f229a1b0a3cbe600a8d0cb7bcc13385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include "lf_ring_buffer.h"

#define BUFFER_LEN 5000000

static rb_data_t[BUFFER_LEN][RB_DATA_LEN]

static const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;

static int64_t time_diff(struct timespec *t0, struct timespec *t1)
{
    return ((t1->tv_sec * 1000000000) + t1->tv_nsec) - ((t0->tv_sec * 1000000000) + t0->tv_nsec);
}

static void print_now(char* s) {
    fprintf(stdout,s);
    fflush(stdout);
}

static void report( int n, uint64_t dt ) {
    fprintf(stdout,"%d operations in %d [ms] => %d [us] => %d [ns]\t%d [ns/op]\n", n, (int)(dt/1000000), (int)(dt/1000), (int)dt, (int)(dt/n) );
}

static void feed_data( int n){
    int i;
    for(i=0; i<n; i++){
        sprintf(data[i],"hello world %04d\n",i);
    }
}

static uint64_t sequential_writes( lf_ring_buffer_t *ring, int n ) {
    int i;
    struct timespec start, end;
    
    print_now("sequential writes ... ");

    clock_gettime(CLOCK_MONOTONIC, &start);

    for(i=0; i<n; i++) lf_ring_buffer_write( ring, data[i], 0 );

    clock_gettime(CLOCK_MONOTONIC, &end);
    printf("done.\n");
    
    return time_diff( &start, &end );
}

static uint64_t sequential_reads( lf_ring_buffer_t *ring, int n ) {
    int i;
    char data[20];
    struct timespec start, end;
    
    print_now("sequential reads ... ");

    clock_gettime(CLOCK_MONOTONIC, &start);

    for(i=0; i<n; i++) lf_ring_buffer_read( ring, data, 0 );

    clock_gettime(CLOCK_MONOTONIC, &end);
    printf("done.\n");
    
    return time_diff( &start, &end );
}

int main( int argc, char** argv ) {
    
    int b_len = BUFFER_LEN;
    lf_ring_buffer_t *ring;

    ring = lf_ring_buffer_create( b_len );
    if(ring==NULL){
        fprintf(stderr,"ERROR : lf_ring_buffer_create( %d );\n",b_len);
        exit( EXIT_FAILURE );
    }

    print_now("feed the data ... ");
    feed_data(b_len);
    printf("done.\n");

    report( b_len, sequential_writes( ring, b_len ) );
    report( sequential_reads( ring, b_len ) );

    lf_ring_buffer_destroy( ring );

    return EXIT_SUCCESS;
}

/*
#define ARRAY_SIZE 2
#define MAX_VALUE 0x10000

jack_ringbuffer_t *rb;
volatile int flowing = 0;

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);
      fflush(stdout);
      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] ");
  fflush(stdout);
  i = fill_int_array (a, i, ARRAY_SIZE);
  while (1)
  {
    if (j == 100 && !flowing)
    {
      printf("[flowing] ");
      fflush(stdout);
      flowing = 1;
    }

    if (jack_ringbuffer_read_space (rb) >= (int) ARRAY_SIZE * (int) 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;
          //
          printf("FAILURE in chunk %lu\n", j);
          fflush(stdout);
          exit(1);
        }
        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] ");
  fflush(stdout);

  i = fill_int_array (a, i, ARRAY_SIZE);

  while (1)
  {
    if (jack_ringbuffer_write_space (rb) >= (int) ARRAY_SIZE * (int) 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;
  sscanf(argv[1], "%d", &size);
  printf("starting (120s max) - array/buffer size: %d/%d\n", 
         (int) sizeof(int) * ARRAY_SIZE, size);
  fflush(stdout);
  rb = jack_ringbuffer_create(size);
  pthread_t reader_thread, writer_thread;
  if (pthread_create (&reader_thread, NULL, reader_start, NULL))
  {
    printf("Failed to create reader thread\n");
    exit(1);
  }
  if (pthread_create (&writer_thread, NULL, writer_start, NULL))
  {
    printf("Failed to create writer thread\n");
    exit(1);
  }
  sleep(120);
  if (flowing)
    printf("SUCCESS\n");
  else
    printf("FAILURE: data did not flow\n");
  fflush(stdout);
  return 0;
}
*/