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 * Host on which the peer is running.
136 struct GNUNET_TESTBED_Host *host_handle;
139 * Filename of the peer's policy file.
144 * Peer's search string.
146 const char *search_str;
149 * Set to GNUNET_YES if the peer successfully matched the above
150 * search string. GNUNET_NO if the string could not be matched
151 * during the profiler run. GNUNET_SYSERR if the string matching
152 * timed out. Undefined if search_str is NULL
154 int search_str_matched;
159 struct GNUNET_DHT_Handle *dht_handle;
162 * Handle to a running regex search.
164 struct GNUNET_REGEX_search_handle *search_handle;
167 * Testbed operation handle for DHT.
169 struct GNUNET_TESTBED_Operation *op_handle;
172 * Peers's statistics handle.
174 struct GNUNET_STATISTICS_Handle *stats_handle;
177 * Testbed operation handle for the statistics service.
179 struct GNUNET_TESTBED_Operation *stats_op_handle;
182 * The starting time of a profiling step.
184 struct GNUNET_TIME_Absolute prof_start_time;
189 GNUNET_SCHEDULER_TaskIdentifier timeout;
194 struct GNUNET_TESTBED_Operation *daemon_op;
199 * The array of peers; we fill this as the peers are given to us by the testbed
201 static struct RegexPeer *peers;
204 * Host registration handle
206 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
209 * Handle to the master controller process
211 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
214 * Handle to the master controller
216 static struct GNUNET_TESTBED_Controller *mc;
219 * Handle to global configuration
221 static struct GNUNET_CONFIGURATION_Handle *cfg;
224 * Abort task identifier
226 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
229 * Shutdown task identifier
231 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
234 * Host registration task identifier
236 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
239 * Global event mask for all testbed events
241 static uint64_t event_mask;
244 * The starting time of a profiling step
246 static struct GNUNET_TIME_Absolute prof_start_time;
249 * Duration profiling step has taken
251 static struct GNUNET_TIME_Relative prof_time;
254 * Number of peers to be started by the profiler
256 static unsigned int num_peers;
259 * Factor of number of links. num_links = num_peers * linking_factor.
261 static unsigned int linking_factor;
264 * Global testing status
269 * current state of profiling
274 * Folder where policy files are stored.
276 static char * policy_dir;
281 static char **search_strings;
284 * Number of search strings.
286 static int num_search_strings;
289 * How many searches are running in parallel
291 static unsigned int parallel_searches;
294 * Number of peers found with search strings.
296 static unsigned int peers_found;
299 * Index of peer to start next announce/search.
301 static unsigned int next_search;
304 * Search timeout task identifier.
306 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
309 * Search timeout in seconds.
311 static struct GNUNET_TIME_Relative search_timeout_time = { 60000 };
314 * How long do we wait before starting the search?
317 static struct GNUNET_TIME_Relative search_delay = { 60000 };
320 * File to log statistics to.
322 static struct GNUNET_DISK_FileHandle *data_file;
325 * Filename to log statistics to.
327 static char *data_filename;
330 * Maximal path compression length.
332 static unsigned int max_path_compression;
335 * Prefix used for regex announcing. We need to prefix the search
336 * strings with it, in order to find something.
338 static char * regex_prefix;
341 * What's the maximum regex reannounce period.
343 static struct GNUNET_TIME_Relative reannounce_period_max;
346 /******************************************************************************/
347 /****************************** DECLARATIONS ********************************/
348 /******************************************************************************/
351 * DHT connect callback.
353 * @param cls internal peer id.
354 * @param op operation handle.
355 * @param ca_result connect adapter result.
356 * @param emsg error message.
359 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
360 void *ca_result, const char *emsg);
363 * DHT connect adapter.
365 * @param cls not used.
366 * @param cfg configuration handle.
371 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
375 * Adapter function called to destroy a connection to
379 * @param op_result service handle returned from the connect adapter
382 dht_da (void *cls, void *op_result);
386 * Function called by testbed once we are connected to stats
387 * service. Get the statistics for the services of interest.
389 * @param cls the 'struct RegexPeer' for which we connected to stats
390 * @param op connect operation handle
391 * @param ca_result handle to stats service
392 * @param emsg error message on failure
395 stats_connect_cb (void *cls,
396 struct GNUNET_TESTBED_Operation *op,
402 * Task to collect all statistics from s, will shutdown the
403 * profiler, when done.
406 * @param tc the task context
409 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
413 * Start announcing the next regex in the DHT.
415 * @param cls Index of the next peer in the peers array.
416 * @param tc TaskContext.
419 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
422 /******************************************************************************/
423 /******************************** SHUTDOWN **********************************/
424 /******************************************************************************/
431 * @param tc the task context
434 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
436 struct RegexPeer *peer;
437 unsigned int peer_cnt;
438 unsigned int search_str_cnt;
439 char output_buffer[512];
442 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
443 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
444 GNUNET_SCHEDULER_cancel (abort_task);
445 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
446 GNUNET_SCHEDULER_cancel (register_hosts_task);
448 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
450 peer = &peers[peer_cnt];
452 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
454 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
456 GNUNET_snprintf (output_buffer,
457 sizeof (output_buffer),
458 "%p Search string not found: %s (%d)\n%p On peer: %u (%p)\n%p With policy file: %s\n%p After: %s\n",
459 peer, peer->search_str, peer->search_str_matched,
460 peer, peer->id, peer,
461 peer, peer->policy_file,
463 GNUNET_STRINGS_relative_time_to_string (prof_time,
465 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
466 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
469 if (NULL != peers[peer_cnt].op_handle)
470 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
471 if (NULL != peers[peer_cnt].stats_op_handle)
472 GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
475 if (NULL != data_file)
476 GNUNET_DISK_file_close (data_file);
478 for (search_str_cnt = 0;
479 search_str_cnt < num_search_strings && NULL != search_strings;
482 GNUNET_free_non_null (search_strings[search_str_cnt]);
484 GNUNET_free_non_null (search_strings);
486 if (NULL != reg_handle)
487 GNUNET_TESTBED_cancel_registration (reg_handle);
490 GNUNET_TESTBED_controller_disconnect (mc);
492 GNUNET_TESTBED_controller_stop (mc_proc);
494 GNUNET_CONFIGURATION_destroy (cfg);
496 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
501 * abort task to run on test timed out
504 * @param tc the task context
507 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
509 unsigned long i = (unsigned long) cls;
511 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting %lu...\n", i);
512 abort_task = GNUNET_SCHEDULER_NO_TASK;
513 result = GNUNET_SYSERR;
514 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
515 GNUNET_SCHEDULER_cancel (shutdown_task);
516 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
520 /******************************************************************************/
521 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
522 /******************************************************************************/
525 * Adapter function called to establish a connection to
526 * statistics service.
529 * @param cfg configuration of the peer to connect to; will be available until
530 * GNUNET_TESTBED_operation_done() is called on the operation returned
531 * from GNUNET_TESTBED_service_connect()
532 * @return service handle to return in 'op_result', NULL on error
535 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
537 return GNUNET_STATISTICS_create ("<driver>", cfg);
542 * Adapter function called to destroy a connection to
543 * statistics service.
546 * @param op_result service handle returned from the connect adapter
549 stats_da (void *cls, void *op_result)
551 struct RegexPeer *peer = cls;
553 GNUNET_assert (op_result == peer->stats_handle);
555 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
556 peer->stats_handle = NULL;
561 * Process statistic values. Write all values to global 'data_file', if present.
564 * @param subsystem name of subsystem that created the statistic
565 * @param name the name of the datum
566 * @param value the current value
567 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
568 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
571 stats_iterator (void *cls, const char *subsystem, const char *name,
572 uint64_t value, int is_persistent)
574 struct RegexPeer *peer = cls;
575 char output_buffer[512];
578 if (NULL == data_file)
580 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
581 "%p -> %s [%s]: %llu\n",
582 peer, subsystem, name, value);
586 GNUNET_snprintf (output_buffer,
587 sizeof (output_buffer),
590 subsystem, value, name);
591 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
592 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
599 * Stats callback. Finish the stats testbed operation and when all stats have
600 * been iterated, shutdown the profiler.
603 * @param success GNUNET_OK if statistics were
604 * successfully obtained, GNUNET_SYSERR if not.
610 static unsigned int peer_cnt;
611 struct RegexPeer *peer = cls;
613 if (GNUNET_OK != success)
615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
616 "Getting statistics for peer %u failed!\n",
621 GNUNET_assert (NULL != peer->stats_op_handle);
623 GNUNET_TESTBED_operation_done (peer->stats_op_handle);
624 peer->stats_op_handle = NULL;
627 peer = &peers[peer_cnt];
629 if (peer_cnt == num_peers)
631 struct GNUNET_TIME_Relative delay = { 100 };
632 shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
636 peer->stats_op_handle =
637 GNUNET_TESTBED_service_connect (NULL,
650 * Function called by testbed once we are connected to stats
651 * service. Get the statistics for the services of interest.
653 * @param cls the 'struct RegexPeer' for which we connected to stats
654 * @param op connect operation handle
655 * @param ca_result handle to stats service
656 * @param emsg error message on failure
659 stats_connect_cb (void *cls,
660 struct GNUNET_TESTBED_Operation *op,
664 struct RegexPeer *peer = cls;
666 if (NULL == ca_result || NULL != emsg)
668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
669 "Failed to connect to statistics service on peer %u: %s\n",
672 peer->stats_handle = NULL;
676 peer->stats_handle = ca_result;
678 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
679 GNUNET_TIME_UNIT_FOREVER_REL,
681 &stats_iterator, peer))
683 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
684 "Could not get statistics of peer %u!\n", peer->id);
690 * Task to collect all statistics from all peers, will shutdown the
691 * profiler, when done.
694 * @param tc the task context
697 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
699 struct RegexPeer *peer = &peers[0];
701 GNUNET_assert (NULL != peer->peer_handle);
703 peer->stats_op_handle =
704 GNUNET_TESTBED_service_connect (NULL,
715 /******************************************************************************/
716 /************************ REGEX FIND CONNECTIONS **************************/
717 /******************************************************************************/
721 * Start searching for the next string in the DHT.
723 * @param cls Index of the next peer in the peers array.
724 * @param tc TaskContext.
727 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
731 * Method called when we've found a peer that announced a regex
732 * that matches our search string. Now get the statistics.
734 * @param cls Closure provided in GNUNET_REGEX_search.
735 * @param id Peer providing a regex that matches the string.
736 * @param get_path Path of the get request.
737 * @param get_path_length Lenght of get_path.
738 * @param put_path Path of the put request.
739 * @param put_path_length Length of the put_path.
742 regex_found_handler (void *cls,
743 const struct GNUNET_PeerIdentity *id,
744 const struct GNUNET_PeerIdentity *get_path,
745 unsigned int get_path_length,
746 const struct GNUNET_PeerIdentity *put_path,
747 unsigned int put_path_length)
749 struct RegexPeer *peer = cls;
750 char output_buffer[512];
753 if (GNUNET_YES == peer->search_str_matched)
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "String %s on peer %u already matched!\n",
757 peer->search_str, peer->id);
764 if (GNUNET_SCHEDULER_NO_TASK != peer->timeout)
766 GNUNET_SCHEDULER_cancel (peer->timeout);
767 peer->timeout = GNUNET_SCHEDULER_NO_TASK;
768 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
773 // FIXME not possible right now
774 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
775 "String matching timed out for string %s on peer %u (%i/%i)\n",
776 peer->search_str, peer->id, peers_found, num_search_strings);
777 peer->search_str_matched = GNUNET_SYSERR;
781 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
783 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
784 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
785 peer->search_str, peer->id,
786 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
787 peers_found, num_search_strings, parallel_searches);
789 peer->search_str_matched = GNUNET_YES;
791 if (NULL != data_file)
794 GNUNET_snprintf (output_buffer,
795 sizeof (output_buffer),
796 "%p Peer: %u\n%p Host: %s\n%p Policy file: %s\n"
797 "%p Search string: %s\n%p Search duration: %s\n\n",
800 GNUNET_TESTBED_host_get_hostname (peer->host_handle),
801 peer, peer->policy_file,
802 peer, peer->search_str,
804 GNUNET_STRINGS_relative_time_to_string (prof_time,
807 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
808 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
812 GNUNET_TESTBED_operation_done (peer->op_handle);
813 peer->op_handle = NULL;
815 if (peers_found == num_search_strings)
817 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
818 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
819 "All strings successfully matched in %s\n",
820 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
822 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
823 GNUNET_SCHEDULER_cancel (search_timeout_task);
825 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
826 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
832 * Connect by string timeout task. This will cancel the profiler after the
833 * specified timeout 'search_timeout'.
836 * @param tc the task context
839 search_timeout (void *cls,
840 const struct GNUNET_SCHEDULER_TaskContext * tc)
842 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
843 "Finding matches to all strings did not succeed after %s.\n",
844 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
846 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
847 "Found %i of %i strings\n", peers_found, num_search_strings);
849 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
850 "Search timed out after %s."
851 "Collecting stats and shutting down.\n",
852 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
855 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
860 * Search timed out. It might still complete in the future,
861 * but we should start another one.
863 * @param cls Index of the next peer in the peers array.
864 * @param tc TaskContext.
867 find_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
869 struct RegexPeer *p = cls;
871 p->timeout = GNUNET_SCHEDULER_NO_TASK;
873 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
875 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
876 "Searching for string \"%s\" on peer %d timed out. Starting new search.\n",
879 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
884 * Start searching for a string in the DHT.
886 * @param cls Index of the next peer in the peers array.
887 * @param tc TaskContext.
890 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
892 unsigned int search_peer = (unsigned int) (long) cls;
894 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
895 search_peer >= num_search_strings)
898 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
899 "Searching for string \"%s\" on peer %d with file %s (%u||)\n",
900 peers[search_peer].search_str,
902 peers[search_peer].policy_file,
905 peers[search_peer].op_handle =
906 GNUNET_TESTBED_service_connect (NULL,
907 peers[search_peer].peer_handle,
913 &peers[search_peer]);
914 GNUNET_assert (NULL != peers[search_peer].op_handle);
915 peers[search_peer].timeout = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
917 &peers[search_peer]);
924 * Callback called when testbed has started the daemon we asked for.
927 * @param op the operation handle
928 * @param emsg NULL on success; otherwise an error description
931 daemon_started (void *cls, struct GNUNET_TESTBED_Operation *op,
934 struct RegexPeer *peer = (struct RegexPeer *) cls;
935 unsigned long search_peer;
939 GNUNET_TESTBED_operation_done (peer->daemon_op);
943 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
944 "Failed to start/stop daemon at peer %u: %s\n", me, emsg);
948 /* Find a peer to look for a string matching the regex announced */
949 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
951 for (i = 0; peers[search_peer].search_str != NULL; i++)
953 search_peer = (search_peer + 1) % num_peers;
955 GNUNET_abort (); /* we ran out of peers, must be a bug */
957 peers[search_peer].search_str = search_strings[me];
958 peers[search_peer].search_str_matched = GNUNET_NO;
959 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
960 reannounce_period_max,
963 (void *) search_peer);
964 if (next_search >= num_peers &&
965 GNUNET_SCHEDULER_NO_TASK == search_timeout_task)
967 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All daemons started.\n");
968 /* FIXME start GLOBAL timeout to abort experiment */
969 search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout_time,
977 * Task to start the daemons on each peer so that the regexes are announced
981 * @param tc the task context
984 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
988 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting announce.\n");
990 for (i = 0; i < SEARCHES_IN_PARALLEL; i++)
992 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
993 " scheduling announce %u\n",
995 (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
1001 * Start announcing the next regex in the DHT.
1003 * @param cls Closure (unused).
1004 * @param tc TaskContext.
1007 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1009 struct RegexPeer *peer;
1011 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
1012 next_search >= num_peers)
1015 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
1016 peer = &peers[next_search];
1018 GNUNET_TESTBED_peer_manage_service (NULL,
1025 parallel_searches++;
1029 * DHT connect callback. Called when we are connected to the dht service for
1030 * the peer in 'cls'. If successfull we connect to the stats service of this
1031 * peer and then try to match the search string of this peer.
1033 * @param cls internal peer id.
1034 * @param op operation handle.
1035 * @param ca_result connect adapter result.
1036 * @param emsg error message.
1039 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1040 void *ca_result, const char *emsg)
1042 struct RegexPeer *peer = (struct RegexPeer *) cls;
1044 if (NULL != emsg || NULL == op || NULL == ca_result)
1046 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1050 GNUNET_assert (NULL != peer->dht_handle);
1051 GNUNET_assert (peer->op_handle == op);
1052 GNUNET_assert (peer->dht_handle == ca_result);
1054 peer->search_str_matched = GNUNET_NO;
1055 peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
1057 ®ex_found_handler, peer,
1059 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1064 * DHT connect adapter. Opens a connection to the dht service.
1066 * @param cls Closure (peer).
1067 * @param cfg Configuration handle.
1072 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1074 struct RegexPeer *peer = cls;
1076 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1078 return peer->dht_handle;
1083 * Adapter function called to destroy a connection to the dht service.
1085 * @param cls Closure (peer).
1086 * @param op_result Service handle returned from the connect adapter.
1089 dht_da (void *cls, void *op_result)
1091 struct RegexPeer *peer = (struct RegexPeer *) cls;
1093 GNUNET_assert (peer->dht_handle == op_result);
1095 if (NULL != peer->search_handle)
1097 GNUNET_REGEX_search_cancel (peer->search_handle);
1098 peer->search_handle = NULL;
1101 if (NULL != peer->dht_handle)
1103 GNUNET_DHT_disconnect (peer->dht_handle);
1104 peer->dht_handle = NULL;
1110 * Signature of a main function for a testcase.
1113 * @param num_peers_ number of peers in 'peers'
1114 * @param peers handle to peers run in the testbed. NULL upon timeout (see
1115 * GNUNET_TESTBED_test_run()).
1116 * @param links_succeeded the number of overlay link connection attempts that
1118 * @param links_failed the number of overlay link connection attempts that
1122 test_master (void *cls,
1123 unsigned int num_peers_,
1124 struct GNUNET_TESTBED_Peer **testbed_peers,
1125 unsigned int links_succeeded,
1126 unsigned int links_failed)
1130 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1131 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1132 "Testbed started in %s\n",
1133 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1135 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1137 GNUNET_SCHEDULER_cancel (abort_task);
1138 abort_task = GNUNET_SCHEDULER_NO_TASK;
1141 for (i = 0; i < num_peers; i++)
1144 GNUNET_SCHEDULER_add_now (&do_announce, NULL);
1148 /******************************************************************************/
1149 /*************************** TESTBED PEER SETUP *****************************/
1150 /******************************************************************************/
1154 * Load search strings from given filename. One search string per line.
1156 * @param filename filename of the file containing the search strings.
1157 * @param strings set of strings loaded from file. Caller needs to free this
1158 * if number returned is greater than zero.
1159 * @param limit upper limit on the number of strings read from the file
1160 * @return number of strings found in the file. GNUNET_SYSERR on error.
1163 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1168 unsigned int offset;
1172 if (NULL == filename)
1174 return GNUNET_SYSERR;
1177 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1179 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1180 "Could not find search strings file %s\n", filename);
1181 return GNUNET_SYSERR;
1183 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1188 return GNUNET_SYSERR;
1190 data = GNUNET_malloc (filesize);
1191 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1194 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1196 return GNUNET_SYSERR;
1201 while (offset < (filesize - 1) && str_cnt < limit)
1204 if (((data[offset] == '\n')) && (buf != &data[offset]))
1206 data[offset] = '\0';
1208 buf = &data[offset + 1];
1210 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1211 buf = &data[offset + 1];
1213 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1215 for (i = 0; i < str_cnt; i++)
1217 GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1218 offset += strlen (&data[offset]) + 1;
1226 * Main function that will be run by the scheduler.
1228 * @param cls closure
1229 * @param args remaining command-line arguments
1230 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1231 * @param config configuration
1234 run (void *cls, char *const *args, const char *cfgfile,
1235 const struct GNUNET_CONFIGURATION_Handle *config)
1237 unsigned int nsearchstrs;
1242 /* Initialize peers */
1243 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1244 for (i = 0; i < num_peers; i++)
1246 struct RegexPeer *peer = &peers[i];
1248 peer->policy_file = NULL;
1249 /* Do not start peers on hosts[0] (master controller) */
1250 /* peer->host_handle = hosts[1 + (peer_cnt % (num_hosts -1))]; */
1251 peer->dht_handle = NULL;
1252 peer->search_handle = NULL;
1253 peer->stats_handle = NULL;
1254 peer->stats_op_handle = NULL;
1255 peer->search_str = NULL;
1256 peer->search_str_matched = GNUNET_NO;
1262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1263 _("No configuration file given. Exiting\n"));
1264 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1267 cfg = GNUNET_CONFIGURATION_dup (config);
1269 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
1273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1274 _("Configuration option \"regex_prefix\" missing. Exiting\n"));
1275 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1279 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
1280 "REANNOUNCE_PERIOD_MAX",
1281 &reannounce_period_max))
1283 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1284 "reannounce_period_max not given. Using 10 minutes.\n");
1285 reannounce_period_max =
1286 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
1289 /* Check arguments */
1290 hosts_file = args[0];
1291 if (NULL == hosts_file)
1293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1294 _("No hosts-file specified on command line. Exiting.\n"));
1297 policy_dir = args[1];
1298 if (NULL == policy_dir)
1300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1301 _("No policy directory specified on command line. Exiting.\n"));
1304 if (GNUNET_YES != GNUNET_DISK_directory_test (policy_dir, GNUNET_YES))
1306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1307 _("Specified policies directory does not exist. Exiting.\n"));
1308 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1311 if (-1 == (num_peers = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL)))
1313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1314 _("No files found in `%s'\n"),
1318 strings_file = args[2];
1319 if (GNUNET_YES != GNUNET_DISK_file_test (strings_file))
1321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1322 _("No search strings file given. Exiting.\n"));
1323 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1326 nsearchstrs = load_search_strings (strings_file,
1328 num_search_strings);
1329 if (num_search_strings != nsearchstrs)
1331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1332 _("Error loading search strings."
1333 "Given file does not contain enough strings. Exiting.\n"));
1334 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1337 if (0 >= num_search_strings || NULL == search_strings)
1339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1340 _("Error loading search strings. Exiting.\n"));
1341 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1344 for (i = 0; i < num_search_strings; i++)
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1346 "search string: %s\n",
1350 if ( (NULL != data_filename) &&
1351 (NULL == (data_file =
1352 GNUNET_DISK_file_open (data_filename,
1353 GNUNET_DISK_OPEN_READWRITE |
1354 GNUNET_DISK_OPEN_TRUNCATE |
1355 GNUNET_DISK_OPEN_CREATE,
1356 GNUNET_DISK_PERM_USER_READ |
1357 GNUNET_DISK_PERM_USER_WRITE))) )
1359 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1366 /* event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1367 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1368 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1369 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);*/
1370 prof_start_time = GNUNET_TIME_absolute_get ();
1371 GNUNET_TESTBED_run (args[0],
1375 NULL, /* master_controller_cb, */
1376 NULL, /* master_controller_cb cls */
1378 NULL); /* test_master cls */
1380 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1381 (GNUNET_TIME_UNIT_MINUTES, 5),
1390 * @param argc argument count
1391 * @param argv argument values
1392 * @return 0 on success
1395 main (int argc, char *const *argv)
1397 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1398 {'d', "details", "FILENAME",
1399 gettext_noop ("name of the file for writing statistics"),
1400 1, &GNUNET_GETOPT_set_string, &data_filename},
1401 {'n', "num-links", "COUNT",
1402 gettext_noop ("create COUNT number of random links between peers"),
1403 GNUNET_YES, &GNUNET_GETOPT_set_uint, &linking_factor },
1404 {'t', "matching-timeout", "TIMEOUT",
1405 gettext_noop ("wait TIMEOUT before considering a string match as failed"),
1406 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time
1408 {'s', "search-delay", "DELAY",
1409 gettext_noop ("wait DELAY before starting string search"),
1410 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
1411 {'a', "num-search-strings", "COUNT",
1412 gettext_noop ("number of search strings to read from search strings file"),
1413 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
1414 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
1415 gettext_noop ("maximum path compression length"),
1416 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
1417 GNUNET_GETOPT_OPTION_END
1421 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1424 result = GNUNET_SYSERR;
1426 GNUNET_PROGRAM_run (argc, argv,
1427 "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
1428 _("Profiler for regex"),
1429 options, &run, NULL);
1431 if (GNUNET_OK != ret)
1433 if (GNUNET_OK != result)