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 \
40 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
41 #define SEARCHES_IN_PARALLEL 5
49 * The testbed operation handle
51 struct GNUNET_TESTBED_Operation *op;
59 * The next pointer for DLL
61 struct DLLOperation *next;
64 * The prev pointer for DLL
66 struct DLLOperation *prev;
71 * Available states during profiling
83 STATE_SLAVES_STARTING,
101 * Matching strings against announced regexes
106 * Destroying peers; we can do this as the controller takes care of stopping a
107 * peer if it is running
109 STATE_PEERS_DESTROYING
124 * Peer configuration handle.
126 struct GNUNET_CONFIGURATION_Handle *cfg;
129 * The actual testbed peer handle.
131 struct GNUNET_TESTBED_Peer *peer_handle;
134 * Host on which the peer is running.
136 struct GNUNET_TESTBED_Host *host_handle;
139 * Filename of the peer's policy file.
144 * Peer's search string.
146 const char *search_str;
149 * Set to GNUNET_YES if the peer successfully matched the above
150 * search string. GNUNET_NO if the string could not be matched
151 * during the profiler run. GNUNET_SYSERR if the string matching
152 * timed out. Undefined if search_str is NULL
154 int search_str_matched;
159 struct GNUNET_ARM_Handle *arm_handle;
164 struct GNUNET_DHT_Handle *dht_handle;
167 * Handle to a running regex search.
169 struct GNUNET_REGEX_search_handle *search_handle;
172 * Testbed operation handle for the ARM and DHT services.
174 struct GNUNET_TESTBED_Operation *op_handle;
177 * Peers's statistics handle.
179 struct GNUNET_STATISTICS_Handle *stats_handle;
182 * Testbed operation handle for the statistics service.
184 struct GNUNET_TESTBED_Operation *stats_op_handle;
187 * The starting time of a profiling step.
189 struct GNUNET_TIME_Absolute prof_start_time;
194 GNUNET_SCHEDULER_TaskIdentifier timeout;
199 * An array of hosts loaded from the hostkeys file
201 static struct GNUNET_TESTBED_Host **hosts;
204 * Array of peer handles used to pass to
205 * GNUNET_TESTBED_overlay_configure_topology
207 static struct GNUNET_TESTBED_Peer **peer_handles;
210 * The array of peers; we fill this as the peers are given to us by the testbed
212 static struct RegexPeer *peers;
215 * Host registration handle
217 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
220 * Handle to the master controller process
222 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
225 * Handle to the master controller
227 static struct GNUNET_TESTBED_Controller *mc;
230 * Handle to global configuration
232 static struct GNUNET_CONFIGURATION_Handle *cfg;
235 * Head of the operations list
237 static struct DLLOperation *dll_op_head;
240 * Tail of the operations list
242 static struct DLLOperation *dll_op_tail;
245 * Peer linking - topology operation
247 static struct GNUNET_TESTBED_Operation *topology_op;
250 * The handle for whether a host is habitable or not
252 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
255 * Abort task identifier
257 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
260 * Shutdown task identifier
262 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
265 * Host registration task identifier
267 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
270 * Global event mask for all testbed events
272 static uint64_t event_mask;
275 * The starting time of a profiling step
277 static struct GNUNET_TIME_Absolute prof_start_time;
280 * Duration profiling step has taken
282 static struct GNUNET_TIME_Relative prof_time;
285 * Number of peers to be started by the profiler
287 static unsigned int num_peers;
290 * Number of hosts in the hosts array
292 static unsigned int num_hosts;
295 * Factor of number of links. num_links = num_peers * linking_factor.
297 static unsigned int linking_factor;
300 * Number of random links to be established between peers
302 static unsigned int num_links;
305 * Number of connect operations that have failed, candidates to retry
307 static unsigned int retry_links;
310 * Global testing status
315 * current state of profiling
320 * Folder where policy files are stored.
322 static char * policy_dir;
327 static char **search_strings;
330 * Number of search strings.
332 static int num_search_strings;
335 * How many searches are running in parallel
337 static unsigned int parallel_searches;
340 * Number of peers found with search strings.
342 static unsigned int peers_found;
345 * Index of peer to start next announce/search.
347 static unsigned int next_search;
350 * Search task identifier
352 static GNUNET_SCHEDULER_TaskIdentifier search_task;
355 * Search timeout task identifier.
357 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
360 * Search timeout in seconds.
362 static struct GNUNET_TIME_Relative search_timeout_time = { 60000 };
365 * How long do we wait before starting the search?
368 static struct GNUNET_TIME_Relative search_delay = { 60000 };
371 * File to log statistics to.
373 static struct GNUNET_DISK_FileHandle *data_file;
376 * Filename to log statistics to.
378 static char *data_filename;
381 * Maximal path compression length.
383 static unsigned int max_path_compression;
386 * Prefix used for regex announcing. We need to prefix the search
387 * strings with it, in order to find something.
389 static char * regex_prefix;
392 * What's the maximum regex reannounce period.
394 static struct GNUNET_TIME_Relative reannounce_period_max;
397 /******************************************************************************/
398 /****************************** DECLARATIONS ********************************/
399 /******************************************************************************/
402 * DHT connect callback.
404 * @param cls internal peer id.
405 * @param op operation handle.
406 * @param ca_result connect adapter result.
407 * @param emsg error message.
410 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
411 void *ca_result, const char *emsg);
414 * DHT connect adapter.
416 * @param cls not used.
417 * @param cfg configuration handle.
422 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
426 * Adapter function called to destroy a connection to
430 * @param op_result service handle returned from the connect adapter
433 dht_da (void *cls, void *op_result);
437 * Function called by testbed once we are connected to stats
438 * service. Get the statistics for the services of interest.
440 * @param cls the 'struct RegexPeer' for which we connected to stats
441 * @param op connect operation handle
442 * @param ca_result handle to stats service
443 * @param emsg error message on failure
446 stats_connect_cb (void *cls,
447 struct GNUNET_TESTBED_Operation *op,
453 * Task to collect all statistics from s, will shutdown the
454 * profiler, when done.
457 * @param tc the task context
460 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
464 * Start announcing the next regex in the DHT.
466 * @param cls Index of the next peer in the peers array.
467 * @param tc TaskContext.
470 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
473 /******************************************************************************/
474 /******************************** SHUTDOWN **********************************/
475 /******************************************************************************/
482 * @param tc the task context
485 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
487 struct DLLOperation *dll_op;
488 struct RegexPeer *peer;
490 unsigned int peer_cnt;
491 unsigned int search_str_cnt;
492 char output_buffer[512];
495 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
496 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
497 GNUNET_SCHEDULER_cancel (abort_task);
498 if (NULL != hc_handles)
500 for (nhost = 0; nhost < num_hosts; nhost++)
501 if (NULL != hc_handles[nhost])
502 GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[nhost]);
503 GNUNET_free (hc_handles);
506 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
507 GNUNET_SCHEDULER_cancel (register_hosts_task);
509 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
511 peer = &peers[peer_cnt];
513 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
515 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
517 GNUNET_snprintf (output_buffer,
518 sizeof (output_buffer),
519 "%p Search string not found: %s (%d)\n%p On peer: %u (%p)\n%p With policy file: %s\n%p After: %s\n",
520 peer, peer->search_str, peer->search_str_matched,
521 peer, peer->id, peer,
522 peer, peer->policy_file,
524 GNUNET_STRINGS_relative_time_to_string (prof_time,
526 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
527 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
530 if (NULL != peers[peer_cnt].op_handle)
531 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
532 if (NULL != peers[peer_cnt].stats_op_handle)
533 GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
536 if (NULL != data_file)
537 GNUNET_DISK_file_close (data_file);
539 for (search_str_cnt = 0;
540 search_str_cnt < num_search_strings && NULL != search_strings;
543 GNUNET_free_non_null (search_strings[search_str_cnt]);
545 GNUNET_free_non_null (search_strings);
547 if (NULL != reg_handle)
548 GNUNET_TESTBED_cancel_registration (reg_handle);
549 if (NULL != topology_op)
550 GNUNET_TESTBED_operation_done (topology_op);
551 for (nhost = 0; nhost < num_hosts; nhost++)
552 if (NULL != hosts[nhost])
553 GNUNET_TESTBED_host_destroy (hosts[nhost]);
554 GNUNET_free_non_null (hosts);
556 while (NULL != (dll_op = dll_op_head))
558 GNUNET_TESTBED_operation_done (dll_op->op);
559 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
560 GNUNET_free (dll_op);
563 GNUNET_TESTBED_controller_disconnect (mc);
565 GNUNET_TESTBED_controller_stop (mc_proc);
567 GNUNET_CONFIGURATION_destroy (cfg);
569 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
574 * abort task to run on test timed out
577 * @param tc the task context
580 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
582 unsigned long i = (unsigned long) cls;
584 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting %lu...\n", i);
585 abort_task = GNUNET_SCHEDULER_NO_TASK;
586 result = GNUNET_SYSERR;
587 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
588 GNUNET_SCHEDULER_cancel (shutdown_task);
589 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
593 /******************************************************************************/
594 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
595 /******************************************************************************/
598 * Adapter function called to establish a connection to
599 * statistics service.
602 * @param cfg configuration of the peer to connect to; will be available until
603 * GNUNET_TESTBED_operation_done() is called on the operation returned
604 * from GNUNET_TESTBED_service_connect()
605 * @return service handle to return in 'op_result', NULL on error
608 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
610 return GNUNET_STATISTICS_create ("<driver>", cfg);
615 * Adapter function called to destroy a connection to
616 * statistics service.
619 * @param op_result service handle returned from the connect adapter
622 stats_da (void *cls, void *op_result)
624 struct RegexPeer *peer = cls;
626 GNUNET_assert (op_result == peer->stats_handle);
628 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
629 peer->stats_handle = NULL;
634 * Process statistic values. Write all values to global 'data_file', if present.
637 * @param subsystem name of subsystem that created the statistic
638 * @param name the name of the datum
639 * @param value the current value
640 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
641 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
644 stats_iterator (void *cls, const char *subsystem, const char *name,
645 uint64_t value, int is_persistent)
647 struct RegexPeer *peer = cls;
648 char output_buffer[512];
651 if (NULL == data_file)
653 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
654 "%p -> %s [%s]: %llu\n",
655 peer, subsystem, name, value);
659 GNUNET_snprintf (output_buffer,
660 sizeof (output_buffer),
663 subsystem, value, name);
664 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
665 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
672 * Stats callback. Finish the stats testbed operation and when all stats have
673 * been iterated, shutdown the profiler.
676 * @param success GNUNET_OK if statistics were
677 * successfully obtained, GNUNET_SYSERR if not.
683 static unsigned int peer_cnt;
684 struct RegexPeer *peer = cls;
686 if (GNUNET_OK != success)
688 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
689 "Getting statistics for peer %u failed!\n",
694 GNUNET_assert (NULL != peer->stats_op_handle);
696 GNUNET_TESTBED_operation_done (peer->stats_op_handle);
697 peer->stats_op_handle = NULL;
700 peer = &peers[peer_cnt];
702 if (peer_cnt == num_peers)
704 struct GNUNET_TIME_Relative delay = { 100 };
705 shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
709 peer->stats_op_handle =
710 GNUNET_TESTBED_service_connect (NULL,
723 * Function called by testbed once we are connected to stats
724 * service. Get the statistics for the services of interest.
726 * @param cls the 'struct RegexPeer' for which we connected to stats
727 * @param op connect operation handle
728 * @param ca_result handle to stats service
729 * @param emsg error message on failure
732 stats_connect_cb (void *cls,
733 struct GNUNET_TESTBED_Operation *op,
737 struct RegexPeer *peer = cls;
739 if (NULL == ca_result || NULL != emsg)
741 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
742 "Failed to connect to statistics service on peer %u: %s\n",
745 peer->stats_handle = NULL;
749 peer->stats_handle = ca_result;
751 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
752 GNUNET_TIME_UNIT_FOREVER_REL,
754 &stats_iterator, peer))
756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
757 "Could not get statistics of peer %u!\n", peer->id);
763 * Task to collect all statistics from all peers, will shutdown the
764 * profiler, when done.
767 * @param tc the task context
770 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
772 struct RegexPeer *peer = &peers[0];
774 GNUNET_assert (NULL != peer->peer_handle);
776 peer->stats_op_handle =
777 GNUNET_TESTBED_service_connect (NULL,
788 /******************************************************************************/
789 /************************ REGEX FIND CONNECTIONS **************************/
790 /******************************************************************************/
794 * Start searching for the next string in the DHT.
796 * @param cls Index of the next peer in the peers array.
797 * @param tc TaskContext.
800 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
804 * Method called when we've found a peer that announced a regex
805 * that matches our search string. Now get the statistics.
807 * @param cls Closure provided in GNUNET_REGEX_search.
808 * @param id Peer providing a regex that matches the string.
809 * @param get_path Path of the get request.
810 * @param get_path_length Lenght of get_path.
811 * @param put_path Path of the put request.
812 * @param put_path_length Length of the put_path.
815 regex_found_handler (void *cls,
816 const struct GNUNET_PeerIdentity *id,
817 const struct GNUNET_PeerIdentity *get_path,
818 unsigned int get_path_length,
819 const struct GNUNET_PeerIdentity *put_path,
820 unsigned int put_path_length)
822 struct RegexPeer *peer = cls;
823 char output_buffer[512];
826 if (GNUNET_YES == peer->search_str_matched)
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "String %s on peer %u already matched!\n",
830 peer->search_str, peer->id);
837 if (GNUNET_SCHEDULER_NO_TASK != peer->timeout)
839 GNUNET_SCHEDULER_cancel (peer->timeout);
840 peer->timeout = GNUNET_SCHEDULER_NO_TASK;
841 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
846 // FIXME not possible right now
847 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
848 "String matching timed out for string %s on peer %u (%i/%i)\n",
849 peer->search_str, peer->id, peers_found, num_search_strings);
850 peer->search_str_matched = GNUNET_SYSERR;
854 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
856 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
857 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
858 peer->search_str, peer->id,
859 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
860 peers_found, num_search_strings, parallel_searches);
862 peer->search_str_matched = GNUNET_YES;
864 if (NULL != data_file)
867 GNUNET_snprintf (output_buffer,
868 sizeof (output_buffer),
869 "%p Peer: %u\n%p Host: %s\n%p Policy file: %s\n"
870 "%p Search string: %s\n%p Search duration: %s\n\n",
873 GNUNET_TESTBED_host_get_hostname (peer->host_handle),
874 peer, peer->policy_file,
875 peer, peer->search_str,
877 GNUNET_STRINGS_relative_time_to_string (prof_time,
880 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
885 GNUNET_TESTBED_operation_done (peer->op_handle);
886 peer->op_handle = NULL;
888 if (peers_found == num_search_strings)
890 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
891 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
892 "All strings successfully matched in %s\n",
893 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
895 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
896 GNUNET_SCHEDULER_cancel (search_timeout_task);
898 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
899 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
905 * Connect by string timeout task. This will cancel the profiler after the
906 * specified timeout 'search_timeout'.
909 * @param tc the task context
912 search_timeout (void *cls,
913 const struct GNUNET_SCHEDULER_TaskContext * tc)
915 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
916 "Finding matches to all strings did not succeed after %s.\n",
917 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
919 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
920 "Found %i of %i strings\n", peers_found, num_search_strings);
922 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
923 "Search timed out after %s."
924 "Collecting stats and shutting down.\n",
925 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
928 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
933 * Search timed out. It might still complete in the future,
934 * but we should start another one.
936 * @param cls Index of the next peer in the peers array.
937 * @param tc TaskContext.
940 find_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
942 struct RegexPeer *p = cls;
944 p->timeout = GNUNET_SCHEDULER_NO_TASK;
946 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
948 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
949 "Searching for string \"%s\" on peer %d timed out. Starting new search.\n",
952 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
957 * Start searching for a string in the DHT.
959 * @param cls Index of the next peer in the peers array.
960 * @param tc TaskContext.
963 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
965 unsigned int search_peer = (unsigned int) (long) cls;
967 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
968 search_peer >= num_search_strings)
971 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
972 "Searching for string \"%s\" on peer %d with file %s (%u||)\n",
973 peers[search_peer].search_str,
975 peers[search_peer].policy_file,
978 peers[search_peer].op_handle =
979 GNUNET_TESTBED_service_connect (NULL,
980 peers[search_peer].peer_handle,
986 &peers[search_peer]);
987 GNUNET_assert (NULL != peers[search_peer].op_handle);
988 peers[search_peer].timeout = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
990 &peers[search_peer]);
995 * ARM connect adapter. Opens a connection to the ARM service.
997 * @param cls Closure (peer).
998 * @param cfg Configuration handle.
1003 arm_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1005 struct RegexPeer *peer = cls;
1007 peer->arm_handle = GNUNET_ARM_connect (cfg, NULL, NULL);
1009 return peer->arm_handle;
1014 * Adapter function called to destroy a connection to the ARM service.
1016 * @param cls Closure (peer).
1017 * @param op_result Service handle returned from the connect adapter.
1020 arm_da (void *cls, void *op_result)
1022 struct RegexPeer *peer = (struct RegexPeer *) cls;
1024 GNUNET_assert (peer->arm_handle == op_result);
1026 if (NULL != peer->arm_handle)
1028 GNUNET_ARM_disconnect_and_free (peer->arm_handle);
1029 peer->arm_handle = NULL;
1034 * Finish and free the operation used to start the regex daemon.
1035 * operation_done calls ARM_disconnect, which cannot happen inside an
1038 * @param cls Closure (Peer info)
1039 * @param tc TaskContext
1042 arm_op_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1044 struct RegexPeer *peer = (struct RegexPeer *) cls;
1046 GNUNET_TESTBED_operation_done (peer->op_handle);
1047 peer->op_handle = NULL;
1052 * Callback called when arm has started the daemon we asked for.
1054 * @param cls Closure ().
1055 * @param arm Arm handle.
1056 * @param rs Status of the request.
1057 * @param service Service we asked to start (deamon).
1058 * @param result Result of the request.
1061 arm_start_cb (void *cls, struct GNUNET_ARM_Handle *arm,
1062 enum GNUNET_ARM_RequestStatus rs, const char *service,
1063 enum GNUNET_ARM_Result result)
1065 struct RegexPeer *peer = (struct RegexPeer *) cls;
1067 if (rs != GNUNET_ARM_REQUEST_SENT_OK)
1069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ARM request was not sent: %u\n", rs);
1075 * Asked to start it, but it's already starting.
1077 case GNUNET_ARM_RESULT_IS_STARTING_ALREADY:
1078 GNUNET_break (0); /* Shouldn't be starting, however it's not fatal. */
1082 * Service is currently being started (due to client request).
1084 case GNUNET_ARM_RESULT_STARTING:
1085 GNUNET_SCHEDULER_add_now (&arm_op_done, peer);
1088 unsigned long search_peer;
1094 /* Find a peer to look for a string matching the regex announced */
1095 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1097 for (i = 0; peers[search_peer].search_str != NULL; i++)
1099 search_peer = (search_peer + 1) % num_peers;
1101 GNUNET_abort (); /* we ran out of peers, must be a bug */
1103 peers[search_peer].search_str = search_strings[me];
1104 peers[search_peer].search_str_matched = GNUNET_NO;
1105 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
1106 reannounce_period_max,
1109 (void *) search_peer);
1111 if (next_search >= num_peers &&
1112 GNUNET_SCHEDULER_NO_TASK == search_timeout_task)
1114 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All daemons started.\n");
1115 /* FIXME start GLOBAL timeout to abort experiment */
1116 search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout_time,
1123 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ARM returned %d\n", result);
1129 * ARM connect callback. Called when we are connected to the arm service for
1130 * the peer in 'cls'. If successfull we start the regex deamon to start
1131 * announcing the regex of this peer.
1133 * @param cls internal peer id.
1134 * @param op operation handle.
1135 * @param ca_result connect adapter result.
1136 * @param emsg error message.
1139 arm_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1140 void *ca_result, const char *emsg)
1142 struct RegexPeer *peer = (struct RegexPeer *) cls;
1144 if (NULL != emsg || NULL == op || NULL == ca_result)
1146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ARM connect failed: %s\n", emsg);
1150 GNUNET_assert (NULL != peer->arm_handle);
1151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "[]%p - ()%p\n", peer->op_handle, op);
1152 GNUNET_assert (peer->op_handle == op);
1153 GNUNET_assert (peer->arm_handle == ca_result);
1155 GNUNET_ARM_request_service_start (ca_result, "regexprofiler",
1156 GNUNET_OS_INHERIT_STD_NONE,
1157 GNUNET_TIME_UNIT_FOREVER_REL,
1163 * Task to start the daemons on each peer so that the regexes are announced
1167 * @param tc the task context
1170 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1174 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting announce.\n");
1176 for (i = 0; i < SEARCHES_IN_PARALLEL; i++)
1178 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1179 " scheduling announce %u\n",
1181 (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
1187 * Start announcing the next regex in the DHT.
1189 * @param cls Closure (unused).
1190 * @param tc TaskContext.
1193 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1195 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
1196 next_search >= num_peers)
1199 /* First connect to arm service, then announce. Next
1200 * a nnounce will be in arm_connect_cb */
1201 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
1202 peers[next_search].op_handle =
1203 GNUNET_TESTBED_service_connect (NULL,
1204 peers[next_search].peer_handle,
1207 &peers[next_search],
1210 &peers[next_search]);
1212 parallel_searches++;
1216 * DHT connect callback. Called when we are connected to the dht service for
1217 * the peer in 'cls'. If successfull we connect to the stats service of this
1218 * peer and then try to match the search string of this peer.
1220 * @param cls internal peer id.
1221 * @param op operation handle.
1222 * @param ca_result connect adapter result.
1223 * @param emsg error message.
1226 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1227 void *ca_result, const char *emsg)
1229 struct RegexPeer *peer = (struct RegexPeer *) cls;
1231 if (NULL != emsg || NULL == op || NULL == ca_result)
1233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1237 GNUNET_assert (NULL != peer->dht_handle);
1238 GNUNET_assert (peer->op_handle == op);
1239 GNUNET_assert (peer->dht_handle == ca_result);
1241 peer->search_str_matched = GNUNET_NO;
1242 peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
1244 ®ex_found_handler, peer,
1246 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1251 * DHT connect adapter. Opens a connection to the dht service.
1253 * @param cls Closure (peer).
1254 * @param cfg Configuration handle.
1259 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1261 struct RegexPeer *peer = cls;
1263 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1265 return peer->dht_handle;
1270 * Adapter function called to destroy a connection to the dht service.
1272 * @param cls Closure (peer).
1273 * @param op_result Service handle returned from the connect adapter.
1276 dht_da (void *cls, void *op_result)
1278 struct RegexPeer *peer = (struct RegexPeer *) cls;
1280 GNUNET_assert (peer->dht_handle == op_result);
1282 if (NULL != peer->search_handle)
1284 GNUNET_REGEX_search_cancel (peer->search_handle);
1285 peer->search_handle = NULL;
1288 if (NULL != peer->dht_handle)
1290 GNUNET_DHT_disconnect (peer->dht_handle);
1291 peer->dht_handle = NULL;
1296 /******************************************************************************/
1297 /*************************** TESTBED PEER SETUP *****************************/
1298 /******************************************************************************/
1302 * Configure the peer overlay topology.
1305 * @param tc the task context
1308 do_configure_topology (void *cls,
1309 const struct GNUNET_SCHEDULER_TaskContext * tc)
1312 if (0 == linking_factor)
1314 num_links = linking_factor * num_peers;
1316 /* num_links = num_peers - 1; */
1317 num_links = linking_factor;
1319 /* Do overlay connect */
1320 prof_start_time = GNUNET_TIME_absolute_get ();
1322 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
1326 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
1328 GNUNET_TESTBED_TOPOLOGY_RETRY_CNT,
1330 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
1331 if (NULL == topology_op)
1333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1334 "Cannot create topology, op handle was NULL\n");
1341 * Functions of this signature are called when a peer has been successfully
1342 * started or stopped.
1344 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
1345 * @param emsg NULL on success; otherwise an error description
1348 peer_churn_cb (void *cls, const char *emsg)
1350 struct DLLOperation *dll_op = cls;
1351 struct GNUNET_TESTBED_Operation *op;
1352 static unsigned int started_peers;
1353 unsigned int peer_cnt;
1356 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1357 GNUNET_free (dll_op);
1360 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1361 _("An operation has failed while starting peers: %s\n"), emsg);
1362 GNUNET_TESTBED_operation_done (op);
1363 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1364 GNUNET_SCHEDULER_cancel (abort_task);
1365 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1368 GNUNET_TESTBED_operation_done (op);
1369 if (++started_peers == num_peers)
1371 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1372 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1373 "All peers started successfully in %s\n",
1374 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1377 peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
1378 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1379 peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
1381 state = STATE_PEERS_LINKING;
1382 GNUNET_SCHEDULER_add_now (&do_configure_topology, NULL);
1388 * Functions of this signature are called when a peer has been successfully
1391 * @param cls the closure from GNUNET_TESTBED_peer_create()
1392 * @param peer the handle for the created peer; NULL on any error during
1394 * @param emsg NULL if peer is not NULL; else MAY contain the error description
1397 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
1399 struct DLLOperation *dll_op = cls;
1400 struct RegexPeer *peer_ptr;
1401 static unsigned int created_peers;
1402 unsigned int peer_cnt;
1406 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1407 _("Creating a peer failed. Error: %s\n"), emsg);
1408 GNUNET_TESTBED_operation_done (dll_op->op);
1409 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1410 GNUNET_free (dll_op);
1411 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1412 GNUNET_SCHEDULER_cancel (abort_task);
1413 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1417 peer_ptr = dll_op->cls;
1418 GNUNET_assert (NULL == peer_ptr->peer_handle);
1419 GNUNET_CONFIGURATION_destroy (peer_ptr->cfg);
1420 peer_ptr->cfg = NULL;
1421 peer_ptr->peer_handle = peer;
1422 GNUNET_TESTBED_operation_done (dll_op->op);
1423 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1424 GNUNET_free (dll_op);
1426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
1428 GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
1430 if (++created_peers == num_peers)
1432 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1433 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1434 "All peers created successfully in %s\n",
1435 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1436 /* Now peers are to be started */
1437 state = STATE_PEERS_STARTING;
1438 prof_start_time = GNUNET_TIME_absolute_get ();
1439 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1441 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1442 dll_op->op = GNUNET_TESTBED_peer_start (dll_op,
1443 peers[peer_cnt].peer_handle,
1444 &peer_churn_cb, dll_op);
1445 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1452 * Function called with a filename for each file in the policy directory. Create
1453 * a peer for each filename and update the peer's configuration to include the
1454 * max_path_compression specified as a command line argument as well as the
1455 * policy_file for this peer. The gnunet-service-regexprofiler service is
1456 * automatically started on this peer. The service reads the configurration and
1457 * announces the regexes stored in the policy file 'filename'.
1459 * @param cls closure
1460 * @param filename complete filename (absolute path)
1461 * @return GNUNET_OK to continue to iterate,
1462 * GNUNET_SYSERR to abort iteration with error!
1465 policy_filename_cb (void *cls, const char *filename)
1467 static unsigned int peer_cnt;
1468 struct DLLOperation *dll_op;
1469 struct RegexPeer *peer = &peers[peer_cnt];
1471 GNUNET_assert (NULL != peer);
1473 peer->policy_file = GNUNET_strdup (filename);
1475 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1476 "Creating peer %i on host %s for policy file %s\n",
1477 peer->id, GNUNET_TESTBED_host_get_hostname (peer->host_handle),
1480 /* Set configuration options specific for this peer
1481 (max_path_compression and policy_file */
1482 peer->cfg = GNUNET_CONFIGURATION_dup (cfg);
1483 GNUNET_CONFIGURATION_set_value_number (peer->cfg, "REGEXPROFILER",
1484 "MAX_PATH_COMPRESSION",
1485 (unsigned long long)
1486 max_path_compression);
1487 GNUNET_CONFIGURATION_set_value_string (peer->cfg, "REGEXPROFILER",
1488 "POLICY_FILE", filename);
1490 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1491 dll_op->cls = &peers[peer_cnt];
1492 dll_op->op = GNUNET_TESTBED_peer_create (mc,
1497 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1506 * Controller event callback.
1509 * @param event the controller event
1512 controller_event_cb (void *cls,
1513 const struct GNUNET_TESTBED_EventInformation *event)
1515 struct DLLOperation *dll_op;
1516 struct GNUNET_TESTBED_Operation *op;
1521 case STATE_SLAVES_STARTING:
1522 switch (event->type)
1524 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1526 static unsigned int slaves_started;
1527 unsigned int peer_cnt;
1529 dll_op = event->op_cls;
1530 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1531 GNUNET_free (dll_op);
1533 if (NULL != event->details.operation_finished.emsg)
1535 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1536 _("An operation has failed while starting slaves\n"));
1537 GNUNET_TESTBED_operation_done (op);
1538 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1539 GNUNET_SCHEDULER_cancel (abort_task);
1540 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1543 GNUNET_TESTBED_operation_done (op);
1544 /* Proceed to start peers */
1545 if (++slaves_started == num_hosts - 1)
1547 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1548 "All slaves started successfully\n");
1550 state = STATE_PEERS_CREATING;
1551 prof_start_time = GNUNET_TIME_absolute_get ();
1553 if (-1 == (ret = GNUNET_DISK_directory_scan (policy_dir,
1557 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1558 _("No files found in `%s'\n"),
1560 GNUNET_SCHEDULER_shutdown ();
1563 num_peers = (unsigned int) ret;
1564 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1566 /* Initialize peers */
1567 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1569 struct RegexPeer *peer = &peers[peer_cnt];
1570 peer->id = peer_cnt;
1571 peer->policy_file = NULL;
1572 /* Do not start peers on hosts[0] (master controller) */
1573 peer->host_handle = hosts[1 + (peer_cnt % (num_hosts -1))];
1574 peer->dht_handle = NULL;
1575 peer->search_handle = NULL;
1576 peer->stats_handle = NULL;
1577 peer->stats_op_handle = NULL;
1578 peer->search_str = NULL;
1579 peer->search_str_matched = GNUNET_NO;
1582 GNUNET_DISK_directory_scan (policy_dir,
1583 &policy_filename_cb,
1592 case STATE_PEERS_STARTING:
1593 switch (event->type)
1595 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1596 /* Control reaches here when peer start fails */
1597 case GNUNET_TESTBED_ET_PEER_START:
1598 /* we handle peer starts in peer_churn_cb */
1604 case STATE_PEERS_LINKING:
1605 switch (event->type)
1607 static unsigned int established_links;
1608 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1609 /* Control reaches here when a peer linking operation fails */
1610 if (NULL != event->details.operation_finished.emsg)
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1613 _("An operation has failed while linking\n"));
1614 printf ("F%u/%u(%s)",
1615 retry_links + 1, established_links + 1,
1616 event->details.operation_finished.emsg);
1620 /* We do no retries, consider this link as established */
1622 case GNUNET_TESTBED_ET_CONNECT:
1624 char output_buffer[1024];
1627 if (0 == established_links)
1628 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Establishing links .");
1631 printf (".");fflush (stdout);
1633 if (++established_links == num_links)
1635 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1636 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1637 "%u links established in %s\n",
1639 GNUNET_STRINGS_relative_time_to_string (prof_time,
1641 prof_time = GNUNET_TIME_relative_divide(prof_time, num_links);
1642 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1643 "Average of %s per connection\n",
1644 GNUNET_STRINGS_relative_time_to_string (prof_time,
1647 GNUNET_free (peer_handles);
1649 if (NULL != data_file)
1652 GNUNET_snprintf (output_buffer,
1653 sizeof (output_buffer),
1654 "# of peers: %u\n# of links established: %u\n"
1655 "Time to establish links: %s\n"
1656 "Linking failures: %u\n"
1657 "path compression length: %u\n"
1658 "# of search strings: %u\n",
1660 (established_links - retry_links),
1661 GNUNET_STRINGS_relative_time_to_string (prof_time,
1664 max_path_compression,
1665 num_search_strings);
1667 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
1668 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
1671 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1672 "\nWaiting %s before starting to announce.\n",
1673 GNUNET_STRINGS_relative_time_to_string (search_delay,
1675 state = STATE_SEARCH_REGEX;
1676 search_task = GNUNET_SCHEDULER_add_delayed (search_delay,
1677 &do_announce, NULL);
1685 case STATE_SEARCH_REGEX:
1687 /* Handled in service connect callback */
1693 case STATE_PEERS_CREATING:
1694 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create peer\n");
1697 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1698 "Unexpected controller_cb with state %i!\n", state);
1706 * Task to register all hosts available in the global host list.
1709 * @param tc the scheduler task context
1712 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1716 * Callback which will be called to after a host registration succeeded or failed
1718 * @param cls the closure
1719 * @param emsg the error message; NULL if host registration is successful
1722 host_registration_completion (void *cls, const char *emsg)
1727 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1728 _("Host registration failed for a host. Error: %s\n"), emsg);
1729 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1730 GNUNET_SCHEDULER_cancel (abort_task);
1731 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1734 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1739 * Task to register all hosts available in the global host list.
1742 * @param tc the scheduler task context
1745 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1747 struct DLLOperation *dll_op;
1748 static unsigned int reg_host;
1751 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1752 if (reg_host == num_hosts - 1)
1754 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1755 "All hosts successfully registered\n");
1757 state = STATE_SLAVES_STARTING;
1758 for (slave = 1; slave < num_hosts; slave++)
1760 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1761 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
1767 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1771 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
1772 host_registration_completion,
1778 * Callback to signal successfull startup of the controller process.
1780 * @param cls the closure from GNUNET_TESTBED_controller_start()
1781 * @param config the configuration with which the controller has been started;
1782 * NULL if status is not GNUNET_OK
1783 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1784 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1787 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
1789 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1790 GNUNET_SCHEDULER_cancel (abort_task);
1791 if (GNUNET_OK != status)
1794 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Oh, dear!\n");
1795 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1799 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1800 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1801 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1802 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1803 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1804 mc = GNUNET_TESTBED_controller_connect (hosts[0], event_mask,
1805 &controller_event_cb, NULL);
1808 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1809 _("Unable to connect to master controller -- Check config\n"));
1810 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1813 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1814 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1815 &do_abort, (void*) __LINE__);
1820 * Load search strings from given filename. One search string per line.
1822 * @param filename filename of the file containing the search strings.
1823 * @param strings set of strings loaded from file. Caller needs to free this
1824 * if number returned is greater than zero.
1825 * @param limit upper limit on the number of strings read from the file
1826 * @return number of strings found in the file. GNUNET_SYSERR on error.
1829 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1834 unsigned int offset;
1838 if (NULL == filename)
1840 return GNUNET_SYSERR;
1843 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1845 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1846 "Could not find search strings file %s\n", filename);
1847 return GNUNET_SYSERR;
1849 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1853 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1854 return GNUNET_SYSERR;
1856 data = GNUNET_malloc (filesize);
1857 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1860 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1862 return GNUNET_SYSERR;
1867 while (offset < (filesize - 1) && str_cnt < limit)
1870 if (((data[offset] == '\n')) && (buf != &data[offset]))
1872 data[offset] = '\0';
1874 buf = &data[offset + 1];
1876 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1877 buf = &data[offset + 1];
1879 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1881 for (i = 0; i < str_cnt; i++)
1883 GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1884 offset += strlen (&data[offset]) + 1;
1892 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
1893 * inform whether the given host is habitable or not. The Handle returned by
1894 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
1897 * @param host the host whose status is being reported; will be NULL if the host
1898 * given to GNUNET_TESTBED_is_host_habitable() is NULL
1899 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
1902 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host, int status)
1904 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handle = cls;
1905 static unsigned int hosts_checked;
1908 if (GNUNET_NO == status)
1910 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1911 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1912 GNUNET_TESTBED_host_get_hostname (host));
1914 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Testbed cannot be started on localhost\n"));
1915 GNUNET_SCHEDULER_cancel (abort_task);
1916 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1920 /* printf (_("\rChecked %u hosts"), hosts_checked); */
1921 /* fflush (stdout); */
1922 if (hosts_checked < num_hosts)
1924 /* printf (_("\nAll hosts can start testbed. Creating peers\n")); */
1925 GNUNET_free (hc_handles);
1928 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
1937 * Main function that will be run by the scheduler.
1939 * @param cls closure
1940 * @param args remaining command-line arguments
1941 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1942 * @param config configuration
1945 run (void *cls, char *const *args, const char *cfgfile,
1946 const struct GNUNET_CONFIGURATION_Handle *config)
1949 unsigned int nsearchstrs;
1951 if (NULL == args[0])
1953 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1954 _("No hosts-file specified on command line. Exiting.\n"));
1957 if (NULL == args[1])
1959 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1960 _("No policy directory specified on command line. Exiting.\n"));
1963 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], config, &hosts);
1966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1967 _("No hosts loaded. Need at least one host\n"));
1970 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1971 _("Checking whether given hosts can start testbed."
1973 hc_handles = GNUNET_malloc (sizeof (struct
1974 GNUNET_TESTBED_HostHabitableCheckHandle *)
1976 for (nhost = 0; nhost < num_hosts; nhost++)
1978 hc_handles[nhost] = GNUNET_TESTBED_is_host_habitable (hosts[nhost], config,
1980 &hc_handles[nhost]);
1981 if (NULL == hc_handles[nhost])
1986 for (i = 0; i <= nhost; i++)
1987 if (NULL != hc_handles[i])
1988 GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[i]);
1989 GNUNET_free (hc_handles);
1994 if (num_hosts != nhost)
1996 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Exiting\n"));
1997 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2002 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2003 _("No configuration file given. Exiting\n"));
2004 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2009 GNUNET_CONFIGURATION_get_value_string (config, "REGEXPROFILER", "REGEX_PREFIX",
2012 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2013 _("Configuration option \"regex_prefix\" missing. Exiting\n"));
2014 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2018 if ( (NULL != data_filename) &&
2019 (NULL == (data_file =
2020 GNUNET_DISK_file_open (data_filename,
2021 GNUNET_DISK_OPEN_READWRITE |
2022 GNUNET_DISK_OPEN_TRUNCATE |
2023 GNUNET_DISK_OPEN_CREATE,
2024 GNUNET_DISK_PERM_USER_READ |
2025 GNUNET_DISK_PERM_USER_WRITE))) )
2026 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2029 if (GNUNET_YES != GNUNET_DISK_directory_test (args[1], GNUNET_YES))
2031 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2032 _("Specified policies directory does not exist. Exiting.\n"));
2033 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2036 policy_dir = args[1];
2037 if (GNUNET_YES != GNUNET_DISK_file_test (args[2]))
2039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2040 _("No search strings file given. Exiting.\n"));
2041 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2044 nsearchstrs = load_search_strings (args[2], &search_strings, num_search_strings);
2045 if (num_search_strings != nsearchstrs)
2047 num_search_strings = nsearchstrs;
2048 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2049 _("Error loading search strings."
2050 "Given file does not contain enough strings. Exiting.\n"));
2051 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2054 if (0 >= num_search_strings || NULL == search_strings)
2056 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2057 _("Error loading search strings. Exiting.\n"));
2058 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2061 cfg = GNUNET_CONFIGURATION_dup (config);
2063 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
2064 "REANNOUNCE_PERIOD_MAX",
2065 &reannounce_period_max))
2067 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2068 "reannounce_period_max not given. Using 10 minutes.\n");
2069 reannounce_period_max =
2070 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
2073 for (i = 0; i < num_search_strings; i++)
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "search string: %s\n", search_strings[i]);
2076 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2077 (GNUNET_TIME_UNIT_SECONDS, 5), &do_abort,
2085 * @param argc argument count
2086 * @param argv argument values
2087 * @return 0 on success
2090 main (int argc, char *const *argv)
2092 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2093 {'d', "details", "FILENAME",
2094 gettext_noop ("name of the file for writing statistics"),
2095 1, &GNUNET_GETOPT_set_string, &data_filename},
2096 {'n', "num-links", "COUNT",
2097 gettext_noop ("create COUNT number of random links between peers"),
2098 GNUNET_YES, &GNUNET_GETOPT_set_uint, &linking_factor },
2099 {'t', "matching-timeout", "TIMEOUT",
2100 gettext_noop ("wait TIMEOUT before considering a string match as failed"),
2101 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time
2103 {'s', "search-delay", "DELAY",
2104 gettext_noop ("wait DELAY before starting string search"),
2105 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
2106 {'a', "num-search-strings", "COUNT",
2107 gettext_noop ("number of search strings to read from search strings file"),
2108 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
2109 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
2110 gettext_noop ("maximum path compression length"),
2111 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
2112 GNUNET_GETOPT_OPTION_END
2116 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2119 result = GNUNET_SYSERR;
2121 GNUNET_PROGRAM_run (argc, argv,
2122 "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
2123 _("Profiler for regex"),
2124 options, &run, NULL);
2126 if (GNUNET_OK != ret)
2128 if (GNUNET_OK != result)