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"
39 #define FIND_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
40 #define SEARCHES_IN_PARALLEL 100
41 #define ANNOUNCE_DELAY GNUNET_TIME_relative_multiply(\
42 GNUNET_TIME_UNIT_MILLISECONDS,\
44 #define SEARCH_DELAY GNUNET_TIME_relative_multiply(\
45 GNUNET_TIME_UNIT_MILLISECONDS,\
54 * The testbed operation handle
56 struct GNUNET_TESTBED_Operation *op;
64 * The next pointer for DLL
66 struct DLLOperation *next;
69 * The prev pointer for DLL
71 struct DLLOperation *prev;
76 * Available states during profiling
88 STATE_SLAVES_STARTING,
106 * Matching strings against announced regexes
111 * Destroying peers; we can do this as the controller takes care of stopping a
112 * peer if it is running
114 STATE_PEERS_DESTROYING
129 * Peer configuration handle.
131 struct GNUNET_CONFIGURATION_Handle *cfg;
134 * The actual testbed peer handle.
136 struct GNUNET_TESTBED_Peer *peer_handle;
139 * Host on which the peer is running.
141 struct GNUNET_TESTBED_Host *host_handle;
144 * Filename of the peer's policy file.
149 * Peers search string.
151 const char *search_str;
154 * Set to GNUNET_YES if the peer successfully matched the above
155 * search string. GNUNET_NO if the string could not be matched
156 * during the profiler run. GNUNET_SYSERR if the string matching
157 * timed out. Undefined if search_str is NULL
159 int search_str_matched;
164 struct GNUNET_ARM_Handle *arm_handle;
169 struct GNUNET_DHT_Handle *dht_handle;
172 * Handle to a running regex search.
174 struct GNUNET_REGEX_search_handle *search_handle;
177 * Testbed operation handle for the ARM and DHT services.
179 struct GNUNET_TESTBED_Operation *op_handle;
182 * Peers's statistics handle.
184 struct GNUNET_STATISTICS_Handle *stats_handle;
187 * Testbed operation handle for the statistics service.
189 struct GNUNET_TESTBED_Operation *stats_op_handle;
192 * The starting time of a profiling step.
194 struct GNUNET_TIME_Absolute prof_start_time;
199 GNUNET_SCHEDULER_TaskIdentifier timeout;
204 * An array of hosts loaded from the hostkeys file
206 static struct GNUNET_TESTBED_Host **hosts;
209 * Array of peer handles used to pass to
210 * GNUNET_TESTBED_overlay_configure_topology
212 static struct GNUNET_TESTBED_Peer **peer_handles;
215 * The array of peers; we fill this as the peers are given to us by the testbed
217 static struct RegexPeer *peers;
220 * Host registration handle
222 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
225 * Handle to the master controller process
227 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
230 * Handle to the master controller
232 static struct GNUNET_TESTBED_Controller *mc;
235 * Handle to global configuration
237 static struct GNUNET_CONFIGURATION_Handle *cfg;
240 * Head of the operations list
242 static struct DLLOperation *dll_op_head;
245 * Tail of the operations list
247 static struct DLLOperation *dll_op_tail;
250 * Peer linking - topology operation
252 static struct GNUNET_TESTBED_Operation *topology_op;
255 * The handle for whether a host is habitable or not
257 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
260 * Abort task identifier
262 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
265 * Shutdown task identifier
267 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
270 * Host registration task identifier
272 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
275 * Global event mask for all testbed events
277 static uint64_t event_mask;
280 * The starting time of a profiling step
282 static struct GNUNET_TIME_Absolute prof_start_time;
285 * Duration profiling step has taken
287 static struct GNUNET_TIME_Relative prof_time;
290 * Number of peers to be started by the profiler
292 static unsigned int num_peers;
295 * Number of hosts in the hosts array
297 static unsigned int num_hosts;
300 * Factor of number of links. num_links = num_peers * linking_factor.
302 static unsigned int linking_factor;
305 * Number of random links to be established between peers
307 static unsigned int num_links;
310 * Number of connect operations that have failed, candidates to retry
312 static unsigned int retry_links;
315 * Global testing status
320 * current state of profiling
325 * Folder where policy files are stored.
327 static char * policy_dir;
332 static char **search_strings;
335 * Number of search strings.
337 static int num_search_strings;
340 * How many searches are running in parallel
342 static unsigned int parallel_searches;
345 * Index of peer/string search.
347 static unsigned int peer_cnt;
350 * Number of peers found with search strings.
352 static unsigned int peers_found;
355 * Index of peer to start next announce/search..
357 static unsigned int search_index;
360 * Search task identifier
362 static GNUNET_SCHEDULER_TaskIdentifier search_task;
365 * Search timeout task identifier.
367 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
370 * Search timeout in seconds.
372 static struct GNUNET_TIME_Relative search_timeout_time = { 60000 };
375 * How long do we wait before starting the search?
378 static struct GNUNET_TIME_Relative search_delay = { 60000 };
381 * File to log statistics to.
383 static struct GNUNET_DISK_FileHandle *data_file;
386 * Filename to log statistics to.
388 static char *data_filename;
391 * Maximal path compression length.
393 static unsigned int max_path_compression;
396 * Prefix used for regex announcing. We need to prefix the search
397 * strings with it, in order to find something.
399 static char * regex_prefix;
402 /******************************************************************************/
403 /****************************** DECLARATIONS ********************************/
404 /******************************************************************************/
407 * DHT connect callback.
409 * @param cls internal peer id.
410 * @param op operation handle.
411 * @param ca_result connect adapter result.
412 * @param emsg error message.
415 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
416 void *ca_result, const char *emsg);
419 * DHT connect adapter.
421 * @param cls not used.
422 * @param cfg configuration handle.
427 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
431 * Adapter function called to destroy a connection to
435 * @param op_result service handle returned from the connect adapter
438 dht_da (void *cls, void *op_result);
442 * Function called by testbed once we are connected to stats
443 * service. Get the statistics for the services of interest.
445 * @param cls the 'struct RegexPeer' for which we connected to stats
446 * @param op connect operation handle
447 * @param ca_result handle to stats service
448 * @param emsg error message on failure
451 stats_connect_cb (void *cls,
452 struct GNUNET_TESTBED_Operation *op,
458 * Task to collect all statistics from s, will shutdown the
459 * profiler, when done.
462 * @param tc the task context
465 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
469 * Start announcing the next regex in the DHT.
471 * @param cls Index of the next peer in the peers array.
472 * @param tc TaskContext.
475 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
478 /******************************************************************************/
479 /******************************** SHUTDOWN **********************************/
480 /******************************************************************************/
487 * @param tc the task context
490 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
492 struct DLLOperation *dll_op;
493 struct RegexPeer *peer;
495 unsigned int peer_cnt;
496 unsigned int search_str_cnt;
497 char output_buffer[512];
500 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
501 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
502 GNUNET_SCHEDULER_cancel (abort_task);
503 if (NULL != hc_handles)
505 for (nhost = 0; nhost < num_hosts; nhost++)
506 if (NULL != hc_handles[nhost])
507 GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[nhost]);
508 GNUNET_free (hc_handles);
511 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
512 GNUNET_SCHEDULER_cancel (register_hosts_task);
514 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
516 peer = &peers[peer_cnt];
518 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
520 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
522 GNUNET_snprintf (output_buffer,
523 sizeof (output_buffer),
524 "%p Search string not found: %s (%d)\n%p On peer: %u (%p)\n%p With policy file: %s\n%p After: %s\n",
525 peer, peer->search_str, peer->search_str_matched,
526 peer, peer->id, peer,
527 peer, peer->policy_file,
529 GNUNET_STRINGS_relative_time_to_string (prof_time,
531 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
532 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
535 if (NULL != peers[peer_cnt].op_handle)
536 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
537 if (NULL != peers[peer_cnt].stats_op_handle)
538 GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
541 if (NULL != data_file)
542 GNUNET_DISK_file_close (data_file);
544 for (search_str_cnt = 0;
545 search_str_cnt < num_search_strings && NULL != search_strings;
548 GNUNET_free_non_null (search_strings[search_str_cnt]);
550 GNUNET_free_non_null (search_strings);
552 if (NULL != reg_handle)
553 GNUNET_TESTBED_cancel_registration (reg_handle);
554 if (NULL != topology_op)
555 GNUNET_TESTBED_operation_done (topology_op);
556 for (nhost = 0; nhost < num_hosts; nhost++)
557 if (NULL != hosts[nhost])
558 GNUNET_TESTBED_host_destroy (hosts[nhost]);
559 GNUNET_free_non_null (hosts);
561 while (NULL != (dll_op = dll_op_head))
563 GNUNET_TESTBED_operation_done (dll_op->op);
564 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
565 GNUNET_free (dll_op);
568 GNUNET_TESTBED_controller_disconnect (mc);
570 GNUNET_TESTBED_controller_stop (mc_proc);
572 GNUNET_CONFIGURATION_destroy (cfg);
574 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
579 * abort task to run on test timed out
582 * @param tc the task context
585 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
587 unsigned long i = (unsigned long) cls;
589 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting %lu...\n", i);
590 abort_task = GNUNET_SCHEDULER_NO_TASK;
591 result = GNUNET_SYSERR;
592 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
593 GNUNET_SCHEDULER_cancel (shutdown_task);
594 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
598 /******************************************************************************/
599 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
600 /******************************************************************************/
603 * Adapter function called to establish a connection to
604 * statistics service.
607 * @param cfg configuration of the peer to connect to; will be available until
608 * GNUNET_TESTBED_operation_done() is called on the operation returned
609 * from GNUNET_TESTBED_service_connect()
610 * @return service handle to return in 'op_result', NULL on error
613 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
615 return GNUNET_STATISTICS_create ("<driver>", cfg);
620 * Adapter function called to destroy a connection to
621 * statistics service.
624 * @param op_result service handle returned from the connect adapter
627 stats_da (void *cls, void *op_result)
629 struct RegexPeer *peer = cls;
631 GNUNET_assert (op_result == peer->stats_handle);
633 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
634 peer->stats_handle = NULL;
639 * Process statistic values. Write all values to global 'data_file', if present.
642 * @param subsystem name of subsystem that created the statistic
643 * @param name the name of the datum
644 * @param value the current value
645 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
646 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
649 stats_iterator (void *cls, const char *subsystem, const char *name,
650 uint64_t value, int is_persistent)
652 struct RegexPeer *peer = cls;
653 char output_buffer[512];
656 if (NULL == data_file)
658 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
659 "%p -> %s [%s]: %llu\n",
660 peer, subsystem, name, value);
664 GNUNET_snprintf (output_buffer,
665 sizeof (output_buffer),
668 subsystem, value, name);
669 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
670 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
677 * Stats callback. Finish the stats testbed operation and when all stats have
678 * been iterated, shutdown the profiler.
681 * @param success GNUNET_OK if statistics were
682 * successfully obtained, GNUNET_SYSERR if not.
688 static unsigned int peer_cnt;
689 struct RegexPeer *peer = cls;
691 if (GNUNET_OK != success)
693 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
694 "Getting statistics for peer %u failed!\n",
699 GNUNET_assert (NULL != peer->stats_op_handle);
701 GNUNET_TESTBED_operation_done (peer->stats_op_handle);
702 peer->stats_op_handle = NULL;
705 peer = &peers[peer_cnt];
707 if (peer_cnt == num_peers)
709 struct GNUNET_TIME_Relative delay = { 100 };
710 shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
714 peer->stats_op_handle =
715 GNUNET_TESTBED_service_connect (NULL,
728 * Function called by testbed once we are connected to stats
729 * service. Get the statistics for the services of interest.
731 * @param cls the 'struct RegexPeer' for which we connected to stats
732 * @param op connect operation handle
733 * @param ca_result handle to stats service
734 * @param emsg error message on failure
737 stats_connect_cb (void *cls,
738 struct GNUNET_TESTBED_Operation *op,
742 struct RegexPeer *peer = cls;
744 if (NULL == ca_result || NULL != emsg)
746 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
747 "Failed to connect to statistics service on peer %u: %s\n",
750 peer->stats_handle = NULL;
754 peer->stats_handle = ca_result;
756 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
757 GNUNET_TIME_UNIT_FOREVER_REL,
759 &stats_iterator, peer))
761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
762 "Could not get statistics of peer %u!\n", peer->id);
768 * Task to collect all statistics from all peers, will shutdown the
769 * profiler, when done.
772 * @param tc the task context
775 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
777 struct RegexPeer *peer = &peers[0];
779 GNUNET_assert (NULL != peer->peer_handle);
781 peer->stats_op_handle =
782 GNUNET_TESTBED_service_connect (NULL,
793 /******************************************************************************/
794 /************************ REGEX FIND CONNECTIONS **************************/
795 /******************************************************************************/
799 * Start searching for the next string in the DHT.
801 * @param cls Index of the next peer in the peers array.
802 * @param tc TaskContext.
805 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
809 * Method called when we've found a peer that announced a regex
810 * that matches our search string. Now get the statistics.
812 * @param cls Closure provided in GNUNET_REGEX_search.
813 * @param id Peer providing a regex that matches the string.
814 * @param get_path Path of the get request.
815 * @param get_path_length Lenght of get_path.
816 * @param put_path Path of the put request.
817 * @param put_path_length Length of the put_path.
820 regex_found_handler (void *cls,
821 const struct GNUNET_PeerIdentity *id,
822 const struct GNUNET_PeerIdentity *get_path,
823 unsigned int get_path_length,
824 const struct GNUNET_PeerIdentity *put_path,
825 unsigned int put_path_length)
827 struct RegexPeer *peer = cls;
828 char output_buffer[512];
831 if (GNUNET_YES == peer->search_str_matched)
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
834 "String %s on peer %u already matched!\n",
835 peer->search_str, peer->id);
842 if (GNUNET_SCHEDULER_NO_TASK != peer->timeout)
844 GNUNET_SCHEDULER_cancel (peer->timeout);
845 peer->timeout = GNUNET_SCHEDULER_NO_TASK;
846 GNUNET_SCHEDULER_add_delayed (SEARCH_DELAY, &announce_next_regex, NULL);
851 // FIXME not possible right now
852 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
853 "String matching timed out for string %s on peer %u (%i/%i)\n",
854 peer->search_str, peer->id, peers_found, num_search_strings);
855 peer->search_str_matched = GNUNET_SYSERR;
859 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
861 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
862 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
863 peer->search_str, peer->id,
864 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
865 peers_found, num_search_strings, parallel_searches);
867 peer->search_str_matched = GNUNET_YES;
869 if (NULL != data_file)
872 GNUNET_snprintf (output_buffer,
873 sizeof (output_buffer),
874 "%p Peer: %u\n%p Host: %s\n%p Policy file: %s\n"
875 "%p Search string: %s\n%p Search duration: %s\n\n",
878 GNUNET_TESTBED_host_get_hostname (peer->host_handle),
879 peer, peer->policy_file,
880 peer, peer->search_str,
882 GNUNET_STRINGS_relative_time_to_string (prof_time,
885 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
886 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
890 GNUNET_TESTBED_operation_done (peer->op_handle);
891 peer->op_handle = NULL;
893 if (peers_found == num_search_strings)
895 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
896 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
897 "All strings successfully matched in %s\n",
898 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
900 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
901 GNUNET_SCHEDULER_cancel (search_timeout_task);
903 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
904 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
910 * Connect by string timeout task. This will cancel the profiler after the
911 * specified timeout 'search_timeout'.
914 * @param tc the task context
917 search_timeout (void *cls,
918 const struct GNUNET_SCHEDULER_TaskContext * tc)
920 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
921 "Finding matches to all strings did not succeed after %s.\n",
922 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
924 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
925 "Found %i of %i strings\n", peers_found, num_search_strings);
927 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
928 "Search timed out after %s."
929 "Collecting stats and shutting down.\n",
930 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
933 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
938 * Search timed out. It might still complete in the future,
939 * but we should start another one.
941 * @param cls Index of the next peer in the peers array.
942 * @param tc TaskContext.
945 find_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
947 struct RegexPeer *p = cls;
949 p->timeout = GNUNET_SCHEDULER_NO_TASK;
951 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
953 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
954 "Searching for string \"%s\" on peer %d timed out. Starting new search.\n",
957 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
962 * Start searching for a string in the DHT.
964 * @param cls Index of the next peer in the peers array.
965 * @param tc TaskContext.
968 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
970 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
971 peer_cnt >= num_search_strings)
975 peers[peer_cnt].search_str = search_strings[peer_cnt];
976 peers[peer_cnt].search_str_matched = GNUNET_NO;
977 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
978 "Searching for string \"%s\" on peer %d with file %s (%u||)\n",
979 peers[peer_cnt].search_str,
981 peers[peer_cnt].policy_file,
984 peers[peer_cnt].op_handle =
985 GNUNET_TESTBED_service_connect (NULL,
986 peers[peer_cnt].peer_handle,
993 peers[peer_cnt].timeout = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
1001 * ARM connect adapter. Opens a connection to the ARM service.
1003 * @param cls Closure (peer).
1004 * @param cfg Configuration handle.
1009 arm_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1011 struct RegexPeer *peer = cls;
1013 peer->arm_handle = GNUNET_ARM_alloc (cfg);
1014 GNUNET_ARM_connect (peer->arm_handle, NULL, NULL);
1016 return peer->arm_handle;
1021 * Adapter function called to destroy a connection to the ARM service.
1023 * @param cls Closure (peer).
1024 * @param op_result Service handle returned from the connect adapter.
1027 arm_da (void *cls, void *op_result)
1029 struct RegexPeer *peer = (struct RegexPeer *) cls;
1031 GNUNET_assert (peer->arm_handle == op_result);
1033 if (NULL != peer->arm_handle)
1035 GNUNET_ARM_disconnect (peer->arm_handle);
1036 peer->arm_handle = NULL;
1041 * Finish and free the operation used to start the regex daemon.
1042 * operation_done calls ARM_disconnect, which cannot happen inside an
1045 * @param cls Closure (Peer info)
1046 * @param tc TaskContext
1049 arm_op_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1051 struct RegexPeer *peer = (struct RegexPeer *) cls;
1053 GNUNET_TESTBED_operation_done (peer->op_handle);
1054 peer->op_handle = NULL;
1059 * Callback called when arm has started the daemon we asked for.
1061 * @param cls Closure ().
1062 * @param arm Arm handle.
1063 * @param rs Status of the request.
1064 * @param service Service we asked to start (deamon).
1065 * @param result Result of the request.
1068 arm_start_cb (void *cls, struct GNUNET_ARM_Handle *arm,
1069 enum GNUNET_ARM_RequestStatus rs, const char *service,
1070 enum GNUNET_ARM_Result result)
1072 struct RegexPeer *peer = (struct RegexPeer *) cls;
1074 if (rs != GNUNET_ARM_REQUEST_SENT_OK)
1076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ARM request was not sent: %u\n", rs);
1082 * Asked to start it, but it's already starting.
1084 case GNUNET_ARM_RESULT_IS_STARTING_ALREADY:
1085 GNUNET_break (0); /* Shouldn't be starting, however it's not fatal. */
1089 * Service is currently being started (due to client request).
1091 case GNUNET_ARM_RESULT_STARTING:
1092 GNUNET_SCHEDULER_add_now (&arm_op_done, peer);
1094 if (search_index < (num_peers - 1))
1099 /* Find a peer to look for a string matching the regex announced */
1100 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1102 for (i = 0; peers[search_peer].search_str != NULL; i++)
1104 search_peer = (search_peer + 1) % num_peers;
1106 GNUNET_abort (); /* we run out of peers, must be a bug */
1108 peers[search_peer].search_str = search_strings[search_index];
1109 GNUNET_SCHEDULER_add_delayed (SEARCH_DELAY,
1111 (void *) search_peer);
1115 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1116 "All daemons started."
1117 " Waiting %s to start string searches\n",
1118 GNUNET_STRINGS_relative_time_to_string (search_delay,
1120 /* FIXME start GLOBAL timeout to abort experiment */
1121 search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout_time,
1128 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ARM returned %d\n", result);
1134 * ARM connect callback. Called when we are connected to the arm service for
1135 * the peer in 'cls'. If successfull we start the regex deamon to start
1136 * announcing the regex of this peer.
1138 * @param cls internal peer id.
1139 * @param op operation handle.
1140 * @param ca_result connect adapter result.
1141 * @param emsg error message.
1144 arm_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1145 void *ca_result, const char *emsg)
1147 struct RegexPeer *peer = (struct RegexPeer *) cls;
1149 if (NULL != emsg || NULL == op || NULL == ca_result)
1151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ARM connect failed: %s\n", emsg);
1155 GNUNET_assert (NULL != peer->arm_handle);
1156 GNUNET_assert (peer->op_handle == op);
1157 GNUNET_assert (peer->arm_handle == ca_result);
1159 GNUNET_ARM_request_service_start (ca_result, "regexprofiler",
1160 GNUNET_OS_INHERIT_STD_NONE,
1161 GNUNET_TIME_UNIT_FOREVER_REL,
1167 * Task to start the daemons on each peer so that the regexes are announced
1171 * @param tc the task context
1174 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1176 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting announce.\n");
1178 for (search_index = 0; search_index < SEARCHES_IN_PARALLEL; search_index++)
1180 /* First connect to arm service, then announce. Next
1181 announce will be in arm_connect_cb */
1182 peers[search_index].op_handle =
1183 GNUNET_TESTBED_service_connect (NULL,
1184 peers[search_index].peer_handle,
1187 &peers[search_index],
1190 &peers[search_index]);
1191 parallel_searches++;
1197 * Start announcing the next regex in the DHT.
1199 * @param cls Closure (unused).
1200 * @param tc TaskContext.
1203 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1205 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1208 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", search_index);
1210 peers[search_index].op_handle =
1211 GNUNET_TESTBED_service_connect (NULL,
1212 peers[search_index].peer_handle,
1215 &peers[search_index],
1218 &peers[search_index]);
1222 * DHT connect callback. Called when we are connected to the dht service for
1223 * the peer in 'cls'. If successfull we connect to the stats service of this
1224 * peer and then try to match the search string of this peer.
1226 * @param cls internal peer id.
1227 * @param op operation handle.
1228 * @param ca_result connect adapter result.
1229 * @param emsg error message.
1232 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1233 void *ca_result, const char *emsg)
1235 struct RegexPeer *peer = (struct RegexPeer *) cls;
1237 if (NULL != emsg || NULL == op || NULL == ca_result)
1239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1243 GNUNET_assert (NULL != peer->dht_handle);
1244 GNUNET_assert (peer->op_handle == op);
1245 GNUNET_assert (peer->dht_handle == ca_result);
1247 peer->search_str_matched = GNUNET_NO;
1248 peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
1250 ®ex_found_handler, peer,
1252 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1257 * DHT connect adapter. Opens a connection to the dht service.
1259 * @param cls Closure (peer).
1260 * @param cfg Configuration handle.
1265 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1267 struct RegexPeer *peer = cls;
1269 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1271 return peer->dht_handle;
1276 * Adapter function called to destroy a connection to the dht service.
1278 * @param cls Closure (peer).
1279 * @param op_result Service handle returned from the connect adapter.
1282 dht_da (void *cls, void *op_result)
1284 struct RegexPeer *peer = (struct RegexPeer *) cls;
1286 GNUNET_assert (peer->dht_handle == op_result);
1288 if (NULL != peer->search_handle)
1290 GNUNET_REGEX_search_cancel (peer->search_handle);
1291 peer->search_handle = NULL;
1294 if (NULL != peer->dht_handle)
1296 GNUNET_DHT_disconnect (peer->dht_handle);
1297 peer->dht_handle = NULL;
1302 /******************************************************************************/
1303 /*************************** TESTBED PEER SETUP *****************************/
1304 /******************************************************************************/
1308 * Configure the peer overlay topology.
1311 * @param tc the task context
1314 do_configure_topology (void *cls,
1315 const struct GNUNET_SCHEDULER_TaskContext * tc)
1318 if (0 == linking_factor)
1320 num_links = linking_factor * num_peers;
1322 /* num_links = num_peers - 1; */
1323 num_links = linking_factor;
1325 /* Do overlay connect */
1326 prof_start_time = GNUNET_TIME_absolute_get ();
1328 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
1332 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
1334 GNUNET_TESTBED_TOPOLOGY_RETRY_CNT,
1336 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
1337 if (NULL == topology_op)
1339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1340 "Cannot create topology, op handle was NULL\n");
1347 * Functions of this signature are called when a peer has been successfully
1348 * started or stopped.
1350 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
1351 * @param emsg NULL on success; otherwise an error description
1354 peer_churn_cb (void *cls, const char *emsg)
1356 struct DLLOperation *dll_op = cls;
1357 struct GNUNET_TESTBED_Operation *op;
1358 static unsigned int started_peers;
1359 unsigned int peer_cnt;
1362 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1363 GNUNET_free (dll_op);
1366 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1367 _("An operation has failed while starting peers: %s\n"), emsg);
1368 GNUNET_TESTBED_operation_done (op);
1369 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1370 GNUNET_SCHEDULER_cancel (abort_task);
1371 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1374 GNUNET_TESTBED_operation_done (op);
1375 if (++started_peers == num_peers)
1377 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1378 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1379 "All peers started successfully in %s\n",
1380 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1383 peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
1384 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1385 peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
1387 state = STATE_PEERS_LINKING;
1388 GNUNET_SCHEDULER_add_now (&do_configure_topology, NULL);
1394 * Functions of this signature are called when a peer has been successfully
1397 * @param cls the closure from GNUNET_TESTBED_peer_create()
1398 * @param peer the handle for the created peer; NULL on any error during
1400 * @param emsg NULL if peer is not NULL; else MAY contain the error description
1403 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
1405 struct DLLOperation *dll_op = cls;
1406 struct RegexPeer *peer_ptr;
1407 static unsigned int created_peers;
1408 unsigned int peer_cnt;
1412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1413 _("Creating a peer failed. Error: %s\n"), emsg);
1414 GNUNET_TESTBED_operation_done (dll_op->op);
1415 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1416 GNUNET_free (dll_op);
1417 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1418 GNUNET_SCHEDULER_cancel (abort_task);
1419 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1423 peer_ptr = dll_op->cls;
1424 GNUNET_assert (NULL == peer_ptr->peer_handle);
1425 GNUNET_CONFIGURATION_destroy (peer_ptr->cfg);
1426 peer_ptr->cfg = NULL;
1427 peer_ptr->peer_handle = peer;
1428 GNUNET_TESTBED_operation_done (dll_op->op);
1429 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1430 GNUNET_free (dll_op);
1432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
1434 GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
1436 if (++created_peers == num_peers)
1438 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1439 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1440 "All peers created successfully in %s\n",
1441 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1442 /* Now peers are to be started */
1443 state = STATE_PEERS_STARTING;
1444 prof_start_time = GNUNET_TIME_absolute_get ();
1445 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1447 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1448 dll_op->op = GNUNET_TESTBED_peer_start (dll_op,
1449 peers[peer_cnt].peer_handle,
1450 &peer_churn_cb, dll_op);
1451 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1458 * Function called with a filename for each file in the policy directory. Create
1459 * a peer for each filename and update the peer's configuration to include the
1460 * max_path_compression specified as a command line argument as well as the
1461 * policy_file for this peer. The gnunet-service-regexprofiler service is
1462 * automatically started on this peer. The service reads the configurration and
1463 * announces the regexes stored in the policy file 'filename'.
1465 * @param cls closure
1466 * @param filename complete filename (absolute path)
1467 * @return GNUNET_OK to continue to iterate,
1468 * GNUNET_SYSERR to abort iteration with error!
1471 policy_filename_cb (void *cls, const char *filename)
1473 static unsigned int peer_cnt;
1474 struct DLLOperation *dll_op;
1475 struct RegexPeer *peer = &peers[peer_cnt];
1477 GNUNET_assert (NULL != peer);
1479 peer->policy_file = GNUNET_strdup (filename);
1481 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1482 "Creating peer %i on host %s for policy file %s\n",
1483 peer->id, GNUNET_TESTBED_host_get_hostname (peer->host_handle),
1486 /* Set configuration options specific for this peer
1487 (max_path_compression and policy_file */
1488 peer->cfg = GNUNET_CONFIGURATION_dup (cfg);
1489 GNUNET_CONFIGURATION_set_value_number (peer->cfg, "REGEXPROFILER",
1490 "MAX_PATH_COMPRESSION",
1491 (unsigned long long)
1492 max_path_compression);
1493 GNUNET_CONFIGURATION_set_value_string (peer->cfg, "REGEXPROFILER",
1494 "POLICY_FILE", filename);
1496 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1497 dll_op->cls = &peers[peer_cnt];
1498 dll_op->op = GNUNET_TESTBED_peer_create (mc,
1503 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1512 * Controller event callback.
1515 * @param event the controller event
1518 controller_event_cb (void *cls,
1519 const struct GNUNET_TESTBED_EventInformation *event)
1521 struct DLLOperation *dll_op;
1522 struct GNUNET_TESTBED_Operation *op;
1527 case STATE_SLAVES_STARTING:
1528 switch (event->type)
1530 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1532 static unsigned int slaves_started;
1533 unsigned int peer_cnt;
1535 dll_op = event->op_cls;
1536 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1537 GNUNET_free (dll_op);
1539 if (NULL != event->details.operation_finished.emsg)
1541 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1542 _("An operation has failed while starting slaves\n"));
1543 GNUNET_TESTBED_operation_done (op);
1544 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1545 GNUNET_SCHEDULER_cancel (abort_task);
1546 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1549 GNUNET_TESTBED_operation_done (op);
1550 /* Proceed to start peers */
1551 if (++slaves_started == num_hosts - 1)
1553 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1554 "All slaves started successfully\n");
1556 state = STATE_PEERS_CREATING;
1557 prof_start_time = GNUNET_TIME_absolute_get ();
1559 if (-1 == (ret = GNUNET_DISK_directory_scan (policy_dir,
1563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1564 _("No files found in `%s'\n"),
1566 GNUNET_SCHEDULER_shutdown ();
1569 num_peers = (unsigned int) ret;
1570 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1572 /* Initialize peers */
1573 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1575 struct RegexPeer *peer = &peers[peer_cnt];
1576 peer->id = peer_cnt;
1577 peer->policy_file = NULL;
1578 /* Do not start peers on hosts[0] (master controller) */
1579 peer->host_handle = hosts[1 + (peer_cnt % (num_hosts -1))];
1580 peer->dht_handle = NULL;
1581 peer->search_handle = NULL;
1582 peer->stats_handle = NULL;
1583 peer->stats_op_handle = NULL;
1584 peer->search_str = NULL;
1585 peer->search_str_matched = GNUNET_NO;
1588 GNUNET_DISK_directory_scan (policy_dir,
1589 &policy_filename_cb,
1598 case STATE_PEERS_STARTING:
1599 switch (event->type)
1601 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1602 /* Control reaches here when peer start fails */
1603 case GNUNET_TESTBED_ET_PEER_START:
1604 /* we handle peer starts in peer_churn_cb */
1610 case STATE_PEERS_LINKING:
1611 switch (event->type)
1613 static unsigned int established_links;
1614 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1615 /* Control reaches here when a peer linking operation fails */
1616 if (NULL != event->details.operation_finished.emsg)
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1619 _("An operation has failed while linking\n"));
1620 printf ("F%u/%u(%s)",
1621 retry_links + 1, established_links + 1,
1622 event->details.operation_finished.emsg);
1626 /* We do no retries, consider this link as established */
1628 case GNUNET_TESTBED_ET_CONNECT:
1630 char output_buffer[1024];
1633 if (0 == established_links)
1634 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Establishing links .");
1637 printf (".");fflush (stdout);
1639 if (++established_links == num_links)
1641 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1642 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1643 "\n%u links established in %s\n",
1645 GNUNET_STRINGS_relative_time_to_string (prof_time,
1647 prof_time = GNUNET_TIME_relative_divide(prof_time, num_links);
1648 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1649 "Average of %s per connection\n",
1650 GNUNET_STRINGS_relative_time_to_string (prof_time,
1653 GNUNET_free (peer_handles);
1655 if (NULL != data_file)
1658 GNUNET_snprintf (output_buffer,
1659 sizeof (output_buffer),
1660 "# of peers: %u\n# of links established: %u\n"
1661 "Time to establish links: %s\n"
1662 "Linking failures: %u\n"
1663 "path compression length: %u\n"
1664 "# of search strings: %u\n",
1666 (established_links - retry_links),
1667 GNUNET_STRINGS_relative_time_to_string (prof_time,
1670 max_path_compression,
1671 num_search_strings);
1673 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
1674 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
1677 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1678 "\nWaiting %s before starting to announce.\n",
1679 GNUNET_STRINGS_relative_time_to_string (search_delay,
1681 state = STATE_SEARCH_REGEX;
1682 search_task = GNUNET_SCHEDULER_add_delayed (search_delay,
1683 &do_announce, NULL);
1691 case STATE_SEARCH_REGEX:
1693 /* Handled in service connect callback */
1699 case STATE_PEERS_CREATING:
1700 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create peer\n");
1703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1704 "Unexpected controller_cb with state %i!\n", state);
1712 * Task to register all hosts available in the global host list.
1715 * @param tc the scheduler task context
1718 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1722 * Callback which will be called to after a host registration succeeded or failed
1724 * @param cls the closure
1725 * @param emsg the error message; NULL if host registration is successful
1728 host_registration_completion (void *cls, const char *emsg)
1733 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1734 _("Host registration failed for a host. Error: %s\n"), emsg);
1735 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1736 GNUNET_SCHEDULER_cancel (abort_task);
1737 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1740 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1745 * Task to register all hosts available in the global host list.
1748 * @param tc the scheduler task context
1751 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1753 struct DLLOperation *dll_op;
1754 static unsigned int reg_host;
1757 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1758 if (reg_host == num_hosts - 1)
1760 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1761 "All hosts successfully registered\n");
1763 state = STATE_SLAVES_STARTING;
1764 for (slave = 1; slave < num_hosts; slave++)
1766 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1767 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
1773 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1777 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
1778 host_registration_completion,
1784 * Callback to signal successfull startup of the controller process.
1786 * @param cls the closure from GNUNET_TESTBED_controller_start()
1787 * @param config the configuration with which the controller has been started;
1788 * NULL if status is not GNUNET_OK
1789 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1790 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1793 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
1795 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1796 GNUNET_SCHEDULER_cancel (abort_task);
1797 if (GNUNET_OK != status)
1800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Oh, dear!\n");
1801 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1805 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1806 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1807 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1808 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1809 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1810 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
1811 &controller_event_cb, NULL);
1814 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1815 _("Unable to connect to master controller -- Check config\n"));
1816 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1819 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1820 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1821 &do_abort, (void*) __LINE__);
1826 * Load search strings from given filename. One search string per line.
1828 * @param filename filename of the file containing the search strings.
1829 * @param strings set of strings loaded from file. Caller needs to free this
1830 * if number returned is greater than zero.
1831 * @param limit upper limit on the number of strings read from the file
1832 * @return number of strings found in the file. GNUNET_SYSERR on error.
1835 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1840 unsigned int offset;
1844 if (NULL == filename)
1846 return GNUNET_SYSERR;
1849 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1851 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1852 "Could not find search strings file %s\n", filename);
1853 return GNUNET_SYSERR;
1855 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1859 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1860 return GNUNET_SYSERR;
1862 data = GNUNET_malloc (filesize);
1863 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1866 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1868 return GNUNET_SYSERR;
1873 while (offset < (filesize - 1) && str_cnt < limit)
1876 if (((data[offset] == '\n')) && (buf != &data[offset]))
1878 data[offset] = '\0';
1880 buf = &data[offset + 1];
1882 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1883 buf = &data[offset + 1];
1885 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1887 for (i = 0; i < str_cnt; i++)
1889 GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1890 offset += strlen (&data[offset]) + 1;
1898 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
1899 * inform whether the given host is habitable or not. The Handle returned by
1900 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
1903 * @param host the host whose status is being reported; will be NULL if the host
1904 * given to GNUNET_TESTBED_is_host_habitable() is NULL
1905 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
1908 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host, int status)
1910 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handle = cls;
1911 static unsigned int hosts_checked;
1914 if (GNUNET_NO == status)
1916 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1917 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1918 GNUNET_TESTBED_host_get_hostname (host));
1920 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Testbed cannot be started on localhost\n"));
1921 GNUNET_SCHEDULER_cancel (abort_task);
1922 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1926 /* printf (_("\rChecked %u hosts"), hosts_checked); */
1927 /* fflush (stdout); */
1928 if (hosts_checked < num_hosts)
1930 /* printf (_("\nAll hosts can start testbed. Creating peers\n")); */
1931 GNUNET_free (hc_handles);
1934 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
1944 * Main function that will be run by the scheduler.
1946 * @param cls closure
1947 * @param args remaining command-line arguments
1948 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1949 * @param config configuration
1952 run (void *cls, char *const *args, const char *cfgfile,
1953 const struct GNUNET_CONFIGURATION_Handle *config)
1956 unsigned int nsearchstrs;
1958 if (NULL == args[0])
1960 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1961 _("No hosts-file specified on command line. Exiting.\n"));
1964 if (NULL == args[1])
1966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1967 _("No policy directory specified on command line. Exiting.\n"));
1970 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], config, &hosts);
1973 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1974 _("No hosts loaded. Need at least one host\n"));
1977 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1978 _("Checking whether given hosts can start testbed."
1980 hc_handles = GNUNET_malloc (sizeof (struct
1981 GNUNET_TESTBED_HostHabitableCheckHandle *)
1983 for (nhost = 0; nhost < num_hosts; nhost++)
1985 hc_handles[nhost] = GNUNET_TESTBED_is_host_habitable (hosts[nhost], config,
1987 &hc_handles[nhost]);
1988 if (NULL == hc_handles[nhost])
1993 for (i = 0; i <= nhost; i++)
1994 if (NULL != hc_handles[i])
1995 GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[i]);
1996 GNUNET_free (hc_handles);
2001 if (num_hosts != nhost)
2003 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Exiting\n"));
2004 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2009 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2010 _("No configuration file given. Exiting\n"));
2011 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2016 GNUNET_CONFIGURATION_get_value_string (config, "REGEXPROFILER", "REGEX_PREFIX",
2019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2020 _("Configuration option \"regex_prefix\" missing. Exiting\n"));
2021 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2025 if ( (NULL != data_filename) &&
2026 (NULL == (data_file =
2027 GNUNET_DISK_file_open (data_filename,
2028 GNUNET_DISK_OPEN_READWRITE |
2029 GNUNET_DISK_OPEN_TRUNCATE |
2030 GNUNET_DISK_OPEN_CREATE,
2031 GNUNET_DISK_PERM_USER_READ |
2032 GNUNET_DISK_PERM_USER_WRITE))) )
2033 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2036 if (GNUNET_YES != GNUNET_DISK_directory_test (args[1], GNUNET_YES))
2038 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2039 _("Specified policies directory does not exist. Exiting.\n"));
2040 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2043 policy_dir = args[1];
2044 if (GNUNET_YES != GNUNET_DISK_file_test (args[2]))
2046 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2047 _("No search strings file given. Exiting.\n"));
2048 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2051 nsearchstrs = load_search_strings (args[2], &search_strings, num_search_strings);
2052 if (num_search_strings != nsearchstrs)
2054 num_search_strings = nsearchstrs;
2055 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2056 _("Error loading search strings."
2057 "Given file does not contain enough strings. Exiting.\n"));
2058 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2061 if (0 >= num_search_strings || NULL == search_strings)
2063 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2064 _("Error loading search strings. Exiting.\n"));
2065 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2069 for (i = 0; i < num_search_strings; i++)
2070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "search string: %s\n", search_strings[i]);
2071 cfg = GNUNET_CONFIGURATION_dup (config);
2073 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2074 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
2082 * @param argc argument count
2083 * @param argv argument values
2084 * @return 0 on success
2087 main (int argc, char *const *argv)
2089 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2090 {'d', "details", "FILENAME",
2091 gettext_noop ("name of the file for writing statistics"),
2092 1, &GNUNET_GETOPT_set_string, &data_filename},
2093 {'n', "num-links", "COUNT",
2094 gettext_noop ("create COUNT number of random links between peers"),
2095 GNUNET_YES, &GNUNET_GETOPT_set_uint, &linking_factor },
2096 {'t', "matching-timeout", "TIMEOUT",
2097 gettext_noop ("wait TIMEOUT before considering a string match as failed"),
2098 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time
2100 {'s', "search-delay", "DELAY",
2101 gettext_noop ("wait DELAY before starting string search"),
2102 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
2103 {'a', "num-search-strings", "COUNT",
2104 gettext_noop ("number of search strings to read from search strings file"),
2105 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
2106 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
2107 gettext_noop ("maximum path compression length"),
2108 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
2109 GNUNET_GETOPT_OPTION_END
2113 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2116 result = GNUNET_SYSERR;
2118 GNUNET_PROGRAM_run (argc, argv,
2119 "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
2120 _("Profiler for regex"),
2121 options, &run, NULL);
2123 if (GNUNET_OK != ret)
2125 if (GNUNET_OK != result)