diff options
| author | liuyu <[email protected]> | 2024-07-04 06:43:11 -0400 |
|---|---|---|
| committer | liuyu <[email protected]> | 2024-07-04 06:43:11 -0400 |
| commit | 194480a8ecce5ab11a8686bb3171fca0fb9efd55 (patch) | |
| tree | 461cb96b551f725a9776725427a2cf2f57224fa0 | |
| parent | 41ef56c43ebb69222d9350d3b9a0f132fc01131b (diff) | |
去掉默认malloc/free函数注册、去掉stdio.h的引用,去掉errno,保持位置无关代码
| -rw-r--r-- | bbq/include/bbq.h | 98 | ||||
| -rw-r--r-- | bbq/src/bbq.c | 145 | ||||
| -rw-r--r-- | bbq/tests/common/test_mix.c | 11 | ||||
| -rw-r--r-- | bbq/tests/common/test_mix.h | 4 | ||||
| -rw-r--r-- | bbq/tests/common/test_queue.c | 7 | ||||
| -rw-r--r-- | bbq/tests/unittest/ut_example.cc | 34 | ||||
| -rw-r--r-- | bbq/tests/unittest/ut_head_cursor.cc | 32 | ||||
| -rw-r--r-- | bbq/tests/unittest/ut_mix.cc | 7 | ||||
| -rw-r--r-- | bbq/tests/unittest/ut_multit.cc | 4 | ||||
| -rw-r--r-- | perf/benchmark/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | perf/benchmark/bcm_benchmark.c | 6 |
11 files changed, 131 insertions, 220 deletions
diff --git a/bbq/include/bbq.h b/bbq/include/bbq.h index 220a00f..485abbc 100644 --- a/bbq/include/bbq.h +++ b/bbq/include/bbq.h @@ -1,6 +1,6 @@ /* * @Author: [email protected] - * @LastEditTime: 2024-07-02 11:33:41 + * @LastEditTime: 2024-07-04 06:42:22 * @Describe: bbq(Block-based Bounded Queue)头文件 * 参考:https://www.usenix.org/system/files/atc22-wang-jiawei.pdf */ @@ -121,12 +121,12 @@ struct bbq { * - BBQ_F_DISABLE_STAT:关闭统计功能(默认) * @return * 非NULL:消息队列结构体指针,用于后续出队入队等操作。 - * NULL:创建失败,可通过bbq_errno分析具体错误原因: - * - BBQ_ERR_OUT_OF_RANGE:name或count参数超出范围 - * - BBQ_ERR_ALLOC:申请内存失败 - * - BBQ_ERR_POWER_OF_TWO:count不为2的n次方 - * - BBQ_ERR_INPUT_NULL:name传入空指针 - * - BBQ_ERR_STAT_NOT_SUPPORT:drop old模式下不支持 + * NULL:创建失败,可能存在的原因: + * - name或count参数超出范围 + * - 申请内存失败 + * - count不为2的n次方 + * - name传入空指针 + * - drop old模式下不支持 */ extern struct bbq *bbq_create(const char *name, uint32_t count, int socket_id, uint32_t flags, bbq_malloc_f malloc_f, bbq_free_f free_f); @@ -140,7 +140,7 @@ extern struct bbq *bbq_create(const char *name, uint32_t count, int socket_id, u * 指向入队指针的指针,如: * int *data = malloc(sizeof(int));*data = TEST_DATA; 传入&data * @return - * 成功返回0,失败返回小于0的错误码。可能存在以下错误码: + * 成功返回0,失败返回小于0的错误码: * - BBQ_ERR_INPUT_NULL:传入空指针 * - BBQ_ERR_FULL:队列已满 * - BBQ_ERR_BUSY:队列忙碌中 @@ -183,11 +183,10 @@ extern int bbq_dequeue(struct bbq *q, void **data); * @param[out] wait_consumed * 如果为非NULL,返回当前队列剩余的个数。注:该赋值可能会带来些许的性能损耗。 * @return - * 返回实际成功入队的个数。当入队返回0时,可通过bbq_errno分析具体错误原因: - * - BBQ_ERR_INPUT_NULL:传入空指针 - * - BBQ_ERR_FULL:队列已满 - * - BBQ_ERR_BUSY:队列忙碌中 - * - BBQ_ERR:其它错误 + * 返回实际成功入队的个数。如果始终返回0,可能存在的错误原因: + * - 传入空指针 + * - 队列已满 + * - 队列忙碌中 */ extern uint32_t bbq_enqueue_burst(struct bbq *q, void *const *obj_table, uint32_t n, uint32_t *wait_consumed); @@ -204,11 +203,10 @@ extern uint32_t bbq_enqueue_burst(struct bbq *q, void *const *obj_table, uint32_ * @param[out] wait_consumed * 如果为非NULL,返回当前队列中,已入队的个数。注:该赋值可能会带来些许的性能损耗 * @return - * 返回实际成功出队的个数。当出队返回0时,可通过bbq_errno分析具体错误原因: - * - BBQ_ERR_INPUT_NULL:传入空指针 - * - BBQ_ERR_EMPTY:队列已空 - * - BBQ_ERR_BUSY:队列忙碌中 - * - BBQ_ERR:其它错误 + * 返回实际出队的个数,如果始终返回0,可能存在的原因: + * - 传入空指针 + * - 队列已空 + * - 队列忙碌中 */ extern uint32_t bbq_dequeue_burst(struct bbq *q, void **obj_table, uint32_t n, uint32_t *wait_consumed); @@ -240,12 +238,12 @@ extern uint32_t bbq_dequeue_burst(struct bbq *q, void **obj_table, uint32_t n, u * - BBQ_F_DISABLE_STAT:关闭统计功能(默认) * @return * 非NULL:消息队列结构体指针,用于后续出队入队等操作。 - * NULL:创建失败。可通过bbq_errno分析具体错误原因: - * - BBQ_ERR_OUT_OF_RANGE:name或count参数超出范围 - * - BBQ_ERR_ALLOC:申请内存失败 - * - BBQ_ERR_POWER_OF_TWO:count不为2的n次方 - * - BBQ_ERR_INPUT_NULL:name传入空指针 - * - BBQ_ERR_STAT_NOT_SUPPORT:drop old模式下不支持 + * NULL:创建失败。可能存在的错误原因: + * - name或count参数超出范围 + * - 申请内存失败 + * - count不为2的n次方 + * - name传入空指针 + * - drop old模式下不支持 */ extern struct bbq *bbq_create_elem(const char *name, uint32_t count, size_t obj_size, int socket_id, uint32_t flags, @@ -259,11 +257,11 @@ extern struct bbq *bbq_create_elem(const char *name, uint32_t count, size_t obj_ * @param[in] data * 传入一级指针,如:int data = 1; 传入&data * @return - * 成功返回0,失败返回小于0的错误码。可通过bbq_errno分析具体错误原因: - * - BBQ_ERR_INPUT_NULL:传入空指针 - * - BBQ_ERR_FULL:队列已满 - * - BBQ_ERR_BUSY:队列忙碌中 - * - BBQ_ERR:其它错误 + * 成功返回0,失败返回小于0的错误码: + * - BBQ_ERR_INPUT_NULL:传入空指针 + * - BBQ_ERR_FULL:队列已满 + * - BBQ_ERR_BUSY:队列忙碌中 + * - BBQ_ERR:其它错误 */ extern int bbq_enqueue_elem(struct bbq *q, void const *data); @@ -275,7 +273,7 @@ extern int bbq_enqueue_elem(struct bbq *q, void const *data); * @param[in] data * 则传入一级指针,如:int data; 传入&data * @return - * 成功返回0,失败返回小于0的错误码。可通过bbq_errno分析具体错误原因: + * 成功返回0,失败返回小于0的错误码: * - BBQ_ERR_INPUT_NULL:传入空指针 * - BBQ_ERR_EMPTY:队列已空 * - BBQ_ERR_BUSY:队列忙碌中 @@ -296,11 +294,10 @@ extern int bbq_dequeue_elem(struct bbq *q, void *data); * @param[out] wait_consumed * 如果为非NULL,返回当前队列中,已入队的个数。。注:该赋值可能会带来些许的性能损耗 * @return - * 返回实际成功入队的个数。当入队返回0时,可通过bbq_errno分析具体错误原因: - * - BBQ_ERR_INPUT_NULL:传入空指针 - * - BBQ_ERR_FULL:队列已满 - * - BBQ_ERR_BUSY:队列忙碌中 - * - BBQ_ERR:其它错误 + * 返回实际成功入队的个数。如果始终返回0,可能存在的错误原因: + * - 传入空指针 + * - 队列已满 + * - 队列忙碌中 */ extern uint32_t bbq_enqueue_burst_elem(struct bbq *q, void const *obj_table, uint32_t n, uint32_t *wait_consumed); @@ -315,13 +312,12 @@ extern uint32_t bbq_enqueue_burst_elem(struct bbq *q, void const *obj_table, uin * @param[in] n * 尝试一次出队的个数 * @param[out] wait_consumed - * 如果为非NULL,返回当前队列中,已入队的个数。。注:该赋值可能会带来些许的性能损耗 + * 如果为非NULL,返回当前队列中,已入队的个数。注:该赋值可能会带来些许的性能损耗 * @return - * 成功返回0,失败返回小于0的错误码。可通过bbq_errno分析具体错误原因: - * - BBQ_ERR_INPUT_NULL:传入空指针 - * - BBQ_ERR_EMPTY:队列已空 - * - BBQ_ERR_BUSY:队列忙碌中 - * - BBQ_ERR:其它错误 + * 返回实际出队的个数,如果始终返回0,可能存在的原因: + * - 传入空指针 + * - 队列已空 + * - 队列忙碌中 */ extern uint32_t bbq_dequeue_burst_elem(struct bbq *q, void *obj_table, uint32_t n, uint32_t *wait_consumed); @@ -344,22 +340,6 @@ bool bbq_empty(struct bbq *q); */ extern void bbq_destory(struct bbq *q); -/** - * 打印消息队列信息(调试用)。 - * - * @param[in] q - * 队列指针 - */ -extern void bbq_debug_struct_print(struct bbq *q); - -/** - * 打印指定块信息(调试用)。 - * - * @param[in] q - * 队列指针 - */ -extern void bbq_debug_block_print(struct bbq *q, struct bbq_block *block); - // 错误码 #define BBQ_OK 0 // 成功 #define BBQ_ERR -1 // 通用错误,无法分类时使用 @@ -372,6 +352,4 @@ extern void bbq_debug_block_print(struct bbq *q, struct bbq_block *block); #define BBQ_ERR_FULL -101 // 队列已满(入队失败) #define BBQ_ERR_BUSY -102 // 队列忙碌中(入队或出队失败) #define BBQ_ERR_EMPTY -103 // 队列已空(出队失败) -#define BBQ_ERR_NOT_SUPPORT -104 // 不支持的操作 - -extern __thread int32_t bbq_errno;
\ No newline at end of file +#define BBQ_ERR_NOT_SUPPORT -104 // 不支持的操作
\ No newline at end of file diff --git a/bbq/src/bbq.c b/bbq/src/bbq.c index 44734e0..76af302 100644 --- a/bbq/src/bbq.c +++ b/bbq/src/bbq.c @@ -1,6 +1,6 @@ /* * @Author: liuyu - * @LastEditTime: 2024-07-02 11:34:25 + * @LastEditTime: 2024-07-04 06:37:09 * @Email: [email protected] * @Describe: bbq(Block-based Bounded Queue)实现 * 参考:https://www.usenix.org/system/files/atc22-wang-jiawei.pdf @@ -8,7 +8,6 @@ #include "bbq.h" #include <math.h> #include <numa.h> -#include <stdio.h> #include <string.h> // flags第1位控制入队时的数据拷贝策略,默认是"拷贝指针" @@ -25,13 +24,6 @@ // 避免无用参数的编译告警 #define AVOID_WARNING(param) ((void)param) -#define BBQ_ERR_LOG(fmt, ...) \ - do { \ - printf("\x1b[31m [ERR][%s:%d:%s]" fmt "\x1b[0m\n", __func__, __LINE__, __FILE__, ##__VA_ARGS__); \ - } while (0) - -__thread int32_t bbq_errno; - struct bbq_status { int32_t status; // 返回状态 uint32_t actual_burst; // 实际出/入队个数 @@ -110,31 +102,14 @@ static inline uint64_t bbq_atomic64_fetch_add(struct bbq_atomic64 *atomic, uint6 #ifdef BBQ_MEMORY #define BBQ_MEM_MAGIC 0xFF -struct bbq_memory_s { - uint64_t malloc_cnt; - uint64_t malloc_size; - uint64_t free_cnt; - uint64_t free_size; -}; -struct bbq_memory_s bbq_memory_g = {0}; #endif void *bbq_malloc_def_callback(int32_t socket_id __attribute__((unused)), size_t size) { -#ifdef BBQ_MEMORY - bbq_memory_g.malloc_cnt++; - bbq_memory_g.malloc_size += size; -#endif return aligned_alloc(BBQ_CACHE_LINE, size); } void bbq_free_def_callback(void *ptr, size_t size __attribute__((unused))) { -#ifdef BBQ_MEMORY - if (ptr) { - bbq_memory_g.free_cnt++; - bbq_memory_g.free_size += size; - } -#endif free(ptr); } @@ -175,18 +150,15 @@ static uint32_t bbq_block_number_calc(uint32_t entries) { int bbq_bnbs_calc(uint32_t entries, uint32_t *bn, uint32_t *bs) { if (bn == NULL || bs == NULL) { - bbq_errno = BBQ_ERR_INPUT_NULL; - return bbq_errno; + return BBQ_ERR_INPUT_NULL; } if (entries <= 1) { - bbq_errno = BBQ_ERR_OUT_OF_RANGE; - return bbq_errno; + return BBQ_ERR_OUT_OF_RANGE; } if (bbq_check_power_of_two(entries) == false) { - bbq_errno = BBQ_ERR_POWER_OF_TWO; - return bbq_errno; + return BBQ_ERR_POWER_OF_TWO; } *bn = bbq_block_number_calc(entries); @@ -224,8 +196,7 @@ int block_init(struct bbq *q, struct bbq_block *block, bool cursor_init) { #endif if (block->entries == NULL) { - bbq_errno = BBQ_ERR_ALLOC; - return bbq_errno; + return BBQ_ERR_ALLOC; } if (cursor_init) { @@ -276,22 +247,18 @@ static struct bbq *__bbq_create_bnbs(const char *name, uint32_t bn, uint32_t bs, int ret = 0; size_t size = 0; if (bbq_check_power_of_two(bn) == false) { - bbq_errno = BBQ_ERR_POWER_OF_TWO; return NULL; } if (bbq_check_power_of_two(bs) == false) { - bbq_errno = BBQ_ERR_POWER_OF_TWO; return NULL; } if (name == NULL) { - bbq_errno = BBQ_ERR_INPUT_NULL; return NULL; } if (strlen(name) >= BBQ_SYMBOL_MAX - 1 || obj_size == 0) { - bbq_errno = BBQ_ERR_OUT_OF_RANGE; return NULL; } @@ -301,16 +268,11 @@ static struct bbq *__bbq_create_bnbs(const char *name, uint32_t bn, uint32_t bs, } if (BBQ_F_CHK_DROP_OLD(flags) && BBQ_F_CHK_STAT_ENABLE(flags)) { - bbq_errno = BBQ_ERR_STAT_NOT_SUPPORT; return NULL; } - if (malloc_f == NULL) { - malloc_f = bbq_malloc_def_callback; - } - - if (free_f == NULL) { - free_f = bbq_free_def_callback; + if (malloc_f == NULL || free_f == NULL) { + return NULL; } uint32_t all_size = 0; @@ -321,7 +283,6 @@ static struct bbq *__bbq_create_bnbs(const char *name, uint32_t bn, uint32_t bs, #endif struct bbq *q = malloc_f(socket_id, all_size); if (q == NULL) { - bbq_errno = BBQ_ERR_ALLOC; return NULL; } @@ -331,12 +292,13 @@ static struct bbq *__bbq_create_bnbs(const char *name, uint32_t bn, uint32_t bs, q->memory_pool.off += sizeof(struct bbq); q->memory_pool.malloc_f = malloc_f; q->memory_pool.free_f = free_f; - - ret = snprintf(q->name, sizeof(q->name), "%s", name); q->bn = bn; q->bs = bs; q->entry_size = obj_size; q->socket_id = socket_id; + strncpy(q->name, name, sizeof(q->name) - 1); + q->name[sizeof(q->name) - 1] = '\0'; + if (BBQ_F_CHK_SP_ENQ(flags)) { q->prod_single = true; } @@ -348,7 +310,6 @@ static struct bbq *__bbq_create_bnbs(const char *name, uint32_t bn, uint32_t bs, size = bn * sizeof(*q->blocks); q->blocks = bbq_malloc_from_pool(q, size); if (q->blocks == NULL) { - bbq_errno = BBQ_ERR_ALLOC; goto error; } memset(q->blocks, 0, size); @@ -381,7 +342,6 @@ error: struct bbq *bbq_create_with_bnbs(const char *name, uint32_t bn, uint32_t bs, int socket_id, uint32_t flags, bbq_malloc_f malloc_f, bbq_free_f free_f) { - bbq_errno = BBQ_OK; return __bbq_create_bnbs(name, bn, bs, sizeof(void *), socket_id, flags | BBQ_F_COPY_PTR, malloc_f, free_f); } @@ -389,7 +349,6 @@ struct bbq *bbq_create_with_bnbs(const char *name, uint32_t bn, uint32_t bs, struct bbq *bbq_create_elem_with_bnbs(const char *name, uint32_t bn, uint32_t bs, size_t obj_size, int socket_id, uint32_t flags, bbq_malloc_f malloc_f, bbq_free_f free_f) { - bbq_errno = BBQ_OK; return __bbq_create_bnbs(name, bn, bs, obj_size, socket_id, flags | BBQ_F_COPY_VALUE, malloc_f, free_f); } @@ -397,7 +356,6 @@ struct bbq *bbq_create_elem_with_bnbs(const char *name, uint32_t bn, uint32_t bs struct bbq *bbq_create_elem(const char *name, uint32_t count, size_t obj_size, int socket_id, uint32_t flags, bbq_malloc_f malloc_f, bbq_free_f free_f) { - bbq_errno = BBQ_OK; uint32_t bn = 0; uint32_t bs = 0; @@ -410,7 +368,6 @@ struct bbq *bbq_create_elem(const char *name, uint32_t count, size_t obj_size, struct bbq *bbq_create(const char *name, uint32_t count, int socket_id, uint32_t flags, bbq_malloc_f malloc_f, bbq_free_f free_f) { - bbq_errno = BBQ_OK; uint32_t bn = 0; uint32_t bs = 0; @@ -454,7 +411,6 @@ void bbq_commit_entry(struct bbq *q, struct bbq_entry_desc *e, void const *data, break; } default: - bbq_errno = BBQ_ERR; break; } } else { @@ -467,7 +423,6 @@ void bbq_commit_entry(struct bbq *q, struct bbq_entry_desc *e, void const *data, break; case BBQ_DATA_TYPE_ARRAY_1D: default: - bbq_errno = BBQ_ERR; break; } } @@ -573,8 +528,7 @@ static struct bbq_status __bbq_enqueue(struct bbq *q, void const *data, uint32_t bool prod_single = q->prod_single; if (q == NULL || data == NULL) { - bbq_errno = BBQ_ERR_INPUT_NULL; - ret.status = bbq_errno; + ret.status = BBQ_ERR_INPUT_NULL; return ret; } @@ -599,21 +553,17 @@ static struct bbq_status __bbq_enqueue(struct bbq *q, void const *data, uint32_t } if (pstate == BBQ_NO_ENTRY) { - bbq_errno = BBQ_ERR_FULL; - ret.status = bbq_errno; + ret.status = BBQ_ERR_FULL; } else if (pstate == BBQ_NOT_AVAILABLE) { - bbq_errno = BBQ_ERR_BUSY; - ret.status = bbq_errno; + ret.status = BBQ_ERR_BUSY; } else { - bbq_errno = BBQ_ERR; - ret.status = bbq_errno; + ret.status = BBQ_ERR; } break; } default: - bbq_errno = BBQ_ERR; - ret.status = bbq_errno; + ret.status = BBQ_ERR; break; } @@ -626,13 +576,11 @@ static struct bbq_status __bbq_enqueue(struct bbq *q, void const *data, uint32_t } int bbq_enqueue(struct bbq *q, void *const *data) { - bbq_errno = BBQ_OK; struct bbq_status ret = __bbq_enqueue(q, data, 1, BBQ_DATA_TYPE_SINGLE, NULL); return ret.status; } int bbq_enqueue_elem(struct bbq *q, void const *data) { - bbq_errno = BBQ_OK; struct bbq_status ret = __bbq_enqueue(q, data, 1, BBQ_DATA_TYPE_SINGLE, NULL); return ret.status; } @@ -798,8 +746,7 @@ static struct bbq_status __bbq_dequeue(struct bbq *q, void *deq_data, uint32_t n bool cons_single = q->cons_single; struct bbq_status ret = {.status = 0, .actual_burst = 0}; if (q == NULL || deq_data == NULL) { - bbq_errno = BBQ_ERR_INPUT_NULL; - ret.status = bbq_errno; + ret.status = BBQ_ERR_INPUT_NULL; return ret; } @@ -823,23 +770,19 @@ static struct bbq_status __bbq_dequeue(struct bbq *q, void *deq_data, uint32_t n } break; case BBQ_NO_ENTRY: - bbq_errno = BBQ_ERR_EMPTY; - ret.status = bbq_errno; + ret.status = BBQ_ERR_EMPTY; break; case BBQ_NOT_AVAILABLE: - bbq_errno = BBQ_ERR_BUSY; - ret.status = bbq_errno; + ret.status = BBQ_ERR_BUSY; break; case BBQ_BLOCK_DONE: if (advance_chead(q, ch, state.vsn)) { continue; } - bbq_errno = BBQ_ERR_EMPTY; - ret.status = bbq_errno; + ret.status = BBQ_ERR_EMPTY; break; default: - bbq_errno = BBQ_ERR; - ret.status = bbq_errno; + ret.status = BBQ_ERR; break; } @@ -852,13 +795,11 @@ static struct bbq_status __bbq_dequeue(struct bbq *q, void *deq_data, uint32_t n } int bbq_dequeue(struct bbq *q, void **data) { - bbq_errno = BBQ_OK; struct bbq_status ret = __bbq_dequeue(q, data, 1, BBQ_DATA_TYPE_SINGLE, NULL); return ret.status; } int bbq_dequeue_elem(struct bbq *q, void *data) { - bbq_errno = BBQ_OK; struct bbq_status ret = __bbq_dequeue(q, data, 1, BBQ_DATA_TYPE_SINGLE, NULL); return ret.status; } @@ -874,12 +815,10 @@ static uint32_t bbq_max_burst(struct bbq *q, uint32_t n) { static uint32_t bbq_dequeue_burst_one_dimensional(struct bbq *q, void *obj_table, uint32_t n, uint32_t *wait_consumed) { if (q == NULL || obj_table == NULL) { - bbq_errno = BBQ_ERR_INPUT_NULL; return 0; } if (!BBQ_F_CHK_COPY_VALUE(q->flags)) { - bbq_errno = BBQ_ERR_NOT_SUPPORT; return 0; } @@ -903,7 +842,6 @@ static uint32_t bbq_dequeue_burst_one_dimensional(struct bbq *q, void *obj_table static uint32_t bbq_dequeue_burst_two_dimensional(struct bbq *q, void **obj_table, uint32_t n, uint32_t *wait_consumed) { if (q == NULL || obj_table == NULL) { - bbq_errno = BBQ_ERR_INPUT_NULL; return 0; } @@ -928,12 +866,10 @@ static uint32_t bbq_dequeue_burst_two_dimensional(struct bbq *q, void **obj_tabl /* 尝试一次入队多个数据,直到达到最大数量,或是入队失败 */ static uint32_t bbq_enqueue_burst_one_dimensional(struct bbq *q, void const *obj_table, uint32_t n, uint32_t *wait_consumed) { if (q == NULL || obj_table == NULL) { - bbq_errno = BBQ_ERR_INPUT_NULL; return 0; } if (!BBQ_F_CHK_COPY_VALUE(q->flags)) { - bbq_errno = BBQ_ERR_NOT_SUPPORT; return 0; } @@ -958,7 +894,6 @@ static uint32_t bbq_enqueue_burst_one_dimensional(struct bbq *q, void const *obj /* 尝试一次入队多个数据,直到达到最大数量,或是入队失败 */ static uint32_t bbq_enqueue_burst_two_dimensional(struct bbq *q, void *const *obj_table, uint32_t n, uint32_t *wait_consumed) { if (q == NULL || obj_table == NULL) { - bbq_errno = BBQ_ERR_INPUT_NULL; return 0; } @@ -981,47 +916,25 @@ static uint32_t bbq_enqueue_burst_two_dimensional(struct bbq *q, void *const *ob } uint32_t bbq_enqueue_burst_elem(struct bbq *q, void const *obj_table, uint32_t n, uint32_t *wait_consumed) { - bbq_errno = BBQ_OK; return bbq_enqueue_burst_one_dimensional(q, obj_table, n, wait_consumed); } uint32_t bbq_enqueue_burst_elem_two_dimensional(struct bbq *q, void *const *obj_table, uint32_t n, uint32_t *wait_consumed) { - bbq_errno = BBQ_OK; return bbq_enqueue_burst_two_dimensional(q, obj_table, n, wait_consumed); } uint32_t bbq_enqueue_burst(struct bbq *q, void *const *obj_table, uint32_t n, uint32_t *wait_consumed) { - bbq_errno = BBQ_OK; return bbq_enqueue_burst_two_dimensional(q, obj_table, n, wait_consumed); } uint32_t bbq_dequeue_burst(struct bbq *q, void **obj_table, uint32_t n, uint32_t *wait_consumed) { - bbq_errno = BBQ_OK; return bbq_dequeue_burst_two_dimensional(q, obj_table, n, wait_consumed); } uint32_t bbq_dequeue_burst_elem(struct bbq *q, void *obj_table, uint32_t n, uint32_t *wait_consumed) { - bbq_errno = BBQ_OK; return bbq_dequeue_burst_one_dimensional(q, obj_table, n, wait_consumed); } -bool bbq_malloc_free_equal() { -#ifdef BBQ_MEMORY - if (bbq_memory_g.malloc_cnt != bbq_memory_g.free_cnt) { - BBQ_ERR_LOG("malloc:%lu free:%lu, bbq mmalloc-free count not equal\n", - bbq_memory_g.malloc_cnt, bbq_memory_g.free_cnt); - return false; - } - - if (bbq_memory_g.malloc_size != bbq_memory_g.free_size) { - BBQ_ERR_LOG("malloc:%lu byte free:%lu byte, bbq mmalloc-free size not equal\n", - bbq_memory_g.malloc_size, bbq_memory_g.free_size); - return false; - } -#endif - return true; -} - bool bbq_debug_check_array_bounds(struct bbq *q) { #ifdef BBQ_MEMORY void *value = malloc(q->entry_size); @@ -1040,20 +953,9 @@ bool bbq_debug_check_array_bounds(struct bbq *q) { return true; } -void bbq_debug_memory_print() { -#ifdef BBQ_MEMORY - printf("bbq malloc:%lu free:%lu\n", - bbq_memory_g.malloc_cnt, - bbq_memory_g.free_cnt); - - if (bbq_malloc_free_equal()) { - printf("all memory free\n"); - } else { - BBQ_ERR_LOG("memory not all free"); - } -#endif -} - +#if 0 +#include <stdio.h> +/* 位置有关代码,需要-fPIC,这里注释掉用于调试 */ void bbq_debug_block_print(struct bbq *q, struct bbq_block *block) { bool prod_single = q->prod_single; bool cons_single = q->cons_single; @@ -1092,6 +994,7 @@ void bbq_debug_struct_print(struct bbq *q) { printf("block[%lu]\n", ch_idx); bbq_debug_block_print(q, &(q->blocks[ch_idx])); } +#endif #if 0 /* 根据实际head以及块上的游标推算出待消费的个数,该函数很影响性能 */ diff --git a/bbq/tests/common/test_mix.c b/bbq/tests/common/test_mix.c index 713dc99..ee0595e 100644 --- a/bbq/tests/common/test_mix.c +++ b/bbq/tests/common/test_mix.c @@ -1,7 +1,7 @@ /* * @Description: 描述信息 * @Date: 2024-05-25 10:55:48 - * @LastEditTime: 2024-06-27 23:39:44 + * @LastEditTime: 2024-07-04 06:21:46 */ #include "test_mix.h" #include "bbq.h" @@ -164,4 +164,13 @@ int test_setaffinity(int core_id) { } return BBQ_OK; +} + +void *test_malloc_def_callback(int32_t socket_id __attribute__((unused)), size_t size) { + return aligned_alloc(BBQ_CACHE_LINE, size); +} + +void test_free_def_callback(void *ptr, + size_t size __attribute__((unused))) { + free(ptr); }
\ No newline at end of file diff --git a/bbq/tests/common/test_mix.h b/bbq/tests/common/test_mix.h index d2299fe..20b8f77 100644 --- a/bbq/tests/common/test_mix.h +++ b/bbq/tests/common/test_mix.h @@ -1,6 +1,6 @@ /* * @Author: liuyu - * @LastEditTime: 2024-06-17 18:13:32 + * @LastEditTime: 2024-07-04 03:45:43 * @Email: [email protected] * @Describe: TODO */ @@ -148,6 +148,8 @@ extern void test_memory_counter_print(); extern void test_memory_counter_clear(); extern bool test_malloc_free_equal(); extern int test_setaffinity(int core_id); +extern void *test_malloc_def_callback(int32_t socket_id __attribute__((unused)), size_t size); +extern void test_free_def_callback(void *ptr, size_t size __attribute__((unused))); #define TEST_PTR_ARRAY_DATA_INIT(table, t_type, t_count) \ do { \ diff --git a/bbq/tests/common/test_queue.c b/bbq/tests/common/test_queue.c index 5d88997..10dd261 100644 --- a/bbq/tests/common/test_queue.c +++ b/bbq/tests/common/test_queue.c @@ -1,6 +1,6 @@ /* * @Author: liuyu - * @LastEditTime: 2024-07-02 23:07:37 + * @LastEditTime: 2024-07-04 03:46:35 * @Email: [email protected] * @Describe: TODO */ @@ -40,11 +40,12 @@ int test_queue_init_bbq(test_cfg *cfg, test_queue_s *q) { } if (cfg->ring.block_count == 0) { - q->ring = bbq_create("test_bbq", cfg->ring.entries_cnt, BBQ_SOCKET_ID_ANY, flags, NULL, NULL); + q->ring = bbq_create("test_bbq", cfg->ring.entries_cnt, BBQ_SOCKET_ID_ANY, flags, + test_malloc_def_callback, test_free_def_callback); } else { q->ring = bbq_create_with_bnbs("test_bbq", cfg->ring.block_count, cfg->ring.entries_cnt / cfg->ring.block_count, - BBQ_SOCKET_ID_ANY, flags, NULL, NULL); + BBQ_SOCKET_ID_ANY, flags, test_malloc_def_callback, test_free_def_callback); } if (q->ring == NULL) { diff --git a/bbq/tests/unittest/ut_example.cc b/bbq/tests/unittest/ut_example.cc index 6d2c94c..5f48476 100644 --- a/bbq/tests/unittest/ut_example.cc +++ b/bbq/tests/unittest/ut_example.cc @@ -1,6 +1,6 @@ /* * @Author: liuyu - * @LastEditTime: 2024-07-02 03:49:00 + * @LastEditTime: 2024-07-04 03:47:48 * @Email: [email protected] * @Describe: 简单的测试用例,测试基本功能 */ @@ -10,7 +10,6 @@ extern "C" { #include "test_mix.h" #include "test_queue.h" #include "ut.h" -extern bool bbq_malloc_free_equal(); extern bool bbq_debug_check_array_bounds(struct bbq *q); extern void bbq_struct_print(struct bbq *q); extern uint32_t bbq_enqueue_burst_elem_two_dimensional(struct bbq *q, void *const *obj_table, uint32_t n, uint32_t *wait_consumed); @@ -36,7 +35,6 @@ class bbq_example : public testing::Test { TEST_PTR_ARRAY_DATA_DESTORY(enq_table2, BUF_CNT); // 2.内存泄漏检测 - EXPECT_TRUE(bbq_malloc_free_equal()); EXPECT_TRUE(test_malloc_free_equal()); } @@ -52,7 +50,8 @@ TEST_F(bbq_example, single_retry_new_cp_ptr) { uint16_t *deq_data = NULL; // 创建队列 - struct bbq *q = bbq_create("test_bbq", BUF_CNT, BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, NULL, NULL); + struct bbq *q = bbq_create("test_bbq", BUF_CNT, BBQ_SOCKET_ID_ANY, + BBQ_F_RETRY_NEW, test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); // 空队出队失败 @@ -97,7 +96,9 @@ TEST_F(bbq_example, single_retry_new_cp_value) { uint16_t deq_data; // 创建队列 - struct bbq *q = bbq_create_elem("test_bbq", BUF_CNT, sizeof(uint16_t), BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, NULL, NULL); + struct bbq *q = bbq_create_elem("test_bbq", BUF_CNT, sizeof(uint16_t), + BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); // 空队出队失败 @@ -144,7 +145,9 @@ TEST_F(bbq_example, single_drop_old_cp_pointer) { uint64_t second_cnt = 1000; // 创建队列 - struct bbq *q = bbq_create("test_bbq", BUF_CNT, BBQ_SOCKET_ID_ANY, BBQ_F_DROP_OLD, NULL, NULL); + struct bbq *q = bbq_create("test_bbq", BUF_CNT, BBQ_SOCKET_ID_ANY, + BBQ_F_DROP_OLD, test_malloc_def_callback, + test_free_def_callback); ASSERT_NE(q, nullptr); EXPECT_LT(second_cnt, q->bs * q->bn); @@ -198,7 +201,9 @@ TEST_F(bbq_example, single_drop_old_cp_value) { uint64_t second_cnt = 1000; // 创建队列 - struct bbq *q = bbq_create_elem("test_bbq", BUF_CNT, sizeof(uint16_t), BBQ_SOCKET_ID_ANY, BBQ_F_DROP_OLD, NULL, NULL); + struct bbq *q = bbq_create_elem("test_bbq", BUF_CNT, sizeof(uint16_t), + BBQ_SOCKET_ID_ANY, BBQ_F_DROP_OLD, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); EXPECT_LT(second_cnt, q->bs * q->bn); @@ -254,7 +259,9 @@ TEST_F(bbq_example, burst_retry_new_cp_value) { uint32_t wait_consumed = 0; // 创建队列 - q = bbq_create_elem("test_bbq", BUF_CNT, sizeof(uint16_t), BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW | BBQ_F_ENABLE_STAT, NULL, NULL); + q = bbq_create_elem("test_bbq", BUF_CNT, sizeof(uint16_t), + BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW | BBQ_F_ENABLE_STAT, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); EXPECT_LT(first_cnt, q->bn * q->bs); @@ -304,7 +311,9 @@ TEST_F(bbq_example, burst_retry_new_cp_pointer) { uint16_t **deq_table2 = (uint16_t **)test_malloc(TEST_MODULE_DATA, sizeof(uint16_t *) * BUF_CNT); // 创建队列 - q = bbq_create("test_bbq", BUF_CNT, BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW | BBQ_F_ENABLE_STAT, NULL, NULL); + q = bbq_create("test_bbq", BUF_CNT, BBQ_SOCKET_ID_ANY, + BBQ_F_RETRY_NEW | BBQ_F_ENABLE_STAT, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); EXPECT_LT(first_cnt, q->bn * q->bs); @@ -353,7 +362,8 @@ TEST_F(bbq_example, burst_drop_old_cp_pointer) { uint16_t **deq_table2 = (uint16_t **)test_malloc(TEST_MODULE_DATA, sizeof(uint16_t *) * BUF_CNT); // 创建队列 - q = bbq_create("test_bbq", BUF_CNT, BBQ_SOCKET_ID_ANY, BBQ_F_DROP_OLD, NULL, NULL); + q = bbq_create("test_bbq", BUF_CNT, BBQ_SOCKET_ID_ANY, BBQ_F_DROP_OLD, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); EXPECT_GT(second_cnt, q->bs); EXPECT_LT(second_cnt, q->bs * q->bn); @@ -402,7 +412,9 @@ TEST_F(bbq_example, burst_drop_old_cp_value) { uint16_t deq_table1[BUF_CNT] = {0}; // 创建队列 - q = bbq_create_elem("test_bbq", BUF_CNT, sizeof(uint16_t), BBQ_SOCKET_ID_ANY, BBQ_F_DROP_OLD, NULL, NULL); + q = bbq_create_elem("test_bbq", BUF_CNT, sizeof(uint16_t), + BBQ_SOCKET_ID_ANY, BBQ_F_DROP_OLD, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); EXPECT_GT(second_cnt, q->bs); EXPECT_LT(second_cnt, q->bs * q->bn); diff --git a/bbq/tests/unittest/ut_head_cursor.cc b/bbq/tests/unittest/ut_head_cursor.cc index 317c6ef..056ab75 100644 --- a/bbq/tests/unittest/ut_head_cursor.cc +++ b/bbq/tests/unittest/ut_head_cursor.cc @@ -1,6 +1,6 @@ /* * @Author: liuyu - * @LastEditTime: 2024-07-02 10:11:03 + * @LastEditTime: 2024-07-04 03:47:56 * @Email: [email protected] * @Describe: TODO */ @@ -8,7 +8,6 @@ extern "C" { #include "test_queue.h" #include "ut.h" -extern bool bbq_malloc_free_equal(); extern bool bbq_debug_check_array_bounds(struct bbq *q); extern struct bbq *bbq_create_elem_with_bnbs(const char *name, uint32_t bn, uint32_t bs, size_t obj_size, int socket_id, uint32_t flags, @@ -27,7 +26,6 @@ class bbq_head_cursor : public testing::Test { // 继承了 testing::Test virtual void TearDown() override { // std::cout << "exit from TearDown" << std::endl; // 内存泄漏检测 - EXPECT_TRUE(bbq_malloc_free_equal()); EXPECT_TRUE(test_malloc_free_equal()); } }; @@ -73,7 +71,9 @@ TEST_F(bbq_head_cursor, init) { struct bbq *q; uint32_t bn = 2; uint32_t bs = 4; - q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, NULL, NULL); + q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), + BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); // 1.初始化状态,除了第一个block外其他块的4个游标都指向最后一个条目 @@ -106,7 +106,9 @@ void ut_produce_something(uint32_t produce_cnt) { EXPECT_GT(produce_cnt, 0); EXPECT_LE(produce_cnt, bs); - q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, NULL, NULL); + q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), + BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); // 生产produce_cnt @@ -165,7 +167,9 @@ void ut_produce_next_block(uint32_t over) { EXPECT_GT(over, 0); EXPECT_LT(over, bs); - q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, NULL, NULL); + q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), + BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); // 生产至第二块的第一个entry @@ -226,7 +230,9 @@ void ut_produce_all_loop(uint32_t loop) { int enqueue_data = TEST_DATA_MAGIC; int dequeue_data = 0; - q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, NULL, NULL); + q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), + BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); for (uint32_t cnt = 0; cnt < loop; cnt++) { @@ -282,7 +288,9 @@ TEST_F(bbq_head_cursor, retry_new_full_empty) { int tmp_data = 0; EXPECT_TRUE(data); - q = bbq_create_elem("test_bbq", entries_cnt, sizeof(int), BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW | BBQ_F_ENABLE_STAT, NULL, NULL); + q = bbq_create_elem("test_bbq", entries_cnt, sizeof(int), BBQ_SOCKET_ID_ANY, + BBQ_F_RETRY_NEW | BBQ_F_ENABLE_STAT, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); EXPECT_TRUE(bbq_empty(q)); @@ -417,7 +425,9 @@ TEST_F(bbq_head_cursor, drop_old_full_empty) { struct bbq *q; int tmp_data = 0; - q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), BBQ_SOCKET_ID_ANY, BBQ_F_DROP_OLD, NULL, NULL); + q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), + BBQ_SOCKET_ID_ANY, BBQ_F_DROP_OLD, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); // EXPECT_TRUE(bbq_empty(q)); @@ -468,7 +478,9 @@ TEST_F(bbq_head_cursor, drop_old_full_empty_cover) { EXPECT_EQ(over_cnt / bs, 1); int tmp_data = 0; - q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), BBQ_SOCKET_ID_ANY, BBQ_F_DROP_OLD, NULL, NULL); + q = bbq_create_elem_with_bnbs("test_bbq", bn, bs, sizeof(int), + BBQ_SOCKET_ID_ANY, BBQ_F_DROP_OLD, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); // EXPECT_TRUE(bbq_empty(q)); diff --git a/bbq/tests/unittest/ut_mix.cc b/bbq/tests/unittest/ut_mix.cc index 6369dd9..3a039b6 100644 --- a/bbq/tests/unittest/ut_mix.cc +++ b/bbq/tests/unittest/ut_mix.cc @@ -1,6 +1,6 @@ /* * @Author: liuyu - * @LastEditTime: 2024-07-02 10:12:33 + * @LastEditTime: 2024-07-04 03:48:34 * @Email: [email protected] * @Describe: bbq除了队列操作外,其他函数的测试 */ @@ -12,7 +12,6 @@ extern "C" { extern bool bbq_check_power_of_two(int n); extern unsigned bbq_ceil_log2(uint64_t x); extern uint64_t bbq_fetch_max(struct bbq_atomic64 *atomic, uint64_t upd, bool single); -extern bool bbq_malloc_free_equal(); extern bool test_malloc_free_equal(); extern int bbq_bnbs_calc(uint32_t entries, uint32_t *bn, uint32_t *bs); extern void bbq_atomic64_store(struct bbq_atomic64 *atomic, uint64_t value, bool single); @@ -30,7 +29,6 @@ class bbq_mix : public testing::Test { // 继承了 testing::Test virtual void TearDown() override { // std::cout << "exit from TearDown" << std::endl; // 内存泄漏检测 - EXPECT_TRUE(bbq_malloc_free_equal()); EXPECT_TRUE(test_malloc_free_equal()); } }; @@ -157,7 +155,8 @@ TEST_F(bbq_mix, bbq_block_number_calc) { TEST_F(bbq_mix, bbq_cache_line) { // 创建队列 - struct bbq *q = bbq_create("test_bbq", 4096, BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, NULL, NULL); + struct bbq *q = bbq_create("test_bbq", 4096, BBQ_SOCKET_ID_ANY, BBQ_F_RETRY_NEW, + test_malloc_def_callback, test_free_def_callback); ASSERT_NE(q, nullptr); // 首地址64字节对齐 diff --git a/bbq/tests/unittest/ut_multit.cc b/bbq/tests/unittest/ut_multit.cc index 31ea246..754f911 100644 --- a/bbq/tests/unittest/ut_multit.cc +++ b/bbq/tests/unittest/ut_multit.cc @@ -1,6 +1,6 @@ /* * @Author: liuyu - * @LastEditTime: 2024-06-30 21:56:17 + * @LastEditTime: 2024-07-04 03:43:33 * @Email: [email protected] * @Describe: TODO */ @@ -10,7 +10,6 @@ extern "C" { #include "test_mix.h" #include "test_queue.h" #include "ut.h" -extern bool bbq_malloc_free_equal(); extern bool test_malloc_free_equal(); bool bbq_debug_check_array_bounds(struct bbq *q); } @@ -26,7 +25,6 @@ class multit : public testing::Test { // 继承了 testing::Test virtual void TearDown() override { // std::cout << "exit from TearDown" << std::endl; // 内存泄漏检测 - EXPECT_TRUE(bbq_malloc_free_equal()); EXPECT_TRUE(test_malloc_free_equal()); } }; diff --git a/perf/benchmark/CMakeLists.txt b/perf/benchmark/CMakeLists.txt index 0c3848d..1e5a318 100644 --- a/perf/benchmark/CMakeLists.txt +++ b/perf/benchmark/CMakeLists.txt @@ -10,4 +10,5 @@ list(APPEND SRC_LIST ${SRC_COMMON_LIST}) set(EXECUTABLE_OUTPUT_PATH ${EXEC_PATH}) add_executable(benchmark ${SRC_LIST}) # 添加可执行程序 -target_link_libraries(benchmark dl iniparser pthread rte_ring rte_eal rte_kvargs rte_telemetry rmind_ringbuf bbq m) # 链接库
\ No newline at end of file +target_link_libraries(benchmark dl iniparser pthread rte_ring rte_eal rte_kvargs rte_telemetry rmind_ringbuf bbq m) # 链接库 +#target_link_libraries(benchmark dl iniparser pthread dpdk rmind_ringbuf bbq m) # 链接库 diff --git a/perf/benchmark/bcm_benchmark.c b/perf/benchmark/bcm_benchmark.c index 5407d2b..13b5028 100644 --- a/perf/benchmark/bcm_benchmark.c +++ b/perf/benchmark/bcm_benchmark.c @@ -1,6 +1,6 @@ /* * @Author: liuyu - * @LastEditTime: 2024-06-27 07:11:39 + * @LastEditTime: 2024-07-04 03:43:44 * @Email: [email protected] * @Describe: TODO */ @@ -16,9 +16,6 @@ #include <sys/prctl.h> #include <unistd.h> -extern void bbq_debug_memory_print(); -extern bool bbq_malloc_free_equal(); - void bcm_report_printf(test_cfg *cfg, test_merge_data *data, test_exit_data **raw_data, uint32_t thread_cnt, test_thread_type_e ttype) { char name[10] = {0}; double latency_ns = 0; @@ -151,7 +148,6 @@ int main(int argc, char *argv[]) { test_free(TEST_MODULE_BCM, exit_data); test_threads_destory(&test_info, threads); test_queue_destory(&q); - bbq_debug_memory_print(); test_memory_counter_print(); return 0; |
