|
11 | 11 | #include <zephyr/sys/kobject.h> |
12 | 12 | #include <zephyr/sys/libc-hooks.h> |
13 | 13 | #include <zephyr/app_memory/mem_domain.h> |
| 14 | +#include <zephyr/sys/util_loops.h> |
| 15 | +#include <zephyr/sys/time_units.h> |
14 | 16 | #include <zephyr/rtio/rtio_spsc.h> |
| 17 | +#include <zephyr/rtio/rtio_mpsc.h> |
15 | 18 | #include <zephyr/rtio/rtio.h> |
16 | 19 | #include <zephyr/rtio/rtio_executor_simple.h> |
17 | 20 | #include <zephyr/rtio/rtio_executor_concurrent.h> |
@@ -234,6 +237,171 @@ ZTEST(rtio_spsc, test_spsc_threaded) |
234 | 237 | k_thread_join(tinfo[0].tid, K_FOREVER); |
235 | 238 | } |
236 | 239 |
|
| 240 | +static struct rtio_mpsc push_pop_q; |
| 241 | +static struct rtio_mpsc_node push_pop_nodes[2]; |
| 242 | + |
| 243 | +/* |
| 244 | + * @brief Push and pop one element |
| 245 | + * |
| 246 | + * @see rtio_mpsc_push(), rtio_mpsc_pop() |
| 247 | + * |
| 248 | + * @ingroup rtio_tests |
| 249 | + */ |
| 250 | +ZTEST(rtio_mpsc, test_push_pop) |
| 251 | +{ |
| 252 | + |
| 253 | + struct rtio_mpsc_node *node, *head, *stub, *next, *tail; |
| 254 | + |
| 255 | + rtio_mpsc_init(&push_pop_q); |
| 256 | + |
| 257 | + head = atomic_ptr_get(&push_pop_q.head); |
| 258 | + tail = push_pop_q.tail; |
| 259 | + stub = &push_pop_q.stub; |
| 260 | + next = atomic_ptr_get(&stub->next); |
| 261 | + |
| 262 | + zassert_equal(head, stub, "Head should point at stub"); |
| 263 | + zassert_equal(tail, stub, "Tail should point at stub"); |
| 264 | + zassert_is_null(next, "Next should be null"); |
| 265 | + |
| 266 | + node = rtio_mpsc_pop(&push_pop_q); |
| 267 | + zassert_is_null(node, "Pop on empty queue should return null"); |
| 268 | + |
| 269 | + rtio_mpsc_push(&push_pop_q, &push_pop_nodes[0]); |
| 270 | + |
| 271 | + head = atomic_ptr_get(&push_pop_q.head); |
| 272 | + |
| 273 | + zassert_equal(head, &push_pop_nodes[0], "Queue head should point at push_pop_node"); |
| 274 | + next = atomic_ptr_get(&push_pop_nodes[0].next); |
| 275 | + zassert_is_null(next, NULL, "push_pop_node next should point at null"); |
| 276 | + next = atomic_ptr_get(&push_pop_q.stub.next); |
| 277 | + zassert_equal(next, &push_pop_nodes[0], "Queue stub should point at push_pop_node"); |
| 278 | + tail = push_pop_q.tail; |
| 279 | + stub = &push_pop_q.stub; |
| 280 | + zassert_equal(tail, stub, "Tail should point at stub"); |
| 281 | + |
| 282 | + node = rtio_mpsc_pop(&push_pop_q); |
| 283 | + stub = &push_pop_q.stub; |
| 284 | + |
| 285 | + zassert_not_equal(node, stub, "Pop should not return stub"); |
| 286 | + zassert_not_null(node, "Pop should not return null"); |
| 287 | + zassert_equal(node, &push_pop_nodes[0], |
| 288 | + "Pop should return push_pop_node %p, instead was %p", |
| 289 | + &push_pop_nodes[0], node); |
| 290 | + |
| 291 | + node = rtio_mpsc_pop(&push_pop_q); |
| 292 | + zassert_is_null(node, "Pop on empty queue should return null"); |
| 293 | +} |
| 294 | + |
| 295 | +#define MPSC_FREEQ_SZ 8 |
| 296 | +#define MPSC_ITERATIONS 100000 |
| 297 | +#define MPSC_STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE) |
| 298 | +#define MPSC_THREADS_NUM 4 |
| 299 | + |
| 300 | +static struct thread_info mpsc_tinfo[MPSC_THREADS_NUM]; |
| 301 | +static struct k_thread mpsc_thread[MPSC_THREADS_NUM]; |
| 302 | +static K_THREAD_STACK_ARRAY_DEFINE(mpsc_stack, MPSC_THREADS_NUM, MPSC_STACK_SIZE); |
| 303 | + |
| 304 | +struct mpsc_node { |
| 305 | + uint32_t id; |
| 306 | + struct rtio_mpsc_node n; |
| 307 | +}; |
| 308 | + |
| 309 | + |
| 310 | +RTIO_SPSC_DECLARE(node_sq, struct mpsc_node, MPSC_FREEQ_SZ); |
| 311 | + |
| 312 | +#define SPSC_INIT(n, sz) RTIO_SPSC_INITIALIZER(sz) |
| 313 | + |
| 314 | +struct rtio_spsc_node_sq node_q[MPSC_THREADS_NUM] = { |
| 315 | + LISTIFY(MPSC_THREADS_NUM, SPSC_INIT, (,), MPSC_FREEQ_SZ) |
| 316 | +}; |
| 317 | + |
| 318 | +static struct rtio_mpsc mpsc_q; |
| 319 | + |
| 320 | +static void mpsc_consumer(void *p1, void *p2, void *p3) |
| 321 | +{ |
| 322 | + struct rtio_mpsc_node *n; |
| 323 | + struct mpsc_node *nn; |
| 324 | + uint32_t start_t = k_cycle_get_32(); |
| 325 | + |
| 326 | + for (int i = 0; i < (MPSC_ITERATIONS)*(MPSC_THREADS_NUM - 1); i++) { |
| 327 | + do { |
| 328 | + n = rtio_mpsc_pop(&mpsc_q); |
| 329 | + } while (n == NULL); |
| 330 | + |
| 331 | + zassert_not_equal(n, &mpsc_q.stub, "mpsc should not produce stub"); |
| 332 | + |
| 333 | + nn = CONTAINER_OF(n, struct mpsc_node, n); |
| 334 | + |
| 335 | + rtio_spsc_acquire(&node_q[nn->id]); |
| 336 | + rtio_spsc_produce(&node_q[nn->id]); |
| 337 | + } |
| 338 | + |
| 339 | + uint32_t diff = k_cycle_get_32() - start_t; |
| 340 | + |
| 341 | + uint32_t us = k_cyc_to_us_floor32(diff); |
| 342 | + |
| 343 | + TC_PRINT("%d mpsc consumes and spsc produces in %u cycles (%u microseconds)\n", |
| 344 | + (MPSC_ITERATIONS)*(MPSC_THREADS_NUM-1), diff, us); |
| 345 | +} |
| 346 | + |
| 347 | +static void mpsc_producer(void *p1, void *p2, void *p3) |
| 348 | +{ |
| 349 | + struct mpsc_node *n; |
| 350 | + uint32_t id = (uint32_t)(uintptr_t)p1; |
| 351 | + |
| 352 | + for (int i = 0; i < MPSC_ITERATIONS; i++) { |
| 353 | + do { |
| 354 | + n = rtio_spsc_consume(&node_q[id]); |
| 355 | + } while (n == NULL); |
| 356 | + |
| 357 | + rtio_spsc_release(&node_q[id]); |
| 358 | + n->id = id; |
| 359 | + rtio_mpsc_push(&mpsc_q, &n->n); |
| 360 | + } |
| 361 | +} |
| 362 | + |
| 363 | +/** |
| 364 | + * @brief Test that the producer and consumer are indeed thread safe |
| 365 | + * |
| 366 | + * This can and should be validated on SMP machines where incoherent |
| 367 | + * memory could cause issues. |
| 368 | + */ |
| 369 | +ZTEST(rtio_mpsc, test_mpsc_threaded) |
| 370 | +{ |
| 371 | + rtio_mpsc_init(&mpsc_q); |
| 372 | + |
| 373 | + TC_PRINT("setting up mpsc producer free queues\n"); |
| 374 | + /* Setup node free queues */ |
| 375 | + for (int i = 0; i < MPSC_THREADS_NUM; i++) { |
| 376 | + for (int j = 0; j < MPSC_FREEQ_SZ; j++) { |
| 377 | + rtio_spsc_acquire(&node_q[i]); |
| 378 | + } |
| 379 | + rtio_spsc_produce_all(&node_q[i]); |
| 380 | + } |
| 381 | + |
| 382 | + TC_PRINT("starting consumer\n"); |
| 383 | + mpsc_tinfo[0].tid = |
| 384 | + k_thread_create(&mpsc_thread[0], mpsc_stack[0], STACK_SIZE, |
| 385 | + (k_thread_entry_t)mpsc_consumer, |
| 386 | + NULL, NULL, NULL, |
| 387 | + K_PRIO_PREEMPT(5), |
| 388 | + K_INHERIT_PERMS, K_NO_WAIT); |
| 389 | + |
| 390 | + for (int i = 1; i < MPSC_THREADS_NUM; i++) { |
| 391 | + TC_PRINT("starting producer %i\n", i); |
| 392 | + mpsc_tinfo[i].tid = |
| 393 | + k_thread_create(&mpsc_thread[i], mpsc_stack[i], STACK_SIZE, |
| 394 | + (k_thread_entry_t)mpsc_producer, |
| 395 | + (void*)(uintptr_t)i, NULL, NULL, |
| 396 | + K_PRIO_PREEMPT(5), |
| 397 | + K_INHERIT_PERMS, K_NO_WAIT); |
| 398 | + } |
| 399 | + |
| 400 | + for (int i = 0; i < MPSC_THREADS_NUM; i++) { |
| 401 | + TC_PRINT("joining mpsc thread %d\n", i); |
| 402 | + k_thread_join(mpsc_tinfo[i].tid, K_FOREVER); |
| 403 | + } |
| 404 | +} |
237 | 405 |
|
238 | 406 | RTIO_EXECUTOR_SIMPLE_DEFINE(simple_exec_simp); |
239 | 407 | RTIO_DEFINE(r_simple_simp, (struct rtio_executor *)&simple_exec_simp, 4, 4); |
@@ -505,4 +673,5 @@ ZTEST(rtio_api, test_rtio_syscalls) |
505 | 673 |
|
506 | 674 |
|
507 | 675 | ZTEST_SUITE(rtio_spsc, NULL, NULL, NULL, NULL, NULL); |
| 676 | +ZTEST_SUITE(rtio_mpsc, NULL, NULL, NULL, NULL, NULL); |
508 | 677 | ZTEST_SUITE(rtio_api, NULL, NULL, NULL, NULL, NULL); |
0 commit comments