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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 "regex_internal_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)
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 * Peer's search string.
135 const char *search_str;
138 * Set to GNUNET_YES if the peer successfully matched the above
139 * search string. GNUNET_NO if the string could not be matched
140 * during the profiler run. GNUNET_SYSERR if the string matching
141 * timed out. Undefined if search_str is NULL
143 int search_str_matched;
148 struct GNUNET_DHT_Handle *dht_handle;
151 * Handle to a running regex search.
153 struct REGEX_INTERNAL_Search *search_handle;
156 * Testbed operation handle for DHT.
158 struct GNUNET_TESTBED_Operation *op_handle;
161 * Peers's statistics handle.
163 struct GNUNET_STATISTICS_Handle *stats_handle;
166 * The starting time of a profiling step.
168 struct GNUNET_TIME_Absolute prof_start_time;
173 struct GNUNET_SCHEDULER_Task * timeout;
178 struct GNUNET_TESTBED_Operation *daemon_op;
182 * Set when shutting down to avoid making more queries.
184 static int in_shutdown;
187 * The array of peers; we fill this as the peers are given to us by the testbed
189 static struct RegexPeer *peers;
192 * Host registration handle
194 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
197 * Handle to the master controller process
199 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
202 * Handle to the master controller
204 static struct GNUNET_TESTBED_Controller *mc;
207 * Handle to global configuration
209 static struct GNUNET_CONFIGURATION_Handle *cfg;
212 * Abort task identifier
214 static struct GNUNET_SCHEDULER_Task * abort_task;
217 * Host registration task identifier
219 static struct GNUNET_SCHEDULER_Task * register_hosts_task;
222 * Global event mask for all testbed events
224 static uint64_t event_mask;
227 * The starting time of a profiling step
229 static struct GNUNET_TIME_Absolute prof_start_time;
232 * Duration profiling step has taken
234 static struct GNUNET_TIME_Relative prof_time;
237 * Number of peers to be started by the profiler
239 static unsigned int num_peers;
242 * Global testing status
247 * current state of profiling
252 * Folder where policy files are stored.
254 static char * policy_dir;
257 * File with hostnames where to execute the test.
259 static char *hosts_file;
262 * File with the strings to look for.
264 static char *strings_file;
267 * Search strings (num_peers of them).
269 static char **search_strings;
272 * How many searches are we going to start in parallel
274 static long long unsigned int init_parallel_searches;
277 * How many searches are running in parallel
279 static unsigned int parallel_searches;
282 * Number of strings found in the published regexes.
284 static unsigned int strings_found;
287 * Index of peer to start next announce/search.
289 static unsigned int next_search;
292 * Search timeout task identifier.
294 static struct GNUNET_SCHEDULER_Task * search_timeout_task;
297 * Search timeout in seconds.
299 static struct GNUNET_TIME_Relative search_timeout_time = { 60000 };
302 * File to log statistics to.
304 static struct GNUNET_DISK_FileHandle *data_file;
307 * Filename to log statistics to.
309 static char *data_filename;
312 * Prefix used for regex announcing. We need to prefix the search
313 * strings with it, in order to find something.
315 static char * regex_prefix;
318 * What's the maximum regex reannounce period.
320 static struct GNUNET_TIME_Relative reannounce_period_max;
323 /******************************************************************************/
324 /****************************** DECLARATIONS ********************************/
325 /******************************************************************************/
328 * DHT connect callback.
330 * @param cls internal peer id.
331 * @param op operation handle.
332 * @param ca_result connect adapter result.
333 * @param emsg error message.
336 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
337 void *ca_result, const char *emsg);
340 * DHT connect adapter.
342 * @param cls not used.
343 * @param cfg configuration handle.
348 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
352 * Adapter function called to destroy a connection to
356 * @param op_result service handle returned from the connect adapter
359 dht_da (void *cls, void *op_result);
363 * Function called by testbed once we are connected to stats
364 * service. Get the statistics for the services of interest.
366 * @param cls the 'struct RegexPeer' for which we connected to stats
367 * @param op connect operation handle
368 * @param ca_result handle to stats service
369 * @param emsg error message on failure
372 stats_connect_cb (void *cls,
373 struct GNUNET_TESTBED_Operation *op,
379 * Start announcing the next regex in the DHT.
381 * @param cls Index of the next peer in the peers array.
384 announce_next_regex (void *cls);
387 /******************************************************************************/
388 /******************************** SHUTDOWN **********************************/
389 /******************************************************************************/
398 do_shutdown (void *cls)
400 struct RegexPeer *peer;
401 unsigned int peer_cnt;
402 unsigned int search_str_cnt;
403 char output_buffer[512];
406 if (NULL != abort_task)
408 GNUNET_SCHEDULER_cancel (abort_task);
411 if (NULL != register_hosts_task)
413 GNUNET_SCHEDULER_cancel (register_hosts_task);
414 register_hosts_task = NULL;
416 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
418 peer = &peers[peer_cnt];
420 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
422 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
424 GNUNET_snprintf (output_buffer,
425 sizeof (output_buffer),
426 "%p Search string not found: %s (%d)\n"
427 "%p On peer: %u (%p)\n"
429 peer, peer->search_str, peer->search_str_matched,
430 peer, peer->id, peer,
432 GNUNET_STRINGS_relative_time_to_string (prof_time,
434 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
438 if (NULL != peers[peer_cnt].op_handle)
439 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
442 if (NULL != data_file)
444 GNUNET_DISK_file_close (data_file);
447 for (search_str_cnt = 0;
448 search_str_cnt < num_peers && NULL != search_strings;
451 GNUNET_free_non_null (search_strings[search_str_cnt]);
453 GNUNET_free_non_null (search_strings);
454 search_strings = NULL;
456 if (NULL != reg_handle)
458 GNUNET_TESTBED_cancel_registration (reg_handle);
463 GNUNET_TESTBED_controller_disconnect (mc);
468 GNUNET_TESTBED_controller_stop (mc_proc);
473 GNUNET_CONFIGURATION_destroy (cfg);
480 * abort task to run on test timed out
487 unsigned long i = (unsigned long) cls;
489 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
490 "Aborting from line %lu...\n", i);
492 result = GNUNET_SYSERR;
493 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
497 /******************************************************************************/
498 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
499 /******************************************************************************/
502 * Adapter function called to establish a connection to
503 * statistics service.
506 * @param cfg configuration of the peer to connect to; will be available until
507 * GNUNET_TESTBED_operation_done() is called on the operation returned
508 * from GNUNET_TESTBED_service_connect()
509 * @return service handle to return in 'op_result', NULL on error
513 const struct GNUNET_CONFIGURATION_Handle *cfg)
515 return GNUNET_STATISTICS_create ("<driver>", cfg);
520 * Adapter function called to destroy a connection to
521 * statistics service.
524 * @param op_result service handle returned from the connect adapter
527 stats_da (void *cls, void *op_result)
529 struct RegexPeer *peer = cls;
531 GNUNET_assert (op_result == peer->stats_handle);
533 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
534 peer->stats_handle = NULL;
539 * Process statistic values. Write all values to global 'data_file', if present.
542 * @param subsystem name of subsystem that created the statistic
543 * @param name the name of the datum
544 * @param value the current value
545 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
546 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
549 stats_iterator (void *cls,
550 const char *subsystem,
552 uint64_t value, int is_persistent)
554 struct RegexPeer *peer = cls;
555 char output_buffer[512];
558 if (NULL == data_file)
560 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
561 "%p -> %s [%s]: %llu\n",
565 (unsigned long long) value);
569 GNUNET_snprintf (output_buffer,
570 sizeof (output_buffer),
573 subsystem, value, name);
574 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
576 "Unable to write to file!\n");
583 * Stats callback. Finish the stats testbed operation and when all stats have
584 * been iterated, shutdown the profiler.
587 * @param success GNUNET_OK if statistics were
588 * successfully obtained, GNUNET_SYSERR if not.
594 static unsigned int peer_cnt;
595 struct RegexPeer *peer = cls;
597 if (GNUNET_OK != success)
599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
600 "Getting statistics for peer %u failed!\n",
605 GNUNET_assert (NULL != peer->op_handle);
607 GNUNET_TESTBED_operation_done (peer->op_handle);
608 peer->op_handle = NULL;
611 peer = &peers[peer_cnt];
613 fprintf (stderr, "s");
614 if (peer_cnt == num_peers)
616 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
617 "\nCollecting stats finished. Shutting down.\n");
618 GNUNET_SCHEDULER_shutdown ();
624 GNUNET_TESTBED_service_connect (NULL,
637 * Function called by testbed once we are connected to stats
638 * service. Get the statistics for the services of interest.
640 * @param cls the 'struct RegexPeer' for which we connected to stats
641 * @param op connect operation handle
642 * @param ca_result handle to stats service
643 * @param emsg error message on failure
646 stats_connect_cb (void *cls,
647 struct GNUNET_TESTBED_Operation *op,
651 struct RegexPeer *peer = cls;
653 if (NULL == ca_result || NULL != emsg)
655 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
656 "Failed to connect to statistics service on peer %u: %s\n",
659 peer->stats_handle = NULL;
663 peer->stats_handle = ca_result;
665 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
667 &stats_iterator, peer))
669 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
670 "Could not get statistics of peer %u!\n", peer->id);
676 * Task to collect all statistics from all peers, will shutdown the
677 * profiler, when done.
682 do_collect_stats (void *cls)
684 struct RegexPeer *peer = &peers[0];
686 GNUNET_assert (NULL != peer->peer_handle);
689 GNUNET_TESTBED_service_connect (NULL,
700 /******************************************************************************/
701 /************************ REGEX FIND CONNECTIONS **************************/
702 /******************************************************************************/
706 * Start searching for the next string in the DHT.
708 * @param cls Index of the next peer in the peers array.
711 find_string (void *cls);
715 * Method called when we've found a peer that announced a regex
716 * that matches our search string. Now get the statistics.
718 * @param cls Closure provided in REGEX_INTERNAL_search.
719 * @param id Peer providing a regex that matches the string.
720 * @param get_path Path of the get request.
721 * @param get_path_length Lenght of get_path.
722 * @param put_path Path of the put request.
723 * @param put_path_length Length of the put_path.
726 regex_found_handler (void *cls,
727 const struct GNUNET_PeerIdentity *id,
728 const struct GNUNET_PeerIdentity *get_path,
729 unsigned int get_path_length,
730 const struct GNUNET_PeerIdentity *put_path,
731 unsigned int put_path_length)
733 struct RegexPeer *peer = cls;
734 char output_buffer[512];
737 if (GNUNET_YES == peer->search_str_matched)
739 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
740 "String %s on peer %u already matched!\n",
741 peer->search_str, peer->id);
748 if (NULL != peer->timeout)
750 GNUNET_SCHEDULER_cancel (peer->timeout);
751 peer->timeout = NULL;
752 if (GNUNET_NO == in_shutdown)
753 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
758 // FIXME not possible right now
759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
760 "String matching timed out for string %s on peer %u (%i/%i)\n",
761 peer->search_str, peer->id, strings_found, num_peers);
762 peer->search_str_matched = GNUNET_SYSERR;
766 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
768 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
769 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
770 peer->search_str, peer->id,
771 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
772 strings_found, num_peers, parallel_searches);
774 peer->search_str_matched = GNUNET_YES;
776 if (NULL != data_file)
779 GNUNET_snprintf (output_buffer,
780 sizeof (output_buffer),
782 "%p Search string: %s\n"
783 "%p Search duration: %s\n\n",
785 peer, peer->search_str,
787 GNUNET_STRINGS_relative_time_to_string (prof_time,
790 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
791 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
795 GNUNET_TESTBED_operation_done (peer->op_handle);
796 peer->op_handle = NULL;
798 if (strings_found == num_peers)
800 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
801 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
802 "All strings successfully matched in %s\n",
803 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
805 if (NULL != search_timeout_task)
807 GNUNET_SCHEDULER_cancel (search_timeout_task);
808 search_timeout_task = NULL;
811 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats.\n");
812 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
818 * Connect by string timeout task. This will cancel the profiler after the
819 * specified timeout 'search_timeout'.
824 search_timed_out (void *cls)
828 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
829 "Finding matches to all strings did not succeed after %s.\n",
830 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
832 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
833 "Found %i of %i strings\n", strings_found, num_peers);
835 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
836 "Search timed out after %s."
837 "Collecting stats and shutting down.\n",
838 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
841 in_shutdown = GNUNET_YES;
842 for (i = 0; i < num_peers; i++)
844 if (NULL != peers[i].op_handle)
846 GNUNET_TESTBED_operation_done (peers[i].op_handle);
847 peers[i].op_handle = NULL;
850 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
855 * Search timed out. It might still complete in the future,
856 * but we should start another one.
858 * @param cls Index of the next peer in the peers array.
861 find_timed_out (void *cls)
863 struct RegexPeer *p = cls;
866 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
867 "Searching for string \"%s\" on peer %d timed out.\n",
870 if (GNUNET_NO == in_shutdown)
871 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
876 * Start searching for a string in the DHT.
878 * @param cls Index of the next peer in the peers array.
881 find_string (void *cls)
883 unsigned int search_peer = (unsigned int) (long) cls;
885 if ( (search_peer >= num_peers) ||
886 (GNUNET_YES == in_shutdown) )
889 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
890 "Searching for string \"%s\" on peer %d (%u||)\n",
891 peers[search_peer].search_str,
895 peers[search_peer].op_handle =
896 GNUNET_TESTBED_service_connect (NULL,
897 peers[search_peer].peer_handle,
903 &peers[search_peer]);
904 GNUNET_assert (NULL != peers[search_peer].op_handle);
905 peers[search_peer].timeout
906 = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
908 &peers[search_peer]);
913 * Callback called when testbed has started the daemon we asked for.
916 * @param op the operation handle
917 * @param emsg NULL on success; otherwise an error description
920 daemon_started (void *cls,
921 struct GNUNET_TESTBED_Operation *op,
924 struct RegexPeer *peer = (struct RegexPeer *) cls;
925 unsigned long search_peer;
928 GNUNET_TESTBED_operation_done (peer->daemon_op);
929 peer->daemon_op = NULL;
932 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
933 "Failed to start/stop daemon at peer %u: %s\n", peer->id, emsg);
938 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
939 "Deamon %u started successfully\n", peer->id);
942 /* Find a peer to look for a string matching the regex announced */
943 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
945 for (i = 0; peers[search_peer].search_str != NULL; i++)
947 search_peer = (search_peer + 1) % num_peers;
949 GNUNET_assert (0); /* we ran out of peers, must be a bug */
951 peers[search_peer].search_str = search_strings[peer->id];
952 peers[search_peer].search_str_matched = GNUNET_NO;
953 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_saturating_multiply(
954 reannounce_period_max,
957 (void *) search_peer);
962 * Task to start the daemons on each peer so that the regexes are announced
966 * @param tc the task context
969 do_announce (void *cls)
973 if (GNUNET_YES == in_shutdown)
975 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
976 "Starting announce.\n");
977 for (i = 0; i < init_parallel_searches; i++)
979 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
980 " scheduling announce %u\n",
982 (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
988 * Start announcing the next regex in the DHT.
990 * @param cls Closure (unused).
993 announce_next_regex (void *cls)
995 struct RegexPeer *peer;
997 if (GNUNET_YES == in_shutdown)
999 if (next_search >= num_peers)
1001 if (strings_found != num_peers)
1003 struct GNUNET_TIME_Relative new_delay;
1004 if (NULL != search_timeout_task)
1005 GNUNET_SCHEDULER_cancel (search_timeout_task);
1006 new_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15);
1007 search_timeout_task = GNUNET_SCHEDULER_add_delayed (new_delay,
1014 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
1015 peer = &peers[next_search];
1017 GNUNET_TESTBED_peer_manage_service (NULL,
1024 parallel_searches++;
1029 * DHT connect callback. Called when we are connected to the dht service for
1030 * the peer in 'cls'. If successfull we connect to the stats service of this
1031 * peer and then try to match the search string of this peer.
1033 * @param cls internal peer id.
1034 * @param op operation handle.
1035 * @param ca_result connect adapter result.
1036 * @param emsg error message.
1039 dht_connect_cb (void *cls,
1040 struct GNUNET_TESTBED_Operation *op,
1044 struct RegexPeer *peer = (struct RegexPeer *) cls;
1046 if (NULL != emsg || NULL == op || NULL == ca_result)
1048 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1052 GNUNET_assert (NULL != peer->dht_handle);
1053 GNUNET_assert (peer->op_handle == op);
1054 GNUNET_assert (peer->dht_handle == ca_result);
1056 peer->search_str_matched = GNUNET_NO;
1057 peer->search_handle = REGEX_INTERNAL_search (peer->dht_handle,
1059 ®ex_found_handler, peer,
1061 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1066 * DHT connect adapter. Opens a connection to the dht service.
1068 * @param cls Closure (peer).
1069 * @param cfg Configuration handle.
1074 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1076 struct RegexPeer *peer = cls;
1078 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1080 return peer->dht_handle;
1085 * Adapter function called to destroy a connection to the dht service.
1087 * @param cls Closure (peer).
1088 * @param op_result Service handle returned from the connect adapter.
1091 dht_da (void *cls, void *op_result)
1093 struct RegexPeer *peer = (struct RegexPeer *) cls;
1095 GNUNET_assert (peer->dht_handle == op_result);
1097 if (NULL != peer->search_handle)
1099 REGEX_INTERNAL_search_cancel (peer->search_handle);
1100 peer->search_handle = NULL;
1103 if (NULL != peer->dht_handle)
1105 GNUNET_DHT_disconnect (peer->dht_handle);
1106 peer->dht_handle = NULL;
1112 * Signature of a main function for a testcase.
1115 * @param h the run handle
1116 * @param num_peers_ number of peers in 'peers'
1117 * @param testbed_peers handle to peers run in the testbed. NULL upon timeout (see
1118 * GNUNET_TESTBED_test_run()).
1119 * @param links_succeeded the number of overlay link connection attempts that
1121 * @param links_failed the number of overlay link connection attempts that
1125 test_master (void *cls,
1126 struct GNUNET_TESTBED_RunHandle *h,
1127 unsigned int num_peers_,
1128 struct GNUNET_TESTBED_Peer **testbed_peers,
1129 unsigned int links_succeeded,
1130 unsigned int links_failed)
1134 GNUNET_assert (num_peers_ == num_peers);
1136 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1137 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1138 "Testbed started in %s\n",
1139 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1141 if (NULL != abort_task)
1143 GNUNET_SCHEDULER_cancel (abort_task);
1147 for (i = 0; i < num_peers; i++)
1149 peers[i].peer_handle = testbed_peers[i];
1152 GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT", "DISABLE_TRY_CONNECT"))
1154 struct GNUNET_TIME_Relative settle_time;
1157 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1159 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1160 "Waiting for DHT for %s to settle new connections.\n\n",
1161 GNUNET_STRINGS_relative_time_to_string(settle_time, GNUNET_NO));
1162 GNUNET_SCHEDULER_add_delayed (settle_time, &do_announce, NULL);
1166 GNUNET_SCHEDULER_add_now (&do_announce, NULL);
1168 search_timeout_task =
1169 GNUNET_SCHEDULER_add_delayed (search_timeout_time, &search_timed_out, NULL);
1173 * Function that will be called whenever something in the testbed changes.
1175 * @param cls closure, NULL
1176 * @param event information on what is happening
1179 master_controller_cb (void *cls,
1180 const struct GNUNET_TESTBED_EventInformation *event)
1182 switch (event->type)
1184 case GNUNET_TESTBED_ET_CONNECT:
1187 case GNUNET_TESTBED_ET_PEER_START:
1197 /******************************************************************************/
1198 /*************************** TESTBED PEER SETUP *****************************/
1199 /******************************************************************************/
1202 * Process the text buffer counting the non-empty lines and separating them
1203 * with NULL characters, for later ease of copy using (as)printf.
1205 * @param data Memory buffer with strings.
1206 * @param data_size Size of the @a data buffer in bytes.
1207 * @param str_max Maximum number of strings to return.
1208 * @return Positive number of lines found in the buffer,
1209 * #GNUNET_SYSERR otherwise.
1212 count_and_separate_strings (char *data,
1214 unsigned int str_max)
1216 char *buf; // Keep track of last string to skip blank lines
1217 unsigned int offset;
1218 unsigned int str_cnt;
1223 while ( (offset < (data_size - 1)) && (str_cnt < str_max) )
1226 if ( ((data[offset] == '\n')) &&
1227 (buf != &data[offset]) )
1229 data[offset] = '\0';
1231 buf = &data[offset + 1];
1233 else if ( (data[offset] == '\n') ||
1234 (data[offset] == '\0') )
1235 buf = &data[offset + 1];
1242 * Allocate a string array and fill it with the prefixed strings
1243 * from a pre-processed, NULL-separated memory region.
1245 * @param data Preprocessed memory with strings
1246 * @param data_size Size of the @a data buffer in bytes.
1247 * @param strings Address of the string array to be created.
1248 * Must be freed by caller if function end in success.
1249 * @param str_cnt String count. The @a data buffer should contain
1250 * at least this many NULL-separated strings.
1251 * @return #GNUNET_OK in ase of success, #GNUNET_SYSERR otherwise.
1252 * In case of error @a strings must not be freed.
1255 create_string_array (char *data, uint64_t data_size,
1256 char ***strings, unsigned int str_cnt)
1262 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1264 for (i = 0; i < str_cnt; i++)
1266 len = strlen (&data[offset]);
1267 if (offset + len >= data_size)
1269 GNUNET_free (*strings);
1271 return GNUNET_SYSERR;
1273 if (0 == len) // empty line
1280 GNUNET_asprintf (&(*strings)[i],
1291 * Load search strings from given filename. One search string per line.
1293 * @param filename filename of the file containing the search strings.
1294 * @param strings set of strings loaded from file. Caller needs to free this
1295 * if number returned is greater than zero.
1296 * @param limit upper limit on the number of strings read from the file
1297 * @return number of strings found in the file. #GNUNET_SYSERR on error.
1300 load_search_strings (const char *filename,
1309 if (NULL == filename)
1311 return GNUNET_SYSERR;
1313 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1315 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1316 "Could not find search strings file %s\n", filename);
1317 return GNUNET_SYSERR;
1320 GNUNET_DISK_file_size (filename,
1325 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1326 "Search strings file %s cannot be read.\n",
1328 return GNUNET_SYSERR;
1332 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1333 "Search strings file %s is empty.\n",
1335 return GNUNET_SYSERR;
1338 /* Read data into memory */
1339 data = GNUNET_malloc (filesize + 1);
1340 if (filesize != GNUNET_DISK_fn_read (filename,
1345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1346 "Could not read search strings file %s.\n",
1348 return GNUNET_SYSERR;
1351 /* Process buffer and build array */
1352 str_cnt = count_and_separate_strings (data, filesize, limit);
1353 if (GNUNET_OK != create_string_array (data, filesize, strings, str_cnt))
1355 str_cnt = GNUNET_SYSERR;
1363 * Main function that will be run by the scheduler.
1365 * @param cls closure
1366 * @param args remaining command-line arguments
1367 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1368 * @param config configuration
1373 const char *cfgfile,
1374 const struct GNUNET_CONFIGURATION_Handle *config)
1376 unsigned int nsearchstrs;
1378 struct GNUNET_TIME_Relative abort_time;
1380 in_shutdown = GNUNET_NO;
1385 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1386 _("No configuration file given. Exiting\n"));
1387 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1390 cfg = GNUNET_CONFIGURATION_dup (config);
1392 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
1396 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1399 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1403 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
1404 "PARALLEL_SEARCHES",
1405 &init_parallel_searches))
1407 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1408 "Configuration option \"PARALLEL_SEARCHES\" missing."
1409 " Using default (%d)\n", 10);
1410 init_parallel_searches = 10;
1413 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
1414 "REANNOUNCE_PERIOD_MAX",
1415 &reannounce_period_max))
1417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1418 "reannounce_period_max not given. Using 10 minutes.\n");
1419 reannounce_period_max =
1420 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
1423 /* Check arguments */
1424 if (NULL == policy_dir)
1426 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1427 _("No policy directory specified on command line. Exiting.\n"));
1430 if (GNUNET_YES != GNUNET_DISK_directory_test (policy_dir, GNUNET_YES))
1432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1433 _("Specified policies directory does not exist. Exiting.\n"));
1434 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1437 if (0 >= (int) (num_peers = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL)))
1439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1440 _("No files found in `%s'\n"),
1444 GNUNET_CONFIGURATION_set_value_string (cfg, "REGEXPROFILER",
1445 "POLICY_DIR", policy_dir);
1446 if (GNUNET_YES != GNUNET_DISK_file_test (strings_file))
1448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1449 _("No search strings file given. Exiting.\n"));
1450 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1453 nsearchstrs = load_search_strings (strings_file,
1456 if (num_peers != nsearchstrs)
1458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1459 "Error loading search strings.\n");
1460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1461 "File (%s) does not contain enough strings (%u/%u).\n",
1462 strings_file, nsearchstrs, num_peers);
1463 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1466 if ( (0 == num_peers) || (NULL == search_strings))
1468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1469 _("Error loading search strings. Exiting.\n"));
1470 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1473 for (i = 0; i < num_peers; i++)
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1475 "search string: %s\n",
1479 if ( (NULL != data_filename) &&
1480 (NULL == (data_file =
1481 GNUNET_DISK_file_open (data_filename,
1482 GNUNET_DISK_OPEN_READWRITE |
1483 GNUNET_DISK_OPEN_TRUNCATE |
1484 GNUNET_DISK_OPEN_CREATE,
1485 GNUNET_DISK_PERM_USER_READ |
1486 GNUNET_DISK_PERM_USER_WRITE))) )
1488 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1494 /* Initialize peers */
1495 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1496 for (i = 0; i < num_peers; i++)
1499 GNUNET_CONFIGURATION_set_value_number (cfg,
1500 "TESTBED", "OVERLAY_RANDOM_LINKS",
1502 GNUNET_CONFIGURATION_set_value_number (cfg,
1504 (long long unsigned)
1505 (log (num_peers) / log (2.0)));
1507 /* For feedback about the start process activate these and pass master_cb */
1508 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1509 // event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1510 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1511 // event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1512 prof_start_time = GNUNET_TIME_absolute_get ();
1513 GNUNET_TESTBED_run (hosts_file,
1517 &master_controller_cb,
1518 NULL, /* master_controller_cb cls */
1520 NULL); /* test_master cls */
1522 GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
1526 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1527 "SETUP_TIMEOUT not given. Using 15 minutes.\n");
1529 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15);
1531 abort_time = GNUNET_TIME_relative_add (abort_time, GNUNET_TIME_UNIT_MINUTES);
1533 GNUNET_SCHEDULER_add_delayed (abort_time,
1536 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1537 "setup_timeout: %s\n",
1538 GNUNET_STRINGS_relative_time_to_string (abort_time, GNUNET_YES));
1545 * @param argc argument count
1546 * @param argv argument values
1547 * @return 0 on success
1550 main (int argc, char *const *argv)
1552 struct GNUNET_GETOPT_CommandLineOption options[] = {
1554 GNUNET_GETOPT_option_filename ('o',
1557 gettext_noop ("name of the file for writing statistics"),
1560 GNUNET_GETOPT_option_relative_time ('t',
1563 gettext_noop ("wait TIMEOUT before ending the experiment"),
1564 &search_timeout_time),
1566 GNUNET_GETOPT_option_filename ('p',
1569 gettext_noop ("directory with policy files"),
1573 GNUNET_GETOPT_option_filename ('s',
1576 gettext_noop ("name of file with input strings"),
1579 GNUNET_GETOPT_option_filename ('H',
1582 gettext_noop ("name of file with hosts' names"),
1585 GNUNET_GETOPT_OPTION_END
1589 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1591 result = GNUNET_SYSERR;
1593 GNUNET_PROGRAM_run (argc, argv,
1594 "gnunet-regex-profiler",
1595 _("Profiler for regex"),
1596 options, &run, NULL);
1597 if (GNUNET_OK != ret)
1599 if (GNUNET_OK != result)