- nse-style round cycling
[oweals/gnunet.git] / src / mesh / gnunet-mesh-profiler.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file mesh/gnunet-mesh-profiler.c
22  *
23  * @brief Profiler for mesh experiments.
24  */
25 #include <stdio.h>
26 #include "platform.h"
27 #include "mesh_test_lib.h"
28 #include "gnunet_mesh_service.h"
29 #include "gnunet_statistics_service.h"
30
31
32 #define PING 1
33 #define PONG 2
34
35 /**
36  * How namy peers to run
37  */
38 #define TOTAL_PEERS 10
39
40 /**
41  * Duration of each round.
42  */
43 #define ROUND_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
44
45 /**
46  * Paximum ping period in milliseconds. Real period = rand (0, PING_PERIOD)
47  */
48 #define PING_PERIOD 2000
49
50 /**
51  * How long until we give up on connecting the peers?
52  */
53 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
54
55 /**
56  * Time to wait for stuff that should be rather fast
57  */
58 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
59
60 static float rounds[] = {0.8, 0.7, 0.6, 0.5, 0.0};
61
62 struct MeshPeer
63 {
64   /**
65    * Testbed Operation (to get peer id, etc).
66    */
67   struct GNUNET_TESTBED_Operation *op;
68
69   /**
70    * Peer ID.
71    */
72   struct GNUNET_PeerIdentity id;
73
74   /**
75    * Mesh handle for the root peer
76    */
77   struct GNUNET_MESH_Handle *mesh;
78
79   /**
80    * Channel handle for the root peer
81    */
82   struct GNUNET_MESH_Channel *ch;
83
84   /**
85    * Channel handle for the dest peer
86    */
87   struct GNUNET_MESH_Channel *incoming_ch;
88
89   /**
90    * Number of payload packes sent
91    */
92   int data_sent;
93
94   /**
95    * Number of payload packets received
96    */
97   int data_received;
98
99   int up;
100
101   struct MeshPeer *dest;
102   struct MeshPeer *incoming;
103   GNUNET_SCHEDULER_TaskIdentifier ping_task;
104   struct GNUNET_TIME_Absolute timestamp;
105 };
106
107 /**
108  * GNUNET_PeerIdentity -> MeshPeer
109  */
110 static struct GNUNET_CONTAINER_MultiPeerMap *ids;
111
112 /**
113  * Testbed peer handles.
114  */
115 static struct GNUNET_TESTBED_Peer **testbed_handles;
116
117 /**
118  * Testbed Operation (to get stats).
119  */
120 static struct GNUNET_TESTBED_Operation *stats_op;
121
122 /**
123  * How many events have happened
124  */
125 static int ok;
126
127 /**
128  * Number of events expected to conclude the test successfully.
129  */
130 static int ok_goal;
131
132 /**
133  * Size of each test packet
134  */
135 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
136
137 /**
138  * Operation to get peer ids.
139  */
140 struct MeshPeer peers[TOTAL_PEERS];
141
142 /**
143  * Peer ids counter.
144  */
145 static unsigned int p_ids;
146
147 /**
148  * Total number of currently running peers.
149  */
150 static unsigned long long peers_running;
151
152 /**
153  * Test context (to shut down).
154  */
155 static struct GNUNET_MESH_TEST_Context *test_ctx;
156
157 /**
158  * Task called to shutdown test.
159  */
160 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
161
162 /**
163  * Task called to disconnect peers, before shutdown.
164  */
165 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
166
167 /**
168  * Task to perform tests
169  */
170 static GNUNET_SCHEDULER_TaskIdentifier test_task;
171
172
173 /**
174  * Flag to notify callbacks not to generate any new traffic anymore.
175  */
176 static int test_finished;
177
178 /**
179  * Calculate a random delay.
180  *
181  * @param max Exclusive maximum, in ms.
182  *
183  * @return A time between 0 a max-1 ms.
184  */
185 static struct GNUNET_TIME_Relative
186 delay_ms_rnd (unsigned int max)
187 {
188   unsigned int rnd;
189
190   rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max);
191   return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd);
192 }
193
194
195 /**
196  * Get the index of a peer in the peers array.
197  *
198  * @param peer Peer whose index to get.
199  *
200  * @return Index of peer in peers.
201  */
202 static unsigned int
203 get_index (struct MeshPeer *peer)
204 {
205   return peer - peers;
206 }
207
208
209 /**
210  * Show the results of the test (banwidth acheived) and log them to GAUGER
211  */
212 static void
213 show_end_data (void)
214 {
215 }
216
217
218 /**
219  * Shut down peergroup, clean up.
220  *
221  * @param cls Closure (unused).
222  * @param tc Task Context.
223  */
224 static void
225 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
226 {
227   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Ending test.\n");
228   shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
229 }
230
231
232 /**
233  * Disconnect from mesh services af all peers, call shutdown.
234  *
235  * @param cls Closure (unused).
236  * @param tc Task Context.
237  */
238 static void
239 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
240 {
241   long line = (long) cls;
242   unsigned int i;
243
244   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245               "disconnecting mesh service of peers, called from line %ld\n",
246               line);
247   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
248   for (i = 0; i < TOTAL_PEERS; i++)
249   {
250     GNUNET_TESTBED_operation_done (peers[i].op);
251     GNUNET_MESH_channel_destroy (peers[i].ch);
252     GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
253   }
254   GNUNET_MESH_TEST_cleanup (test_ctx);
255   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
256   {
257     GNUNET_SCHEDULER_cancel (shutdown_handle);
258   }
259   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
260 }
261
262
263 /**
264  * Finish test normally: schedule disconnect and shutdown
265  *
266  * @param line Line in the code the abort is requested from (__LINE__).
267  */
268 static void
269 abort_test (long line)
270 {
271   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
272   {
273     GNUNET_SCHEDULER_cancel (disconnect_task);
274     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
275                                                 (void *) line);
276   }
277 }
278
279 /**
280  * Stats callback. Finish the stats testbed operation and when all stats have
281  * been iterated, shutdown the test.
282  *
283  * @param cls closure
284  * @param op the operation that has been finished
285  * @param emsg error message in case the operation has failed; will be NULL if
286  *          operation has executed successfully.
287  */
288 static void
289 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
290 {
291   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
292   GNUNET_TESTBED_operation_done (stats_op);
293
294   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
295     GNUNET_SCHEDULER_cancel (disconnect_task);
296   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
297                                               (void *) __LINE__);
298
299 }
300
301
302 /**
303  * Process statistic values.
304  *
305  * @param cls closure
306  * @param peer the peer the statistic belong to
307  * @param subsystem name of subsystem that created the statistic
308  * @param name the name of the datum
309  * @param value the current value
310  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
311  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
312  */
313 static int
314 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
315                 const char *subsystem, const char *name,
316                 uint64_t value, int is_persistent)
317 {
318   uint32_t i;
319
320   i = GNUNET_TESTBED_get_index (peer);
321   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " STATS %u - %s [%s]: %llu\n",
322               i, subsystem, name, value);
323
324   return GNUNET_OK;
325 }
326
327
328 /**
329  * Task check that keepalives were sent and received.
330  *
331  * @param cls Closure (NULL).
332  * @param tc Task Context.
333  */
334 static void
335 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
336 {
337   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
338     return;
339
340   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
341   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
342   stats_op = GNUNET_TESTBED_get_statistics (TOTAL_PEERS, testbed_handles,
343                                             NULL, NULL,
344                                             stats_iterator, stats_cont, NULL);
345 }
346
347
348 /**
349  * @brief Finish profiler normally. Signal finish and start collecting stats.
350  *
351  * @param cls Closure (unused).
352  * @param tc Task context.
353  */
354 static void
355 finish_profiler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
356 {
357   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
358     return;
359
360   test_finished = GNUNET_YES;
361   show_end_data();
362   GNUNET_SCHEDULER_add_now (&collect_stats, NULL);
363 }
364
365 /**
366  * Set the total number of running peers.
367  *
368  * @param target Desired number of running peers.
369  */
370 static void
371 adjust_running_peers (unsigned int target)
372 {
373   struct GNUNET_TESTBED_Operation *op;
374   unsigned int delta;
375   unsigned int run;
376   unsigned int i;
377   unsigned int r;
378
379   GNUNET_assert (target <= TOTAL_PEERS);
380
381   if (target > peers_running)
382   {
383     delta = target - peers_running;
384     run = GNUNET_YES;
385   }
386   else
387   {
388     delta = peers_running - target;
389     run = GNUNET_NO;
390   }
391
392   for (i = 0; i < delta; i++)
393   {
394     do {
395       r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
396     } while (!run == peers[r].up);
397     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "St%s peer %\n",
398                 run ? "arting" : "opping", GNUNET_i2s (&peers[r].id));
399     op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
400                                              "mesh", NULL, NULL, run);
401     GNUNET_break (NULL != op);
402   }
403 }
404
405
406 /**
407  * @brief Move to next round.
408  *
409  * @param cls Closure (round #).
410  * @param tc Task context.
411  */
412 static void
413 next_round (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
414 {
415   long round = (long) cls;
416
417   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
418     return;
419
420   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "ROUND %ld\n", round);
421   if (0.0 == rounds[round])
422   {
423     GNUNET_SCHEDULER_add_now (&finish_profiler, NULL);
424     return;
425   }
426   adjust_running_peers (rounds[round] * TOTAL_PEERS);
427
428   GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_round, (void *) (round + 1));
429 }
430
431
432 /**
433  * Transmit ready callback.
434  *
435  * @param cls Closure (peer for PING, NULL for PONG).
436  * @param size Size of the tranmist buffer.
437  * @param buf Pointer to the beginning of the buffer.
438  *
439  * @return Number of bytes written to buf.
440  */
441 static size_t
442 tmt_rdy (void *cls, size_t size, void *buf);
443
444
445 /**
446  * @brief Send a ping to destination
447  *
448  * @param cls Closure (peer).
449  * @param tc Task context.
450  */
451 static void
452 ping (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
453 {
454   struct MeshPeer *peer = (struct MeshPeer *) cls;
455
456   peer->ping_task = GNUNET_SCHEDULER_NO_TASK;
457
458   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0
459       || GNUNET_YES == test_finished
460       || 0 != peer->timestamp.abs_value_us)
461     return;
462
463   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u -> %u\n",
464               get_index (peer), get_index (peer->dest));
465
466   GNUNET_MESH_notify_transmit_ready (peer->ch, GNUNET_NO,
467                                      GNUNET_TIME_UNIT_FOREVER_REL,
468                                      size_payload, &tmt_rdy, peer);
469 }
470
471 /**
472  * @brief Reply with a pong to origin.
473  *
474  * @param cls Closure (peer).
475  * @param tc Task context.
476  */
477 static void
478 pong (struct GNUNET_MESH_Channel *channel)
479 {
480   GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
481                                      GNUNET_TIME_UNIT_FOREVER_REL,
482                                      size_payload, &tmt_rdy, NULL);
483 }
484
485
486 /**
487  * Transmit ready callback
488  *
489  * @param cls Closure (peer for PING, NULL for PONG).
490  * @param size Size of the buffer we have.
491  * @param buf Buffer to copy data to.
492  */
493 static size_t
494 tmt_rdy (void *cls, size_t size, void *buf)
495 {
496   struct MeshPeer *peer = (struct MeshPeer *) cls;
497   struct GNUNET_MessageHeader *msg = buf;
498   uint32_t *data;
499
500   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
501   if (size < size_payload || NULL == buf)
502   {
503     GNUNET_break (0);
504     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505                 "size %u, buf %p, data_sent %u, data_received %u\n",
506                 size, buf, peer->data_sent, peer->data_received);
507
508     return 0;
509   }
510   msg->size = htons (size);
511   if (NULL == peer)
512   {
513     msg->type = htons (PONG);
514     return sizeof (*msg);
515   }
516
517   msg->type = htons (PING);
518   data = (uint32_t *) &msg[1];
519   *data = htonl (peer->data_sent);
520   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent: msg %d\n", peer->data_sent);
521   peer->data_sent++;
522   peer->timestamp = GNUNET_TIME_absolute_get ();
523   peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD),
524                                                   &ping, peer);
525
526   return size_payload;
527 }
528
529
530 /**
531  * Function is called whenever a PING message is received.
532  *
533  * @param cls closure (peer #, set from GNUNET_MESH_connect)
534  * @param channel connection to the other end
535  * @param channel_ctx place to store local state associated with the channel
536  * @param message the actual message
537  * @return GNUNET_OK to keep the connection open,
538  *         GNUNET_SYSERR to close it (signal serious error)
539  */
540 int
541 ping_handler (void *cls, struct GNUNET_MESH_Channel *channel,
542               void **channel_ctx,
543               const struct GNUNET_MessageHeader *message)
544 {
545   long n = (long) cls;
546
547   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u got PING\n", n);
548   GNUNET_MESH_receive_done (channel);
549   if (GNUNET_NO == test_finished)
550     pong (channel);
551
552   return GNUNET_OK;
553 }
554
555
556 /**
557  * Function is called whenever a PONG message is received.
558  *
559  * @param cls closure (peer #, set from GNUNET_MESH_connect)
560  * @param channel connection to the other end
561  * @param channel_ctx place to store local state associated with the channel
562  * @param message the actual message
563  * @return GNUNET_OK to keep the connection open,
564  *         GNUNET_SYSERR to close it (signal serious error)
565  */
566 int
567 pong_handler (void *cls, struct GNUNET_MESH_Channel *channel,
568               void **channel_ctx,
569               const struct GNUNET_MessageHeader *message)
570 {
571   long n = (long) cls;
572   struct MeshPeer *peer;
573   struct GNUNET_TIME_Relative latency;
574
575   GNUNET_MESH_receive_done (channel);
576   peer = &peers[n];
577
578   GNUNET_break (0 != peer->timestamp.abs_value_us);
579   latency = GNUNET_TIME_absolute_get_duration (peer->timestamp);
580   FPRINTF (stderr, "%ld latency: %s\n",
581            n, GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
582
583   if (GNUNET_SCHEDULER_NO_TASK == peer->ping_task)
584   {
585     peer->timestamp = GNUNET_TIME_absolute_get ();
586     peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (60 * 1000),
587                                                     &ping, peer);
588   }
589   else
590   {
591     peer->timestamp.abs_value_us = 0;
592   }
593
594   return GNUNET_OK;
595 }
596
597
598 /**
599  * Handlers, for diverse services
600  */
601 static struct GNUNET_MESH_MessageHandler handlers[] = {
602   {&ping_handler, PING, sizeof (struct GNUNET_MessageHeader)},
603   {&pong_handler, PONG, sizeof (struct GNUNET_MessageHeader)},
604   {NULL, 0, 0}
605 };
606
607
608 /**
609  * Method called whenever another peer has added us to a channel
610  * the other peer initiated.
611  *
612  * @param cls Closure.
613  * @param channel New handle to the channel.
614  * @param initiator Peer that started the channel.
615  * @param port Port this channel is connected to.
616  * @param options channel option flags
617  * @return Initial channel context for the channel
618  *         (can be NULL -- that's not an error).
619  */
620 static void *
621 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
622                  const struct GNUNET_PeerIdentity *initiator,
623                  uint32_t port, enum GNUNET_MESH_ChannelOption options)
624 {
625   long n = (long) cls;
626   struct MeshPeer *peer;
627
628   peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator);
629   GNUNET_assert (NULL != peer);
630   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <= %u\n", n, get_index (peer));
631   peers[n].incoming_ch = channel;
632
633   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
634   {
635     GNUNET_SCHEDULER_cancel (disconnect_task);
636     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
637                                                     &disconnect_mesh_peers,
638                                                     (void *) __LINE__);
639   }
640
641   return NULL;
642 }
643
644 /**
645  * Function called whenever an inbound channel is destroyed.  Should clean up
646  * any associated state.
647  *
648  * @param cls closure (set from GNUNET_MESH_connect)
649  * @param channel connection to the other end (henceforth invalid)
650  * @param channel_ctx place where local state associated
651  *                   with the channel is stored
652  */
653 static void
654 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
655                  void *channel_ctx)
656 {
657   long n = (long) cls;
658
659   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
660               "Incoming channel disconnected at peer %ld\n", n);
661   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
662
663   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
664   {
665     GNUNET_SCHEDULER_cancel (disconnect_task);
666     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
667                                                 (void *) __LINE__);
668   }
669 }
670
671
672 static struct MeshPeer *
673 select_random_peer (struct MeshPeer *peer)
674 {
675   unsigned int r;
676
677   do
678   {
679     r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, TOTAL_PEERS);
680   } while (NULL != peers[r].incoming);
681   peers[r].incoming = peer;
682
683   return &peers[r];
684 }
685
686 /**
687  * START THE TEST ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
688  *
689  * Testcase continues when the root receives confirmation of connected peers,
690  * on callback funtion ch.
691  *
692  * @param cls Closure (unsued).
693  * @param tc Task Context.
694  */
695 static void
696 start_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
697 {
698   enum GNUNET_MESH_ChannelOption flags;
699   unsigned long i;
700
701   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
702     return;
703
704   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
705
706   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
707     GNUNET_SCHEDULER_cancel (disconnect_task);
708   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
709                                                   &disconnect_mesh_peers,
710                                                   (void *) __LINE__);
711
712   flags = GNUNET_MESH_OPTION_DEFAULT;
713   for (i = 0; i < TOTAL_PEERS; i++)
714   {
715
716     peers[i].dest = select_random_peer (&peers[i]);
717     peers[i].ch = GNUNET_MESH_channel_create (peers[i].mesh, NULL,
718                                               &peers[i].dest->id,
719                                               1, flags);
720     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u => %u\n",
721                 i, get_index (peers[i].dest));
722     peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
723                                                        &ping, &peers[i]);
724   }
725   peers_running = TOTAL_PEERS;
726   GNUNET_SCHEDULER_add_delayed (ROUND_TIME, &next_round, NULL);
727 }
728
729
730 /**
731  * Callback to be called when the requested peer information is available
732  *
733  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
734  * @param op the operation this callback corresponds to
735  * @param pinfo the result; will be NULL if the operation has failed
736  * @param emsg error message if the operation has failed;
737  *             NULL if the operation is successfull
738  */
739 static void
740 peer_id_cb (void *cls,
741        struct GNUNET_TESTBED_Operation *op,
742        const struct GNUNET_TESTBED_PeerInformation *pinfo,
743        const char *emsg)
744 {
745   long n = (long) cls;
746
747   if (NULL == pinfo || NULL != emsg)
748   {
749     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
750     abort_test (__LINE__);
751     return;
752   }
753   peers[n].id = *(pinfo->result.id);
754   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u  id: %s\n",
755               n, GNUNET_i2s (&peers[n].id));
756   GNUNET_break (GNUNET_OK ==
757                 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n],
758                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
759   p_ids++;
760   if (p_ids < TOTAL_PEERS)
761     return;
762   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
763   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
764                                             &start_test, NULL);
765 }
766
767 /**
768  * test main: start test when all peers are connected
769  *
770  * @param cls Closure.
771  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
772  * @param num_peers Number of peers that are running.
773  * @param testbed_peers Array of peers.
774  * @param meshes Handle to each of the MESHs of the peers.
775  */
776 static void
777 tmain (void *cls,
778        struct GNUNET_MESH_TEST_Context *ctx,
779        unsigned int num_peers,
780        struct GNUNET_TESTBED_Peer **testbed_peers,
781        struct GNUNET_MESH_Handle **meshes)
782 {
783   unsigned long i;
784
785   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
786   ok = 0;
787   test_ctx = ctx;
788   GNUNET_assert (TOTAL_PEERS == num_peers);
789   peers_running = num_peers;
790   testbed_handles = testbed_peers;
791   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
792                                                   &disconnect_mesh_peers,
793                                                   (void *) __LINE__);
794   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
795                                                   &shutdown_task, NULL);
796   for (i = 0; i < TOTAL_PEERS; i++)
797   {
798     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requesting id %ld\n", i);
799     peers[i].up = GNUNET_YES;
800     peers[i].mesh = meshes[i];
801     peers[i].op =
802       GNUNET_TESTBED_peer_get_information (testbed_handles[i],
803                                            GNUNET_TESTBED_PIT_IDENTITY,
804                                            &peer_id_cb, (void *) i);
805   }
806   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
807   /* Continues from pi_cb -> do_test */
808 }
809
810
811 /**
812  * Main: start profiler.
813  */
814 int
815 main (int argc, char *argv[])
816 {
817   static uint32_t ports[2];
818   const char *config_file;
819
820   config_file = "test_mesh.conf";
821
822   ids = GNUNET_CONTAINER_multipeermap_create (2 * TOTAL_PEERS, GNUNET_YES);
823   GNUNET_assert (NULL != ids);
824   p_ids = 0;
825   test_finished = GNUNET_NO;
826   ports[0] = 1;
827   ports[1] = 0;
828   GNUNET_MESH_TEST_run ("mesh-profiler", config_file, TOTAL_PEERS,
829                         &tmain, NULL, /* tmain cls */
830                         &incoming_channel, &channel_cleaner,
831                         handlers, ports);
832
833   if (ok_goal > ok)
834   {
835     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
836                 "FAILED! (%d/%d)\n", ok, ok_goal);
837     return 1;
838   }
839   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
840   return 0;
841 }
842
843 /* end of gnunet-mesh-profiler.c */
844