From 1fa306643fbdb77fc050d567c6e880ea4c77c453 Mon Sep 17 00:00:00 2001 From: amir Date: Mon, 9 Mar 2026 18:01:11 +0100 Subject: [PATCH] Align the MPMC queue to pagesize --- binaries/changelog.txt | 2 ++ lf_mpmc.h | 16 +++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/binaries/changelog.txt b/binaries/changelog.txt index 6acb7a0..ac2b8b4 100644 --- a/binaries/changelog.txt +++ b/binaries/changelog.txt @@ -31,3 +31,5 @@ Making the LF MPMC queue generic and in a seperate header file 4.0: Implementing a semaphore in the LF MPMC queue to wake up consumers when there is items in the queue instead of spinning (busy waiting) or sleeping, this makes the queue spin only when the slots are transitionning (multiple consumers claiming the same slot) Making the MPMC queue platform agnostic + +Align the MPMC queue to pagesize diff --git a/lf_mpmc.h b/lf_mpmc.h index 7884796..c302f30 100644 --- a/lf_mpmc.h +++ b/lf_mpmc.h @@ -58,15 +58,13 @@ typedef struct { /* INIT */ /* ----------------------------------------------------------- */ static void mpmc_init(MPMCQueue *q, size_t max_capacity) { - if ((max_capacity & (max_capacity - 1)) != 0) { - fprintf(stderr, "capacity must be power of two\n"); - exit(1); - } q->capacity = max_capacity; q->mask = max_capacity - 1; - size_t bytes = sizeof(MPMCSlot) * max_capacity; + u32 pagesize = plat_get_pagesize(); + + size_t bytes = ALIGN_UP_POW2(sizeof(MPMCSlot) * max_capacity, pagesize); q->slots = (MPMCSlot *)plat_mem_reserve(bytes); @@ -75,12 +73,16 @@ static void mpmc_init(MPMCQueue *q, size_t max_capacity) { exit(1); } - q->commit_step = (64ull * 1024 * 1024) / sizeof(MPMCSlot); + u64 commit_bytes = pagesize; + commit_bytes = ALIGN_UP_POW2(commit_bytes, pagesize); + + q->commit_step = commit_bytes / sizeof(MPMCSlot); + atomic_flag_clear(&q->commit_lock); q->committed = q->commit_step; - plat_mem_commit(q->slots, q->commit_step * sizeof(MPMCSlot)); + plat_mem_commit(q->slots, commit_bytes); for (size_t i = 0; i < q->committed; i++) { atomic_init(&q->slots[i].seq, i);