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 * Filename of the peer's policy file.
139 * Peer's search string.
141 const char *search_str;
144 * Set to GNUNET_YES if the peer successfully matched the above
145 * search string. GNUNET_NO if the string could not be matched
146 * during the profiler run. GNUNET_SYSERR if the string matching
147 * timed out. Undefined if search_str is NULL
149 int search_str_matched;
154 struct GNUNET_DHT_Handle *dht_handle;
157 * Handle to a running regex search.
159 struct GNUNET_REGEX_search_handle *search_handle;
162 * Testbed operation handle for DHT.
164 struct GNUNET_TESTBED_Operation *op_handle;
167 * Peers's statistics handle.
169 struct GNUNET_STATISTICS_Handle *stats_handle;
172 * Testbed operation handle for the statistics service.
174 struct GNUNET_TESTBED_Operation *stats_op_handle;
177 * The starting time of a profiling step.
179 struct GNUNET_TIME_Absolute prof_start_time;
184 GNUNET_SCHEDULER_TaskIdentifier timeout;
189 struct GNUNET_TESTBED_Operation *daemon_op;
194 * The array of peers; we fill this as the peers are given to us by the testbed
196 static struct RegexPeer *peers;
199 * Host registration handle
201 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
204 * Handle to the master controller process
206 static struct GNUNET_TESTBED_ControllerProc *mc_proc;
209 * Handle to the master controller
211 static struct GNUNET_TESTBED_Controller *mc;
214 * Handle to global configuration
216 static struct GNUNET_CONFIGURATION_Handle *cfg;
219 * Abort task identifier
221 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
224 * Shutdown task identifier
226 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
229 * Host registration task identifier
231 static GNUNET_SCHEDULER_TaskIdentifier register_hosts_task;
234 * Global event mask for all testbed events
236 static uint64_t event_mask;
239 * The starting time of a profiling step
241 static struct GNUNET_TIME_Absolute prof_start_time;
244 * Duration profiling step has taken
246 static struct GNUNET_TIME_Relative prof_time;
249 * Number of peers to be started by the profiler
251 static unsigned int num_peers;
254 * Global testing status
259 * current state of profiling
264 * Folder where policy files are stored.
266 static char * policy_dir;
269 * File with hostnames where to execute the test.
271 static char *hosts_file;
274 * File with the strings to look for.
276 static char *strings_file;
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 * File to log statistics to.
316 static struct GNUNET_DISK_FileHandle *data_file;
319 * Filename to log statistics to.
321 static char *data_filename;
324 * Prefix used for regex announcing. We need to prefix the search
325 * strings with it, in order to find something.
327 static char * regex_prefix;
330 * What's the maximum regex reannounce period.
332 static struct GNUNET_TIME_Relative reannounce_period_max;
335 /******************************************************************************/
336 /****************************** DECLARATIONS ********************************/
337 /******************************************************************************/
340 * DHT connect callback.
342 * @param cls internal peer id.
343 * @param op operation handle.
344 * @param ca_result connect adapter result.
345 * @param emsg error message.
348 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
349 void *ca_result, const char *emsg);
352 * DHT connect adapter.
354 * @param cls not used.
355 * @param cfg configuration handle.
360 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
364 * Adapter function called to destroy a connection to
368 * @param op_result service handle returned from the connect adapter
371 dht_da (void *cls, void *op_result);
375 * Function called by testbed once we are connected to stats
376 * service. Get the statistics for the services of interest.
378 * @param cls the 'struct RegexPeer' for which we connected to stats
379 * @param op connect operation handle
380 * @param ca_result handle to stats service
381 * @param emsg error message on failure
384 stats_connect_cb (void *cls,
385 struct GNUNET_TESTBED_Operation *op,
391 * Task to collect all statistics from s, will shutdown the
392 * profiler, when done.
395 * @param tc the task context
398 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
402 * Start announcing the next regex in the DHT.
404 * @param cls Index of the next peer in the peers array.
405 * @param tc TaskContext.
408 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
411 /******************************************************************************/
412 /******************************** SHUTDOWN **********************************/
413 /******************************************************************************/
420 * @param tc the task context
423 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
425 struct RegexPeer *peer;
426 unsigned int peer_cnt;
427 unsigned int search_str_cnt;
428 char output_buffer[512];
431 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
432 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
433 GNUNET_SCHEDULER_cancel (abort_task);
434 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
435 GNUNET_SCHEDULER_cancel (register_hosts_task);
437 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
439 peer = &peers[peer_cnt];
441 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
443 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
445 GNUNET_snprintf (output_buffer,
446 sizeof (output_buffer),
447 "%p Search string not found: %s (%d)\n%p On peer: %u (%p)\n%p With policy file: %s\n%p After: %s\n",
448 peer, peer->search_str, peer->search_str_matched,
449 peer, peer->id, peer,
450 peer, peer->policy_file,
452 GNUNET_STRINGS_relative_time_to_string (prof_time,
454 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
455 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
458 if (NULL != peers[peer_cnt].op_handle)
459 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
460 if (NULL != peers[peer_cnt].stats_op_handle)
461 GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
464 if (NULL != data_file)
465 GNUNET_DISK_file_close (data_file);
467 for (search_str_cnt = 0;
468 search_str_cnt < num_search_strings && NULL != search_strings;
471 GNUNET_free_non_null (search_strings[search_str_cnt]);
473 GNUNET_free_non_null (search_strings);
475 if (NULL != reg_handle)
476 GNUNET_TESTBED_cancel_registration (reg_handle);
479 GNUNET_TESTBED_controller_disconnect (mc);
481 GNUNET_TESTBED_controller_stop (mc_proc);
483 GNUNET_CONFIGURATION_destroy (cfg);
485 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
490 * abort task to run on test timed out
493 * @param tc the task context
496 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
498 unsigned long i = (unsigned long) cls;
500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting %lu...\n", i);
501 abort_task = GNUNET_SCHEDULER_NO_TASK;
502 result = GNUNET_SYSERR;
503 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
504 GNUNET_SCHEDULER_cancel (shutdown_task);
505 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
509 /******************************************************************************/
510 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
511 /******************************************************************************/
514 * Adapter function called to establish a connection to
515 * statistics service.
518 * @param cfg configuration of the peer to connect to; will be available until
519 * GNUNET_TESTBED_operation_done() is called on the operation returned
520 * from GNUNET_TESTBED_service_connect()
521 * @return service handle to return in 'op_result', NULL on error
524 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
526 return GNUNET_STATISTICS_create ("<driver>", cfg);
531 * Adapter function called to destroy a connection to
532 * statistics service.
535 * @param op_result service handle returned from the connect adapter
538 stats_da (void *cls, void *op_result)
540 struct RegexPeer *peer = cls;
542 GNUNET_assert (op_result == peer->stats_handle);
544 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
545 peer->stats_handle = NULL;
550 * Process statistic values. Write all values to global 'data_file', if present.
553 * @param subsystem name of subsystem that created the statistic
554 * @param name the name of the datum
555 * @param value the current value
556 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
557 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
560 stats_iterator (void *cls, const char *subsystem, const char *name,
561 uint64_t value, int is_persistent)
563 struct RegexPeer *peer = cls;
564 char output_buffer[512];
567 if (NULL == data_file)
569 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
570 "%p -> %s [%s]: %llu\n",
571 peer, subsystem, name, value);
575 GNUNET_snprintf (output_buffer,
576 sizeof (output_buffer),
579 subsystem, value, name);
580 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
581 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
588 * Stats callback. Finish the stats testbed operation and when all stats have
589 * been iterated, shutdown the profiler.
592 * @param success GNUNET_OK if statistics were
593 * successfully obtained, GNUNET_SYSERR if not.
599 static unsigned int peer_cnt;
600 struct RegexPeer *peer = cls;
602 if (GNUNET_OK != success)
604 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
605 "Getting statistics for peer %u failed!\n",
610 GNUNET_assert (NULL != peer->stats_op_handle);
612 GNUNET_TESTBED_operation_done (peer->stats_op_handle);
613 peer->stats_op_handle = NULL;
616 peer = &peers[peer_cnt];
618 if (peer_cnt == num_peers)
620 struct GNUNET_TIME_Relative delay = { 100 };
621 shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
625 peer->stats_op_handle =
626 GNUNET_TESTBED_service_connect (NULL,
639 * Function called by testbed once we are connected to stats
640 * service. Get the statistics for the services of interest.
642 * @param cls the 'struct RegexPeer' for which we connected to stats
643 * @param op connect operation handle
644 * @param ca_result handle to stats service
645 * @param emsg error message on failure
648 stats_connect_cb (void *cls,
649 struct GNUNET_TESTBED_Operation *op,
653 struct RegexPeer *peer = cls;
655 if (NULL == ca_result || NULL != emsg)
657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
658 "Failed to connect to statistics service on peer %u: %s\n",
661 peer->stats_handle = NULL;
665 peer->stats_handle = ca_result;
667 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
668 GNUNET_TIME_UNIT_FOREVER_REL,
670 &stats_iterator, peer))
672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673 "Could not get statistics of peer %u!\n", peer->id);
679 * Task to collect all statistics from all peers, will shutdown the
680 * profiler, when done.
683 * @param tc the task context
686 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
688 struct RegexPeer *peer = &peers[0];
690 GNUNET_assert (NULL != peer->peer_handle);
692 peer->stats_op_handle =
693 GNUNET_TESTBED_service_connect (NULL,
704 /******************************************************************************/
705 /************************ REGEX FIND CONNECTIONS **************************/
706 /******************************************************************************/
710 * Start searching for the next string in the DHT.
712 * @param cls Index of the next peer in the peers array.
713 * @param tc TaskContext.
716 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
720 * Method called when we've found a peer that announced a regex
721 * that matches our search string. Now get the statistics.
723 * @param cls Closure provided in GNUNET_REGEX_search.
724 * @param id Peer providing a regex that matches the string.
725 * @param get_path Path of the get request.
726 * @param get_path_length Lenght of get_path.
727 * @param put_path Path of the put request.
728 * @param put_path_length Length of the put_path.
731 regex_found_handler (void *cls,
732 const struct GNUNET_PeerIdentity *id,
733 const struct GNUNET_PeerIdentity *get_path,
734 unsigned int get_path_length,
735 const struct GNUNET_PeerIdentity *put_path,
736 unsigned int put_path_length)
738 struct RegexPeer *peer = cls;
739 char output_buffer[512];
742 if (GNUNET_YES == peer->search_str_matched)
744 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
745 "String %s on peer %u already matched!\n",
746 peer->search_str, peer->id);
753 if (GNUNET_SCHEDULER_NO_TASK != peer->timeout)
755 GNUNET_SCHEDULER_cancel (peer->timeout);
756 peer->timeout = GNUNET_SCHEDULER_NO_TASK;
757 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
762 // FIXME not possible right now
763 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
764 "String matching timed out for string %s on peer %u (%i/%i)\n",
765 peer->search_str, peer->id, peers_found, num_search_strings);
766 peer->search_str_matched = GNUNET_SYSERR;
770 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
772 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
773 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
774 peer->search_str, peer->id,
775 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
776 peers_found, num_search_strings, parallel_searches);
778 peer->search_str_matched = GNUNET_YES;
780 if (NULL != data_file)
783 GNUNET_snprintf (output_buffer,
784 sizeof (output_buffer),
785 "%p Peer: %u\n%p Policy file: %s\n"
786 "%p Search string: %s\n%p Search duration: %s\n\n",
788 peer, peer->policy_file,
789 peer, peer->search_str,
791 GNUNET_STRINGS_relative_time_to_string (prof_time,
794 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
795 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
799 GNUNET_TESTBED_operation_done (peer->op_handle);
800 peer->op_handle = NULL;
802 if (peers_found == num_search_strings)
804 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
805 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
806 "All strings successfully matched in %s\n",
807 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
809 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
810 GNUNET_SCHEDULER_cancel (search_timeout_task);
812 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
813 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
819 * Connect by string timeout task. This will cancel the profiler after the
820 * specified timeout 'search_timeout'.
823 * @param tc the task context
826 search_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
828 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
829 "Finding matches to all strings did not succeed after %s.\n",
830 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
832 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
833 "Found %i of %i strings\n", peers_found, num_search_strings);
835 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
836 "Search timed out after %s."
837 "Collecting stats and shutting down.\n",
838 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
841 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
846 * Search timed out. It might still complete in the future,
847 * but we should start another one.
849 * @param cls Index of the next peer in the peers array.
850 * @param tc TaskContext.
853 find_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
855 struct RegexPeer *p = cls;
857 p->timeout = GNUNET_SCHEDULER_NO_TASK;
859 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
861 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
862 "Searching for string \"%s\" on peer %d timed out. Starting new search.\n",
865 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
870 * Start searching for a string in the DHT.
872 * @param cls Index of the next peer in the peers array.
873 * @param tc TaskContext.
876 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
878 unsigned int search_peer = (unsigned int) (long) cls;
880 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
881 search_peer >= num_search_strings)
884 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
885 "Searching for string \"%s\" on peer %d with file %s (%u||)\n",
886 peers[search_peer].search_str,
888 peers[search_peer].policy_file,
891 peers[search_peer].op_handle =
892 GNUNET_TESTBED_service_connect (NULL,
893 peers[search_peer].peer_handle,
899 &peers[search_peer]);
900 GNUNET_assert (NULL != peers[search_peer].op_handle);
901 peers[search_peer].timeout = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
903 &peers[search_peer]);
910 * Callback called when testbed has started the daemon we asked for.
913 * @param op the operation handle
914 * @param emsg NULL on success; otherwise an error description
917 daemon_started (void *cls, struct GNUNET_TESTBED_Operation *op,
920 struct RegexPeer *peer = (struct RegexPeer *) cls;
921 unsigned long search_peer;
925 GNUNET_TESTBED_operation_done (peer->daemon_op);
926 peer->daemon_op = NULL;
930 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
931 "Failed to start/stop daemon at peer %u: %s\n", me, emsg);
935 /* Find a peer to look for a string matching the regex announced */
936 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
938 for (i = 0; peers[search_peer].search_str != NULL; i++)
940 search_peer = (search_peer + 1) % num_peers;
942 GNUNET_abort (); /* we ran out of peers, must be a bug */
944 peers[search_peer].search_str = search_strings[me];
945 peers[search_peer].search_str_matched = GNUNET_NO;
946 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
947 reannounce_period_max,
950 (void *) search_peer);
955 * Task to start the daemons on each peer so that the regexes are announced
959 * @param tc the task context
962 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
966 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting announce.\n");
968 for (i = 0; i < SEARCHES_IN_PARALLEL; i++)
970 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
971 " scheduling announce %u\n",
973 (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
979 * Start announcing the next regex in the DHT.
981 * @param cls Closure (unused).
982 * @param tc TaskContext.
985 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
987 struct RegexPeer *peer;
989 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
990 next_search >= num_peers)
993 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
994 peer = &peers[next_search];
996 GNUNET_TESTBED_peer_manage_service (NULL,
1003 parallel_searches++;
1007 * DHT connect callback. Called when we are connected to the dht service for
1008 * the peer in 'cls'. If successfull we connect to the stats service of this
1009 * peer and then try to match the search string of this peer.
1011 * @param cls internal peer id.
1012 * @param op operation handle.
1013 * @param ca_result connect adapter result.
1014 * @param emsg error message.
1017 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1018 void *ca_result, const char *emsg)
1020 struct RegexPeer *peer = (struct RegexPeer *) cls;
1022 if (NULL != emsg || NULL == op || NULL == ca_result)
1024 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1028 GNUNET_assert (NULL != peer->dht_handle);
1029 GNUNET_assert (peer->op_handle == op);
1030 GNUNET_assert (peer->dht_handle == ca_result);
1032 peer->search_str_matched = GNUNET_NO;
1033 peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
1035 ®ex_found_handler, peer,
1037 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1042 * DHT connect adapter. Opens a connection to the dht service.
1044 * @param cls Closure (peer).
1045 * @param cfg Configuration handle.
1050 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1052 struct RegexPeer *peer = cls;
1054 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1056 return peer->dht_handle;
1061 * Adapter function called to destroy a connection to the dht service.
1063 * @param cls Closure (peer).
1064 * @param op_result Service handle returned from the connect adapter.
1067 dht_da (void *cls, void *op_result)
1069 struct RegexPeer *peer = (struct RegexPeer *) cls;
1071 GNUNET_assert (peer->dht_handle == op_result);
1073 if (NULL != peer->search_handle)
1075 GNUNET_REGEX_search_cancel (peer->search_handle);
1076 peer->search_handle = NULL;
1079 if (NULL != peer->dht_handle)
1081 GNUNET_DHT_disconnect (peer->dht_handle);
1082 peer->dht_handle = NULL;
1088 * Signature of a main function for a testcase.
1091 * @param num_peers_ number of peers in 'peers'
1092 * @param peers handle to peers run in the testbed. NULL upon timeout (see
1093 * GNUNET_TESTBED_test_run()).
1094 * @param links_succeeded the number of overlay link connection attempts that
1096 * @param links_failed the number of overlay link connection attempts that
1100 test_master (void *cls,
1101 unsigned int num_peers_,
1102 struct GNUNET_TESTBED_Peer **testbed_peers,
1103 unsigned int links_succeeded,
1104 unsigned int links_failed)
1108 GNUNET_assert (num_peers_ == num_peers);
1110 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1111 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1112 "Testbed started in %s\n",
1113 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1115 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1117 GNUNET_SCHEDULER_cancel (abort_task);
1118 abort_task = GNUNET_SCHEDULER_NO_TASK;
1121 for (i = 0; i < num_peers; i++)
1123 peers[i].peer_handle = testbed_peers[i];
1125 GNUNET_SCHEDULER_add_now (&do_announce, NULL);
1127 GNUNET_SCHEDULER_add_delayed (search_timeout_time,
1133 /******************************************************************************/
1134 /*************************** TESTBED PEER SETUP *****************************/
1135 /******************************************************************************/
1139 * Load search strings from given filename. One search string per line.
1141 * @param filename filename of the file containing the search strings.
1142 * @param strings set of strings loaded from file. Caller needs to free this
1143 * if number returned is greater than zero.
1144 * @param limit upper limit on the number of strings read from the file
1145 * @return number of strings found in the file. GNUNET_SYSERR on error.
1148 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1153 unsigned int offset;
1157 if (NULL == filename)
1159 return GNUNET_SYSERR;
1162 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1164 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1165 "Could not find search strings file %s\n", filename);
1166 return GNUNET_SYSERR;
1168 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1172 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1173 return GNUNET_SYSERR;
1175 data = GNUNET_malloc (filesize);
1176 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1179 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1181 return GNUNET_SYSERR;
1186 while (offset < (filesize - 1) && str_cnt < limit)
1189 if (((data[offset] == '\n')) && (buf != &data[offset]))
1191 data[offset] = '\0';
1193 buf = &data[offset + 1];
1195 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1196 buf = &data[offset + 1];
1198 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1200 for (i = 0; i < str_cnt; i++)
1202 GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1203 offset += strlen (&data[offset]) + 1;
1211 * Main function that will be run by the scheduler.
1213 * @param cls closure
1214 * @param args remaining command-line arguments
1215 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1216 * @param config configuration
1219 run (void *cls, char *const *args, const char *cfgfile,
1220 const struct GNUNET_CONFIGURATION_Handle *config)
1222 unsigned int nsearchstrs;
1228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1229 _("No configuration file given. Exiting\n"));
1230 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1233 cfg = GNUNET_CONFIGURATION_dup (config);
1235 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
1239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1240 _("Configuration option \"regex_prefix\" missing. Exiting\n"));
1241 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1245 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
1246 "REANNOUNCE_PERIOD_MAX",
1247 &reannounce_period_max))
1249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1250 "reannounce_period_max not given. Using 10 minutes.\n");
1251 reannounce_period_max =
1252 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
1255 /* Check arguments */
1256 if (NULL == hosts_file)
1258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1259 _("No hosts-file specified on command line. Exiting.\n"));
1262 if (NULL == policy_dir)
1264 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1265 _("No policy directory specified on command line. Exiting.\n"));
1268 if (GNUNET_YES != GNUNET_DISK_directory_test (policy_dir, GNUNET_YES))
1270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1271 _("Specified policies directory does not exist. Exiting.\n"));
1272 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1275 if (-1 == (num_peers = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL)))
1277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1278 _("No files found in `%s'\n"),
1282 GNUNET_CONFIGURATION_set_value_string (cfg, "REGEXPROFILER",
1283 "POLICY_DIR", policy_dir);
1284 if (GNUNET_YES != GNUNET_DISK_file_test (strings_file))
1286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1287 _("No search strings file given. Exiting.\n"));
1288 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1291 nsearchstrs = load_search_strings (strings_file,
1293 num_search_strings);
1294 if (num_search_strings != nsearchstrs)
1296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1297 _("Error loading search strings."
1298 "Given file does not contain enough strings. Exiting.\n"));
1299 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1302 if (0 >= num_search_strings || NULL == search_strings)
1304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1305 _("Error loading search strings. Exiting.\n"));
1306 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1309 for (i = 0; i < num_search_strings; i++)
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1311 "search string: %s\n",
1315 if ( (NULL != data_filename) &&
1316 (NULL == (data_file =
1317 GNUNET_DISK_file_open (data_filename,
1318 GNUNET_DISK_OPEN_READWRITE |
1319 GNUNET_DISK_OPEN_TRUNCATE |
1320 GNUNET_DISK_OPEN_CREATE,
1321 GNUNET_DISK_PERM_USER_READ |
1322 GNUNET_DISK_PERM_USER_WRITE))) )
1324 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1330 /* Initialize peers */
1331 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1332 for (i = 0; i < num_peers; i++)
1338 /* For feedback about the start process activate these and pass master_cb
1339 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1340 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1341 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1342 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);*/
1343 prof_start_time = GNUNET_TIME_absolute_get ();
1344 GNUNET_TESTBED_run (args[0],
1348 NULL, /* master_controller_cb, */
1349 NULL, /* master_controller_cb cls */
1351 NULL); /* test_master cls */
1353 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1354 (GNUNET_TIME_UNIT_MINUTES, 5),
1363 * @param argc argument count
1364 * @param argv argument values
1365 * @return 0 on success
1368 main (int argc, char *const *argv)
1370 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1371 {'o', "log-file", "FILENAME",
1372 gettext_noop ("name of the file for writing statistics"),
1373 1, &GNUNET_GETOPT_set_string, &data_filename},
1374 {'t', "matching-timeout", "TIMEOUT",
1375 gettext_noop ("wait TIMEOUT before considering a string match as failed"),
1376 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time },
1377 {'n', "num-search-strings", "COUNT",
1378 gettext_noop ("number of search strings to read from search strings file"),
1379 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
1380 {'p', "policy-dir", "DIRECTORY",
1381 gettext_noop ("directory with policy files"),
1382 GNUNET_YES, &GNUNET_GETOPT_set_filename, &policy_dir },
1383 {'s', "strings-file", "FILENAME",
1384 gettext_noop ("name of file with input strings"),
1385 GNUNET_YES, &GNUNET_GETOPT_set_filename, &strings_file },
1386 {'H', "hosts-file", "FILENAME",
1387 gettext_noop ("name of file with hosts' names"),
1388 GNUNET_YES, &GNUNET_GETOPT_set_filename, &hosts_file },
1389 GNUNET_GETOPT_OPTION_END
1393 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1395 result = GNUNET_SYSERR;
1397 GNUNET_PROGRAM_run (argc, argv,
1398 "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
1399 _("Profiler for regex"),
1400 options, &run, NULL);
1401 if (GNUNET_OK != ret)
1403 if (GNUNET_OK != result)