@cryptotaxi247 / netdata-1 / commits / d78c82a0b

switched atomic operations from legacy __sync to new __atomic ones

Costa Tsaousis committed Aug 16, 2016 at 19:14 UTC d78c82a0b0a4c232a74599ccecba698c196801a0
2 files changed +31 -32
src/common.h
+4 -6
@@ -60,17 +60,15 @@
60 #include <zlib.h>
61 #endif
62
63 +#ifndef __ATOMIC_SEQ_CST
64 +#define NETDATA_NO_ATOMIC_INSTRUCTIONS 1
65 +#endif
66 +
67 #ifdef __GNUC__
68 #define GCC_VERSION (__GNUC__ * 10000 \
69 + __GNUC_MINOR__ * 100 \
70 + __GNUC_PATCHLEVEL__)
71
68 -// test for gcc version < 4.1.2
69 -// to disable gcc atomic operations
70 -#if GCC_VERSION < 40102
71 -#define NETDATA_NO_ATOMIC_INSTRUCTIONS 1
72 -#endif
73 -
72 #if __x86_64__ || __ppc64__
73 #define ENVIRONMENT64
74 #else
src/global_statistics.c
+27 -26
@@ -26,21 +26,19 @@ void finished_web_request_statistics(uint64_t dt,
26 uint64_t content_size,
27 uint64_t compressed_content_size) {
28 #ifndef NETDATA_NO_ATOMIC_INSTRUCTIONS
29 +#warning using atomic operations
30 uint64_t old_web_usec_max = global_statistics.web_usec_max;
30 - while(dt > old_web_usec_max) {
31 - if(__sync_bool_compare_and_swap(&global_statistics.web_usec_max, old_web_usec_max, dt))
32 - old_web_usec_max = dt;
33 - else
34 - old_web_usec_max = global_statistics.web_usec_max;
35 - }
36 -
37 - __sync_fetch_and_add(&global_statistics.web_requests, 1);
38 - __sync_fetch_and_add(&global_statistics.web_usec, dt);
39 - __sync_fetch_and_add(&global_statistics.bytes_received, bytes_received);
40 - __sync_fetch_and_add(&global_statistics.bytes_sent, bytes_sent);
41 - __sync_fetch_and_add(&global_statistics.content_size, content_size);
42 - __sync_fetch_and_add(&global_statistics.compressed_content_size, compressed_content_size);
31 + while(dt > old_web_usec_max)
32 + __atomic_compare_exchange(&global_statistics.web_usec_max, &old_web_usec_max, &dt, 1, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
33 +
34 + __atomic_fetch_add(&global_statistics.web_requests, 1, __ATOMIC_SEQ_CST);
35 + __atomic_fetch_add(&global_statistics.web_usec, dt, __ATOMIC_SEQ_CST);
36 + __atomic_fetch_add(&global_statistics.bytes_received, bytes_received, __ATOMIC_SEQ_CST);
37 + __atomic_fetch_add(&global_statistics.bytes_sent, bytes_sent, __ATOMIC_SEQ_CST);
38 + __atomic_fetch_add(&global_statistics.content_size, content_size, __ATOMIC_SEQ_CST);
39 + __atomic_fetch_add(&global_statistics.compressed_content_size, compressed_content_size, __ATOMIC_SEQ_CST);
40 #else
41 + #warning NOT using atomic operations
42 if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
43 global_statistics_lock();
44
@@ -61,7 +59,7 @@ void finished_web_request_statistics(uint64_t dt,
59
60 void web_client_connected(void) {
61 #ifndef NETDATA_NO_ATOMIC_INSTRUCTIONS
64 - __sync_fetch_and_add(&global_statistics.connected_clients, 1);
62 + __atomic_fetch_add(&global_statistics.connected_clients, 1, __ATOMIC_SEQ_CST);
63 #else
64 if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
65 global_statistics_lock();
@@ -75,7 +73,7 @@ void web_client_connected(void) {
73
74 void web_client_disconnected(void) {
75 #ifndef NETDATA_NO_ATOMIC_INSTRUCTIONS
78 - __sync_fetch_and_sub(&global_statistics.connected_clients, 1);
76 + __atomic_fetch_sub(&global_statistics.connected_clients, 1, __ATOMIC_SEQ_CST);
77 #else
78 if (web_server_mode == WEB_SERVER_MODE_MULTI_THREADED)
79 global_statistics_lock();
@@ -90,17 +88,20 @@ void web_client_disconnected(void) {
88
89 inline void global_statistics_copy(struct global_statistics *gs, uint8_t options) {
90 #ifndef NETDATA_NO_ATOMIC_INSTRUCTIONS
93 - gs->connected_clients = __sync_fetch_and_add(&global_statistics.connected_clients, 0);
94 - gs->web_requests = __sync_fetch_and_add(&global_statistics.web_requests, 0);
95 - gs->web_usec = __sync_fetch_and_add(&global_statistics.web_usec, 0);
96 - gs->web_usec_max = __sync_fetch_and_add(&global_statistics.web_usec_max, 0);
97 - gs->bytes_received = __sync_fetch_and_add(&global_statistics.bytes_received, 0);
98 - gs->bytes_sent = __sync_fetch_and_add(&global_statistics.bytes_sent, 0);
99 - gs->content_size = __sync_fetch_and_add(&global_statistics.content_size, 0);
100 - gs->compressed_content_size = __sync_fetch_and_add(&global_statistics.compressed_content_size, 0);
101 -
102 - if(options & GLOBAL_STATS_RESET_WEB_USEC_MAX)
103 - __sync_bool_compare_and_swap(&global_statistics.web_usec_max, gs->web_usec_max, 0);
91 + gs->connected_clients = __atomic_fetch_add(&global_statistics.connected_clients, 0, __ATOMIC_SEQ_CST);
92 + gs->web_requests = __atomic_fetch_add(&global_statistics.web_requests, 0, __ATOMIC_SEQ_CST);
93 + gs->web_usec = __atomic_fetch_add(&global_statistics.web_usec, 0, __ATOMIC_SEQ_CST);
94 + gs->web_usec_max = __atomic_fetch_add(&global_statistics.web_usec_max, 0, __ATOMIC_SEQ_CST);
95 + gs->bytes_received = __atomic_fetch_add(&global_statistics.bytes_received, 0, __ATOMIC_SEQ_CST);
96 + gs->bytes_sent = __atomic_fetch_add(&global_statistics.bytes_sent, 0, __ATOMIC_SEQ_CST);
97 + gs->content_size = __atomic_fetch_add(&global_statistics.content_size, 0, __ATOMIC_SEQ_CST);
98 + gs->compressed_content_size = __atomic_fetch_add(&global_statistics.compressed_content_size, 0, __ATOMIC_SEQ_CST);
99 +
100 + if(options & GLOBAL_STATS_RESET_WEB_USEC_MAX) {
101 + uint64_t n = 0;
102 + __atomic_compare_exchange(&global_statistics.web_usec_max, &gs->web_usec_max, &n, 1, __ATOMIC_SEQ_CST,
103 + __ATOMIC_SEQ_CST);
104 + }
105 #else
106 global_statistics_lock();
107