perf: Optimize ATOMIC_READ

This commit is contained in:
luwenpeng
2023-10-13 12:00:39 +08:00
parent 8d1a9b3be5
commit 8f11f8381d
3 changed files with 17 additions and 10 deletions

View File

@@ -22,7 +22,8 @@ extern "C"
#define ATOMIC_INC(x) __atomic_fetch_add(x, 1, __ATOMIC_RELAXED) #define ATOMIC_INC(x) __atomic_fetch_add(x, 1, __ATOMIC_RELAXED)
#define ATOMIC_DEC(x) __atomic_fetch_sub(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_ZERO(x) __atomic_fetch_and(x, 0, __ATOMIC_RELAXED)
#define ATOMIC_ADD(x, y) __atomic_fetch_add(x, y, __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) #define ATOMIC_SET(x, y) __atomic_store_n(x, y, __ATOMIC_RELAXED)

View File

@@ -41,7 +41,7 @@ void timestamp_update(struct timestamp *ts)
clock_gettime(CLOCK_MONOTONIC, &temp); clock_gettime(CLOCK_MONOTONIC, &temp);
uint64_t current_timestamp_ms = temp.tv_sec * 1000 + temp.tv_nsec / 1000000; 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) 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) uint64_t timestamp_get_msec(struct timestamp *ts)
{ {
return __atomic_load_n(&ts->timestamp_ms, __ATOMIC_ACQUIRE); return ATOMIC_READ(&ts->timestamp_ms);
} }

View File

@@ -65,16 +65,21 @@ static void *worker_thread_cycle(void *arg)
struct packet_io *handle = thread_ctx->ref_io; struct packet_io *handle = thread_ctx->ref_io;
struct sce_ctx *sce_ctx = thread_ctx->ref_sce_ctx; struct sce_ctx *sce_ctx = thread_ctx->ref_sce_ctx;
struct timestamp *ts = sce_ctx->ts; 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 timeout_ms = 0;
int n_pkt_recv_from_nf = 0; int n_pkt_recv_from_nf = 0;
int n_pkt_recv_from_endp = 0; int n_pkt_recv_from_endp = 0;
char thread_name[16]; char thread_name[16];
uint64_t sf_metrics_last_send_ts = timestamp_get_msec(ts); 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); 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); prctl(PR_SET_NAME, (unsigned long long)thread_name, NULL, NULL, NULL);
if (packet_io_thread_init(handle, thread_ctx) != 0) if (packet_io_thread_init(handle, thread_ctx) != 0)
@@ -82,7 +87,7 @@ static void *worker_thread_cycle(void *arg)
goto error_out; 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)) 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); 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) 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); ATOMIC_ZERO(&thread_ctx->session_table_need_reset);
} }
if (timestamp_get_msec(ts) - sf_metrics_last_send_ts >= sf_metrics_send_interval) 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(sf_metrics);
sf_metrics_send(thread_ctx->sf_metrics); sf_metrics_reset(sf_metrics);
sf_metrics_reset(thread_ctx->sf_metrics);
sf_metrics_last_send_ts = timestamp_get_msec(ts); sf_metrics_last_send_ts = timestamp_get_msec(ts);
} }
} }