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 printf("%s\n", __FUNCTION__);
433 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
434 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
435 GNUNET_SCHEDULER_cancel (abort_task);
436 if (GNUNET_SCHEDULER_NO_TASK != register_hosts_task)
437 GNUNET_SCHEDULER_cancel (register_hosts_task);
439 for (peer_cnt = 0; peer_cnt < num_peers; peer_cnt++)
441 peer = &peers[peer_cnt];
443 if (GNUNET_YES != peer->search_str_matched && NULL != data_file)
445 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
447 GNUNET_snprintf (output_buffer,
448 sizeof (output_buffer),
449 "%p Search string not found: %s (%d)\n%p On peer: %u (%p)\n%p With policy file: %s\n%p After: %s\n",
450 peer, peer->search_str, peer->search_str_matched,
451 peer, peer->id, peer,
452 peer, peer->policy_file,
454 GNUNET_STRINGS_relative_time_to_string (prof_time,
456 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
457 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
460 if (NULL != peers[peer_cnt].op_handle)
461 GNUNET_TESTBED_operation_done (peers[peer_cnt].op_handle);
462 if (NULL != peers[peer_cnt].stats_op_handle)
463 GNUNET_TESTBED_operation_done (peers[peer_cnt].stats_op_handle);
466 if (NULL != data_file)
467 GNUNET_DISK_file_close (data_file);
469 for (search_str_cnt = 0;
470 search_str_cnt < num_search_strings && NULL != search_strings;
473 GNUNET_free_non_null (search_strings[search_str_cnt]);
475 GNUNET_free_non_null (search_strings);
477 if (NULL != reg_handle)
478 GNUNET_TESTBED_cancel_registration (reg_handle);
481 GNUNET_TESTBED_controller_disconnect (mc);
483 GNUNET_TESTBED_controller_stop (mc_proc);
485 GNUNET_CONFIGURATION_destroy (cfg);
487 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
492 * abort task to run on test timed out
495 * @param tc the task context
498 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
500 unsigned long i = (unsigned long) cls;
502 printf("%s\n", __FUNCTION__);
503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Aborting %lu...\n", i);
504 abort_task = GNUNET_SCHEDULER_NO_TASK;
505 result = GNUNET_SYSERR;
506 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
507 GNUNET_SCHEDULER_cancel (shutdown_task);
508 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
512 /******************************************************************************/
513 /********************* STATISTICS SERVICE CONNECTIONS ***********************/
514 /******************************************************************************/
517 * Adapter function called to establish a connection to
518 * statistics service.
521 * @param cfg configuration of the peer to connect to; will be available until
522 * GNUNET_TESTBED_operation_done() is called on the operation returned
523 * from GNUNET_TESTBED_service_connect()
524 * @return service handle to return in 'op_result', NULL on error
527 stats_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
529 return GNUNET_STATISTICS_create ("<driver>", cfg);
534 * Adapter function called to destroy a connection to
535 * statistics service.
538 * @param op_result service handle returned from the connect adapter
541 stats_da (void *cls, void *op_result)
543 struct RegexPeer *peer = cls;
545 GNUNET_assert (op_result == peer->stats_handle);
547 GNUNET_STATISTICS_destroy (peer->stats_handle, GNUNET_NO);
548 peer->stats_handle = NULL;
553 * Process statistic values. Write all values to global 'data_file', if present.
556 * @param subsystem name of subsystem that created the statistic
557 * @param name the name of the datum
558 * @param value the current value
559 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
560 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
563 stats_iterator (void *cls, const char *subsystem, const char *name,
564 uint64_t value, int is_persistent)
566 struct RegexPeer *peer = cls;
567 char output_buffer[512];
570 if (NULL == data_file)
572 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
573 "%p -> %s [%s]: %llu\n",
574 peer, subsystem, name, value);
578 GNUNET_snprintf (output_buffer,
579 sizeof (output_buffer),
582 subsystem, value, name);
583 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
584 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
591 * Stats callback. Finish the stats testbed operation and when all stats have
592 * been iterated, shutdown the profiler.
595 * @param success GNUNET_OK if statistics were
596 * successfully obtained, GNUNET_SYSERR if not.
602 static unsigned int peer_cnt;
603 struct RegexPeer *peer = cls;
605 printf("%s\n", __FUNCTION__);
606 if (GNUNET_OK != success)
608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
609 "Getting statistics for peer %u failed!\n",
614 GNUNET_assert (NULL != peer->stats_op_handle);
616 GNUNET_TESTBED_operation_done (peer->stats_op_handle);
617 peer->stats_op_handle = NULL;
620 peer = &peers[peer_cnt];
622 if (peer_cnt == num_peers)
624 struct GNUNET_TIME_Relative delay = { 100 };
625 shutdown_task = GNUNET_SCHEDULER_add_delayed (delay, &do_shutdown, NULL);
629 peer->stats_op_handle =
630 GNUNET_TESTBED_service_connect (NULL,
643 * Function called by testbed once we are connected to stats
644 * service. Get the statistics for the services of interest.
646 * @param cls the 'struct RegexPeer' for which we connected to stats
647 * @param op connect operation handle
648 * @param ca_result handle to stats service
649 * @param emsg error message on failure
652 stats_connect_cb (void *cls,
653 struct GNUNET_TESTBED_Operation *op,
657 struct RegexPeer *peer = cls;
659 if (NULL == ca_result || NULL != emsg)
661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
662 "Failed to connect to statistics service on peer %u: %s\n",
665 peer->stats_handle = NULL;
669 peer->stats_handle = ca_result;
671 if (NULL == GNUNET_STATISTICS_get (peer->stats_handle, NULL, NULL,
672 GNUNET_TIME_UNIT_FOREVER_REL,
674 &stats_iterator, peer))
676 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
677 "Could not get statistics of peer %u!\n", peer->id);
683 * Task to collect all statistics from all peers, will shutdown the
684 * profiler, when done.
687 * @param tc the task context
690 do_collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
692 struct RegexPeer *peer = &peers[0];
694 GNUNET_assert (NULL != peer->peer_handle);
696 peer->stats_op_handle =
697 GNUNET_TESTBED_service_connect (NULL,
708 /******************************************************************************/
709 /************************ REGEX FIND CONNECTIONS **************************/
710 /******************************************************************************/
714 * Start searching for the next string in the DHT.
716 * @param cls Index of the next peer in the peers array.
717 * @param tc TaskContext.
720 find_string (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
724 * Method called when we've found a peer that announced a regex
725 * that matches our search string. Now get the statistics.
727 * @param cls Closure provided in GNUNET_REGEX_search.
728 * @param id Peer providing a regex that matches the string.
729 * @param get_path Path of the get request.
730 * @param get_path_length Lenght of get_path.
731 * @param put_path Path of the put request.
732 * @param put_path_length Length of the put_path.
735 regex_found_handler (void *cls,
736 const struct GNUNET_PeerIdentity *id,
737 const struct GNUNET_PeerIdentity *get_path,
738 unsigned int get_path_length,
739 const struct GNUNET_PeerIdentity *put_path,
740 unsigned int put_path_length)
742 struct RegexPeer *peer = cls;
743 char output_buffer[512];
746 printf("%s\n", __FUNCTION__);
747 if (GNUNET_YES == peer->search_str_matched)
749 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
750 "String %s on peer %u already matched!\n",
751 peer->search_str, peer->id);
758 if (GNUNET_SCHEDULER_NO_TASK != peer->timeout)
760 GNUNET_SCHEDULER_cancel (peer->timeout);
761 peer->timeout = GNUNET_SCHEDULER_NO_TASK;
762 GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
767 // FIXME not possible right now
768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
769 "String matching timed out for string %s on peer %u (%i/%i)\n",
770 peer->search_str, peer->id, peers_found, num_search_strings);
771 peer->search_str_matched = GNUNET_SYSERR;
775 prof_time = GNUNET_TIME_absolute_get_duration (peer->prof_start_time);
777 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
778 "String %s found on peer %u after %s (%i/%i) (%u||)\n",
779 peer->search_str, peer->id,
780 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO),
781 peers_found, num_search_strings, parallel_searches);
783 peer->search_str_matched = GNUNET_YES;
785 if (NULL != data_file)
788 GNUNET_snprintf (output_buffer,
789 sizeof (output_buffer),
790 "%p Peer: %u\n%p Policy file: %s\n"
791 "%p Search string: %s\n%p Search duration: %s\n\n",
793 peer, peer->policy_file,
794 peer, peer->search_str,
796 GNUNET_STRINGS_relative_time_to_string (prof_time,
799 if (size != GNUNET_DISK_file_write (data_file, output_buffer, size))
800 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Unable to write to file!\n");
804 GNUNET_TESTBED_operation_done (peer->op_handle);
805 peer->op_handle = NULL;
807 if (peers_found == num_search_strings)
809 prof_time = GNUNET_TIME_absolute_get_duration (prof_start_time);
810 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
811 "All strings successfully matched in %s\n",
812 GNUNET_STRINGS_relative_time_to_string (prof_time, GNUNET_NO));
814 if (GNUNET_SCHEDULER_NO_TASK != search_timeout_task)
815 GNUNET_SCHEDULER_cancel (search_timeout_task);
817 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Collecting stats and shutting down.\n");
818 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
824 * Connect by string timeout task. This will cancel the profiler after the
825 * specified timeout 'search_timeout'.
828 * @param tc the task context
831 search_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
833 printf("%s\n", __FUNCTION__);
834 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
835 "Finding matches to all strings did not succeed after %s.\n",
836 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
839 "Found %i of %i strings\n", peers_found, num_search_strings);
841 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
842 "Search timed out after %s."
843 "Collecting stats and shutting down.\n",
844 GNUNET_STRINGS_relative_time_to_string (search_timeout_time,
847 GNUNET_SCHEDULER_add_now (&do_collect_stats, NULL);
852 * Search timed out. It might still complete in the future,
853 * but we should start another one.
855 * @param cls Index of the next peer in the peers array.
856 * @param tc TaskContext.
859 find_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
861 struct RegexPeer *p = cls;
863 printf("%s\n", __FUNCTION__);
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 printf("%s\n", __FUNCTION__);
888 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
889 search_peer >= num_search_strings)
892 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
893 "Searching for string \"%s\" on peer %d with file %s (%u||)\n",
894 peers[search_peer].search_str,
896 peers[search_peer].policy_file,
899 peers[search_peer].op_handle =
900 GNUNET_TESTBED_service_connect (NULL,
901 peers[search_peer].peer_handle,
907 &peers[search_peer]);
908 GNUNET_assert (NULL != peers[search_peer].op_handle);
909 peers[search_peer].timeout = GNUNET_SCHEDULER_add_delayed (FIND_TIMEOUT,
911 &peers[search_peer]);
918 * Callback called when testbed has started the daemon we asked for.
921 * @param op the operation handle
922 * @param emsg NULL on success; otherwise an error description
925 daemon_started (void *cls, struct GNUNET_TESTBED_Operation *op,
928 struct RegexPeer *peer = (struct RegexPeer *) cls;
929 unsigned long search_peer;
933 printf("%s\n", __FUNCTION__);
934 GNUNET_TESTBED_operation_done (peer->daemon_op);
935 peer->daemon_op = NULL;
939 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
940 "Failed to start/stop daemon at peer %u: %s\n", me, emsg);
944 /* Find a peer to look for a string matching the regex announced */
945 search_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
947 for (i = 0; peers[search_peer].search_str != NULL; i++)
949 search_peer = (search_peer + 1) % num_peers;
951 GNUNET_abort (); /* we ran out of peers, must be a bug */
953 peers[search_peer].search_str = search_strings[me];
954 peers[search_peer].search_str_matched = GNUNET_NO;
955 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
956 reannounce_period_max,
959 (void *) search_peer);
964 * Task to start the daemons on each peer so that the regexes are announced
968 * @param tc the task context
971 do_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
975 printf("%s\n", __FUNCTION__);
976 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting announce.\n");
978 for (i = 0; i < SEARCHES_IN_PARALLEL; i++)
980 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
981 " scheduling announce %u\n",
983 (void) GNUNET_SCHEDULER_add_now (&announce_next_regex, NULL);
989 * Start announcing the next regex in the DHT.
991 * @param cls Closure (unused).
992 * @param tc TaskContext.
995 announce_next_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
997 struct RegexPeer *peer;
999 printf("%s\n", __FUNCTION__);
1000 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) ||
1001 next_search >= num_peers)
1004 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting daemon %u\n", next_search);
1005 peer = &peers[next_search];
1007 GNUNET_TESTBED_peer_manage_service (NULL,
1014 parallel_searches++;
1018 * DHT connect callback. Called when we are connected to the dht service for
1019 * the peer in 'cls'. If successfull we connect to the stats service of this
1020 * peer and then try to match the search string of this peer.
1022 * @param cls internal peer id.
1023 * @param op operation handle.
1024 * @param ca_result connect adapter result.
1025 * @param emsg error message.
1028 dht_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
1029 void *ca_result, const char *emsg)
1031 struct RegexPeer *peer = (struct RegexPeer *) cls;
1033 printf("%s\n", __FUNCTION__);
1034 if (NULL != emsg || NULL == op || NULL == ca_result)
1036 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "DHT connect failed: %s\n", emsg);
1040 GNUNET_assert (NULL != peer->dht_handle);
1041 GNUNET_assert (peer->op_handle == op);
1042 GNUNET_assert (peer->dht_handle == ca_result);
1044 peer->search_str_matched = GNUNET_NO;
1045 peer->search_handle = GNUNET_REGEX_search (peer->dht_handle,
1047 ®ex_found_handler, peer,
1049 peer->prof_start_time = GNUNET_TIME_absolute_get ();
1054 * DHT connect adapter. Opens a connection to the dht service.
1056 * @param cls Closure (peer).
1057 * @param cfg Configuration handle.
1062 dht_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
1064 struct RegexPeer *peer = cls;
1066 peer->dht_handle = GNUNET_DHT_connect (cfg, 32);
1068 return peer->dht_handle;
1073 * Adapter function called to destroy a connection to the dht service.
1075 * @param cls Closure (peer).
1076 * @param op_result Service handle returned from the connect adapter.
1079 dht_da (void *cls, void *op_result)
1081 struct RegexPeer *peer = (struct RegexPeer *) cls;
1083 GNUNET_assert (peer->dht_handle == op_result);
1085 if (NULL != peer->search_handle)
1087 GNUNET_REGEX_search_cancel (peer->search_handle);
1088 peer->search_handle = NULL;
1091 if (NULL != peer->dht_handle)
1093 GNUNET_DHT_disconnect (peer->dht_handle);
1094 peer->dht_handle = NULL;
1100 * Signature of a main function for a testcase.
1103 * @param num_peers_ number of peers in 'peers'
1104 * @param peers handle to peers run in the testbed. NULL upon timeout (see
1105 * GNUNET_TESTBED_test_run()).
1106 * @param links_succeeded the number of overlay link connection attempts that
1108 * @param links_failed the number of overlay link connection attempts that
1112 test_master (void *cls,
1113 unsigned int num_peers_,
1114 struct GNUNET_TESTBED_Peer **testbed_peers,
1115 unsigned int links_succeeded,
1116 unsigned int links_failed)
1120 printf("%s\n", __FUNCTION__);
1121 GNUNET_assert (num_peers_ == num_peers);
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++)
1136 peers[i].peer_handle = testbed_peers[i];
1138 GNUNET_SCHEDULER_add_now (&do_announce, NULL);
1140 GNUNET_SCHEDULER_add_delayed (search_timeout_time,
1146 /******************************************************************************/
1147 /*************************** TESTBED PEER SETUP *****************************/
1148 /******************************************************************************/
1152 * Load search strings from given filename. One search string per line.
1154 * @param filename filename of the file containing the search strings.
1155 * @param strings set of strings loaded from file. Caller needs to free this
1156 * if number returned is greater than zero.
1157 * @param limit upper limit on the number of strings read from the file
1158 * @return number of strings found in the file. GNUNET_SYSERR on error.
1161 load_search_strings (const char *filename, char ***strings, unsigned int limit)
1166 unsigned int offset;
1170 printf("%s\n", __FUNCTION__);
1171 if (NULL == filename)
1173 return GNUNET_SYSERR;
1176 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
1178 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1179 "Could not find search strings file %s\n", filename);
1180 return GNUNET_SYSERR;
1182 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &filesize, GNUNET_YES, GNUNET_YES))
1186 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Search strings file %s is empty.\n", filename);
1187 return GNUNET_SYSERR;
1189 data = GNUNET_malloc (filesize);
1190 if (filesize != GNUNET_DISK_fn_read (filename, data, filesize))
1193 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Could not read search strings file %s.\n",
1195 return GNUNET_SYSERR;
1200 while (offset < (filesize - 1) && str_cnt < limit)
1203 if (((data[offset] == '\n')) && (buf != &data[offset]))
1205 data[offset] = '\0';
1207 buf = &data[offset + 1];
1209 else if ((data[offset] == '\n') || (data[offset] == '\0'))
1210 buf = &data[offset + 1];
1212 *strings = GNUNET_malloc (sizeof (char *) * str_cnt);
1214 for (i = 0; i < str_cnt; i++)
1216 GNUNET_asprintf (&(*strings)[i], "%s%s", regex_prefix, &data[offset]);
1217 offset += strlen (&data[offset]) + 1;
1225 * Main function that will be run by the scheduler.
1227 * @param cls closure
1228 * @param args remaining command-line arguments
1229 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1230 * @param config configuration
1233 run (void *cls, char *const *args, const char *cfgfile,
1234 const struct GNUNET_CONFIGURATION_Handle *config)
1236 unsigned int nsearchstrs;
1239 printf("%s\n", __FUNCTION__);
1244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1245 _("No configuration file given. Exiting\n"));
1246 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1249 cfg = GNUNET_CONFIGURATION_dup (config);
1251 GNUNET_CONFIGURATION_get_value_string (cfg, "REGEXPROFILER",
1255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1256 _("Configuration option \"regex_prefix\" missing. Exiting\n"));
1257 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1261 GNUNET_CONFIGURATION_get_value_time (cfg, "REGEXPROFILER",
1262 "REANNOUNCE_PERIOD_MAX",
1263 &reannounce_period_max))
1265 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1266 "reannounce_period_max not given. Using 10 minutes.\n");
1267 reannounce_period_max =
1268 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 10);
1271 /* Check arguments */
1272 if (NULL == hosts_file)
1274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1275 _("No hosts-file specified on command line. Exiting.\n"));
1278 if (NULL == policy_dir)
1280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1281 _("No policy directory specified on command line. Exiting.\n"));
1284 if (GNUNET_YES != GNUNET_DISK_directory_test (policy_dir, GNUNET_YES))
1286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1287 _("Specified policies directory does not exist. Exiting.\n"));
1288 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1291 if (-1 == (num_peers = GNUNET_DISK_directory_scan (policy_dir, NULL, NULL)))
1293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1294 _("No files found in `%s'\n"),
1298 GNUNET_CONFIGURATION_set_value_string (cfg, "REGEXPROFILER",
1299 "POLICY_DIR", policy_dir);
1300 if (GNUNET_YES != GNUNET_DISK_file_test (strings_file))
1302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1303 _("No search strings file given. Exiting.\n"));
1304 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1307 nsearchstrs = load_search_strings (strings_file,
1309 num_search_strings);
1310 if (num_search_strings != nsearchstrs)
1312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1313 _("Error loading search strings."
1314 "Given file does not contain enough strings. Exiting.\n"));
1315 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1318 if (0 >= num_search_strings || NULL == search_strings)
1320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1321 _("Error loading search strings. Exiting.\n"));
1322 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
1325 for (i = 0; i < num_search_strings; i++)
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 "search string: %s\n",
1331 if ( (NULL != data_filename) &&
1332 (NULL == (data_file =
1333 GNUNET_DISK_file_open (data_filename,
1334 GNUNET_DISK_OPEN_READWRITE |
1335 GNUNET_DISK_OPEN_TRUNCATE |
1336 GNUNET_DISK_OPEN_CREATE,
1337 GNUNET_DISK_PERM_USER_READ |
1338 GNUNET_DISK_PERM_USER_WRITE))) )
1340 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1346 /* Initialize peers */
1347 peers = GNUNET_malloc (sizeof (struct RegexPeer) * num_peers);
1348 for (i = 0; i < num_peers; i++)
1354 /* For feedback about the start process activate these and pass master_cb
1355 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
1356 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
1357 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
1358 event_mask |= (1LL << GNUNET_TESTBED_ET_DISCONNECT);*/
1359 prof_start_time = GNUNET_TIME_absolute_get ();
1360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "1.\n");
1361 GNUNET_TESTBED_run (args[0],
1365 NULL, /* master_controller_cb, */
1366 NULL, /* master_controller_cb cls */
1368 NULL); /* test_master cls */
1369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "2.\n");
1371 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1372 (GNUNET_TIME_UNIT_MINUTES, 5),
1375 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "3.\n");
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 {'o', "log-file", "FILENAME",
1391 gettext_noop ("name of the file for writing statistics"),
1392 1, &GNUNET_GETOPT_set_string, &data_filename},
1393 {'t', "matching-timeout", "TIMEOUT",
1394 gettext_noop ("wait TIMEOUT before considering a string match as failed"),
1395 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &search_timeout_time },
1396 {'n', "num-search-strings", "COUNT",
1397 gettext_noop ("number of search strings to read from search strings file"),
1398 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_search_strings },
1399 {'p', "policy-dir", "DIRECTORY",
1400 gettext_noop ("directory with policy files"),
1401 GNUNET_YES, &GNUNET_GETOPT_set_filename, &policy_dir },
1402 {'s', "strings-file", "FILENAME",
1403 gettext_noop ("name of file with input strings"),
1404 GNUNET_YES, &GNUNET_GETOPT_set_filename, &strings_file },
1405 {'H', "hosts-file", "FILENAME",
1406 gettext_noop ("name of file with hosts' names"),
1407 GNUNET_YES, &GNUNET_GETOPT_set_filename, &hosts_file },
1408 GNUNET_GETOPT_OPTION_END
1412 printf("%s\n", __FUNCTION__);
1414 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);
1422 if (GNUNET_OK != ret)
1424 if (GNUNET_OK != result)