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 "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 GNUNET_SCHEDULER_TaskIdentifier 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 GNUNET_SCHEDULER_TaskIdentifier abort_task;
217 * Shutdown task identifier
219 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
222 * Host registration task identifier
224 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
227 * Global event mask for all testbed events
229 static uint64_t event_mask;
232 * The starting time of a profiling step
234 static struct GNUNET_TIME_Absolute prof_start_time;
237 * Duration profiling step has taken
239 static struct GNUNET_TIME_Relative prof_time;
242 * Number of peers to be started by the profiler
244 static unsigned int num_peers;
247 * Global testing status
252 * current state of profiling
257 * Folder where policy files are stored.
259 static char * policy_dir;
262 * File with hostnames where to execute the test.
264 static char *hosts_file;
267 * File with the strings to look for.
269 static char *strings_file;
272 * Search strings (num_peers of them).
274 static char **search_strings;
277 * How many searches are we going to start in parallel
279 static long long unsigned int init_parallel_searches;
282 * How many searches are running in parallel
284 static unsigned int parallel_searches;
287 * Number of strings found in the published regexes.
289 static unsigned int strings_found;
292 * Index of peer to start next announce/search.
294 static unsigned int next_search;
297 * Search timeout task identifier.
299 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
302 * Search timeout in seconds.
304 static struct GNUNET_TIME_Relative search_timeout_time = { 60000 };
307 * File to log statistics to.
309 static struct GNUNET_DISK_FileHandle *data_file;
312 * Filename to log statistics to.
314 static char *data_filename;
317 * Prefix used for regex announcing. We need to prefix the search
318 * strings with it, in order to find something.
320 static char * regex_prefix;
323 * What's the maximum regex reannounce period.
325 static struct GNUNET_TIME_Relative reannounce_period_max;
328 /******************************************************************************/
329 /****************************** DECLARATIONS ********************************/
330 /******************************************************************************/
333 * DHT connect callback.
335 * @param cls internal peer id.
336 * @param op operation handle.
337 * @param ca_result connect adapter result.
338 * @param emsg error message.
341 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
342 void *ca_result, const char *emsg);
345 * DHT connect adapter.
347 * @param cls not used.
348 * @param cfg configuration handle.
353 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
357 * Adapter function called to destroy a connection to
361 * @param op_result service handle returned from the connect adapter
364 dht_da (void *cls, void *op_result);
368 * Function called by testbed once we are connected to stats
369 * service. Get the statistics for the services of interest.
371 * @param cls the 'struct RegexPeer' for which we connected to stats
372 * @param op connect operation handle
373 * @param ca_result handle to stats service
374 * @param emsg error message on failure
377 stats_connect_cb (void *cls,
378 struct GNUNET_TESTBED_Operation *op,
384 * Start announcing the next regex in the DHT.
386 * @param cls Index of the next peer in the peers array.
387 * @param tc TaskContext.
390 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
393 /******************************************************************************/
394 /******************************** SHUTDOWN **********************************/
395 /******************************************************************************/
402 * @param tc the task context
405 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
407 struct RegexPeer *peer;
408 unsigned int peer_cnt;
409 unsigned int search_str_cnt;
410 char output_buffer[512];
413 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
414 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
415 GNUNET_SCHEDULER_cancel (abort_task);
416 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
417 GNUNET_SCHEDULER_cancel (register_hosts_task);
419 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
421 peer = &peers[peer_cnt];
423 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
425 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
427 GNUNET_snprintf (output_buffer,
428 sizeof (output_buffer),
429 "%p Search string not found: %s (%d)\n"
430 "%p On peer: %u (%p)\n"
432 peer, peer->search_str, peer->search_str_matched,
433 peer, peer->id, peer,
435 GNUNET_STRINGS_relative_time_to_string (prof_time,
437 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
441 if (NULL != peers[peer_cnt].op_handle)
442 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
445 if (NULL != data_file)
446 GNUNET_DISK_file_close (data_file);
448 for (search_str_cnt = 0;
449 search_str_cnt < num_peers && NULL != search_strings;
452 GNUNET_free_non_null (search_strings[search_str_cnt]);
454 GNUNET_free_non_null (search_strings);
456 if (NULL != reg_handle)
457 GNUNET_TESTBED_cancel_registration (reg_handle);
460 GNUNET_TESTBED_controller_disconnect (mc);
462 GNUNET_TESTBED_controller_stop (mc_proc);
464 GNUNET_CONFIGURATION_destroy (cfg);
466 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
471 * abort task to run on test timed out
474 * @param tc the task context
477 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
479 unsigned long i = (unsigned long) cls;
481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting from line %lu...\n", i);
482 abort_task = GNUNET_SCHEDULER_NO_TASK;
483 result = GNUNET_SYSERR;
484 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
485 GNUNET_SCHEDULER_cancel (shutdown_task);
486 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
490 /******************************************************************************/
491 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
492 /******************************************************************************/
495 * Adapter function called to establish a connection to
496 * statistics service.
499 * @param cfg configuration of the peer to connect to; will be available until
500 * GNUNET_TESTBED_operation_done() is called on the operation returned
501 * from GNUNET_TESTBED_service_connect()
502 * @return service handle to return in 'op_result', NULL on error
505 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
507 return GNUNET_STATISTICS_create ("<driver>", cfg);
512 * Adapter function called to destroy a connection to
513 * statistics service.
516 * @param op_result service handle returned from the connect adapter
519 stats_da (void *cls, void *op_result)
521 struct RegexPeer *peer = cls;
523 GNUNET_assert (op_result == peer->stats_handle);
525 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
526 peer->stats_handle = NULL;
531 * Process statistic values. Write all values to global 'data_file', if present.
534 * @param subsystem name of subsystem that created the statistic
535 * @param name the name of the datum
536 * @param value the current value
537 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
538 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
541 stats_iterator (void *cls, const char *subsystem, const char *name,
542 uint64_t value, int is_persistent)
544 struct RegexPeer *peer = cls;
545 char output_buffer[512];
548 if (NULL == data_file)
550 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
551 "%p -> %s [%s]: %llu\n",
552 peer, subsystem, name, value);
556 GNUNET_snprintf (output_buffer,
557 sizeof (output_buffer),
560 subsystem, value, name);
561 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
562 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
569 * Stats callback. Finish the stats testbed operation and when all stats have
570 * been iterated, shutdown the profiler.
573 * @param success GNUNET_OK if statistics were
574 * successfully obtained, GNUNET_SYSERR if not.
580 static unsigned int peer_cnt;
581 struct RegexPeer *peer = cls;
583 if (GNUNET_OK != success)
585 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
586 "Getting statistics for peer %u failed!\n",
591 GNUNET_assert (NULL != peer->op_handle);
593 GNUNET_TESTBED_operation_done (peer->op_handle);
594 peer->op_handle = NULL;
597 peer = &peers[peer_cnt];
599 if (peer_cnt == num_peers)
601 struct GNUNET_TIME_Relative delay = { 100 };
602 shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
608 GNUNET_TESTBED_service_connect (NULL,
621 * Function called by testbed once we are connected to stats
622 * service. Get the statistics for the services of interest.
624 * @param cls the 'struct RegexPeer' for which we connected to stats
625 * @param op connect operation handle
626 * @param ca_result handle to stats service
627 * @param emsg error message on failure
630 stats_connect_cb (void *cls,
631 struct GNUNET_TESTBED_Operation *op,
635 struct RegexPeer *peer = cls;
637 if (NULL == ca_result || NULL != emsg)
639 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
640 "Failed to connect to statistics service on peer %u: %s\n",
643 peer->stats_handle = NULL;
647 peer->stats_handle = ca_result;
649 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
650 GNUNET_TIME_UNIT_FOREVER_REL,
652 &stats_iterator, peer))
654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
655 "Could not get statistics of peer %u!\n", peer->id);
661 * Task to collect all statistics from all peers, will shutdown the
662 * profiler, when done.
665 * @param tc the task context
668 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
670 struct RegexPeer *peer = &peers[0];
672 GNUNET_assert (NULL != peer->peer_handle);
675 GNUNET_TESTBED_service_connect (NULL,
686 /******************************************************************************/
687 /************************ REGEX FIND CONNECTIONS **************************/
688 /******************************************************************************/
692 * Start searching for the next string in the DHT.
694 * @param cls Index of the next peer in the peers array.
695 * @param tc TaskContext.
698 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
702 * Method called when we've found a peer that announced a regex
703 * that matches our search string. Now get the statistics.
705 * @param cls Closure provided in REGEX_INTERNAL_search.
706 * @param id Peer providing a regex that matches the string.
707 * @param get_path Path of the get request.
708 * @param get_path_length Lenght of get_path.
709 * @param put_path Path of the put request.
710 * @param put_path_length Length of the put_path.
713 regex_found_handler (void *cls,
714 const struct GNUNET_PeerIdentity *id,
715 const struct GNUNET_PeerIdentity *get_path,
716 unsigned int get_path_length,
717 const struct GNUNET_PeerIdentity *put_path,
718 unsigned int put_path_length)
720 struct RegexPeer *peer = cls;
721 char output_buffer[512];
724 if (GNUNET_YES == peer->search_str_matched)
726 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
727 "String %s on peer %u already matched!\n",
728 peer->search_str, peer->id);
735 if (GNUNET_SCHEDULER_NO_TASK != peer->timeout)
737 GNUNET_SCHEDULER_cancel (peer->timeout);
738 peer->timeout = GNUNET_SCHEDULER_NO_TASK;
739 if (GNUNET_NO == in_shutdown)
740 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
745 // FIXME not possible right now
746 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
747 "String matching timed out for string %s on peer %u (%i/%i)\n",
748 peer->search_str, peer->id, strings_found, num_peers);
749 peer->search_str_matched = GNUNET_SYSERR;
753 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
755 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
756 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
757 peer->search_str, peer->id,
758 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
759 strings_found, num_peers, parallel_searches);
761 peer->search_str_matched = GNUNET_YES;
763 if (NULL != data_file)
766 GNUNET_snprintf (output_buffer,
767 sizeof (output_buffer),
769 "%p Search string: %s\n"
770 "%p Search duration: %s\n\n",
772 peer, peer->search_str,
774 GNUNET_STRINGS_relative_time_to_string (prof_time,
777 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
778 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
782 GNUNET_TESTBED_operation_done (peer->op_handle);
783 peer->op_handle = NULL;
785 if (strings_found == num_peers)
787 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
788 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
789 "All strings successfully matched in %s\n",
790 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
792 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
794 GNUNET_SCHEDULER_cancel (search_timeout_task);
795 search_timeout_task = GNUNET_SCHEDULER_NO_TASK;
798 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
799 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
805 * Connect by string timeout task. This will cancel the profiler after the
806 * specified timeout 'search_timeout'.
809 * @param tc the task context
812 search_timed_out (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
816 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
817 "Finding matches to all strings did not succeed after %s.\n",
818 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
820 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
821 "Found %i of %i strings\n", strings_found, num_peers);
823 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
824 "Search timed out after %s."
825 "Collecting stats and shutting down.\n",
826 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
829 in_shutdown = GNUNET_YES;
830 for (i = 0; i < num_peers; i++)
832 if (NULL != peers[i].op_handle)
834 GNUNET_TESTBED_operation_done (peers[i].op_handle);
835 peers[i].op_handle = NULL;
838 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
843 * Search timed out. It might still complete in the future,
844 * but we should start another one.
846 * @param cls Index of the next peer in the peers array.
847 * @param tc TaskContext.
850 find_timed_out (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
852 struct RegexPeer *p = cls;
854 p->timeout = GNUNET_SCHEDULER_NO_TASK;
856 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
858 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
859 "Searching for string \"%s\" on peer %d timed out.\n",
862 if (GNUNET_NO == in_shutdown)
863 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
868 * Start searching for a string in the DHT.
870 * @param cls Index of the next peer in the peers array.
871 * @param tc TaskContext.
874 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
876 unsigned int search_peer = (unsigned int) (long) cls;
878 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
879 search_peer >= num_peers ||
880 GNUNET_YES == in_shutdown)
883 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
884 "Searching for string \"%s\" on peer %d (%u||)\n",
885 peers[search_peer].search_str,
889 peers[search_peer].op_handle =
890 GNUNET_TESTBED_service_connect (NULL,
891 peers[search_peer].peer_handle,
897 &peers[search_peer]);
898 GNUNET_assert (NULL != peers[search_peer].op_handle);
899 peers[search_peer].timeout = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
901 &peers[search_peer]);
908 * Callback called when testbed has started the daemon we asked for.
911 * @param op the operation handle
912 * @param emsg NULL on success; otherwise an error description
915 daemon_started (void *cls, struct GNUNET_TESTBED_Operation *op,
918 struct RegexPeer *peer = (struct RegexPeer *) cls;
919 unsigned long search_peer;
922 GNUNET_TESTBED_operation_done (peer->daemon_op);
923 peer->daemon_op = NULL;
926 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
927 "Failed to start/stop daemon at peer %u: %s\n", peer->id, emsg);
931 /* Find a peer to look for a string matching the regex announced */
932 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
934 for (i = 0; peers[search_peer].search_str != NULL; i++)
936 search_peer = (search_peer + 1) % num_peers;
938 GNUNET_abort (); /* we ran out of peers, must be a bug */
940 peers[search_peer].search_str = search_strings[peer->id];
941 peers[search_peer].search_str_matched = GNUNET_NO;
942 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
943 reannounce_period_max,
946 (void *) search_peer);
951 * Task to start the daemons on each peer so that the regexes are announced
955 * @param tc the task context
958 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
962 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting announce.\n");
964 for (i = 0; i < init_parallel_searches; i++)
966 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
967 " scheduling announce %u\n",
969 (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
975 * Start announcing the next regex in the DHT.
977 * @param cls Closure (unused).
978 * @param tc TaskContext.
981 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
983 struct RegexPeer *peer;
985 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
987 if (next_search >= num_peers)
989 if (strings_found != num_peers)
991 struct GNUNET_TIME_Relative new_delay;
992 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
993 GNUNET_SCHEDULER_cancel (search_timeout_task);
994 new_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15);
995 search_timeout_task = GNUNET_SCHEDULER_add_delayed (new_delay,
1002 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
1003 peer = &peers[next_search];
1005 GNUNET_TESTBED_peer_manage_service (NULL,
1012 parallel_searches++;
1016 * DHT connect callback. Called when we are connected to the dht service for
1017 * the peer in 'cls'. If successfull we connect to the stats service of this
1018 * peer and then try to match the search string of this peer.
1020 * @param cls internal peer id.
1021 * @param op operation handle.
1022 * @param ca_result connect adapter result.
1023 * @param emsg error message.
1026 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1027 void *ca_result, const char *emsg)
1029 struct RegexPeer *peer = (struct RegexPeer *) cls;
1031 if (NULL != emsg || NULL == op || NULL == ca_result)
1033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1037 GNUNET_assert (NULL != peer->dht_handle);
1038 GNUNET_assert (peer->op_handle == op);
1039 GNUNET_assert (peer->dht_handle == ca_result);
1041 peer->search_str_matched = GNUNET_NO;
1042 peer->search_handle = REGEX_INTERNAL_search (peer->dht_handle,
1044 ®ex_found_handler, peer,
1046 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1051 * DHT connect adapter. Opens a connection to the dht service.
1053 * @param cls Closure (peer).
1054 * @param cfg Configuration handle.
1059 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1061 struct RegexPeer *peer = cls;
1063 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1065 return peer->dht_handle;
1070 * Adapter function called to destroy a connection to the dht service.
1072 * @param cls Closure (peer).
1073 * @param op_result Service handle returned from the connect adapter.
1076 dht_da (void *cls, void *op_result)
1078 struct RegexPeer *peer = (struct RegexPeer *) cls;
1080 GNUNET_assert (peer->dht_handle == op_result);
1082 if (NULL != peer->search_handle)
1084 REGEX_INTERNAL_search_cancel (peer->search_handle);
1085 peer->search_handle = NULL;
1088 if (NULL != peer->dht_handle)
1090 GNUNET_DHT_disconnect (peer->dht_handle);
1091 peer->dht_handle = NULL;
1097 * Signature of a main function for a testcase.
1100 * @param num_peers_ number of peers in 'peers'
1101 * @param testbed_peers handle to peers run in the testbed. NULL upon timeout (see
1102 * GNUNET_TESTBED_test_run()).
1103 * @param links_succeeded the number of overlay link connection attempts that
1105 * @param links_failed the number of overlay link connection attempts that
1109 test_master (void *cls,
1110 unsigned int num_peers_,
1111 struct GNUNET_TESTBED_Peer **testbed_peers,
1112 unsigned int links_succeeded,
1113 unsigned int links_failed)
1117 GNUNET_assert (num_peers_ == num_peers);
1119 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1120 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1121 "Testbed started in %s\n",
1122 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1124 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1126 GNUNET_SCHEDULER_cancel (abort_task);
1127 abort_task = GNUNET_SCHEDULER_NO_TASK;
1130 for (i = 0; i < num_peers; i++)
1132 peers[i].peer_handle = testbed_peers[i];
1135 GNUNET_CONFIGURATION_get_value_yesno (cfg, "DHT", "DISABLE_TRY_CONNECT"))
1137 struct GNUNET_TIME_Relative settle_time;
1140 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1142 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1143 "Waiting for DHT for %s to settle new connections.\n\n",
1144 GNUNET_STRINGS_relative_time_to_string(settle_time, GNUNET_NO));
1145 GNUNET_SCHEDULER_add_delayed (settle_time, &do_announce, NULL);
1149 GNUNET_SCHEDULER_add_now (&do_announce, NULL);
1151 search_timeout_task =
1152 GNUNET_SCHEDULER_add_delayed (search_timeout_time, &search_timed_out, NULL);
1156 * Function that will be called whenever something in the testbed changes.
1158 * @param cls closure, NULL
1159 * @param event information on what is happening
1162 master_controller_cb (void *cls,
1163 const struct GNUNET_TESTBED_EventInformation *event)
1165 switch (event->type)
1167 case GNUNET_TESTBED_ET_CONNECT:
1170 case GNUNET_TESTBED_ET_PEER_START:
1180 /******************************************************************************/
1181 /*************************** TESTBED PEER SETUP *****************************/
1182 /******************************************************************************/
1186 * Load search strings from given filename. One search string per line.
1188 * @param filename filename of the file containing the search strings.
1189 * @param strings set of strings loaded from file. Caller needs to free this
1190 * if number returned is greater than zero.
1191 * @param limit upper limit on the number of strings read from the file
1192 * @return number of strings found in the file. GNUNET_SYSERR on error.
1195 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1200 unsigned int offset;
1204 if (NULL == filename)
1206 return GNUNET_SYSERR;
1209 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1212 "Could not find search strings file %s\n", filename);
1213 return GNUNET_SYSERR;
1215 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1220 return GNUNET_SYSERR;
1222 data = GNUNET_malloc (filesize);
1223 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1226 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1228 return GNUNET_SYSERR;
1233 while (offset < (filesize - 1) && str_cnt < limit)
1236 if (((data[offset] == '\n')) && (buf != &data[offset]))
1238 data[offset] = '\0';
1240 buf = &data[offset + 1];
1242 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1243 buf = &data[offset + 1];
1245 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1247 for (i = 0; i < str_cnt; i++)
1249 GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1250 offset += strlen (&data[offset]) + 1;
1258 * Main function that will be run by the scheduler.
1260 * @param cls closure
1261 * @param args remaining command-line arguments
1262 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1263 * @param config configuration
1266 run (void *cls, char *const *args, const char *cfgfile,
1267 const struct GNUNET_CONFIGURATION_Handle *config)
1269 unsigned int nsearchstrs;
1271 struct GNUNET_TIME_Relative abort_time;
1273 in_shutdown = GNUNET_NO;
1278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1279 _("No configuration file given. Exiting\n"));
1280 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1283 cfg = GNUNET_CONFIGURATION_dup (config);
1285 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
1289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1290 _("Configuration option \"regex_prefix\" missing. Exiting\n"));
1291 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1295 GNUNET_CONFIGURATION_get_value_number (cfg, "REGEXPROFILER",
1296 "PARALLEL_SEARCHES",
1297 &init_parallel_searches))
1299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1300 "Configuration option \"PARALLEL_SEARCHES\" missing."
1301 " Using default (%d)\n", 10);
1302 init_parallel_searches = 10;
1305 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
1306 "REANNOUNCE_PERIOD_MAX",
1307 &reannounce_period_max))
1309 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1310 "reannounce_period_max not given. Using 10 minutes.\n");
1311 reannounce_period_max =
1312 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
1315 /* Check arguments */
1316 if (NULL == policy_dir)
1318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1319 _("No policy directory specified on command line. Exiting.\n"));
1322 if (GNUNET_YES != GNUNET_DISK_directory_test (policy_dir, GNUNET_YES))
1324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1325 _("Specified policies directory does not exist. Exiting.\n"));
1326 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1329 if (-1 == (num_peers = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL)))
1331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1332 _("No files found in `%s'\n"),
1336 GNUNET_CONFIGURATION_set_value_string (cfg, "REGEXPROFILER",
1337 "POLICY_DIR", policy_dir);
1338 if (GNUNET_YES != GNUNET_DISK_file_test (strings_file))
1340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1341 _("No search strings file given. Exiting.\n"));
1342 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1345 nsearchstrs = load_search_strings (strings_file,
1348 if (num_peers != nsearchstrs)
1350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1351 "Error loading search strings.\n");
1352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1353 "File (%s) does not contain enough strings (%u/%u).\n",
1354 strings_file, nsearchstrs, num_peers);
1355 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1358 if (0 >= num_peers || NULL == search_strings)
1360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1361 _("Error loading search strings. Exiting.\n"));
1362 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1365 for (i = 0; i < num_peers; i++)
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 "search string: %s\n",
1371 if ( (NULL != data_filename) &&
1372 (NULL == (data_file =
1373 GNUNET_DISK_file_open (data_filename,
1374 GNUNET_DISK_OPEN_READWRITE |
1375 GNUNET_DISK_OPEN_TRUNCATE |
1376 GNUNET_DISK_OPEN_CREATE,
1377 GNUNET_DISK_PERM_USER_READ |
1378 GNUNET_DISK_PERM_USER_WRITE))) )
1380 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1386 /* Initialize peers */
1387 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1388 for (i = 0; i < num_peers; i++)
1393 GNUNET_CONFIGURATION_set_value_number (cfg,
1394 "TESTBED", "OVERLAY_RANDOM_LINKS",
1396 GNUNET_CONFIGURATION_set_value_number (cfg,
1398 (long long unsigned)
1399 (log (num_peers) / log (2.0)));
1401 /* For feedback about the start process activate these and pass master_cb */
1402 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1403 // event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1404 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1405 // event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);
1406 prof_start_time = GNUNET_TIME_absolute_get ();
1407 GNUNET_TESTBED_run (hosts_file,
1411 &master_controller_cb,
1412 NULL, /* master_controller_cb cls */
1414 NULL); /* test_master cls */
1416 GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
1421 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15);
1423 abort_time = GNUNET_TIME_relative_add (abort_time, GNUNET_TIME_UNIT_MINUTES);
1425 GNUNET_SCHEDULER_add_delayed (abort_time,
1434 * @param argc argument count
1435 * @param argv argument values
1436 * @return 0 on success
1439 main (int argc, char *const *argv)
1441 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1442 {'o', "output-file", "FILENAME",
1443 gettext_noop ("name of the file for writing statistics"),
1444 GNUNET_YES, &GNUNET_GETOPT_set_string, &data_filename},
1445 {'t', "matching-timeout", "TIMEOUT",
1446 gettext_noop ("wait TIMEOUT before ending the experiment"),
1447 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time},
1448 {'p', "policy-dir", "DIRECTORY",
1449 gettext_noop ("directory with policy files"),
1450 GNUNET_YES, &GNUNET_GETOPT_set_filename, &policy_dir},
1451 {'s', "strings-file", "FILENAME",
1452 gettext_noop ("name of file with input strings"),
1453 GNUNET_YES, &GNUNET_GETOPT_set_filename, &strings_file},
1454 {'H', "hosts-file", "FILENAME",
1455 gettext_noop ("name of file with hosts' names"),
1456 GNUNET_YES, &GNUNET_GETOPT_set_filename, &hosts_file},
1457 GNUNET_GETOPT_OPTION_END
1461 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1463 result = GNUNET_SYSERR;
1465 GNUNET_PROGRAM_run (argc, argv,
1466 "gnunet-regex-profiler",
1467 _("Profiler for regex"),
1468 options, &run, NULL);
1469 if (GNUNET_OK != ret)
1471 if (GNUNET_OK != result)