aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-10-18 11:37:58 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-10-18 11:37:58 +0000
commit5dcfccf5803510e9f4d2339b19dc617b4043153a (patch)
tree19e7101a29dc9659c3779c0fe0dc08663dc97ddd /src
parentb29cf59e12a0aa522b218a22353f37eeb736f2ee (diff)
downloadgnunet-5dcfccf5803510e9f4d2339b19dc617b4043153a.tar.gz
gnunet-5dcfccf5803510e9f4d2339b19dc617b4043153a.zip
clean up code and code for utilization
Diffstat (limited to 'src')
-rw-r--r--src/transport/gnunet-service-transport_neighbours.c226
1 files changed, 24 insertions, 202 deletions
diff --git a/src/transport/gnunet-service-transport_neighbours.c b/src/transport/gnunet-service-transport_neighbours.c
index dd598859f..5195c90cd 100644
--- a/src/transport/gnunet-service-transport_neighbours.c
+++ b/src/transport/gnunet-service-transport_neighbours.c
@@ -97,178 +97,6 @@
97 */ 97 */
98#define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1) 98#define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
99 99
100#define DEBUG_MALLOC GNUNET_NO
101
102#if DEBUG_MALLOC
103
104struct Allocator
105{
106 struct Allocator *prev;
107 struct Allocator *next;
108
109 unsigned int bytes_alloced;
110 unsigned int max_alloced;
111 unsigned int diff;
112 unsigned int line;
113
114 struct GNUNET_TIME_Absolute max_alloced_when;
115 struct GNUNET_TIME_Absolute last_alloced_when;
116
117};
118
119struct Allocator *aehead;
120struct Allocator *aetail;
121
122struct Allocation
123{
124 struct Allocation *prev;
125 struct Allocation *next;
126
127 struct Allocator *alloc;
128 unsigned int bytes_alloced;
129 void *p;
130 unsigned int line;
131};
132
133struct Allocation *ahead;
134struct Allocation *atail;
135
136static int bytes_alloced;
137
138static struct Allocator *
139find_allocator (int line)
140{
141 struct Allocator *cur = aehead;
142 while (NULL != cur)
143 {
144 if (line == cur->line)
145 return cur;
146 cur = cur->next;
147 }
148 return cur;
149}
150
151static void
152print_allocators ()
153{
154 static int start = GNUNET_YES;
155 static struct GNUNET_TIME_Absolute next;
156 static struct GNUNET_TIME_Relative rem;
157 struct Allocator *cur = aehead;
158 if (start)
159 {
160 next = GNUNET_TIME_UNIT_ZERO_ABS;
161 start = GNUNET_NO;
162 }
163 if (0 == (rem = GNUNET_TIME_absolute_get_remaining(next)).rel_value_us)
164 {
165 fprintf (stderr, "Allocated in `%s' total: %5u bytes\n", __FILE__, bytes_alloced);
166 while (NULL != cur)
167 {
168 char *last_alloc = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string(cur->max_alloced_when));
169 fprintf (stderr, "Allocated from line %4u :%5u bytes (diff %5i bytes, max alloc: %5u @ %s, last alloc %s)\n",
170 cur->line, cur->bytes_alloced, cur->diff, cur->max_alloced,
171 last_alloc,
172 GNUNET_STRINGS_absolute_time_to_string(cur->last_alloced_when));
173 GNUNET_free (last_alloc);
174 cur->diff = 0;
175 cur = cur->next;
176 }
177 fprintf (stderr, "\n");
178 next = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), GNUNET_TIME_UNIT_SECONDS);
179 }
180}
181
182#endif
183
184static void
185MEMDEBUG_add_alloc (void *p, size_t size, int line)
186{
187#if DEBUG_MALLOC
188 struct Allocation *alloc = GNUNET_malloc (sizeof (struct Allocation));
189 struct Allocator *allocator = find_allocator(line);
190 if (NULL == allocator)
191 {
192 allocator = GNUNET_malloc (sizeof (struct Allocator));
193 allocator->line = line;
194 GNUNET_CONTAINER_DLL_insert (aehead, aetail, allocator);
195 }
196 alloc->alloc = allocator;
197 alloc->p = p;
198 alloc->line = line;
199 alloc->bytes_alloced = size;
200 allocator->bytes_alloced += size;
201 allocator->last_alloced_when = GNUNET_TIME_absolute_get();
202 if (allocator->bytes_alloced >= allocator->max_alloced)
203 {
204 allocator->max_alloced = allocator->bytes_alloced;
205 allocator->max_alloced_when = allocator->last_alloced_when;
206 }
207 allocator->diff += size;
208 GNUNET_CONTAINER_DLL_insert (ahead, atail, alloc);
209 print_allocators ();
210 bytes_alloced += size;
211#endif
212}
213
214
215static void *
216MEMDEBUG_malloc (size_t size, int line)
217{
218 void * ret;
219
220 ret = GNUNET_malloc (size);
221#if DEBUG_MALLOC
222 if (NULL != ret)
223 MEMDEBUG_add_alloc (ret, size, line);
224#endif
225 return ret;
226
227}
228
229static void
230MEMDEBUG_free (void * alloc, int line)
231{
232#if DEBUG_MALLOC
233 struct Allocation *cur;
234 struct Allocator *allocator;
235 cur = ahead;
236 while (NULL != cur)
237 {
238 if (alloc == cur->p)
239 break;
240 cur = cur->next;
241 }
242 if (NULL == cur)
243 {
244 fprintf (stderr, "Unmonitored free from line %4u\n", line);
245 GNUNET_break (0);
246 return;
247 }
248 allocator = cur->alloc;
249 if (NULL == allocator)
250 {
251 GNUNET_break (0);
252 }
253 GNUNET_CONTAINER_DLL_remove (ahead, atail, cur);
254 allocator->bytes_alloced -= cur->bytes_alloced;
255 allocator->diff -= cur->bytes_alloced;
256 GNUNET_assert (allocator->bytes_alloced >= 0);
257 bytes_alloced -= cur->bytes_alloced;
258 GNUNET_assert (bytes_alloced >= 0);
259 GNUNET_free (cur);
260#endif
261 GNUNET_free (alloc);
262}
263
264static void
265MEMDEBUG_free_non_null (void * alloc, int line)
266{
267 if (alloc != NULL)
268 MEMDEBUG_free (alloc, line);
269}
270
271
272GNUNET_NETWORK_STRUCT_BEGIN 100GNUNET_NETWORK_STRUCT_BEGIN
273 101
274/** 102/**
@@ -712,12 +540,12 @@ struct NeighbourMapEntry
712 /** 540 /**
713 * Tracking utilization of outbound bandwidth 541 * Tracking utilization of outbound bandwidth
714 */ 542 */
715 unsigned int util_bytes_sent; 543 uint32_t util_bytes_sent;
716 544
717 /** 545 /**
718 * Tracking utilization of inbound bandwidth 546 * Tracking utilization of inbound bandwidth
719 */ 547 */
720 unsigned int util_bytes_recv; 548 uint32_t util_bytes_recv;
721 549
722 550
723 /** 551 /**
@@ -950,8 +778,7 @@ free_address (struct NeighbourAddress *na)
950 na->ats_active = GNUNET_NO; 778 na->ats_active = GNUNET_NO;
951 if (NULL != na->address) 779 if (NULL != na->address)
952 { 780 {
953 MEMDEBUG_free (na->address, __LINE__); 781 GNUNET_HELLO_address_free (na->address);
954 //GNUNET_HELLO_address_free (na->address);
955 na->address = NULL; 782 na->address = NULL;
956 } 783 }
957 na->session = NULL; 784 na->session = NULL;
@@ -1016,7 +843,6 @@ set_address (struct NeighbourAddress *na,
1016 return; 843 return;
1017 } 844 }
1018 na->address = GNUNET_HELLO_address_copy (address); 845 na->address = GNUNET_HELLO_address_copy (address);
1019 MEMDEBUG_add_alloc (na->address, GNUNET_HELLO_address_get_size (na->address), __LINE__);
1020 na->bandwidth_in = bandwidth_in; 846 na->bandwidth_in = bandwidth_in;
1021 na->bandwidth_out = bandwidth_out; 847 na->bandwidth_out = bandwidth_out;
1022 na->session = session; 848 na->session = session;
@@ -1056,7 +882,7 @@ free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
1056 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq); 882 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1057 if (NULL != mq->cont) 883 if (NULL != mq->cont)
1058 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0); 884 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
1059 MEMDEBUG_free (mq, __LINE__); 885 GNUNET_free (mq);
1060 } 886 }
1061 /* It is too late to send other peer disconnect notifications, but at 887 /* It is too late to send other peer disconnect notifications, but at
1062 least internally we need to get clean... */ 888 least internally we need to get clean... */
@@ -1074,7 +900,6 @@ free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
1074 if (NULL != n->primary_address.address) 900 if (NULL != n->primary_address.address)
1075 { 901 {
1076 backup_primary = GNUNET_HELLO_address_copy(n->primary_address.address); 902 backup_primary = GNUNET_HELLO_address_copy(n->primary_address.address);
1077 MEMDEBUG_add_alloc (backup_primary, GNUNET_HELLO_address_get_size(backup_primary), __LINE__);
1078 } 903 }
1079 else 904 else
1080 backup_primary = NULL; 905 backup_primary = NULL;
@@ -1099,7 +924,7 @@ free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
1099 (NULL != (papi = GST_plugins_find (backup_primary->transport_name)))) 924 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
1100 papi->disconnect (papi->cls, &n->id); 925 papi->disconnect (papi->cls, &n->id);
1101 926
1102 MEMDEBUG_free_non_null (backup_primary, __LINE__); 927 GNUNET_free_non_null (backup_primary);
1103 928
1104 GNUNET_assert (GNUNET_YES == 929 GNUNET_assert (GNUNET_YES ==
1105 GNUNET_CONTAINER_multipeermap_remove (neighbours, 930 GNUNET_CONTAINER_multipeermap_remove (neighbours,
@@ -1121,7 +946,7 @@ free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
1121 n->task = GNUNET_SCHEDULER_NO_TASK; 946 n->task = GNUNET_SCHEDULER_NO_TASK;
1122 } 947 }
1123 /* free rest of memory */ 948 /* free rest of memory */
1124 MEMDEBUG_free (n, __LINE__); 949 GNUNET_free (n);
1125} 950}
1126 951
1127/** 952/**
@@ -1341,7 +1166,7 @@ transmit_send_continuation (void *cls,
1341 1166
1342 if (NULL == (n = lookup_neighbour (receiver))) 1167 if (NULL == (n = lookup_neighbour (receiver)))
1343 { 1168 {
1344 MEMDEBUG_free (mq, __LINE__); 1169 GNUNET_free (mq);
1345 return; /* disconnect or other error while transmitting, can happen */ 1170 return; /* disconnect or other error while transmitting, can happen */
1346 } 1171 }
1347 if (n->is_active == mq) 1172 if (n->is_active == mq)
@@ -1390,7 +1215,7 @@ transmit_send_continuation (void *cls,
1390 (success == GNUNET_OK) ? "success" : "FAILURE"); 1215 (success == GNUNET_OK) ? "success" : "FAILURE");
1391 if (NULL != mq->cont) 1216 if (NULL != mq->cont)
1392 mq->cont (mq->cont_cls, success, size_payload, physical); 1217 mq->cont (mq->cont_cls, success, size_payload, physical);
1393 MEMDEBUG_free (mq, __LINE__); 1218 GNUNET_free (mq);
1394} 1219}
1395 1220
1396 1221
@@ -1708,7 +1533,7 @@ GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1708 gettext_noop 1533 gettext_noop
1709 ("# bytes in message queue for other peers"), 1534 ("# bytes in message queue for other peers"),
1710 bytes_in_send_queue, GNUNET_NO); 1535 bytes_in_send_queue, GNUNET_NO);
1711 mq = MEMDEBUG_malloc (sizeof (struct MessageQueue) + msg_size, __LINE__); 1536 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1712 mq->cont = cont; 1537 mq->cont = cont;
1713 mq->cont_cls = cont_cls; 1538 mq->cont_cls = cont_cls;
1714 memcpy (&mq[1], msg, msg_size); 1539 memcpy (&mq[1], msg, msg_size);
@@ -1818,7 +1643,7 @@ setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1819 "Creating new neighbour entry for `%s'\n", 1644 "Creating new neighbour entry for `%s'\n",
1820 GNUNET_i2s (peer)); 1645 GNUNET_i2s (peer));
1821 n = MEMDEBUG_malloc (sizeof (struct NeighbourMapEntry), __LINE__); 1646 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1822 n->id = *peer; 1647 n->id = *peer;
1823 n->state = S_NOT_CONNECTED; 1648 n->state = S_NOT_CONNECTED;
1824 n->latency = GNUNET_TIME_UNIT_FOREVER_REL; 1649 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
@@ -2157,12 +1982,11 @@ handle_test_blacklist_cont (void *cls,
2157 break; 1982 break;
2158 } 1983 }
2159 cleanup: 1984 cleanup:
2160 MEMDEBUG_free (bcc->na.address, __LINE__); 1985 GNUNET_HELLO_address_free (bcc->na.address);
2161 //GNUNET_HELLO_address_free (bcc->na.address);
2162 GNUNET_CONTAINER_DLL_remove (bc_head, 1986 GNUNET_CONTAINER_DLL_remove (bc_head,
2163 bc_tail, 1987 bc_tail,
2164 bcc); 1988 bcc);
2165 MEMDEBUG_free (bcc, __LINE__); 1989 GNUNET_free (bcc);
2166} 1990}
2167 1991
2168 1992
@@ -2185,10 +2009,8 @@ check_blacklist (const struct GNUNET_PeerIdentity *peer,
2185 struct BlackListCheckContext *bcc; 2009 struct BlackListCheckContext *bcc;
2186 struct GST_BlacklistCheck *bc; 2010 struct GST_BlacklistCheck *bc;
2187 2011
2188 bcc = 2012 bcc = GNUNET_malloc (sizeof (struct BlackListCheckContext));
2189 MEMDEBUG_malloc (sizeof (struct BlackListCheckContext), __LINE__);
2190 bcc->na.address = GNUNET_HELLO_address_copy (address); 2013 bcc->na.address = GNUNET_HELLO_address_copy (address);
2191 MEMDEBUG_add_alloc (bcc->na.address, GNUNET_HELLO_address_get_size (address), __LINE__);
2192 bcc->na.session = session; 2014 bcc->na.session = session;
2193 bcc->na.connect_timestamp = ts; 2015 bcc->na.connect_timestamp = ts;
2194 GNUNET_CONTAINER_DLL_insert (bc_head, 2016 GNUNET_CONTAINER_DLL_insert (bc_head,
@@ -2352,8 +2174,6 @@ GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2352 if (NULL == (papi = GST_plugins_find (address->transport_name))) 2174 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2353 { 2175 {
2354 /* we don't have the plugin for this address */ 2176 /* we don't have the plugin for this address */
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2356 "2348 : `%s' \n", address->transport_name);
2357 GNUNET_ATS_address_destroyed (GST_ats, address, NULL); 2177 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2358 return; 2178 return;
2359 } 2179 }
@@ -2544,7 +2364,7 @@ GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2544} 2364}
2545 2365
2546static int 2366static int
2547util_it (void *cls, 2367send_utilization_data (void *cls,
2548 const struct GNUNET_PeerIdentity *key, 2368 const struct GNUNET_PeerIdentity *key,
2549 void *value) 2369 void *value)
2550{ 2370{
@@ -2557,12 +2377,15 @@ util_it (void *cls,
2557 delta = GNUNET_TIME_absolute_get_difference(n->last_util_transmission, GNUNET_TIME_absolute_get()); 2377 delta = GNUNET_TIME_absolute_get_difference(n->last_util_transmission, GNUNET_TIME_absolute_get());
2558 bps_in = 0; 2378 bps_in = 0;
2559 if (0 != n->util_bytes_recv) 2379 if (0 != n->util_bytes_recv)
2560 bps_in = ((1000 * 1000) * n->util_bytes_recv) / (delta.rel_value_us); 2380 bps_in = (1000LL * 1000LL * n->util_bytes_recv) / (delta.rel_value_us);
2561 bps_out = 0; 2381 bps_out = 0;
2562 if (0 != n->util_bytes_sent) 2382 if (0 != n->util_bytes_sent)
2563 bps_out = ((1000 * 1000) * n->util_bytes_sent) / (delta.rel_value_us); 2383 bps_out = (1000LL * 1000LL * n->util_bytes_sent) / delta.rel_value_us;
2564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s': Bytes sent: %u recv %u in %llu sec.\n", 2384
2565 GNUNET_i2s (key), bps_out, bps_in, delta.rel_value_us / (1000 * 1000)); 2385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s'received %u Bytes/s, sent %u Bytes/s \n",
2386 GNUNET_i2s (key), bps_in, bps_out);
2387
2388
2566 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_UP); 2389 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_UP);
2567 atsi[0].value = htonl (bps_out); 2390 atsi[0].value = htonl (bps_out);
2568 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_DOWN); 2391 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_DOWN);
@@ -2588,7 +2411,7 @@ utilization_transmission (void *cls,
2588 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK; 2411 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2589 2412
2590 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours)) 2413 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2591 GNUNET_CONTAINER_multipeermap_iterate (neighbours, util_it, NULL); 2414 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2592 2415
2593 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL, 2416 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2594 utilization_transmission, NULL); 2417 utilization_transmission, NULL);
@@ -3000,12 +2823,11 @@ GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3000 if (bcc->na.session == session) 2823 if (bcc->na.session == session)
3001 { 2824 {
3002 GST_blacklist_test_cancel (bcc->bc); 2825 GST_blacklist_test_cancel (bcc->bc);
3003 MEMDEBUG_free (bcc->na.address, __LINE__); 2826 GNUNET_HELLO_address_free (bcc->na.address);
3004 //GNUNET_HELLO_address_free (bcc->na.address);
3005 GNUNET_CONTAINER_DLL_remove (bc_head, 2827 GNUNET_CONTAINER_DLL_remove (bc_head,
3006 bc_tail, 2828 bc_tail,
3007 bcc); 2829 bcc);
3008 MEMDEBUG_free (bcc, __LINE__); 2830 GNUNET_free (bcc);
3009 } 2831 }
3010 } 2832 }
3011 if (NULL == (n = lookup_neighbour (peer))) 2833 if (NULL == (n = lookup_neighbour (peer)))