From b87064de5d81410bad797b41437487deadc96469 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Thu, 6 Feb 2020 14:22:13 +0100 Subject: [PATCH] memaslap: fix atomics --- .gitignore | 1 + clients/ms_atomic.h | 10 ++++++++-- clients/ms_conn.c | 2 +- clients/ms_memslap.h | 33 +++++++++++++++++---------------- clients/ms_setting.h | 4 ++-- clients/ms_thread.c | 2 +- 6 files changed, 30 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 7b63c298..6febe384 100644 --- a/.gitignore +++ b/.gitignore @@ -168,3 +168,4 @@ tests/var/ tmp_chroot unittests/unittests venv/ +/infer-out/ diff --git a/clients/ms_atomic.h b/clients/ms_atomic.h index 045f0a61..6ec990c0 100644 --- a/clients/ms_atomic.h +++ b/clients/ms_atomic.h @@ -12,6 +12,12 @@ #ifndef CLIENTS_MS_ATOMIC_H #define CLIENTS_MS_ATOMIC_H +#if HAVE_C_STDATOMIC +# define ATOMIC _Atomic +#else +# define ATOMIC volatile +#endif + #if defined(__SUNPRO_C) # define _KERNEL # include @@ -57,7 +63,7 @@ # define atomic_dec_size(X) atomic_fetch_sub(X, 1) /* The same as above, but these return the new value instead of void */ # define ATOMIC_ADD_FETCH_DECL(T) \ -static inline T atomic_add_fetch_##T(volatile T *ptr, T add) { \ +static inline T atomic_add_fetch_##T(ATOMIC T *ptr, T add) { \ T des, cur = atomic_load(ptr); \ do { \ des = cur + add; \ @@ -65,7 +71,7 @@ static inline T atomic_add_fetch_##T(volatile T *ptr, T add) { \ return des; \ } # define ATOMIC_SUB_FETCH_DECL(T) \ -T atomic_sub_fetch_##T(volatile T *ptr) { \ +T atomic_sub_fetch_##T(ATOMIC T *ptr) { \ T des, cur = atomic_load(ptr); \ do { \ des = cur - 1; \ diff --git a/clients/ms_conn.c b/clients/ms_conn.c index 9c4b7f53..71353d5b 100644 --- a/clients/ms_conn.c +++ b/clients/ms_conn.c @@ -66,7 +66,7 @@ static uint64_t key_prefix_seq= KEY_PREFIX_BASE; /* global increasing counter, generating request id for UDP */ -static volatile uint32_t udp_request_id= 0; +static ATOMIC uint32_t udp_request_id= 0; extern pthread_key_t ms_thread_key; diff --git a/clients/ms_memslap.h b/clients/ms_memslap.h index 1c1b29eb..9575a810 100644 --- a/clients/ms_memslap.h +++ b/clients/ms_memslap.h @@ -25,6 +25,7 @@ #include #include "ms_stats.h" +#include "ms_atomic.h" #ifdef __cplusplus extern "C" { @@ -71,22 +72,22 @@ typedef struct statistic /* global status statistic structure */ typedef struct stats { - volatile uint32_t active_conns; /* active connections */ - size_t bytes_read; /* read bytes */ - size_t bytes_written; /* written bytes */ - size_t obj_bytes; /* object bytes */ - size_t pre_cmd_get; /* previous total get command count */ - size_t pre_cmd_set; /* previous total set command count */ - size_t cmd_get; /* current total get command count */ - size_t cmd_set; /* current total set command count */ - size_t get_misses; /* total objects of get miss */ - size_t vef_miss; /* total objects of verification miss */ - size_t vef_failed; /* total objects of verification failed */ - size_t unexp_unget; /* total objects which is unexpired but not get */ - size_t exp_get; /* total objects which is expired but get */ - volatile size_t pkt_disorder; /* disorder packages of UDP */ - size_t pkt_drop; /* packages dropped of UDP */ - size_t udp_timeout; /* how many times timeout of UDP happens */ + ATOMIC uint32_t active_conns; /* active connections */ + ATOMIC size_t bytes_read; /* read bytes */ + ATOMIC size_t bytes_written; /* written bytes */ + ATOMIC size_t obj_bytes; /* object bytes */ + ATOMIC size_t pre_cmd_get; /* previous total get command count */ + ATOMIC size_t pre_cmd_set; /* previous total set command count */ + ATOMIC size_t cmd_get; /* current total get command count */ + ATOMIC size_t cmd_set; /* current total set command count */ + ATOMIC size_t get_misses; /* total objects of get miss */ + ATOMIC size_t vef_miss; /* total objects of verification miss */ + ATOMIC size_t vef_failed; /* total objects of verification failed */ + ATOMIC size_t unexp_unget; /* total objects which is unexpired but not get */ + ATOMIC size_t exp_get; /* total objects which is expired but get */ + ATOMIC size_t pkt_disorder; /* disorder packages of UDP */ + ATOMIC size_t pkt_drop; /* packages dropped of UDP */ + ATOMIC size_t udp_timeout; /* how many times timeout of UDP happens */ } ms_stats_t; /* lock adapter */ diff --git a/clients/ms_setting.h b/clients/ms_setting.h index 4bab2b99..9db956c9 100644 --- a/clients/ms_setting.h +++ b/clients/ms_setting.h @@ -53,8 +53,8 @@ typedef struct mcd_server int srv_port; /* server port */ /* for calculating how long the server disconnects */ - volatile uint32_t disconn_cnt; /* number of disconnections count */ - volatile uint32_t reconn_cnt; /* number of reconnections count */ + ATOMIC uint32_t disconn_cnt; /* number of disconnections count */ + ATOMIC uint32_t reconn_cnt; /* number of reconnections count */ struct timeval disconn_time; /* start time of disconnection */ struct timeval reconn_time; /* end time of reconnection */ } ms_mcd_server_t; diff --git a/clients/ms_thread.c b/clients/ms_thread.c index 264cca7c..f9f52bfb 100644 --- a/clients/ms_thread.c +++ b/clients/ms_thread.c @@ -184,7 +184,7 @@ static int ms_setup_thread(ms_thread_ctx_t *thread_ctx) ms_thread->thread_ctx= thread_ctx; ms_thread->nactive_conn= thread_ctx->nconns; ms_thread->initialized= false; - static volatile uint32_t cnt= 0; + static ATOMIC uint32_t cnt= 0; gettimeofday(&ms_thread->startup_time, NULL); -- 2.30.2