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)
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 GNUNET_REGEX_search_handle *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 * Testbed operation handle for the statistics service.
168 struct GNUNET_TESTBED_Operation *stats_op_handle;
171 * The starting time of a profiling step.
173 struct GNUNET_TIME_Absolute prof_start_time;
178 GNUNET_SCHEDULER_TaskIdentifier timeout;
183 struct GNUNET_TESTBED_Operation *daemon_op;
187 * Set when shutting down to avoid making more queries.
189 static int in_shutdown;
192 * The array of peers; we fill this as the peers are given to us by the testbed
194 static struct RegexPeer *peers;
197 * Host registration handle
199 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
202 * Handle to the master controller process
204 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
207 * Handle to the master controller
209 static struct GNUNET_TESTBED_Controller *mc;
212 * Handle to global configuration
214 static struct GNUNET_CONFIGURATION_Handle *cfg;
217 * Abort task identifier
219 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
222 * Shutdown task identifier
224 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
227 * Host registration task identifier
229 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
232 * Global event mask for all testbed events
234 static uint64_t event_mask;
237 * The starting time of a profiling step
239 static struct GNUNET_TIME_Absolute prof_start_time;
242 * Duration profiling step has taken
244 static struct GNUNET_TIME_Relative prof_time;
247 * Number of peers to be started by the profiler
249 static unsigned int num_peers;
252 * Global testing status
257 * current state of profiling
262 * Folder where policy files are stored.
264 static char * policy_dir;
267 * File with hostnames where to execute the test.
269 static char *hosts_file;
272 * File with the strings to look for.
274 static char *strings_file;
279 static char **search_strings;
282 * Number of search strings.
284 static int num_search_strings;
287 * How many searches are we going to start in parallel
289 static long long unsigned int init_parallel_searches;
292 * How many searches are running in parallel
294 static unsigned int parallel_searches;
297 * Number of strings found in the published regexes.
299 static unsigned int strings_found;
302 * Index of peer to start next announce/search.
304 static unsigned int next_search;
307 * Search timeout task identifier.
309 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
312 * Search timeout in seconds.
314 static struct GNUNET_TIME_Relative search_timeout_time = { 60000 };
317 * File to log statistics to.
319 static struct GNUNET_DISK_FileHandle *data_file;
322 * Filename to log statistics to.
324 static char *data_filename;
327 * Prefix used for regex announcing. We need to prefix the search
328 * strings with it, in order to find something.
330 static char * regex_prefix;
333 * What's the maximum regex reannounce period.
335 static struct GNUNET_TIME_Relative reannounce_period_max;
338 /******************************************************************************/
339 /****************************** DECLARATIONS ********************************/
340 /******************************************************************************/
343 * DHT connect callback.
345 * @param cls internal peer id.
346 * @param op operation handle.
347 * @param ca_result connect adapter result.
348 * @param emsg error message.
351 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
352 void *ca_result, const char *emsg);
355 * DHT connect adapter.
357 * @param cls not used.
358 * @param cfg configuration handle.
363 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
367 * Adapter function called to destroy a connection to
371 * @param op_result service handle returned from the connect adapter
374 dht_da (void *cls, void *op_result);
378 * Function called by testbed once we are connected to stats
379 * service. Get the statistics for the services of interest.
381 * @param cls the 'struct RegexPeer' for which we connected to stats
382 * @param op connect operation handle
383 * @param ca_result handle to stats service
384 * @param emsg error message on failure
387 stats_connect_cb (void *cls,
388 struct GNUNET_TESTBED_Operation *op,
394 * Task to collect all statistics from s, will shutdown the
395 * profiler, when done.
398 * @param tc the task context
401 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
405 * Start announcing the next regex in the DHT.
407 * @param cls Index of the next peer in the peers array.
408 * @param tc TaskContext.
411 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
414 /******************************************************************************/
415 /******************************** SHUTDOWN **********************************/
416 /******************************************************************************/
423 * @param tc the task context
426 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
428 struct RegexPeer *peer;
429 unsigned int peer_cnt;
430 unsigned int search_str_cnt;
431 char output_buffer[512];
434 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
435 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
436 GNUNET_SCHEDULER_cancel (abort_task);
437 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
438 GNUNET_SCHEDULER_cancel (register_hosts_task);
440 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
442 peer = &peers[peer_cnt];
444 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
446 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
448 GNUNET_snprintf (output_buffer,
449 sizeof (output_buffer),
450 "%p Search string not found: %s (%d)\n"
451 "%p On peer: %u (%p)\n"
453 peer, peer->search_str, peer->search_str_matched,
454 peer, peer->id, peer,
456 GNUNET_STRINGS_relative_time_to_string (prof_time,
458 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
459 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
462 if (NULL != peers[peer_cnt].op_handle)
463 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
464 if (NULL != peers[peer_cnt].stats_op_handle)
465 GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
468 if (NULL != data_file)
469 GNUNET_DISK_file_close (data_file);
471 for (search_str_cnt = 0;
472 search_str_cnt < num_search_strings && NULL != search_strings;
475 GNUNET_free_non_null (search_strings[search_str_cnt]);
477 GNUNET_free_non_null (search_strings);
479 if (NULL != reg_handle)
480 GNUNET_TESTBED_cancel_registration (reg_handle);
483 GNUNET_TESTBED_controller_disconnect (mc);
485 GNUNET_TESTBED_controller_stop (mc_proc);
487 GNUNET_CONFIGURATION_destroy (cfg);
489 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
494 * abort task to run on test timed out
497 * @param tc the task context
500 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
502 unsigned long i = (unsigned long) cls;
504 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting from line %lu...\n", i);
505 abort_task = GNUNET_SCHEDULER_NO_TASK;
506 result = GNUNET_SYSERR;
507 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
508 GNUNET_SCHEDULER_cancel (shutdown_task);
509 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
513 /******************************************************************************/
514 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
515 /******************************************************************************/
518 * Adapter function called to establish a connection to
519 * statistics service.
522 * @param cfg configuration of the peer to connect to; will be available until
523 * GNUNET_TESTBED_operation_done() is called on the operation returned
524 * from GNUNET_TESTBED_service_connect()
525 * @return service handle to return in 'op_result', NULL on error
528 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
530 return GNUNET_STATISTICS_create ("<driver>", cfg);
535 * Adapter function called to destroy a connection to
536 * statistics service.
539 * @param op_result service handle returned from the connect adapter
542 stats_da (void *cls, void *op_result)
544 struct RegexPeer *peer = cls;
546 GNUNET_assert (op_result == peer->stats_handle);
548 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
549 peer->stats_handle = NULL;
554 * Process statistic values. Write all values to global 'data_file', if present.
557 * @param subsystem name of subsystem that created the statistic
558 * @param name the name of the datum
559 * @param value the current value
560 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
561 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
564 stats_iterator (void *cls, const char *subsystem, const char *name,
565 uint64_t value, int is_persistent)
567 struct RegexPeer *peer = cls;
568 char output_buffer[512];
571 if (NULL == data_file)
573 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
574 "%p -> %s [%s]: %llu\n",
575 peer, subsystem, name, value);
579 GNUNET_snprintf (output_buffer,
580 sizeof (output_buffer),
583 subsystem, value, name);
584 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
585 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
592 * Stats callback. Finish the stats testbed operation and when all stats have
593 * been iterated, shutdown the profiler.
596 * @param success GNUNET_OK if statistics were
597 * successfully obtained, GNUNET_SYSERR if not.
603 static unsigned int peer_cnt;
604 struct RegexPeer *peer = cls;
606 if (GNUNET_OK != success)
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 "Getting statistics for peer %u failed!\n",
614 GNUNET_assert (NULL != peer->stats_op_handle);
616 GNUNET_TESTBED_operation_done (peer->stats_op_handle);
617 peer->stats_op_handle = NULL;
620 peer = &peers[peer_cnt];
622 if (peer_cnt == num_peers)
624 struct GNUNET_TIME_Relative delay = { 100 };
625 shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
629 peer->stats_op_handle =
630 GNUNET_TESTBED_service_connect (NULL,
643 * Function called by testbed once we are connected to stats
644 * service. Get the statistics for the services of interest.
646 * @param cls the 'struct RegexPeer' for which we connected to stats
647 * @param op connect operation handle
648 * @param ca_result handle to stats service
649 * @param emsg error message on failure
652 stats_connect_cb (void *cls,
653 struct GNUNET_TESTBED_Operation *op,
657 struct RegexPeer *peer = cls;
659 if (NULL == ca_result || NULL != emsg)
661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
662 "Failed to connect to statistics service on peer %u: %s\n",
665 peer->stats_handle = NULL;
669 peer->stats_handle = ca_result;
671 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
672 GNUNET_TIME_UNIT_FOREVER_REL,
674 &stats_iterator, peer))
676 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
677 "Could not get statistics of peer %u!\n", peer->id);
683 * Task to collect all statistics from all peers, will shutdown the
684 * profiler, when done.
687 * @param tc the task context
690 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
692 struct RegexPeer *peer = &peers[0];
694 GNUNET_assert (NULL != peer->peer_handle);
696 peer->stats_op_handle =
697 GNUNET_TESTBED_service_connect (NULL,
708 /******************************************************************************/
709 /************************ REGEX FIND CONNECTIONS **************************/
710 /******************************************************************************/
714 * Start searching for the next string in the DHT.
716 * @param cls Index of the next peer in the peers array.
717 * @param tc TaskContext.
720 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
724 * Method called when we've found a peer that announced a regex
725 * that matches our search string. Now get the statistics.
727 * @param cls Closure provided in GNUNET_REGEX_search.
728 * @param id Peer providing a regex that matches the string.
729 * @param get_path Path of the get request.
730 * @param get_path_length Lenght of get_path.
731 * @param put_path Path of the put request.
732 * @param put_path_length Length of the put_path.
735 regex_found_handler (void *cls,
736 const struct GNUNET_PeerIdentity *id,
737 const struct GNUNET_PeerIdentity *get_path,
738 unsigned int get_path_length,
739 const struct GNUNET_PeerIdentity *put_path,
740 unsigned int put_path_length)
742 struct RegexPeer *peer = cls;
743 char output_buffer[512];
746 if (GNUNET_YES == peer->search_str_matched)
748 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
749 "String %s on peer %u already matched!\n",
750 peer->search_str, peer->id);
757 if (GNUNET_SCHEDULER_NO_TASK != peer->timeout)
759 GNUNET_SCHEDULER_cancel (peer->timeout);
760 peer->timeout = GNUNET_SCHEDULER_NO_TASK;
761 if (GNUNET_NO == in_shutdown)
762 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
767 // FIXME not possible right now
768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
769 "String matching timed out for string %s on peer %u (%i/%i)\n",
770 peer->search_str, peer->id, strings_found, num_search_strings);
771 peer->search_str_matched = GNUNET_SYSERR;
775 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
777 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
778 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
779 peer->search_str, peer->id,
780 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
781 strings_found, num_search_strings, parallel_searches);
783 peer->search_str_matched = GNUNET_YES;
785 if (NULL != data_file)
788 GNUNET_snprintf (output_buffer,
789 sizeof (output_buffer),
791 "%p Search string: %s\n"
792 "%p Search duration: %s\n\n",
794 peer, peer->search_str,
796 GNUNET_STRINGS_relative_time_to_string (prof_time,
799 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
800 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
804 GNUNET_TESTBED_operation_done (peer->op_handle);
805 peer->op_handle = NULL;
807 if (strings_found == num_search_strings)
809 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
810 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
811 "All strings successfully matched in %s\n",
812 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
814 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
815 GNUNET_SCHEDULER_cancel (search_timeout_task);
817 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
818 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
824 * Connect by string timeout task. This will cancel the profiler after the
825 * specified timeout 'search_timeout'.
828 * @param tc the task context
831 search_timed_out (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
835 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
836 "Finding matches to all strings did not succeed after %s.\n",
837 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
839 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
840 "Found %i of %i strings\n", strings_found, num_search_strings);
842 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
843 "Search timed out after %s."
844 "Collecting stats and shutting down.\n",
845 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
848 in_shutdown = GNUNET_YES;
849 for (i = 0; i < num_peers; i++)
851 if (NULL != peers[i].op_handle)
853 GNUNET_TESTBED_operation_done (peers[i].op_handle);
854 peers[i].op_handle = NULL;
857 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
862 * Search timed out. It might still complete in the future,
863 * but we should start another one.
865 * @param cls Index of the next peer in the peers array.
866 * @param tc TaskContext.
869 find_timed_out (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
871 struct RegexPeer *p = cls;
873 p->timeout = GNUNET_SCHEDULER_NO_TASK;
875 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
877 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
878 "Searching for string \"%s\" on peer %d timed out."
879 "Starting new search: %d.\n",
883 if (GNUNET_NO == in_shutdown)
884 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
889 * Start searching for a string in the DHT.
891 * @param cls Index of the next peer in the peers array.
892 * @param tc TaskContext.
895 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
897 unsigned int search_peer = (unsigned int) (long) cls;
899 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
900 search_peer >= num_search_strings)
903 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
904 "Searching for string \"%s\" on peer %d (%u||)\n",
905 peers[search_peer].search_str,
909 peers[search_peer].op_handle =
910 GNUNET_TESTBED_service_connect (NULL,
911 peers[search_peer].peer_handle,
917 &peers[search_peer]);
918 GNUNET_assert (NULL != peers[search_peer].op_handle);
919 peers[search_peer].timeout = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
921 &peers[search_peer]);
928 * Callback called when testbed has started the daemon we asked for.
931 * @param op the operation handle
932 * @param emsg NULL on success; otherwise an error description
935 daemon_started (void *cls, struct GNUNET_TESTBED_Operation *op,
938 struct RegexPeer *peer = (struct RegexPeer *) cls;
939 unsigned long search_peer;
943 GNUNET_TESTBED_operation_done (peer->daemon_op);
944 peer->daemon_op = NULL;
948 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
949 "Failed to start/stop daemon at peer %u: %s\n", me, emsg);
953 /* Find a peer to look for a string matching the regex announced */
954 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
956 for (i = 0; peers[search_peer].search_str != NULL; i++)
958 search_peer = (search_peer + 1) % num_peers;
960 GNUNET_abort (); /* we ran out of peers, must be a bug */
962 peers[search_peer].search_str = search_strings[me];
963 peers[search_peer].search_str_matched = GNUNET_NO;
964 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
965 reannounce_period_max,
968 (void *) search_peer);
973 * Task to start the daemons on each peer so that the regexes are announced
977 * @param tc the task context
980 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
984 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting announce.\n");
986 for (i = 0; i < init_parallel_searches; i++)
988 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
989 " scheduling announce %u\n",
991 (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
997 * Start announcing the next regex in the DHT.
999 * @param cls Closure (unused).
1000 * @param tc TaskContext.
1003 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1005 struct RegexPeer *peer;
1007 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
1008 next_search >= num_peers)
1011 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
1012 peer = &peers[next_search];
1014 GNUNET_TESTBED_peer_manage_service (NULL,
1021 parallel_searches++;
1025 * DHT connect callback. Called when we are connected to the dht service for
1026 * the peer in 'cls'. If successfull we connect to the stats service of this
1027 * peer and then try to match the search string of this peer.
1029 * @param cls internal peer id.
1030 * @param op operation handle.
1031 * @param ca_result connect adapter result.
1032 * @param emsg error message.
1035 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1036 void *ca_result, const char *emsg)
1038 struct RegexPeer *peer = (struct RegexPeer *) cls;
1040 if (NULL != emsg || NULL == op || NULL == ca_result)
1042 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1046 GNUNET_assert (NULL != peer->dht_handle);
1047 GNUNET_assert (peer->op_handle == op);
1048 GNUNET_assert (peer->dht_handle == ca_result);
1050 peer->search_str_matched = GNUNET_NO;
1051 peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
1053 ®ex_found_handler, peer,
1055 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1060 * DHT connect adapter. Opens a connection to the dht service.
1062 * @param cls Closure (peer).
1063 * @param cfg Configuration handle.
1068 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1070 struct RegexPeer *peer = cls;
1072 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1074 return peer->dht_handle;
1079 * Adapter function called to destroy a connection to the dht service.
1081 * @param cls Closure (peer).
1082 * @param op_result Service handle returned from the connect adapter.
1085 dht_da (void *cls, void *op_result)
1087 struct RegexPeer *peer = (struct RegexPeer *) cls;
1089 GNUNET_assert (peer->dht_handle == op_result);
1091 if (NULL != peer->search_handle)
1093 GNUNET_REGEX_search_cancel (peer->search_handle);
1094 peer->search_handle = NULL;
1097 if (NULL != peer->dht_handle)
1099 GNUNET_DHT_disconnect (peer->dht_handle);
1100 peer->dht_handle = NULL;
1106 * Signature of a main function for a testcase.
1109 * @param num_peers_ number of peers in 'peers'
1110 * @param peers handle to peers run in the testbed. NULL upon timeout (see
1111 * GNUNET_TESTBED_test_run()).
1112 * @param links_succeeded the number of overlay link connection attempts that
1114 * @param links_failed the number of overlay link connection attempts that
1118 test_master (void *cls,
1119 unsigned int num_peers_,
1120 struct GNUNET_TESTBED_Peer **testbed_peers,
1121 unsigned int links_succeeded,
1122 unsigned int links_failed)
1126 GNUNET_assert (num_peers_ == num_peers);
1128 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1129 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1130 "Testbed started in %s\n",
1131 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1133 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1135 GNUNET_SCHEDULER_cancel (abort_task);
1136 abort_task = GNUNET_SCHEDULER_NO_TASK;
1139 for (i = 0; i < num_peers; i++)
1141 peers[i].peer_handle = testbed_peers[i];
1143 GNUNET_SCHEDULER_add_now (&do_announce, NULL);
1144 search_timeout_task =
1145 GNUNET_SCHEDULER_add_delayed (search_timeout_time, &search_timed_out, NULL);
1149 * Function that will be called whenever something in the testbed changes.
1151 * @param cls closure, NULL
1152 * @param event information on what is happening
1155 master_controller_cb (void *cls,
1156 const struct GNUNET_TESTBED_EventInformation *event)
1158 switch (event->type)
1160 case GNUNET_TESTBED_ET_CONNECT:
1163 case GNUNET_TESTBED_ET_PEER_START:
1173 /******************************************************************************/
1174 /*************************** TESTBED PEER SETUP *****************************/
1175 /******************************************************************************/
1179 * Load search strings from given filename. One search string per line.
1181 * @param filename filename of the file containing the search strings.
1182 * @param strings set of strings loaded from file. Caller needs to free this
1183 * if number returned is greater than zero.
1184 * @param limit upper limit on the number of strings read from the file
1185 * @return number of strings found in the file. GNUNET_SYSERR on error.
1188 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1193 unsigned int offset;
1197 if (NULL == filename)
1199 return GNUNET_SYSERR;
1202 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1204 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1205 "Could not find search strings file %s\n", filename);
1206 return GNUNET_SYSERR;
1208 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1212 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1213 return GNUNET_SYSERR;
1215 data = GNUNET_malloc (filesize);
1216 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1221 return GNUNET_SYSERR;
1226 while (offset < (filesize - 1) && str_cnt < limit)
1229 if (((data[offset] == '\n')) && (buf != &data[offset]))
1231 data[offset] = '\0';
1233 buf = &data[offset + 1];
1235 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1236 buf = &data[offset + 1];
1238 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1240 for (i = 0; i < str_cnt; i++)
1242 GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1243 offset += strlen (&data[offset]) + 1;
1251 * Main function that will be run by the scheduler.
1253 * @param cls closure
1254 * @param args remaining command-line arguments
1255 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1256 * @param config configuration
1259 run (void *cls, char *const *args, const char *cfgfile,
1260 const struct GNUNET_CONFIGURATION_Handle *config)
1262 unsigned int nsearchstrs;
1265 in_shutdown = GNUNET_NO;
1270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1271 _("No configuration file given. Exiting\n"));
1272 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1275 cfg = GNUNET_CONFIGURATION_dup (config);
1277 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
1281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1282 _("Configuration option \"regex_prefix\" missing. Exiting\n"));
1283 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1287 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
1288 "PARALLEL_SEARCHES",
1289 &init_parallel_searches))
1291 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1292 "Configuration option \"PARALLEL_SEARCHES\" missing."
1293 " Using default (%d)\n", 10);
1294 init_parallel_searches = 10;
1297 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
1298 "REANNOUNCE_PERIOD_MAX",
1299 &reannounce_period_max))
1301 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1302 "reannounce_period_max not given. Using 10 minutes.\n");
1303 reannounce_period_max =
1304 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
1307 /* Check arguments */
1308 if (NULL == policy_dir)
1310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1311 _("No policy directory specified on command line. Exiting.\n"));
1314 if (GNUNET_YES != GNUNET_DISK_directory_test (policy_dir, GNUNET_YES))
1316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1317 _("Specified policies directory does not exist. Exiting.\n"));
1318 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1321 if (-1 == (num_peers = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL)))
1323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1324 _("No files found in `%s'\n"),
1328 GNUNET_CONFIGURATION_set_value_string (cfg, "REGEXPROFILER",
1329 "POLICY_DIR", policy_dir);
1330 if (GNUNET_YES != GNUNET_DISK_file_test (strings_file))
1332 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1333 _("No search strings file given. Exiting.\n"));
1334 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1337 nsearchstrs = load_search_strings (strings_file,
1339 num_search_strings);
1340 if (num_search_strings != nsearchstrs)
1342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1343 _("Error loading search strings."
1344 "Given file does not contain enough strings. Exiting.\n"));
1345 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1348 if (0 >= num_search_strings || NULL == search_strings)
1350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1351 _("Error loading search strings. Exiting.\n"));
1352 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1355 for (i = 0; i < num_search_strings; i++)
1356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1357 "search string: %s\n",
1361 if ( (NULL != data_filename) &&
1362 (NULL == (data_file =
1363 GNUNET_DISK_file_open (data_filename,
1364 GNUNET_DISK_OPEN_READWRITE |
1365 GNUNET_DISK_OPEN_TRUNCATE |
1366 GNUNET_DISK_OPEN_CREATE,
1367 GNUNET_DISK_PERM_USER_READ |
1368 GNUNET_DISK_PERM_USER_WRITE))) )
1370 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1376 /* Initialize peers */
1377 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1378 for (i = 0; i < num_peers; i++)
1384 /* For feedback about the start process activate these and pass master_cb */
1385 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1386 // event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1387 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1388 // event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1389 prof_start_time = GNUNET_TIME_absolute_get ();
1390 GNUNET_TESTBED_run (hosts_file,
1394 &master_controller_cb,
1395 NULL, /* master_controller_cb cls */
1397 NULL); /* test_master cls */
1399 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1400 (GNUNET_TIME_UNIT_MINUTES, 15),
1409 * @param argc argument count
1410 * @param argv argument values
1411 * @return 0 on success
1414 main (int argc, char *const *argv)
1416 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1417 {'o', "output-file", "FILENAME",
1418 gettext_noop ("name of the file for writing statistics"),
1419 GNUNET_YES, &GNUNET_GETOPT_set_string, &data_filename},
1420 {'t', "matching-timeout", "TIMEOUT",
1421 gettext_noop ("wait TIMEOUT before ending the experiment"),
1422 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time },
1423 {'n', "num-search-strings", "COUNT",
1424 gettext_noop ("number of search strings to read from search strings file"),
1425 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
1426 {'p', "policy-dir", "DIRECTORY",
1427 gettext_noop ("directory with policy files"),
1428 GNUNET_YES, &GNUNET_GETOPT_set_filename, &policy_dir },
1429 {'s', "strings-file", "FILENAME",
1430 gettext_noop ("name of file with input strings"),
1431 GNUNET_YES, &GNUNET_GETOPT_set_filename, &strings_file },
1432 {'H', "hosts-file", "FILENAME",
1433 gettext_noop ("name of file with hosts' names"),
1434 GNUNET_NO, &GNUNET_GETOPT_set_filename, &hosts_file },
1435 GNUNET_GETOPT_OPTION_END
1439 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1441 result = GNUNET_SYSERR;
1443 GNUNET_PROGRAM_run (argc, argv,
1444 "gnunet-regex-profiler",
1445 _("Profiler for regex"),
1446 options, &run, NULL);
1447 if (GNUNET_OK != ret)
1449 if (GNUNET_OK != result)