- change test to profiler
[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 struct MeshPeer
55 {
56   /**
57    * Testbed Operation (to get peer id, etc).
58    */
59   struct GNUNET_TESTBED_Operation *op;
60
61   /**
62    * Testbed peer handle.
63    */
64   struct GNUNET_TESTBED_Peer *testbed_peer;
65
66   /**
67    * Peer ID.
68    */
69   struct GNUNET_PeerIdentity *id;
70
71   /**
72    * Mesh handle for the root peer
73    */
74   static struct GNUNET_MESH_Handle *mesh;
75
76   /**
77    * Channel handle for the root peer
78    */
79   static struct GNUNET_MESH_Channel *ch;
80
81   /**
82    * Channel handle for the dest peer
83    */
84   static struct GNUNET_MESH_Channel *incoming_ch;
85 };
86
87
88 /**
89  * How many events have happened
90  */
91 static int ok;
92
93 /**
94  * Number of events expected to conclude the test successfully.
95  */
96 int ok_goal;
97
98 /**
99  * Size of each test packet
100  */
101 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
102
103 /**
104  * Operation to get peer ids.
105  */
106 struct MeshPeer peers[TOTAL_PEERS];
107
108 /**
109  * Peer ids counter.
110  */
111 unsigned int p_ids;
112
113 /**
114  * Is the setup initialized?
115  */
116 static int initialized;
117
118 /**
119  * Number of payload packes sent
120  */
121 static int data_sent;
122
123 /**
124  * Number of payload packets received
125  */
126 static int data_received;
127
128 /**
129  * Number of payload packed explicitly (app level) acknowledged
130  */
131 static int data_ack;
132
133 /**
134  * Total number of currently running peers.
135  */
136 static unsigned long long peers_running;
137
138 /**
139  * Test context (to shut down).
140  */
141 struct GNUNET_MESH_TEST_Context *test_ctx;
142
143 /**
144  * Task called to shutdown test.
145  */
146 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
147
148 /**
149  * Task called to disconnect peers, before shutdown.
150  */
151 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
152
153 /**
154  * Task to perform tests
155  */
156 static GNUNET_SCHEDULER_TaskIdentifier test_task;
157
158 /**
159  * Time we started the data transmission (after channel has been established
160  * and initilized).
161  */
162 static struct GNUNET_TIME_Absolute start_time;
163
164 /**
165  *
166  */
167 static unsigned int ka_sent;
168 static unsigned int ka_received;
169
170
171 /**
172  * Show the results of the test (banwidth acheived) and log them to GAUGER
173  */
174 static void
175 show_end_data (void)
176 {
177   static struct GNUNET_TIME_Absolute end_time;
178   static struct GNUNET_TIME_Relative total_time;
179
180   end_time = GNUNET_TIME_absolute_get();
181   total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
182   FPRINTF (stderr, "Test time %s\n",
183            GNUNET_STRINGS_relative_time_to_string (total_time,
184                                                    GNUNET_YES));
185   FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
186            4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
187   FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
188            TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
189   GAUGER ("MESH", test_name,
190           TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000),
191           "packets/s");
192 }
193
194
195 /**
196  * Shut down peergroup, clean up.
197  *
198  * @param cls Closure (unused).
199  * @param tc Task Context.
200  */
201 static void
202 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
203 {
204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
205   shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
206 }
207
208
209 /**
210  * Disconnect from mesh services af all peers, call shutdown.
211  *
212  * @param cls Closure (unused).
213  * @param tc Task Context.
214  */
215 static void
216 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
217 {
218   long line = (long) cls;
219   unsigned int i;
220
221   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222               "disconnecting mesh service of peers, called from line %ld\n",
223               line);
224   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
225   for (i = 0; i < TOTAL_PEERS; i++)
226   {
227     GNUNET_TESTBED_operation_done (peers[i].op);
228     GNUNET_MESH_channel_destroy (peers[i].ch);
229     GNUNET_MESH_channel_destroy (peers[i].incoming_ch);
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
372   th = GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
373                                           GNUNET_TIME_UNIT_FOREVER_REL,
374                                           size_payload, &tmt_rdy, (void *) 1L);
375   if (NULL == th)
376     GNUNET_abort (0);
377 }
378
379
380 /**
381  * Transmit ready callback
382  *
383  * @param cls Closure (message type).
384  * @param size Size of the buffer we have.
385  * @param buf Buffer to copy data to.
386  */
387 size_t
388 tmt_rdy (void *cls, size_t size, void *buf)
389 {
390   struct GNUNET_MessageHeader *msg = buf;
391   uint32_t *data;
392
393   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy called, filling buffer\n");
394   if (size < size_payload || NULL == buf)
395   {
396     GNUNET_break (ok >= ok_goal - 2);
397     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398                 "size %u, buf %p, data_sent %u, data_received %u\n",
399                 size, buf, data_sent, data_received);
400     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal);
401
402     return 0;
403   }
404   msg->size = htons (size);
405   msg->type = htons ((long) cls);
406   data = (uint32_t *) &msg[1];
407   *data = htonl (data_sent);
408   if (GNUNET_NO == initialized)
409   {
410     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
411                 "sending initializer\n");
412   }
413   else
414   {
415     data_sent++;
416     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417               " Sent packet %d\n", data_sent);
418     if (data_sent < TOTAL_PACKETS)
419     {
420       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421               " Scheduling packet %d\n", data_sent + 1);
422       GNUNET_SCHEDULER_add_now (&data_task, NULL);
423     }
424   }
425
426   return size_payload;
427 }
428
429
430 /**
431  * Function is called whenever a message is received.
432  *
433  * @param cls closure (set from GNUNET_MESH_connect)
434  * @param channel connection to the other end
435  * @param channel_ctx place to store local state associated with the channel
436  * @param message the actual message
437  * @return GNUNET_OK to keep the connection open,
438  *         GNUNET_SYSERR to close it (signal serious error)
439  */
440 int
441 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
442                void **channel_ctx,
443                const struct GNUNET_MessageHeader *message)
444 {
445   long client = (long) cls;
446
447   GNUNET_MESH_receive_done (channel);
448
449
450   if (GNUNET_NO == initialized)
451   {
452     initialized = GNUNET_YES;
453     start_time = GNUNET_TIME_absolute_get ();
454     GNUNET_SCHEDULER_add_now (&data_task, NULL);
455   }
456   GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
457                                      GNUNET_TIME_UNIT_FOREVER_REL,
458                                      size_payload, &tmt_rdy, (void *) 1L);
459
460   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
461   {
462     GNUNET_SCHEDULER_cancel (disconnect_task);
463     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
464                                                     &disconnect_mesh_peers,
465                                                     (void *) __LINE__);
466   }
467
468   return GNUNET_OK;
469 }
470
471
472 /**
473  * Handlers, for diverse services
474  */
475 static struct GNUNET_MESH_MessageHandler handlers[] = {
476   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
477   {NULL, 0, 0}
478 };
479
480
481 /**
482  * Method called whenever another peer has added us to a channel
483  * the other peer initiated.
484  *
485  * @param cls Closure.
486  * @param channel New handle to the channel.
487  * @param initiator Peer that started the channel.
488  * @param port Port this channel is connected to.
489  * @param options channel option flags
490  * @return Initial channel context for the channel
491  *         (can be NULL -- that's not an error).
492  */
493 static void *
494 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
495                  const struct GNUNET_PeerIdentity *initiator,
496                  uint32_t port, enum GNUNET_MESH_ChannelOption options)
497 {
498   long n = (long) cls;
499
500   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
501               "Incoming channel from %s to peer %ld\n",
502               GNUNET_i2s (initiator), n);
503   ok++;
504   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
505   peers[n].incoming_ch = channel;
506
507   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
508   {
509     GNUNET_SCHEDULER_cancel (disconnect_task);
510     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
511                                                     &disconnect_mesh_peers,
512                                                     (void *) __LINE__);
513   }
514
515   return NULL;
516 }
517
518 /**
519  * Function called whenever an inbound channel is destroyed.  Should clean up
520  * any associated state.
521  *
522  * @param cls closure (set from GNUNET_MESH_connect)
523  * @param channel connection to the other end (henceforth invalid)
524  * @param channel_ctx place where local state associated
525  *                   with the channel is stored
526  */
527 static void
528 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
529                  void *channel_ctx)
530 {
531   long n = (long) cls;
532
533   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
534               "Incoming channel disconnected at peer %ld\n", n);
535   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
536
537   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
538   {
539     GNUNET_SCHEDULER_cancel (disconnect_task);
540     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
541                                                 (void *) __LINE__);
542   }
543 }
544
545
546 /**
547  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
548  *
549  * Testcase continues when the root receives confirmation of connected peers,
550  * on callback funtion ch.
551  *
552  * @param cls Closure (unsued).
553  * @param tc Task Context.
554  */
555 static void
556 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
557 {
558   enum GNUNET_MESH_ChannelOption flags;
559
560   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
561     return;
562
563   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
564
565   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
566     GNUNET_SCHEDULER_cancel (disconnect_task);
567   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
568                                                   &disconnect_mesh_peers,
569                                                   (void *) __LINE__);
570
571   flags = GNUNET_MESH_OPTION_DEFAULT;
572   peers[0].ch = GNUNET_MESH_channel_create (peers[0].mesh, NULL,
573                                             &peers[TOTAL_PEERS -1].id,
574                                             1, flags);
575
576   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending data initializer...\n");
577   data_ack = 0;
578   data_received = 0;
579   data_sent = 0;
580   GNUNET_MESH_notify_transmit_ready (peers[0].ch, GNUNET_NO,
581                                      GNUNET_TIME_UNIT_FOREVER_REL,
582                                      size_payload, &tmt_rdy, (void *) 1L);
583 }
584
585
586 /**
587  * Callback to be called when the requested peer information is available
588  *
589  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
590  * @param op the operation this callback corresponds to
591  * @param pinfo the result; will be NULL if the operation has failed
592  * @param emsg error message if the operation has failed;
593  *             NULL if the operation is successfull
594  */
595 static void
596 pi_cb (void *cls,
597        struct GNUNET_TESTBED_Operation *op,
598        const struct GNUNET_TESTBED_PeerInformation *pinfo,
599        const char *emsg)
600 {
601   long n = (long) cls;
602
603   if (NULL == pinfo || NULL != emsg)
604   {
605     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
606     abort_test (__LINE__);
607     return;
608   }
609   peers[n].id = pinfo->result.id;
610   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u  id: %s\n",
611               i, GNUNET_i2s (p_id[i]));
612   p_ids++;
613   if (p_ids < TOTAL_PEERS)
614     return;
615   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
616   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
617                                             &do_test, NULL);
618 }
619
620 /**
621  * test main: start test when all peers are connected
622  *
623  * @param cls Closure.
624  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
625  * @param num_peers Number of peers that are running.
626  * @param testbed_peers Array of peers.
627  * @param meshes Handle to each of the MESHs of the peers.
628  */
629 static void
630 tmain (void *cls,
631        struct GNUNET_MESH_TEST_Context *ctx,
632        unsigned int num_peers,
633        struct GNUNET_TESTBED_Peer **testbed_peers,
634        struct GNUNET_MESH_Handle **meshes)
635 {
636   unsigned long i;
637
638   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
639   ok = 0;
640   test_ctx = ctx;
641   GNUNET_assert (TOTAL_PEERS == num_peers);
642   peers_running = num_peers;
643   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
644                                                   &disconnect_mesh_peers,
645                                                   (void *) __LINE__);
646   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
647                                                   &shutdown_task, NULL);
648   for (i = 0; i < TOTAL_PEERS; i++)
649   {
650     peers[i].testbed_peer = testbed_peers[i];
651     peers[i].mesh = meshes[i];
652     peers[i].op =
653       GNUNET_TESTBED_peer_get_information (peers[i].testbed_peer,
654                                            GNUNET_TESTBED_PIT_IDENTITY,
655                                            &pi_cb, (void *) i);
656   }
657   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
658   /* Continues from pi_cb -> do_test */
659 }
660
661
662 /**
663  * Main: start profiler.
664  */
665 int
666 main (int argc, char *argv[])
667 {
668   initialized = GNUNET_NO;
669   static uint32_t ports[2];
670   const char *config_file;
671
672   config_file = "test_mesh.conf";
673
674   p_ids = 0;
675   ports[0] = 1;
676   ports[1] = 0;
677   GNUNET_MESH_TEST_run ("mesh_profiler", config_file, TOTAL_PEERS,
678                         &tmain, NULL, /* tmain cls */
679                         &incoming_channel, &channel_cleaner,
680                         handlers, ports);
681
682   if (ok_goal > ok)
683   {
684     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
685                 "FAILED! (%d/%d)\n", ok, ok_goal);
686     return 1;
687   }
688   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
689   return 0;
690 }
691
692 /* end of test_mesh_small.c */
693