From: Costa Tsaousis Date: Tue, 16 Aug 2016 16:14:14 +0000 (+0300) Subject: switched atomic operations from legacy __sync to new __atomic ones X-Git-Tag: v1.3.0~23^2~34 X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d78c82a0b0a4c232a74599ccecba698c196801a0;p=netdata.git switched atomic operations from legacy __sync to new __atomic ones --- diff --git a/src/common.h b/src/common.h index b9aa6772..a6e85034 100644 --- a/src/common.h +++ b/src/common.h @@ -60,17 +60,15 @@ #include #endif +#ifndef __ATOMIC_SEQ_CST +#define NETDATA_NO_ATOMIC_INSTRUCTIONS 1 +#endif + #ifdef __GNUC__ #define GCC_VERSION (__GNUC__ * 10000 \ + __GNUC_MINOR__ * 100 \ + __GNUC_PATCHLEVEL__) -// test for gcc version < 4.1.2 -// to disable gcc atomic operations -#if GCC_VERSION < 40102 -#define NETDATA_NO_ATOMIC_INSTRUCTIONS 1 -#endif - #if __x86_64__ || __ppc64__ #define ENVIRONMENT64 #else diff --git a/src/global_statistics.c b/src/global_statistics.c index ad89ab54..d6277f2d 100644 --- a/src/global_statistics.c +++ b/src/global_statistics.c @@ -26,21 +26,19 @@ void finished_web_request_statistics(uint64_t dt, uint64_t content_size, uint64_t compressed_content_size) { #ifndef NETDATA_NO_ATOMIC_INSTRUCTIONS +#warning using atomic operations uint64_t old_web_usec_max = global_statistics.web_usec_max; - while(dt > old_web_usec_max) { - if(__sync_bool_compare_and_swap(&global_statistics.web_usec_max, old_web_usec_max, dt)) - old_web_usec_max = dt; - else - old_web_usec_max = global_statistics.web_usec_max; - } - - __sync_fetch_and_add(&global_statistics.web_requests, 1); - __sync_fetch_and_add(&global_statistics.web_usec, dt); - __sync_fetch_and_add(&global_statistics.bytes_received, bytes_received); - __sync_fetch_and_add(&global_statistics.bytes_sent, bytes_sent); - __sync_fetch_and_add(&global_statistics.content_size, content_size); - __sync_fetch_and_add(&global_statistics.compressed_content_size, compressed_content_size); + while(dt > old_web_usec_max) + __atomic_compare_exchange(&global_statistics.web_usec_max, &old_web_usec_max, &dt, 1, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); + + __atomic_fetch_add(&global_statistics.web_requests, 1, __ATOMIC_SEQ_CST); + __atomic_fetch_add(&global_statistics.web_usec, dt, __ATOMIC_SEQ_CST); + __atomic_fetch_add(&global_statistics.bytes_received, bytes_received, __ATOMIC_SEQ_CST); + __atomic_fetch_add(&global_statistics.bytes_sent, bytes_sent, __ATOMIC_SEQ_CST); + __atomic_fetch_add(&global_statistics.content_size, content_size, __ATOMIC_SEQ_CST); + __atomic_fetch_add(&global_statistics.compressed_content_size, compressed_content_size, __ATOMIC_SEQ_CST); #else + #warning NOT using atomic operations if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED) global_statistics_lock(); @@ -61,7 +59,7 @@ void finished_web_request_statistics(uint64_t dt, void web_client_connected(void) { #ifndef NETDATA_NO_ATOMIC_INSTRUCTIONS - __sync_fetch_and_add(&global_statistics.connected_clients, 1); + __atomic_fetch_add(&global_statistics.connected_clients, 1, __ATOMIC_SEQ_CST); #else if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED) global_statistics_lock(); @@ -75,7 +73,7 @@ void web_client_connected(void) { void web_client_disconnected(void) { #ifndef NETDATA_NO_ATOMIC_INSTRUCTIONS - __sync_fetch_and_sub(&global_statistics.connected_clients, 1); + __atomic_fetch_sub(&global_statistics.connected_clients, 1, __ATOMIC_SEQ_CST); #else if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED) global_statistics_lock(); @@ -90,17 +88,20 @@ void web_client_disconnected(void) { inline void global_statistics_copy(struct global_statistics *gs, uint8_t options) { #ifndef NETDATA_NO_ATOMIC_INSTRUCTIONS - gs->connected_clients = __sync_fetch_and_add(&global_statistics.connected_clients, 0); - gs->web_requests = __sync_fetch_and_add(&global_statistics.web_requests, 0); - gs->web_usec = __sync_fetch_and_add(&global_statistics.web_usec, 0); - gs->web_usec_max = __sync_fetch_and_add(&global_statistics.web_usec_max, 0); - gs->bytes_received = __sync_fetch_and_add(&global_statistics.bytes_received, 0); - gs->bytes_sent = __sync_fetch_and_add(&global_statistics.bytes_sent, 0); - gs->content_size = __sync_fetch_and_add(&global_statistics.content_size, 0); - gs->compressed_content_size = __sync_fetch_and_add(&global_statistics.compressed_content_size, 0); - - if(options & GLOBAL_STATS_RESET_WEB_USEC_MAX) - __sync_bool_compare_and_swap(&global_statistics.web_usec_max, gs->web_usec_max, 0); + gs->connected_clients = __atomic_fetch_add(&global_statistics.connected_clients, 0, __ATOMIC_SEQ_CST); + gs->web_requests = __atomic_fetch_add(&global_statistics.web_requests, 0, __ATOMIC_SEQ_CST); + gs->web_usec = __atomic_fetch_add(&global_statistics.web_usec, 0, __ATOMIC_SEQ_CST); + gs->web_usec_max = __atomic_fetch_add(&global_statistics.web_usec_max, 0, __ATOMIC_SEQ_CST); + gs->bytes_received = __atomic_fetch_add(&global_statistics.bytes_received, 0, __ATOMIC_SEQ_CST); + gs->bytes_sent = __atomic_fetch_add(&global_statistics.bytes_sent, 0, __ATOMIC_SEQ_CST); + gs->content_size = __atomic_fetch_add(&global_statistics.content_size, 0, __ATOMIC_SEQ_CST); + gs->compressed_content_size = __atomic_fetch_add(&global_statistics.compressed_content_size, 0, __ATOMIC_SEQ_CST); + + if(options & GLOBAL_STATS_RESET_WEB_USEC_MAX) { + uint64_t n = 0; + __atomic_compare_exchange(&global_statistics.web_usec_max, &gs->web_usec_max, &n, 1, __ATOMIC_SEQ_CST, + __ATOMIC_SEQ_CST); + } #else global_statistics_lock();