2 This file is part of GNUnet.
3 Copyright (C) 2011 - 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file regex/gnunet-regex-profiler.c
21 * @brief Regex profiler for testing distributed regex use.
22 * @author Bartlomiej Polot
23 * @author Maximilian Szengel
30 #include "gnunet_applications.h"
31 #include "gnunet_util_lib.h"
32 #include "regex_internal_lib.h"
33 #include "gnunet_arm_service.h"
34 #include "gnunet_dht_service.h"
35 #include "gnunet_testbed_service.h"
37 #define FIND_TIMEOUT \
38 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
46 * The testbed operation handle
48 struct GNUNET_TESTBED_Operation *op;
56 * The next pointer for DLL
58 struct DLLOperation *next;
61 * The prev pointer for DLL
63 struct DLLOperation *prev;
68 * Available states during profiling
80 STATE_SLAVES_STARTING,
98 * Matching strings against announced regexes
103 * Destroying peers; we can do this as the controller takes care of stopping a
104 * peer if it is running
106 STATE_PEERS_DESTROYING
121 * Peer configuration handle.
123 struct GNUNET_CONFIGURATION_Handle *cfg;
126 * The actual testbed peer handle.
128 struct GNUNET_TESTBED_Peer *peer_handle;
131 * Peer's search string.
133 const char *search_str;
136 * Set to GNUNET_YES if the peer successfully matched the above
137 * search string. GNUNET_NO if the string could not be matched
138 * during the profiler run. GNUNET_SYSERR if the string matching
139 * timed out. Undefined if search_str is NULL
141 int search_str_matched;
146 struct GNUNET_DHT_Handle *dht_handle;
149 * Handle to a running regex search.
151 struct REGEX_INTERNAL_Search *search_handle;
154 * Testbed operation handle for DHT.
156 struct GNUNET_TESTBED_Operation *op_handle;
159 * Peers's statistics handle.
161 struct GNUNET_STATISTICS_Handle *stats_handle;
164 * The starting time of a profiling step.
166 struct GNUNET_TIME_Absolute prof_start_time;
171 struct GNUNET_SCHEDULER_Task * timeout;
176 struct GNUNET_TESTBED_Operation *daemon_op;
180 * Set when shutting down to avoid making more queries.
182 static int in_shutdown;
185 * The array of peers; we fill this as the peers are given to us by the testbed
187 static struct RegexPeer *peers;
190 * Host registration handle
192 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
195 * Handle to the master controller process
197 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
200 * Handle to the master controller
202 static struct GNUNET_TESTBED_Controller *mc;
205 * Handle to global configuration
207 static struct GNUNET_CONFIGURATION_Handle *cfg;
210 * Abort task identifier
212 static struct GNUNET_SCHEDULER_Task * abort_task;
215 * Host registration task identifier
217 static struct GNUNET_SCHEDULER_Task * register_hosts_task;
220 * Global event mask for all testbed events
222 static uint64_t event_mask;
225 * The starting time of a profiling step
227 static struct GNUNET_TIME_Absolute prof_start_time;
230 * Duration profiling step has taken
232 static struct GNUNET_TIME_Relative prof_time;
235 * Number of peers to be started by the profiler
237 static unsigned int num_peers;
240 * Global testing status
245 * current state of profiling
250 * Folder where policy files are stored.
252 static char * policy_dir;
255 * File with hostnames where to execute the test.
257 static char *hosts_file;
260 * File with the strings to look for.
262 static char *strings_file;
265 * Search strings (num_peers of them).
267 static char **search_strings;
270 * How many searches are we going to start in parallel
272 static long long unsigned int init_parallel_searches;
275 * How many searches are running in parallel
277 static unsigned int parallel_searches;
280 * Number of strings found in the published regexes.
282 static unsigned int strings_found;
285 * Index of peer to start next announce/search.
287 static unsigned int next_search;
290 * Search timeout task identifier.
292 static struct GNUNET_SCHEDULER_Task * search_timeout_task;
295 * Search timeout in seconds.
297 static struct GNUNET_TIME_Relative search_timeout_time = { 60000 };
300 * File to log statistics to.
302 static struct GNUNET_DISK_FileHandle *data_file;
305 * Filename to log statistics to.
307 static char *data_filename;
310 * Prefix used for regex announcing. We need to prefix the search
311 * strings with it, in order to find something.
313 static char * regex_prefix;
316 * What's the maximum regex reannounce period.
318 static struct GNUNET_TIME_Relative reannounce_period_max;
321 /******************************************************************************/
322 /****************************** DECLARATIONS ********************************/
323 /******************************************************************************/
326 * DHT connect callback.
328 * @param cls internal peer id.
329 * @param op operation handle.
330 * @param ca_result connect adapter result.
331 * @param emsg error message.
334 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
335 void *ca_result, const char *emsg);
338 * DHT connect adapter.
340 * @param cls not used.
341 * @param cfg configuration handle.
346 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
350 * Adapter function called to destroy a connection to
354 * @param op_result service handle returned from the connect adapter
357 dht_da (void *cls, void *op_result);
361 * Function called by testbed once we are connected to stats
362 * service. Get the statistics for the services of interest.
364 * @param cls the 'struct RegexPeer' for which we connected to stats
365 * @param op connect operation handle
366 * @param ca_result handle to stats service
367 * @param emsg error message on failure
370 stats_connect_cb (void *cls,
371 struct GNUNET_TESTBED_Operation *op,
377 * Start announcing the next regex in the DHT.
379 * @param cls Index of the next peer in the peers array.
382 announce_next_regex (void *cls);
385 /******************************************************************************/
386 /******************************** SHUTDOWN **********************************/
387 /******************************************************************************/
396 do_shutdown (void *cls)
398 struct RegexPeer *peer;
399 unsigned int peer_cnt;
400 unsigned int search_str_cnt;
401 char output_buffer[512];
404 if (NULL != abort_task)
406 GNUNET_SCHEDULER_cancel (abort_task);
409 if (NULL != register_hosts_task)
411 GNUNET_SCHEDULER_cancel (register_hosts_task);
412 register_hosts_task = NULL;
414 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
416 peer = &peers[peer_cnt];
418 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
420 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
422 GNUNET_snprintf (output_buffer,
423 sizeof (output_buffer),
424 "%p Search string not found: %s (%d)\n"
425 "%p On peer: %u (%p)\n"
427 peer, peer->search_str, peer->search_str_matched,
428 peer, peer->id, peer,
430 GNUNET_STRINGS_relative_time_to_string (prof_time,
432 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
436 if (NULL != peers[peer_cnt].op_handle)
437 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
440 if (NULL != data_file)
442 GNUNET_DISK_file_close (data_file);
445 for (search_str_cnt = 0;
446 search_str_cnt < num_peers && NULL != search_strings;
449 GNUNET_free_non_null (search_strings[search_str_cnt]);
451 GNUNET_free_non_null (search_strings);
452 search_strings = NULL;
454 if (NULL != reg_handle)
456 GNUNET_TESTBED_cancel_registration (reg_handle);
461 GNUNET_TESTBED_controller_disconnect (mc);
466 GNUNET_TESTBED_controller_stop (mc_proc);
471 GNUNET_CONFIGURATION_destroy (cfg);
478 * abort task to run on test timed out
485 unsigned long i = (unsigned long) cls;
487 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
488 "Aborting from line %lu...\n", i);
490 result = GNUNET_SYSERR;
491 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
495 /******************************************************************************/
496 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
497 /******************************************************************************/
500 * Adapter function called to establish a connection to
501 * statistics service.
504 * @param cfg configuration of the peer to connect to; will be available until
505 * GNUNET_TESTBED_operation_done() is called on the operation returned
506 * from GNUNET_TESTBED_service_connect()
507 * @return service handle to return in 'op_result', NULL on error
511 const struct GNUNET_CONFIGURATION_Handle *cfg)
513 return GNUNET_STATISTICS_create ("<driver>", cfg);
518 * Adapter function called to destroy a connection to
519 * statistics service.
522 * @param op_result service handle returned from the connect adapter
525 stats_da (void *cls, void *op_result)
527 struct RegexPeer *peer = cls;
529 GNUNET_assert (op_result == peer->stats_handle);
531 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
532 peer->stats_handle = NULL;
537 * Process statistic values. Write all values to global 'data_file', if present.
540 * @param subsystem name of subsystem that created the statistic
541 * @param name the name of the datum
542 * @param value the current value
543 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
544 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
547 stats_iterator (void *cls,
548 const char *subsystem,
550 uint64_t value, int is_persistent)
552 struct RegexPeer *peer = cls;
553 char output_buffer[512];
556 if (NULL == data_file)
558 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
559 "%p -> %s [%s]: %llu\n",
563 (unsigned long long) value);
567 GNUNET_snprintf (output_buffer,
568 sizeof (output_buffer),
571 subsystem, value, name);
572 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
573 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
574 "Unable to write to file!\n");
581 * Stats callback. Finish the stats testbed operation and when all stats have
582 * been iterated, shutdown the profiler.
585 * @param success GNUNET_OK if statistics were
586 * successfully obtained, GNUNET_SYSERR if not.
592 static unsigned int peer_cnt;
593 struct RegexPeer *peer = cls;
595 if (GNUNET_OK != success)
597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
598 "Getting statistics for peer %u failed!\n",
603 GNUNET_assert (NULL != peer->op_handle);
605 GNUNET_TESTBED_operation_done (peer->op_handle);
606 peer->op_handle = NULL;
609 peer = &peers[peer_cnt];
611 fprintf (stderr, "s");
612 if (peer_cnt == num_peers)
614 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
615 "\nCollecting stats finished. Shutting down.\n");
616 GNUNET_SCHEDULER_shutdown ();
622 GNUNET_TESTBED_service_connect (NULL,
635 * Function called by testbed once we are connected to stats
636 * service. Get the statistics for the services of interest.
638 * @param cls the 'struct RegexPeer' for which we connected to stats
639 * @param op connect operation handle
640 * @param ca_result handle to stats service
641 * @param emsg error message on failure
644 stats_connect_cb (void *cls,
645 struct GNUNET_TESTBED_Operation *op,
649 struct RegexPeer *peer = cls;
651 if (NULL == ca_result || NULL != emsg)
653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
654 "Failed to connect to statistics service on peer %u: %s\n",
657 peer->stats_handle = NULL;
661 peer->stats_handle = ca_result;
663 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
665 &stats_iterator, peer))
667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
668 "Could not get statistics of peer %u!\n", peer->id);
674 * Task to collect all statistics from all peers, will shutdown the
675 * profiler, when done.
680 do_collect_stats (void *cls)
682 struct RegexPeer *peer = &peers[0];
684 GNUNET_assert (NULL != peer->peer_handle);
687 GNUNET_TESTBED_service_connect (NULL,
698 /******************************************************************************/
699 /************************ REGEX FIND CONNECTIONS **************************/
700 /******************************************************************************/
704 * Start searching for the next string in the DHT.
706 * @param cls Index of the next peer in the peers array.
709 find_string (void *cls);
713 * Method called when we've found a peer that announced a regex
714 * that matches our search string. Now get the statistics.
716 * @param cls Closure provided in REGEX_INTERNAL_search.
717 * @param id Peer providing a regex that matches the string.
718 * @param get_path Path of the get request.
719 * @param get_path_length Lenght of get_path.
720 * @param put_path Path of the put request.
721 * @param put_path_length Length of the put_path.
724 regex_found_handler (void *cls,
725 const struct GNUNET_PeerIdentity *id,
726 const struct GNUNET_PeerIdentity *get_path,
727 unsigned int get_path_length,
728 const struct GNUNET_PeerIdentity *put_path,
729 unsigned int put_path_length)
731 struct RegexPeer *peer = cls;
732 char output_buffer[512];
735 if (GNUNET_YES == peer->search_str_matched)
737 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
738 "String %s on peer %u already matched!\n",
739 peer->search_str, peer->id);
746 if (NULL != peer->timeout)
748 GNUNET_SCHEDULER_cancel (peer->timeout);
749 peer->timeout = NULL;
750 if (GNUNET_NO == in_shutdown)
751 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
756 // FIXME not possible right now
757 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
758 "String matching timed out for string %s on peer %u (%i/%i)\n",
759 peer->search_str, peer->id, strings_found, num_peers);
760 peer->search_str_matched = GNUNET_SYSERR;
764 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
766 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
767 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
768 peer->search_str, peer->id,
769 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
770 strings_found, num_peers, parallel_searches);
772 peer->search_str_matched = GNUNET_YES;
774 if (NULL != data_file)
777 GNUNET_snprintf (output_buffer,
778 sizeof (output_buffer),
780 "%p Search string: %s\n"
781 "%p Search duration: %s\n\n",
783 peer, peer->search_str,
785 GNUNET_STRINGS_relative_time_to_string (prof_time,
788 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
789 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
793 GNUNET_TESTBED_operation_done (peer->op_handle);
794 peer->op_handle = NULL;
796 if (strings_found == num_peers)
798 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
799 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
800 "All strings successfully matched in %s\n",
801 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
803 if (NULL != search_timeout_task)
805 GNUNET_SCHEDULER_cancel (search_timeout_task);
806 search_timeout_task = NULL;
809 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats.\n");
810 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
816 * Connect by string timeout task. This will cancel the profiler after the
817 * specified timeout 'search_timeout'.
822 search_timed_out (void *cls)
826 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
827 "Finding matches to all strings did not succeed after %s.\n",
828 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
830 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
831 "Found %i of %i strings\n", strings_found, num_peers);
833 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
834 "Search timed out after %s."
835 "Collecting stats and shutting down.\n",
836 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
839 in_shutdown = GNUNET_YES;
840 for (i = 0; i < num_peers; i++)
842 if (NULL != peers[i].op_handle)
844 GNUNET_TESTBED_operation_done (peers[i].op_handle);
845 peers[i].op_handle = NULL;
848 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
853 * Search timed out. It might still complete in the future,
854 * but we should start another one.
856 * @param cls Index of the next peer in the peers array.
859 find_timed_out (void *cls)
861 struct RegexPeer *p = cls;
864 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
865 "Searching for string \"%s\" on peer %d timed out.\n",
868 if (GNUNET_NO == in_shutdown)
869 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
874 * Start searching for a string in the DHT.
876 * @param cls Index of the next peer in the peers array.
879 find_string (void *cls)
881 unsigned int search_peer = (unsigned int) (long) cls;
883 if ( (search_peer >= num_peers) ||
884 (GNUNET_YES == in_shutdown) )
887 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
888 "Searching for string \"%s\" on peer %d (%u||)\n",
889 peers[search_peer].search_str,
893 peers[search_peer].op_handle =
894 GNUNET_TESTBED_service_connect (NULL,
895 peers[search_peer].peer_handle,
901 &peers[search_peer]);
902 GNUNET_assert (NULL != peers[search_peer].op_handle);
903 peers[search_peer].timeout
904 = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
906 &peers[search_peer]);
911 * Callback called when testbed has started the daemon we asked for.
914 * @param op the operation handle
915 * @param emsg NULL on success; otherwise an error description
918 daemon_started (void *cls,
919 struct GNUNET_TESTBED_Operation *op,
922 struct RegexPeer *peer = (struct RegexPeer *) cls;
923 unsigned long search_peer;
926 GNUNET_TESTBED_operation_done (peer->daemon_op);
927 peer->daemon_op = NULL;
930 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
931 "Failed to start/stop daemon at peer %u: %s\n", peer->id, emsg);
936 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
937 "Deamon %u started successfully\n", peer->id);
940 /* Find a peer to look for a string matching the regex announced */
941 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
943 for (i = 0; peers[search_peer].search_str != NULL; i++)
945 search_peer = (search_peer + 1) % num_peers;
947 GNUNET_assert (0); /* we ran out of peers, must be a bug */
949 peers[search_peer].search_str = search_strings[peer->id];
950 peers[search_peer].search_str_matched = GNUNET_NO;
951 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_saturating_multiply(
952 reannounce_period_max,
955 (void *) search_peer);
960 * Task to start the daemons on each peer so that the regexes are announced
964 * @param tc the task context
967 do_announce (void *cls)
971 if (GNUNET_YES == in_shutdown)
973 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
974 "Starting announce.\n");
975 for (i = 0; i < init_parallel_searches; i++)
977 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
978 " scheduling announce %u\n",
980 (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
986 * Start announcing the next regex in the DHT.
988 * @param cls Closure (unused).
991 announce_next_regex (void *cls)
993 struct RegexPeer *peer;
995 if (GNUNET_YES == in_shutdown)
997 if (next_search >= num_peers)
999 if (strings_found != num_peers)
1001 struct GNUNET_TIME_Relative new_delay;
1002 if (NULL != search_timeout_task)
1003 GNUNET_SCHEDULER_cancel (search_timeout_task);
1004 new_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15);
1005 search_timeout_task = GNUNET_SCHEDULER_add_delayed (new_delay,
1012 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
1013 peer = &peers[next_search];
1015 GNUNET_TESTBED_peer_manage_service (NULL,
1022 parallel_searches++;
1027 * DHT connect callback. Called when we are connected to the dht service for
1028 * the peer in 'cls'. If successfull we connect to the stats service of this
1029 * peer and then try to match the search string of this peer.
1031 * @param cls internal peer id.
1032 * @param op operation handle.
1033 * @param ca_result connect adapter result.
1034 * @param emsg error message.
1037 dht_connect_cb (void *cls,
1038 struct GNUNET_TESTBED_Operation *op,
1042 struct RegexPeer *peer = (struct RegexPeer *) cls;
1044 if (NULL != emsg || NULL == op || NULL == ca_result)
1046 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1050 GNUNET_assert (NULL != peer->dht_handle);
1051 GNUNET_assert (peer->op_handle == op);
1052 GNUNET_assert (peer->dht_handle == ca_result);
1054 peer->search_str_matched = GNUNET_NO;
1055 peer->search_handle = REGEX_INTERNAL_search (peer->dht_handle,
1057 ®ex_found_handler, peer,
1059 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1064 * DHT connect adapter. Opens a connection to the dht service.
1066 * @param cls Closure (peer).
1067 * @param cfg Configuration handle.
1072 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1074 struct RegexPeer *peer = cls;
1076 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1078 return peer->dht_handle;
1083 * Adapter function called to destroy a connection to the dht service.
1085 * @param cls Closure (peer).
1086 * @param op_result Service handle returned from the connect adapter.
1089 dht_da (void *cls, void *op_result)
1091 struct RegexPeer *peer = (struct RegexPeer *) cls;
1093 GNUNET_assert (peer->dht_handle == op_result);
1095 if (NULL != peer->search_handle)
1097 REGEX_INTERNAL_search_cancel (peer->search_handle);
1098 peer->search_handle = NULL;
1101 if (NULL != peer->dht_handle)
1103 GNUNET_DHT_disconnect (peer->dht_handle);
1104 peer->dht_handle = NULL;
1110 * Signature of a main function for a testcase.
1113 * @param h the run handle
1114 * @param num_peers_ number of peers in 'peers'
1115 * @param testbed_peers handle to peers run in the testbed. NULL upon timeout (see
1116 * GNUNET_TESTBED_test_run()).
1117 * @param links_succeeded the number of overlay link connection attempts that
1119 * @param links_failed the number of overlay link connection attempts that
1123 test_master (void *cls,
1124 struct GNUNET_TESTBED_RunHandle *h,
1125 unsigned int num_peers_,
1126 struct GNUNET_TESTBED_Peer **testbed_peers,
1127 unsigned int links_succeeded,
1128 unsigned int links_failed)
1132 GNUNET_assert (num_peers_ == num_peers);
1134 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1135 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1136 "Testbed started in %s\n",
1137 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1139 if (NULL != abort_task)
1141 GNUNET_SCHEDULER_cancel (abort_task);
1145 for (i = 0; i < num_peers; i++)
1147 peers[i].peer_handle = testbed_peers[i];
1150 GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT", "DISABLE_TRY_CONNECT"))
1152 struct GNUNET_TIME_Relative settle_time;
1155 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1157 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1158 "Waiting for DHT for %s to settle new connections.\n\n",
1159 GNUNET_STRINGS_relative_time_to_string(settle_time, GNUNET_NO));
1160 GNUNET_SCHEDULER_add_delayed (settle_time, &do_announce, NULL);
1164 GNUNET_SCHEDULER_add_now (&do_announce, NULL);
1166 search_timeout_task =
1167 GNUNET_SCHEDULER_add_delayed (search_timeout_time, &search_timed_out, NULL);
1171 * Function that will be called whenever something in the testbed changes.
1173 * @param cls closure, NULL
1174 * @param event information on what is happening
1177 master_controller_cb (void *cls,
1178 const struct GNUNET_TESTBED_EventInformation *event)
1180 switch (event->type)
1182 case GNUNET_TESTBED_ET_CONNECT:
1185 case GNUNET_TESTBED_ET_PEER_START:
1195 /******************************************************************************/
1196 /*************************** TESTBED PEER SETUP *****************************/
1197 /******************************************************************************/
1200 * Process the text buffer counting the non-empty lines and separating them
1201 * with NULL characters, for later ease of copy using (as)printf.
1203 * @param data Memory buffer with strings.
1204 * @param data_size Size of the @a data buffer in bytes.
1205 * @param str_max Maximum number of strings to return.
1206 * @return Positive number of lines found in the buffer,
1207 * #GNUNET_SYSERR otherwise.
1210 count_and_separate_strings (char *data,
1212 unsigned int str_max)
1214 char *buf; // Keep track of last string to skip blank lines
1215 unsigned int offset;
1216 unsigned int str_cnt;
1221 while ( (offset < (data_size - 1)) && (str_cnt < str_max) )
1224 if ( ((data[offset] == '\n')) &&
1225 (buf != &data[offset]) )
1227 data[offset] = '\0';
1229 buf = &data[offset + 1];
1231 else if ( (data[offset] == '\n') ||
1232 (data[offset] == '\0') )
1233 buf = &data[offset + 1];
1240 * Allocate a string array and fill it with the prefixed strings
1241 * from a pre-processed, NULL-separated memory region.
1243 * @param data Preprocessed memory with strings
1244 * @param data_size Size of the @a data buffer in bytes.
1245 * @param strings Address of the string array to be created.
1246 * Must be freed by caller if function end in success.
1247 * @param str_cnt String count. The @a data buffer should contain
1248 * at least this many NULL-separated strings.
1249 * @return #GNUNET_OK in ase of success, #GNUNET_SYSERR otherwise.
1250 * In case of error @a strings must not be freed.
1253 create_string_array (char *data, uint64_t data_size,
1254 char ***strings, unsigned int str_cnt)
1260 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1262 for (i = 0; i < str_cnt; i++)
1264 len = strlen (&data[offset]);
1265 if (offset + len >= data_size)
1267 GNUNET_free (*strings);
1269 return GNUNET_SYSERR;
1271 if (0 == len) // empty line
1278 GNUNET_asprintf (&(*strings)[i],
1289 * Load search strings from given filename. One search string per line.
1291 * @param filename filename of the file containing the search strings.
1292 * @param strings set of strings loaded from file. Caller needs to free this
1293 * if number returned is greater than zero.
1294 * @param limit upper limit on the number of strings read from the file
1295 * @return number of strings found in the file. #GNUNET_SYSERR on error.
1298 load_search_strings (const char *filename,
1307 if (NULL == filename)
1309 return GNUNET_SYSERR;
1311 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1314 "Could not find search strings file %s\n", filename);
1315 return GNUNET_SYSERR;
1318 GNUNET_DISK_file_size (filename,
1323 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1324 "Search strings file %s cannot be read.\n",
1326 return GNUNET_SYSERR;
1330 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1331 "Search strings file %s is empty.\n",
1333 return GNUNET_SYSERR;
1336 /* Read data into memory */
1337 data = GNUNET_malloc (filesize + 1);
1338 if (filesize != GNUNET_DISK_fn_read (filename,
1343 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1344 "Could not read search strings file %s.\n",
1346 return GNUNET_SYSERR;
1349 /* Process buffer and build array */
1350 str_cnt = count_and_separate_strings (data, filesize, limit);
1351 if (GNUNET_OK != create_string_array (data, filesize, strings, str_cnt))
1353 str_cnt = GNUNET_SYSERR;
1361 * Main function that will be run by the scheduler.
1363 * @param cls closure
1364 * @param args remaining command-line arguments
1365 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1366 * @param config configuration
1371 const char *cfgfile,
1372 const struct GNUNET_CONFIGURATION_Handle *config)
1374 unsigned int nsearchstrs;
1376 struct GNUNET_TIME_Relative abort_time;
1378 in_shutdown = GNUNET_NO;
1383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1384 _("No configuration file given. Exiting\n"));
1385 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1388 cfg = GNUNET_CONFIGURATION_dup (config);
1390 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
1394 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1397 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1401 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
1402 "PARALLEL_SEARCHES",
1403 &init_parallel_searches))
1405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1406 "Configuration option \"PARALLEL_SEARCHES\" missing."
1407 " Using default (%d)\n", 10);
1408 init_parallel_searches = 10;
1411 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
1412 "REANNOUNCE_PERIOD_MAX",
1413 &reannounce_period_max))
1415 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1416 "reannounce_period_max not given. Using 10 minutes.\n");
1417 reannounce_period_max =
1418 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
1421 /* Check arguments */
1422 if (NULL == policy_dir)
1424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1425 _("No policy directory specified on command line. Exiting.\n"));
1428 if (GNUNET_YES != GNUNET_DISK_directory_test (policy_dir, GNUNET_YES))
1430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1431 _("Specified policies directory does not exist. Exiting.\n"));
1432 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1435 if (0 >= (int) (num_peers = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL)))
1437 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1438 _("No files found in `%s'\n"),
1442 GNUNET_CONFIGURATION_set_value_string (cfg, "REGEXPROFILER",
1443 "POLICY_DIR", policy_dir);
1444 if (GNUNET_YES != GNUNET_DISK_file_test (strings_file))
1446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1447 _("No search strings file given. Exiting.\n"));
1448 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1451 nsearchstrs = load_search_strings (strings_file,
1454 if (num_peers != nsearchstrs)
1456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1457 "Error loading search strings.\n");
1458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1459 "File (%s) does not contain enough strings (%u/%u).\n",
1460 strings_file, nsearchstrs, num_peers);
1461 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1464 if ( (0 == num_peers) || (NULL == search_strings))
1466 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1467 _("Error loading search strings. Exiting.\n"));
1468 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1471 for (i = 0; i < num_peers; i++)
1472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1473 "search string: %s\n",
1477 if ( (NULL != data_filename) &&
1478 (NULL == (data_file =
1479 GNUNET_DISK_file_open (data_filename,
1480 GNUNET_DISK_OPEN_READWRITE |
1481 GNUNET_DISK_OPEN_TRUNCATE |
1482 GNUNET_DISK_OPEN_CREATE,
1483 GNUNET_DISK_PERM_USER_READ |
1484 GNUNET_DISK_PERM_USER_WRITE))) )
1486 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1492 /* Initialize peers */
1493 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1494 for (i = 0; i < num_peers; i++)
1497 GNUNET_CONFIGURATION_set_value_number (cfg,
1498 "TESTBED", "OVERLAY_RANDOM_LINKS",
1500 GNUNET_CONFIGURATION_set_value_number (cfg,
1502 (long long unsigned)
1503 (log (num_peers) / log (2.0)));
1505 /* For feedback about the start process activate these and pass master_cb */
1506 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1507 // event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1508 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1509 // event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1510 prof_start_time = GNUNET_TIME_absolute_get ();
1511 GNUNET_TESTBED_run (hosts_file,
1515 &master_controller_cb,
1516 NULL, /* master_controller_cb cls */
1518 NULL); /* test_master cls */
1520 GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
1524 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1525 "SETUP_TIMEOUT not given. Using 15 minutes.\n");
1527 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15);
1529 abort_time = GNUNET_TIME_relative_add (abort_time, GNUNET_TIME_UNIT_MINUTES);
1531 GNUNET_SCHEDULER_add_delayed (abort_time,
1534 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1535 "setup_timeout: %s\n",
1536 GNUNET_STRINGS_relative_time_to_string (abort_time, GNUNET_YES));
1543 * @param argc argument count
1544 * @param argv argument values
1545 * @return 0 on success
1548 main (int argc, char *const *argv)
1550 struct GNUNET_GETOPT_CommandLineOption options[] = {
1552 GNUNET_GETOPT_option_filename ('o',
1555 gettext_noop ("name of the file for writing statistics"),
1558 GNUNET_GETOPT_option_relative_time ('t',
1561 gettext_noop ("wait TIMEOUT before ending the experiment"),
1562 &search_timeout_time),
1564 GNUNET_GETOPT_option_filename ('p',
1567 gettext_noop ("directory with policy files"),
1571 GNUNET_GETOPT_option_filename ('s',
1574 gettext_noop ("name of file with input strings"),
1577 GNUNET_GETOPT_option_filename ('H',
1580 gettext_noop ("name of file with hosts' names"),
1583 GNUNET_GETOPT_OPTION_END
1587 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1589 result = GNUNET_SYSERR;
1591 GNUNET_PROGRAM_run (argc, argv,
1592 "gnunet-regex-profiler",
1593 _("Profiler for regex"),
1594 options, &run, NULL);
1595 if (GNUNET_OK != ret)
1597 if (GNUNET_OK != result)