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 * Factor of number of links. num_links = num_peers * linking_factor.
256 static unsigned int linking_factor;
259 * Global testing status
264 * current state of profiling
269 * Folder where policy files are stored.
271 static char * policy_dir;
276 static char **search_strings;
279 * Number of search strings.
281 static int num_search_strings;
284 * How many searches are running in parallel
286 static unsigned int parallel_searches;
289 * Number of peers found with search strings.
291 static unsigned int peers_found;
294 * Index of peer to start next announce/search.
296 static unsigned int next_search;
299 * Search timeout task identifier.
301 static GNUNET_SCHEDULER_TaskIdentifier search_timeout_task;
304 * Search timeout in seconds.
306 static struct GNUNET_TIME_Relative search_timeout_time = { 60000 };
309 * How long do we wait before starting the search?
312 static struct GNUNET_TIME_Relative search_delay = { 60000 };
315 * File to log statistics to.
317 static struct GNUNET_DISK_FileHandle *data_file;
320 * Filename to log statistics to.
322 static char *data_filename;
325 * Maximal path compression length.
327 static unsigned int max_path_compression;
330 * Prefix used for regex announcing. We need to prefix the search
331 * strings with it, in order to find something.
333 static char * regex_prefix;
336 * What's the maximum regex reannounce period.
338 static struct GNUNET_TIME_Relative reannounce_period_max;
341 /******************************************************************************/
342 /****************************** DECLARATIONS ********************************/
343 /******************************************************************************/
346 * DHT connect callback.
348 * @param cls internal peer id.
349 * @param op operation handle.
350 * @param ca_result connect adapter result.
351 * @param emsg error message.
354 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
355 void *ca_result, const char *emsg);
358 * DHT connect adapter.
360 * @param cls not used.
361 * @param cfg configuration handle.
366 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg);
370 * Adapter function called to destroy a connection to
374 * @param op_result service handle returned from the connect adapter
377 dht_da (void *cls, void *op_result);
381 * Function called by testbed once we are connected to stats
382 * service. Get the statistics for the services of interest.
384 * @param cls the 'struct RegexPeer' for which we connected to stats
385 * @param op connect operation handle
386 * @param ca_result handle to stats service
387 * @param emsg error message on failure
390 stats_connect_cb (void *cls,
391 struct GNUNET_TESTBED_Operation *op,
397 * Task to collect all statistics from s, will shutdown the
398 * profiler, when done.
401 * @param tc the task context
404 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
408 * Start announcing the next regex in the DHT.
410 * @param cls Index of the next peer in the peers array.
411 * @param tc TaskContext.
414 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
417 /******************************************************************************/
418 /******************************** SHUTDOWN **********************************/
419 /******************************************************************************/
426 * @param tc the task context
429 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
431 struct RegexPeer *peer;
432 unsigned int peer_cnt;
433 unsigned int search_str_cnt;
434 char output_buffer[512];
437 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
438 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
439 GNUNET_SCHEDULER_cancel (abort_task);
440 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
441 GNUNET_SCHEDULER_cancel (register_hosts_task);
443 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
445 peer = &peers[peer_cnt];
447 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
449 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
451 GNUNET_snprintf (output_buffer,
452 sizeof (output_buffer),
453 "%p Search string not found: %s (%d)\n%p On peer: %u (%p)\n%p With policy file: %s\n%p After: %s\n",
454 peer, peer->search_str, peer->search_str_matched,
455 peer, peer->id, peer,
456 peer, peer->policy_file,
458 GNUNET_STRINGS_relative_time_to_string (prof_time,
460 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
461 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
464 if (NULL != peers[peer_cnt].op_handle)
465 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
466 if (NULL != peers[peer_cnt].stats_op_handle)
467 GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
470 if (NULL != data_file)
471 GNUNET_DISK_file_close (data_file);
473 for (search_str_cnt = 0;
474 search_str_cnt < num_search_strings && NULL != search_strings;
477 GNUNET_free_non_null (search_strings[search_str_cnt]);
479 GNUNET_free_non_null (search_strings);
481 if (NULL != reg_handle)
482 GNUNET_TESTBED_cancel_registration (reg_handle);
485 GNUNET_TESTBED_controller_disconnect (mc);
487 GNUNET_TESTBED_controller_stop (mc_proc);
489 GNUNET_CONFIGURATION_destroy (cfg);
491 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
496 * abort task to run on test timed out
499 * @param tc the task context
502 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
504 unsigned long i = (unsigned long) cls;
506 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting %lu...\n", i);
507 abort_task = GNUNET_SCHEDULER_NO_TASK;
508 result = GNUNET_SYSERR;
509 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
510 GNUNET_SCHEDULER_cancel (shutdown_task);
511 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
515 /******************************************************************************/
516 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
517 /******************************************************************************/
520 * Adapter function called to establish a connection to
521 * statistics service.
524 * @param cfg configuration of the peer to connect to; will be available until
525 * GNUNET_TESTBED_operation_done() is called on the operation returned
526 * from GNUNET_TESTBED_service_connect()
527 * @return service handle to return in 'op_result', NULL on error
530 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
532 return GNUNET_STATISTICS_create ("<driver>", cfg);
537 * Adapter function called to destroy a connection to
538 * statistics service.
541 * @param op_result service handle returned from the connect adapter
544 stats_da (void *cls, void *op_result)
546 struct RegexPeer *peer = cls;
548 GNUNET_assert (op_result == peer->stats_handle);
550 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
551 peer->stats_handle = NULL;
556 * Process statistic values. Write all values to global 'data_file', if present.
559 * @param subsystem name of subsystem that created the statistic
560 * @param name the name of the datum
561 * @param value the current value
562 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
563 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
566 stats_iterator (void *cls, const char *subsystem, const char *name,
567 uint64_t value, int is_persistent)
569 struct RegexPeer *peer = cls;
570 char output_buffer[512];
573 if (NULL == data_file)
575 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
576 "%p -> %s [%s]: %llu\n",
577 peer, subsystem, name, value);
581 GNUNET_snprintf (output_buffer,
582 sizeof (output_buffer),
585 subsystem, value, name);
586 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
587 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
594 * Stats callback. Finish the stats testbed operation and when all stats have
595 * been iterated, shutdown the profiler.
598 * @param success GNUNET_OK if statistics were
599 * successfully obtained, GNUNET_SYSERR if not.
605 static unsigned int peer_cnt;
606 struct RegexPeer *peer = cls;
608 if (GNUNET_OK != success)
610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
611 "Getting statistics for peer %u failed!\n",
616 GNUNET_assert (NULL != peer->stats_op_handle);
618 GNUNET_TESTBED_operation_done (peer->stats_op_handle);
619 peer->stats_op_handle = NULL;
622 peer = &peers[peer_cnt];
624 if (peer_cnt == num_peers)
626 struct GNUNET_TIME_Relative delay = { 100 };
627 shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
631 peer->stats_op_handle =
632 GNUNET_TESTBED_service_connect (NULL,
645 * Function called by testbed once we are connected to stats
646 * service. Get the statistics for the services of interest.
648 * @param cls the 'struct RegexPeer' for which we connected to stats
649 * @param op connect operation handle
650 * @param ca_result handle to stats service
651 * @param emsg error message on failure
654 stats_connect_cb (void *cls,
655 struct GNUNET_TESTBED_Operation *op,
659 struct RegexPeer *peer = cls;
661 if (NULL == ca_result || NULL != emsg)
663 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
664 "Failed to connect to statistics service on peer %u: %s\n",
667 peer->stats_handle = NULL;
671 peer->stats_handle = ca_result;
673 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
674 GNUNET_TIME_UNIT_FOREVER_REL,
676 &stats_iterator, peer))
678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
679 "Could not get statistics of peer %u!\n", peer->id);
685 * Task to collect all statistics from all peers, will shutdown the
686 * profiler, when done.
689 * @param tc the task context
692 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
694 struct RegexPeer *peer = &peers[0];
696 GNUNET_assert (NULL != peer->peer_handle);
698 peer->stats_op_handle =
699 GNUNET_TESTBED_service_connect (NULL,
710 /******************************************************************************/
711 /************************ REGEX FIND CONNECTIONS **************************/
712 /******************************************************************************/
716 * Start searching for the next string in the DHT.
718 * @param cls Index of the next peer in the peers array.
719 * @param tc TaskContext.
722 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
726 * Method called when we've found a peer that announced a regex
727 * that matches our search string. Now get the statistics.
729 * @param cls Closure provided in GNUNET_REGEX_search.
730 * @param id Peer providing a regex that matches the string.
731 * @param get_path Path of the get request.
732 * @param get_path_length Lenght of get_path.
733 * @param put_path Path of the put request.
734 * @param put_path_length Length of the put_path.
737 regex_found_handler (void *cls,
738 const struct GNUNET_PeerIdentity *id,
739 const struct GNUNET_PeerIdentity *get_path,
740 unsigned int get_path_length,
741 const struct GNUNET_PeerIdentity *put_path,
742 unsigned int put_path_length)
744 struct RegexPeer *peer = cls;
745 char output_buffer[512];
748 if (GNUNET_YES == peer->search_str_matched)
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
751 "String %s on peer %u already matched!\n",
752 peer->search_str, peer->id);
759 if (GNUNET_SCHEDULER_NO_TASK != peer->timeout)
761 GNUNET_SCHEDULER_cancel (peer->timeout);
762 peer->timeout = GNUNET_SCHEDULER_NO_TASK;
763 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
768 // FIXME not possible right now
769 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
770 "String matching timed out for string %s on peer %u (%i/%i)\n",
771 peer->search_str, peer->id, peers_found, num_search_strings);
772 peer->search_str_matched = GNUNET_SYSERR;
776 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
778 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
779 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
780 peer->search_str, peer->id,
781 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
782 peers_found, num_search_strings, parallel_searches);
784 peer->search_str_matched = GNUNET_YES;
786 if (NULL != data_file)
789 GNUNET_snprintf (output_buffer,
790 sizeof (output_buffer),
791 "%p Peer: %u\n%p Policy file: %s\n"
792 "%p Search string: %s\n%p Search duration: %s\n\n",
794 peer, peer->policy_file,
795 peer, peer->search_str,
797 GNUNET_STRINGS_relative_time_to_string (prof_time,
800 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
801 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
805 GNUNET_TESTBED_operation_done (peer->op_handle);
806 peer->op_handle = NULL;
808 if (peers_found == num_search_strings)
810 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
811 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
812 "All strings successfully matched in %s\n",
813 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
815 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
816 GNUNET_SCHEDULER_cancel (search_timeout_task);
818 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
819 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
825 * Connect by string timeout task. This will cancel the profiler after the
826 * specified timeout 'search_timeout'.
829 * @param tc the task context
832 search_timeout (void *cls,
833 const struct GNUNET_SCHEDULER_TaskContext * tc)
835 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
836 "Finding matches to all strings did not succeed after %s.\n",
837 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
839 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
840 "Found %i of %i strings\n", peers_found, num_search_strings);
842 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
843 "Search timed out after %s."
844 "Collecting stats and shutting down.\n",
845 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
848 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
853 * Search timed out. It might still complete in the future,
854 * but we should start another one.
856 * @param cls Index of the next peer in the peers array.
857 * @param tc TaskContext.
860 find_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
862 struct RegexPeer *p = cls;
864 p->timeout = GNUNET_SCHEDULER_NO_TASK;
866 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
868 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
869 "Searching for string \"%s\" on peer %d timed out. Starting new search.\n",
872 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
877 * Start searching for a string in the DHT.
879 * @param cls Index of the next peer in the peers array.
880 * @param tc TaskContext.
883 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
885 unsigned int search_peer = (unsigned int) (long) cls;
887 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
888 search_peer >= num_search_strings)
891 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
892 "Searching for string \"%s\" on peer %d with file %s (%u||)\n",
893 peers[search_peer].search_str,
895 peers[search_peer].policy_file,
898 peers[search_peer].op_handle =
899 GNUNET_TESTBED_service_connect (NULL,
900 peers[search_peer].peer_handle,
906 &peers[search_peer]);
907 GNUNET_assert (NULL != peers[search_peer].op_handle);
908 peers[search_peer].timeout = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
910 &peers[search_peer]);
917 * Callback called when testbed has started the daemon we asked for.
920 * @param op the operation handle
921 * @param emsg NULL on success; otherwise an error description
924 daemon_started (void *cls, struct GNUNET_TESTBED_Operation *op,
927 struct RegexPeer *peer = (struct RegexPeer *) cls;
928 unsigned long search_peer;
932 GNUNET_TESTBED_operation_done (peer->daemon_op);
936 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
937 "Failed to start/stop daemon at peer %u: %s\n", me, emsg);
941 /* Find a peer to look for a string matching the regex announced */
942 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
944 for (i = 0; peers[search_peer].search_str != NULL; i++)
946 search_peer = (search_peer + 1) % num_peers;
948 GNUNET_abort (); /* we ran out of peers, must be a bug */
950 peers[search_peer].search_str = search_strings[me];
951 peers[search_peer].search_str_matched = GNUNET_NO;
952 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
953 reannounce_period_max,
956 (void *) search_peer);
957 if (next_search >= num_peers &&
958 GNUNET_SCHEDULER_NO_TASK == search_timeout_task)
960 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All daemons started.\n");
961 /* FIXME start GLOBAL timeout to abort experiment */
962 search_timeout_task = GNUNET_SCHEDULER_add_delayed (search_timeout_time,
970 * Task to start the daemons on each peer so that the regexes are announced
974 * @param tc the task context
977 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
981 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting announce.\n");
983 for (i = 0; i < SEARCHES_IN_PARALLEL; i++)
985 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
986 " scheduling announce %u\n",
988 (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
994 * Start announcing the next regex in the DHT.
996 * @param cls Closure (unused).
997 * @param tc TaskContext.
1000 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1002 struct RegexPeer *peer;
1004 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
1005 next_search >= num_peers)
1008 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
1009 peer = &peers[next_search];
1011 GNUNET_TESTBED_peer_manage_service (NULL,
1018 parallel_searches++;
1022 * DHT connect callback. Called when we are connected to the dht service for
1023 * the peer in 'cls'. If successfull we connect to the stats service of this
1024 * peer and then try to match the search string of this peer.
1026 * @param cls internal peer id.
1027 * @param op operation handle.
1028 * @param ca_result connect adapter result.
1029 * @param emsg error message.
1032 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1033 void *ca_result, const char *emsg)
1035 struct RegexPeer *peer = (struct RegexPeer *) cls;
1037 if (NULL != emsg || NULL == op || NULL == ca_result)
1039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1043 GNUNET_assert (NULL != peer->dht_handle);
1044 GNUNET_assert (peer->op_handle == op);
1045 GNUNET_assert (peer->dht_handle == ca_result);
1047 peer->search_str_matched = GNUNET_NO;
1048 peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
1050 ®ex_found_handler, peer,
1052 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1057 * DHT connect adapter. Opens a connection to the dht service.
1059 * @param cls Closure (peer).
1060 * @param cfg Configuration handle.
1065 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1067 struct RegexPeer *peer = cls;
1069 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1071 return peer->dht_handle;
1076 * Adapter function called to destroy a connection to the dht service.
1078 * @param cls Closure (peer).
1079 * @param op_result Service handle returned from the connect adapter.
1082 dht_da (void *cls, void *op_result)
1084 struct RegexPeer *peer = (struct RegexPeer *) cls;
1086 GNUNET_assert (peer->dht_handle == op_result);
1088 if (NULL != peer->search_handle)
1090 GNUNET_REGEX_search_cancel (peer->search_handle);
1091 peer->search_handle = NULL;
1094 if (NULL != peer->dht_handle)
1096 GNUNET_DHT_disconnect (peer->dht_handle);
1097 peer->dht_handle = NULL;
1103 * Signature of a main function for a testcase.
1106 * @param num_peers_ number of peers in 'peers'
1107 * @param peers handle to peers run in the testbed. NULL upon timeout (see
1108 * GNUNET_TESTBED_test_run()).
1109 * @param links_succeeded the number of overlay link connection attempts that
1111 * @param links_failed the number of overlay link connection attempts that
1115 test_master (void *cls,
1116 unsigned int num_peers_,
1117 struct GNUNET_TESTBED_Peer **testbed_peers,
1118 unsigned int links_succeeded,
1119 unsigned int links_failed)
1123 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
1124 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1125 "Testbed started in %s\n",
1126 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
1128 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
1130 GNUNET_SCHEDULER_cancel (abort_task);
1131 abort_task = GNUNET_SCHEDULER_NO_TASK;
1134 for (i = 0; i < num_peers; i++)
1137 GNUNET_SCHEDULER_add_now (&do_announce, NULL);
1141 /******************************************************************************/
1142 /*************************** TESTBED PEER SETUP *****************************/
1143 /******************************************************************************/
1147 * Load search strings from given filename. One search string per line.
1149 * @param filename filename of the file containing the search strings.
1150 * @param strings set of strings loaded from file. Caller needs to free this
1151 * if number returned is greater than zero.
1152 * @param limit upper limit on the number of strings read from the file
1153 * @return number of strings found in the file. GNUNET_SYSERR on error.
1156 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1161 unsigned int offset;
1165 if (NULL == filename)
1167 return GNUNET_SYSERR;
1170 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1172 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1173 "Could not find search strings file %s\n", filename);
1174 return GNUNET_SYSERR;
1176 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1181 return GNUNET_SYSERR;
1183 data = GNUNET_malloc (filesize);
1184 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1189 return GNUNET_SYSERR;
1194 while (offset < (filesize - 1) && str_cnt < limit)
1197 if (((data[offset] == '\n')) && (buf != &data[offset]))
1199 data[offset] = '\0';
1201 buf = &data[offset + 1];
1203 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1204 buf = &data[offset + 1];
1206 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1208 for (i = 0; i < str_cnt; i++)
1210 GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1211 offset += strlen (&data[offset]) + 1;
1219 * Main function that will be run by the scheduler.
1221 * @param cls closure
1222 * @param args remaining command-line arguments
1223 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1224 * @param config configuration
1227 run (void *cls, char *const *args, const char *cfgfile,
1228 const struct GNUNET_CONFIGURATION_Handle *config)
1230 unsigned int nsearchstrs;
1238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1239 _("No configuration file given. Exiting\n"));
1240 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1243 cfg = GNUNET_CONFIGURATION_dup (config);
1245 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
1249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1250 _("Configuration option \"regex_prefix\" missing. Exiting\n"));
1251 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1255 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
1256 "REANNOUNCE_PERIOD_MAX",
1257 &reannounce_period_max))
1259 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1260 "reannounce_period_max not given. Using 10 minutes.\n");
1261 reannounce_period_max =
1262 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
1265 /* Check arguments */
1266 hosts_file = args[0];
1267 if (NULL == hosts_file)
1269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1270 _("No hosts-file specified on command line. Exiting.\n"));
1273 policy_dir = args[1];
1274 if (NULL == policy_dir)
1276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1277 _("No policy directory specified on command line. Exiting.\n"));
1280 if (GNUNET_YES != GNUNET_DISK_directory_test (policy_dir, GNUNET_YES))
1282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1283 _("Specified policies directory does not exist. Exiting.\n"));
1284 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1287 if (-1 == (num_peers = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL)))
1289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1290 _("No files found in `%s'\n"),
1294 strings_file = args[2];
1295 if (GNUNET_YES != GNUNET_DISK_file_test (strings_file))
1297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1298 _("No search strings file given. Exiting.\n"));
1299 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1302 nsearchstrs = load_search_strings (strings_file,
1304 num_search_strings);
1305 if (num_search_strings != nsearchstrs)
1307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1308 _("Error loading search strings."
1309 "Given file does not contain enough strings. Exiting.\n"));
1310 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1313 if (0 >= num_search_strings || NULL == search_strings)
1315 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1316 _("Error loading search strings. Exiting.\n"));
1317 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1320 for (i = 0; i < num_search_strings; i++)
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "search string: %s\n",
1326 if ( (NULL != data_filename) &&
1327 (NULL == (data_file =
1328 GNUNET_DISK_file_open (data_filename,
1329 GNUNET_DISK_OPEN_READWRITE |
1330 GNUNET_DISK_OPEN_TRUNCATE |
1331 GNUNET_DISK_OPEN_CREATE,
1332 GNUNET_DISK_PERM_USER_READ |
1333 GNUNET_DISK_PERM_USER_WRITE))) )
1335 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1341 /* Initialize peers */
1342 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1343 for (i = 0; i < num_peers; i++)
1345 struct RegexPeer *peer = &peers[i];
1347 peer->policy_file = NULL;
1348 peer->dht_handle = NULL;
1349 peer->search_handle = NULL;
1350 peer->stats_handle = NULL;
1351 peer->stats_op_handle = NULL;
1352 peer->search_str = NULL;
1353 peer->search_str_matched = GNUNET_NO;
1357 /* For feedback about the start process activate these and pass master_cb
1358 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1359 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1360 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1361 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);*/
1362 prof_start_time = GNUNET_TIME_absolute_get ();
1363 GNUNET_TESTBED_run (args[0],
1367 NULL, /* master_controller_cb, */
1368 NULL, /* master_controller_cb cls */
1370 NULL); /* test_master cls */
1372 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1373 (GNUNET_TIME_UNIT_MINUTES, 5),
1382 * @param argc argument count
1383 * @param argv argument values
1384 * @return 0 on success
1387 main (int argc, char *const *argv)
1389 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1390 {'d', "details", "FILENAME",
1391 gettext_noop ("name of the file for writing statistics"),
1392 1, &GNUNET_GETOPT_set_string, &data_filename},
1393 {'n', "num-links", "COUNT",
1394 gettext_noop ("create COUNT number of random links between peers"),
1395 GNUNET_YES, &GNUNET_GETOPT_set_uint, &linking_factor },
1396 {'t', "matching-timeout", "TIMEOUT",
1397 gettext_noop ("wait TIMEOUT before considering a string match as failed"),
1398 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time
1400 {'s', "search-delay", "DELAY",
1401 gettext_noop ("wait DELAY before starting string search"),
1402 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_delay },
1403 {'a', "num-search-strings", "COUNT",
1404 gettext_noop ("number of search strings to read from search strings file"),
1405 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
1406 {'p', "max-path-compression", "MAX_PATH_COMPRESSION",
1407 gettext_noop ("maximum path compression length"),
1408 1, &GNUNET_GETOPT_set_uint, &max_path_compression},
1409 GNUNET_GETOPT_OPTION_END
1413 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1416 result = GNUNET_SYSERR;
1418 GNUNET_PROGRAM_run (argc, argv,
1419 "gnunet-regex-profiler [OPTIONS] hosts-file policy-dir search-strings-file",
1420 _("Profiler for regex"),
1421 options, &run, NULL);
1423 if (GNUNET_OK != ret)
1425 if (GNUNET_OK != result)