2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file mesh/gnunet-regex-profiler.c
23 * @brief Regex profiler for testing distributed regex use.
31 #include "gnunet_applications.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_mesh_service.h"
34 #include "gnunet_stream_lib.h"
35 #include "gnunet_testbed_service.h"
43 * The testbed operation handle
45 struct GNUNET_TESTBED_Operation *op;
53 * The next pointer for DLL
55 struct DLLOperation *next;
58 * The prev pointer for DLL
60 struct DLLOperation *prev;
65 * Available states during profiling
77 STATE_SLAVES_STARTING,
95 * Destroying peers; we can do this as the controller takes care of stopping a
96 * peer if it is running
98 STATE_PEERS_DESTROYING
103 * An array of hosts loaded from the hostkeys file
105 static struct GNUNET_TESTBED_Host **hosts;
113 * The actual testbed peer handle.
115 struct GNUNET_TESTBED_Peer *peer_handle;
118 * Peer's mesh handle.
120 struct GNUNET_MESH_Handle *mesh_handle;
123 * Host on which the peer is running.
125 struct GNUNET_TESTBED_Host *host_handle;
128 * Testbed operation handle.
130 struct GNUNET_TESTBED_Operation *op_handle;
134 * Array of peer handles used to pass to
135 * GNUNET_TESTBED_overlay_configure_topology
137 struct GNUNET_TESTBED_Peer **peer_handles;
140 * The array of peers; we fill this as the peers are given to us by the testbed
142 static struct Peer *peers;
145 * Host registration handle
147 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
150 * Handle to the master controller process
152 struct GNUNET_TESTBED_ControllerProc *mc_proc;
155 * Handle to the master controller
157 struct GNUNET_TESTBED_Controller *mc;
160 * Handle to global configuration
162 struct GNUNET_CONFIGURATION_Handle *cfg;
165 * Head of the operations list
167 struct DLLOperation *dll_op_head;
170 * Tail of the operations list
172 struct DLLOperation *dll_op_tail;
175 * Peer linking - topology operation
177 struct GNUNET_TESTBED_Operation *topology_op;
180 * Abort task identifier
182 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
185 * Host registration task identifier
187 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
190 * Global event mask for all testbed events
195 * The starting time of a profiling step
197 struct GNUNET_TIME_Absolute prof_start_time;
200 * Duration profiling step has taken
202 struct GNUNET_TIME_Relative prof_time;
207 unsigned int peer_id;
210 * Number of peers to be started by the profiler
212 static unsigned int num_peers;
215 * Number of hosts in the hosts array
217 static unsigned int num_hosts;
220 * Number of random links to be established between peers
222 static unsigned int num_links;
225 * Global testing status
230 * current state of profiling
239 * @param tc the task context
242 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
244 struct DLLOperation *dll_op;
247 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
248 GNUNET_SCHEDULER_cancel (abort_task);
249 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
250 GNUNET_SCHEDULER_cancel (register_hosts_task);
251 if (NULL != reg_handle)
252 GNUNET_TESTBED_cancel_registration (reg_handle);
253 if (NULL != topology_op)
254 GNUNET_TESTBED_operation_cancel (topology_op);
255 for (nhost = 0; nhost < num_hosts; nhost++)
256 if (NULL != hosts[nhost])
257 GNUNET_TESTBED_host_destroy (hosts[nhost]);
258 GNUNET_free_non_null (hosts);
259 while (NULL != (dll_op = dll_op_head))
261 GNUNET_TESTBED_operation_cancel (dll_op->op);
262 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
263 GNUNET_free (dll_op);
266 GNUNET_TESTBED_controller_disconnect (mc);
268 GNUNET_TESTBED_controller_stop (mc_proc);
270 GNUNET_CONFIGURATION_destroy (cfg);
272 //FIXME GNUNET_MESH_disconnect (peers[i].mesh_handle);
274 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
279 * abort task to run on test timed out
282 * @param tc the task context
285 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
287 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
288 abort_task = GNUNET_SCHEDULER_NO_TASK;
289 result = GNUNET_SYSERR;
290 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
295 * Method called whenever another peer has added us to a tunnel
296 * the other peer initiated.
297 * Only called (once) upon reception of data with a message type which was
298 * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
299 * causes te tunnel to be ignored and no further notifications are sent about
303 * @param tunnel new handle to the tunnel
304 * @param initiator peer that started the tunnel
305 * @param atsi performance information for the tunnel
306 * @return initial tunnel context for the tunnel
307 * (can be NULL -- that's not an error)
310 mesh_inbound_tunnel_handler (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
311 const struct GNUNET_PeerIdentity *initiator,
312 const struct GNUNET_ATS_Information *atsi)
319 * Function called whenever an inbound tunnel is destroyed. Should clean up
320 * any associated state. This function is NOT called if the client has
321 * explicitly asked for the tunnel to be destroyed using
322 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
325 * @param cls closure (set from GNUNET_MESH_connect)
326 * @param tunnel connection to the other end (henceforth invalid)
327 * @param tunnel_ctx place where local state associated
328 * with the tunnel is stored
331 mesh_tunnel_end_handler (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
339 * Mesh connect callback.
341 * @param cls internal peer id.
342 * @param op operation handle.
343 * @param ca_result connect adapter result.
344 * @param emsg error message.
347 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
348 void *ca_result, const char *emsg)
350 static unsigned int connected_mesh_handles;
351 struct Peer *peer = (struct Peer *) cls;
352 unsigned int peer_cnt;
356 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Mesh connect failed: %s\n", emsg);
360 GNUNET_assert (peer->op_handle == op);
361 GNUNET_assert (peer->mesh_handle == ca_result);
366 if (++connected_mesh_handles == num_peers)
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All mesh handles connected\n");
370 // TODO announce regexes...
372 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
374 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
382 * Mesh connect adapter.
384 * @param cls not used.
385 * @param cfg configuration handle.
390 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
392 struct Peer *peer = (struct Peer *) cls;
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh connect adapter\n");
396 static struct GNUNET_MESH_MessageHandler handlers[] = {
400 static GNUNET_MESH_ApplicationType apptypes[] = {
401 GNUNET_APPLICATION_TYPE_END
405 GNUNET_MESH_connect (cfg, cls, &mesh_inbound_tunnel_handler,
406 &mesh_tunnel_end_handler, handlers, apptypes);
408 return peer->mesh_handle;
413 * Adapter function called to destroy a connection to
417 * @param op_result service handle returned from the connect adapter
420 mesh_da (void *cls, void *op_result)
422 static unsigned int disconnected_mesh_handles;
423 struct Peer *peer = (struct Peer *) cls;
425 GNUNET_assert (peer->mesh_handle == op_result);
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh disconnect adapter\n");
429 GNUNET_MESH_disconnect (peer->mesh_handle);
430 peer->mesh_handle = NULL;
432 if (++disconnected_mesh_handles == num_peers)
434 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
440 * Functions of this signature are called when a peer has been successfully
441 * started or stopped.
443 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
444 * @param emsg NULL on success; otherwise an error description
447 peer_churn_cb (void *cls, const char *emsg)
449 struct DLLOperation *dll_op = cls;
450 struct GNUNET_TESTBED_Operation *op;
451 static unsigned int started_peers;
452 unsigned int peer_cnt;
455 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
456 GNUNET_free (dll_op);
459 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
460 _("An operation has failed while starting peers\n"));
461 GNUNET_TESTBED_operation_done (op);
462 GNUNET_SCHEDULER_cancel (abort_task);
463 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
466 GNUNET_TESTBED_operation_done (op);
467 if (++started_peers == num_peers)
469 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
470 printf ("All peers started successfully in %.2f seconds\n",
471 ((double) prof_time.rel_value) / 1000.00);
475 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
479 peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
480 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
481 peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
483 state = STATE_PEERS_LINKING;
484 /* Do overlay connect */
485 prof_start_time = GNUNET_TIME_absolute_get ();
487 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
488 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
490 if (NULL == topology_op)
492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
493 "Cannot create topology, op handle was NULL\n");
501 * Functions of this signature are called when a peer has been successfully
504 * @param cls the closure from GNUNET_TESTBED_peer_create()
505 * @param peer the handle for the created peer; NULL on any error during
507 * @param emsg NULL if peer is not NULL; else MAY contain the error description
510 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
512 struct DLLOperation *dll_op = cls;
513 struct Peer *peer_ptr;
514 static unsigned int created_peers;
515 unsigned int peer_cnt;
519 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
520 _("Creating a peer failed. Error: %s\n"), emsg);
521 GNUNET_TESTBED_operation_done (dll_op->op);
522 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
523 GNUNET_free (dll_op);
524 GNUNET_SCHEDULER_cancel (abort_task);
525 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
529 peer_ptr = dll_op->cls;
530 GNUNET_assert (NULL == peer_ptr->peer_handle);
531 peer_ptr->peer_handle = peer;
532 GNUNET_TESTBED_operation_done (dll_op->op);
533 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
534 GNUNET_free (dll_op);
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
538 GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
540 if (++created_peers == num_peers)
542 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
543 printf ("All peers created successfully in %.2f seconds\n",
544 ((double) prof_time.rel_value) / 1000.00);
545 /* Now peers are to be started */
546 state = STATE_PEERS_STARTING;
547 prof_start_time = GNUNET_TIME_absolute_get ();
548 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
550 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
551 dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt].peer_handle,
552 &peer_churn_cb, dll_op);
553 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
560 * Controller event callback
563 * @param event the controller event
566 controller_event_cb (void *cls,
567 const struct GNUNET_TESTBED_EventInformation *event)
569 struct DLLOperation *dll_op;
570 struct GNUNET_TESTBED_Operation *op;
574 case STATE_SLAVES_STARTING:
577 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
579 static unsigned int slaves_started;
580 unsigned int peer_cnt;
582 dll_op = event->details.operation_finished.op_cls;
583 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
584 GNUNET_free (dll_op);
585 op = event->details.operation_finished.operation;
586 if (NULL != event->details.operation_finished.emsg)
588 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
589 _("An operation has failed while starting slaves\n"));
590 GNUNET_TESTBED_operation_done (op);
591 GNUNET_SCHEDULER_cancel (abort_task);
592 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
595 GNUNET_TESTBED_operation_done (op);
596 /* Proceed to start peers */
597 if (++slaves_started == num_hosts - 1)
599 printf ("All slaves started successfully\n");
600 state = STATE_PEERS_CREATING;
601 prof_start_time = GNUNET_TIME_absolute_get ();
602 peers = GNUNET_malloc (sizeof (struct Peer) * num_peers);
603 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating peer %i on host %s\n",
607 GNUNET_TESTBED_host_get_hostname (hosts[peer_cnt % num_hosts]));
609 peers[peer_cnt].host_handle = hosts[peer_cnt % num_hosts];
611 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
612 dll_op->cls = &peers[peer_cnt];
613 dll_op->op = GNUNET_TESTBED_peer_create (mc,
614 hosts[peer_cnt % num_hosts],
618 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
627 case STATE_PEERS_STARTING:
630 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
631 /* Control reaches here when peer start fails */
632 case GNUNET_TESTBED_ET_PEER_START:
633 /* we handle peer starts in peer_churn_cb */
639 case STATE_PEERS_LINKING:
642 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
643 /* Control reaches here when a peer linking operation fails */
644 if (NULL != event->details.operation_finished.emsg)
646 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
647 _("An operation has failed while linking\n"));
648 GNUNET_SCHEDULER_cancel (abort_task);
649 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
652 case GNUNET_TESTBED_ET_CONNECT:
654 static unsigned int established_links;
655 unsigned int peer_cnt;
657 if (0 == established_links)
658 printf ("Establishing links\n .");
664 if (++established_links == num_links)
666 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
667 printf ("\n%u links established in %.2f seconds\n",
668 num_links, ((double) prof_time.rel_value) / 1000.00);
670 GNUNET_free (peer_handles);
671 printf ("\nConnecting to mesh service...\n");
672 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
674 peers[peer_cnt].op_handle = GNUNET_TESTBED_service_connect (NULL,
675 peers[peer_cnt].peer_handle,
697 * Task to register all hosts available in the global host list
700 * @param tc the scheduler task context
703 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
707 * Callback which will be called to after a host registration succeeded or failed
709 * @param cls the closure
710 * @param emsg the error message; NULL if host registration is successful
713 host_registration_completion (void *cls, const char *emsg)
718 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
719 _("Host registration failed for a host. Error: %s\n"), emsg);
720 GNUNET_SCHEDULER_cancel (abort_task);
721 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
724 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
729 * Task to register all hosts available in the global host list
732 * @param tc the scheduler task context
735 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
737 struct DLLOperation *dll_op;
738 static unsigned int reg_host;
741 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
742 if (reg_host == num_hosts - 1)
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "All hosts successfully registered\n");
747 state = STATE_SLAVES_STARTING;
748 for (slave = 1; slave < num_hosts; slave++)
750 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
751 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
757 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
761 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
762 host_registration_completion,
768 * Callback to signal successfull startup of the controller process
770 * @param cls the closure from GNUNET_TESTBED_controller_start()
771 * @param cfg the configuration with which the controller has been started;
772 * NULL if status is not GNUNET_OK
773 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
774 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
777 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
779 GNUNET_SCHEDULER_cancel (abort_task);
780 if (GNUNET_OK != status)
783 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
787 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
788 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
789 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
790 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
791 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
792 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
793 &controller_event_cb, NULL);
796 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
797 _("Unable to connect to master controller -- Check config\n"));
798 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
801 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
802 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
808 * Main function that will be run by the scheduler.
811 * @param args remaining command-line arguments
812 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
813 * @param config configuration
816 run (void *cls, char *const *args, const char *cfgfile,
817 const struct GNUNET_CONFIGURATION_Handle *config)
823 fprintf (stderr, _("No hosts-file specified on command line\n"));
831 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
834 fprintf (stderr, _("No hosts loaded. Need at least one host\n"));
837 for (nhost = 0; nhost < num_hosts; nhost++)
839 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
841 fprintf (stderr, _("Host %s cannot start testbed\n"),
842 GNUNET_TESTBED_host_get_hostname (hosts[nhost]));
846 if (num_hosts != nhost)
848 fprintf (stderr, _("Exiting\n"));
849 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
854 fprintf (stderr, _("No configuration file given. Exiting\n"));
855 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
859 cfg = GNUNET_CONFIGURATION_dup (config);
861 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
868 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
869 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
877 * @return 0 on success
880 main (int argc, char *const *argv)
882 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
883 { 'p', "num-peers", "COUNT",
884 gettext_noop ("create COUNT number of peers"),
885 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
886 { 'n', "num-links", "COUNT",
887 gettext_noop ("create COUNT number of random links"),
888 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
889 GNUNET_GETOPT_OPTION_END
893 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
896 result = GNUNET_SYSERR;
898 GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file",
899 _("Profiler for regex/mesh"),
900 options, &run, NULL);
901 if (GNUNET_OK != ret)
903 if (GNUNET_OK != result)