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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/*
* File : lf_fifo_test.c
* Author : Jérémy Zurcher <jeremy@asynk.ch>
* Date : 2013/01/30
* License :
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "stdio.h"
#include "stdlib.h"
#include <pthread.h>
#include "lf_fifo.h"
typedef struct _node_t
{
uint32_t data;
lf_pointer_t link;
} node_t;
typedef struct _thread_params_t
{
uint32_t n;
lf_fifo_t *fifo;
node_t *nodes;
} thread_params_t;
static void _failure(const char *msg)
{
fprintf(stderr,"%s\n",msg);
exit(EXIT_FAILURE);
}
static void _check(int cond, const char *msg)
{
if(!cond)
{
fprintf(stderr,"%s\n",msg);
exit(EXIT_FAILURE);
}
}
static uint64_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 report( char* op, uint32_t threads, uint32_t nodes, uint64_t dt)
{
uint32_t n = threads*nodes;
fprintf(stdout," - %s: %3d threads * %6d op in %4u [ms] : %7u [us] : %10u [ns]\t -> %6d [ns/op]\n",
op, threads, nodes, (unsigned int)(dt/1000000), (unsigned int)(dt/1000), (unsigned int)dt, (int)(dt/n));
}
static uint32_t fifo_length(lf_fifo_t *fifo)
{
uint32_t l = 0;
lf_pointer_t* link;
link = (lf_pointer_t*)fifo->head.pointer;
while(link)
{
l++;
link = (lf_pointer_t*)link->pointer;
}
return l;
}
void* aggressive_push( void* param )
{
uint32_t i;
lf_fifo_t *fifo;
node_t *nodes;
thread_params_t *params;
params = (thread_params_t*)param;
fifo = params->fifo;
nodes = params->nodes;
if (nodes==NULL)
{
nodes = params->nodes = malloc( sizeof(node_t)*params->n);
if (nodes==NULL) _failure("nodes malloc failure");
}
for(i=0; i<params->n; i++)
{
nodes[i].data = i+1;
lf_fifo_push(fifo,&nodes[i].link);
}
params->n = i;
return NULL;
}
void* aggressive_pop( void* param )
{
uint32_t i;
lf_fifo_t *fifo;
thread_params_t *params;
params = (thread_params_t*)param;
fifo = params->fifo;
i = 0;
for(;;)
{
if(lf_fifo_pop(fifo)==NULL) break;
i++;
}
params->n = i;
return NULL;
}
static void run_aggressive_push_pop(int threads_n, int nodes_n)
{
uint32_t i, j;
lf_fifo_t fifo;
pthread_t *threads;
thread_params_t *params;
struct timespec start, end;
threads = malloc( sizeof(pthread_t)*threads_n);
if (threads==NULL) _failure("threads malloc failure");
params = malloc( sizeof(thread_params_t)*threads_n);
if (params==NULL) _failure("params malloc failure");
lf_fifo_init( &fifo);
for(i=0; i<threads_n; i++)
{
params[i].fifo = &fifo;
params[i].n = nodes_n;
}
clock_gettime(CLOCK_MONOTONIC, &start);
for(i=0; i<threads_n; i++)
{
if (pthread_create( &threads[i], NULL, aggressive_push, ¶ms[i]))
_failure("Failed to create thread");
}
for(i=0; i<threads_n; i++)
{
pthread_join( threads[i], NULL );
}
clock_gettime(CLOCK_MONOTONIC, &end);
report( "aggressive push", threads_n, nodes_n, time_diff( &start, &end ));
_check((fifo_length(&fifo)==(threads_n*nodes_n)),"fifo length failure after aggressive push");
for(i=0; i<threads_n; i++)
{
_check((params[i].n==nodes_n),"push n failure after aggressive push");
}
clock_gettime(CLOCK_MONOTONIC, &start);
for(i=0; i<threads_n; i++)
{
if (pthread_create( &threads[i], NULL, aggressive_pop, ¶ms[i]))
_failure("Failed to create thread");
}
for(i=0; i<threads_n; i++)
{
pthread_join( threads[i], NULL );
}
clock_gettime(CLOCK_MONOTONIC, &end);
report( "aggressive pop", threads_n, nodes_n, time_diff( &start, &end ));
_check((fifo_length(&fifo)==0),"fifo length failure after aggressive pop");
j = 0;
for(i=0; i<threads_n; i++)
{
j += params[i].n;
if (params[i].nodes) free(params[i].nodes);
}
_check((j==(threads_n*nodes_n)),"poped nodes count failure");
free(threads);
free(params);
}
static void run_interlaced_push_pop(int push_threads_n, int pop_threads_n, int nodes_n)
{
int threads_n;
uint32_t i,j;
lf_fifo_t fifo;
struct timespec start, end;
pthread_t *threads;
thread_params_t *params;
threads_n = push_threads_n + pop_threads_n;
threads = malloc( sizeof(pthread_t)*threads_n);
if (threads==NULL) _failure("threads malloc failure");
params = malloc( sizeof(thread_params_t)*threads_n);
if (params==NULL) _failure("params malloc failure");
lf_fifo_init( &fifo);
for(i=0; i<threads_n; i++)
{
params[i].fifo = &fifo;
params[i].n = nodes_n;
}
clock_gettime(CLOCK_MONOTONIC, &start);
for(i=0; i<push_threads_n; i++)
{
if (pthread_create( &threads[i], NULL, aggressive_push, ¶ms[i]))
_failure("Failed to create push thread");
}
for(; i<threads_n-1; i++)
{
if (pthread_create( &threads[i], NULL, aggressive_pop, ¶ms[i]))
_failure("Failed to create pop thread");
}
for(i=0; i<threads_n-1; i++)
{
pthread_join( threads[i], NULL );
}
if (pthread_create( &threads[threads_n-1], NULL, aggressive_pop, ¶ms[threads_n-1]))
_failure("Failed to create pop thread");
pthread_join( threads[threads_n-1], NULL );
clock_gettime(CLOCK_MONOTONIC, &end);
for(i=0; i<push_threads_n; i++)
{
_check((params[i].n==nodes_n),"push n failure after aggressive push");
if (params[i].nodes) free(params[i].nodes);
}
j = 0;
for(; i<threads_n; i++)
{
j += params[i].n;
if (params[i].nodes) free(params[i].nodes);
}
/* _check((j==(push_threads_n*nodes_n)),"poped nodes count failure"); */
fprintf(stderr,"%d/%d\n",j,(push_threads_n*nodes_n));
/* report( "interlaced push-pop", (push_threads_n+pop_threads), nodes_n, time_diff( &start, &end )); */
/* _check((fifo_length(&fifo)==(threads_n*nodes_n)),"fifo length failure after aggressive push"); */
free(threads);
free(params);
}
int main(int argc, char *argv[])
{
printf("running...\n");
/* run_aggressive_push_pop(100,1000000); */
run_interlaced_push_pop(10,10,10000000);
printf("success\n");
return EXIT_SUCCESS;
}
|