From 8f11f8381d23bffc8395524725e20fde9b004740 Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Fri, 13 Oct 2023 12:00:39 +0800 Subject: [PATCH] perf: Optimize ATOMIC_READ --- common/include/utils.h | 3 ++- common/src/timestamp.cpp | 4 ++-- platform/src/main.cpp | 20 +++++++++++++------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/common/include/utils.h b/common/include/utils.h index 58a7e44..7ad3749 100644 --- a/common/include/utils.h +++ b/common/include/utils.h @@ -22,7 +22,8 @@ extern "C" #define ATOMIC_INC(x) __atomic_fetch_add(x, 1, __ATOMIC_RELAXED) #define ATOMIC_DEC(x) __atomic_fetch_sub(x, 1, __ATOMIC_RELAXED) -#define ATOMIC_READ(x) __atomic_fetch_add(x, 0, __ATOMIC_RELAXED) +//#define ATOMIC_READ(x) __atomic_fetch_add(x, 0, __ATOMIC_RELAXED) +#define ATOMIC_READ(x) __atomic_load_n(x, __ATOMIC_RELAXED) #define ATOMIC_ZERO(x) __atomic_fetch_and(x, 0, __ATOMIC_RELAXED) #define ATOMIC_ADD(x, y) __atomic_fetch_add(x, y, __ATOMIC_RELAXED) #define ATOMIC_SET(x, y) __atomic_store_n(x, y, __ATOMIC_RELAXED) diff --git a/common/src/timestamp.cpp b/common/src/timestamp.cpp index e9b9a55..4760816 100644 --- a/common/src/timestamp.cpp +++ b/common/src/timestamp.cpp @@ -41,7 +41,7 @@ void timestamp_update(struct timestamp *ts) clock_gettime(CLOCK_MONOTONIC, &temp); uint64_t current_timestamp_ms = temp.tv_sec * 1000 + temp.tv_nsec / 1000000; - __atomic_store_n(&ts->timestamp_ms, current_timestamp_ms, __ATOMIC_RELEASE); + ATOMIC_SET(&ts->timestamp_ms, current_timestamp_ms); } uint64_t timestamp_update_interval_ms(struct timestamp *ts) @@ -51,5 +51,5 @@ uint64_t timestamp_update_interval_ms(struct timestamp *ts) uint64_t timestamp_get_msec(struct timestamp *ts) { - return __atomic_load_n(&ts->timestamp_ms, __ATOMIC_ACQUIRE); + return ATOMIC_READ(&ts->timestamp_ms); } diff --git a/platform/src/main.cpp b/platform/src/main.cpp index 0a51942..66562d6 100644 --- a/platform/src/main.cpp +++ b/platform/src/main.cpp @@ -65,16 +65,21 @@ static void *worker_thread_cycle(void *arg) struct packet_io *handle = thread_ctx->ref_io; struct sce_ctx *sce_ctx = thread_ctx->ref_sce_ctx; struct timestamp *ts = sce_ctx->ts; + struct sf_metrics *sf_metrics = thread_ctx->sf_metrics; + struct session_table *session_table = thread_ctx->session_table; + struct thread_metrics *thread_metrics = &thread_ctx->thread_metrics; + struct global_metrics *global_metrics = thread_ctx->ref_global_metrics; + int thread_index = thread_ctx->thread_index; int timeout_ms = 0; int n_pkt_recv_from_nf = 0; int n_pkt_recv_from_endp = 0; char thread_name[16]; uint64_t sf_metrics_last_send_ts = timestamp_get_msec(ts); - uint64_t sf_metrics_send_interval = sf_metrics_get_interval(thread_ctx->sf_metrics) * 1000; + uint64_t sf_metrics_send_interval = sf_metrics_get_interval(sf_metrics) * 1000; ATOMIC_SET(&thread_ctx->thread_is_runing, 1); - snprintf(thread_name, sizeof(thread_name), "sce:worker-%d", thread_ctx->thread_index); + snprintf(thread_name, sizeof(thread_name), "sce:worker-%d", thread_index); prctl(PR_SET_NAME, (unsigned long long)thread_name, NULL, NULL, NULL); if (packet_io_thread_init(handle, thread_ctx) != 0) @@ -82,7 +87,7 @@ static void *worker_thread_cycle(void *arg) goto error_out; } - LOG_INFO("%s: worker thread %d is running", LOG_TAG_SCE, thread_ctx->thread_index); + LOG_INFO("%s: worker thread %d is running", LOG_TAG_SCE, thread_index); while (!ATOMIC_READ(&is_need_stop)) { @@ -99,17 +104,18 @@ static void *worker_thread_cycle(void *arg) packet_io_thread_wait(handle, thread_ctx, timeout_ms); } + global_metrics_update(global_metrics, thread_metrics, thread_index); + if (ATOMIC_READ(&thread_ctx->session_table_need_reset) > 0) { - session_table_reset(thread_ctx->session_table); + session_table_reset(session_table); ATOMIC_ZERO(&thread_ctx->session_table_need_reset); } if (timestamp_get_msec(ts) - sf_metrics_last_send_ts >= sf_metrics_send_interval) { - global_metrics_update(thread_ctx->ref_global_metrics, &thread_ctx->thread_metrics, thread_ctx->thread_index); - sf_metrics_send(thread_ctx->sf_metrics); - sf_metrics_reset(thread_ctx->sf_metrics); + sf_metrics_send(sf_metrics); + sf_metrics_reset(sf_metrics); sf_metrics_last_send_ts = timestamp_get_msec(ts); } }