2 This file is part of GNUnet.
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 sensor/gnunet-sensor-profiler.c
23 * @brief Profiler for the sensor service
24 * @author Omar Tarabai
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_peerstore_service.h"
30 #include "gnunet_sensor_service.h"
31 #include "gnunet_sensor_util_lib.h"
32 #include "gnunet_transport_service.h"
35 * Time to wait for the peer to startup completely
37 #define PEER_STARTUP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
40 * Information about a single peer
48 struct GNUNET_PeerIdentity peer_id;
53 struct GNUNET_TESTBED_Peer *testbed_peer;
56 * Index of this peer within our list
61 * TESTBED operation used to connect to statistics service
63 struct GNUNET_TESTBED_Operation *statistics_op;
66 * Handle to the peer's statistics service
68 struct GNUNET_STATISTICS_Handle *statistics;
73 * Name of the configuration file used
75 static const char *cfg_filename = "gnunet-sensor-profiler.conf";
78 * Directory to read sensor definitions from
80 static const char *sensor_src_dir = "sensors";
83 * Directory to write new sensor definitions to
85 static const char *sensor_dst_dir = "/tmp/gnunet-sensor-profiler";
88 * Scheduled task to shutdown
90 static struct GNUNET_SCHEDULER_Task * shutdown_task = NULL;
93 * GNUnet configuration
95 static struct GNUNET_CONFIGURATION_Handle *cfg;
98 * Number of peers to run (Option -p)
100 static unsigned int num_peers = 0;
103 * Set sensors running interval to this value (Option -i)
105 static unsigned int sensors_interval = 0;
108 * Path to topology file (Option -t)
110 static char *topology_file;
113 * Number of peers to simulate anomalies on (Option -a)
115 static unsigned int anomalous_peers = 0;
118 * Array of peer info for all peers
120 static struct PeerInfo *all_peers_info;
123 * Number of peers that we already collected and start their info
125 static int peers_known = 0;
128 * TESTBED operation connecting us to peerstore service on collection point
130 static struct GNUNET_TESTBED_Operation *peerstore_op;
133 * Handle to peerstore service on collection point
135 static struct GNUNET_PEERSTORE_Handle *peerstore;
138 * Dashboard service on collection point started?
140 static int dashboard_service_started = GNUNET_NO;
143 * Number of peers started the sensor service successfully
145 static int sensor_services_started = 0;
148 * Array of sensor names to be used for watching peerstore records
150 static char **sensor_names;
153 * Size of 'sensor_names' array
155 static unsigned int sensor_names_size = 0;
158 * Task run after any waiting period
160 static struct GNUNET_SCHEDULER_Task * delayed_task = NULL;
164 * Copy directory recursively
166 * @param src Path to source directory
167 * @param dst Destination directory, will be created if it does not exist
168 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
171 copy_dir (const char *src, const char *dst);
175 * Do clean up and shutdown scheduler
178 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down.\n");
183 if (NULL != delayed_task)
185 GNUNET_SCHEDULER_cancel (delayed_task);
188 for (i = 0; i < num_peers; i++)
190 if (NULL != all_peers_info[i].statistics_op)
192 GNUNET_TESTBED_operation_done (all_peers_info[i].statistics_op);
193 all_peers_info[i].statistics_op = NULL;
196 if (NULL != peerstore_op)
198 GNUNET_TESTBED_operation_done (peerstore_op);
201 if (NULL != all_peers_info)
203 GNUNET_free (all_peers_info);
204 all_peers_info = NULL;
208 GNUNET_CONFIGURATION_destroy (cfg);
211 if (NULL != sensor_names)
213 for (i = 0; i < sensor_names_size; i++)
214 GNUNET_free (sensor_names[i]);
215 GNUNET_array_grow (sensor_names, sensor_names_size, 0);
217 GNUNET_SCHEDULER_shutdown ();
222 * Function called with each file/folder inside a directory that is being copied.
224 * @param cls closure, destination directory
225 * @param filename complete filename (absolute path)
226 * @return #GNUNET_OK to continue to iterate.
227 * #GNUNET_SYSERR to abort iteration with error
230 copy_dir_scanner (void *cls, const char *filename)
236 GNUNET_asprintf (&dst, "%s%s%s", dst_dir, DIR_SEPARATOR_STR,
237 GNUNET_STRINGS_get_short_name (filename));
238 if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_YES))
239 copy_result = copy_dir (filename, dst);
242 if (GNUNET_YES == GNUNET_DISK_file_test (dst))
243 GNUNET_DISK_directory_remove (dst);
244 copy_result = GNUNET_DISK_file_copy (filename, dst);
245 if (GNUNET_OK == copy_result)
246 GNUNET_DISK_fix_permissions (dst, GNUNET_NO, GNUNET_NO);
254 * Copy directory recursively
256 * @param src Path to source directory
257 * @param dst Destination directory, will be created if it does not exist
258 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
261 copy_dir (const char *src, const char *dst)
263 if (GNUNET_YES != GNUNET_DISK_directory_test (src, GNUNET_YES))
264 return GNUNET_SYSERR;
265 if (GNUNET_OK != GNUNET_DISK_directory_create (dst))
266 return GNUNET_SYSERR;
268 GNUNET_DISK_directory_scan (src, ©_dir_scanner, (char *) dst))
269 return GNUNET_SYSERR;
275 * Function called with each file/folder inside source sensor directory.
277 * @param cls closure (unused)
278 * @param filename complete filename (absolute path)
279 * @return #GNUNET_OK to continue to iterate.
282 sensor_dir_scanner (void *cls, const char *filename)
284 const char *file_basename;
286 struct GNUNET_CONFIGURATION_Handle *sensor_cfg;
289 file_basename = GNUNET_STRINGS_get_short_name (filename);
290 GNUNET_asprintf (&dst_path, "%s%s%s", sensor_dst_dir, DIR_SEPARATOR_STR,
292 if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_NO))
294 GNUNET_assert (GNUNET_OK == copy_dir (filename, dst_path));
298 sensor_name = GNUNET_strdup (file_basename);
299 GNUNET_array_append (sensor_names, sensor_names_size, sensor_name);
300 sensor_cfg = GNUNET_CONFIGURATION_create ();
301 GNUNET_assert (GNUNET_OK ==
302 GNUNET_CONFIGURATION_parse (sensor_cfg, filename));
303 GNUNET_CONFIGURATION_set_value_string (sensor_cfg, file_basename,
305 GNUNET_i2s_full (&all_peers_info
307 if (sensors_interval > 0)
309 GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename,
311 (unsigned long long int)
314 GNUNET_CONFIGURATION_write (sensor_cfg, dst_path);
315 GNUNET_CONFIGURATION_destroy (sensor_cfg);
317 GNUNET_free (dst_path);
323 * Load sensor definitions and rewrite them to tmp location.
324 * Add collection point peer id and change running interval if needed.
329 GNUNET_assert (GNUNET_YES ==
330 GNUNET_DISK_directory_test (sensor_src_dir, GNUNET_YES));
331 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (sensor_dst_dir));
332 GNUNET_DISK_directory_scan (sensor_src_dir, &sensor_dir_scanner, NULL);
337 * Callback to be called when dashboard service is started
339 * @param cls the callback closure from functions generating an operation
340 * @param op the operation that has been finished
341 * @param emsg error message in case the operation has failed; will be NULL if
342 * operation has executed successfully.
345 dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op,
350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Dashboard service started.\n");
354 GNUNET_TESTBED_operation_done (op);
355 dashboard_service_started = GNUNET_YES;
360 * Function called by PEERSTORE for each matching record.
363 * @param record peerstore record information
364 * @param emsg error message, or NULL if no errors
365 * @return #GNUNET_YES to continue iterating, #GNUNET_NO to stop
368 peerstore_watch_cb (void *cls,
369 const struct GNUNET_PEERSTORE_Record *record,
372 struct PeerInfo *peer = cls;
373 struct GNUNET_SENSOR_DashboardAnomalyEntry *anomaly;
377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
380 GNUNET_assert (record->value_size ==
381 sizeof (struct GNUNET_SENSOR_DashboardAnomalyEntry));
382 anomaly = record->value;
384 GNUNET_CRYPTO_cmp_peer_identity (&peer->peer_id,
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Anomaly report:{'peerid': '%s'," "'peer': %d," "'sensor': '%s',"
388 "'anomalous': %d," "'neighbors': %f}\n",
389 GNUNET_i2s (&peer->peer_id), peer->index, record->key,
390 anomaly->anomalous, anomaly->anomalous_neighbors);
396 * Callback to be called when peerstore service connect operation is completed
398 * @param cls the callback closure from functions generating an operation
399 * @param op the operation that has been finished
400 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
401 * @param emsg error message in case the operation has failed; will be NULL if
402 * operation has executed successfully.
405 peerstore_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
406 void *ca_result, const char *emsg)
410 struct PeerInfo *peer;
414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peerstore service.\n");
418 /* Watch for anomaly reports from other peers */
419 for (i = 0; i < num_peers; i++)
421 peer = &all_peers_info[i];
422 for (j = 0; j < sensor_names_size; j++)
424 GNUNET_PEERSTORE_watch (peerstore, "sensordashboard-anomalies",
425 &peer->peer_id, sensor_names[j],
426 &peerstore_watch_cb, peer);
433 * Adapter function called to establish a connection to peerstore service.
436 * @param cfg configuration of the peer to connect to; will be available until
437 * GNUNET_TESTBED_operation_done() is called on the operation returned
438 * from GNUNET_TESTBED_service_connect()
439 * @return service handle to return in 'op_result', NULL on error
442 peerstore_connect_adapter (void *cls,
443 const struct GNUNET_CONFIGURATION_Handle *cfg)
445 peerstore = GNUNET_PEERSTORE_connect (cfg);
446 GNUNET_assert (NULL != peerstore);
452 * Adapter function called to destroy a connection to peerstore service.
455 * @param op_result service handle returned from the connect adapter
458 peerstore_disconnect_adapter (void *cls, void *op_result)
460 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO);
467 * Callback to be called when statistics service connect operation is completed
469 * @param cls the callback closure from functions generating an operation
470 * @param op the operation that has been finished
471 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
472 * @param emsg error message in case the operation has failed; will be NULL if
473 * operation has executed successfully.
476 statistics_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
477 void *ca_result, const char *emsg)
479 struct PeerInfo *peer = cls;
483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Connected to statistics service on peer `%s'.\n", GNUNET_i2s (&peer->peer_id));
488 GNUNET_STATISTICS_set (peer->statistics, "# peers connected", 0, GNUNET_NO);
493 * Adapter function called to establish a connection to statistics service.
496 * @param cfg configuration of the peer to connect to; will be available until
497 * GNUNET_TESTBED_operation_done() is called on the operation returned
498 * from GNUNET_TESTBED_service_connect()
499 * @return service handle to return in 'op_result', NULL on error
502 statistics_connect_adapter (void *cls,
503 const struct GNUNET_CONFIGURATION_Handle *cfg)
505 struct PeerInfo *peer = cls;
507 peer->statistics = GNUNET_STATISTICS_create ("core", cfg);
508 GNUNET_assert (NULL != peer->statistics);
509 return peer->statistics;
514 * Adapter function called to destroy a connection to statistics service.
517 * @param op_result service handle returned from the connect adapter
520 statistics_disconnect_adapter (void *cls, void *op_result)
522 struct PeerInfo *peer = cls;
524 GNUNET_STATISTICS_destroy (peer->statistics, GNUNET_NO);
525 peer->statistics = NULL;
530 * This function is called after the estimated training period is over.
533 simulate_anomalies (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
537 struct GNUNET_TIME_Relative shutdown_delay;
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "Training period over, simulating anomalies now.\n");
542 GNUNET_assert (anomalous_peers <= num_peers);
543 for (i = 0; i < anomalous_peers; i++)
545 an_peer = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
546 if (NULL != all_peers_info[an_peer].statistics_op)
551 all_peers_info[an_peer].statistics_op =
552 GNUNET_TESTBED_service_connect (NULL, all_peers_info[an_peer].testbed_peer,
553 "statistics", &statistics_connect_cb,
554 &all_peers_info[an_peer], &statistics_connect_adapter,
555 &statistics_disconnect_adapter, &all_peers_info[an_peer]);
557 shutdown_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, num_peers * 6);
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down in %s\n",
559 GNUNET_STRINGS_relative_time_to_string (shutdown_delay, GNUNET_NO));
560 GNUNET_SCHEDULER_cancel (shutdown_task);
561 shutdown_task = GNUNET_SCHEDULER_add_delayed (shutdown_delay, &do_shutdown, NULL);
566 * This function is called after a delay which ensures that all peers are
567 * properly initialized
570 peers_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
572 unsigned long long int training_points;
573 struct GNUNET_TIME_Relative training_period;
576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers are ready.\n");
577 GNUNET_assert (GNUNET_OK ==
578 GNUNET_CONFIGURATION_get_value_number (cfg,
579 "sensor-model-gaussian",
583 GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_multiply
584 (GNUNET_TIME_UNIT_SECONDS,
586 0) ? 60 : sensors_interval),
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "Sleeping for a training period of %s.\n",
590 GNUNET_STRINGS_relative_time_to_string (training_period,
593 GNUNET_SCHEDULER_add_delayed (training_period, &simulate_anomalies, NULL);
598 * Callback to be called when sensor service is started
600 * @param cls the callback closure from functions generating an operation
601 * @param op the operation that has been finished
602 * @param emsg error message in case the operation has failed; will be NULL if
603 * operation has executed successfully.
606 sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op,
609 struct PeerInfo *peer = cls;
613 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sensor service started on peer `%s'.\n",
617 GNUNET_i2s (&peer->peer_id));
618 GNUNET_TESTBED_operation_done (op);
619 sensor_services_started++;
620 if (sensor_services_started == num_peers)
623 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
624 (PEER_STARTUP_TIME, num_peers),
631 * Callback to be called when the requested peer information is available
633 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
634 * @param op the operation this callback corresponds to
635 * @param pinfo the result; will be NULL if the operation has failed
636 * @param emsg error message if the operation has failed; will be NULL if the
637 * operation is successfull
640 peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
641 const struct GNUNET_TESTBED_PeerInformation *pinfo,
644 struct GNUNET_TESTBED_Peer *testbed_peer = cb_cls;
645 struct PeerInfo *peer = &all_peers_info[peers_known];
649 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
652 peer->testbed_peer = testbed_peer;
653 GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id);
654 peer->index = peers_known;
656 if (1 == peers_known) /* First peer is collection point */
658 /* Rewrite sensors */
660 /* Start dashboard */
661 GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensordashboard",
662 &dashboard_started, NULL, 1);
664 /* Start sensor service on every peer */
665 GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensor",
666 &sensor_service_started, peer, 1);
667 if (num_peers == peers_known) /* Last peer */
669 /* Connect to peerstore on first peer (collection point) */
671 GNUNET_TESTBED_service_connect (NULL, all_peers_info[0].testbed_peer,
672 "peerstore", &peerstore_connect_cb,
673 NULL, &peerstore_connect_adapter,
674 &peerstore_disconnect_adapter, NULL);
676 GNUNET_TESTBED_operation_done (op);
681 * Signature of a main function for a testcase.
684 * @param h the run handle
685 * @param num number of peers in 'peers'
686 * @param peers handle to peers run in the testbed. NULL upon timeout (see
687 * GNUNET_TESTBED_test_run()).
688 * @param links_succeeded the number of overlay link connection attempts that
690 * @param links_failed the number of overlay link connection attempts that
692 * @see GNUNET_TESTBED_test_run()
695 test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num,
696 struct GNUNET_TESTBED_Peer **peers, unsigned int links_succeeded,
697 unsigned int links_failed)
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "%d peers started. %d links succeeded. %d links failed.\n",
703 num_peers, links_succeeded, links_failed);
704 GNUNET_assert (num == num_peers);
705 /* Collect peer information */
706 all_peers_info = GNUNET_new_array (num_peers, struct PeerInfo);
708 for (i = 0; i < num_peers; i++)
710 GNUNET_TESTBED_peer_get_information (peers[i],
711 GNUNET_TESTBED_PIT_CONFIGURATION,
712 &peer_info_cb, peers[i]);
718 * Verify that the user passed correct CL args
720 * @return #GNUNET_OK if arguments are valid, #GNUNET_SYSERR otherwise
727 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
729 ("Invalid or missing number of peers. Set at least 2 peers.\n"));
730 return GNUNET_SYSERR;
732 if (NULL == topology_file ||
733 GNUNET_YES != GNUNET_DISK_file_test (topology_file))
735 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
736 _("Missing or invalid topology file.\n"));
737 return GNUNET_SYSERR;
744 * Actual main function.
747 * @param args remaining args, unused
748 * @param cfgfile name of the configuration
749 * @param cfg configuration handle
752 run (void *cls, char *const *args, const char *cf,
753 const struct GNUNET_CONFIGURATION_Handle *c)
755 if (GNUNET_OK != verify_args ())
757 do_shutdown (NULL, NULL);
760 cfg = GNUNET_CONFIGURATION_create ();
761 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_filename));
762 GNUNET_CONFIGURATION_set_value_string ((struct GNUNET_CONFIGURATION_Handle *)
764 "OVERLAY_TOPOLOGY_FILE",
767 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_shutdown,
769 GNUNET_TESTBED_run (NULL, cfg, num_peers, 0, NULL, NULL, &test_master, NULL);
776 * @return 0 on success
779 main (int argc, char *const *argv)
781 static struct GNUNET_GETOPT_CommandLineOption options[] = {
782 {'p', "peers", "COUNT", gettext_noop ("Number of peers to run"), GNUNET_YES,
783 &GNUNET_GETOPT_set_uint, &num_peers},
784 {'t', "topology-file", "FILEPATH", gettext_noop ("Path to topology file"),
785 GNUNET_YES, &GNUNET_GETOPT_set_filename, &topology_file},
786 {'i', "sensors-interval", "INTERVAL",
787 gettext_noop ("Change the interval of running sensors to given value"),
788 GNUNET_YES, &GNUNET_GETOPT_set_uint, &sensors_interval},
789 {'a', "anomalous-peers", "COUNT",
790 gettext_noop ("Number of peers to simulate anomalies on"), GNUNET_YES,
791 &GNUNET_GETOPT_set_uint, &anomalous_peers},
792 GNUNET_GETOPT_OPTION_END
796 GNUNET_PROGRAM_run (argc, argv, "gnunet-sensor-profiler",
797 gettext_noop ("Profiler for sensor service"),
798 options, &run, NULL)) ? 0 : 1;
801 /* end of gnunet-sensor-profiler.c */