@cryptotaxi247 / netdata-1 / commits / b80559089

use atomic operations for debugging malloc issues

Costa Tsaousis (ktsaou) committed Oct 1, 2016 at 14:09 UTC b80559089bac365626e2305371643ac64186e5de
1 file changed +52 -12
src/common.c
+52 -12
@@ -16,17 +16,15 @@ volatile sig_atomic_t netdata_exit = 0;
16
17 #ifdef NETDATA_LOG_ALLOCATIONS
18 static struct memory_statistics {
19 - size_t malloc_calls_made;
20 - size_t calloc_calls_made;
21 - size_t realloc_calls_made;
22 - size_t strdup_calls_made;
23 - size_t free_calls_made;
24 - size_t memory_calls_made;
25 - size_t allocated_memory;
26 - size_t mmapped_memory;
27 -} memory_statistics = {
28 - 0, 0, 0, 0, 0, 0, 0, 0
29 -};
19 + volatile size_t malloc_calls_made;
20 + volatile size_t calloc_calls_made;
21 + volatile size_t realloc_calls_made;
22 + volatile size_t strdup_calls_made;
23 + volatile size_t free_calls_made;
24 + volatile size_t memory_calls_made;
25 + volatile size_t allocated_memory;
26 + volatile size_t mmapped_memory;
27 +} memory_statistics;
28
29 static inline void print_allocations(const char *file, const char *function, const unsigned long line) {
30 static struct memory_statistics old = { 0, 0, 0, 0, 0, 0, 0, 0 };
@@ -48,37 +46,73 @@ static inline void print_allocations(const char *file, const char *function, con
46 }
47
48 static inline void malloc_accounting(const char *file, const char *function, const unsigned long line, size_t size) {
49 +#if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
50 + __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
51 + __atomic_fetch_add(&memory_statistics.malloc_calls_made, 1, __ATOMIC_SEQ_CST);
52 + __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
53 +#else
54 + // this is for debugging - we don't care locking it
55 memory_statistics.memory_calls_made++;
56 memory_statistics.malloc_calls_made++;
57 memory_statistics.allocated_memory += size;
58 +#endif
59 print_allocations(file, function, line);
60 }
61
62 static inline void mmap_accounting(size_t size) {
63 +#if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
64 + __atomic_fetch_add(&memory_statistics.malloc_calls_made, 1, __ATOMIC_SEQ_CST);
65 + __atomic_fetch_add(&memory_statistics.mmapped_memory, size, __ATOMIC_SEQ_CST);
66 +#else
67 + // this is for debugging - we don't care locking it
68 memory_statistics.memory_calls_made++;
69 memory_statistics.mmapped_memory += size;
70 +#endif
71 }
72
73 static inline void calloc_accounting(const char *file, const char *function, const unsigned long line, size_t size) {
74 +#if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
75 + __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
76 + __atomic_fetch_add(&memory_statistics.calloc_calls_made, 1, __ATOMIC_SEQ_CST);
77 + __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
78 +#else
79 + // this is for debugging - we don't care locking it
80 memory_statistics.memory_calls_made++;
81 memory_statistics.calloc_calls_made++;
82 memory_statistics.allocated_memory += size;
83 +#endif
84 print_allocations(file, function, line);
85 }
86
87 static inline void realloc_accounting(const char *file, const char *function, const unsigned long line, void *ptr, size_t size) {
88 (void)ptr;
89
90 +#if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
91 + __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
92 + __atomic_fetch_add(&memory_statistics.realloc_calls_made, 1, __ATOMIC_SEQ_CST);
93 + __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
94 +#else
95 + // this is for debugging - we don't care locking it
96 memory_statistics.memory_calls_made++;
97 memory_statistics.realloc_calls_made++;
98 memory_statistics.allocated_memory += size;
99 +#endif
100 print_allocations(file, function, line);
101 }
102
103 static inline void strdup_accounting(const char *file, const char *function, const unsigned long line, const char *s) {
104 + size_t size = strlen(s) + 1;
105 +
106 +#if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
107 + __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
108 + __atomic_fetch_add(&memory_statistics.strdup_calls_made, 1, __ATOMIC_SEQ_CST);
109 + __atomic_fetch_add(&memory_statistics.allocated_memory, size, __ATOMIC_SEQ_CST);
110 +#else
111 + // this is for debugging - we don't care locking it
112 memory_statistics.memory_calls_made++;
113 memory_statistics.strdup_calls_made++;
81 - memory_statistics.allocated_memory += strlen(s) + 1;
114 + memory_statistics.allocated_memory += size;
115 +#endif
116 print_allocations(file, function, line);
117 }
118
@@ -88,8 +122,14 @@ static inline void free_accounting(const char *file, const char *function, const
122 (void)line;
123
124 if(likely(ptr)) {
125 +#if defined(HAVE_C___ATOMIC) && !defined(NETDATA_NO_ATOMIC_INSTRUCTIONS)
126 + __atomic_fetch_add(&memory_statistics.memory_calls_made, 1, __ATOMIC_SEQ_CST);
127 + __atomic_fetch_add(&memory_statistics.free_calls_made, 1, __ATOMIC_SEQ_CST);
128 +#else
129 + // this is for debugging - we don't care locking it
130 memory_statistics.memory_calls_made++;
131 memory_statistics.free_calls_made++;
132 +#endif
133 }
134 }
135 #endif