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 * Peer's mesh tunnel handle.
125 struct GNUNET_MESH_Tunnel *mesh_tunnel_handle;
128 * Host on which the peer is running.
130 struct GNUNET_TESTBED_Host *host_handle;
133 * Testbed operation handle.
135 struct GNUNET_TESTBED_Operation *op_handle;
138 * Filename of the peer's policy file.
144 * Array of peer handles used to pass to
145 * GNUNET_TESTBED_overlay_configure_topology
147 struct GNUNET_TESTBED_Peer **peer_handles;
150 * The array of peers; we fill this as the peers are given to us by the testbed
152 static struct Peer *peers;
155 * Host registration handle
157 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
160 * Handle to the master controller process
162 struct GNUNET_TESTBED_ControllerProc *mc_proc;
165 * Handle to the master controller
167 struct GNUNET_TESTBED_Controller *mc;
170 * Handle to global configuration
172 struct GNUNET_CONFIGURATION_Handle *cfg;
175 * Head of the operations list
177 struct DLLOperation *dll_op_head;
180 * Tail of the operations list
182 struct DLLOperation *dll_op_tail;
185 * Peer linking - topology operation
187 struct GNUNET_TESTBED_Operation *topology_op;
190 * Abort task identifier
192 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
195 * Host registration task identifier
197 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
200 * Global event mask for all testbed events
205 * The starting time of a profiling step
207 struct GNUNET_TIME_Absolute prof_start_time;
210 * Duration profiling step has taken
212 struct GNUNET_TIME_Relative prof_time;
217 unsigned int peer_id;
220 * Number of peers to be started by the profiler
222 static unsigned int num_peers;
225 * Number of hosts in the hosts array
227 static unsigned int num_hosts;
230 * Number of random links to be established between peers
232 static unsigned int num_links;
235 * Number of timeout failures to tolerate
237 static unsigned int num_cont_fails;
240 * Number of times we try overlay connect operations
242 static unsigned int retry_links;
245 * Continuous failures during overlay connect operations
247 static unsigned int cont_fails;
250 * Global testing status
255 * current state of profiling
260 * Folder where policy files are stored.
262 static char * policy_dir;
267 static char **search_strings;
270 * Number of search strings.
272 static int num_search_strings;
275 * Number of peers found with search strings.
277 static unsigned int peers_found;
280 * Search task identifier
282 static GNUNET_SCHEDULER_TaskIdentifier search_task;
285 * Search timeout task identifier.
287 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
290 * Search timeout in seconds.
292 static long search_timeout_sec;
295 * Search wait time in minutes.
297 static long search_wait_min;
304 * @param tc the task context
307 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
309 struct DLLOperation *dll_op;
311 unsigned int peer_cnt;
312 unsigned int search_str_cnt;
314 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
316 if (NULL != peers[peer_cnt].op_handle)
317 GNUNET_TESTBED_operation_cancel (peers[peer_cnt].op_handle);
319 for (search_str_cnt = 0; search_str_cnt < num_search_strings; search_str_cnt++)
321 GNUNET_free (search_strings[search_str_cnt]);
323 GNUNET_free (search_strings);
324 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
325 GNUNET_SCHEDULER_cancel (abort_task);
326 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
327 GNUNET_SCHEDULER_cancel (register_hosts_task);
328 if (NULL != reg_handle)
329 GNUNET_TESTBED_cancel_registration (reg_handle);
330 if (NULL != topology_op)
331 GNUNET_TESTBED_operation_cancel (topology_op);
332 for (nhost = 0; nhost < num_hosts; nhost++)
333 if (NULL != hosts[nhost])
334 GNUNET_TESTBED_host_destroy (hosts[nhost]);
335 GNUNET_free_non_null (hosts);
336 while (NULL != (dll_op = dll_op_head))
338 GNUNET_TESTBED_operation_cancel (dll_op->op);
339 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
340 GNUNET_free (dll_op);
343 GNUNET_TESTBED_controller_disconnect (mc);
345 GNUNET_TESTBED_controller_stop (mc_proc);
347 GNUNET_CONFIGURATION_destroy (cfg);
349 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
354 * abort task to run on test timed out
357 * @param tc the task context
360 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
362 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
363 abort_task = GNUNET_SCHEDULER_NO_TASK;
364 result = GNUNET_SYSERR;
365 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
370 * Method called whenever another peer has added us to a tunnel
371 * the other peer initiated.
372 * Only called (once) upon reception of data with a message type which was
373 * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
374 * causes te tunnel to be ignored and no further notifications are sent about
378 * @param tunnel new handle to the tunnel
379 * @param initiator peer that started the tunnel
380 * @param atsi performance information for the tunnel
381 * @return initial tunnel context for the tunnel
382 * (can be NULL -- that's not an error)
385 mesh_inbound_tunnel_handler (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
386 const struct GNUNET_PeerIdentity *initiator,
387 const struct GNUNET_ATS_Information *atsi)
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh inbound tunnel handler.\n");
396 * Function called whenever an inbound tunnel is destroyed. Should clean up
397 * any associated state. This function is NOT called if the client has
398 * explicitly asked for the tunnel to be destroyed using
399 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
402 * @param cls closure (set from GNUNET_MESH_connect)
403 * @param tunnel connection to the other end (henceforth invalid)
404 * @param tunnel_ctx place where local state associated
405 * with the tunnel is stored
408 mesh_tunnel_end_handler (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh tunnel end handler.\n");
416 * Method called whenever a peer has disconnected from the tunnel.
417 * Implementations of this callback must NOT call
418 * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
419 * to run in some other task later. However, calling
420 * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
423 * @param peer_id peer identity the tunnel stopped working with
426 mesh_peer_disconnect_handler (void *cls,
427 const struct GNUNET_PeerIdentity * peer_id)
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh peer disconnect handler.\n");
434 * Method called whenever a peer has connected to the tunnel.
437 * @param peer_id peer identity the tunnel was created to, NULL on timeout
438 * @param atsi performance data for the connection
442 mesh_peer_connect_handler (void *cls,
443 const struct GNUNET_PeerIdentity* peer_id,
444 const struct GNUNET_ATS_Information * atsi)
446 const char * search_str = (const char *)cls;
452 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
453 "String matching timed out for string %s (%i/%i)\n",
454 search_str, peers_found, num_search_strings);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "String %s successfully matched (%i/%i)\n",
460 search_str, peers_found, num_search_strings);
463 if (peers_found == num_search_strings)
466 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
467 printf ("\nAll strings successfully matched in %.2f minutes\n", ((double)prof_time.rel_value / 1000.0 / 60.0));
468 GNUNET_SCHEDULER_cancel (search_timeout_task);
469 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
475 * Connect by string timeout task
478 * @param tc the task context
481 do_connect_by_string_timeout (void *cls,
482 const struct GNUNET_SCHEDULER_TaskContext * tc)
484 long sec = (long)cls;
486 printf ("Searching for all strings did not succeed after %ld seconds\n", sec);
487 printf ("Found %i of %i strings\n", peers_found, num_search_strings);
489 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
494 * Connect by string task that is run to search for a string in the NFA
497 * @param tc the task context
500 do_connect_by_string (void *cls,
501 const struct GNUNET_SCHEDULER_TaskContext * tc)
503 unsigned int search_cnt;
506 for (search_cnt = 0; search_cnt < num_search_strings; search_cnt++)
508 peer = &peers[search_cnt % num_peers];
510 printf ("Searching for string \"%s\" on peer %d\n",
511 search_strings[search_cnt], (search_cnt % num_peers));
513 peer->mesh_tunnel_handle = GNUNET_MESH_tunnel_create (peer->mesh_handle,
515 &mesh_peer_connect_handler,
516 &mesh_peer_disconnect_handler,
517 search_strings[search_cnt]);
518 GNUNET_MESH_peer_request_connect_by_string (peer->mesh_tunnel_handle,
519 search_strings[search_cnt]);
523 prof_start_time = GNUNET_TIME_absolute_get ();
525 search_timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
526 (GNUNET_TIME_UNIT_SECONDS, search_timeout_sec),
527 &do_connect_by_string_timeout, (void *)search_timeout_sec);
532 * Mesh connect callback.
534 * @param cls internal peer id.
535 * @param op operation handle.
536 * @param ca_result connect adapter result.
537 * @param emsg error message.
540 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
541 void *ca_result, const char *emsg)
543 static unsigned int connected_mesh_handles;
544 struct Peer *peer = (struct Peer *) cls;
553 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Mesh connect failed: %s\n", emsg);
557 GNUNET_assert (peer->op_handle == op);
558 GNUNET_assert (peer->mesh_handle == ca_result);
559 GNUNET_assert (NULL != peer->policy_file);
561 printf ("Announcing regexes for peer with file %s\n", peer->policy_file);
564 if (GNUNET_YES != GNUNET_DISK_file_test (peer->policy_file))
566 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
567 "Could not find policy file %s\n", peer->policy_file);
570 if (GNUNET_OK != GNUNET_DISK_file_size (peer->policy_file, &filesize, GNUNET_YES, GNUNET_YES))
574 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n", peer->policy_file);
577 data = GNUNET_malloc (filesize);
578 if (filesize != GNUNET_DISK_fn_read (peer->policy_file, data, filesize))
581 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read policy file %s.\n",
588 while (offset < (filesize - 1))
591 if (((data[offset] == '\n')) && (buf != &data[offset]))
595 GNUNET_assert (NULL != regex);
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
597 GNUNET_MESH_announce_regex (peer->mesh_handle, regex);
598 buf = &data[offset + 1];
600 else if ((data[offset] == '\n') || (data[offset] == '\0'))
601 buf = &data[offset + 1];
605 if (++connected_mesh_handles == num_peers)
607 printf ("\nAll mesh handles connected.\nWaiting %ld minutes before starting to search.\n",
610 search_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
611 (GNUNET_TIME_UNIT_MINUTES, search_wait_min),
612 &do_connect_by_string, NULL);
618 * Mesh connect adapter.
620 * @param cls not used.
621 * @param cfg configuration handle.
626 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
628 GNUNET_MESH_ApplicationType app;
629 struct Peer *peer = (struct Peer *) cls;
631 static struct GNUNET_MESH_MessageHandler handlers[] = {
635 app = (GNUNET_MESH_ApplicationType)0;
638 GNUNET_MESH_connect (cfg, cls, NULL, NULL, handlers, &app);
640 return peer->mesh_handle;
645 * Adapter function called to destroy a connection to
649 * @param op_result service handle returned from the connect adapter
652 mesh_da (void *cls, void *op_result)
654 struct Peer *peer = (struct Peer *) cls;
656 GNUNET_assert (peer->mesh_handle == op_result);
658 if (NULL != peer->mesh_tunnel_handle)
660 GNUNET_MESH_tunnel_destroy (peer->mesh_tunnel_handle);
661 peer->mesh_tunnel_handle = NULL;
663 if (NULL != peer->mesh_handle)
665 GNUNET_MESH_disconnect (peer->mesh_handle);
666 peer->mesh_handle = NULL;
672 * Functions of this signature are called when a peer has been successfully
673 * started or stopped.
675 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
676 * @param emsg NULL on success; otherwise an error description
679 peer_churn_cb (void *cls, const char *emsg)
681 struct DLLOperation *dll_op = cls;
682 struct GNUNET_TESTBED_Operation *op;
683 static unsigned int started_peers;
684 unsigned int peer_cnt;
687 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
688 GNUNET_free (dll_op);
691 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
692 _("An operation has failed while starting peers\n"));
693 GNUNET_TESTBED_operation_done (op);
694 GNUNET_SCHEDULER_cancel (abort_task);
695 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
698 GNUNET_TESTBED_operation_done (op);
699 if (++started_peers == num_peers)
701 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
702 printf ("All peers started successfully in %.2f seconds\n",
703 ((double) prof_time.rel_value) / 1000.00);
707 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
711 peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
712 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
713 peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
715 state = STATE_PEERS_LINKING;
716 /* Do overlay connect */
717 prof_start_time = GNUNET_TIME_absolute_get ();
719 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
720 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
722 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
723 if (NULL == topology_op)
725 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
726 "Cannot create topology, op handle was NULL\n");
734 * Functions of this signature are called when a peer has been successfully
737 * @param cls the closure from GNUNET_TESTBED_peer_create()
738 * @param peer the handle for the created peer; NULL on any error during
740 * @param emsg NULL if peer is not NULL; else MAY contain the error description
743 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
745 struct DLLOperation *dll_op = cls;
746 struct Peer *peer_ptr;
747 static unsigned int created_peers;
748 unsigned int peer_cnt;
752 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
753 _("Creating a peer failed. Error: %s\n"), emsg);
754 GNUNET_TESTBED_operation_done (dll_op->op);
755 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
756 GNUNET_free (dll_op);
757 GNUNET_SCHEDULER_cancel (abort_task);
758 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
762 peer_ptr = dll_op->cls;
763 GNUNET_assert (NULL == peer_ptr->peer_handle);
764 peer_ptr->peer_handle = peer;
765 GNUNET_TESTBED_operation_done (dll_op->op);
766 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
767 GNUNET_free (dll_op);
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
771 GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
773 if (++created_peers == num_peers)
775 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
776 printf ("All peers created successfully in %.2f seconds\n",
777 ((double) prof_time.rel_value) / 1000.00);
778 /* Now peers are to be started */
779 state = STATE_PEERS_STARTING;
780 prof_start_time = GNUNET_TIME_absolute_get ();
781 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
783 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
784 dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt].peer_handle,
785 &peer_churn_cb, dll_op);
786 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
792 * Function called with a filename.
795 * @param filename complete filename (absolute path)
796 * @return GNUNET_OK to continue to iterate,
797 * GNUNET_SYSERR to abort iteration with error!
800 policy_filename_cb (void *cls, const char *filename)
802 static unsigned int peer_cnt;
803 struct DLLOperation *dll_op;
805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating peer %i on host %s for policy file %s\n",
807 GNUNET_TESTBED_host_get_hostname (hosts[peer_cnt % num_hosts]),
810 peers[peer_cnt].policy_file = GNUNET_strdup (filename);
811 peers[peer_cnt].host_handle = hosts[peer_cnt % num_hosts];
813 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
814 dll_op->cls = &peers[peer_cnt];
815 dll_op->op = GNUNET_TESTBED_peer_create (mc,
816 hosts[peer_cnt % num_hosts],
820 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
828 * Controller event callback
831 * @param event the controller event
834 controller_event_cb (void *cls,
835 const struct GNUNET_TESTBED_EventInformation *event)
837 struct DLLOperation *dll_op;
838 struct GNUNET_TESTBED_Operation *op;
842 case STATE_SLAVES_STARTING:
845 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
847 static unsigned int slaves_started;
849 dll_op = event->details.operation_finished.op_cls;
850 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
851 GNUNET_free (dll_op);
852 op = event->details.operation_finished.operation;
853 if (NULL != event->details.operation_finished.emsg)
855 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
856 _("An operation has failed while starting slaves\n"));
857 GNUNET_TESTBED_operation_done (op);
858 GNUNET_SCHEDULER_cancel (abort_task);
859 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
862 GNUNET_TESTBED_operation_done (op);
863 /* Proceed to start peers */
864 if (++slaves_started == num_hosts - 1)
866 printf ("All slaves started successfully\n");
868 state = STATE_PEERS_CREATING;
869 prof_start_time = GNUNET_TIME_absolute_get ();
871 num_peers = GNUNET_DISK_directory_scan (policy_dir,
874 peers = GNUNET_malloc (sizeof (struct Peer) * num_peers);
876 GNUNET_DISK_directory_scan (policy_dir,
886 case STATE_PEERS_STARTING:
889 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
890 /* Control reaches here when peer start fails */
891 case GNUNET_TESTBED_ET_PEER_START:
892 /* we handle peer starts in peer_churn_cb */
898 case STATE_PEERS_LINKING:
901 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
902 /* Control reaches here when a peer linking operation fails */
903 if (NULL != event->details.operation_finished.emsg)
905 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
906 _("An operation has failed while linking\n"));
908 if (++cont_fails > num_cont_fails)
910 printf ("\nAborting due to very high failure rate");
911 GNUNET_SCHEDULER_cancel (abort_task);
912 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
916 case GNUNET_TESTBED_ET_CONNECT:
918 static unsigned int established_links;
919 unsigned int peer_cnt;
921 if (0 == established_links)
922 printf ("Establishing links\n .");
928 if (++established_links == num_links)
930 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
931 printf ("\n%u links established in %.2f seconds\n",
932 num_links, ((double) prof_time.rel_value) / 1000.00);
934 GNUNET_free (peer_handles);
935 printf ("\nConnecting to mesh service...\n");
936 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
938 peers[peer_cnt].op_handle = GNUNET_TESTBED_service_connect (NULL,
939 peers[peer_cnt].peer_handle,
961 * Task to register all hosts available in the global host list
964 * @param tc the scheduler task context
967 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
971 * Callback which will be called to after a host registration succeeded or failed
973 * @param cls the closure
974 * @param emsg the error message; NULL if host registration is successful
977 host_registration_completion (void *cls, const char *emsg)
982 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
983 _("Host registration failed for a host. Error: %s\n"), emsg);
984 GNUNET_SCHEDULER_cancel (abort_task);
985 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
988 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
993 * Task to register all hosts available in the global host list
996 * @param tc the scheduler task context
999 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1001 struct DLLOperation *dll_op;
1002 static unsigned int reg_host;
1005 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1006 if (reg_host == num_hosts - 1)
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1009 "All hosts successfully registered\n");
1011 state = STATE_SLAVES_STARTING;
1012 for (slave = 1; slave < num_hosts; slave++)
1014 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1015 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
1021 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1025 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
1026 host_registration_completion,
1032 * Callback to signal successfull startup of the controller process
1034 * @param cls the closure from GNUNET_TESTBED_controller_start()
1035 * @param config the configuration with which the controller has been started;
1036 * NULL if status is not GNUNET_OK
1037 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1038 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1041 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
1043 GNUNET_SCHEDULER_cancel (abort_task);
1044 if (GNUNET_OK != status)
1047 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1051 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1052 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1053 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1054 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1055 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1056 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
1057 &controller_event_cb, NULL);
1060 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1061 _("Unable to connect to master controller -- Check config\n"));
1062 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1065 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1066 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1071 * Load search strings from given filename. One search string per line.
1073 * @param filename filename of the file containing the search strings.
1074 * @param strings set of strings loaded from file. Caller needs to free this
1075 * if number returned is greater than zero.
1076 * @return number of strings found in the file. GNUNET_SYSERR on error.
1079 load_search_strings (const char *filename, char ***strings)
1084 unsigned int offset;
1088 if (NULL == filename)
1090 return GNUNET_SYSERR;
1093 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1095 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1096 "Could not find search strings file %s\n", filename);
1097 return GNUNET_SYSERR;
1099 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1104 return GNUNET_SYSERR;
1106 data = GNUNET_malloc (filesize);
1107 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1110 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1112 return GNUNET_SYSERR;
1117 while (offset < (filesize - 1))
1120 if (((data[offset] == '\n')) && (buf != &data[offset]))
1122 data[offset] = '\0';
1124 buf = &data[offset + 1];
1126 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1127 buf = &data[offset + 1];
1129 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1131 for (i = 0; i < str_cnt; i++)
1133 (*strings)[i] = GNUNET_strdup (&data[offset]);
1134 offset += strlen ((*strings)[i]) + 1;
1141 * Main function that will be run by the scheduler.
1143 * @param cls closure
1144 * @param args remaining command-line arguments
1145 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1146 * @param config configuration
1149 run (void *cls, char *const *args, const char *cfgfile,
1150 const struct GNUNET_CONFIGURATION_Handle *config)
1154 if (NULL == args[0])
1156 fprintf (stderr, _("No hosts-file specified on command line. Exiting.\n"));
1159 if (NULL == args[1])
1161 fprintf (stderr, _("No policy directory specified on command line. Exiting.\n"));
1164 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
1167 fprintf (stderr, _("No hosts loaded. Need at least one host\n"));
1170 for (nhost = 0; nhost < num_hosts; nhost++)
1172 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
1174 fprintf (stderr, _("Host %s cannot start testbed\n"),
1175 GNUNET_TESTBED_host_get_hostname (hosts[nhost]));
1179 if (num_hosts != nhost)
1181 fprintf (stderr, _("Exiting\n"));
1182 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1187 fprintf (stderr, _("No configuration file given. Exiting\n"));
1188 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1191 if (GNUNET_YES != GNUNET_DISK_directory_test (args[1]))
1193 fprintf (stderr, _("Specified policies directory does not exist. Exiting.\n"));
1194 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1197 policy_dir = args[1];
1198 if (GNUNET_YES != GNUNET_DISK_file_test (args[2]))
1200 fprintf (stderr, _("No search strings file given. Exiting.\n"));
1201 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1204 num_search_strings = load_search_strings (args[2], &search_strings);
1205 if (0 >= num_search_strings || NULL == search_strings)
1207 fprintf (stderr, _("Error loading search strings. Exiting.\n"));
1208 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1212 for (i = 0; i < num_search_strings; i++)
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "search string: %s\n", search_strings[i]);
1214 cfg = GNUNET_CONFIGURATION_dup (config);
1216 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
1223 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1224 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
1232 * @param argc argument count
1233 * @param argv argument values
1234 * @return 0 on success
1237 main (int argc, char *const *argv)
1239 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1240 { 'n', "num-links", "COUNT",
1241 gettext_noop ("create COUNT number of random links"),
1242 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
1243 { 'e', "num-errors", "COUNT",
1244 gettext_noop ("tolerate COUNT number of continious timeout failures"),
1245 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails },
1246 { 't', "matching-timeout", "TIMEOUT",
1247 gettext_noop ("wait TIMEOUT seconds before considering a string match as failed"),
1248 GNUNET_YES, &GNUNET_GETOPT_set_uint, &search_timeout_sec },
1249 { 's', "search-wait", "WAIT",
1250 gettext_noop ("wait WAIT minutes before starting string search"),
1251 GNUNET_YES, &GNUNET_GETOPT_set_uint, &search_wait_min },
1252 GNUNET_GETOPT_OPTION_END
1256 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1259 result = GNUNET_SYSERR;
1261 GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
1262 _("Profiler for regex/mesh"),
1263 options, &run, NULL);
1264 if (GNUNET_OK != ret)
1266 if (GNUNET_OK != result)