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