diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2012-12-04 10:47:05 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2012-12-04 10:47:05 +0000 |
commit | 1947bc8ef4eb37738ffc1c566d9c6f96071b29f7 (patch) | |
tree | 881fd7b84296e59e9fbd30dabde297a501edc2ad /src/transport/gnunet-service-transport_neighbours.c | |
parent | a317854b14a85a34f43655b1f74433c07f7ebbf9 (diff) | |
download | gnunet-1947bc8ef4eb37738ffc1c566d9c6f96071b29f7.tar.gz gnunet-1947bc8ef4eb37738ffc1c566d9c6f96071b29f7.zip |
- mem debug
Diffstat (limited to 'src/transport/gnunet-service-transport_neighbours.c')
-rw-r--r-- | src/transport/gnunet-service-transport_neighbours.c | 207 |
1 files changed, 193 insertions, 14 deletions
diff --git a/src/transport/gnunet-service-transport_neighbours.c b/src/transport/gnunet-service-transport_neighbours.c index 4785b4068..047f13144 100644 --- a/src/transport/gnunet-service-transport_neighbours.c +++ b/src/transport/gnunet-service-transport_neighbours.c | |||
@@ -92,6 +92,177 @@ | |||
92 | */ | 92 | */ |
93 | #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500) | 93 | #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500) |
94 | 94 | ||
95 | #define DEBUG_MALLOC GNUNET_NO | ||
96 | |||
97 | #if DEBUG_MALLOC | ||
98 | |||
99 | struct Allocator | ||
100 | { | ||
101 | struct Allocator *prev; | ||
102 | struct Allocator *next; | ||
103 | |||
104 | unsigned int bytes_alloced; | ||
105 | unsigned int max_alloced; | ||
106 | unsigned int diff; | ||
107 | unsigned int line; | ||
108 | |||
109 | struct GNUNET_TIME_Absolute max_alloced_when; | ||
110 | struct GNUNET_TIME_Absolute last_alloced_when; | ||
111 | |||
112 | }; | ||
113 | |||
114 | struct Allocator *aehead; | ||
115 | struct Allocator *aetail; | ||
116 | |||
117 | struct Allocation | ||
118 | { | ||
119 | struct Allocation *prev; | ||
120 | struct Allocation *next; | ||
121 | |||
122 | struct Allocator *alloc; | ||
123 | unsigned int bytes_alloced; | ||
124 | void *p; | ||
125 | unsigned int line; | ||
126 | }; | ||
127 | |||
128 | struct Allocation *ahead; | ||
129 | struct Allocation *atail; | ||
130 | |||
131 | static int bytes_alloced; | ||
132 | |||
133 | static struct Allocator * | ||
134 | find_allocator (int line) | ||
135 | { | ||
136 | struct Allocator *cur = aehead; | ||
137 | while (NULL != cur) | ||
138 | { | ||
139 | if (line == cur->line) | ||
140 | return cur; | ||
141 | cur = cur->next; | ||
142 | } | ||
143 | return cur; | ||
144 | } | ||
145 | |||
146 | static void | ||
147 | print_allocators () | ||
148 | { | ||
149 | static int start = GNUNET_YES; | ||
150 | static struct GNUNET_TIME_Absolute next; | ||
151 | static struct GNUNET_TIME_Relative rem; | ||
152 | struct Allocator *cur = aehead; | ||
153 | if (start) | ||
154 | { | ||
155 | next = GNUNET_TIME_UNIT_ZERO_ABS; | ||
156 | start = GNUNET_NO; | ||
157 | } | ||
158 | if (0 == (rem = GNUNET_TIME_absolute_get_remaining(next)).rel_value) | ||
159 | { | ||
160 | fprintf (stderr, "Allocated in `%s' total: %5u bytes\n", __FILE__, bytes_alloced); | ||
161 | while (NULL != cur) | ||
162 | { | ||
163 | char *last_alloc = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string(cur->max_alloced_when)); | ||
164 | fprintf (stderr, "Allocated from line %4u :%5u bytes (diff %5i bytes, max alloc: %5u @ %s, last alloc %s)\n", | ||
165 | cur->line, cur->bytes_alloced, cur->diff, cur->max_alloced, | ||
166 | last_alloc, | ||
167 | GNUNET_STRINGS_absolute_time_to_string(cur->last_alloced_when)); | ||
168 | GNUNET_free (last_alloc); | ||
169 | cur->diff = 0; | ||
170 | cur = cur->next; | ||
171 | } | ||
172 | fprintf (stderr, "\n"); | ||
173 | next = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), GNUNET_TIME_UNIT_SECONDS); | ||
174 | } | ||
175 | } | ||
176 | |||
177 | #endif | ||
178 | |||
179 | static void | ||
180 | MEMDEBUG_add_alloc (void *p, size_t size, int line) | ||
181 | { | ||
182 | #if DEBUG_MALLOC | ||
183 | struct Allocation *alloc = GNUNET_malloc (sizeof (struct Allocation)); | ||
184 | struct Allocator *allocator = find_allocator(line); | ||
185 | if (NULL == allocator) | ||
186 | { | ||
187 | allocator = GNUNET_malloc (sizeof (struct Allocator)); | ||
188 | allocator->line = line; | ||
189 | GNUNET_CONTAINER_DLL_insert (aehead, aetail, allocator); | ||
190 | } | ||
191 | alloc->alloc = allocator; | ||
192 | alloc->p = p; | ||
193 | alloc->line = line; | ||
194 | alloc->bytes_alloced = size; | ||
195 | allocator->bytes_alloced += size; | ||
196 | allocator->last_alloced_when = GNUNET_TIME_absolute_get(); | ||
197 | if (allocator->bytes_alloced >= allocator->max_alloced) | ||
198 | { | ||
199 | allocator->max_alloced = allocator->bytes_alloced; | ||
200 | allocator->max_alloced_when = allocator->last_alloced_when; | ||
201 | } | ||
202 | allocator->diff += size; | ||
203 | GNUNET_CONTAINER_DLL_insert (ahead, atail, alloc); | ||
204 | print_allocators (); | ||
205 | bytes_alloced += size; | ||
206 | #endif | ||
207 | } | ||
208 | |||
209 | |||
210 | static void * | ||
211 | MEMDEBUG_malloc (size_t size, int line) | ||
212 | { | ||
213 | void * ret; | ||
214 | |||
215 | ret = GNUNET_malloc (size); | ||
216 | #if DEBUG_MALLOC | ||
217 | if (NULL != ret) | ||
218 | MEMDEBUG_add_alloc (ret, size, line); | ||
219 | #endif | ||
220 | return ret; | ||
221 | |||
222 | } | ||
223 | |||
224 | static void | ||
225 | MEMDEBUG_free (void * alloc, int line) | ||
226 | { | ||
227 | #if DEBUG_MALLOC | ||
228 | struct Allocation *cur; | ||
229 | struct Allocator *allocator; | ||
230 | cur = ahead; | ||
231 | while (NULL != cur) | ||
232 | { | ||
233 | if (alloc == cur->p) | ||
234 | break; | ||
235 | cur = cur->next; | ||
236 | } | ||
237 | if (NULL == cur) | ||
238 | { | ||
239 | fprintf (stderr, "Unmonitored free from line %4u\n", line); | ||
240 | GNUNET_break (0); | ||
241 | return; | ||
242 | } | ||
243 | allocator = cur->alloc; | ||
244 | if (NULL == allocator) | ||
245 | { | ||
246 | GNUNET_break (0); | ||
247 | } | ||
248 | GNUNET_CONTAINER_DLL_remove (ahead, atail, cur); | ||
249 | allocator->bytes_alloced -= cur->bytes_alloced; | ||
250 | allocator->diff -= cur->bytes_alloced; | ||
251 | GNUNET_assert (allocator->bytes_alloced >= 0); | ||
252 | bytes_alloced -= cur->bytes_alloced; | ||
253 | GNUNET_assert (bytes_alloced >= 0); | ||
254 | GNUNET_free (cur); | ||
255 | #endif | ||
256 | GNUNET_free (alloc); | ||
257 | } | ||
258 | |||
259 | static void | ||
260 | MEMDEBUG_free_non_null (void * alloc, int line) | ||
261 | { | ||
262 | if (alloc != NULL) | ||
263 | MEMDEBUG_free (alloc, line); | ||
264 | } | ||
265 | |||
95 | 266 | ||
96 | GNUNET_NETWORK_STRUCT_BEGIN | 267 | GNUNET_NETWORK_STRUCT_BEGIN |
97 | 268 | ||
@@ -760,7 +931,8 @@ free_address (struct NeighbourAddress *na) | |||
760 | na->ats_active = GNUNET_NO; | 931 | na->ats_active = GNUNET_NO; |
761 | if (NULL != na->address) | 932 | if (NULL != na->address) |
762 | { | 933 | { |
763 | GNUNET_HELLO_address_free (na->address); | 934 | MEMDEBUG_free (na->address, __LINE__); |
935 | //GNUNET_HELLO_address_free (na->address); | ||
764 | na->address = NULL; | 936 | na->address = NULL; |
765 | } | 937 | } |
766 | na->session = NULL; | 938 | na->session = NULL; |
@@ -825,6 +997,7 @@ set_address (struct NeighbourAddress *na, | |||
825 | return; | 997 | return; |
826 | } | 998 | } |
827 | na->address = GNUNET_HELLO_address_copy (address); | 999 | na->address = GNUNET_HELLO_address_copy (address); |
1000 | MEMDEBUG_add_alloc (na->address, GNUNET_HELLO_address_get_size (na->address), __LINE__); | ||
828 | na->bandwidth_in = bandwidth_in; | 1001 | na->bandwidth_in = bandwidth_in; |
829 | na->bandwidth_out = bandwidth_out; | 1002 | na->bandwidth_out = bandwidth_out; |
830 | na->session = session; | 1003 | na->session = session; |
@@ -864,7 +1037,7 @@ free_neighbour (struct NeighbourMapEntry *n, int keep_sessions) | |||
864 | GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq); | 1037 | GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq); |
865 | if (NULL != mq->cont) | 1038 | if (NULL != mq->cont) |
866 | mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0); | 1039 | mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0); |
867 | GNUNET_free (mq); | 1040 | MEMDEBUG_free (mq, __LINE__); |
868 | } | 1041 | } |
869 | /* It is too late to send other peer disconnect notifications, but at | 1042 | /* It is too late to send other peer disconnect notifications, but at |
870 | least internally we need to get clean... */ | 1043 | least internally we need to get clean... */ |
@@ -880,7 +1053,10 @@ free_neighbour (struct NeighbourMapEntry *n, int keep_sessions) | |||
880 | n->state = S_DISCONNECT_FINISHED; | 1053 | n->state = S_DISCONNECT_FINISHED; |
881 | 1054 | ||
882 | if (NULL != n->primary_address.address) | 1055 | if (NULL != n->primary_address.address) |
1056 | { | ||
883 | backup_primary = GNUNET_HELLO_address_copy(n->primary_address.address); | 1057 | backup_primary = GNUNET_HELLO_address_copy(n->primary_address.address); |
1058 | MEMDEBUG_add_alloc (backup_primary, GNUNET_HELLO_address_get_size(backup_primary), __LINE__); | ||
1059 | } | ||
884 | else | 1060 | else |
885 | backup_primary = NULL; | 1061 | backup_primary = NULL; |
886 | 1062 | ||
@@ -904,7 +1080,7 @@ free_neighbour (struct NeighbourMapEntry *n, int keep_sessions) | |||
904 | (NULL != (papi = GST_plugins_find (backup_primary->transport_name)))) | 1080 | (NULL != (papi = GST_plugins_find (backup_primary->transport_name)))) |
905 | papi->disconnect (papi->cls, &n->id); | 1081 | papi->disconnect (papi->cls, &n->id); |
906 | 1082 | ||
907 | GNUNET_free_non_null (backup_primary); | 1083 | MEMDEBUG_free_non_null (backup_primary, __LINE__); |
908 | 1084 | ||
909 | GNUNET_assert (GNUNET_YES == | 1085 | GNUNET_assert (GNUNET_YES == |
910 | GNUNET_CONTAINER_multihashmap_remove (neighbours, | 1086 | GNUNET_CONTAINER_multihashmap_remove (neighbours, |
@@ -922,7 +1098,7 @@ free_neighbour (struct NeighbourMapEntry *n, int keep_sessions) | |||
922 | n->task = GNUNET_SCHEDULER_NO_TASK; | 1098 | n->task = GNUNET_SCHEDULER_NO_TASK; |
923 | } | 1099 | } |
924 | /* free rest of memory */ | 1100 | /* free rest of memory */ |
925 | GNUNET_free (n); | 1101 | MEMDEBUG_free (n, __LINE__); |
926 | } | 1102 | } |
927 | 1103 | ||
928 | /** | 1104 | /** |
@@ -1142,7 +1318,7 @@ transmit_send_continuation (void *cls, | |||
1142 | 1318 | ||
1143 | if (NULL == (n = lookup_neighbour (receiver))) | 1319 | if (NULL == (n = lookup_neighbour (receiver))) |
1144 | { | 1320 | { |
1145 | GNUNET_free (mq); | 1321 | MEMDEBUG_free (mq, __LINE__); |
1146 | return; /* disconnect or other error while transmitting, can happen */ | 1322 | return; /* disconnect or other error while transmitting, can happen */ |
1147 | } | 1323 | } |
1148 | if (n->is_active == mq) | 1324 | if (n->is_active == mq) |
@@ -1188,7 +1364,7 @@ transmit_send_continuation (void *cls, | |||
1188 | (success == GNUNET_OK) ? "success" : "FAILURE"); | 1364 | (success == GNUNET_OK) ? "success" : "FAILURE"); |
1189 | if (NULL != mq->cont) | 1365 | if (NULL != mq->cont) |
1190 | mq->cont (mq->cont_cls, success, size_payload, physical); | 1366 | mq->cont (mq->cont_cls, success, size_payload, physical); |
1191 | GNUNET_free (mq); | 1367 | MEMDEBUG_free (mq, __LINE__); |
1192 | } | 1368 | } |
1193 | 1369 | ||
1194 | 1370 | ||
@@ -1508,7 +1684,7 @@ GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg, | |||
1508 | gettext_noop | 1684 | gettext_noop |
1509 | ("# bytes in message queue for other peers"), | 1685 | ("# bytes in message queue for other peers"), |
1510 | bytes_in_send_queue, GNUNET_NO); | 1686 | bytes_in_send_queue, GNUNET_NO); |
1511 | mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size); | 1687 | mq = MEMDEBUG_malloc (sizeof (struct MessageQueue) + msg_size, __LINE__); |
1512 | mq->cont = cont; | 1688 | mq->cont = cont; |
1513 | mq->cont_cls = cont_cls; | 1689 | mq->cont_cls = cont_cls; |
1514 | memcpy (&mq[1], msg, msg_size); | 1690 | memcpy (&mq[1], msg, msg_size); |
@@ -1618,7 +1794,7 @@ setup_neighbour (const struct GNUNET_PeerIdentity *peer) | |||
1618 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1794 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1619 | "Creating new neighbour entry for `%s'\n", | 1795 | "Creating new neighbour entry for `%s'\n", |
1620 | GNUNET_i2s (peer)); | 1796 | GNUNET_i2s (peer)); |
1621 | n = GNUNET_malloc (sizeof (struct NeighbourMapEntry)); | 1797 | n = MEMDEBUG_malloc (sizeof (struct NeighbourMapEntry), __LINE__); |
1622 | n->id = *peer; | 1798 | n->id = *peer; |
1623 | n->state = S_NOT_CONNECTED; | 1799 | n->state = S_NOT_CONNECTED; |
1624 | n->latency = GNUNET_TIME_UNIT_FOREVER_REL; | 1800 | n->latency = GNUNET_TIME_UNIT_FOREVER_REL; |
@@ -1955,11 +2131,12 @@ handle_test_blacklist_cont (void *cls, | |||
1955 | break; | 2131 | break; |
1956 | } | 2132 | } |
1957 | cleanup: | 2133 | cleanup: |
1958 | GNUNET_HELLO_address_free (bcc->na.address); | 2134 | MEMDEBUG_free (bcc->na.address, __LINE__); |
2135 | //GNUNET_HELLO_address_free (bcc->na.address); | ||
1959 | GNUNET_CONTAINER_DLL_remove (bc_head, | 2136 | GNUNET_CONTAINER_DLL_remove (bc_head, |
1960 | bc_tail, | 2137 | bc_tail, |
1961 | bcc); | 2138 | bcc); |
1962 | GNUNET_free (bcc); | 2139 | MEMDEBUG_free (bcc, __LINE__); |
1963 | } | 2140 | } |
1964 | 2141 | ||
1965 | 2142 | ||
@@ -1987,10 +2164,11 @@ check_blacklist (const struct GNUNET_PeerIdentity *peer, | |||
1987 | struct GST_BlacklistCheck *bc; | 2164 | struct GST_BlacklistCheck *bc; |
1988 | 2165 | ||
1989 | bcc = | 2166 | bcc = |
1990 | GNUNET_malloc (sizeof (struct BlackListCheckContext) + | 2167 | MEMDEBUG_malloc (sizeof (struct BlackListCheckContext) + |
1991 | sizeof (struct GNUNET_ATS_Information) * ats_count); | 2168 | sizeof (struct GNUNET_ATS_Information) * ats_count, __LINE__); |
1992 | bcc->ats_count = ats_count; | 2169 | bcc->ats_count = ats_count; |
1993 | bcc->na.address = GNUNET_HELLO_address_copy (address); | 2170 | bcc->na.address = GNUNET_HELLO_address_copy (address); |
2171 | MEMDEBUG_add_alloc (bcc->na.address, GNUNET_HELLO_address_get_size (address), __LINE__); | ||
1994 | bcc->na.session = session; | 2172 | bcc->na.session = session; |
1995 | bcc->na.connect_timestamp = ts; | 2173 | bcc->na.connect_timestamp = ts; |
1996 | bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1]; | 2174 | bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1]; |
@@ -2744,11 +2922,12 @@ GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer, | |||
2744 | if (bcc->na.session == session) | 2922 | if (bcc->na.session == session) |
2745 | { | 2923 | { |
2746 | GST_blacklist_test_cancel (bcc->bc); | 2924 | GST_blacklist_test_cancel (bcc->bc); |
2747 | GNUNET_HELLO_address_free (bcc->na.address); | 2925 | MEMDEBUG_free (bcc->na.address, __LINE__); |
2926 | //GNUNET_HELLO_address_free (bcc->na.address); | ||
2748 | GNUNET_CONTAINER_DLL_remove (bc_head, | 2927 | GNUNET_CONTAINER_DLL_remove (bc_head, |
2749 | bc_tail, | 2928 | bc_tail, |
2750 | bcc); | 2929 | bcc); |
2751 | GNUNET_free (bcc); | 2930 | MEMDEBUG_free (bcc, __LINE__); |
2752 | } | 2931 | } |
2753 | } | 2932 | } |
2754 | if (NULL == (n = lookup_neighbour (peer))) | 2933 | if (NULL == (n = lookup_neighbour (peer))) |