diff options
Diffstat (limited to 'src/cadet/gnunet-cadet-profiler.c')
-rw-r--r-- | src/cadet/gnunet-cadet-profiler.c | 1157 |
1 files changed, 0 insertions, 1157 deletions
diff --git a/src/cadet/gnunet-cadet-profiler.c b/src/cadet/gnunet-cadet-profiler.c deleted file mode 100644 index 22cbe55dc..000000000 --- a/src/cadet/gnunet-cadet-profiler.c +++ /dev/null | |||
@@ -1,1157 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2011 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file cadet/gnunet-cadet-profiler.c | ||
22 | * | ||
23 | * @brief Profiler for cadet experiments. | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include <stdio.h> | ||
27 | #include "cadet_test_lib.h" | ||
28 | #include "gnunet_cadet_service.h" | ||
29 | #include "gnunet_statistics_service.h" | ||
30 | |||
31 | |||
32 | #define PING 1 | ||
33 | #define PONG 2 | ||
34 | |||
35 | |||
36 | /** | ||
37 | * Paximum ping period in milliseconds. Real period = rand (0, PING_PERIOD) | ||
38 | */ | ||
39 | #define PING_PERIOD 500 | ||
40 | |||
41 | /** | ||
42 | * How long until we give up on connecting the peers? | ||
43 | */ | ||
44 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) | ||
45 | |||
46 | /** | ||
47 | * Time to wait for stuff that should be rather fast | ||
48 | */ | ||
49 | #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300) | ||
50 | |||
51 | /** | ||
52 | * Total number of rounds. | ||
53 | */ | ||
54 | #define number_rounds sizeof(rounds) / sizeof(rounds[0]) | ||
55 | |||
56 | /** | ||
57 | * Ratio of peers active. First round always is 1.0. | ||
58 | */ | ||
59 | static float rounds[] = { 0.8, 0.6, 0.8, 0.5, 0.3, 0.8, 0.0 }; | ||
60 | |||
61 | /** | ||
62 | * Message type for pings. | ||
63 | */ | ||
64 | struct CadetPingMessage | ||
65 | { | ||
66 | /** | ||
67 | * Header. Type PING/PONG. | ||
68 | */ | ||
69 | struct GNUNET_MessageHeader header; | ||
70 | |||
71 | /** | ||
72 | * Message number. | ||
73 | */ | ||
74 | uint32_t counter; | ||
75 | |||
76 | /** | ||
77 | * Time the message was sent. | ||
78 | */ | ||
79 | struct GNUNET_TIME_AbsoluteNBO timestamp; | ||
80 | |||
81 | /** | ||
82 | * Round number. | ||
83 | */ | ||
84 | uint32_t round_number; | ||
85 | }; | ||
86 | |||
87 | /** | ||
88 | * Peer description. | ||
89 | */ | ||
90 | struct CadetPeer | ||
91 | { | ||
92 | /** | ||
93 | * Testbed Operation (to get peer id, etc). | ||
94 | */ | ||
95 | struct GNUNET_TESTBED_Operation *op; | ||
96 | |||
97 | /** | ||
98 | * Peer ID. | ||
99 | */ | ||
100 | struct GNUNET_PeerIdentity id; | ||
101 | |||
102 | /** | ||
103 | * Cadet handle for the root peer | ||
104 | */ | ||
105 | struct GNUNET_CADET_Handle *cadet; | ||
106 | |||
107 | /** | ||
108 | * Channel handle for the root peer | ||
109 | */ | ||
110 | struct GNUNET_CADET_Channel *ch; | ||
111 | |||
112 | /** | ||
113 | * Channel handle for the dest peer | ||
114 | */ | ||
115 | struct GNUNET_CADET_Channel *incoming_ch; | ||
116 | |||
117 | /** | ||
118 | * Channel handle for a warmup channel. | ||
119 | */ | ||
120 | struct GNUNET_CADET_Channel *warmup_ch; | ||
121 | |||
122 | /** | ||
123 | * Number of payload packes sent | ||
124 | */ | ||
125 | int data_sent; | ||
126 | |||
127 | /** | ||
128 | * Number of payload packets received | ||
129 | */ | ||
130 | int data_received; | ||
131 | |||
132 | /** | ||
133 | * Is peer up? | ||
134 | */ | ||
135 | int up; | ||
136 | |||
137 | /** | ||
138 | * Destinaton to ping. | ||
139 | */ | ||
140 | struct CadetPeer *dest; | ||
141 | |||
142 | /** | ||
143 | * Incoming channel for pings. | ||
144 | */ | ||
145 | struct CadetPeer *incoming; | ||
146 | |||
147 | /** | ||
148 | * Task to do the next ping. | ||
149 | */ | ||
150 | struct GNUNET_SCHEDULER_Task *ping_task; | ||
151 | |||
152 | /** | ||
153 | * NTR operation for the next ping. | ||
154 | */ | ||
155 | struct GNUNET_CADET_TransmitHandle *ping_ntr; | ||
156 | |||
157 | float mean[number_rounds]; | ||
158 | float var[number_rounds]; | ||
159 | unsigned int pongs[number_rounds]; | ||
160 | unsigned int pings[number_rounds]; | ||
161 | }; | ||
162 | |||
163 | /** | ||
164 | * Duration of each round. | ||
165 | */ | ||
166 | static struct GNUNET_TIME_Relative round_time; | ||
167 | |||
168 | /** | ||
169 | * GNUNET_PeerIdentity -> CadetPeer | ||
170 | */ | ||
171 | static struct GNUNET_CONTAINER_MultiPeerMap *ids; | ||
172 | |||
173 | /** | ||
174 | * Testbed peer handles. | ||
175 | */ | ||
176 | static struct GNUNET_TESTBED_Peer **testbed_handles; | ||
177 | |||
178 | /** | ||
179 | * Testbed Operation (to get stats). | ||
180 | */ | ||
181 | static struct GNUNET_TESTBED_Operation *stats_op; | ||
182 | |||
183 | /** | ||
184 | * Operation to get peer ids. | ||
185 | */ | ||
186 | static struct CadetPeer *peers; | ||
187 | |||
188 | /** | ||
189 | * Peer ids counter. | ||
190 | */ | ||
191 | static unsigned int p_ids; | ||
192 | |||
193 | /** | ||
194 | * Total number of peers. | ||
195 | */ | ||
196 | static unsigned long long peers_total; | ||
197 | |||
198 | /** | ||
199 | * Number of currently running peers. | ||
200 | */ | ||
201 | static unsigned long long peers_running; | ||
202 | |||
203 | /** | ||
204 | * Number of peers doing pings. | ||
205 | */ | ||
206 | static unsigned long long peers_pinging; | ||
207 | |||
208 | /** | ||
209 | * Test context (to shut down). | ||
210 | */ | ||
211 | static struct GNUNET_CADET_TEST_Context *test_ctx; | ||
212 | |||
213 | /** | ||
214 | * Task called to disconnect peers, before shutdown. | ||
215 | */ | ||
216 | static struct GNUNET_SCHEDULER_Task *disconnect_task; | ||
217 | |||
218 | /** | ||
219 | * Task to perform tests | ||
220 | */ | ||
221 | static struct GNUNET_SCHEDULER_Task *test_task; | ||
222 | |||
223 | /** | ||
224 | * Round number. | ||
225 | */ | ||
226 | static unsigned int current_round; | ||
227 | |||
228 | /** | ||
229 | * Do preconnect? (Each peer creates a tunnel to one other peer). | ||
230 | */ | ||
231 | static int do_warmup; | ||
232 | |||
233 | /** | ||
234 | * Warmup progress. | ||
235 | */ | ||
236 | static unsigned int peers_warmup; | ||
237 | |||
238 | /** | ||
239 | * Flag to notify callbacks not to generate any new traffic anymore. | ||
240 | */ | ||
241 | static int test_finished; | ||
242 | |||
243 | /** | ||
244 | * Task running each round of the benchmark. | ||
245 | */ | ||
246 | static struct GNUNET_SCHEDULER_Task *round_task; | ||
247 | |||
248 | |||
249 | /** | ||
250 | * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES. | ||
251 | * | ||
252 | * Testcase continues when the root receives confirmation of connected peers, | ||
253 | * on callback function ch. | ||
254 | * | ||
255 | * @param cls Closure (unused). | ||
256 | */ | ||
257 | static void | ||
258 | start_test (void *cls); | ||
259 | |||
260 | |||
261 | /** | ||
262 | * Calculate a random delay. | ||
263 | * | ||
264 | * @param max Exclusive maximum, in ms. | ||
265 | * | ||
266 | * @return A time between 0 a max-1 ms. | ||
267 | */ | ||
268 | static struct GNUNET_TIME_Relative | ||
269 | delay_ms_rnd (unsigned int max) | ||
270 | { | ||
271 | unsigned int rnd; | ||
272 | |||
273 | rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max); | ||
274 | return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd); | ||
275 | } | ||
276 | |||
277 | |||
278 | /** | ||
279 | * Get the index of a peer in the peers array. | ||
280 | * | ||
281 | * @param peer Peer whose index to get. | ||
282 | * | ||
283 | * @return Index of peer in peers. | ||
284 | */ | ||
285 | static unsigned int | ||
286 | get_index (struct CadetPeer *peer) | ||
287 | { | ||
288 | return peer - peers; | ||
289 | } | ||
290 | |||
291 | |||
292 | /** | ||
293 | * Show the results of the test (banwidth achieved) and log them to GAUGER | ||
294 | */ | ||
295 | static void | ||
296 | show_end_data (void) | ||
297 | { | ||
298 | struct CadetPeer *peer; | ||
299 | unsigned int i; | ||
300 | unsigned int j; | ||
301 | |||
302 | for (i = 0; i < number_rounds; i++) | ||
303 | { | ||
304 | for (j = 0; j < peers_pinging; j++) | ||
305 | { | ||
306 | peer = &peers[j]; | ||
307 | fprintf (stdout, | ||
308 | "ROUND %3u PEER %3u: %10.2f / %10.2f, PINGS: %3u, PONGS: %3u\n", | ||
309 | i, j, peer->mean[i], sqrt (peer->var[i] / (peer->pongs[i] - 1)), | ||
310 | peer->pings[i], peer->pongs[i]); | ||
311 | } | ||
312 | } | ||
313 | } | ||
314 | |||
315 | |||
316 | /** | ||
317 | * Disconnect from cadet services af all peers, call shutdown. | ||
318 | * | ||
319 | * @param cls Closure (unused). | ||
320 | */ | ||
321 | static void | ||
322 | disconnect_cadet_peers (void *cls) | ||
323 | { | ||
324 | long line = (long) cls; | ||
325 | unsigned int i; | ||
326 | |||
327 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
328 | "disconnecting cadet service, called from line %ld\n", | ||
329 | line); | ||
330 | disconnect_task = NULL; | ||
331 | for (i = 0; i < peers_total; i++) | ||
332 | { | ||
333 | if (NULL != peers[i].op) | ||
334 | GNUNET_TESTBED_operation_done (peers[i].op); | ||
335 | |||
336 | if (peers[i].up != GNUNET_YES) | ||
337 | continue; | ||
338 | |||
339 | if (NULL != peers[i].ch) | ||
340 | { | ||
341 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
342 | "%u: channel %p\n", i, peers[i].ch); | ||
343 | GNUNET_CADET_channel_destroy (peers[i].ch); | ||
344 | } | ||
345 | if (NULL != peers[i].warmup_ch) | ||
346 | { | ||
347 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
348 | "%u: warmup channel %p\n", | ||
349 | i, peers[i].warmup_ch); | ||
350 | GNUNET_CADET_channel_destroy (peers[i].warmup_ch); | ||
351 | } | ||
352 | if (NULL != peers[i].incoming_ch) | ||
353 | { | ||
354 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
355 | "%u: incoming channel %p\n", | ||
356 | i, peers[i].incoming_ch); | ||
357 | GNUNET_CADET_channel_destroy (peers[i].incoming_ch); | ||
358 | } | ||
359 | } | ||
360 | GNUNET_CADET_TEST_cleanup (test_ctx); | ||
361 | GNUNET_SCHEDULER_shutdown (); | ||
362 | } | ||
363 | |||
364 | |||
365 | /** | ||
366 | * Shut down peergroup, clean up. | ||
367 | * | ||
368 | * @param cls Closure (unused). | ||
369 | */ | ||
370 | static void | ||
371 | shutdown_task (void *cls) | ||
372 | { | ||
373 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
374 | "Ending test.\n"); | ||
375 | if (NULL != disconnect_task) | ||
376 | { | ||
377 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
378 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
379 | (void *) __LINE__); | ||
380 | } | ||
381 | if (NULL != round_task) | ||
382 | { | ||
383 | GNUNET_SCHEDULER_cancel (round_task); | ||
384 | round_task = NULL; | ||
385 | } | ||
386 | if (NULL != test_task) | ||
387 | { | ||
388 | GNUNET_SCHEDULER_cancel (test_task); | ||
389 | test_task = NULL; | ||
390 | } | ||
391 | } | ||
392 | |||
393 | |||
394 | /** | ||
395 | * Finish test normally: schedule disconnect and shutdown | ||
396 | * | ||
397 | * @param line Line in the code the abort is requested from (__LINE__). | ||
398 | */ | ||
399 | static void | ||
400 | abort_test (long line) | ||
401 | { | ||
402 | if (disconnect_task != NULL) | ||
403 | { | ||
404 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
405 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
406 | (void *) line); | ||
407 | } | ||
408 | } | ||
409 | |||
410 | |||
411 | /** | ||
412 | * Stats callback. Finish the stats testbed operation and when all stats have | ||
413 | * been iterated, shutdown the test. | ||
414 | * | ||
415 | * @param cls closure | ||
416 | * @param op the operation that has been finished | ||
417 | * @param emsg error message in case the operation has failed; will be NULL if | ||
418 | * operation has executed successfully. | ||
419 | */ | ||
420 | static void | ||
421 | stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg) | ||
422 | { | ||
423 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n"); | ||
424 | GNUNET_TESTBED_operation_done (stats_op); | ||
425 | |||
426 | if (NULL != disconnect_task) | ||
427 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
428 | disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, | ||
429 | (void *) __LINE__); | ||
430 | } | ||
431 | |||
432 | |||
433 | /** | ||
434 | * Process statistic values. | ||
435 | * | ||
436 | * @param cls closure | ||
437 | * @param peer the peer the statistic belong to | ||
438 | * @param subsystem name of subsystem that created the statistic | ||
439 | * @param name the name of the datum | ||
440 | * @param value the current value | ||
441 | * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not | ||
442 | * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration | ||
443 | */ | ||
444 | static int | ||
445 | stats_iterator (void *cls, | ||
446 | const struct GNUNET_TESTBED_Peer *peer, | ||
447 | const char *subsystem, | ||
448 | const char *name, | ||
449 | uint64_t value, | ||
450 | int is_persistent) | ||
451 | { | ||
452 | uint32_t i; | ||
453 | |||
454 | i = GNUNET_TESTBED_get_index (peer); | ||
455 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
456 | " STATS %u - %s [%s]: %llu\n", | ||
457 | i, subsystem, name, | ||
458 | (unsigned long long) value); | ||
459 | |||
460 | return GNUNET_OK; | ||
461 | } | ||
462 | |||
463 | |||
464 | /** | ||
465 | * Task check that keepalives were sent and received. | ||
466 | * | ||
467 | * @param cls Closure (NULL). | ||
468 | */ | ||
469 | static void | ||
470 | collect_stats (void *cls) | ||
471 | { | ||
472 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
473 | "Start collecting statistics...\n"); | ||
474 | stats_op = GNUNET_TESTBED_get_statistics (peers_total, | ||
475 | testbed_handles, | ||
476 | NULL, NULL, | ||
477 | &stats_iterator, | ||
478 | &stats_cont, NULL); | ||
479 | } | ||
480 | |||
481 | |||
482 | /** | ||
483 | * @brief Finish profiler normally. Signal finish and start collecting stats. | ||
484 | * | ||
485 | * @param cls Closure (unused). | ||
486 | */ | ||
487 | static void | ||
488 | finish_profiler (void *cls) | ||
489 | { | ||
490 | test_finished = GNUNET_YES; | ||
491 | show_end_data (); | ||
492 | GNUNET_SCHEDULER_add_now (&collect_stats, NULL); | ||
493 | } | ||
494 | |||
495 | |||
496 | /** | ||
497 | * Set the total number of running peers. | ||
498 | * | ||
499 | * @param target Desired number of running peers. | ||
500 | */ | ||
501 | static void | ||
502 | adjust_running_peers (unsigned int target) | ||
503 | { | ||
504 | struct GNUNET_TESTBED_Operation *op; | ||
505 | unsigned int delta; | ||
506 | unsigned int run; | ||
507 | unsigned int i; | ||
508 | unsigned int r; | ||
509 | |||
510 | GNUNET_assert (target <= peers_total); | ||
511 | |||
512 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target); | ||
513 | if (target > peers_running) | ||
514 | { | ||
515 | delta = target - peers_running; | ||
516 | run = GNUNET_YES; | ||
517 | } | ||
518 | else | ||
519 | { | ||
520 | delta = peers_running - target; | ||
521 | run = GNUNET_NO; | ||
522 | } | ||
523 | |||
524 | for (i = 0; i < delta; i++) | ||
525 | { | ||
526 | do | ||
527 | { | ||
528 | r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, | ||
529 | peers_total - peers_pinging); | ||
530 | r += peers_pinging; | ||
531 | } | ||
532 | while (peers[r].up == run || NULL != peers[r].incoming); | ||
533 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n", | ||
534 | run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id)); | ||
535 | |||
536 | if (NULL != peers[r].ping_task) | ||
537 | { | ||
538 | GNUNET_SCHEDULER_cancel (peers[r].ping_task); | ||
539 | peers[r].ping_task = NULL; | ||
540 | } | ||
541 | if (NULL != peers[r].ping_ntr) | ||
542 | { | ||
543 | GNUNET_CADET_notify_transmit_ready_cancel (peers[r].ping_ntr); | ||
544 | peers[r].ping_ntr = NULL; | ||
545 | } | ||
546 | peers[r].up = run; | ||
547 | |||
548 | if (NULL != peers[r].ch) | ||
549 | GNUNET_CADET_channel_destroy (peers[r].ch); | ||
550 | peers[r].ch = NULL; | ||
551 | if (NULL != peers[r].dest) | ||
552 | { | ||
553 | if (NULL != peers[r].dest->incoming_ch) | ||
554 | GNUNET_CADET_channel_destroy (peers[r].dest->incoming_ch); | ||
555 | peers[r].dest->incoming_ch = NULL; | ||
556 | } | ||
557 | |||
558 | op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r], | ||
559 | "cadet", NULL, NULL, run); | ||
560 | GNUNET_break (NULL != op); | ||
561 | peers_running += run ? 1 : -1; | ||
562 | GNUNET_assert (peers_running > 0); | ||
563 | } | ||
564 | } | ||
565 | |||
566 | |||
567 | /** | ||
568 | * @brief Move to next round. | ||
569 | * | ||
570 | * @param cls Closure (round #). | ||
571 | */ | ||
572 | static void | ||
573 | next_rnd (void *cls) | ||
574 | { | ||
575 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
576 | "ROUND %u\n", | ||
577 | current_round); | ||
578 | if (0.0 == rounds[current_round]) | ||
579 | { | ||
580 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n"); | ||
581 | GNUNET_SCHEDULER_add_now (&finish_profiler, NULL); | ||
582 | return; | ||
583 | } | ||
584 | adjust_running_peers (rounds[current_round] * peers_total); | ||
585 | current_round++; | ||
586 | |||
587 | round_task = GNUNET_SCHEDULER_add_delayed (round_time, | ||
588 | &next_rnd, | ||
589 | NULL); | ||
590 | } | ||
591 | |||
592 | |||
593 | /** | ||
594 | * Transmit ping callback. | ||
595 | * | ||
596 | * @param cls Closure (peer for PING, NULL for PONG). | ||
597 | * @param size Size of the transmit buffer. | ||
598 | * @param buf Pointer to the beginning of the buffer. | ||
599 | * | ||
600 | * @return Number of bytes written to buf. | ||
601 | */ | ||
602 | static size_t | ||
603 | tmt_rdy_ping (void *cls, size_t size, void *buf); | ||
604 | |||
605 | |||
606 | /** | ||
607 | * Transmit pong callback. | ||
608 | * | ||
609 | * @param cls Closure (copy of PING message, to be freed). | ||
610 | * @param size Size of the buffer we have. | ||
611 | * @param buf Buffer to copy data to. | ||
612 | */ | ||
613 | static size_t | ||
614 | tmt_rdy_pong (void *cls, size_t size, void *buf) | ||
615 | { | ||
616 | struct CadetPingMessage *ping = cls; | ||
617 | struct CadetPingMessage *pong; | ||
618 | |||
619 | if ((0 == size) || (NULL == buf)) | ||
620 | { | ||
621 | GNUNET_free (ping); | ||
622 | return 0; | ||
623 | } | ||
624 | pong = (struct CadetPingMessage *) buf; | ||
625 | GNUNET_memcpy (pong, ping, sizeof(*ping)); | ||
626 | pong->header.type = htons (PONG); | ||
627 | |||
628 | GNUNET_free (ping); | ||
629 | return sizeof(*ping); | ||
630 | } | ||
631 | |||
632 | |||
633 | /** | ||
634 | * @brief Send a ping to destination | ||
635 | * | ||
636 | * @param cls Closure (peer). | ||
637 | */ | ||
638 | static void | ||
639 | ping (void *cls) | ||
640 | { | ||
641 | struct CadetPeer *peer = cls; | ||
642 | |||
643 | peer->ping_task = NULL; | ||
644 | if (GNUNET_YES == test_finished) | ||
645 | return; | ||
646 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
647 | "%u -> %u (%u)\n", | ||
648 | get_index (peer), | ||
649 | get_index (peer->dest), | ||
650 | peer->data_sent); | ||
651 | peer->ping_ntr = GNUNET_CADET_notify_transmit_ready (peer->ch, GNUNET_NO, | ||
652 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
653 | sizeof(struct | ||
654 | CadetPingMessage), | ||
655 | &tmt_rdy_ping, peer); | ||
656 | } | ||
657 | |||
658 | |||
659 | /** | ||
660 | * @brief Reply with a pong to origin. | ||
661 | * | ||
662 | */ | ||
663 | static void | ||
664 | pong (struct GNUNET_CADET_Channel *channel, | ||
665 | const struct CadetPingMessage *ping) | ||
666 | { | ||
667 | struct CadetPingMessage *copy; | ||
668 | |||
669 | copy = GNUNET_new (struct CadetPingMessage); | ||
670 | *copy = *ping; | ||
671 | GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO, | ||
672 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
673 | sizeof(struct CadetPingMessage), | ||
674 | &tmt_rdy_pong, copy); | ||
675 | } | ||
676 | |||
677 | |||
678 | /** | ||
679 | * Transmit ping callback | ||
680 | * | ||
681 | * @param cls Closure (peer). | ||
682 | * @param size Size of the buffer we have. | ||
683 | * @param buf Buffer to copy data to. | ||
684 | */ | ||
685 | static size_t | ||
686 | tmt_rdy_ping (void *cls, size_t size, void *buf) | ||
687 | { | ||
688 | struct CadetPeer *peer = cls; | ||
689 | struct CadetPingMessage *msg = buf; | ||
690 | |||
691 | peer->ping_ntr = NULL; | ||
692 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
693 | "tmt_rdy called, filling buffer\n"); | ||
694 | if ((size < sizeof(struct CadetPingMessage)) || (NULL == buf)) | ||
695 | { | ||
696 | GNUNET_break (GNUNET_YES == test_finished); | ||
697 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
698 | "size %u, buf %p, data_sent %u, data_received %u\n", | ||
699 | (unsigned int) size, | ||
700 | buf, | ||
701 | peer->data_sent, | ||
702 | peer->data_received); | ||
703 | |||
704 | return 0; | ||
705 | } | ||
706 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
707 | "Sending: msg %d\n", | ||
708 | peer->data_sent); | ||
709 | msg->header.size = htons (size); | ||
710 | msg->header.type = htons (PING); | ||
711 | msg->counter = htonl (peer->data_sent++); | ||
712 | msg->round_number = htonl (current_round); | ||
713 | msg->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ()); | ||
714 | peer->pings[current_round]++; | ||
715 | peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD), | ||
716 | &ping, peer); | ||
717 | |||
718 | return sizeof(struct CadetPingMessage); | ||
719 | } | ||
720 | |||
721 | |||
722 | /** | ||
723 | * Function is called whenever a PING message is received. | ||
724 | * | ||
725 | * @param cls closure (peer #, set from GNUNET_CADET_connect) | ||
726 | * @param channel connection to the other end | ||
727 | * @param channel_ctx place to store local state associated with the channel | ||
728 | * @param message the actual message | ||
729 | * @return GNUNET_OK to keep the connection open, | ||
730 | * GNUNET_SYSERR to close it (signal serious error) | ||
731 | */ | ||
732 | int | ||
733 | ping_handler (void *cls, struct GNUNET_CADET_Channel *channel, | ||
734 | void **channel_ctx, | ||
735 | const struct GNUNET_MessageHeader *message) | ||
736 | { | ||
737 | long n = (long) cls; | ||
738 | |||
739 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
740 | "%u got PING\n", | ||
741 | (unsigned int) n); | ||
742 | GNUNET_CADET_receive_done (channel); | ||
743 | if (GNUNET_NO == test_finished) | ||
744 | pong (channel, (struct CadetPingMessage *) message); | ||
745 | |||
746 | return GNUNET_OK; | ||
747 | } | ||
748 | |||
749 | |||
750 | /** | ||
751 | * Function is called whenever a PONG message is received. | ||
752 | * | ||
753 | * @param cls closure (peer #, set from GNUNET_CADET_connect) | ||
754 | * @param channel connection to the other end | ||
755 | * @param channel_ctx place to store local state associated with the channel | ||
756 | * @param message the actual message | ||
757 | * @return GNUNET_OK to keep the connection open, | ||
758 | * GNUNET_SYSERR to close it (signal serious error) | ||
759 | */ | ||
760 | int | ||
761 | pong_handler (void *cls, struct GNUNET_CADET_Channel *channel, | ||
762 | void **channel_ctx, | ||
763 | const struct GNUNET_MessageHeader *message) | ||
764 | { | ||
765 | long n = (long) cls; | ||
766 | struct CadetPeer *peer; | ||
767 | struct CadetPingMessage *msg; | ||
768 | struct GNUNET_TIME_Absolute send_time; | ||
769 | struct GNUNET_TIME_Relative latency; | ||
770 | unsigned int r /* Ping round */; | ||
771 | float delta; | ||
772 | |||
773 | GNUNET_CADET_receive_done (channel); | ||
774 | peer = &peers[n]; | ||
775 | |||
776 | msg = (struct CadetPingMessage *) message; | ||
777 | |||
778 | send_time = GNUNET_TIME_absolute_ntoh (msg->timestamp); | ||
779 | latency = GNUNET_TIME_absolute_get_duration (send_time); | ||
780 | r = ntohl (msg->round_number); | ||
781 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u (%u) latency: %s\n", | ||
782 | get_index (peer), | ||
783 | get_index (peer->dest), | ||
784 | (uint32_t) ntohl (msg->counter), | ||
785 | GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO)); | ||
786 | |||
787 | /* Online variance calculation */ | ||
788 | peer->pongs[r]++; | ||
789 | delta = latency.rel_value_us - peer->mean[r]; | ||
790 | peer->mean[r] = peer->mean[r] + delta / peer->pongs[r]; | ||
791 | peer->var[r] += delta * (latency.rel_value_us - peer->mean[r]); | ||
792 | |||
793 | return GNUNET_OK; | ||
794 | } | ||
795 | |||
796 | |||
797 | /** | ||
798 | * Handlers, for diverse services | ||
799 | */ | ||
800 | static struct GNUNET_CADET_MessageHandler handlers[] = { | ||
801 | { &ping_handler, PING, sizeof(struct CadetPingMessage) }, | ||
802 | { &pong_handler, PONG, sizeof(struct CadetPingMessage) }, | ||
803 | { NULL, 0, 0 } | ||
804 | }; | ||
805 | |||
806 | |||
807 | /** | ||
808 | * Method called whenever another peer has added us to a channel | ||
809 | * the other peer initiated. | ||
810 | * | ||
811 | * @param cls Closure. | ||
812 | * @param channel New handle to the channel. | ||
813 | * @param initiator Peer that started the channel. | ||
814 | * @param port Port this channel is connected to. | ||
815 | * @param options channel option flags | ||
816 | * @return Initial channel context for the channel | ||
817 | * (can be NULL -- that's not an error). | ||
818 | */ | ||
819 | static void * | ||
820 | incoming_channel (void *cls, struct GNUNET_CADET_Channel *channel, | ||
821 | const struct GNUNET_PeerIdentity *initiator, | ||
822 | const struct GNUNET_HashCode *port, | ||
823 | enum GNUNET_CADET_ChannelOption options) | ||
824 | { | ||
825 | long n = (long) cls; | ||
826 | struct CadetPeer *peer; | ||
827 | |||
828 | peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator); | ||
829 | GNUNET_assert (NULL != peer); | ||
830 | if (NULL == peers[n].incoming) | ||
831 | { | ||
832 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
833 | "WARMUP %3u: %u <= %u\n", | ||
834 | peers_warmup, | ||
835 | (unsigned int) n, | ||
836 | get_index (peer)); | ||
837 | peers_warmup++; | ||
838 | if (peers_warmup < peers_total) | ||
839 | return NULL; | ||
840 | if (NULL != test_task) | ||
841 | { | ||
842 | GNUNET_SCHEDULER_cancel (test_task); | ||
843 | test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
844 | &start_test, NULL); | ||
845 | } | ||
846 | return NULL; | ||
847 | } | ||
848 | GNUNET_assert (peer == peers[n].incoming); | ||
849 | GNUNET_assert (peer->dest == &peers[n]); | ||
850 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
851 | "%u <= %u %p\n", | ||
852 | (unsigned int) n, | ||
853 | get_index (peer), | ||
854 | channel); | ||
855 | peers[n].incoming_ch = channel; | ||
856 | |||
857 | return NULL; | ||
858 | } | ||
859 | |||
860 | |||
861 | /** | ||
862 | * Function called whenever an inbound channel is destroyed. Should clean up | ||
863 | * any associated state. | ||
864 | * | ||
865 | * @param cls closure (set from GNUNET_CADET_connect) | ||
866 | * @param channel connection to the other end (henceforth invalid) | ||
867 | * @param channel_ctx place where local state associated | ||
868 | * with the channel is stored | ||
869 | */ | ||
870 | static void | ||
871 | channel_cleaner (void *cls, | ||
872 | const struct GNUNET_CADET_Channel *channel, | ||
873 | void *channel_ctx) | ||
874 | { | ||
875 | long n = (long) cls; | ||
876 | struct CadetPeer *peer = &peers[n]; | ||
877 | |||
878 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
879 | "Channel %p disconnected at peer %ld\n", channel, n); | ||
880 | if (peer->ch == channel) | ||
881 | peer->ch = NULL; | ||
882 | } | ||
883 | |||
884 | |||
885 | /** | ||
886 | * Select a random peer that has no incoming channel | ||
887 | * | ||
888 | * @param peer ID of the peer connecting. NULL if irrelevant (warmup). | ||
889 | * | ||
890 | * @return Random peer not yet connected to. | ||
891 | */ | ||
892 | static struct CadetPeer * | ||
893 | select_random_peer (struct CadetPeer *peer) | ||
894 | { | ||
895 | unsigned int r; | ||
896 | |||
897 | do | ||
898 | { | ||
899 | r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, peers_total); | ||
900 | } | ||
901 | while (NULL != peers[r].incoming); | ||
902 | peers[r].incoming = peer; | ||
903 | |||
904 | return &peers[r]; | ||
905 | } | ||
906 | |||
907 | |||
908 | /** | ||
909 | * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES. | ||
910 | * | ||
911 | * Testcase continues when the root receives confirmation of connected peers, | ||
912 | * on callback function ch. | ||
913 | * | ||
914 | * @param cls Closure (unused). | ||
915 | */ | ||
916 | static void | ||
917 | start_test (void *cls) | ||
918 | { | ||
919 | unsigned long i; | ||
920 | |||
921 | test_task = NULL; | ||
922 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n"); | ||
923 | |||
924 | |||
925 | for (i = 0; i < peers_pinging; i++) | ||
926 | { | ||
927 | peers[i].dest = select_random_peer (&peers[i]); | ||
928 | peers[i].ch = GNUNET_CADET_channel_create (peers[i].cadet, NULL, | ||
929 | &peers[i].dest->id, | ||
930 | GC_u2h (1)); | ||
931 | if (NULL == peers[i].ch) | ||
932 | { | ||
933 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Channel %lu failed\n", i); | ||
934 | GNUNET_CADET_TEST_cleanup (test_ctx); | ||
935 | return; | ||
936 | } | ||
937 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
938 | "%lu => %u %p\n", | ||
939 | i, | ||
940 | get_index (peers[i].dest), | ||
941 | peers[i].ch); | ||
942 | peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000), | ||
943 | &ping, &peers[i]); | ||
944 | } | ||
945 | peers_running = peers_total; | ||
946 | if (NULL != disconnect_task) | ||
947 | GNUNET_SCHEDULER_cancel (disconnect_task); | ||
948 | disconnect_task = | ||
949 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (round_time, | ||
950 | number_rounds | ||
951 | + 1), | ||
952 | &disconnect_cadet_peers, | ||
953 | (void *) __LINE__); | ||
954 | round_task = GNUNET_SCHEDULER_add_delayed (round_time, | ||
955 | &next_rnd, | ||
956 | NULL); | ||
957 | } | ||
958 | |||
959 | |||
960 | /** | ||
961 | * Do warmup: create some channels to spread information about the topology. | ||
962 | */ | ||
963 | static void | ||
964 | warmup (void) | ||
965 | { | ||
966 | struct CadetPeer *peer; | ||
967 | unsigned int i; | ||
968 | |||
969 | for (i = 0; i < peers_total; i++) | ||
970 | { | ||
971 | peer = select_random_peer (NULL); | ||
972 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "WARMUP %u => %u\n", | ||
973 | i, get_index (peer)); | ||
974 | peers[i].warmup_ch = | ||
975 | GNUNET_CADET_channel_create (peers[i].cadet, NULL, &peer->id, | ||
976 | GC_u2h (1)); | ||
977 | if (NULL == peers[i].warmup_ch) | ||
978 | { | ||
979 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Warmup %u failed\n", i); | ||
980 | GNUNET_CADET_TEST_cleanup (test_ctx); | ||
981 | return; | ||
982 | } | ||
983 | } | ||
984 | } | ||
985 | |||
986 | |||
987 | /** | ||
988 | * Callback to be called when the requested peer information is available | ||
989 | * | ||
990 | * @param cls the closure from GNUNET_TESTBED_peer_get_information() | ||
991 | * @param op the operation this callback corresponds to | ||
992 | * @param pinfo the result; will be NULL if the operation has failed | ||
993 | * @param emsg error message if the operation has failed; | ||
994 | * NULL if the operation is successful | ||
995 | */ | ||
996 | static void | ||
997 | peer_id_cb (void *cls, | ||
998 | struct GNUNET_TESTBED_Operation *op, | ||
999 | const struct GNUNET_TESTBED_PeerInformation *pinfo, | ||
1000 | const char *emsg) | ||
1001 | { | ||
1002 | long n = (long) cls; | ||
1003 | |||
1004 | if ((NULL == pinfo) || (NULL != emsg)) | ||
1005 | { | ||
1006 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg); | ||
1007 | abort_test (__LINE__); | ||
1008 | return; | ||
1009 | } | ||
1010 | peers[n].id = *(pinfo->result.id); | ||
1011 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1012 | "%ld id: %s\n", | ||
1013 | n, | ||
1014 | GNUNET_i2s (&peers[n].id)); | ||
1015 | GNUNET_break (GNUNET_OK == | ||
1016 | GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n], | ||
1017 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); | ||
1018 | |||
1019 | GNUNET_TESTBED_operation_done (peers[n].op); | ||
1020 | peers[n].op = NULL; | ||
1021 | |||
1022 | p_ids++; | ||
1023 | if (p_ids < peers_total) | ||
1024 | return; | ||
1025 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n"); | ||
1026 | if (do_warmup) | ||
1027 | { | ||
1028 | struct GNUNET_TIME_Relative delay; | ||
1029 | |||
1030 | warmup (); | ||
1031 | delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, | ||
1032 | 100 * peers_total); | ||
1033 | test_task = GNUNET_SCHEDULER_add_delayed (delay, &start_test, NULL); | ||
1034 | return; /* start_test from incoming_channel */ | ||
1035 | } | ||
1036 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting in a second...\n"); | ||
1037 | test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
1038 | &start_test, NULL); | ||
1039 | } | ||
1040 | |||
1041 | |||
1042 | /** | ||
1043 | * test main: start test when all peers are connected | ||
1044 | * | ||
1045 | * @param cls Closure. | ||
1046 | * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end. | ||
1047 | * @param num_peers Number of peers that are running. | ||
1048 | * @param testbed_peers Array of peers. | ||
1049 | * @param cadetes Handle to each of the CADETs of the peers. | ||
1050 | */ | ||
1051 | static void | ||
1052 | tmain (void *cls, | ||
1053 | struct GNUNET_CADET_TEST_Context *ctx, | ||
1054 | unsigned int num_peers, | ||
1055 | struct GNUNET_TESTBED_Peer **testbed_peers, | ||
1056 | struct GNUNET_CADET_Handle **cadetes) | ||
1057 | { | ||
1058 | unsigned long i; | ||
1059 | |||
1060 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1061 | "test main\n"); | ||
1062 | test_ctx = ctx; | ||
1063 | GNUNET_assert (peers_total == num_peers); | ||
1064 | peers_running = num_peers; | ||
1065 | testbed_handles = testbed_peers; | ||
1066 | disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME, | ||
1067 | &disconnect_cadet_peers, | ||
1068 | (void *) __LINE__); | ||
1069 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); | ||
1070 | for (i = 0; i < peers_total; i++) | ||
1071 | { | ||
1072 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1073 | "requesting id %ld\n", | ||
1074 | i); | ||
1075 | peers[i].up = GNUNET_YES; | ||
1076 | peers[i].cadet = cadetes[i]; | ||
1077 | peers[i].op = | ||
1078 | GNUNET_TESTBED_peer_get_information (testbed_handles[i], | ||
1079 | GNUNET_TESTBED_PIT_IDENTITY, | ||
1080 | &peer_id_cb, (void *) i); | ||
1081 | } | ||
1082 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "requested peer ids\n"); | ||
1083 | /* Continues from pi_cb -> do_test */ | ||
1084 | } | ||
1085 | |||
1086 | |||
1087 | /** | ||
1088 | * Main: start profiler. | ||
1089 | */ | ||
1090 | int | ||
1091 | main (int argc, char *argv[]) | ||
1092 | { | ||
1093 | static const struct GNUNET_HashCode *ports[2]; | ||
1094 | const char *config_file; | ||
1095 | |||
1096 | config_file = ".profiler.conf"; | ||
1097 | |||
1098 | if (4 > argc) | ||
1099 | { | ||
1100 | fprintf (stderr, | ||
1101 | "usage: %s ROUND_TIME PEERS PINGS [DO_WARMUP]\n", | ||
1102 | argv[0]); | ||
1103 | fprintf (stderr, | ||
1104 | "example: %s 30s 16 1 Y\n", | ||
1105 | argv[0]); | ||
1106 | return 1; | ||
1107 | } | ||
1108 | |||
1109 | if (GNUNET_OK != | ||
1110 | GNUNET_STRINGS_fancy_time_to_relative (argv[1], | ||
1111 | &round_time)) | ||
1112 | { | ||
1113 | fprintf (stderr, | ||
1114 | "%s is not a valid time\n", | ||
1115 | argv[1]); | ||
1116 | return 1; | ||
1117 | } | ||
1118 | |||
1119 | peers_total = atoll (argv[2]); | ||
1120 | if (2 > peers_total) | ||
1121 | { | ||
1122 | fprintf (stderr, | ||
1123 | "%s peers is not valid (> 2)\n", | ||
1124 | argv[1]); | ||
1125 | return 1; | ||
1126 | } | ||
1127 | peers = GNUNET_new_array (peers_total, | ||
1128 | struct CadetPeer); | ||
1129 | peers_pinging = atoll (argv[3]); | ||
1130 | |||
1131 | if (peers_total < 2 * peers_pinging) | ||
1132 | { | ||
1133 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1134 | "not enough peers, total should be > 2 * peers_pinging\n"); | ||
1135 | return 1; | ||
1136 | } | ||
1137 | |||
1138 | do_warmup = (5 > argc || argv[4][0] != 'N'); | ||
1139 | |||
1140 | ids = GNUNET_CONTAINER_multipeermap_create (2 * peers_total, | ||
1141 | GNUNET_YES); | ||
1142 | GNUNET_assert (NULL != ids); | ||
1143 | p_ids = 0; | ||
1144 | test_finished = GNUNET_NO; | ||
1145 | ports[0] = GC_u2h (1); | ||
1146 | ports[1] = 0; | ||
1147 | GNUNET_CADET_TEST_run ("cadet-profiler", config_file, peers_total, | ||
1148 | &tmain, NULL, /* tmain cls */ | ||
1149 | &incoming_channel, &channel_cleaner, | ||
1150 | handlers, ports); | ||
1151 | GNUNET_free (peers); | ||
1152 | |||
1153 | return 0; | ||
1154 | } | ||
1155 | |||
1156 | |||
1157 | /* end of gnunet-cadet-profiler.c */ | ||