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 * Destroying peers; we can do this as the controller takes care of stopping a
97 * peer if it is running
99 STATE_PEERS_DESTROYING
104 * An array of hosts loaded from the hostkeys file
106 static struct GNUNET_TESTBED_Host **hosts;
119 * The actual testbed peer handle.
121 struct GNUNET_TESTBED_Peer *peer_handle;
124 * Host on which the peer is running.
126 struct GNUNET_TESTBED_Host *host_handle;
129 * Filename of the peer's policy file.
134 * Peers search string.
136 const char *search_str;
139 * Peer's mesh handle.
141 struct GNUNET_MESH_Handle *mesh_handle;
144 * Peer's mesh tunnel handle.
146 struct GNUNET_MESH_Tunnel *mesh_tunnel_handle;
149 * Testbed operation handle for the mesh service.
151 struct GNUNET_TESTBED_Operation *mesh_op_handle;
154 * Peers's statistics handle.
156 struct GNUNET_STATISTICS_Handle *stats_handle;
159 * Testbed operation handle for the statistics service.
161 struct GNUNET_TESTBED_Operation *stats_op_handle;
165 * Array of peer handles used to pass to
166 * GNUNET_TESTBED_overlay_configure_topology
168 struct GNUNET_TESTBED_Peer **peer_handles;
171 * The array of peers; we fill this as the peers are given to us by the testbed
173 static struct RegexPeer *peers;
176 * Host registration handle
178 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
181 * Handle to the master controller process
183 struct GNUNET_TESTBED_ControllerProc *mc_proc;
186 * Handle to the master controller
188 struct GNUNET_TESTBED_Controller *mc;
191 * Handle to global configuration
193 struct GNUNET_CONFIGURATION_Handle *cfg;
196 * Head of the operations list
198 struct DLLOperation *dll_op_head;
201 * Tail of the operations list
203 struct DLLOperation *dll_op_tail;
206 * Peer linking - topology operation
208 struct GNUNET_TESTBED_Operation *topology_op;
211 * Abort task identifier
213 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
216 * Host registration task identifier
218 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
221 * Global event mask for all testbed events
226 * The starting time of a profiling step
228 struct GNUNET_TIME_Absolute prof_start_time;
231 * Duration profiling step has taken
233 struct GNUNET_TIME_Relative prof_time;
238 unsigned int peer_id;
241 * Number of peers to be started by the profiler
243 static unsigned int num_peers;
246 * Number of hosts in the hosts array
248 static unsigned int num_hosts;
251 * Number of random links to be established between peers
253 static unsigned int num_links;
256 * Number of timeout failures to tolerate
258 static unsigned int num_cont_fails;
261 * Number of times we try overlay connect operations
263 static unsigned int retry_links;
266 * Continuous failures during overlay connect operations
268 static unsigned int cont_fails;
271 * Global testing status
276 * current state of profiling
281 * Folder where policy files are stored.
283 static char * policy_dir;
288 static char **search_strings;
291 * Number of search strings.
293 static int num_search_strings;
296 * Number of peers found with search strings.
298 static unsigned int peers_found;
301 * Search task identifier
303 static GNUNET_SCHEDULER_TaskIdentifier search_task;
306 * Search timeout task identifier.
308 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
311 * Search timeout in seconds.
313 static struct GNUNET_TIME_Relative search_timeout = { 60000 };
316 * How long do we wait before starting the search?
319 static struct GNUNET_TIME_Relative search_delay = { 60000 };
322 * File to log statistics to.
324 static struct GNUNET_DISK_FileHandle *data_file;
327 * Filename to log statistics to.
329 static char *data_filename;
336 * @param tc the task context
339 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
341 struct DLLOperation *dll_op;
343 unsigned int peer_cnt;
344 unsigned int search_str_cnt;
346 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
348 if (NULL != peers[peer_cnt].mesh_op_handle)
349 GNUNET_TESTBED_operation_cancel (peers[peer_cnt].mesh_op_handle);
350 if (NULL != peers[peer_cnt].stats_op_handle)
351 GNUNET_TESTBED_operation_cancel (peers[peer_cnt].stats_op_handle);
353 for (search_str_cnt = 0; search_str_cnt < num_search_strings; search_str_cnt++)
355 GNUNET_free (search_strings[search_str_cnt]);
357 GNUNET_free (search_strings);
358 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
359 GNUNET_SCHEDULER_cancel (abort_task);
360 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
361 GNUNET_SCHEDULER_cancel (register_hosts_task);
362 if (NULL != reg_handle)
363 GNUNET_TESTBED_cancel_registration (reg_handle);
364 if (NULL != topology_op)
365 GNUNET_TESTBED_operation_cancel (topology_op);
366 for (nhost = 0; nhost < num_hosts; nhost++)
367 if (NULL != hosts[nhost])
368 GNUNET_TESTBED_host_destroy (hosts[nhost]);
369 GNUNET_free_non_null (hosts);
370 while (NULL != (dll_op = dll_op_head))
372 GNUNET_TESTBED_operation_cancel (dll_op->op);
373 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
374 GNUNET_free (dll_op);
377 GNUNET_TESTBED_controller_disconnect (mc);
379 GNUNET_TESTBED_controller_stop (mc_proc);
381 GNUNET_CONFIGURATION_destroy (cfg);
382 if (NULL != data_file)
383 GNUNET_DISK_file_close (data_file);
385 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
390 * abort task to run on test timed out
393 * @param tc the task context
396 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting\n");
399 abort_task = GNUNET_SCHEDULER_NO_TASK;
400 result = GNUNET_SYSERR;
401 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
405 /******************************************************************************/
406 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
407 /******************************************************************************/
410 * Adapter function called to establish a connection to
411 * statistics service.
414 * @param cfg configuration of the peer to connect to; will be available until
415 * GNUNET_TESTBED_operation_done() is called on the operation returned
416 * from GNUNET_TESTBED_service_connect()
417 * @return service handle to return in 'op_result', NULL on error
420 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
422 return GNUNET_STATISTICS_create ("<driver>", cfg);
427 * Adapter function called to destroy a connection to
428 * statistics service.
431 * @param op_result service handle returned from the connect adapter
434 stats_da (void *cls, void *op_result)
436 GNUNET_STATISTICS_destroy (op_result, GNUNET_NO);
441 * Process statistic values.
444 * @param subsystem name of subsystem that created the statistic
445 * @param name the name of the datum
446 * @param value the current value
447 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
448 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
451 stats_iterator (void *cls, const char *subsystem, const char *name,
452 uint64_t value, int is_persistent)
454 struct RegexPeer *peer = cls;
455 char output_buffer[512];
458 if (NULL == data_file)
460 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
461 "%p -> %s [%s]: %llu\n",
462 peer, subsystem, name, value);
466 GNUNET_snprintf (output_buffer,
467 sizeof (output_buffer),
470 subsystem, value, name);
471 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
472 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
482 * @param success GNUNET_OK if statistics were
483 * successfully obtained, GNUNET_SYSERR if not.
489 static unsigned int peer_cnt;
490 struct RegexPeer *peer = cls;
492 if (GNUNET_OK != success)
494 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
495 "Getting statistics for peer %u failed!\n",
500 if (++peer_cnt == num_search_strings)
502 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
508 * Function called by testbed once we are connected to stats service.
510 * @param cls the 'struct RegexPeer' for which we connected to stats
511 * @param op connect operation handle
512 * @param ca_result handle to stats service
513 * @param emsg error message on failure
516 stats_connect_cb (void *cls,
517 struct GNUNET_TESTBED_Operation *op,
521 struct RegexPeer *peer = cls;
523 if (NULL == ca_result)
525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
526 "Failed to connect to statistics service on peer %u: %s\n",
528 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
532 peer->stats_handle = ca_result;
536 /******************************************************************************/
537 /************************ MESH SERVICE CONNECTIONS **************************/
538 /******************************************************************************/
541 * Method called whenever another peer has added us to a tunnel
542 * the other peer initiated.
543 * Only called (once) upon reception of data with a message type which was
544 * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
545 * causes te tunnel to be ignored and no further notifications are sent about
549 * @param tunnel new handle to the tunnel
550 * @param initiator peer that started the tunnel
551 * @param atsi performance information for the tunnel
552 * @return initial tunnel context for the tunnel
553 * (can be NULL -- that's not an error)
556 mesh_inbound_tunnel_handler (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
557 const struct GNUNET_PeerIdentity *initiator,
558 const struct GNUNET_ATS_Information *atsi)
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh inbound tunnel handler.\n");
567 * Function called whenever an inbound tunnel is destroyed. Should clean up
568 * any associated state. This function is NOT called if the client has
569 * explicitly asked for the tunnel to be destroyed using
570 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
573 * @param cls closure (set from GNUNET_MESH_connect)
574 * @param tunnel connection to the other end (henceforth invalid)
575 * @param tunnel_ctx place where local state associated
576 * with the tunnel is stored
579 mesh_tunnel_end_handler (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh tunnel end handler.\n");
587 * Method called whenever a peer has disconnected from the tunnel.
588 * Implementations of this callback must NOT call
589 * GNUNET_MESH_tunnel_destroy immediately, but instead schedule those
590 * to run in some other task later. However, calling
591 * "GNUNET_MESH_notify_transmit_ready_cancel" is allowed.
594 * @param peer_id peer identity the tunnel stopped working with
597 mesh_peer_disconnect_handler (void *cls,
598 const struct GNUNET_PeerIdentity * peer_id)
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh peer disconnect handler.\n");
605 * Method called whenever a peer has connected to the tunnel.
608 * @param peer_id peer identity the tunnel was created to, NULL on timeout
609 * @param atsi performance data for the connection
613 mesh_peer_connect_handler (void *cls,
614 const struct GNUNET_PeerIdentity* peer_id,
615 const struct GNUNET_ATS_Information * atsi)
617 struct RegexPeer *peer = cls;
618 char output_buffer[512];
625 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
626 "String matching timed out for string %s on peer %u (%i/%i)\n",
627 peer->search_str, peer->id, peers_found, num_search_strings);
631 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
632 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
633 "String %s successfully matched on peer %u after %s (%i/%i)\n",
634 peer->search_str, peer->id, GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
635 peers_found, num_search_strings);
637 if (NULL != data_file)
640 GNUNET_snprintf (output_buffer,
641 sizeof (output_buffer),
642 "Peer: %u (%p)\nHost: %s\nPolicy file: %s\nSearch string: %s\nSearch duration: %s\n\n",
645 GNUNET_TESTBED_host_get_hostname (peer->host_handle),
648 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
650 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
651 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
654 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, "mesh", NULL,
655 GNUNET_TIME_UNIT_FOREVER_REL,
657 &stats_iterator, peer))
659 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
660 "Could not get mesh statistics of peer %u!\n", peer->id);
662 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, "transport", NULL,
663 GNUNET_TIME_UNIT_FOREVER_REL,
665 &stats_iterator, peer))
667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
668 "Could not get transport statistics of peer %u!\n", peer->id);
670 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, "dht", NULL,
671 GNUNET_TIME_UNIT_FOREVER_REL,
673 &stats_iterator, peer))
675 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
676 "Could not get dht statistics of peer %u!\n", peer->id);
680 if (peers_found == num_search_strings)
682 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
683 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
684 "All strings successfully matched in %s\n",
685 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
687 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
688 GNUNET_SCHEDULER_cancel (search_timeout_task);
694 * Connect by string timeout task
697 * @param tc the task context
700 do_connect_by_string_timeout (void *cls,
701 const struct GNUNET_SCHEDULER_TaskContext * tc)
703 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
704 "Finding matches to all strings did not succeed after %s.\n",
705 GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
706 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
707 "Found %i of %i strings\n", peers_found, num_search_strings);
709 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
714 * Connect by string task that is run to search for a string in the NFA
717 * @param tc the task context
720 do_connect_by_string (void *cls,
721 const struct GNUNET_SCHEDULER_TaskContext * tc)
723 unsigned int search_cnt;
724 struct RegexPeer *peer;
726 for (search_cnt = 0; search_cnt < num_search_strings; search_cnt++)
728 peer = &peers[search_cnt % num_peers];
729 peer->search_str = search_strings[search_cnt];
731 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
732 "Searching for string \"%s\" on peer %d with file %s\n",
733 peer->search_str, (search_cnt % num_peers), peer->policy_file);
735 peer->mesh_tunnel_handle = GNUNET_MESH_tunnel_create (peer->mesh_handle,
737 &mesh_peer_connect_handler,
738 &mesh_peer_disconnect_handler,
741 GNUNET_MESH_peer_request_connect_by_string (peer->mesh_tunnel_handle,
745 prof_start_time = GNUNET_TIME_absolute_get ();
747 search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout,
748 &do_connect_by_string_timeout, NULL);
753 * Mesh connect callback.
755 * @param cls internal peer id.
756 * @param op operation handle.
757 * @param ca_result connect adapter result.
758 * @param emsg error message.
761 mesh_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
762 void *ca_result, const char *emsg)
764 static unsigned int connected_mesh_handles;
765 struct RegexPeer *peer = (struct RegexPeer *) cls;
774 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Mesh connect failed: %s\n", emsg);
778 GNUNET_assert (peer->mesh_op_handle == op);
779 GNUNET_assert (peer->mesh_handle == ca_result);
780 GNUNET_assert (NULL != peer->policy_file);
782 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
783 "Announcing regexes for peer %u with file %s\n",
784 peer->id, peer->policy_file);
786 if (GNUNET_YES != GNUNET_DISK_file_test (peer->policy_file))
788 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
789 "Could not find policy file %s\n", peer->policy_file);
792 if (GNUNET_OK != GNUNET_DISK_file_size (peer->policy_file, &filesize, GNUNET_YES, GNUNET_YES))
796 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Policy file %s is empty.\n", peer->policy_file);
799 data = GNUNET_malloc (filesize);
800 if (filesize != GNUNET_DISK_fn_read (peer->policy_file, data, filesize))
803 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read policy file %s.\n",
810 while (offset < (filesize - 1))
813 if (((data[offset] == '\n')) && (buf != &data[offset]))
817 GNUNET_assert (NULL != regex);
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s on peer %u \n",
820 GNUNET_MESH_announce_regex (peer->mesh_handle, regex);
821 buf = &data[offset + 1];
823 else if ((data[offset] == '\n') || (data[offset] == '\0'))
824 buf = &data[offset + 1];
828 if (++connected_mesh_handles == num_peers)
830 printf ("Waiting %s before starting to search.\n",
831 GNUNET_STRINGS_relative_time_to_string (search_delay, GNUNET_YES));
834 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
835 "All mesh handles connected. Waiting %s before starting to search.\n",
836 GNUNET_STRINGS_relative_time_to_string (search_delay, GNUNET_YES));
838 search_task = GNUNET_SCHEDULER_add_delayed (search_delay,
839 &do_connect_by_string, NULL);
845 * Mesh connect adapter.
847 * @param cls not used.
848 * @param cfg configuration handle.
853 mesh_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
855 GNUNET_MESH_ApplicationType app;
856 struct RegexPeer *peer = cls;
858 static struct GNUNET_MESH_MessageHandler handlers[] = {
862 app = (GNUNET_MESH_ApplicationType)0;
865 GNUNET_MESH_connect (cfg, cls, NULL, NULL, handlers, &app);
867 return peer->mesh_handle;
872 * Adapter function called to destroy a connection to
876 * @param op_result service handle returned from the connect adapter
879 mesh_da (void *cls, void *op_result)
881 struct RegexPeer *peer = (struct RegexPeer *) cls;
883 GNUNET_assert (peer->mesh_handle == op_result);
885 if (NULL != peer->mesh_tunnel_handle)
887 GNUNET_MESH_tunnel_destroy (peer->mesh_tunnel_handle);
888 peer->mesh_tunnel_handle = NULL;
891 if (NULL != peer->mesh_handle)
893 GNUNET_MESH_disconnect (peer->mesh_handle);
894 peer->mesh_handle = NULL;
899 /******************************************************************************/
900 /*************************** TESTBED PEER SETUP *****************************/
901 /******************************************************************************/
905 * Functions of this signature are called when a peer has been successfully
906 * started or stopped.
908 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
909 * @param emsg NULL on success; otherwise an error description
912 peer_churn_cb (void *cls, const char *emsg)
914 struct DLLOperation *dll_op = cls;
915 struct GNUNET_TESTBED_Operation *op;
916 static unsigned int started_peers;
917 unsigned int peer_cnt;
920 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
921 GNUNET_free (dll_op);
924 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
925 _("An operation has failed while starting peers\n"));
926 GNUNET_TESTBED_operation_done (op);
927 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
928 GNUNET_SCHEDULER_cancel (abort_task);
929 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
932 GNUNET_TESTBED_operation_done (op);
933 if (++started_peers == num_peers)
935 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
936 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
937 "All peers started successfully in %s\n",
938 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
942 num_links = num_peers * 5;
944 peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
945 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
946 peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
948 state = STATE_PEERS_LINKING;
949 /* Do overlay connect */
950 prof_start_time = GNUNET_TIME_absolute_get ();
952 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
953 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
955 GNUNET_TESTBED_TOPOLOGY_DISABLE_AUTO_RETRY,
956 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
957 if (NULL == topology_op)
959 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
960 "Cannot create topology, op handle was NULL\n");
968 * Functions of this signature are called when a peer has been successfully
971 * @param cls the closure from GNUNET_TESTBED_peer_create()
972 * @param peer the handle for the created peer; NULL on any error during
974 * @param emsg NULL if peer is not NULL; else MAY contain the error description
977 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
979 struct DLLOperation *dll_op = cls;
980 struct RegexPeer *peer_ptr;
981 static unsigned int created_peers;
982 unsigned int peer_cnt;
986 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
987 _("Creating a peer failed. Error: %s\n"), emsg);
988 GNUNET_TESTBED_operation_done (dll_op->op);
989 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
990 GNUNET_free (dll_op);
991 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
992 GNUNET_SCHEDULER_cancel (abort_task);
993 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
997 peer_ptr = dll_op->cls;
998 GNUNET_assert (NULL == peer_ptr->peer_handle);
999 peer_ptr->peer_handle = peer;
1000 GNUNET_TESTBED_operation_done (dll_op->op);
1001 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1002 GNUNET_free (dll_op);
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
1006 GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
1008 if (++created_peers == num_peers)
1010 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1011 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1012 "All peers created successfully in %s\n",
1013 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1014 /* Now peers are to be started */
1015 state = STATE_PEERS_STARTING;
1016 prof_start_time = GNUNET_TIME_absolute_get ();
1017 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1019 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1020 dll_op->op = GNUNET_TESTBED_peer_start (dll_op, peers[peer_cnt].peer_handle,
1021 &peer_churn_cb, dll_op);
1022 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1028 * Function called with a filename.
1030 * @param cls closure
1031 * @param filename complete filename (absolute path)
1032 * @return GNUNET_OK to continue to iterate,
1033 * GNUNET_SYSERR to abort iteration with error!
1036 policy_filename_cb (void *cls, const char *filename)
1038 static unsigned int peer_cnt;
1039 struct DLLOperation *dll_op;
1040 struct RegexPeer *peer = &peers[peer_cnt];
1042 GNUNET_assert (NULL != peer);
1044 peer->id = peer_cnt;
1045 peer->policy_file = GNUNET_strdup (filename);
1046 /* Do not start peers on hosts[0] (master controller) */
1047 peer->host_handle = hosts[1 + (peer_cnt % (num_hosts -1))];
1048 peer->mesh_handle = NULL;
1049 peer->mesh_tunnel_handle = NULL;
1050 peer->stats_handle = NULL;
1051 peer->stats_op_handle = NULL;
1053 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Creating peer %i on host %s for policy file %s\n",
1055 GNUNET_TESTBED_host_get_hostname (peer->host_handle),
1058 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1059 dll_op->cls = &peers[peer_cnt];
1060 dll_op->op = GNUNET_TESTBED_peer_create (mc,
1065 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1073 * Controller event callback.
1076 * @param event the controller event
1079 controller_event_cb (void *cls,
1080 const struct GNUNET_TESTBED_EventInformation *event)
1082 struct DLLOperation *dll_op;
1083 struct GNUNET_TESTBED_Operation *op;
1087 case STATE_SLAVES_STARTING:
1088 switch (event->type)
1090 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1092 static unsigned int slaves_started;
1094 dll_op = event->details.operation_finished.op_cls;
1095 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1096 GNUNET_free (dll_op);
1097 op = event->details.operation_finished.operation;
1098 if (NULL != event->details.operation_finished.emsg)
1100 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1101 _("An operation has failed while starting slaves\n"));
1102 GNUNET_TESTBED_operation_done (op);
1103 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1104 GNUNET_SCHEDULER_cancel (abort_task);
1105 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1108 GNUNET_TESTBED_operation_done (op);
1109 /* Proceed to start peers */
1110 if (++slaves_started == num_hosts - 1)
1112 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1113 "All slaves started successfully\n");
1115 state = STATE_PEERS_CREATING;
1116 prof_start_time = GNUNET_TIME_absolute_get ();
1118 num_peers = GNUNET_DISK_directory_scan (policy_dir,
1121 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1123 GNUNET_DISK_directory_scan (policy_dir,
1124 &policy_filename_cb,
1133 case STATE_PEERS_STARTING:
1134 switch (event->type)
1136 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1137 /* Control reaches here when peer start fails */
1138 case GNUNET_TESTBED_ET_PEER_START:
1139 /* we handle peer starts in peer_churn_cb */
1145 case STATE_PEERS_LINKING:
1146 switch (event->type)
1148 static unsigned int established_links;
1149 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1150 /* Control reaches here when a peer linking operation fails */
1151 if (NULL != event->details.operation_finished.emsg)
1153 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1154 _("An operation has failed while linking\n"));
1159 if (++cont_fails > num_cont_fails)
1161 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1162 "We have a very high peer linking failure rate: %u (threshold: %u)\n",
1167 /* We do no retries, consider this link as established */
1169 case GNUNET_TESTBED_ET_CONNECT:
1171 unsigned int peer_cnt;
1172 char output_buffer[512];
1175 if (0 == established_links)
1176 printf ("Establishing links .");
1182 if (++established_links == num_links)
1186 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1187 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1188 "%u links established in %s\n",
1190 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1192 GNUNET_free (peer_handles);
1194 if (NULL != data_file)
1197 GNUNET_snprintf (output_buffer,
1198 sizeof (output_buffer),
1199 "# of peers: %u\n# of links established: %u\nTime to establish links: %s\nLinking failures: %u\n\n",
1201 (established_links - cont_fails),
1202 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
1205 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
1206 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
1209 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1210 "Connecting to mesh and statistics service...\n");
1211 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1213 peers[peer_cnt].mesh_op_handle =
1214 GNUNET_TESTBED_service_connect (NULL,
1215 peers[peer_cnt].peer_handle,
1223 peers[peer_cnt].stats_op_handle =
1224 GNUNET_TESTBED_service_connect (NULL,
1225 peers[peer_cnt].peer_handle,
1247 * Task to register all hosts available in the global host list
1250 * @param tc the scheduler task context
1253 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1257 * Callback which will be called to after a host registration succeeded or failed
1259 * @param cls the closure
1260 * @param emsg the error message; NULL if host registration is successful
1263 host_registration_completion (void *cls, const char *emsg)
1268 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1269 _("Host registration failed for a host. Error: %s\n"), emsg);
1270 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1271 GNUNET_SCHEDULER_cancel (abort_task);
1272 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1275 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1280 * Task to register all hosts available in the global host list
1283 * @param tc the scheduler task context
1286 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1288 struct DLLOperation *dll_op;
1289 static unsigned int reg_host;
1292 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1293 if (reg_host == num_hosts - 1)
1295 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1296 "All hosts successfully registered\n");
1298 state = STATE_SLAVES_STARTING;
1299 for (slave = 1; slave < num_hosts; slave++)
1301 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1302 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
1308 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1312 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
1313 host_registration_completion,
1319 * Callback to signal successfull startup of the controller process
1321 * @param cls the closure from GNUNET_TESTBED_controller_start()
1322 * @param config the configuration with which the controller has been started;
1323 * NULL if status is not GNUNET_OK
1324 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1325 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1328 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
1330 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1331 GNUNET_SCHEDULER_cancel (abort_task);
1332 if (GNUNET_OK != status)
1335 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1339 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1340 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1341 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1342 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1343 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1344 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
1345 &controller_event_cb, NULL);
1348 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1349 _("Unable to connect to master controller -- Check config\n"));
1350 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
1353 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1354 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1359 * Load search strings from given filename. One search string per line.
1361 * @param filename filename of the file containing the search strings.
1362 * @param strings set of strings loaded from file. Caller needs to free this
1363 * if number returned is greater than zero.
1364 * @return number of strings found in the file. GNUNET_SYSERR on error.
1367 load_search_strings (const char *filename, char ***strings)
1372 unsigned int offset;
1376 if (NULL == filename)
1378 return GNUNET_SYSERR;
1381 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1384 "Could not find search strings file %s\n", filename);
1385 return GNUNET_SYSERR;
1387 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1392 return GNUNET_SYSERR;
1394 data = GNUNET_malloc (filesize);
1395 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1400 return GNUNET_SYSERR;
1405 while (offset < (filesize - 1))
1408 if (((data[offset] == '\n')) && (buf != &data[offset]))
1410 data[offset] = '\0';
1412 buf = &data[offset + 1];
1414 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1415 buf = &data[offset + 1];
1417 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1419 for (i = 0; i < str_cnt; i++)
1421 (*strings)[i] = GNUNET_strdup (&data[offset]);
1422 offset += strlen ((*strings)[i]) + 1;
1429 * Main function that will be run by the scheduler.
1431 * @param cls closure
1432 * @param args remaining command-line arguments
1433 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1434 * @param config configuration
1437 run (void *cls, char *const *args, const char *cfgfile,
1438 const struct GNUNET_CONFIGURATION_Handle *config)
1442 if (NULL == args[0])
1444 fprintf (stderr, _("No hosts-file specified on command line. Exiting.\n"));
1447 if (NULL == args[1])
1449 fprintf (stderr, _("No policy directory specified on command line. Exiting.\n"));
1452 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
1455 fprintf (stderr, _("No hosts loaded. Need at least one host\n"));
1458 for (nhost = 0; nhost < num_hosts; nhost++)
1460 if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (hosts[nhost]))
1462 fprintf (stderr, _("Host %s cannot start testbed\n"),
1463 GNUNET_TESTBED_host_get_hostname (hosts[nhost]));
1467 if (num_hosts != nhost)
1469 fprintf (stderr, _("Exiting\n"));
1470 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1475 fprintf (stderr, _("No configuration file given. Exiting\n"));
1476 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1479 if ( (NULL != data_filename) &&
1480 (NULL == (data_file =
1481 GNUNET_DISK_file_open (data_filename,
1482 GNUNET_DISK_OPEN_READWRITE |
1483 GNUNET_DISK_OPEN_TRUNCATE |
1484 GNUNET_DISK_OPEN_CREATE,
1485 GNUNET_DISK_PERM_USER_READ |
1486 GNUNET_DISK_PERM_USER_WRITE))) )
1487 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1490 if (GNUNET_YES != GNUNET_DISK_directory_test (args[1]))
1492 fprintf (stderr, _("Specified policies directory does not exist. Exiting.\n"));
1493 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1496 policy_dir = args[1];
1497 if (GNUNET_YES != GNUNET_DISK_file_test (args[2]))
1499 fprintf (stderr, _("No search strings file given. Exiting.\n"));
1500 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1503 num_search_strings = load_search_strings (args[2], &search_strings);
1504 if (0 >= num_search_strings || NULL == search_strings)
1506 fprintf (stderr, _("Error loading search strings. Exiting.\n"));
1507 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1511 for (i = 0; i < num_search_strings; i++)
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "search string: %s\n", search_strings[i]);
1513 cfg = GNUNET_CONFIGURATION_dup (config);
1515 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
1522 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1523 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
1531 * @param argc argument count
1532 * @param argv argument values
1533 * @return 0 on success
1536 main (int argc, char *const *argv)
1538 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1539 {'d', "details", "FILENAME",
1540 gettext_noop ("name of the file for writing statistics"),
1541 1, &GNUNET_GETOPT_set_string, &data_filename},
1542 { 'n', "num-links", "COUNT",
1543 gettext_noop ("create COUNT number of random links"),
1544 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_links },
1545 { 'e', "num-errors", "COUNT",
1546 gettext_noop ("tolerate COUNT number of continious timeout failures"),
1547 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_cont_fails },
1548 { 't', "matching-timeout", "TIMEOUT",
1549 gettext_noop ("wait TIMEOUT seconds before considering a string match as failed"),
1550 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout },
1551 { 's', "search-delay", "DELAY",
1552 gettext_noop ("wait DELAY minutes before starting string search"),
1553 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
1554 GNUNET_GETOPT_OPTION_END
1558 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1561 result = GNUNET_SYSERR;
1563 GNUNET_PROGRAM_run (argc, argv, "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
1564 _("Profiler for regex/mesh"),
1565 options, &run, NULL);
1566 if (GNUNET_OK != ret)
1568 if (GNUNET_OK != result)