From 232d3470d85aa15900c8d1471e86690ac3a53815 Mon Sep 17 00:00:00 2001 From: anzizhuo Date: Sat, 22 Nov 2025 17:23:39 +0800 Subject: [PATCH] compile upgrade --- src/ubsocket/CMakeLists.txt | 25 +++- src/ubsocket/brpc/CMakeLists.txt | 5 +- src/ubsocket/brpc/qbuf_list.h | 73 ++++++++++++ src/ubsocket/urpc_util.c | 85 ++++++++++++++ src/ubsocket/urpc_util.h | 191 +++++++++++++++++++++++++++++++ src/ubsocket/util_vlog.c | 103 +++++++++++++++++ src/ubsocket/util_vlog.h | 84 ++++++++++++++ 7 files changed, 559 insertions(+), 7 deletions(-) create mode 100644 src/ubsocket/brpc/qbuf_list.h create mode 100644 src/ubsocket/urpc_util.c create mode 100644 src/ubsocket/urpc_util.h create mode 100644 src/ubsocket/util_vlog.c create mode 100644 src/ubsocket/util_vlog.h diff --git a/src/ubsocket/CMakeLists.txt b/src/ubsocket/CMakeLists.txt index 94f86fe..e8316fa 100644 --- a/src/ubsocket/CMakeLists.txt +++ b/src/ubsocket/CMakeLists.txt @@ -1,8 +1,25 @@ -include_directories(${CMAKE_CURRENT_LIST_DIR}/../util) -include_directories(${CMAKE_CURRENT_LIST_DIR}/../include/umq) -include_directories(${CMAKE_CURRENT_LIST_DIR}/../umq) -include_directories(${CMAKE_CURRENT_LIST_DIR}/../umq/qbuf) +cmake_minimum_required(VERSION 3.12.1) +project(ubsocket) +set(CMAKE_FLAGS_PUBILC " -Wall -Werror -Wfloat-equal -Wtrampolines -g -rdynamic -Wl,-z,noexecstack,-z,relro,-z,now \ +-fno-strict-aliasing -fstack-protector-strong -fPIE -pie -fPIC -Wextra -Wno-unused-parameter -Wno-missing-field-initializers \ +-Wno-type-limits -fno-common") + +set(CMAKE_BUILD_TYPE "Release") +set(CMAKE_C_FLAGS_RELEASE "-O2 -D_FORTIFY_SOURCE=2") +set(CMAKE_CXX_FLAGS_RELEASE "-O2 -D_FORTIFY_SOURCE=2") + +set(CMAKE_FLAGS_ARM64 " -march=armv8-a+crc -DUB_ARCH_ARM64") +set(CMAKE_FLAGS_x86_64 " -msse4.2 -DUB_ARCH_X86_64") +if("${CMAKE_HOST_SYSTEM_PROCESSOR}" STREQUAL "aarch64") + set(CMAKE_C_FLAGS "${CMAKE_FLAGS_PUBILC} ${CMAKE_FLAGS_ARM64} -std=gnu11") + set(CMAKE_CXX_FLAGS "${CMAKE_FLAGS_PUBILC} ${CMAKE_FLAGS_ARM64} -std=c++11") +else() + set(CMAKE_C_FLAGS "${CMAKE_FLAGS_PUBILC} ${CMAKE_FLAGS_x86_64} -std=gnu11") + set(CMAKE_CXX_FLAGS "${CMAKE_FLAGS_PUBILC} ${CMAKE_FLAGS_x86_64} -std=c++11") +endif() + +include_directories(/usr/include/ub/umdk/urpc/umq/) aux_source_directory(${CMAKE_CURRENT_LIST_DIR} COMMON_RPC_ADPT_SRC) add_subdirectory(brpc) diff --git a/src/ubsocket/brpc/CMakeLists.txt b/src/ubsocket/brpc/CMakeLists.txt index 0bf70dc..fae8533 100644 --- a/src/ubsocket/brpc/CMakeLists.txt +++ b/src/ubsocket/brpc/CMakeLists.txt @@ -1,10 +1,9 @@ -include_directories(${CMAKE_SOURCE_DIR}/lib/umq/qbuf) include_directories(${CMAKE_CURRENT_LIST_DIR}/../) aux_source_directory(${CMAKE_CURRENT_LIST_DIR} BRPC_ADPT_SRC) -add_library(rpc_adapter_brpc SHARED $ ${COMMON_RPC_ADPT_SRC} ${BRPC_ADPT_SRC}) +add_library(rpc_adapter_brpc SHARED ${COMMON_RPC_ADPT_SRC} ${BRPC_ADPT_SRC}) set_property(TARGET rpc_adapter_brpc PROPERTY CMAKE_CXX_STANDARD 11) -target_link_libraries(rpc_adapter_brpc PUBLIC dl boundscheck PRIVATE common_util umq) +target_link_libraries(rpc_adapter_brpc PUBLIC dl boundscheck PRIVATE umq) install(TARGETS rpc_adapter_brpc DESTINATION /usr/lib64/) \ No newline at end of file diff --git a/src/ubsocket/brpc/qbuf_list.h b/src/ubsocket/brpc/qbuf_list.h new file mode 100644 index 0000000..b01d336 --- /dev/null +++ b/src/ubsocket/brpc/qbuf_list.h @@ -0,0 +1,73 @@ +/* + * SPDX-License-Identifier: MIT + * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved. + * Description: umq qbuf Singly-linked list + */ +#ifndef UMQ_BUF_LIST_H +#define UMQ_BUF_LIST_H + +#include "umq_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +typedef struct umq_buf_list { + umq_buf_t *first; +} umq_buf_list_t; + +#define QBUF_LIST_FIRST(head) ((head)->first) + +#define QBUF_LIST_INIT(head) do { \ + QBUF_LIST_FIRST((head)) = NULL; \ +} while (0) + +#define QBUF_LIST_EMPTY(head) ((head)->first == NULL) + +#define QBUF_LIST_NEXT(element) ((element)->qbuf_next) + +#define QBUF_LIST_INSERT_HEAD(head, element) do { \ + QBUF_LIST_NEXT((element)) = QBUF_LIST_FIRST((head)); \ + QBUF_LIST_FIRST((head)) = (element); \ +} while (0) + +#define QBUF_LIST_INSERT_AFTER(element1, element2) do { \ + QBUF_LIST_NEXT(element2) = QBUF_LIST_NEXT(element1); \ + QBUF_LIST_NEXT(element1) = (element2); \ +} while (0) + +#define QBUF_LIST_FOR_EACH(element, head) \ + for ((element) = QBUF_LIST_FIRST((head)); \ + (element); \ + (element) = QBUF_LIST_NEXT(element)) + +#define QBUF_LIST_FOR_EACH_SAFE(element, head, next) \ + for ((element) = QBUF_LIST_FIRST((head)); \ + (element) && ((next) = QBUF_LIST_NEXT(element), 1); \ + (element) = (next)) + +#define QBUF_LIST_REMOVE_HEAD(head) do { \ + QBUF_LIST_FIRST((head)) = QBUF_LIST_NEXT(QBUF_LIST_FIRST((head))); \ +} while (0) + +#define QBUF_LIST_REMOVE_AFTER(element) do { \ + QBUF_LIST_NEXT(element) = QBUF_LIST_NEXT(QBUF_LIST_NEXT(element)); \ +} while (0) + +#define QBUF_LIST_REMOVE(head, element, type) do { \ + if (QBUF_LIST_FIRST((head)) == (element)) { \ + QBUF_LIST_REMOVE_HEAD((head)); \ + } else if (QBUF_LIST_FIRST((head)) != NULL) { \ + struct type *curelement = QBUF_LIST_FIRST(head); \ + while (QBUF_LIST_NEXT(curelement) != (element)) \ + curelement = QBUF_LIST_NEXT(curelement); \ + QBUF_LIST_REMOVE_AFTER(curelement); \ + } \ +} while (0) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/ubsocket/urpc_util.c b/src/ubsocket/urpc_util.c new file mode 100644 index 0000000..88931e4 --- /dev/null +++ b/src/ubsocket/urpc_util.c @@ -0,0 +1,85 @@ +/* + * SPDX-License-Identifier: MIT + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved. + * Description: urpc util function + * Create: 2024-11-04 + */ + +#include +#include +#include + +#include "urpc_util.h" + +#define URPC_RANDOM_SEED_SIZE 48 + +#if defined(__x86_64__) +#include "ub_get_clock.h" + +static uint64_t g_urpc_cpu_hz; + +uint64_t urpc_get_cpu_hz(void) +{ + // ub get_cpu_mhz will cost 200+ms + if (URPC_UNLIKELY(g_urpc_cpu_hz == 0)) { + g_urpc_cpu_hz = (uint64_t)(get_cpu_mhz(false) * US_PER_SEC); + } + + return g_urpc_cpu_hz; +} +#elif defined(__aarch64__) +uint64_t urpc_get_cpu_hz(void) +{ + // cost x ns + return urpc_get_cpu_hz_aarch64(); +} +#else +#warning urpc_get_cpu_hz not implemented +#endif + +static int read_random_seed(int fd, uint8_t *seed, int seed_len) +{ + ssize_t read_bytes; + int total_bytes = seed_len; + while (total_bytes > 0) { + read_bytes = read(fd, seed, (size_t)total_bytes); + if ((read_bytes > 0) && (read_bytes <= total_bytes)) { + total_bytes -= read_bytes; + seed += read_bytes; + } else if ((errno != EINTR) || ((read_bytes == 0) && (total_bytes != 0))) { + return -1; + } + } + return 0; +} + +int urpc_rand_seed_init(void) +{ + uint8_t seed[URPC_RANDOM_SEED_SIZE]; + int fd = open("/dev/random", O_RDONLY); + if (fd < 0) { + return -1; + } + + int ret = read_random_seed(fd, seed, URPC_RANDOM_SEED_SIZE); + (void)close(fd); + if (ret != 0) { + return -1; + } + + RAND_seed(seed, URPC_RANDOM_SEED_SIZE); + if (RAND_status() != 1) { + return -1; + } + + return 0; +} + +int urpc_rand_generate(uint8_t *buf, uint32_t num) +{ + if (RAND_priv_bytes(buf, num) != 1) { + return -1; + } + + return 0; +} diff --git a/src/ubsocket/urpc_util.h b/src/ubsocket/urpc_util.h new file mode 100644 index 0000000..1ac756f --- /dev/null +++ b/src/ubsocket/urpc_util.h @@ -0,0 +1,191 @@ +/* + * SPDX-License-Identifier: MIT + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved. + * Description: urpc util + */ +#ifndef URPC_UTIL_H +#define URPC_UTIL_H + +#include +#include +#include +#include +#include + +#ifdef __cplusplus +#include + +#else +#include +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#if __GNUC__ && !defined(__CHECKER__) +#define URPC_LIKELY(CONDITION) __builtin_expect(!!(CONDITION), 1) +#define URPC_UNLIKELY(CONDITION) __builtin_expect(!!(CONDITION), 0) +#else +#define URPC_LIKELY(CONDITION) (!!(CONDITION)) +#define URPC_UNLIKELY(CONDITION) (!!(CONDITION)) +#endif + +#ifndef ALWAYS_INLINE +#define ALWAYS_INLINE inline __attribute__((always_inline)) +#endif + +#ifndef EID_FMT +#define EID_FMT "%2.2x%2.2x:%2.2x%2.2x:%2.2x%2.2x:%2.2x%2.2x:%2.2x%2.2x:%2.2x%2.2x:%2.2x%2.2x:%2.2x%2.2x" +#endif + +#ifndef EID_RAW_ARGS +#define EID_RAW_ARGS(eid) eid[0], eid[1], eid[2], eid[3], eid[4], eid[5], eid[6], eid[7], eid[8], eid[9], \ + eid[10], eid[11], eid[12], eid[13], eid[14], eid[15] +#endif + +#ifndef EID_ARGS +#define EID_ARGS(eid) EID_RAW_ARGS((eid).raw) +#endif + +#define OBJ_OFFSETOF(obj_ptr, field) offsetof(typeof(*(obj_ptr)), field) + +/* get the size of field in the struct_type. */ +#define SIZEOF_FIELD(struct_type, field) (sizeof(((struct_type *)NULL)->field)) + +/* get the offset of the end of field in the struct. */ +#define OFFSET_OF_FIELD_END(struct_type, field) (offsetof(struct_type, field) + SIZEOF_FIELD(struct_type, field)) + +/* get the structure object from the pointer of the given field by struct type */ +#define CONTAINER_OF_FIELD(field_ptr, struct_type, field) \ + ((struct_type *)(void *)((char *)(field_ptr) - offsetof(struct_type, field))) + +/* get the structure object from the pointer of the given field by type of obj_ptr */ +#define OBJ_CONTAINING(field_ptr, obj_ptr, field) \ + ((typeof(obj_ptr))(void *)((char *)(field_ptr)-OBJ_OFFSETOF(obj_ptr, field))) + +/* get the structure object from the pointer of the given field by struct type, + * Then assign the structure object to the obj_ptr + */ +#define ASSIGN_CONTAINER_PTR(obj_ptr, field_ptr, field) ((obj_ptr) = OBJ_CONTAINING(field_ptr, obj_ptr, field), (void)0) + +/* initialize obj_ptr and ASSIGN_CONTAINER_PTR to avoid compile warnings. */ +#define INIT_CONTAINER_PTR(obj_ptr, field_ptr, field) \ + ((obj_ptr) = NULL, ASSIGN_CONTAINER_PTR(obj_ptr, field_ptr, field)) + +#define URPC_RUNNING (1) +#define URPC_INVALID_TASK_ID (-1) + +enum constructor_priority { + CONSTRUCTOR_PRIORITY_GLOBAL = 101, + CONSTRUCTOR_PRIORITY_DRIVER, + CONSTRUCTOR_PRIORITY_FEATURE +}; + +enum destructor_priority { + DESTRUCTOR_PRIORITY_GLOBAL = 101, + DESTRUCTOR_PRIORITY_DRIVER, + DESTRUCTOR_PRIORITY_FEATURE +}; + +#define URPC_CONSTRUCTOR(func, priority) static void __attribute__((constructor(priority), used)) func(void) +#define URPC_DESTRUCTOR(func, priority) static void __attribute__((destructor(priority), used)) func(void) + +#ifndef DIV_ROUND_UP +#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) +#endif + +#ifndef MAX +#define MAX(x, y) ((x) > (y) ? (x) : (y)) +#endif +#ifndef MIN +#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#endif + +#define BITS_PER_LONG 64 +#define BITS_PER_LONG_SHIFT 6 +#define BITS_PER_LONG_MASK (BITS_PER_LONG - 1) +#define BITS_TO_LONGS(cnt) DIV_ROUND_UP((cnt), BITS_PER_LONG) + +#define MS_PER_SEC 1000 +#define US_PER_SEC 1000000 +#define NS_PER_SEC 1000000000UL +#define NS_PER_MS 1000000 +#define NS_PER_US 1000 + +/* Undefined when x == 0 */ +static inline int urpc_count_trail_zero(uint64_t x) +{ + return (__builtin_constant_p(x <= UINT32_MAX) && x <= UINT32_MAX + ? __builtin_ctz((unsigned int)x) + : __builtin_ctzll(x)); +} + +static inline unsigned long __attribute__((always_inline)) urpc_ffs(unsigned long word) +{ + return (unsigned long)((unsigned long)__builtin_ffsl(word) - 1UL); +} + +#if defined(__x86_64__) +static inline uint64_t urpc_get_cpu_cycles(void) +{ + uint32_t low, high; + uint64_t val; + asm volatile("rdtsc" : "=a"(low), "=d"(high)); + val = high; + // 32 is bit size of high + val = (val << 32) | low; + return val; +} +#elif defined(__aarch64__) +static inline uint64_t urpc_get_cpu_hz_aarch64(void) +{ + uint64_t freq; + asm volatile("mrs %0, cntfrq_el0" : "=r"(freq)); + return freq; +} + +// cpu cycles may not be monotonically increasing in multithread +static inline uint64_t urpc_get_cpu_cycles(void) +{ + uint64_t tsc; + asm volatile("mrs %0, cntvct_el0" : "=r"(tsc)); + return tsc; +} +#else +#warning urpc_get_cpu_cycles not implemented +#endif + +uint64_t urpc_get_cpu_hz(void); + +// get timestamp in seconds +static inline uint32_t get_timestamp(void) +{ + struct timespec tc; + (void)clock_gettime(CLOCK_MONOTONIC, &tc); + return tc.tv_sec; +} + +static inline uint64_t get_timestamp_ns(void) +{ + struct timespec tc; + (void)clock_gettime(CLOCK_MONOTONIC, &tc); + return (uint64_t)(tc.tv_sec * NS_PER_SEC + tc.tv_nsec); +} + +static inline uint64_t get_timestamp_ms(void) +{ + struct timespec tc; + (void)clock_gettime(CLOCK_MONOTONIC, &tc); + return (uint64_t)(tc.tv_sec * MS_PER_SEC + tc.tv_nsec / NS_PER_MS); +} + +int urpc_rand_seed_init(void); +int urpc_rand_generate(uint8_t *buf, uint32_t num); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/ubsocket/util_vlog.c b/src/ubsocket/util_vlog.c new file mode 100644 index 0000000..556a8a6 --- /dev/null +++ b/src/ubsocket/util_vlog.c @@ -0,0 +1,103 @@ +/* + * SPDX-License-Identifier: MIT + * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved. + * Description: Provide vlog module + * Create: 2025-07-29 + */ + +#include + +#include "urpc_util.h" +#include "util_vlog.h" + +typedef struct util_vlog_level_def { + const char *output_name; + const char **alias_names; +} util_vlog_level_def_t; + +static const char *g_emerg_alias_names_def[] = { "emerg", "emergency", "0", NULL }; +static const char *g_alert_alias_names_def[] = { "alert", "1", NULL }; +static const char *g_crit_alias_names_def[] = { "crit", "critical", "2", NULL }; +static const char *g_err_alias_names_def[] = { "err", "error", "3", NULL }; +static const char *g_warn_alias_names_def[] = { "warn", "warning", "4", NULL }; +static const char *g_notice_alias_names_def[] = { "notice", "5", NULL }; +static const char *g_info_alias_names_def[] = { "info", "informational", "6", NULL }; +static const char *g_debug_alias_names_def[] = { "debug", "7", NULL }; + +static const util_vlog_level_def_t g_util_vlog_level_def[] = { + { "EMERG", g_emerg_alias_names_def }, + { "ALERT", g_alert_alias_names_def }, + { "CRIT", g_crit_alias_names_def }, + { "ERR", g_err_alias_names_def }, + { "WARN", g_warn_alias_names_def }, + { "NOTICE", g_notice_alias_names_def }, + { "INFO", g_info_alias_names_def }, + { "DEBUG", g_debug_alias_names_def }, +}; + +static bool next_print_cycle(uint64_t *last_time, uint32_t time_interval) +{ + uint64_t now_time = urpc_get_cpu_cycles(); + if (((now_time - *last_time) * MS_PER_SEC / urpc_get_cpu_hz()) >= time_interval) { + *last_time = now_time; + return true; + } + return false; +} + +bool util_vlog_limit(util_vlog_ctx_t *ctx, uint32_t *print_count, uint64_t *last_time) +{ + if (ctx->rate_limited.interval_ms == 0) { + return true; + } + + if (next_print_cycle(last_time, ctx->rate_limited.interval_ms)) { + *print_count = 0; + } + + if (*print_count < ctx->rate_limited.num) { + *print_count += 1; + return true; + } + + return false; +} + +void util_vlog_output( + util_vlog_ctx_t *ctx, util_vlog_level_t level, const char *function, int line, const char *format, ...) +{ + char log_msg[UTIL_VLOG_SIZE]; + int len = snprintf(log_msg, UTIL_VLOG_SIZE, "%s|%s[%d]|", ctx->vlog_name, function, line); + if (len < 0) { + return; + } + + va_list va; + va_start(va, format); + len = vsnprintf(&log_msg[len], (size_t)(UTIL_VLOG_SIZE - len), format, va); + va_end(va); + if (len < 0) { + return; + } + + ctx->vlog_output_func(level, log_msg); +} + +util_vlog_level_t util_vlog_level_converter_from_str(const char *str, util_vlog_level_t default_level) +{ + int array_size = (int)sizeof(g_util_vlog_level_def) / (int)sizeof(g_util_vlog_level_def[0]); + for (int i = 0; i < array_size; ++i) { + for (const char **name_def = g_util_vlog_level_def[i].alias_names; *name_def != NULL; ++name_def) { + if (strcasecmp(str, *name_def) == 0) { + return (util_vlog_level_t)i; + } + } + } + + return default_level; +} + +const char *util_vlog_level_converter_to_str(util_vlog_level_t level) +{ + return g_util_vlog_level_def[level].output_name; +} \ No newline at end of file diff --git a/src/ubsocket/util_vlog.h b/src/ubsocket/util_vlog.h new file mode 100644 index 0000000..9c8222b --- /dev/null +++ b/src/ubsocket/util_vlog.h @@ -0,0 +1,84 @@ +/* + * SPDX-License-Identifier: MIT + * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved. + * Description: Provide vlog module + * Create: 2025-07-29 + */ + +#ifndef UTIL_VLOG_H +#define UTIL_VLOG_H + +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define UTIL_VLOG_SIZE (1024) +#define UTIL_VLOG_NAME_STR_LEN (64) +#define UTIL_VLOG_PRINT_PERIOD_MS (1000) +#define UTIL_VLOG_PRINT_TIMES (10) + +typedef enum util_vlog_level { + UTIL_VLOG_LEVEL_EMERG = 0, + UTIL_VLOG_LEVEL_ALERT, + UTIL_VLOG_LEVEL_CRIT, + UTIL_VLOG_LEVEL_ERR, + UTIL_VLOG_LEVEL_WARN, + UTIL_VLOG_LEVEL_NOTICE, + UTIL_VLOG_LEVEL_INFO, + UTIL_VLOG_LEVEL_DEBUG, + UTIL_VLOG_LEVEL_MAX, +} util_vlog_level_t; + +typedef struct util_vlog_ctx { + util_vlog_level_t level; + char vlog_name[UTIL_VLOG_NAME_STR_LEN]; + void (*vlog_output_func)(int level, char *log_msg); + struct { + uint32_t interval_ms; // rate-limited log output interval. If the value is 0, rate is not limited. + uint32_t num; // maximum number of rate-limited logs that can be output in a specified interval. + } rate_limited; +} util_vlog_ctx_t; + +#define UTIL_VLOG(__ctx, __level, ...) \ + if (!util_vlog_drop(__ctx, __level)) { \ + util_vlog_output(__ctx, __level, __func__, __LINE__, ##__VA_ARGS__); \ + } + +#define UTIL_LIMIT_VLOG(__ctx, __level, ...) \ + if (!util_vlog_drop(__ctx, __level)) { \ + static uint32_t count_call = 0; \ + static uint64_t last_time = 0; \ + if (util_vlog_limit(__ctx, &count_call, &last_time)) { \ + util_vlog_output(__ctx, __level, __func__, __LINE__, ##__VA_ARGS__); \ + } \ + } + +static inline void default_vlog_output(int level, char *log_msg) +{ + syslog(level, "%s", log_msg); +} + +bool util_vlog_limit(util_vlog_ctx_t *ctx, uint32_t *print_count, uint64_t *last_time); + +static inline bool util_vlog_drop(util_vlog_ctx_t *ctx, util_vlog_level_t level) +{ + return level > ctx->level; +} + +void util_vlog_output( + util_vlog_ctx_t *ctx, util_vlog_level_t level, const char *function, int line, const char *format, ...); + +util_vlog_level_t util_vlog_level_converter_from_str(const char *str, util_vlog_level_t default_level); +const char *util_vlog_level_converter_to_str(util_vlog_level_t level); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file -- Gitee