2 This file is part of GNUnet.
3 (C) 2011 - 2013 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 regex/gnunet-regex-profiler.c
23 * @brief Regex profiler for testing distributed regex use.
24 * @author Bartlomiej Polot
25 * @author Maximilian Szengel
32 #include "gnunet_applications.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_regex_lib.h"
35 #include "gnunet_arm_service.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_testbed_service.h"
45 * The testbed operation handle
47 struct GNUNET_TESTBED_Operation *op;
55 * The next pointer for DLL
57 struct DLLOperation *next;
60 * The prev pointer for DLL
62 struct DLLOperation *prev;
67 * Available states during profiling
79 STATE_SLAVES_STARTING,
97 * Matching strings against announced regexes
102 * Destroying peers; we can do this as the controller takes care of stopping a
103 * peer if it is running
105 STATE_PEERS_DESTROYING
120 * Peer configuration handle.
122 struct GNUNET_CONFIGURATION_Handle *cfg;
125 * The actual testbed peer handle.
127 struct GNUNET_TESTBED_Peer *peer_handle;
130 * Host on which the peer is running.
132 struct GNUNET_TESTBED_Host *host_handle;
135 * Filename of the peer's policy file.
140 * Peers search string.
142 const char *search_str;
145 * Set to GNUNET_YES if the peer successfully matched the above
146 * search string. GNUNET_NO if the string could not be matched
147 * during the profiler run. GNUNET_SYSERR if the string matching
148 * timed out. Undefined if search_str is NULL
150 int search_str_matched;
155 struct GNUNET_ARM_Handle *arm_handle;
160 struct GNUNET_DHT_Handle *dht_handle;
163 * Handle to a running regex search.
165 struct GNUNET_REGEX_search_handle *search_handle;
168 * Testbed operation handle for the ARM and DHT services.
170 struct GNUNET_TESTBED_Operation *op_handle;
173 * Peers's statistics handle.
175 struct GNUNET_STATISTICS_Handle *stats_handle;
178 * Testbed operation handle for the statistics service.
180 struct GNUNET_TESTBED_Operation *stats_op_handle;
183 * The starting time of a profiling step.
185 struct GNUNET_TIME_Absolute prof_start_time;
190 * An array of hosts loaded from the hostkeys file
192 static struct GNUNET_TESTBED_Host **hosts;
195 * Array of peer handles used to pass to
196 * GNUNET_TESTBED_overlay_configure_topology
198 static struct GNUNET_TESTBED_Peer **peer_handles;
201 * The array of peers; we fill this as the peers are given to us by the testbed
203 static struct RegexPeer *peers;
206 * Host registration handle
208 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
211 * Handle to the master controller process
213 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
216 * Handle to the master controller
218 static struct GNUNET_TESTBED_Controller *mc;
221 * Handle to global configuration
223 static struct GNUNET_CONFIGURATION_Handle *cfg;
226 * Head of the operations list
228 static struct DLLOperation *dll_op_head;
231 * Tail of the operations list
233 static struct DLLOperation *dll_op_tail;
236 * Peer linking - topology operation
238 static struct GNUNET_TESTBED_Operation *topology_op;
241 * The handle for whether a host is habitable or not
243 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
246 * Abort task identifier
248 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
251 * Shutdown task identifier
253 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
256 * Host registration task identifier
258 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
261 * Global event mask for all testbed events
263 static uint64_t event_mask;
266 * The starting time of a profiling step
268 static struct GNUNET_TIME_Absolute prof_start_time;
271 * Duration profiling step has taken
273 static struct GNUNET_TIME_Relative prof_time;
276 * Number of peers to be started by the profiler
278 static unsigned int num_peers;
281 * Number of hosts in the hosts array
283 static unsigned int num_hosts;
286 * Factor of number of links. num_links = num_peers * linking_factor.
288 static unsigned int linking_factor;
291 * Number of random links to be established between peers
293 static unsigned int num_links;
296 * Number of times we try overlay connect operations
298 static unsigned int retry_links;
301 * Continuous failures during overlay connect operations
303 static unsigned int cont_fails;
306 * Global testing status
311 * current state of profiling
316 * Folder where policy files are stored.
318 static char * policy_dir;
323 static char **search_strings;
326 * Number of search strings.
328 static int num_search_strings;
331 * Number of peers found with search strings.
333 static unsigned int peers_found;
336 * Search task identifier
338 static GNUNET_SCHEDULER_TaskIdentifier search_task;
341 * Search timeout task identifier.
343 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
346 * Search timeout in seconds.
348 static struct GNUNET_TIME_Relative search_timeout = { 60000 };
351 * How long do we wait before starting the search?
354 static struct GNUNET_TIME_Relative search_delay = { 60000 };
357 * File to log statistics to.
359 static struct GNUNET_DISK_FileHandle *data_file;
362 * Filename to log statistics to.
364 static char *data_filename;
367 * Maximal path compression length.
369 static unsigned int max_path_compression;
372 * If we should distribute the search evenly throught all peers (each
373 * peer searches for a string) or if only one peer should search for
376 static int no_distributed_search;
379 * Prefix used for regex announcing. We need to prefix the search
380 * strings with it, in order to find something.
382 static char * regex_prefix;
385 /******************************************************************************/
386 /****************************** DECLARATIONS ********************************/
387 /******************************************************************************/
391 * Search callback function.
393 * @param cls Closure provided in GNUNET_REGEX_search.
394 * @param id Peer providing a regex that matches the string.
395 * @param get_path Path of the get request.
396 * @param get_path_length Lenght of get_path.
397 * @param put_path Path of the put request.
398 * @param put_path_length Length of the put_path.
401 regex_found_handler (void *cls,
402 const struct GNUNET_PeerIdentity *id,
403 const struct GNUNET_PeerIdentity *get_path,
404 unsigned int get_path_length,
405 const struct GNUNET_PeerIdentity *put_path,
406 unsigned int put_path_length);
410 * DHT connect callback.
412 * @param cls internal peer id.
413 * @param op operation handle.
414 * @param ca_result connect adapter result.
415 * @param emsg error message.
418 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
419 void *ca_result, const char *emsg);
422 * DHT connect adapter.
424 * @param cls not used.
425 * @param cfg configuration handle.
430 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
434 * Adapter function called to destroy a connection to
438 * @param op_result service handle returned from the connect adapter
441 dht_da (void *cls, void *op_result);
445 * Function called by testbed once we are connected to stats
446 * service. Get the statistics for the services of interest.
448 * @param cls the 'struct RegexPeer' for which we connected to stats
449 * @param op connect operation handle
450 * @param ca_result handle to stats service
451 * @param emsg error message on failure
454 stats_connect_cb (void *cls,
455 struct GNUNET_TESTBED_Operation *op,
461 * Task to collect all statistics from all peers, will shutdown the
462 * profiler, when done.
465 * @param tc the task context
468 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
471 /******************************************************************************/
472 /******************************** SHUTDOWN **********************************/
473 /******************************************************************************/
480 * @param tc the task context
483 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
485 struct DLLOperation *dll_op;
486 struct RegexPeer *peer;
488 unsigned int peer_cnt;
489 unsigned int search_str_cnt;
490 char output_buffer[512];
493 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
494 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
495 GNUNET_SCHEDULER_cancel (abort_task);
496 if (NULL != hc_handles)
498 for (nhost = 0; nhost < num_hosts; nhost++)
499 if (NULL != hc_handles[nhost])
500 GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[nhost]);
501 GNUNET_free (hc_handles);
504 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
505 GNUNET_SCHEDULER_cancel (register_hosts_task);
507 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
509 peer = &peers[peer_cnt];
511 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
513 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
515 GNUNET_snprintf (output_buffer,
516 sizeof (output_buffer),
517 "%p Search string not found: %s (%d)\n%p On peer: %u (%p)\n%p With policy file: %s\n%p After: %s\n",
518 peer, peer->search_str, peer->search_str_matched,
519 peer, peer->id, peer,
520 peer, peer->policy_file,
522 GNUNET_STRINGS_relative_time_to_string (prof_time,
524 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
525 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
528 if (NULL != peers[peer_cnt].op_handle)
529 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
530 if (NULL != peers[peer_cnt].stats_op_handle)
531 GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
534 if (NULL != data_file)
535 GNUNET_DISK_file_close (data_file);
537 for (search_str_cnt = 0;
538 search_str_cnt < num_search_strings && NULL != search_strings;
541 GNUNET_free_non_null (search_strings[search_str_cnt]);
543 GNUNET_free_non_null (search_strings);
545 if (NULL != reg_handle)
546 GNUNET_TESTBED_cancel_registration (reg_handle);
547 if (NULL != topology_op)
548 GNUNET_TESTBED_operation_done (topology_op);
549 for (nhost = 0; nhost < num_hosts; nhost++)
550 if (NULL != hosts[nhost])
551 GNUNET_TESTBED_host_destroy (hosts[nhost]);
552 GNUNET_free_non_null (hosts);
554 while (NULL != (dll_op = dll_op_head))
556 GNUNET_TESTBED_operation_done (dll_op->op);
557 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
558 GNUNET_free (dll_op);
561 GNUNET_TESTBED_controller_disconnect (mc);
563 GNUNET_TESTBED_controller_stop (mc_proc);
565 GNUNET_CONFIGURATION_destroy (cfg);
567 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
572 * abort task to run on test timed out
575 * @param tc the task context
578 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
580 unsigned long i = (unsigned long) cls;
582 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting %lu...\n", i);
583 abort_task = GNUNET_SCHEDULER_NO_TASK;
584 result = GNUNET_SYSERR;
585 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
586 GNUNET_SCHEDULER_cancel (shutdown_task);
587 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
591 /******************************************************************************/
592 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
593 /******************************************************************************/
596 * Adapter function called to establish a connection to
597 * statistics service.
600 * @param cfg configuration of the peer to connect to; will be available until
601 * GNUNET_TESTBED_operation_done() is called on the operation returned
602 * from GNUNET_TESTBED_service_connect()
603 * @return service handle to return in 'op_result', NULL on error
606 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
608 return GNUNET_STATISTICS_create ("<driver>", cfg);
613 * Adapter function called to destroy a connection to
614 * statistics service.
617 * @param op_result service handle returned from the connect adapter
620 stats_da (void *cls, void *op_result)
622 struct RegexPeer *peer = cls;
624 GNUNET_assert (op_result == peer->stats_handle);
626 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
627 peer->stats_handle = NULL;
632 * Process statistic values. Write all values to global 'data_file', if present.
635 * @param subsystem name of subsystem that created the statistic
636 * @param name the name of the datum
637 * @param value the current value
638 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
639 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
642 stats_iterator (void *cls, const char *subsystem, const char *name,
643 uint64_t value, int is_persistent)
645 struct RegexPeer *peer = cls;
646 char output_buffer[512];
649 if (NULL == data_file)
651 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
652 "%p -> %s [%s]: %llu\n",
653 peer, subsystem, name, value);
657 GNUNET_snprintf (output_buffer,
658 sizeof (output_buffer),
661 subsystem, value, name);
662 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
663 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
670 * Stats callback. Finish the stats testbed operation and when all stats have
671 * been iterated, shutdown the profiler.
674 * @param success GNUNET_OK if statistics were
675 * successfully obtained, GNUNET_SYSERR if not.
681 static unsigned int peer_cnt;
682 struct RegexPeer *peer = cls;
684 if (GNUNET_OK != success)
686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
687 "Getting statistics for peer %u failed!\n",
692 GNUNET_assert (NULL != peer->stats_op_handle);
694 GNUNET_TESTBED_operation_done (peer->stats_op_handle);
695 peer->stats_op_handle = NULL;
698 peer = &peers[peer_cnt];
700 if (peer_cnt == num_peers)
702 struct GNUNET_TIME_Relative delay = { 100 };
703 shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
707 peer->stats_op_handle =
708 GNUNET_TESTBED_service_connect (NULL,
721 * Function called by testbed once we are connected to stats
722 * service. Get the statistics for the services of interest.
724 * @param cls the 'struct RegexPeer' for which we connected to stats
725 * @param op connect operation handle
726 * @param ca_result handle to stats service
727 * @param emsg error message on failure
730 stats_connect_cb (void *cls,
731 struct GNUNET_TESTBED_Operation *op,
735 struct RegexPeer *peer = cls;
737 if (NULL == ca_result || NULL != emsg)
739 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
740 "Failed to connect to statistics service on peer %u: %s\n",
743 peer->stats_handle = NULL;
747 peer->stats_handle = ca_result;
749 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
750 GNUNET_TIME_UNIT_FOREVER_REL,
752 &stats_iterator, peer))
754 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
755 "Could not get statistics of peer %u!\n", peer->id);
761 * Task to collect all statistics from all peers, will shutdown the
762 * profiler, when done.
765 * @param tc the task context
768 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
770 struct RegexPeer *peer = &peers[0];
772 GNUNET_assert (NULL != peer->peer_handle);
774 peer->stats_op_handle =
775 GNUNET_TESTBED_service_connect (NULL,
786 /******************************************************************************/
787 /************************ MESH SERVICE CONNECTIONS **************************/
788 /******************************************************************************/
791 * Method called when we've found a peer that announced a regex
792 * that matches our search string. Now get the statistics.
794 * @param cls Closure provided in GNUNET_REGEX_search.
795 * @param id Peer providing a regex that matches the string.
796 * @param get_path Path of the get request.
797 * @param get_path_length Lenght of get_path.
798 * @param put_path Path of the put request.
799 * @param put_path_length Length of the put_path.
802 regex_found_handler (void *cls,
803 const struct GNUNET_PeerIdentity *id,
804 const struct GNUNET_PeerIdentity *get_path,
805 unsigned int get_path_length,
806 const struct GNUNET_PeerIdentity *put_path,
807 unsigned int put_path_length)
809 struct RegexPeer *peer = cls;
810 char output_buffer[512];
813 if (GNUNET_YES == peer->search_str_matched)
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "String %s on peer %u already matched!\n",
817 peer->search_str, peer->id);
825 // FIXME not possible right now
826 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
827 "String matching timed out for string %s on peer %u (%i/%i)\n",
828 peer->search_str, peer->id, peers_found, num_search_strings);
830 printf ("String matching timed out for string %s on peer %u (%i/%i)\n",
831 peer->search_str, peer->id, peers_found, num_search_strings);
833 peer->search_str_matched = GNUNET_SYSERR;
837 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
839 "String %s successfully matched on peer %u after %s (%i/%i)\n",
840 peer->search_str, peer->id, GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
841 peers_found, num_search_strings);
843 printf ("String %s successfully matched on peer %u after %s (%i/%i)\n",
844 peer->search_str, peer->id, GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
845 peers_found, num_search_strings);
848 peer->search_str_matched = GNUNET_YES;
850 if (NULL != data_file)
853 GNUNET_snprintf (output_buffer,
854 sizeof (output_buffer),
855 "%p Peer: %u\n%p Host: %s\n%p Policy file: %s\n"
856 "%p Search string: %s\n%p Search duration: %s\n\n",
859 GNUNET_TESTBED_host_get_hostname (peer->host_handle),
860 peer, peer->policy_file,
861 peer, peer->search_str,
863 GNUNET_STRINGS_relative_time_to_string (prof_time,
866 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
867 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
871 GNUNET_TESTBED_operation_done (peer->op_handle);
872 peer->op_handle = NULL;
874 if (peers_found == num_search_strings)
876 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
877 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
878 "All strings successfully matched in %s\n",
879 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
880 printf ("All strings successfully matched.\n");
883 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
884 GNUNET_SCHEDULER_cancel (search_timeout_task);
886 printf ("Collecting stats and shutting down.\n");
887 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
893 * Connect by string timeout task. This will cancel the profiler after the
894 * specified timeout 'search_timeout'.
897 * @param tc the task context
900 do_connect_by_string_timeout (void *cls,
901 const struct GNUNET_SCHEDULER_TaskContext * tc)
903 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
904 "Finding matches to all strings did not succeed after %s.\n",
905 GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
906 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
907 "Found %i of %i strings\n", peers_found, num_search_strings);
909 printf ("Search timed out after %s. Collecting stats and shutting down.\n",
910 GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
913 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
918 * Connect by string task that is run to search for a string in the
919 * NFA. It first connects to the mesh service and when a connection is
920 * established it starts to search for the string.
923 * @param tc the task context
926 do_connect_by_string (void *cls,
927 const struct GNUNET_SCHEDULER_TaskContext * tc)
929 printf ("Starting string search.\n");
932 peers[0].search_str = search_strings[0];
933 peers[0].search_str_matched = GNUNET_NO;
935 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
936 "Searching for string \"%s\" on peer %d with file %s\n",
937 peers[0].search_str, 0, peers[0].policy_file);
939 /* First connect to mesh service, then search for string. Next
940 connect will be in mesh_connect_cb */
942 GNUNET_TESTBED_service_connect (NULL,
943 peers[0].peer_handle,
951 search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout,
952 &do_connect_by_string_timeout, NULL);
956 * Start searching for the next string in the DHT.
958 * @param cls Index of the next peer in the peers array.
959 * @param tc TaskContext.
962 find_next_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
964 long next_p = (long) cls;
966 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
969 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
970 "Searching for string \"%s\" on peer %d with file %s\n",
971 peers[next_p].search_str, next_p, peers[next_p].policy_file);
974 * dont connect to a new dht for each peer, we might want to seach for n
975 * strings on m peers where n > m
977 peers[next_p].op_handle =
978 GNUNET_TESTBED_service_connect (NULL,
979 peers[next_p].peer_handle,
990 * ARM connect adapter. Opens a connection to the ARM service.
992 * @param cls Closure (peer).
993 * @param cfg Configuration handle.
998 arm_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1000 struct RegexPeer *peer = cls;
1002 peer->arm_handle = GNUNET_ARM_connect (cfg, NULL);
1004 return peer->arm_handle;
1009 * Adapter function called to destroy a connection to the ARM service.
1011 * @param cls Closure (peer).
1012 * @param op_result Service handle returned from the connect adapter.
1015 arm_da (void *cls, void *op_result)
1017 struct RegexPeer *peer = (struct RegexPeer *) cls;
1019 GNUNET_assert (peer->arm_handle == op_result);
1021 if (NULL != peer->arm_handle)
1023 GNUNET_ARM_disconnect (peer->arm_handle);
1024 peer->arm_handle = NULL;
1030 * Start announcing the next regex in the DHT.
1032 * @param cls Index of the next peer in the peers array.
1033 * @param tc TaskContext.
1036 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1040 * Callback function invoked when ARM peration is complete: deamon is started.
1042 * @param cls Closure (RegexPeer).
1043 * @param result Outcome of the operation.
1046 arm_start_cb (void *cls, enum GNUNET_ARM_ProcessStatus result)
1048 struct RegexPeer *peer = (struct RegexPeer *) cls;
1049 static unsigned int peer_cnt;
1050 unsigned int next_p;
1055 * Service is currently being started (due to client request).
1057 case GNUNET_ARM_PROCESS_STARTING:
1058 GNUNET_TESTBED_operation_done (peer->op_handle);
1059 peer->op_handle = NULL;
1061 if (peer_cnt < (num_peers - 1))
1063 next_p = (++peer_cnt % num_peers);
1064 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1065 &announce_next_regex,
1066 (void *) (long) next_p);
1070 printf ("All daemons started. Waiting %s to start string searches\n",
1071 GNUNET_STRINGS_relative_time_to_string (search_delay,
1074 GNUNET_SCHEDULER_add_delayed (search_delay,
1075 do_connect_by_string,
1081 * Service name is unknown to ARM.
1083 case GNUNET_ARM_PROCESS_UNKNOWN:
1085 * Service is now down (due to client request).
1087 case GNUNET_ARM_PROCESS_DOWN:
1089 * Service is already running.
1091 case GNUNET_ARM_PROCESS_ALREADY_RUNNING:
1093 * Service is already being stopped by some other client.
1095 case GNUNET_ARM_PROCESS_ALREADY_STOPPING:
1097 * Service is already down (no action taken)
1099 case GNUNET_ARM_PROCESS_ALREADY_DOWN:
1101 * ARM is currently being shut down (no more process starts)
1103 case GNUNET_ARM_PROCESS_SHUTDOWN:
1105 * Error in communication with ARM
1107 case GNUNET_ARM_PROCESS_COMMUNICATION_ERROR:
1109 * Timeout in communication with ARM
1111 case GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT:
1113 * Failure to perform operation
1115 case GNUNET_ARM_PROCESS_FAILURE:
1117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ARM returned %d\n", result);
1124 * ARM connect callback. Called when we are connected to the arm service for
1125 * the peer in 'cls'. If successfull we start the regex deamon to start
1126 * announcing the regex of this peer.
1128 * @param cls internal peer id.
1129 * @param op operation handle.
1130 * @param ca_result connect adapter result.
1131 * @param emsg error message.
1134 arm_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1135 void *ca_result, const char *emsg)
1137 struct RegexPeer *peer = (struct RegexPeer *) cls;
1139 if (NULL != emsg || NULL == op || NULL == ca_result)
1141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ARM connect failed: %s\n", emsg);
1145 GNUNET_assert (NULL != peer->arm_handle);
1146 GNUNET_assert (peer->op_handle == op);
1147 GNUNET_assert (peer->arm_handle == ca_result);
1149 GNUNET_ARM_start_service (ca_result, "regexprofiler",
1150 GNUNET_OS_INHERIT_STD_NONE,
1151 GNUNET_TIME_UNIT_FOREVER_REL,
1158 * Task to start the daemons on each peer so that the regexes are announced
1162 * @param tc the task context
1165 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1167 printf ("Starting announce.\n");
1170 /* First connect to arm service, then announce. Next
1171 announce will be in arm_connect_cb */
1172 peers[0].op_handle =
1173 GNUNET_TESTBED_service_connect (NULL,
1174 peers[0].peer_handle,
1186 * Start announcing the next regex in the DHT.
1188 * @param cls Index of the next peer in the peers array.
1189 * @param tc TaskContext.
1192 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1194 long next_p = (long) cls;
1196 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1199 printf ("Starting daemon %ld\n", next_p);
1202 peers[next_p].op_handle =
1203 GNUNET_TESTBED_service_connect (NULL,
1204 peers[next_p].peer_handle,
1215 * DHT connect callback. Called when we are connected to the dht service for
1216 * the peer in 'cls'. If successfull we connect to the stats service of this
1217 * peer and then try to match the search string of this peer.
1219 * @param cls internal peer id.
1220 * @param op operation handle.
1221 * @param ca_result connect adapter result.
1222 * @param emsg error message.
1225 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1226 void *ca_result, const char *emsg)
1228 struct RegexPeer *peer = (struct RegexPeer *) cls;
1229 static unsigned int peer_cnt;
1230 unsigned int next_p;
1232 if (NULL != emsg || NULL == op || NULL == ca_result)
1234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1238 GNUNET_assert (NULL != peer->dht_handle);
1239 GNUNET_assert (peer->op_handle == op);
1240 GNUNET_assert (peer->dht_handle == ca_result);
1242 peer->search_str_matched = GNUNET_NO;
1243 peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
1245 ®ex_found_handler, peer,
1247 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1249 if (peer_cnt < (num_search_strings - 1))
1251 if (GNUNET_YES == no_distributed_search)
1254 next_p = (++peer_cnt % num_peers);
1256 peers[next_p].search_str = search_strings[next_p];
1257 peers[next_p].search_str_matched = GNUNET_NO;
1259 /* Don't start all searches at once */
1260 /* TODO add some intelligence to the timeout */
1261 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1263 (void *) (long) next_p);
1269 * DHT connect adapter. Opens a connection to the dht service.
1271 * @param cls Closure (peer).
1272 * @param cfg Configuration handle.
1277 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1279 struct RegexPeer *peer = cls;
1281 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1283 return peer->dht_handle;
1288 * Adapter function called to destroy a connection to the dht service.
1290 * @param cls Closure (peer).
1291 * @param op_result Service handle returned from the connect adapter.
1294 dht_da (void *cls, void *op_result)
1296 struct RegexPeer *peer = (struct RegexPeer *) cls;
1298 GNUNET_assert (peer->dht_handle == op_result);
1300 if (NULL != peer->search_handle)
1302 GNUNET_REGEX_search_cancel (peer->search_handle);
1303 peer->search_handle = NULL;
1306 if (NULL != peer->dht_handle)
1308 GNUNET_DHT_disconnect (peer->dht_handle);
1309 peer->dht_handle = NULL;
1314 /******************************************************************************/
1315 /*************************** TESTBED PEER SETUP *****************************/
1316 /******************************************************************************/
1320 * Configure the peer overlay topology.
1323 * @param tc the task context
1326 do_configure_topology (void *cls,
1327 const struct GNUNET_SCHEDULER_TaskContext * tc)
1330 if (0 == linking_factor)
1332 num_links = linking_factor * num_peers;
1334 /* num_links = num_peers - 1; */
1335 num_links = linking_factor;
1337 /* Do overlay connect */
1338 prof_start_time = GNUNET_TIME_absolute_get ();
1340 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
1344 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
1346 GNUNET_TESTBED_TOPOLOGY_RETRY_CNT,
1348 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
1349 if (NULL == topology_op)
1351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1352 "Cannot create topology, op handle was NULL\n");
1359 * Functions of this signature are called when a peer has been successfully
1360 * started or stopped.
1362 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
1363 * @param emsg NULL on success; otherwise an error description
1366 peer_churn_cb (void *cls, const char *emsg)
1368 struct DLLOperation *dll_op = cls;
1369 struct GNUNET_TESTBED_Operation *op;
1370 static unsigned int started_peers;
1371 unsigned int peer_cnt;
1374 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1375 GNUNET_free (dll_op);
1378 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1379 _("An operation has failed while starting peers: %s\n"), emsg);
1380 GNUNET_TESTBED_operation_done (op);
1381 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1382 GNUNET_SCHEDULER_cancel (abort_task);
1383 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1386 GNUNET_TESTBED_operation_done (op);
1387 if (++started_peers == num_peers)
1389 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1391 "All peers started successfully in %s\n",
1392 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1395 peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
1396 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1397 peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
1399 state = STATE_PEERS_LINKING;
1400 GNUNET_SCHEDULER_add_now (&do_configure_topology, NULL);
1406 * Functions of this signature are called when a peer has been successfully
1409 * @param cls the closure from GNUNET_TESTBED_peer_create()
1410 * @param peer the handle for the created peer; NULL on any error during
1412 * @param emsg NULL if peer is not NULL; else MAY contain the error description
1415 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
1417 struct DLLOperation *dll_op = cls;
1418 struct RegexPeer *peer_ptr;
1419 static unsigned int created_peers;
1420 unsigned int peer_cnt;
1424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1425 _("Creating a peer failed. Error: %s\n"), emsg);
1426 GNUNET_TESTBED_operation_done (dll_op->op);
1427 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1428 GNUNET_free (dll_op);
1429 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1430 GNUNET_SCHEDULER_cancel (abort_task);
1431 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1435 peer_ptr = dll_op->cls;
1436 GNUNET_assert (NULL == peer_ptr->peer_handle);
1437 GNUNET_CONFIGURATION_destroy (peer_ptr->cfg);
1438 peer_ptr->cfg = NULL;
1439 peer_ptr->peer_handle = peer;
1440 GNUNET_TESTBED_operation_done (dll_op->op);
1441 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1442 GNUNET_free (dll_op);
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
1446 GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
1448 if (++created_peers == num_peers)
1450 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1452 "All peers created successfully in %s\n",
1453 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1454 /* Now peers are to be started */
1455 state = STATE_PEERS_STARTING;
1456 prof_start_time = GNUNET_TIME_absolute_get ();
1457 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1459 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1460 dll_op->op = GNUNET_TESTBED_peer_start (dll_op,
1461 peers[peer_cnt].peer_handle,
1462 &peer_churn_cb, dll_op);
1463 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1470 * Function called with a filename for each file in the policy directory. Create
1471 * a peer for each filename and update the peer's configuration to include the
1472 * max_path_compression specified as a command line argument as well as the
1473 * policy_file for this peer. The gnunet-service-regexprofiler service is
1474 * automatically started on this peer. The service reads the configurration and
1475 * announces the regexes stored in the policy file 'filename'.
1477 * @param cls closure
1478 * @param filename complete filename (absolute path)
1479 * @return GNUNET_OK to continue to iterate,
1480 * GNUNET_SYSERR to abort iteration with error!
1483 policy_filename_cb (void *cls, const char *filename)
1485 static unsigned int peer_cnt;
1486 struct DLLOperation *dll_op;
1487 struct RegexPeer *peer = &peers[peer_cnt];
1489 GNUNET_assert (NULL != peer);
1491 peer->policy_file = GNUNET_strdup (filename);
1493 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Creating peer %i on host %s for policy file %s\n",
1495 GNUNET_TESTBED_host_get_hostname (peer->host_handle),
1498 /* Set configuration options specific for this peer
1499 (max_path_compression and policy_file */
1500 peer->cfg = GNUNET_CONFIGURATION_dup (cfg);
1501 GNUNET_CONFIGURATION_set_value_number (peer->cfg, "REGEXPROFILER",
1502 "MAX_PATH_COMPRESSION",
1503 (unsigned long long)max_path_compression);
1504 GNUNET_CONFIGURATION_set_value_string (peer->cfg, "REGEXPROFILER",
1505 "POLICY_FILE", filename);
1507 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1508 dll_op->cls = &peers[peer_cnt];
1509 dll_op->op = GNUNET_TESTBED_peer_create (mc,
1514 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1523 * Controller event callback.
1526 * @param event the controller event
1529 controller_event_cb (void *cls,
1530 const struct GNUNET_TESTBED_EventInformation *event)
1532 struct DLLOperation *dll_op;
1533 struct GNUNET_TESTBED_Operation *op;
1538 case STATE_SLAVES_STARTING:
1539 switch (event->type)
1541 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1543 static unsigned int slaves_started;
1544 unsigned int peer_cnt;
1546 dll_op = event->details.operation_finished.op_cls;
1547 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1548 GNUNET_free (dll_op);
1549 op = event->details.operation_finished.operation;
1550 if (NULL != event->details.operation_finished.emsg)
1552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1553 _("An operation has failed while starting slaves\n"));
1554 GNUNET_TESTBED_operation_done (op);
1555 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1556 GNUNET_SCHEDULER_cancel (abort_task);
1557 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1560 GNUNET_TESTBED_operation_done (op);
1561 /* Proceed to start peers */
1562 if (++slaves_started == num_hosts - 1)
1564 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1565 "All slaves started successfully\n");
1567 state = STATE_PEERS_CREATING;
1568 prof_start_time = GNUNET_TIME_absolute_get ();
1570 if (-1 == (ret = GNUNET_DISK_directory_scan (policy_dir,
1574 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1575 _("No files found in `%s'\n"),
1577 GNUNET_SCHEDULER_shutdown ();
1580 num_peers = (unsigned int) ret;
1581 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1583 /* Initialize peers */
1584 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1586 struct RegexPeer *peer = &peers[peer_cnt];
1587 peer->id = peer_cnt;
1588 peer->policy_file = NULL;
1589 /* Do not start peers on hosts[0] (master controller) */
1590 peer->host_handle = hosts[1 + (peer_cnt % (num_hosts -1))];
1591 peer->dht_handle = NULL;
1592 peer->search_handle = NULL;
1593 peer->stats_handle = NULL;
1594 peer->stats_op_handle = NULL;
1595 peer->search_str = NULL;
1596 peer->search_str_matched = GNUNET_NO;
1599 GNUNET_DISK_directory_scan (policy_dir,
1600 &policy_filename_cb,
1609 case STATE_PEERS_STARTING:
1610 switch (event->type)
1612 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1613 /* Control reaches here when peer start fails */
1614 case GNUNET_TESTBED_ET_PEER_START:
1615 /* we handle peer starts in peer_churn_cb */
1621 case STATE_PEERS_LINKING:
1622 switch (event->type)
1624 static unsigned int established_links;
1625 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1626 /* Control reaches here when a peer linking operation fails */
1627 if (NULL != event->details.operation_finished.emsg)
1629 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1630 _("An operation has failed while linking\n"));
1631 printf ("F%u ", retry_links);
1635 /* We do no retries, consider this link as established */
1637 case GNUNET_TESTBED_ET_CONNECT:
1639 char output_buffer[512];
1642 if (0 == established_links)
1643 printf ("Establishing links .");
1649 if (++established_links == num_links)
1652 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1653 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1654 "%u links established in %s\n",
1656 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1658 GNUNET_free (peer_handles);
1660 if (NULL != data_file)
1663 GNUNET_snprintf (output_buffer,
1664 sizeof (output_buffer),
1665 "# of peers: %u\n# of links established: %u\n"
1666 "Time to establish links: %s\nLinking failures: %u\n"
1667 "path compression length: %u\n# of search strings: %u\n",
1669 (established_links - cont_fails),
1670 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
1672 max_path_compression,
1673 num_search_strings);
1675 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
1676 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
1679 printf ("\nWaiting %s before starting to announce.\n",
1680 GNUNET_STRINGS_relative_time_to_string (search_delay, GNUNET_NO));
1683 state = STATE_SEARCH_REGEX;
1685 search_task = GNUNET_SCHEDULER_add_delayed (search_delay,
1686 &do_announce, NULL);
1694 case STATE_SEARCH_REGEX:
1696 /* Handled in service connect callback */
1702 case STATE_PEERS_CREATING:
1703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create peer\n");
1706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1707 "Unexpected controller_cb with state %i!\n", state);
1715 * Task to register all hosts available in the global host list.
1718 * @param tc the scheduler task context
1721 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1725 * Callback which will be called to after a host registration succeeded or failed
1727 * @param cls the closure
1728 * @param emsg the error message; NULL if host registration is successful
1731 host_registration_completion (void *cls, const char *emsg)
1736 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1737 _("Host registration failed for a host. Error: %s\n"), emsg);
1738 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1739 GNUNET_SCHEDULER_cancel (abort_task);
1740 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1743 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1748 * Task to register all hosts available in the global host list.
1751 * @param tc the scheduler task context
1754 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1756 struct DLLOperation *dll_op;
1757 static unsigned int reg_host;
1760 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1761 if (reg_host == num_hosts - 1)
1763 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1764 "All hosts successfully registered\n");
1766 state = STATE_SLAVES_STARTING;
1767 for (slave = 1; slave < num_hosts; slave++)
1769 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1770 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
1776 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1780 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
1781 host_registration_completion,
1787 * Callback to signal successfull startup of the controller process.
1789 * @param cls the closure from GNUNET_TESTBED_controller_start()
1790 * @param config the configuration with which the controller has been started;
1791 * NULL if status is not GNUNET_OK
1792 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1793 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1796 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
1798 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1799 GNUNET_SCHEDULER_cancel (abort_task);
1800 if (GNUNET_OK != status)
1803 printf("Oh, dear!\n");
1804 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1808 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1809 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1810 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1811 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1812 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1813 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
1814 &controller_event_cb, NULL);
1817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1818 _("Unable to connect to master controller -- Check config\n"));
1819 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1822 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1823 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1824 &do_abort, (void*) __LINE__);
1829 * Load search strings from given filename. One search string per line.
1831 * @param filename filename of the file containing the search strings.
1832 * @param strings set of strings loaded from file. Caller needs to free this
1833 * if number returned is greater than zero.
1834 * @param limit upper limit on the number of strings read from the file
1835 * @return number of strings found in the file. GNUNET_SYSERR on error.
1838 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1843 unsigned int offset;
1847 if (NULL == filename)
1849 return GNUNET_SYSERR;
1852 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1855 "Could not find search strings file %s\n", filename);
1856 return GNUNET_SYSERR;
1858 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1862 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1863 return GNUNET_SYSERR;
1865 data = GNUNET_malloc (filesize);
1866 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1869 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1871 return GNUNET_SYSERR;
1876 while (offset < (filesize - 1) && str_cnt < limit)
1879 if (((data[offset] == '\n')) && (buf != &data[offset]))
1881 data[offset] = '\0';
1883 buf = &data[offset + 1];
1885 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1886 buf = &data[offset + 1];
1888 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1890 for (i = 0; i < str_cnt; i++)
1892 GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1893 offset += strlen (&data[offset]) + 1;
1901 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
1902 * inform whether the given host is habitable or not. The Handle returned by
1903 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
1906 * @param host the host whose status is being reported; will be NULL if the host
1907 * given to GNUNET_TESTBED_is_host_habitable() is NULL
1908 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
1911 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host, int status)
1913 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handle = cls;
1914 static unsigned int hosts_checked;
1917 if (GNUNET_NO == status)
1919 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1920 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1921 GNUNET_TESTBED_host_get_hostname (host));
1923 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Testbed cannot be started on localhost\n"));
1924 GNUNET_SCHEDULER_cancel (abort_task);
1925 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1929 /* printf (_("\rChecked %u hosts"), hosts_checked); */
1930 /* fflush (stdout); */
1931 if (hosts_checked < num_hosts)
1933 /* printf (_("\nAll hosts can start testbed. Creating peers\n")); */
1934 GNUNET_free (hc_handles);
1937 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
1947 * Main function that will be run by the scheduler.
1949 * @param cls closure
1950 * @param args remaining command-line arguments
1951 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1952 * @param config configuration
1955 run (void *cls, char *const *args, const char *cfgfile,
1956 const struct GNUNET_CONFIGURATION_Handle *config)
1959 unsigned int nsearchstrs;
1961 if (NULL == args[0])
1963 fprintf (stderr, _("No hosts-file specified on command line. Exiting.\n"));
1966 if (NULL == args[1])
1968 fprintf (stderr, _("No policy directory specified on command line. Exiting.\n"));
1971 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], &hosts);
1974 fprintf (stderr, _("No hosts loaded. Need at least one host\n"));
1977 printf (_("Checking whether given hosts can start testbed. Please wait\n"));
1978 hc_handles = GNUNET_malloc (sizeof (struct
1979 GNUNET_TESTBED_HostHabitableCheckHandle *)
1981 for (nhost = 0; nhost < num_hosts; nhost++)
1983 hc_handles[nhost] = GNUNET_TESTBED_is_host_habitable (hosts[nhost], config,
1985 &hc_handles[nhost]);
1986 if (NULL == hc_handles[nhost])
1991 for (i = 0; i <= nhost; i++)
1992 if (NULL != hc_handles[i])
1993 GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[i]);
1994 GNUNET_free (hc_handles);
1999 if (num_hosts != nhost)
2001 fprintf (stderr, _("Exiting\n"));
2002 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2007 fprintf (stderr, _("No configuration file given. Exiting\n"));
2008 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2013 GNUNET_CONFIGURATION_get_value_string (config, "REGEXPROFILER", "REGEX_PREFIX",
2016 fprintf (stderr, _("Configuration option (regex_prefix) missing. Exiting\n"));
2017 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2021 if ( (NULL != data_filename) &&
2022 (NULL == (data_file =
2023 GNUNET_DISK_file_open (data_filename,
2024 GNUNET_DISK_OPEN_READWRITE |
2025 GNUNET_DISK_OPEN_TRUNCATE |
2026 GNUNET_DISK_OPEN_CREATE,
2027 GNUNET_DISK_PERM_USER_READ |
2028 GNUNET_DISK_PERM_USER_WRITE))) )
2029 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2032 if (GNUNET_YES != GNUNET_DISK_directory_test (args[1], GNUNET_YES))
2034 fprintf (stderr, _("Specified policies directory does not exist. Exiting.\n"));
2035 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2038 policy_dir = args[1];
2039 if (GNUNET_YES != GNUNET_DISK_file_test (args[2]))
2041 fprintf (stderr, _("No search strings file given. Exiting.\n"));
2042 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2045 nsearchstrs = load_search_strings (args[2], &search_strings, num_search_strings);
2046 if (num_search_strings != nsearchstrs)
2048 num_search_strings = nsearchstrs;
2049 fprintf (stderr, _("Error loading search strings. Given file does not contain enough strings. Exiting.\n"));
2050 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2053 if (0 >= num_search_strings || NULL == search_strings)
2055 fprintf (stderr, _("Error loading search strings. Exiting.\n"));
2056 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2060 for (i = 0; i < num_search_strings; i++)
2061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "search string: %s\n", search_strings[i]);
2062 cfg = GNUNET_CONFIGURATION_dup (config);
2064 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2065 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
2073 * @param argc argument count
2074 * @param argv argument values
2075 * @return 0 on success
2078 main (int argc, char *const *argv)
2080 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2081 {'d', "details", "FILENAME",
2082 gettext_noop ("name of the file for writing statistics"),
2083 1, &GNUNET_GETOPT_set_string, &data_filename},
2084 {'n', "num-links", "COUNT",
2085 gettext_noop ("create COUNT number of random links between peers"),
2086 GNUNET_YES, &GNUNET_GETOPT_set_uint, &linking_factor },
2087 {'t', "matching-timeout", "TIMEOUT",
2088 gettext_noop ("wait TIMEOUT before considering a string match as failed"),
2089 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout },
2090 {'s', "search-delay", "DELAY",
2091 gettext_noop ("wait DELAY before starting string search"),
2092 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
2093 {'a', "num-search-strings", "COUNT",
2094 gettext_noop ("number of search strings to read from search strings file"),
2095 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
2096 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
2097 gettext_noop ("maximum path compression length"),
2098 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
2099 {'i', "no-distributed-search", "",
2100 gettext_noop ("if this option is set, only one peer is responsible for searching all strings"),
2101 0, &GNUNET_GETOPT_set_one, &no_distributed_search},
2102 GNUNET_GETOPT_OPTION_END
2106 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2109 result = GNUNET_SYSERR;
2111 GNUNET_PROGRAM_run (argc, argv,
2112 "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
2113 _("Profiler for regex"),
2114 options, &run, NULL);
2116 if (GNUNET_OK != ret)
2118 if (GNUNET_OK != result)