2 This file is part of GNUnet.
3 (C) 2011 - 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file regex/gnunet-regex-profiler.c
23 * @brief Regex profiler for testing distributed regex use.
24 * @author Bartlomiej Polot
25 * @author Maximilian Szengel
32 #include "gnunet_applications.h"
33 #include "gnunet_util_lib.h"
34 #include "gnunet_regex_lib.h"
35 #include "gnunet_arm_service.h"
36 #include "gnunet_dht_service.h"
37 #include "gnunet_testbed_service.h"
39 #define FIND_TIMEOUT \
40 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
41 #define SEARCHES_IN_PARALLEL 5
49 * The testbed operation handle
51 struct GNUNET_TESTBED_Operation *op;
59 * The next pointer for DLL
61 struct DLLOperation *next;
64 * The prev pointer for DLL
66 struct DLLOperation *prev;
71 * Available states during profiling
83 STATE_SLAVES_STARTING,
101 * Matching strings against announced regexes
106 * Destroying peers; we can do this as the controller takes care of stopping a
107 * peer if it is running
109 STATE_PEERS_DESTROYING
124 * Peer configuration handle.
126 struct GNUNET_CONFIGURATION_Handle *cfg;
129 * The actual testbed peer handle.
131 struct GNUNET_TESTBED_Peer *peer_handle;
134 * Peer's search string.
136 const char *search_str;
139 * Set to GNUNET_YES if the peer successfully matched the above
140 * search string. GNUNET_NO if the string could not be matched
141 * during the profiler run. GNUNET_SYSERR if the string matching
142 * timed out. Undefined if search_str is NULL
144 int search_str_matched;
149 struct GNUNET_DHT_Handle *dht_handle;
152 * Handle to a running regex search.
154 struct GNUNET_REGEX_search_handle *search_handle;
157 * Testbed operation handle for DHT.
159 struct GNUNET_TESTBED_Operation *op_handle;
162 * Peers's statistics handle.
164 struct GNUNET_STATISTICS_Handle *stats_handle;
167 * Testbed operation handle for the statistics service.
169 struct GNUNET_TESTBED_Operation *stats_op_handle;
172 * The starting time of a profiling step.
174 struct GNUNET_TIME_Absolute prof_start_time;
179 GNUNET_SCHEDULER_TaskIdentifier timeout;
184 struct GNUNET_TESTBED_Operation *daemon_op;
189 * The array of peers; we fill this as the peers are given to us by the testbed
191 static struct RegexPeer *peers;
194 * Host registration handle
196 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
199 * Handle to the master controller process
201 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
204 * Handle to the master controller
206 static struct GNUNET_TESTBED_Controller *mc;
209 * Handle to global configuration
211 static struct GNUNET_CONFIGURATION_Handle *cfg;
214 * Abort task identifier
216 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
219 * Shutdown task identifier
221 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
224 * Host registration task identifier
226 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
229 * Global event mask for all testbed events
231 static uint64_t event_mask;
234 * The starting time of a profiling step
236 static struct GNUNET_TIME_Absolute prof_start_time;
239 * Duration profiling step has taken
241 static struct GNUNET_TIME_Relative prof_time;
244 * Number of peers to be started by the profiler
246 static unsigned int num_peers;
249 * Global testing status
254 * current state of profiling
259 * Folder where policy files are stored.
261 static char * policy_dir;
264 * File with hostnames where to execute the test.
266 static char *hosts_file;
269 * File with the strings to look for.
271 static char *strings_file;
276 static char **search_strings;
279 * Number of search strings.
281 static int num_search_strings;
284 * How many searches are running in parallel
286 static unsigned int parallel_searches;
289 * Number of peers found with search strings.
291 static unsigned int peers_found;
294 * Index of peer to start next announce/search.
296 static unsigned int next_search;
299 * Search timeout task identifier.
301 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
304 * Search timeout in seconds.
306 static struct GNUNET_TIME_Relative search_timeout_time = { 60000 };
309 * File to log statistics to.
311 static struct GNUNET_DISK_FileHandle *data_file;
314 * Filename to log statistics to.
316 static char *data_filename;
319 * Prefix used for regex announcing. We need to prefix the search
320 * strings with it, in order to find something.
322 static char * regex_prefix;
325 * What's the maximum regex reannounce period.
327 static struct GNUNET_TIME_Relative reannounce_period_max;
330 /******************************************************************************/
331 /****************************** DECLARATIONS ********************************/
332 /******************************************************************************/
335 * DHT connect callback.
337 * @param cls internal peer id.
338 * @param op operation handle.
339 * @param ca_result connect adapter result.
340 * @param emsg error message.
343 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
344 void *ca_result, const char *emsg);
347 * DHT connect adapter.
349 * @param cls not used.
350 * @param cfg configuration handle.
355 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
359 * Adapter function called to destroy a connection to
363 * @param op_result service handle returned from the connect adapter
366 dht_da (void *cls, void *op_result);
370 * Function called by testbed once we are connected to stats
371 * service. Get the statistics for the services of interest.
373 * @param cls the 'struct RegexPeer' for which we connected to stats
374 * @param op connect operation handle
375 * @param ca_result handle to stats service
376 * @param emsg error message on failure
379 stats_connect_cb (void *cls,
380 struct GNUNET_TESTBED_Operation *op,
386 * Task to collect all statistics from s, will shutdown the
387 * profiler, when done.
390 * @param tc the task context
393 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
397 * Start announcing the next regex in the DHT.
399 * @param cls Index of the next peer in the peers array.
400 * @param tc TaskContext.
403 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
406 /******************************************************************************/
407 /******************************** SHUTDOWN **********************************/
408 /******************************************************************************/
415 * @param tc the task context
418 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
420 struct RegexPeer *peer;
421 unsigned int peer_cnt;
422 unsigned int search_str_cnt;
423 char output_buffer[512];
426 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
427 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
428 GNUNET_SCHEDULER_cancel (abort_task);
429 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
430 GNUNET_SCHEDULER_cancel (register_hosts_task);
432 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
434 peer = &peers[peer_cnt];
436 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
438 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
440 GNUNET_snprintf (output_buffer,
441 sizeof (output_buffer),
442 "%p Search string not found: %s (%d)\n"
443 "%p On peer: %u (%p)\n"
445 peer, peer->search_str, peer->search_str_matched,
446 peer, peer->id, peer,
448 GNUNET_STRINGS_relative_time_to_string (prof_time,
450 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
451 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
454 if (NULL != peers[peer_cnt].op_handle)
455 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
456 if (NULL != peers[peer_cnt].stats_op_handle)
457 GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
460 if (NULL != data_file)
461 GNUNET_DISK_file_close (data_file);
463 for (search_str_cnt = 0;
464 search_str_cnt < num_search_strings && NULL != search_strings;
467 GNUNET_free_non_null (search_strings[search_str_cnt]);
469 GNUNET_free_non_null (search_strings);
471 if (NULL != reg_handle)
472 GNUNET_TESTBED_cancel_registration (reg_handle);
475 GNUNET_TESTBED_controller_disconnect (mc);
477 GNUNET_TESTBED_controller_stop (mc_proc);
479 GNUNET_CONFIGURATION_destroy (cfg);
481 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
486 * abort task to run on test timed out
489 * @param tc the task context
492 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
494 unsigned long i = (unsigned long) cls;
496 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting from line %lu...\n", i);
497 abort_task = GNUNET_SCHEDULER_NO_TASK;
498 result = GNUNET_SYSERR;
499 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
500 GNUNET_SCHEDULER_cancel (shutdown_task);
501 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
505 /******************************************************************************/
506 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
507 /******************************************************************************/
510 * Adapter function called to establish a connection to
511 * statistics service.
514 * @param cfg configuration of the peer to connect to; will be available until
515 * GNUNET_TESTBED_operation_done() is called on the operation returned
516 * from GNUNET_TESTBED_service_connect()
517 * @return service handle to return in 'op_result', NULL on error
520 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
522 return GNUNET_STATISTICS_create ("<driver>", cfg);
527 * Adapter function called to destroy a connection to
528 * statistics service.
531 * @param op_result service handle returned from the connect adapter
534 stats_da (void *cls, void *op_result)
536 struct RegexPeer *peer = cls;
538 GNUNET_assert (op_result == peer->stats_handle);
540 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
541 peer->stats_handle = NULL;
546 * Process statistic values. Write all values to global 'data_file', if present.
549 * @param subsystem name of subsystem that created the statistic
550 * @param name the name of the datum
551 * @param value the current value
552 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
553 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
556 stats_iterator (void *cls, const char *subsystem, const char *name,
557 uint64_t value, int is_persistent)
559 struct RegexPeer *peer = cls;
560 char output_buffer[512];
563 if (NULL == data_file)
565 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
566 "%p -> %s [%s]: %llu\n",
567 peer, subsystem, name, value);
571 GNUNET_snprintf (output_buffer,
572 sizeof (output_buffer),
575 subsystem, value, name);
576 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
577 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
584 * Stats callback. Finish the stats testbed operation and when all stats have
585 * been iterated, shutdown the profiler.
588 * @param success GNUNET_OK if statistics were
589 * successfully obtained, GNUNET_SYSERR if not.
595 static unsigned int peer_cnt;
596 struct RegexPeer *peer = cls;
598 if (GNUNET_OK != success)
600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
601 "Getting statistics for peer %u failed!\n",
606 GNUNET_assert (NULL != peer->stats_op_handle);
608 GNUNET_TESTBED_operation_done (peer->stats_op_handle);
609 peer->stats_op_handle = NULL;
612 peer = &peers[peer_cnt];
614 if (peer_cnt == num_peers)
616 struct GNUNET_TIME_Relative delay = { 100 };
617 shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
621 peer->stats_op_handle =
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,
664 GNUNET_TIME_UNIT_FOREVER_REL,
666 &stats_iterator, peer))
668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
669 "Could not get statistics of peer %u!\n", peer->id);
675 * Task to collect all statistics from all peers, will shutdown the
676 * profiler, when done.
679 * @param tc the task context
682 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
684 struct RegexPeer *peer = &peers[0];
686 GNUNET_assert (NULL != peer->peer_handle);
688 peer->stats_op_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.
709 * @param tc TaskContext.
712 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
716 * Method called when we've found a peer that announced a regex
717 * that matches our search string. Now get the statistics.
719 * @param cls Closure provided in GNUNET_REGEX_search.
720 * @param id Peer providing a regex that matches the string.
721 * @param get_path Path of the get request.
722 * @param get_path_length Lenght of get_path.
723 * @param put_path Path of the put request.
724 * @param put_path_length Length of the put_path.
727 regex_found_handler (void *cls,
728 const struct GNUNET_PeerIdentity *id,
729 const struct GNUNET_PeerIdentity *get_path,
730 unsigned int get_path_length,
731 const struct GNUNET_PeerIdentity *put_path,
732 unsigned int put_path_length)
734 struct RegexPeer *peer = cls;
735 char output_buffer[512];
738 if (GNUNET_YES == peer->search_str_matched)
740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
741 "String %s on peer %u already matched!\n",
742 peer->search_str, peer->id);
749 if (GNUNET_SCHEDULER_NO_TASK != peer->timeout)
751 GNUNET_SCHEDULER_cancel (peer->timeout);
752 peer->timeout = GNUNET_SCHEDULER_NO_TASK;
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, peers_found, num_search_strings);
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 peers_found, num_search_strings, 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 (peers_found == num_search_strings)
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 (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
806 GNUNET_SCHEDULER_cancel (search_timeout_task);
808 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
809 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
815 * Connect by string timeout task. This will cancel the profiler after the
816 * specified timeout 'search_timeout'.
819 * @param tc the task context
822 search_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
824 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
825 "Finding matches to all strings did not succeed after %s.\n",
826 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
828 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
829 "Found %i of %i strings\n", peers_found, num_search_strings);
831 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
832 "Search timed out after %s."
833 "Collecting stats and shutting down.\n",
834 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
837 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
842 * Search timed out. It might still complete in the future,
843 * but we should start another one.
845 * @param cls Index of the next peer in the peers array.
846 * @param tc TaskContext.
849 find_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
851 struct RegexPeer *p = cls;
853 p->timeout = GNUNET_SCHEDULER_NO_TASK;
855 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
857 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
858 "Searching for string \"%s\" on peer %d timed out. Starting new search.\n",
861 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
866 * Start searching for a string in the DHT.
868 * @param cls Index of the next peer in the peers array.
869 * @param tc TaskContext.
872 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
874 unsigned int search_peer = (unsigned int) (long) cls;
876 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
877 search_peer >= num_search_strings)
880 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
881 "Searching for string \"%s\" on peer %d (%u||)\n",
882 peers[search_peer].search_str,
886 peers[search_peer].op_handle =
887 GNUNET_TESTBED_service_connect (NULL,
888 peers[search_peer].peer_handle,
894 &peers[search_peer]);
895 GNUNET_assert (NULL != peers[search_peer].op_handle);
896 peers[search_peer].timeout = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
898 &peers[search_peer]);
905 * Callback called when testbed has started the daemon we asked for.
908 * @param op the operation handle
909 * @param emsg NULL on success; otherwise an error description
912 daemon_started (void *cls, struct GNUNET_TESTBED_Operation *op,
915 struct RegexPeer *peer = (struct RegexPeer *) cls;
916 unsigned long search_peer;
920 GNUNET_TESTBED_operation_done (peer->daemon_op);
921 peer->daemon_op = NULL;
925 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
926 "Failed to start/stop daemon at peer %u: %s\n", me, emsg);
930 /* Find a peer to look for a string matching the regex announced */
931 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
933 for (i = 0; peers[search_peer].search_str != NULL; i++)
935 search_peer = (search_peer + 1) % num_peers;
937 GNUNET_abort (); /* we ran out of peers, must be a bug */
939 peers[search_peer].search_str = search_strings[me];
940 peers[search_peer].search_str_matched = GNUNET_NO;
941 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
942 reannounce_period_max,
945 (void *) search_peer);
950 * Task to start the daemons on each peer so that the regexes are announced
954 * @param tc the task context
957 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
961 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting announce.\n");
963 for (i = 0; i < SEARCHES_IN_PARALLEL; i++)
965 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
966 " scheduling announce %u\n",
968 (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
974 * Start announcing the next regex in the DHT.
976 * @param cls Closure (unused).
977 * @param tc TaskContext.
980 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
982 struct RegexPeer *peer;
984 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
985 next_search >= num_peers)
988 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
989 peer = &peers[next_search];
991 GNUNET_TESTBED_peer_manage_service (NULL,
1002 * DHT connect callback. Called when we are connected to the dht service for
1003 * the peer in 'cls'. If successfull we connect to the stats service of this
1004 * peer and then try to match the search string of this peer.
1006 * @param cls internal peer id.
1007 * @param op operation handle.
1008 * @param ca_result connect adapter result.
1009 * @param emsg error message.
1012 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1013 void *ca_result, const char *emsg)
1015 struct RegexPeer *peer = (struct RegexPeer *) cls;
1017 if (NULL != emsg || NULL == op || NULL == ca_result)
1019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1023 GNUNET_assert (NULL != peer->dht_handle);
1024 GNUNET_assert (peer->op_handle == op);
1025 GNUNET_assert (peer->dht_handle == ca_result);
1027 peer->search_str_matched = GNUNET_NO;
1028 peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
1030 ®ex_found_handler, peer,
1032 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1037 * DHT connect adapter. Opens a connection to the dht service.
1039 * @param cls Closure (peer).
1040 * @param cfg Configuration handle.
1045 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1047 struct RegexPeer *peer = cls;
1049 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1051 return peer->dht_handle;
1056 * Adapter function called to destroy a connection to the dht service.
1058 * @param cls Closure (peer).
1059 * @param op_result Service handle returned from the connect adapter.
1062 dht_da (void *cls, void *op_result)
1064 struct RegexPeer *peer = (struct RegexPeer *) cls;
1066 GNUNET_assert (peer->dht_handle == op_result);
1068 if (NULL != peer->search_handle)
1070 GNUNET_REGEX_search_cancel (peer->search_handle);
1071 peer->search_handle = NULL;
1074 if (NULL != peer->dht_handle)
1076 GNUNET_DHT_disconnect (peer->dht_handle);
1077 peer->dht_handle = NULL;
1083 * Signature of a main function for a testcase.
1086 * @param num_peers_ number of peers in 'peers'
1087 * @param peers handle to peers run in the testbed. NULL upon timeout (see
1088 * GNUNET_TESTBED_test_run()).
1089 * @param links_succeeded the number of overlay link connection attempts that
1091 * @param links_failed the number of overlay link connection attempts that
1095 test_master (void *cls,
1096 unsigned int num_peers_,
1097 struct GNUNET_TESTBED_Peer **testbed_peers,
1098 unsigned int links_succeeded,
1099 unsigned int links_failed)
1103 GNUNET_assert (num_peers_ == num_peers);
1105 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1106 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1107 "Testbed started in %s\n",
1108 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1110 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1112 GNUNET_SCHEDULER_cancel (abort_task);
1113 abort_task = GNUNET_SCHEDULER_NO_TASK;
1116 for (i = 0; i < num_peers; i++)
1118 peers[i].peer_handle = testbed_peers[i];
1120 GNUNET_SCHEDULER_add_now (&do_announce, NULL);
1121 search_timeout_task =
1122 GNUNET_SCHEDULER_add_delayed (search_timeout_time, &search_timeout, NULL);
1126 * Function that will be called whenever something in the testbed changes.
1128 * @param cls closure, NULL
1129 * @param event information on what is happening
1132 master_controller_cb (void *cls,
1133 const struct GNUNET_TESTBED_EventInformation *event)
1135 switch (event->type)
1137 case GNUNET_TESTBED_ET_CONNECT:
1140 case GNUNET_TESTBED_ET_PEER_START:
1150 /******************************************************************************/
1151 /*************************** TESTBED PEER SETUP *****************************/
1152 /******************************************************************************/
1156 * Load search strings from given filename. One search string per line.
1158 * @param filename filename of the file containing the search strings.
1159 * @param strings set of strings loaded from file. Caller needs to free this
1160 * if number returned is greater than zero.
1161 * @param limit upper limit on the number of strings read from the file
1162 * @return number of strings found in the file. GNUNET_SYSERR on error.
1165 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1170 unsigned int offset;
1174 if (NULL == filename)
1176 return GNUNET_SYSERR;
1179 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1181 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1182 "Could not find search strings file %s\n", filename);
1183 return GNUNET_SYSERR;
1185 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1189 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1190 return GNUNET_SYSERR;
1192 data = GNUNET_malloc (filesize);
1193 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1196 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1198 return GNUNET_SYSERR;
1203 while (offset < (filesize - 1) && str_cnt < limit)
1206 if (((data[offset] == '\n')) && (buf != &data[offset]))
1208 data[offset] = '\0';
1210 buf = &data[offset + 1];
1212 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1213 buf = &data[offset + 1];
1215 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1217 for (i = 0; i < str_cnt; i++)
1219 GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1220 offset += strlen (&data[offset]) + 1;
1228 * Main function that will be run by the scheduler.
1230 * @param cls closure
1231 * @param args remaining command-line arguments
1232 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1233 * @param config configuration
1236 run (void *cls, char *const *args, const char *cfgfile,
1237 const struct GNUNET_CONFIGURATION_Handle *config)
1239 unsigned int nsearchstrs;
1245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1246 _("No configuration file given. Exiting\n"));
1247 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1250 cfg = GNUNET_CONFIGURATION_dup (config);
1252 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
1256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1257 _("Configuration option \"regex_prefix\" missing. Exiting\n"));
1258 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1262 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
1263 "REANNOUNCE_PERIOD_MAX",
1264 &reannounce_period_max))
1266 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1267 "reannounce_period_max not given. Using 10 minutes.\n");
1268 reannounce_period_max =
1269 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
1272 /* Check arguments */
1273 if (NULL == hosts_file)
1275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1276 _("No hosts-file specified on command line. Exiting.\n"));
1279 if (NULL == policy_dir)
1281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1282 _("No policy directory specified on command line. Exiting.\n"));
1285 if (GNUNET_YES != GNUNET_DISK_directory_test (policy_dir, GNUNET_YES))
1287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1288 _("Specified policies directory does not exist. Exiting.\n"));
1289 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1292 if (-1 == (num_peers = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL)))
1294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1295 _("No files found in `%s'\n"),
1299 GNUNET_CONFIGURATION_set_value_string (cfg, "REGEXPROFILER",
1300 "POLICY_DIR", policy_dir);
1301 if (GNUNET_YES != GNUNET_DISK_file_test (strings_file))
1303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1304 _("No search strings file given. Exiting.\n"));
1305 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1308 nsearchstrs = load_search_strings (strings_file,
1310 num_search_strings);
1311 if (num_search_strings != nsearchstrs)
1313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1314 _("Error loading search strings."
1315 "Given file does not contain enough strings. Exiting.\n"));
1316 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1319 if (0 >= num_search_strings || NULL == search_strings)
1321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1322 _("Error loading search strings. Exiting.\n"));
1323 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1326 for (i = 0; i < num_search_strings; i++)
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1328 "search string: %s\n",
1332 if ( (NULL != data_filename) &&
1333 (NULL == (data_file =
1334 GNUNET_DISK_file_open (data_filename,
1335 GNUNET_DISK_OPEN_READWRITE |
1336 GNUNET_DISK_OPEN_TRUNCATE |
1337 GNUNET_DISK_OPEN_CREATE,
1338 GNUNET_DISK_PERM_USER_READ |
1339 GNUNET_DISK_PERM_USER_WRITE))) )
1341 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1347 /* Initialize peers */
1348 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1349 for (i = 0; i < num_peers; i++)
1355 /* For feedback about the start process activate these and pass master_cb */
1356 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1357 // event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1358 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1359 // event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1360 prof_start_time = GNUNET_TIME_absolute_get ();
1361 GNUNET_TESTBED_run (args[0],
1365 &master_controller_cb,
1366 NULL, /* master_controller_cb cls */
1368 NULL); /* test_master cls */
1370 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1371 (GNUNET_TIME_UNIT_MINUTES, 5),
1380 * @param argc argument count
1381 * @param argv argument values
1382 * @return 0 on success
1385 main (int argc, char *const *argv)
1387 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1388 {'o', "output-file", "FILENAME",
1389 gettext_noop ("name of the file for writing statistics"),
1390 1, &GNUNET_GETOPT_set_string, &data_filename},
1391 {'t', "matching-timeout", "TIMEOUT",
1392 gettext_noop ("wait TIMEOUT before considering a string match as failed"),
1393 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time },
1394 {'n', "num-search-strings", "COUNT",
1395 gettext_noop ("number of search strings to read from search strings file"),
1396 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
1397 {'p', "policy-dir", "DIRECTORY",
1398 gettext_noop ("directory with policy files"),
1399 GNUNET_YES, &GNUNET_GETOPT_set_filename, &policy_dir },
1400 {'s', "strings-file", "FILENAME",
1401 gettext_noop ("name of file with input strings"),
1402 GNUNET_YES, &GNUNET_GETOPT_set_filename, &strings_file },
1403 {'H', "hosts-file", "FILENAME",
1404 gettext_noop ("name of file with hosts' names"),
1405 GNUNET_YES, &GNUNET_GETOPT_set_filename, &hosts_file },
1406 GNUNET_GETOPT_OPTION_END
1410 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1412 result = GNUNET_SYSERR;
1414 GNUNET_PROGRAM_run (argc, argv,
1415 "gnunet-regex-profiler",
1416 _("Profiler for regex"),
1417 options, &run, NULL);
1418 if (GNUNET_OK != ret)
1420 if (GNUNET_OK != result)