2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include "svm_fifo_segment.h"
19 hello_world (int verbose)
21 svm_fifo_segment_create_args_t _a, *a = &_a;
22 svm_fifo_segment_private_t *sp;
26 u8 *retrieved_data = 0;
27 clib_error_t *error = 0;
29 memset (a, 0, sizeof (*a));
31 a->segment_name = "fifo-test1";
32 a->segment_size = 256 << 10;
34 rv = svm_fifo_segment_create (a);
37 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
39 sp = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
41 f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
44 return clib_error_return (0, "svm_fifo_segment_alloc_fifo failed");
46 test_data = format (0, "Hello world%c", 0);
47 vec_validate (retrieved_data, vec_len (test_data) - 1);
49 while (svm_fifo_max_enqueue (f) >= vec_len (test_data))
50 svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data);
52 while (svm_fifo_max_dequeue (f) >= vec_len (test_data))
53 svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data);
55 while (svm_fifo_max_enqueue (f) >= vec_len (test_data))
56 svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data);
58 while (svm_fifo_max_dequeue (f) >= vec_len (test_data))
59 svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data);
61 if (!memcmp (retrieved_data, test_data, vec_len (test_data)))
62 error = clib_error_return (0, "data test OK, got '%s'", retrieved_data);
64 error = clib_error_return (0, "data test FAIL!");
66 svm_fifo_segment_free_fifo (sp, f, FIFO_SEGMENT_RX_FREELIST);
74 svm_fifo_segment_create_args_t _a, *a = &_a;
75 svm_fifo_segment_private_t *sp;
79 u8 *retrieved_data = 0;
82 memset (a, 0, sizeof (*a));
84 a->segment_name = "fifo-test1";
85 a->segment_size = 256 << 10;
87 rv = svm_fifo_segment_create (a);
90 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
92 sp = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
94 f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
97 return clib_error_return (0, "svm_fifo_segment_alloc_fifo failed");
99 test_data = format (0, "Hello world%c", 0);
100 vec_validate (retrieved_data, vec_len (test_data) - 1);
102 for (i = 0; i < 1000; i++)
103 svm_fifo_enqueue_nowait (f, vec_len (test_data), test_data);
105 return clib_error_return (0, "master (enqueue) done");
111 svm_fifo_segment_create_args_t _a, *a = &_a;
112 svm_fifo_segment_private_t *sp;
114 svm_fifo_t **flist = 0;
118 memset (a, 0, sizeof (*a));
120 a->segment_name = "fifo-test1";
121 a->segment_size = 256 << 10;
123 rv = svm_fifo_segment_create (a);
126 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
128 sp = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
130 for (i = 0; i < 1000; i++)
132 f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
138 fformat (stdout, "Try #1: created %d fifos...\n", vec_len (flist));
139 for (i = 0; i < vec_len (flist); i++)
142 svm_fifo_segment_free_fifo (sp, f, FIFO_SEGMENT_RX_FREELIST);
145 _vec_len (flist) = 0;
147 for (i = 0; i < 1000; i++)
149 f = svm_fifo_segment_alloc_fifo (sp, 4096, FIFO_SEGMENT_RX_FREELIST);
155 fformat (stdout, "Try #2: created %d fifos...\n", vec_len (flist));
156 for (i = 0; i < vec_len (flist); i++)
159 svm_fifo_segment_free_fifo (sp, f, FIFO_SEGMENT_RX_FREELIST);
168 svm_fifo_segment_create_args_t _a, *a = &_a;
169 svm_fifo_segment_private_t *sp;
173 u32 *recovered_data = 0;
176 memset (a, 0, sizeof (*a));
178 a->segment_name = "fifo-test1";
179 a->segment_size = 256 << 10;
181 rv = svm_fifo_segment_create (a);
184 return clib_error_return (0, "svm_fifo_segment_create returned %d", rv);
186 sp = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
188 f = svm_fifo_segment_alloc_fifo (sp, 200 << 10, FIFO_SEGMENT_RX_FREELIST);
191 return clib_error_return (0, "svm_fifo_segment_alloc_fifo failed");
193 for (i = 0; i < (3 * 1024); i++)
194 vec_add1 (test_data, i);
196 /* Enqueue the first 1024 u32's */
197 svm_fifo_enqueue_nowait (f, 4096 /* bytes to enqueue */ ,
200 /* Enqueue the third 1024 u32's 2048 ahead of the current tail */
201 svm_fifo_enqueue_with_offset (f, 4096, 4096, (u8 *) & test_data[2048]);
203 /* Enqueue the second 1024 u32's at the current tail */
204 svm_fifo_enqueue_nowait (f, 4096 /* bytes to enqueue */ ,
205 (u8 *) & test_data[1024]);
207 vec_validate (recovered_data, (3 * 1024) - 1);
209 svm_fifo_dequeue_nowait (f, 3 * 4096, (u8 *) recovered_data);
211 for (i = 0; i < (3 * 1024); i++)
213 if (recovered_data[i] != test_data[i])
215 clib_warning ("[%d] expected %d recovered %d", i,
216 test_data[i], recovered_data[i]);
217 return clib_error_return (0, "offset test FAILED");
221 return clib_error_return (0, "offset test OK");
227 svm_fifo_segment_create_args_t _a, *a = &_a;
228 svm_fifo_segment_private_t *sp;
230 ssvm_shared_header_t *sh;
231 svm_fifo_segment_header_t *fsh;
234 u8 *retrieved_data = 0;
237 memset (a, 0, sizeof (*a));
239 a->segment_name = "fifo-test1";
241 rv = svm_fifo_segment_attach (a);
244 return clib_error_return (0, "svm_fifo_segment_attach returned %d", rv);
246 sp = svm_fifo_segment_get_segment (a->new_segment_indices[0]);
248 fsh = (svm_fifo_segment_header_t *) sh->opaque[0];
250 /* might wanna wait.. */
253 /* Lazy bastards united */
254 test_data = format (0, "Hello world%c", 0);
255 vec_validate (retrieved_data, vec_len (test_data) - 1);
257 for (i = 0; i < 1000; i++)
259 svm_fifo_dequeue_nowait (f, vec_len (retrieved_data), retrieved_data);
260 if (memcmp (retrieved_data, test_data, vec_len (retrieved_data)))
261 return clib_error_return (0, "retrieved data incorrect, '%s'",
265 return clib_error_return (0, "slave (dequeue) done");
270 test_ssvm_fifo1 (unformat_input_t * input)
272 clib_error_t *error = 0;
276 svm_fifo_segment_main_init (0x200000000ULL, 20);
278 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
280 if (unformat (input, "verbose %d", &verbose))
282 else if (unformat (input, "verbose"))
284 else if (unformat (input, "master"))
286 else if (unformat (input, "slave"))
288 else if (unformat (input, "mempig"))
290 else if (unformat (input, "offset"))
294 error = clib_error_create ("unknown input `%U'\n",
295 format_unformat_error, input);
303 error = hello_world (verbose);
307 error = master (verbose);
311 error = slave (verbose);
315 error = mempig (verbose);
319 error = offset (verbose);
323 error = clib_error_return (0, "test id %d unknown", test_id);
329 clib_error_report (error);
337 main (int argc, char *argv[])
342 unformat_init_command_line (&i, argv);
343 r = test_ssvm_fifo1 (&i);
349 * fd.io coding-style-patch-verification: ON
352 * eval: (c-set-style "gnu")