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 2
48 * The testbed operation handle
50 struct GNUNET_TESTBED_Operation *op;
58 * The next pointer for DLL
60 struct DLLOperation *next;
63 * The prev pointer for DLL
65 struct DLLOperation *prev;
70 * Available states during profiling
82 STATE_SLAVES_STARTING,
100 * Matching strings against announced regexes
105 * Destroying peers; we can do this as the controller takes care of stopping a
106 * peer if it is running
108 STATE_PEERS_DESTROYING
123 * Peer configuration handle.
125 struct GNUNET_CONFIGURATION_Handle *cfg;
128 * The actual testbed peer handle.
130 struct GNUNET_TESTBED_Peer *peer_handle;
133 * Host on which the peer is running.
135 struct GNUNET_TESTBED_Host *host_handle;
138 * Filename of the peer's policy file.
143 * Peers search string.
145 const char *search_str;
148 * Set to GNUNET_YES if the peer successfully matched the above
149 * search string. GNUNET_NO if the string could not be matched
150 * during the profiler run. GNUNET_SYSERR if the string matching
151 * timed out. Undefined if search_str is NULL
153 int search_str_matched;
158 struct GNUNET_ARM_Handle *arm_handle;
163 struct GNUNET_DHT_Handle *dht_handle;
166 * Handle to a running regex search.
168 struct GNUNET_REGEX_search_handle *search_handle;
171 * Testbed operation handle for the ARM and DHT services.
173 struct GNUNET_TESTBED_Operation *op_handle;
176 * Peers's statistics handle.
178 struct GNUNET_STATISTICS_Handle *stats_handle;
181 * Testbed operation handle for the statistics service.
183 struct GNUNET_TESTBED_Operation *stats_op_handle;
186 * The starting time of a profiling step.
188 struct GNUNET_TIME_Absolute prof_start_time;
193 GNUNET_SCHEDULER_TaskIdentifier timeout;
198 * An array of hosts loaded from the hostkeys file
200 static struct GNUNET_TESTBED_Host **hosts;
203 * Array of peer handles used to pass to
204 * GNUNET_TESTBED_overlay_configure_topology
206 static struct GNUNET_TESTBED_Peer **peer_handles;
209 * The array of peers; we fill this as the peers are given to us by the testbed
211 static struct RegexPeer *peers;
214 * Host registration handle
216 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
219 * Handle to the master controller process
221 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
224 * Handle to the master controller
226 static struct GNUNET_TESTBED_Controller *mc;
229 * Handle to global configuration
231 static struct GNUNET_CONFIGURATION_Handle *cfg;
234 * Head of the operations list
236 static struct DLLOperation *dll_op_head;
239 * Tail of the operations list
241 static struct DLLOperation *dll_op_tail;
244 * Peer linking - topology operation
246 static struct GNUNET_TESTBED_Operation *topology_op;
249 * The handle for whether a host is habitable or not
251 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handles;
254 * Abort task identifier
256 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
259 * Shutdown task identifier
261 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
264 * Host registration task identifier
266 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
269 * Global event mask for all testbed events
271 static uint64_t event_mask;
274 * The starting time of a profiling step
276 static struct GNUNET_TIME_Absolute prof_start_time;
279 * Duration profiling step has taken
281 static struct GNUNET_TIME_Relative prof_time;
284 * Number of peers to be started by the profiler
286 static unsigned int num_peers;
289 * Number of hosts in the hosts array
291 static unsigned int num_hosts;
294 * Factor of number of links. num_links = num_peers * linking_factor.
296 static unsigned int linking_factor;
299 * Number of random links to be established between peers
301 static unsigned int num_links;
304 * Number of connect operations that have failed, candidates to retry
306 static unsigned int retry_links;
309 * Global testing status
314 * current state of profiling
319 * Folder where policy files are stored.
321 static char * policy_dir;
326 static char **search_strings;
329 * Number of search strings.
331 static int num_search_strings;
334 * How many searches are running in parallel
336 static unsigned int parallel_searches;
339 * Index of peer/string search.
341 static unsigned int peer_cnt;
344 * Number of peers found with search strings.
346 static unsigned int peers_found;
349 * Search task identifier
351 static GNUNET_SCHEDULER_TaskIdentifier search_task;
354 * Search timeout task identifier.
356 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
359 * Search timeout in seconds.
361 static struct GNUNET_TIME_Relative search_timeout = { 60000 };
364 * How long do we wait before starting the search?
367 static struct GNUNET_TIME_Relative search_delay = { 60000 };
370 * File to log statistics to.
372 static struct GNUNET_DISK_FileHandle *data_file;
375 * Filename to log statistics to.
377 static char *data_filename;
380 * Maximal path compression length.
382 static unsigned int max_path_compression;
385 * Prefix used for regex announcing. We need to prefix the search
386 * strings with it, in order to find something.
388 static char * regex_prefix;
391 /******************************************************************************/
392 /****************************** DECLARATIONS ********************************/
393 /******************************************************************************/
396 * DHT connect callback.
398 * @param cls internal peer id.
399 * @param op operation handle.
400 * @param ca_result connect adapter result.
401 * @param emsg error message.
404 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
405 void *ca_result, const char *emsg);
408 * DHT connect adapter.
410 * @param cls not used.
411 * @param cfg configuration handle.
416 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
420 * Adapter function called to destroy a connection to
424 * @param op_result service handle returned from the connect adapter
427 dht_da (void *cls, void *op_result);
431 * Function called by testbed once we are connected to stats
432 * service. Get the statistics for the services of interest.
434 * @param cls the 'struct RegexPeer' for which we connected to stats
435 * @param op connect operation handle
436 * @param ca_result handle to stats service
437 * @param emsg error message on failure
440 stats_connect_cb (void *cls,
441 struct GNUNET_TESTBED_Operation *op,
447 * Task to collect all statistics from s, will shutdown the
448 * profiler, when done.
451 * @param tc the task context
454 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
457 /******************************************************************************/
458 /******************************** SHUTDOWN **********************************/
459 /******************************************************************************/
466 * @param tc the task context
469 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
471 struct DLLOperation *dll_op;
472 struct RegexPeer *peer;
474 unsigned int peer_cnt;
475 unsigned int search_str_cnt;
476 char output_buffer[512];
479 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
480 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
481 GNUNET_SCHEDULER_cancel (abort_task);
482 if (NULL != hc_handles)
484 for (nhost = 0; nhost < num_hosts; nhost++)
485 if (NULL != hc_handles[nhost])
486 GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[nhost]);
487 GNUNET_free (hc_handles);
490 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
491 GNUNET_SCHEDULER_cancel (register_hosts_task);
493 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
495 peer = &peers[peer_cnt];
497 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
499 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
501 GNUNET_snprintf (output_buffer,
502 sizeof (output_buffer),
503 "%p Search string not found: %s (%d)\n%p On peer: %u (%p)\n%p With policy file: %s\n%p After: %s\n",
504 peer, peer->search_str, peer->search_str_matched,
505 peer, peer->id, peer,
506 peer, peer->policy_file,
508 GNUNET_STRINGS_relative_time_to_string (prof_time,
510 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
511 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
514 if (NULL != peers[peer_cnt].op_handle)
515 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
516 if (NULL != peers[peer_cnt].stats_op_handle)
517 GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
520 if (NULL != data_file)
521 GNUNET_DISK_file_close (data_file);
523 for (search_str_cnt = 0;
524 search_str_cnt < num_search_strings && NULL != search_strings;
527 GNUNET_free_non_null (search_strings[search_str_cnt]);
529 GNUNET_free_non_null (search_strings);
531 if (NULL != reg_handle)
532 GNUNET_TESTBED_cancel_registration (reg_handle);
533 if (NULL != topology_op)
534 GNUNET_TESTBED_operation_done (topology_op);
535 for (nhost = 0; nhost < num_hosts; nhost++)
536 if (NULL != hosts[nhost])
537 GNUNET_TESTBED_host_destroy (hosts[nhost]);
538 GNUNET_free_non_null (hosts);
540 while (NULL != (dll_op = dll_op_head))
542 GNUNET_TESTBED_operation_done (dll_op->op);
543 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
544 GNUNET_free (dll_op);
547 GNUNET_TESTBED_controller_disconnect (mc);
549 GNUNET_TESTBED_controller_stop (mc_proc);
551 GNUNET_CONFIGURATION_destroy (cfg);
553 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
558 * abort task to run on test timed out
561 * @param tc the task context
564 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
566 unsigned long i = (unsigned long) cls;
568 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting %lu...\n", i);
569 abort_task = GNUNET_SCHEDULER_NO_TASK;
570 result = GNUNET_SYSERR;
571 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
572 GNUNET_SCHEDULER_cancel (shutdown_task);
573 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
577 /******************************************************************************/
578 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
579 /******************************************************************************/
582 * Adapter function called to establish a connection to
583 * statistics service.
586 * @param cfg configuration of the peer to connect to; will be available until
587 * GNUNET_TESTBED_operation_done() is called on the operation returned
588 * from GNUNET_TESTBED_service_connect()
589 * @return service handle to return in 'op_result', NULL on error
592 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
594 return GNUNET_STATISTICS_create ("<driver>", cfg);
599 * Adapter function called to destroy a connection to
600 * statistics service.
603 * @param op_result service handle returned from the connect adapter
606 stats_da (void *cls, void *op_result)
608 struct RegexPeer *peer = cls;
610 GNUNET_assert (op_result == peer->stats_handle);
612 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
613 peer->stats_handle = NULL;
618 * Process statistic values. Write all values to global 'data_file', if present.
621 * @param subsystem name of subsystem that created the statistic
622 * @param name the name of the datum
623 * @param value the current value
624 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
625 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
628 stats_iterator (void *cls, const char *subsystem, const char *name,
629 uint64_t value, int is_persistent)
631 struct RegexPeer *peer = cls;
632 char output_buffer[512];
635 if (NULL == data_file)
637 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
638 "%p -> %s [%s]: %llu\n",
639 peer, subsystem, name, value);
643 GNUNET_snprintf (output_buffer,
644 sizeof (output_buffer),
647 subsystem, value, name);
648 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
649 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
656 * Stats callback. Finish the stats testbed operation and when all stats have
657 * been iterated, shutdown the profiler.
660 * @param success GNUNET_OK if statistics were
661 * successfully obtained, GNUNET_SYSERR if not.
667 static unsigned int peer_cnt;
668 struct RegexPeer *peer = cls;
670 if (GNUNET_OK != success)
672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673 "Getting statistics for peer %u failed!\n",
678 GNUNET_assert (NULL != peer->stats_op_handle);
680 GNUNET_TESTBED_operation_done (peer->stats_op_handle);
681 peer->stats_op_handle = NULL;
684 peer = &peers[peer_cnt];
686 if (peer_cnt == num_peers)
688 struct GNUNET_TIME_Relative delay = { 100 };
689 shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
693 peer->stats_op_handle =
694 GNUNET_TESTBED_service_connect (NULL,
707 * Function called by testbed once we are connected to stats
708 * service. Get the statistics for the services of interest.
710 * @param cls the 'struct RegexPeer' for which we connected to stats
711 * @param op connect operation handle
712 * @param ca_result handle to stats service
713 * @param emsg error message on failure
716 stats_connect_cb (void *cls,
717 struct GNUNET_TESTBED_Operation *op,
721 struct RegexPeer *peer = cls;
723 if (NULL == ca_result || NULL != emsg)
725 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
726 "Failed to connect to statistics service on peer %u: %s\n",
729 peer->stats_handle = NULL;
733 peer->stats_handle = ca_result;
735 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
736 GNUNET_TIME_UNIT_FOREVER_REL,
738 &stats_iterator, peer))
740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
741 "Could not get statistics of peer %u!\n", peer->id);
747 * Task to collect all statistics from all peers, will shutdown the
748 * profiler, when done.
751 * @param tc the task context
754 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
756 struct RegexPeer *peer = &peers[0];
758 GNUNET_assert (NULL != peer->peer_handle);
760 peer->stats_op_handle =
761 GNUNET_TESTBED_service_connect (NULL,
772 /******************************************************************************/
773 /************************ REGEX FIND CONNECTIONS **************************/
774 /******************************************************************************/
778 * Start searching for the next string in the DHT.
780 * @param cls Index of the next peer in the peers array.
781 * @param tc TaskContext.
784 find_next_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
788 * Method called when we've found a peer that announced a regex
789 * that matches our search string. Now get the statistics.
791 * @param cls Closure provided in GNUNET_REGEX_search.
792 * @param id Peer providing a regex that matches the string.
793 * @param get_path Path of the get request.
794 * @param get_path_length Lenght of get_path.
795 * @param put_path Path of the put request.
796 * @param put_path_length Length of the put_path.
799 regex_found_handler (void *cls,
800 const struct GNUNET_PeerIdentity *id,
801 const struct GNUNET_PeerIdentity *get_path,
802 unsigned int get_path_length,
803 const struct GNUNET_PeerIdentity *put_path,
804 unsigned int put_path_length)
806 struct RegexPeer *peer = cls;
807 char output_buffer[512];
810 if (GNUNET_YES == peer->search_str_matched)
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
813 "String %s on peer %u already matched!\n",
814 peer->search_str, peer->id);
821 if (GNUNET_SCHEDULER_NO_TASK != peer->timeout)
823 GNUNET_SCHEDULER_cancel (peer->timeout);
824 peer->timeout = GNUNET_SCHEDULER_NO_TASK;
825 GNUNET_SCHEDULER_add_now (&find_next_string, NULL);
830 // FIXME not possible right now
831 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
832 "String matching timed out for string %s on peer %u (%i/%i)\n",
833 peer->search_str, peer->id, peers_found, num_search_strings);
834 peer->search_str_matched = GNUNET_SYSERR;
838 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
840 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
841 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
842 peer->search_str, peer->id,
843 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
844 peers_found, num_search_strings, parallel_searches);
846 peer->search_str_matched = GNUNET_YES;
848 if (NULL != data_file)
851 GNUNET_snprintf (output_buffer,
852 sizeof (output_buffer),
853 "%p Peer: %u\n%p Host: %s\n%p Policy file: %s\n"
854 "%p Search string: %s\n%p Search duration: %s\n\n",
857 GNUNET_TESTBED_host_get_hostname (peer->host_handle),
858 peer, peer->policy_file,
859 peer, peer->search_str,
861 GNUNET_STRINGS_relative_time_to_string (prof_time,
864 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
865 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
869 GNUNET_TESTBED_operation_done (peer->op_handle);
870 peer->op_handle = NULL;
872 if (peers_found == num_search_strings)
874 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
875 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
876 "All strings successfully matched in %s\n",
877 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
879 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
880 GNUNET_SCHEDULER_cancel (search_timeout_task);
882 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
883 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
889 * Connect by string timeout task. This will cancel the profiler after the
890 * specified timeout 'search_timeout'.
893 * @param tc the task context
896 do_connect_by_string_timeout (void *cls,
897 const struct GNUNET_SCHEDULER_TaskContext * tc)
899 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
900 "Finding matches to all strings did not succeed after %s.\n",
901 GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
902 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
903 "Found %i of %i strings\n", peers_found, num_search_strings);
905 printf ("Search timed out after %s. Collecting stats and shutting down.\n",
906 GNUNET_STRINGS_relative_time_to_string (search_timeout, GNUNET_NO));
909 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
914 * Connect by string task that is run to search for a string in the
915 * NFA. It first connects to the mesh service and when a connection is
916 * established it starts to search for the string.
919 * @param tc the task context
922 do_connect_by_string (void *cls,
923 const struct GNUNET_SCHEDULER_TaskContext * tc)
927 printf ("Starting string search.\n");
930 search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout,
931 &do_connect_by_string_timeout, NULL);
932 for (i = 0; i < SEARCHES_IN_PARALLEL; i++)
933 GNUNET_SCHEDULER_add_now (&find_next_string, 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 (&find_next_string, NULL);
962 * Start searching for the next string in the DHT.
964 * @param cls Index of the next peer in the peers array.
965 * @param tc TaskContext.
968 find_next_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_connect (cfg, NULL);
1015 return peer->arm_handle;
1020 * Adapter function called to destroy a connection to the ARM service.
1022 * @param cls Closure (peer).
1023 * @param op_result Service handle returned from the connect adapter.
1026 arm_da (void *cls, void *op_result)
1028 struct RegexPeer *peer = (struct RegexPeer *) cls;
1030 GNUNET_assert (peer->arm_handle == op_result);
1032 if (NULL != peer->arm_handle)
1034 GNUNET_ARM_disconnect (peer->arm_handle);
1035 peer->arm_handle = NULL;
1041 * Start announcing the next regex in the DHT.
1043 * @param cls Index of the next peer in the peers array.
1044 * @param tc TaskContext.
1047 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1051 * Callback function invoked when ARM peration is complete: deamon is started.
1053 * @param cls Closure (RegexPeer).
1054 * @param result Outcome of the operation.
1057 arm_start_cb (void *cls, enum GNUNET_ARM_ProcessStatus result)
1059 struct RegexPeer *peer = (struct RegexPeer *) cls;
1060 static unsigned int peer_cnt;
1061 unsigned int next_p;
1066 * Service is currently being started (due to client request).
1068 case GNUNET_ARM_PROCESS_STARTING:
1069 GNUNET_TESTBED_operation_done (peer->op_handle);
1070 peer->op_handle = NULL;
1072 if (peer_cnt < (num_peers - 1))
1074 next_p = (++peer_cnt % num_peers);
1075 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
1076 GNUNET_TIME_UNIT_MILLISECONDS,
1078 &announce_next_regex,
1079 (void *) (long) next_p);
1083 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1084 "All daemons started."
1085 " Waiting %s to start string searches\n",
1086 GNUNET_STRINGS_relative_time_to_string (search_delay,
1088 GNUNET_SCHEDULER_add_delayed (search_delay,
1089 do_connect_by_string,
1095 * Service name is unknown to ARM.
1097 case GNUNET_ARM_PROCESS_UNKNOWN:
1099 * Service is now down (due to client request).
1101 case GNUNET_ARM_PROCESS_DOWN:
1103 * Service is already running.
1105 case GNUNET_ARM_PROCESS_ALREADY_RUNNING:
1107 * Service is already being stopped by some other client.
1109 case GNUNET_ARM_PROCESS_ALREADY_STOPPING:
1111 * Service is already down (no action taken)
1113 case GNUNET_ARM_PROCESS_ALREADY_DOWN:
1115 * ARM is currently being shut down (no more process starts)
1117 case GNUNET_ARM_PROCESS_SHUTDOWN:
1119 * Error in communication with ARM
1121 case GNUNET_ARM_PROCESS_COMMUNICATION_ERROR:
1123 * Timeout in communication with ARM
1125 case GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT:
1127 * Failure to perform operation
1129 case GNUNET_ARM_PROCESS_FAILURE:
1131 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ARM returned %d\n", result);
1138 * ARM connect callback. Called when we are connected to the arm service for
1139 * the peer in 'cls'. If successfull we start the regex deamon to start
1140 * announcing the regex of this peer.
1142 * @param cls internal peer id.
1143 * @param op operation handle.
1144 * @param ca_result connect adapter result.
1145 * @param emsg error message.
1148 arm_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1149 void *ca_result, const char *emsg)
1151 struct RegexPeer *peer = (struct RegexPeer *) cls;
1153 if (NULL != emsg || NULL == op || NULL == ca_result)
1155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ARM connect failed: %s\n", emsg);
1159 GNUNET_assert (NULL != peer->arm_handle);
1160 GNUNET_assert (peer->op_handle == op);
1161 GNUNET_assert (peer->arm_handle == ca_result);
1163 GNUNET_ARM_start_service (ca_result, "regexprofiler",
1164 GNUNET_OS_INHERIT_STD_NONE,
1165 GNUNET_TIME_UNIT_FOREVER_REL,
1172 * Task to start the daemons on each peer so that the regexes are announced
1176 * @param tc the task context
1179 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1181 printf ("Starting announce.\n");
1184 /* First connect to arm service, then announce. Next
1185 announce will be in arm_connect_cb */
1186 peers[0].op_handle =
1187 GNUNET_TESTBED_service_connect (NULL,
1188 peers[0].peer_handle,
1200 * Start announcing the next regex in the DHT.
1202 * @param cls Index of the next peer in the peers array.
1203 * @param tc TaskContext.
1206 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1208 long next_p = (long) cls;
1210 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1213 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %ld\n", next_p);
1215 peers[next_p].op_handle =
1216 GNUNET_TESTBED_service_connect (NULL,
1217 peers[next_p].peer_handle,
1227 * DHT connect callback. Called when we are connected to the dht service for
1228 * the peer in 'cls'. If successfull we connect to the stats service of this
1229 * peer and then try to match the search string of this peer.
1231 * @param cls internal peer id.
1232 * @param op operation handle.
1233 * @param ca_result connect adapter result.
1234 * @param emsg error message.
1237 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1238 void *ca_result, const char *emsg)
1240 struct RegexPeer *peer = (struct RegexPeer *) cls;
1242 if (NULL != emsg || NULL == op || NULL == ca_result)
1244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1248 GNUNET_assert (NULL != peer->dht_handle);
1249 GNUNET_assert (peer->op_handle == op);
1250 GNUNET_assert (peer->dht_handle == ca_result);
1252 peer->search_str_matched = GNUNET_NO;
1253 peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
1255 ®ex_found_handler, peer,
1257 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1262 * DHT connect adapter. Opens a connection to the dht service.
1264 * @param cls Closure (peer).
1265 * @param cfg Configuration handle.
1270 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1272 struct RegexPeer *peer = cls;
1274 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1276 return peer->dht_handle;
1281 * Adapter function called to destroy a connection to the dht service.
1283 * @param cls Closure (peer).
1284 * @param op_result Service handle returned from the connect adapter.
1287 dht_da (void *cls, void *op_result)
1289 struct RegexPeer *peer = (struct RegexPeer *) cls;
1291 GNUNET_assert (peer->dht_handle == op_result);
1293 if (NULL != peer->search_handle)
1295 GNUNET_REGEX_search_cancel (peer->search_handle);
1296 peer->search_handle = NULL;
1299 if (NULL != peer->dht_handle)
1301 GNUNET_DHT_disconnect (peer->dht_handle);
1302 peer->dht_handle = NULL;
1307 /******************************************************************************/
1308 /*************************** TESTBED PEER SETUP *****************************/
1309 /******************************************************************************/
1313 * Configure the peer overlay topology.
1316 * @param tc the task context
1319 do_configure_topology (void *cls,
1320 const struct GNUNET_SCHEDULER_TaskContext * tc)
1323 if (0 == linking_factor)
1325 num_links = linking_factor * num_peers;
1327 /* num_links = num_peers - 1; */
1328 num_links = linking_factor;
1330 /* Do overlay connect */
1331 prof_start_time = GNUNET_TIME_absolute_get ();
1333 GNUNET_TESTBED_overlay_configure_topology (NULL, num_peers, peer_handles,
1337 GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
1339 GNUNET_TESTBED_TOPOLOGY_RETRY_CNT,
1341 GNUNET_TESTBED_TOPOLOGY_OPTION_END);
1342 if (NULL == topology_op)
1344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1345 "Cannot create topology, op handle was NULL\n");
1352 * Functions of this signature are called when a peer has been successfully
1353 * started or stopped.
1355 * @param cls the closure from GNUNET_TESTBED_peer_start/stop()
1356 * @param emsg NULL on success; otherwise an error description
1359 peer_churn_cb (void *cls, const char *emsg)
1361 struct DLLOperation *dll_op = cls;
1362 struct GNUNET_TESTBED_Operation *op;
1363 static unsigned int started_peers;
1364 unsigned int peer_cnt;
1367 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1368 GNUNET_free (dll_op);
1371 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1372 _("An operation has failed while starting peers: %s\n"), emsg);
1373 GNUNET_TESTBED_operation_done (op);
1374 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1375 GNUNET_SCHEDULER_cancel (abort_task);
1376 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1379 GNUNET_TESTBED_operation_done (op);
1380 if (++started_peers == num_peers)
1382 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1383 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1384 "All peers started successfully in %s\n",
1385 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1388 peer_handles = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Peer *) * num_peers);
1389 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1390 peer_handles[peer_cnt] = peers[peer_cnt].peer_handle;
1392 state = STATE_PEERS_LINKING;
1393 GNUNET_SCHEDULER_add_now (&do_configure_topology, NULL);
1399 * Functions of this signature are called when a peer has been successfully
1402 * @param cls the closure from GNUNET_TESTBED_peer_create()
1403 * @param peer the handle for the created peer; NULL on any error during
1405 * @param emsg NULL if peer is not NULL; else MAY contain the error description
1408 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
1410 struct DLLOperation *dll_op = cls;
1411 struct RegexPeer *peer_ptr;
1412 static unsigned int created_peers;
1413 unsigned int peer_cnt;
1417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1418 _("Creating a peer failed. Error: %s\n"), emsg);
1419 GNUNET_TESTBED_operation_done (dll_op->op);
1420 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1421 GNUNET_free (dll_op);
1422 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1423 GNUNET_SCHEDULER_cancel (abort_task);
1424 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1428 peer_ptr = dll_op->cls;
1429 GNUNET_assert (NULL == peer_ptr->peer_handle);
1430 GNUNET_CONFIGURATION_destroy (peer_ptr->cfg);
1431 peer_ptr->cfg = NULL;
1432 peer_ptr->peer_handle = peer;
1433 GNUNET_TESTBED_operation_done (dll_op->op);
1434 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1435 GNUNET_free (dll_op);
1437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %i created on host %s\n",
1439 GNUNET_TESTBED_host_get_hostname (peer_ptr->host_handle));
1441 if (++created_peers == num_peers)
1443 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1444 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1445 "All peers created successfully in %s\n",
1446 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1447 /* Now peers are to be started */
1448 state = STATE_PEERS_STARTING;
1449 prof_start_time = GNUNET_TIME_absolute_get ();
1450 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1452 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1453 dll_op->op = GNUNET_TESTBED_peer_start (dll_op,
1454 peers[peer_cnt].peer_handle,
1455 &peer_churn_cb, dll_op);
1456 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1463 * Function called with a filename for each file in the policy directory. Create
1464 * a peer for each filename and update the peer's configuration to include the
1465 * max_path_compression specified as a command line argument as well as the
1466 * policy_file for this peer. The gnunet-service-regexprofiler service is
1467 * automatically started on this peer. The service reads the configurration and
1468 * announces the regexes stored in the policy file 'filename'.
1470 * @param cls closure
1471 * @param filename complete filename (absolute path)
1472 * @return GNUNET_OK to continue to iterate,
1473 * GNUNET_SYSERR to abort iteration with error!
1476 policy_filename_cb (void *cls, const char *filename)
1478 static unsigned int peer_cnt;
1479 struct DLLOperation *dll_op;
1480 struct RegexPeer *peer = &peers[peer_cnt];
1482 GNUNET_assert (NULL != peer);
1484 peer->policy_file = GNUNET_strdup (filename);
1486 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Creating peer %i on host %s for policy file %s\n",
1488 GNUNET_TESTBED_host_get_hostname (peer->host_handle),
1491 /* Set configuration options specific for this peer
1492 (max_path_compression and policy_file */
1493 peer->cfg = GNUNET_CONFIGURATION_dup (cfg);
1494 GNUNET_CONFIGURATION_set_value_number (peer->cfg, "REGEXPROFILER",
1495 "MAX_PATH_COMPRESSION",
1496 (unsigned long long)max_path_compression);
1497 GNUNET_CONFIGURATION_set_value_string (peer->cfg, "REGEXPROFILER",
1498 "POLICY_FILE", filename);
1500 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1501 dll_op->cls = &peers[peer_cnt];
1502 dll_op->op = GNUNET_TESTBED_peer_create (mc,
1507 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1516 * Controller event callback.
1519 * @param event the controller event
1522 controller_event_cb (void *cls,
1523 const struct GNUNET_TESTBED_EventInformation *event)
1525 struct DLLOperation *dll_op;
1526 struct GNUNET_TESTBED_Operation *op;
1531 case STATE_SLAVES_STARTING:
1532 switch (event->type)
1534 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1536 static unsigned int slaves_started;
1537 unsigned int peer_cnt;
1539 dll_op = event->details.operation_finished.op_cls;
1540 GNUNET_CONTAINER_DLL_remove (dll_op_head, dll_op_tail, dll_op);
1541 GNUNET_free (dll_op);
1542 op = event->details.operation_finished.operation;
1543 if (NULL != event->details.operation_finished.emsg)
1545 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1546 _("An operation has failed while starting slaves\n"));
1547 GNUNET_TESTBED_operation_done (op);
1548 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1549 GNUNET_SCHEDULER_cancel (abort_task);
1550 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1553 GNUNET_TESTBED_operation_done (op);
1554 /* Proceed to start peers */
1555 if (++slaves_started == num_hosts - 1)
1557 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1558 "All slaves started successfully\n");
1560 state = STATE_PEERS_CREATING;
1561 prof_start_time = GNUNET_TIME_absolute_get ();
1563 if (-1 == (ret = GNUNET_DISK_directory_scan (policy_dir,
1567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1568 _("No files found in `%s'\n"),
1570 GNUNET_SCHEDULER_shutdown ();
1573 num_peers = (unsigned int) ret;
1574 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1576 /* Initialize peers */
1577 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
1579 struct RegexPeer *peer = &peers[peer_cnt];
1580 peer->id = peer_cnt;
1581 peer->policy_file = NULL;
1582 /* Do not start peers on hosts[0] (master controller) */
1583 peer->host_handle = hosts[1 + (peer_cnt % (num_hosts -1))];
1584 peer->dht_handle = NULL;
1585 peer->search_handle = NULL;
1586 peer->stats_handle = NULL;
1587 peer->stats_op_handle = NULL;
1588 peer->search_str = NULL;
1589 peer->search_str_matched = GNUNET_NO;
1592 GNUNET_DISK_directory_scan (policy_dir,
1593 &policy_filename_cb,
1602 case STATE_PEERS_STARTING:
1603 switch (event->type)
1605 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1606 /* Control reaches here when peer start fails */
1607 case GNUNET_TESTBED_ET_PEER_START:
1608 /* we handle peer starts in peer_churn_cb */
1614 case STATE_PEERS_LINKING:
1615 switch (event->type)
1617 static unsigned int established_links;
1618 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
1619 /* Control reaches here when a peer linking operation fails */
1620 if (NULL != event->details.operation_finished.emsg)
1622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1623 _("An operation has failed while linking\n"));
1624 printf ("F%u/%u(%s)",
1625 retry_links + 1, established_links + 1,
1626 event->details.operation_finished.emsg);
1629 /* We do no retries, consider this link as established */
1631 case GNUNET_TESTBED_ET_CONNECT:
1633 char output_buffer[1024];
1636 if (0 == established_links)
1637 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Establishing links .");
1642 if (++established_links == num_links)
1644 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1645 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1646 "\n%u links established in %s\n",
1648 GNUNET_STRINGS_relative_time_to_string (prof_time,
1650 prof_time = GNUNET_TIME_relative_divide(prof_time, num_links);
1651 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1652 "Average of %s per connection\n",
1653 GNUNET_STRINGS_relative_time_to_string (prof_time,
1656 GNUNET_free (peer_handles);
1658 if (NULL != data_file)
1661 GNUNET_snprintf (output_buffer,
1662 sizeof (output_buffer),
1663 "# of peers: %u\n# of links established: %u\n"
1664 "Time to establish links: %s\n"
1665 "Linking failures: %u\n"
1666 "path compression length: %u\n"
1667 "# of search strings: %u\n",
1669 (established_links - retry_links),
1670 GNUNET_STRINGS_relative_time_to_string (prof_time,
1673 max_path_compression,
1674 num_search_strings);
1676 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
1677 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
1680 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1681 "\nWaiting %s before starting to announce.\n",
1682 GNUNET_STRINGS_relative_time_to_string (search_delay,
1684 state = STATE_SEARCH_REGEX;
1685 search_task = GNUNET_SCHEDULER_add_delayed (search_delay,
1686 &do_announce, NULL);
1694 case STATE_SEARCH_REGEX:
1696 /* Handled in service connect callback */
1702 case STATE_PEERS_CREATING:
1703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create peer\n");
1706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1707 "Unexpected controller_cb with state %i!\n", state);
1715 * Task to register all hosts available in the global host list.
1718 * @param tc the scheduler task context
1721 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1725 * Callback which will be called to after a host registration succeeded or failed
1727 * @param cls the closure
1728 * @param emsg the error message; NULL if host registration is successful
1731 host_registration_completion (void *cls, const char *emsg)
1736 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1737 _("Host registration failed for a host. Error: %s\n"), emsg);
1738 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1739 GNUNET_SCHEDULER_cancel (abort_task);
1740 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1743 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1748 * Task to register all hosts available in the global host list.
1751 * @param tc the scheduler task context
1754 register_hosts (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1756 struct DLLOperation *dll_op;
1757 static unsigned int reg_host;
1760 register_hosts_task = GNUNET_SCHEDULER_NO_TASK;
1761 if (reg_host == num_hosts - 1)
1763 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1764 "All hosts successfully registered\n");
1766 state = STATE_SLAVES_STARTING;
1767 for (slave = 1; slave < num_hosts; slave++)
1769 dll_op = GNUNET_malloc (sizeof (struct DLLOperation));
1770 dll_op->op = GNUNET_TESTBED_controller_link (dll_op,
1776 GNUNET_CONTAINER_DLL_insert_tail (dll_op_head, dll_op_tail, dll_op);
1780 reg_handle = GNUNET_TESTBED_register_host (mc, hosts[++reg_host],
1781 host_registration_completion,
1787 * Callback to signal successfull startup of the controller process.
1789 * @param cls the closure from GNUNET_TESTBED_controller_start()
1790 * @param config the configuration with which the controller has been started;
1791 * NULL if status is not GNUNET_OK
1792 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1793 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1796 status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status)
1798 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1799 GNUNET_SCHEDULER_cancel (abort_task);
1800 if (GNUNET_OK != status)
1803 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Oh, dear!\n");
1804 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1808 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1809 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1810 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1811 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1812 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
1813 mc = GNUNET_TESTBED_controller_connect (config, hosts[0], event_mask,
1814 &controller_event_cb, NULL);
1817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1818 _("Unable to connect to master controller -- Check config\n"));
1819 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1822 register_hosts_task = GNUNET_SCHEDULER_add_now (®ister_hosts, NULL);
1823 abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1824 &do_abort, (void*) __LINE__);
1829 * Load search strings from given filename. One search string per line.
1831 * @param filename filename of the file containing the search strings.
1832 * @param strings set of strings loaded from file. Caller needs to free this
1833 * if number returned is greater than zero.
1834 * @param limit upper limit on the number of strings read from the file
1835 * @return number of strings found in the file. GNUNET_SYSERR on error.
1838 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1843 unsigned int offset;
1847 if (NULL == filename)
1849 return GNUNET_SYSERR;
1852 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1855 "Could not find search strings file %s\n", filename);
1856 return GNUNET_SYSERR;
1858 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1862 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1863 return GNUNET_SYSERR;
1865 data = GNUNET_malloc (filesize);
1866 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1869 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1871 return GNUNET_SYSERR;
1876 while (offset < (filesize - 1) && str_cnt < limit)
1879 if (((data[offset] == '\n')) && (buf != &data[offset]))
1881 data[offset] = '\0';
1883 buf = &data[offset + 1];
1885 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1886 buf = &data[offset + 1];
1888 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1890 for (i = 0; i < str_cnt; i++)
1892 GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1893 offset += strlen (&data[offset]) + 1;
1901 * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
1902 * inform whether the given host is habitable or not. The Handle returned by
1903 * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
1906 * @param host the host whose status is being reported; will be NULL if the host
1907 * given to GNUNET_TESTBED_is_host_habitable() is NULL
1908 * @param status GNUNET_YES if it is habitable; GNUNET_NO if not
1911 host_habitable_cb (void *cls, const struct GNUNET_TESTBED_Host *host, int status)
1913 struct GNUNET_TESTBED_HostHabitableCheckHandle **hc_handle = cls;
1914 static unsigned int hosts_checked;
1917 if (GNUNET_NO == status)
1919 if ((NULL != host) && (NULL != GNUNET_TESTBED_host_get_hostname (host)))
1920 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Host %s cannot start testbed\n"),
1921 GNUNET_TESTBED_host_get_hostname (host));
1923 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Testbed cannot be started on localhost\n"));
1924 GNUNET_SCHEDULER_cancel (abort_task);
1925 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, (void*) __LINE__);
1929 /* printf (_("\rChecked %u hosts"), hosts_checked); */
1930 /* fflush (stdout); */
1931 if (hosts_checked < num_hosts)
1933 /* printf (_("\nAll hosts can start testbed. Creating peers\n")); */
1934 GNUNET_free (hc_handles);
1937 GNUNET_TESTBED_controller_start (GNUNET_TESTBED_host_get_hostname
1947 * Main function that will be run by the scheduler.
1949 * @param cls closure
1950 * @param args remaining command-line arguments
1951 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1952 * @param config configuration
1955 run (void *cls, char *const *args, const char *cfgfile,
1956 const struct GNUNET_CONFIGURATION_Handle *config)
1959 unsigned int nsearchstrs;
1961 if (NULL == args[0])
1963 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1964 _("No hosts-file specified on command line. Exiting.\n"));
1967 if (NULL == args[1])
1969 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1970 _("No policy directory specified on command line. Exiting.\n"));
1973 num_hosts = GNUNET_TESTBED_hosts_load_from_file (args[0], config, &hosts);
1976 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1977 _("No hosts loaded. Need at least one host\n"));
1980 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1981 _("Checking whether given hosts can start testbed."
1983 hc_handles = GNUNET_malloc (sizeof (struct
1984 GNUNET_TESTBED_HostHabitableCheckHandle *)
1986 for (nhost = 0; nhost < num_hosts; nhost++)
1988 hc_handles[nhost] = GNUNET_TESTBED_is_host_habitable (hosts[nhost], config,
1990 &hc_handles[nhost]);
1991 if (NULL == hc_handles[nhost])
1996 for (i = 0; i <= nhost; i++)
1997 if (NULL != hc_handles[i])
1998 GNUNET_TESTBED_is_host_habitable_cancel (hc_handles[i]);
1999 GNUNET_free (hc_handles);
2004 if (num_hosts != nhost)
2006 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Exiting\n"));
2007 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2012 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2013 _("No configuration file given. Exiting\n"));
2014 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2019 GNUNET_CONFIGURATION_get_value_string (config, "REGEXPROFILER", "REGEX_PREFIX",
2022 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2023 _("Configuration option \"regex_prefix\" missing. Exiting\n"));
2024 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2028 if ( (NULL != data_filename) &&
2029 (NULL == (data_file =
2030 GNUNET_DISK_file_open (data_filename,
2031 GNUNET_DISK_OPEN_READWRITE |
2032 GNUNET_DISK_OPEN_TRUNCATE |
2033 GNUNET_DISK_OPEN_CREATE,
2034 GNUNET_DISK_PERM_USER_READ |
2035 GNUNET_DISK_PERM_USER_WRITE))) )
2036 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
2039 if (GNUNET_YES != GNUNET_DISK_directory_test (args[1], GNUNET_YES))
2041 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2042 _("Specified policies directory does not exist. Exiting.\n"));
2043 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2046 policy_dir = args[1];
2047 if (GNUNET_YES != GNUNET_DISK_file_test (args[2]))
2049 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2050 _("No search strings file given. Exiting.\n"));
2051 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2054 nsearchstrs = load_search_strings (args[2], &search_strings, num_search_strings);
2055 if (num_search_strings != nsearchstrs)
2057 num_search_strings = nsearchstrs;
2058 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2059 _("Error loading search strings."
2060 "Given file does not contain enough strings. Exiting.\n"));
2061 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2064 if (0 >= num_search_strings || NULL == search_strings)
2066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2067 _("Error loading search strings. Exiting.\n"));
2068 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
2072 for (i = 0; i < num_search_strings; i++)
2073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "search string: %s\n", search_strings[i]);
2074 cfg = GNUNET_CONFIGURATION_dup (config);
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 },
2102 {'s', "search-delay", "DELAY",
2103 gettext_noop ("wait DELAY before starting string search"),
2104 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
2105 {'a', "num-search-strings", "COUNT",
2106 gettext_noop ("number of search strings to read from search strings file"),
2107 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
2108 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
2109 gettext_noop ("maximum path compression length"),
2110 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
2111 GNUNET_GETOPT_OPTION_END
2115 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2118 result = GNUNET_SYSERR;
2120 GNUNET_PROGRAM_run (argc, argv,
2121 "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
2122 _("Profiler for regex"),
2123 options, &run, NULL);
2125 if (GNUNET_OK != ret)
2127 if (GNUNET_OK != result)