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.
32 #include "gnunet_applications.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_mesh_service.h"
35 #include "gnunet_stream_lib.h"
36 #include "gnunet_testbed_service.h"
44 * The testbed operation handle
46 struct GNUNET_TESTBED_Operation *op;
54 * The next pointer for DLL
56 struct DLLOperation *next;
59 * The prev pointer for DLL
61 struct DLLOperation *prev;
66 * Available states during profiling
78 STATE_SLAVES_STARTING,
96 * Matching strings against announced regexes
101 * Destroying peers; we can do this as the controller takes care of stopping a
102 * peer if it is running
104 STATE_PEERS_DESTROYING
119 * Peer configuration handle.
121 struct GNUNET_CONFIGURATION_Handle *cfg;
124 * The actual testbed peer handle.
126 struct GNUNET_TESTBED_Peer *peer_handle;
129 * Host on which the peer is running.
131 struct GNUNET_TESTBED_Host *host_handle;
134 * Filename of the peer's policy file.
139 * Peers search string.
141 const char *search_str;
144 * Set to GNUNET_YES if the peer successfully matched the above
145 * search string. GNUNET_NO if the string could not be matched
146 * during the profiler run. GNUNET_SYSERR if the string matching
147 * timed out. Undefined if search_str is NULL
149 int search_str_matched;
152 * Peer's mesh handle.
154 struct GNUNET_MESH_Handle *mesh_handle;
157 * Peer's mesh tunnel handle.
159 struct GNUNET_MESH_Tunnel *mesh_tunnel_handle;
162 * Testbed operation handle for the mesh service.
164 struct GNUNET_TESTBED_Operation *mesh_op_handle;
167 * Peers's statistics handle.
169 struct GNUNET_STATISTICS_Handle *stats_handle;
172 * Testbed operation handle for the statistics service.
174 struct GNUNET_TESTBED_Operation *stats_op_handle;
177 * The starting time of a profiling step.
179 struct GNUNET_TIME_Absolute prof_start_time;
184 * An array of hosts loaded from the hostkeys file
186 static struct GNUNET_TESTBED_Host **hosts;
189 * Array of peer handles used to pass to
190 * GNUNET_TESTBED_overlay_configure_topology
192 static struct GNUNET_TESTBED_Peer **peer_handles;
195 * The array of peers; we fill this as the peers are given to us by the testbed
197 static struct RegexPeer *peers;
200 * Host registration handle
202 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
205 * Handle to the master controller process
207 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
210 * Handle to the master controller
212 static struct GNUNET_TESTBED_Controller *mc;
215 * Handle to global configuration
217 static struct GNUNET_CONFIGURATION_Handle *cfg;
220 * Head of the operations list
222 static struct DLLOperation *dll_op_head;
225 * Tail of the operations list
227 static struct DLLOperation *dll_op_tail;
230 * Peer linking - topology operation
232 static struct GNUNET_TESTBED_Operation *topology_op;
235 * Abort task identifier
237 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
240 * Host registration task identifier
242 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
245 * Global event mask for all testbed events
247 static uint64_t event_mask;
250 * The starting time of a profiling step
252 static struct GNUNET_TIME_Absolute prof_start_time;
255 * Duration profiling step has taken
257 static struct GNUNET_TIME_Relative prof_time;
260 * Number of peers to be started by the profiler
262 static unsigned int num_peers;
265 * Number of hosts in the hosts array
267 static unsigned int num_hosts;
270 * Factor of number of links. num_links = num_peers * linking_factor.
272 static unsigned int linking_factor;
275 * Number of random links to be established between peers
277 static unsigned int num_links;
280 * Number of times we try overlay connect operations
282 static unsigned int retry_links;
285 * Continuous failures during overlay connect operations
287 static unsigned int cont_fails;
290 * Global testing status
295 * current state of profiling
300 * Folder where policy files are stored.
302 static char * policy_dir;
307 static char **search_strings;
310 * Number of search strings.
312 static int num_search_strings;
315 * Number of peers found with search strings.
317 static unsigned int peers_found;
320 * Search task identifier
322 static GNUNET_SCHEDULER_TaskIdentifier search_task;
325 * Search timeout task identifier.
327 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
330 * Search timeout in seconds.
332 static struct GNUNET_TIME_Relative search_timeout = { 60000 };
335 * How long do we wait before starting the search?
338 static struct GNUNET_TIME_Relative search_delay = { 60000 };
341 * Delay to wait before starting to configure the overlay topology
343 static struct GNUNET_TIME_Relative conf_topo_delay = { 10000 };
346 * File to log statistics to.
348 static struct GNUNET_DISK_FileHandle *data_file;
351 * Filename to log statistics to.
353 static char *data_filename;
356 * Maximal path compression length.
358 static unsigned int max_path_compression;
360 /******************************************************************************/
361 /****************************** DECLARATIONS ********************************/
362 /******************************************************************************/
366 * Method called whenever a peer has connected to the tunnel.
369 * @param peer_id peer identity the tunnel was created to, NULL on timeout
370 * @param atsi performance data for the connection
374 mesh_peer_connect_handler (void *cls,
375 const struct GNUNET_PeerIdentity* peer_id,
376 const struct GNUNET_ATS_Information * atsi);
380 * Method called whenever a peer has disconnected from the tunnel.
381 * Implementations of this callback must NOT call
382 * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
383 * to run in some other task later. However, calling
384 * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
387 * @param peer_id peer identity the tunnel stopped working with
390 mesh_peer_disconnect_handler (void *cls,
391 const struct GNUNET_PeerIdentity * peer_id);
394 * Mesh connect callback.
396 * @param cls internal peer id.
397 * @param op operation handle.
398 * @param ca_result connect adapter result.
399 * @param emsg error message.
402 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
403 void *ca_result, const char *emsg);
406 * Mesh connect adapter.
408 * @param cls not used.
409 * @param cfg configuration handle.
414 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
418 * Adapter function called to destroy a connection to
422 * @param op_result service handle returned from the connect adapter
425 mesh_da (void *cls, void *op_result);
428 /******************************************************************************/
429 /******************************** SHUTDOWN **********************************/
430 /******************************************************************************/
437 * @param tc the task context
440 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
442 struct DLLOperation *dll_op;
443 struct RegexPeer *peer;
445 unsigned int peer_cnt;
446 unsigned int search_str_cnt;
447 char output_buffer[512];
450 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
451 GNUNET_SCHEDULER_cancel (abort_task);
452 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
453 GNUNET_SCHEDULER_cancel (register_hosts_task);
455 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
457 peer = &peers[peer_cnt];
459 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
461 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
463 GNUNET_snprintf (output_buffer,
464 sizeof (output_buffer),
465 "Search string not found: %s (%d)\nOn peer: %u (%p)\nWith policy file: %s\nAfter: %s\n",
467 peer->search_str_matched,
471 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
472 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
473 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
476 if (NULL != peers[peer_cnt].mesh_op_handle)
477 GNUNET_TESTBED_operation_done (peers[peer_cnt].mesh_op_handle);
478 if (NULL != peers[peer_cnt].stats_op_handle)
479 GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
482 if (NULL != data_file)
483 GNUNET_DISK_file_close (data_file);
485 for (search_str_cnt = 0;
486 search_str_cnt < num_search_strings && NULL != search_strings;
489 GNUNET_free_non_null (search_strings[search_str_cnt]);
491 GNUNET_free_non_null (search_strings);
493 if (NULL != reg_handle)
494 GNUNET_TESTBED_cancel_registration (reg_handle);
495 if (NULL != topology_op)
496 GNUNET_TESTBED_operation_done (topology_op);
497 for (nhost = 0; nhost < num_hosts; nhost++)
498 if (NULL != hosts[nhost])
499 GNUNET_TESTBED_host_destroy (hosts[nhost]);
500 GNUNET_free_non_null (hosts);
502 while (NULL != (dll_op = dll_op_head))
504 GNUNET_TESTBED_operation_done (dll_op->op);
505 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
506 GNUNET_free (dll_op);
509 GNUNET_TESTBED_controller_disconnect (mc);
511 GNUNET_TESTBED_controller_stop (mc_proc);
513 GNUNET_CONFIGURATION_destroy (cfg);
515 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
520 * abort task to run on test timed out
523 * @param tc the task context
526 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
529 abort_task = GNUNET_SCHEDULER_NO_TASK;
530 result = GNUNET_SYSERR;
531 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
535 /******************************************************************************/
536 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
537 /******************************************************************************/
540 * Adapter function called to establish a connection to
541 * statistics service.
544 * @param cfg configuration of the peer to connect to; will be available until
545 * GNUNET_TESTBED_operation_done() is called on the operation returned
546 * from GNUNET_TESTBED_service_connect()
547 * @return service handle to return in 'op_result', NULL on error
550 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
552 return GNUNET_STATISTICS_create ("<driver>", cfg);
557 * Adapter function called to destroy a connection to
558 * statistics service.
561 * @param op_result service handle returned from the connect adapter
564 stats_da (void *cls, void *op_result)
566 struct RegexPeer *peer = cls;
568 GNUNET_assert (op_result == peer->stats_handle);
570 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
571 peer->stats_handle = NULL;
576 * Process statistic values. Write all values to global 'data_file', if present.
579 * @param subsystem name of subsystem that created the statistic
580 * @param name the name of the datum
581 * @param value the current value
582 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
583 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
586 stats_iterator (void *cls, const char *subsystem, const char *name,
587 uint64_t value, int is_persistent)
589 struct RegexPeer *peer = cls;
590 char output_buffer[512];
593 if (NULL == data_file)
595 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
596 "%p -> %s [%s]: %llu\n",
597 peer, subsystem, name, value);
601 GNUNET_snprintf (output_buffer,
602 sizeof (output_buffer),
605 subsystem, value, name);
606 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
607 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
614 * Stats callback. Finish the stats testbed operation and when all stats have
615 * been iterated, shutdown the profiler.
618 * @param success GNUNET_OK if statistics were
619 * successfully obtained, GNUNET_SYSERR if not.
625 static unsigned int peer_cnt;
626 struct RegexPeer *peer = cls;
628 if (GNUNET_OK != success)
630 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
631 "Getting statistics for peer %u failed!\n",
636 GNUNET_TESTBED_operation_done (peer->stats_op_handle);
637 peer->stats_op_handle = NULL;
639 if (++peer_cnt == num_search_strings)
641 struct GNUNET_TIME_Relative delay = { 100 };
642 GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
648 * Function called by testbed once we are connected to stats service. Create a
649 * mesh tunnel and try to match the peer's string.
651 * @param cls the 'struct RegexPeer' for which we connected to stats
652 * @param op connect operation handle
653 * @param ca_result handle to stats service
654 * @param emsg error message on failure
657 stats_connect_cb (void *cls,
658 struct GNUNET_TESTBED_Operation *op,
662 struct RegexPeer *peer = cls;
664 if (NULL == ca_result || NULL != emsg)
666 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
667 "Failed to connect to statistics service on peer %u: %s\n",
670 peer->stats_handle = NULL;
674 GNUNET_assert (NULL != peer->mesh_handle);
676 peer->stats_handle = ca_result;
678 peer->mesh_tunnel_handle = GNUNET_MESH_tunnel_create (peer->mesh_handle,
680 &mesh_peer_connect_handler,
681 &mesh_peer_disconnect_handler,
684 peer->prof_start_time = GNUNET_TIME_absolute_get ();
686 peer->search_str_matched = GNUNET_NO;
687 GNUNET_MESH_peer_request_connect_by_string (peer->mesh_tunnel_handle,
692 /******************************************************************************/
693 /************************ MESH SERVICE CONNECTIONS **************************/
694 /******************************************************************************/
697 * Method called whenever a peer has disconnected from the tunnel.
698 * Implementations of this callback must NOT call
699 * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
700 * to run in some other task later. However, calling
701 * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
704 * @param peer_id peer identity the tunnel stopped working with
707 mesh_peer_disconnect_handler (void *cls,
708 const struct GNUNET_PeerIdentity * peer_id)
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh peer disconnect handler.\n");
715 * Method called when the mesh connection succeeded (or timed out), which means
716 * we've found a peer that announced a regex that matches our search string. Now
717 * get the statistics.
720 * @param peer_id peer identity the tunnel was created to, NULL on timeout
721 * @param atsi performance data for the connection
725 mesh_peer_connect_handler (void *cls,
726 const struct GNUNET_PeerIdentity* peer_id,
727 const struct GNUNET_ATS_Information * atsi)
729 struct RegexPeer *peer = cls;
730 char output_buffer[512];
737 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
738 "String matching timed out for string %s on peer %u (%i/%i)\n",
739 peer->search_str, peer->id, peers_found, num_search_strings);
741 printf ("String matching timed out for string %s on peer %u (%i/%i)\n",
742 peer->search_str, peer->id, peers_found, num_search_strings);
744 peer->search_str_matched = GNUNET_SYSERR;
748 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
749 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
750 "String %s successfully matched on peer %u after %s (%i/%i)\n",
751 peer->search_str, peer->id, GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
752 peers_found, num_search_strings);
754 printf ("String %s successfully matched on peer %u after %s (%i/%i)\n",
755 peer->search_str, peer->id, GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
756 peers_found, num_search_strings);
759 peer->search_str_matched = GNUNET_YES;
761 if (NULL != data_file)
764 GNUNET_snprintf (output_buffer,
765 sizeof (output_buffer),
766 "Peer: %u (%p)\nHost: %s\nPolicy file: %s\nSearch string: %s\nSearch duration: %s\n\n",
769 GNUNET_TESTBED_host_get_hostname (peer->host_handle),
772 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
774 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
775 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
778 if (NULL == peer->stats_handle)
780 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
781 "Cannot get statistics for peer %u, stats handle is NULL!\n");
785 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, "mesh", NULL,
786 GNUNET_TIME_UNIT_FOREVER_REL,
788 &stats_iterator, peer))
790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
791 "Could not get mesh statistics of peer %u!\n", peer->id);
793 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, "regexprofiler", NULL,
794 GNUNET_TIME_UNIT_FOREVER_REL,
796 &stats_iterator, peer))
798 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
799 "Could not get regexprofiler statistics of peer %u!\n", peer->id);
801 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, "transport", NULL,
802 GNUNET_TIME_UNIT_FOREVER_REL,
804 &stats_iterator, peer))
806 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
807 "Could not get transport statistics of peer %u!\n", peer->id);
809 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, "dht", NULL,
810 GNUNET_TIME_UNIT_FOREVER_REL,
812 &stats_iterator, peer))
814 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
815 "Could not get dht statistics of peer %u!\n", peer->id);
819 if (peers_found == num_search_strings)
821 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
822 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
823 "All strings successfully matched in %s\n",
824 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
825 printf ("All strings successfully matched. Shutting down.\n");
828 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
829 GNUNET_SCHEDULER_cancel (search_timeout_task);
835 * Connect by string timeout task. This will cancel the profiler after the
836 * specified timeout 'search_timeout'.
839 * @param tc the task context
842 do_connect_by_string_timeout (void *cls,
843 const struct GNUNET_SCHEDULER_TaskContext * tc)
845 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
846 "Finding matches to all strings did not succeed after %s.\n",
847 GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
848 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
849 "Found %i of %i strings\n", peers_found, num_search_strings);
851 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
856 * Connect by string task that is run to search for a string in the NFA. It
857 * first connects to the mesh service, then connects to the stats service of
858 * this peer and then it starts the string search.
861 * @param tc the task context
864 do_connect_by_string (void *cls,
865 const struct GNUNET_SCHEDULER_TaskContext * tc)
867 unsigned int search_cnt;
868 struct RegexPeer *peer;
870 printf ("Starting string search.\n");
873 for (search_cnt = 0; search_cnt < num_search_strings; search_cnt++)
875 peer = &peers[search_cnt % num_peers];
876 peer->search_str = search_strings[search_cnt];
877 peer->search_str_matched = GNUNET_NO;
879 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
880 "Searching for string \"%s\" on peer %d with file %s\n",
881 peer->search_str, (search_cnt % num_peers), peer->policy_file);
883 /* First connect to mesh service, then connect to stats service
884 and then try connecting by string in stats_connect_cb */
885 peer->mesh_op_handle =
886 GNUNET_TESTBED_service_connect (NULL,
896 search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout,
897 &do_connect_by_string_timeout, NULL);
902 * Mesh connect callback. Called when we are connected to the mesh service for
903 * the peer in 'cls'. If successfull we connect to the stats service of this
904 * peer and then try to match the search string of this peer.
906 * @param cls internal peer id.
907 * @param op operation handle.
908 * @param ca_result connect adapter result.
909 * @param emsg error message.
912 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
913 void *ca_result, const char *emsg)
915 struct RegexPeer *peer = (struct RegexPeer *) cls;
917 if (NULL != emsg || NULL == op || NULL == ca_result)
919 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Mesh connect failed: %s\n", emsg);
923 GNUNET_assert (peer->mesh_handle != NULL);
924 GNUNET_assert (peer->mesh_op_handle == op);
925 GNUNET_assert (peer->mesh_handle == ca_result);
927 /* First connect to the stats service, then start to search */
928 peer->stats_op_handle =
929 GNUNET_TESTBED_service_connect (NULL,
941 * Mesh connect adapter. Opens a connection to the mesh service.
943 * @param cls not used.
944 * @param cfg configuration handle.
949 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
951 GNUNET_MESH_ApplicationType app;
952 struct RegexPeer *peer = cls;
954 static struct GNUNET_MESH_MessageHandler handlers[] = {
958 app = (GNUNET_MESH_ApplicationType)0;
961 GNUNET_MESH_connect (cfg, peer, NULL, NULL, handlers, &app);
963 return peer->mesh_handle;
968 * Adapter function called to destroy a connection to
972 * @param op_result service handle returned from the connect adapter
975 mesh_da (void *cls, void *op_result)
977 struct RegexPeer *peer = (struct RegexPeer *) cls;
979 GNUNET_assert (peer->mesh_handle == op_result);
981 if (NULL != peer->mesh_tunnel_handle)
983 GNUNET_MESH_tunnel_destroy (peer->mesh_tunnel_handle);
984 peer->mesh_tunnel_handle = NULL;
987 if (NULL != peer->mesh_handle)
989 GNUNET_MESH_disconnect (peer->mesh_handle);
990 peer->mesh_handle = NULL;
995 /******************************************************************************/
996 /*************************** TESTBED PEER SETUP *****************************/
997 /******************************************************************************/
1001 * Configure the peer overlay topology.
1004 * @param tc the task context
1007 do_configure_topology (void *cls,
1008 const struct GNUNET_SCHEDULER_TaskContext * tc)
1011 if (0 == linking_factor)
1013 num_links = linking_factor * num_peers;
1015 /* num_links = num_peers - 1; */
1016 num_links = linking_factor;
1018 /* Do overlay connect */
1019 prof_start_time = GNUNET_TIME_absolute_get ();
1021 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
1022 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
1024 GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
1025 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
1026 if (NULL == topology_op)
1028 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1029 "Cannot create topology, op handle was NULL\n");
1036 * Functions of this signature are called when a peer has been successfully
1037 * started or stopped.
1039 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
1040 * @param emsg NULL on success; otherwise an error description
1043 peer_churn_cb (void *cls, const char *emsg)
1045 struct DLLOperation *dll_op = cls;
1046 struct GNUNET_TESTBED_Operation *op;
1047 static unsigned int started_peers;
1048 unsigned int peer_cnt;
1051 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1052 GNUNET_free (dll_op);
1055 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1056 _("An operation has failed while starting peers\n"));
1057 GNUNET_TESTBED_operation_done (op);
1058 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1059 GNUNET_SCHEDULER_cancel (abort_task);
1060 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1063 GNUNET_TESTBED_operation_done (op);
1064 if (++started_peers == num_peers)
1066 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1067 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1068 "All peers started successfully in %s\n",
1069 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1072 peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
1073 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1074 peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
1076 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1077 "Waiting %s before starting to link peers\n",
1078 GNUNET_STRINGS_relative_time_to_string (conf_topo_delay, GNUNET_YES));
1080 printf ("Waiting %s before starting to link peers\n",
1081 GNUNET_STRINGS_relative_time_to_string (conf_topo_delay, GNUNET_YES));
1084 state = STATE_PEERS_LINKING;
1085 GNUNET_SCHEDULER_add_delayed (conf_topo_delay, &do_configure_topology, NULL);
1091 * Functions of this signature are called when a peer has been successfully
1094 * @param cls the closure from GNUNET_TESTBED_peer_create()
1095 * @param peer the handle for the created peer; NULL on any error during
1097 * @param emsg NULL if peer is not NULL; else MAY contain the error description
1100 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
1102 struct DLLOperation *dll_op = cls;
1103 struct RegexPeer *peer_ptr;
1104 static unsigned int created_peers;
1105 unsigned int peer_cnt;
1109 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1110 _("Creating a peer failed. Error: %s\n"), emsg);
1111 GNUNET_TESTBED_operation_done (dll_op->op);
1112 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1113 GNUNET_free (dll_op);
1114 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1115 GNUNET_SCHEDULER_cancel (abort_task);
1116 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1120 peer_ptr = dll_op->cls;
1121 GNUNET_assert (NULL == peer_ptr->peer_handle);
1122 GNUNET_CONFIGURATION_destroy (peer_ptr->cfg);
1123 peer_ptr->cfg = NULL;
1124 peer_ptr->peer_handle = peer;
1125 GNUNET_TESTBED_operation_done (dll_op->op);
1126 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1127 GNUNET_free (dll_op);
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
1131 GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
1133 if (++created_peers == num_peers)
1135 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1136 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1137 "All peers created successfully in %s\n",
1138 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1139 /* Now peers are to be started */
1140 state = STATE_PEERS_STARTING;
1141 prof_start_time = GNUNET_TIME_absolute_get ();
1142 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1144 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1145 dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt].peer_handle,
1146 &peer_churn_cb, dll_op);
1147 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1154 * Function called with a filename for each file in the policy directory. Create
1155 * a peer for each filename and update the peer's configuration to include the
1156 * max_path_compression specified as a command line argument as well as the
1157 * policy_file for this peer. The gnunet-service-regexprofiler service is
1158 * automatically started on this peer. The service reads the configurration and
1159 * announces the regexes stored in the policy file 'filename'.
1161 * @param cls closure
1162 * @param filename complete filename (absolute path)
1163 * @return GNUNET_OK to continue to iterate,
1164 * GNUNET_SYSERR to abort iteration with error!
1167 policy_filename_cb (void *cls, const char *filename)
1169 static unsigned int peer_cnt;
1170 struct DLLOperation *dll_op;
1171 struct RegexPeer *peer = &peers[peer_cnt];
1173 GNUNET_assert (NULL != peer);
1175 peer->policy_file = GNUNET_strdup (filename);
1177 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Creating peer %i on host %s for policy file %s\n",
1179 GNUNET_TESTBED_host_get_hostname (peer->host_handle),
1182 /* Set configuration options specific for this peer
1183 (max_path_compression and policy_file */
1184 peer->cfg = GNUNET_CONFIGURATION_dup (cfg);
1185 GNUNET_CONFIGURATION_set_value_number (peer->cfg, "REGEXPROFILER",
1186 "MAX_PATH_COMPRESSION",
1187 (unsigned long long)max_path_compression);
1188 GNUNET_CONFIGURATION_set_value_string (peer->cfg, "REGEXPROFILER",
1189 "POLICY_FILE", filename);
1191 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1192 dll_op->cls = &peers[peer_cnt];
1193 dll_op->op = GNUNET_TESTBED_peer_create (mc,
1198 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1207 * Controller event callback.
1210 * @param event the controller event
1213 controller_event_cb (void *cls,
1214 const struct GNUNET_TESTBED_EventInformation *event)
1216 struct DLLOperation *dll_op;
1217 struct GNUNET_TESTBED_Operation *op;
1222 case STATE_SLAVES_STARTING:
1223 switch (event->type)
1225 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1227 static unsigned int slaves_started;
1228 unsigned int peer_cnt;
1230 dll_op = event->details.operation_finished.op_cls;
1231 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1232 GNUNET_free (dll_op);
1233 op = event->details.operation_finished.operation;
1234 if (NULL != event->details.operation_finished.emsg)
1236 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1237 _("An operation has failed while starting slaves\n"));
1238 GNUNET_TESTBED_operation_done (op);
1239 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1240 GNUNET_SCHEDULER_cancel (abort_task);
1241 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1244 GNUNET_TESTBED_operation_done (op);
1245 /* Proceed to start peers */
1246 if (++slaves_started == num_hosts - 1)
1248 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1249 "All slaves started successfully\n");
1251 state = STATE_PEERS_CREATING;
1252 prof_start_time = GNUNET_TIME_absolute_get ();
1254 if (-1 == (ret = GNUNET_DISK_directory_scan (policy_dir,
1258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1259 _("No files found in `%s'\n"),
1261 GNUNET_SCHEDULER_shutdown ();
1264 num_peers = (unsigned int) ret;
1265 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1267 /* Initialize peers */
1268 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1270 struct RegexPeer *peer = &peers[peer_cnt];
1271 peer->id = peer_cnt;
1272 peer->policy_file = NULL;
1273 /* Do not start peers on hosts[0] (master controller) */
1274 peer->host_handle = hosts[1 + (peer_cnt % (num_hosts -1))];
1275 peer->mesh_handle = NULL;
1276 peer->mesh_tunnel_handle = NULL;
1277 peer->stats_handle = NULL;
1278 peer->stats_op_handle = NULL;
1279 peer->search_str = NULL;
1280 peer->search_str_matched = GNUNET_NO;
1283 GNUNET_DISK_directory_scan (policy_dir,
1284 &policy_filename_cb,
1293 case STATE_PEERS_STARTING:
1294 switch (event->type)
1296 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1297 /* Control reaches here when peer start fails */
1298 case GNUNET_TESTBED_ET_PEER_START:
1299 /* we handle peer starts in peer_churn_cb */
1305 case STATE_PEERS_LINKING:
1306 switch (event->type)
1308 static unsigned int established_links;
1309 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1310 /* Control reaches here when a peer linking operation fails */
1311 if (NULL != event->details.operation_finished.emsg)
1313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1314 _("An operation has failed while linking\n"));
1319 /* We do no retries, consider this link as established */
1321 case GNUNET_TESTBED_ET_CONNECT:
1323 char output_buffer[512];
1326 if (0 == established_links)
1327 printf ("Establishing links .");
1333 if (++established_links == num_links)
1336 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1337 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1338 "%u links established in %s\n",
1340 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1342 GNUNET_free (peer_handles);
1344 if (NULL != data_file)
1347 GNUNET_snprintf (output_buffer,
1348 sizeof (output_buffer),
1349 "# of peers: %u\n# of links established: %u\n"
1350 "Time to establish links: %s\nLinking failures: %u\n"
1351 "path compression length: %u\n# of search strings: %u\n",
1353 (established_links - cont_fails),
1354 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
1356 max_path_compression,
1357 num_search_strings);
1359 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
1360 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
1363 printf ("\nWaiting %s before starting to search.\n",
1364 GNUNET_STRINGS_relative_time_to_string (search_delay, GNUNET_YES));
1367 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1368 "Waiting %s before starting to search.\n",
1369 GNUNET_STRINGS_relative_time_to_string (search_delay, GNUNET_NO));
1371 state = STATE_SEARCH_REGEX;
1373 search_task = GNUNET_SCHEDULER_add_delayed (search_delay,
1374 &do_connect_by_string, NULL);
1382 case STATE_SEARCH_REGEX:
1384 /* Handled in service connect callback */
1390 case STATE_PEERS_CREATING:
1391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create peer\n");
1394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1395 "Unexpected controller_cb with state %i!\n", state);
1403 * Task to register all hosts available in the global host list.
1406 * @param tc the scheduler task context
1409 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1413 * Callback which will be called to after a host registration succeeded or failed
1415 * @param cls the closure
1416 * @param emsg the error message; NULL if host registration is successful
1419 host_registration_completion (void *cls, const char *emsg)
1424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1425 _("Host registration failed for a host. Error: %s\n"), emsg);
1426 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1427 GNUNET_SCHEDULER_cancel (abort_task);
1428 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1431 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1436 * Task to register all hosts available in the global host list.
1439 * @param tc the scheduler task context
1442 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1444 struct DLLOperation *dll_op;
1445 static unsigned int reg_host;
1448 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1449 if (reg_host == num_hosts - 1)
1451 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1452 "All hosts successfully registered\n");
1454 state = STATE_SLAVES_STARTING;
1455 for (slave = 1; slave < num_hosts; slave++)
1457 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1458 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
1464 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1468 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
1469 host_registration_completion,
1475 * Callback to signal successfull startup of the controller process.
1477 * @param cls the closure from GNUNET_TESTBED_controller_start()
1478 * @param config the configuration with which the controller has been started;
1479 * NULL if status is not GNUNET_OK
1480 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1481 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1484 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
1486 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1487 GNUNET_SCHEDULER_cancel (abort_task);
1488 if (GNUNET_OK != status)
1491 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1495 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1496 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1497 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1498 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1499 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1500 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
1501 &controller_event_cb, NULL);
1504 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1505 _("Unable to connect to master controller -- Check config\n"));
1506 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1509 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1510 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1516 * Load search strings from given filename. One search string per line.
1518 * @param filename filename of the file containing the search strings.
1519 * @param strings set of strings loaded from file. Caller needs to free this
1520 * if number returned is greater than zero.
1521 * @param limit upper limit on the number of strings read from the file
1522 * @return number of strings found in the file. GNUNET_SYSERR on error.
1525 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1530 unsigned int offset;
1534 if (NULL == filename)
1536 return GNUNET_SYSERR;
1539 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1541 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1542 "Could not find search strings file %s\n", filename);
1543 return GNUNET_SYSERR;
1545 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1549 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1550 return GNUNET_SYSERR;
1552 data = GNUNET_malloc (filesize);
1553 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1556 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1558 return GNUNET_SYSERR;
1563 while (offset < (filesize - 1) && str_cnt < limit)
1566 if (((data[offset] == '\n')) && (buf != &data[offset]))
1568 data[offset] = '\0';
1570 buf = &data[offset + 1];
1572 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1573 buf = &data[offset + 1];
1575 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1577 for (i = 0; i < str_cnt; i++)
1579 (*strings)[i] = GNUNET_strdup (&data[offset]);
1580 offset += strlen ((*strings)[i]) + 1;
1588 * Main function that will be run by the scheduler.
1590 * @param cls closure
1591 * @param args remaining command-line arguments
1592 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1593 * @param config configuration
1596 run (void *cls, char *const *args, const char *cfgfile,
1597 const struct GNUNET_CONFIGURATION_Handle *config)
1600 unsigned int nsearchstrs;
1602 if (NULL == args[0])
1604 fprintf (stderr, _("No hosts-file specified on command line. Exiting.\n"));
1607 if (NULL == args[1])
1609 fprintf (stderr, _("No policy directory specified on command line. Exiting.\n"));
1612 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
1615 fprintf (stderr, _("No hosts loaded. Need at least one host\n"));
1618 for (nhost = 0; nhost < num_hosts; nhost++)
1620 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost], config))
1622 fprintf (stderr, _("Host %s cannot start testbed\n"),
1623 GNUNET_TESTBED_host_get_hostname (hosts[nhost]));
1627 if (num_hosts != nhost)
1629 fprintf (stderr, _("Exiting\n"));
1630 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1635 fprintf (stderr, _("No configuration file given. Exiting\n"));
1636 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1639 if ( (NULL != data_filename) &&
1640 (NULL == (data_file =
1641 GNUNET_DISK_file_open (data_filename,
1642 GNUNET_DISK_OPEN_READWRITE |
1643 GNUNET_DISK_OPEN_TRUNCATE |
1644 GNUNET_DISK_OPEN_CREATE,
1645 GNUNET_DISK_PERM_USER_READ |
1646 GNUNET_DISK_PERM_USER_WRITE))) )
1647 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1650 if (GNUNET_YES != GNUNET_DISK_directory_test (args[1]))
1652 fprintf (stderr, _("Specified policies directory does not exist. Exiting.\n"));
1653 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1656 policy_dir = args[1];
1657 if (GNUNET_YES != GNUNET_DISK_file_test (args[2]))
1659 fprintf (stderr, _("No search strings file given. Exiting.\n"));
1660 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1663 nsearchstrs = load_search_strings (args[2], &search_strings, num_search_strings);
1664 if (num_search_strings != nsearchstrs)
1666 num_search_strings = nsearchstrs;
1667 fprintf (stderr, _("Error loading search strings. Given file does not contain enough strings. Exiting.\n"));
1668 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1671 if (0 >= num_search_strings || NULL == search_strings)
1673 fprintf (stderr, _("Error loading search strings. Exiting.\n"));
1674 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1678 for (i = 0; i < num_search_strings; i++)
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "search string: %s\n", search_strings[i]);
1680 cfg = GNUNET_CONFIGURATION_dup (config);
1682 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
1689 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1690 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
1698 * @param argc argument count
1699 * @param argv argument values
1700 * @return 0 on success
1703 main (int argc, char *const *argv)
1705 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1706 {'d', "details", "FILENAME",
1707 gettext_noop ("name of the file for writing statistics"),
1708 1, &GNUNET_GETOPT_set_string, &data_filename},
1709 {'n', "num-links", "COUNT",
1710 gettext_noop ("create COUNT number of random links between peers"),
1711 GNUNET_YES, &GNUNET_GETOPT_set_uint, &linking_factor },
1712 {'t', "matching-timeout", "TIMEOUT",
1713 gettext_noop ("wait TIMEOUT before considering a string match as failed"),
1714 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout },
1715 {'s', "search-delay", "DELAY",
1716 gettext_noop ("wait DELAY before starting string search"),
1717 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
1718 {'a', "num-search-strings", "COUNT",
1719 gettext_noop ("number of search strings to read from search strings file"),
1720 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
1721 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
1722 gettext_noop ("maximum path compression length"),
1723 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
1724 GNUNET_GETOPT_OPTION_END
1728 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1731 result = GNUNET_SYSERR;
1733 GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
1734 _("Profiler for regex/mesh"),
1735 options, &run, NULL);
1736 GNUNET_free ((void*) argv);
1737 if (GNUNET_OK != ret)
1739 if (GNUNET_OK != result)