summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorliuyu <[email protected]>2024-07-01 05:20:37 -0400
committerliuyu <[email protected]>2024-07-01 05:20:37 -0400
commit075525658564b863fe6feec425b2fe878575e3c7 (patch)
tree926cffa25e3a1e8215839ddb45756a34d34adfbd
parent79f2c35744a3a805c2f7b0884cf72f310a7d2dd2 (diff)
bbq使用内存池(临时代码)
-rw-r--r--bbq/include/bbq.h14
-rw-r--r--bbq/src/bbq.c58
2 files changed, 66 insertions, 6 deletions
diff --git a/bbq/include/bbq.h b/bbq/include/bbq.h
index 9fbae2b..d752130 100644
--- a/bbq/include/bbq.h
+++ b/bbq/include/bbq.h
@@ -1,6 +1,6 @@
/*
* @Author: [email protected]
- * @LastEditTime: 2024-07-01 03:53:40
+ * @LastEditTime: 2024-07-01 09:18:56
* @Describe: bbq(Block-based Bounded Queue)头文件
* 参考:https://www.usenix.org/system/files/atc22-wang-jiawei.pdf
*/
@@ -11,6 +11,9 @@
#include <stdint.h>
#include <stdlib.h>
+#define TEST_PERF_1 // 去除多余原子操作
+#define TEST_PERF_MEM // 一次性分配所有内存
+
#ifndef __cplusplus
// C
#include <stdatomic.h>
@@ -65,8 +68,15 @@ struct bbq {
struct bbq_atomic64 n_enq;
struct bbq_atomic64 n_deq;
} stat;
-
struct bbq_block *blocks; // bn大小的数组
+
+#ifdef TEST_PERF_MEM
+ struct {
+ char *ptr; // 内存池起始地址
+ size_t off; // 已使用的偏移大小
+ size_t size; // 内存池总大小
+ } memory_pool;
+#endif
} __BBQ_CACHE_ALIGNED;
#define BBQ_F_DEFAULT 0x0
diff --git a/bbq/src/bbq.c b/bbq/src/bbq.c
index 882ba67..90bcdbe 100644
--- a/bbq/src/bbq.c
+++ b/bbq/src/bbq.c
@@ -1,6 +1,6 @@
/*
* @Author: liuyu
- * @LastEditTime: 2024-07-01 04:05:34
+ * @LastEditTime: 2024-07-01 05:20:02
* @Describe: bbq(Block-based Bounded Queue)实现
* 参考:https://www.usenix.org/system/files/atc22-wang-jiawei.pdf
@@ -11,8 +11,6 @@
#include <stdio.h>
#include <string.h>
-#define TEST_PERF_1
-
// flags第1位控制入队时的数据拷贝策略,默认是"拷贝指针"
#define BBQ_F_COPY_PTR BBQ_F_DEFAULT /**< 默认为拷贝指针 */
#define BBQ_F_COPY_VALUE 0x0001 /**< 创建队列时设置为拷贝数值 */
@@ -197,19 +195,41 @@ int bbq_bnbs_calc(uint32_t entries, uint32_t *bn, uint32_t *bs) {
return BBQ_OK;
}
+#ifdef TEST_PERF_MEM
+void *bbq_malloc_from_pool(struct bbq *q, size_t size) {
+ if (q->memory_pool.size - q->memory_pool.off < size) {
+ return NULL;
+ }
+
+ char *mem = q->memory_pool.ptr + q->memory_pool.off;
+ q->memory_pool.off += size;
+
+ return mem;
+}
+#endif
+
/* 块初始化 */
int block_init(struct bbq *q, struct bbq_block *block, bool cursor_init, uint32_t flags) {
size_t size = 0;
+
#ifdef BBQ_MEMORY
// 末尾多分配一个entry,它永远不应该被修改,以此检查是否存在写越界的问题
size = (q->bs + 1) * q->entry_size;
+#ifdef TEST_PERF_MEM
+ block->entries = bbq_malloc_from_pool(q, size);
+#else
block->entries = q->malloc_f(q->socket_id, size);
+#endif
char *last_entry = block->entries + q->entry_size * q->bs;
memset(block->entries, 0, size);
memset(last_entry, BBQ_MEM_MAGIC, q->entry_size);
#else
size = q->bs * q->entry_size;
- block->entries = q->malloc_f(q->socket_id, q->bs * q->entry_size);
+#ifdef TEST_PERF_MEM
+ block->entries = bbq_malloc_from_pool(q, size);
+#else
+ block->entries = q->malloc_f(q->socket_id, size);
+#endif
memset(block->entries, 0, size);
#endif
@@ -308,12 +328,33 @@ static struct bbq *__bbq_create_bnbs(const char *name, uint32_t bn, uint32_t bs,
free_f = bbq_free_def_callback;
}
+#ifdef TEST_PERF_MEM
+ uint32_t all_size = 0;
+
+#ifdef BBQ_MEMORY
+ all_size = sizeof(struct bbq) + bn * sizeof(struct bbq_block) + bn * (bs + 1) * obj_size;
+#else
+ all_size = sizeof(struct bbq) + bn * sizeof(struct bbq_block) + bn * bs * obj_size;
+#endif
+ struct bbq *q = malloc_f(socket_id, all_size);
+ if (q == NULL) {
+ bbq_errno = BBQ_ERR_ALLOC;
+ return NULL;
+ }
+
+ memset(q, 0, all_size);
+ q->memory_pool.size = all_size;
+ q->memory_pool.ptr = (char *)q;
+ q->memory_pool.off += sizeof(struct bbq);
+#else
struct bbq *q = malloc_f(socket_id, sizeof(*q));
if (q == NULL) {
bbq_errno = BBQ_ERR_ALLOC;
return NULL;
}
memset(q, 0, sizeof(*q));
+#endif
+
ret = snprintf(q->name, sizeof(q->name), "%s", name);
q->bn = bn;
q->bs = bs;
@@ -330,7 +371,12 @@ static struct bbq *__bbq_create_bnbs(const char *name, uint32_t bn, uint32_t bs,
q->free_f = free_f;
size = bn * sizeof(*q->blocks);
+#ifdef TEST_PERF_MEM
+ q->blocks = bbq_malloc_from_pool(q, size);
+#else
q->blocks = q->malloc_f(socket_id, size);
+
+#endif
if (q->blocks == NULL) {
bbq_errno = BBQ_ERR_ALLOC;
goto error;
@@ -411,12 +457,16 @@ void bbq_destory(struct bbq *q) {
return;
}
+#ifdef TEST_PERF_MEM
+ q->free_f(q->memory_pool.ptr, q->memory_pool.size);
+#else
for (uint32_t i = 0; i < q->bn; ++i) {
block_destory(q, &(q->blocks[i]));
}
q->free_f(q->blocks, q->bn * sizeof(*q->blocks));
q->free_f(q, sizeof(*q));
+#endif
}
#define BBQ_DATA_TYPE_SINGLE 0x0