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.
24 * - Rewrite interval time (optional)
26 * - Monitor dashboard records
27 * - Prompt for anomalies when ready:
28 * -- Cut Y peers (remove their connections to other X-Y peers but not the connections among themselves)
32 * @file sensor/gnunet-sensor-profiler.c
33 * @brief Profiler for the sensor service
34 * @author Omar Tarabai
37 #include "gnunet_util_lib.h"
38 #include "gnunet_testbed_service.h"
39 #include "gnunet_peerstore_service.h"
40 #include "gnunet_sensor_service.h"
43 * Information about a single peer
51 struct GNUNET_PeerIdentity peer_id;
56 struct GNUNET_TESTBED_Peer *testbed_peer;
62 * Name of the configuration file used
64 static const char *cfg_filename = "gnunet-sensor-profiler.conf";
67 * Directory to read sensor definitions from
69 static const char *sensor_src_dir = "sensors";
72 * Directory to write new sensor definitions to
74 static const char *sensor_dst_dir = "/tmp/gnunet-sensor-profiler";
77 * Return value of the program
82 * Number of peers to run (Option -p)
84 static unsigned int num_peers = 0;
87 * Set sensors running interval to this value (Option -i)
89 static unsigned int sensors_interval = 0;
92 * Array of peer info for all peers
94 static struct PeerInfo *all_peers_info;
97 * Number of peers that we already collected and start their info
99 static int peers_known = 0;
102 * TESTBED operation connecting us to peerstore service on collection point
104 static struct GNUNET_TESTBED_Operation *peerstore_op;
107 * Handle to peerstore service on collection point
109 static struct GNUNET_PEERSTORE_Handle *peerstore;
113 * Copy directory recursively
115 * @param src Path to source directory
116 * @param dst Destination directory, will be created if it does not exist
117 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
120 copy_dir (const char *src, const char *dst);
124 * Do clean up and shutdown scheduler
127 do_shutdown () // TODO: schedule timeout shutdown
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down.\n");
130 if (NULL != peerstore_op)
132 GNUNET_TESTBED_operation_done (peerstore_op);
135 if (NULL != all_peers_info)
137 GNUNET_free (all_peers_info);
138 all_peers_info = NULL;
140 GNUNET_SCHEDULER_shutdown ();
145 * Function called with each file/folder inside a directory that is being copied.
147 * @param cls closure, destination directory
148 * @param filename complete filename (absolute path)
149 * @return #GNUNET_OK to continue to iterate.
150 * #GNUNET_SYSERR to abort iteration with error
153 copy_dir_scanner (void *cls, const char *filename)
159 GNUNET_asprintf (&dst, "%s%s%s", dst_dir, DIR_SEPARATOR_STR,
160 GNUNET_STRINGS_get_short_name (filename));
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Copying `%s' to `%s'.\n", filename,
163 if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_YES))
164 copy_result = copy_dir (filename, dst);
167 if (GNUNET_YES == GNUNET_DISK_file_test (dst))
168 GNUNET_DISK_directory_remove (dst);
169 copy_result = GNUNET_DISK_file_copy (filename, dst);
177 * Copy directory recursively
179 * @param src Path to source directory
180 * @param dst Destination directory, will be created if it does not exist
181 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
184 copy_dir (const char *src, const char *dst)
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Copying directory `%s' to `%s'.\n", src,
188 if (GNUNET_YES != GNUNET_DISK_directory_test (src, GNUNET_YES))
189 return GNUNET_SYSERR;
190 if (GNUNET_OK != GNUNET_DISK_directory_create (dst))
191 return GNUNET_SYSERR;
193 GNUNET_DISK_directory_scan (src, ©_dir_scanner, (char *) dst))
194 return GNUNET_SYSERR;
200 * Function called with each file/folder inside source sensor directory.
202 * @param cls closure (unused)
203 * @param filename complete filename (absolute path)
204 * @return #GNUNET_OK to continue to iterate.
207 sensor_dir_scanner (void *cls, const char *filename)
209 const char *file_basename;
211 struct GNUNET_CONFIGURATION_Handle *sensor_cfg;
213 file_basename = GNUNET_STRINGS_get_short_name (filename);
214 GNUNET_asprintf (&dst_path, "%s%s%s", sensor_dst_dir, DIR_SEPARATOR_STR,
216 if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_NO))
218 GNUNET_assert (GNUNET_OK == copy_dir (filename, dst_path));
222 sensor_cfg = GNUNET_CONFIGURATION_create ();
223 GNUNET_assert (GNUNET_OK ==
224 GNUNET_CONFIGURATION_parse (sensor_cfg, filename));
225 GNUNET_CONFIGURATION_set_value_string (sensor_cfg, file_basename,
227 GNUNET_i2s_full (&all_peers_info[0].
229 if (sensors_interval > 0)
231 GNUNET_CONFIGURATION_set_value_number (sensor_cfg, file_basename,
233 (unsigned long long int)
236 GNUNET_CONFIGURATION_write (sensor_cfg, dst_path);
237 GNUNET_CONFIGURATION_destroy (sensor_cfg);
239 GNUNET_free (dst_path);
245 * Load sensor definitions and rewrite them to tmp location.
246 * Add collection point peer id and change running interval if needed.
251 GNUNET_assert (GNUNET_YES ==
252 GNUNET_DISK_directory_test (sensor_src_dir, GNUNET_YES));
253 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (sensor_dst_dir));
254 GNUNET_DISK_directory_scan (sensor_src_dir, &sensor_dir_scanner, NULL);
259 * Callback to be called when dashboard service is started
261 * @param cls the callback closure from functions generating an operation
262 * @param op the operation that has been finished
263 * @param emsg error message in case the operation has failed; will be NULL if
264 * operation has executed successfully.
267 dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op,
272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Dashboard service started.\n");
277 GNUNET_TESTBED_operation_done (op);
282 * Callback to be called when peerstore service connect operation is completed
284 * @param cls the callback closure from functions generating an operation
285 * @param op the operation that has been finished
286 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
287 * @param emsg error message in case the operation has failed; will be NULL if
288 * operation has executed successfully.
291 peerstore_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
292 void *ca_result, const char *emsg)
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected to peerstore service.\n");
305 * Adapter function called to establish a connection to peerstore service.
308 * @param cfg configuration of the peer to connect to; will be available until
309 * GNUNET_TESTBED_operation_done() is called on the operation returned
310 * from GNUNET_TESTBED_service_connect()
311 * @return service handle to return in 'op_result', NULL on error
314 peerstore_connect_adapter (void *cls,
315 const struct GNUNET_CONFIGURATION_Handle *cfg)
317 peerstore = GNUNET_PEERSTORE_connect (cfg);
318 GNUNET_assert (NULL != peerstore);
324 * Adapter function called to destroy a connection to peerstore service.
327 * @param op_result service handle returned from the connect adapter
330 peerstore_disconnect_adapter (void *cls, void *op_result)
332 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO);
339 * Callback to be called when sensor service is started
341 * @param cls the callback closure from functions generating an operation
342 * @param op the operation that has been finished
343 * @param emsg error message in case the operation has failed; will be NULL if
344 * operation has executed successfully.
347 sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op,
350 struct PeerInfo *peer = cls;
354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sensor service started on peer `%s'.\n",
358 GNUNET_i2s (&peer->peer_id));
360 GNUNET_TESTBED_operation_done (op);
365 * Callback to be called when the requested peer information is available
367 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
368 * @param op the operation this callback corresponds to
369 * @param pinfo the result; will be NULL if the operation has failed
370 * @param emsg error message if the operation has failed; will be NULL if the
371 * operation is successfull
374 peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
375 const struct GNUNET_TESTBED_PeerInformation *pinfo,
378 struct GNUNET_TESTBED_Peer *testbed_peer = cb_cls;
379 struct PeerInfo *peer = &all_peers_info[peers_known];
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
386 peer->testbed_peer = testbed_peer;
387 GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id);
389 if (1 == peers_known) /* First peer is collection point */
391 /* Rewrite sensors */
393 /* Start dashboard */
394 GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensordashboard",
395 &dashboard_started, NULL, 1);
397 /* Start sensor service on every peer */
398 GNUNET_TESTBED_peer_manage_service (NULL, testbed_peer, "sensor",
399 &sensor_service_started, peer, 1);
400 if (num_peers == peers_known) /* Last peer */
402 /* Connect to peerstore on first peer (collection point) */
404 GNUNET_TESTBED_service_connect (NULL, all_peers_info[0].testbed_peer,
405 "peerstore", &peerstore_connect_cb,
406 NULL, &peerstore_connect_adapter,
407 &peerstore_disconnect_adapter, NULL);
409 GNUNET_TESTBED_operation_done (op);
414 * Signature of a main function for a testcase.
417 * @param h the run handle
418 * @param num number of peers in 'peers'
419 * @param peers handle to peers run in the testbed. NULL upon timeout (see
420 * GNUNET_TESTBED_test_run()).
421 * @param links_succeeded the number of overlay link connection attempts that
423 * @param links_failed the number of overlay link connection attempts that
425 * @see GNUNET_TESTBED_test_run()
428 test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num,
429 struct GNUNET_TESTBED_Peer **peers, unsigned int links_succeeded,
430 unsigned int links_failed)
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "%d peers started. %d links succeeded. %d links failed.\n",
436 num_peers, links_succeeded, links_failed);
437 GNUNET_assert (num == num_peers);
438 GNUNET_assert (0 == links_failed);
439 /* Collect peer information */
440 all_peers_info = GNUNET_new_array (num_peers, struct PeerInfo);
442 for (i = 0; i < num_peers; i++)
444 GNUNET_TESTBED_peer_get_information (peers[i],
445 GNUNET_TESTBED_PIT_CONFIGURATION,
446 &peer_info_cb, peers[i]);
452 * Verify that the user passed correct CL args
454 * @return #GNUNET_OK if arguments are valid, #GNUNET_SYSERR otherwise
461 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
463 ("Invalid or missing number of peers. Set at least 3 peers.\n"));
464 return GNUNET_SYSERR;
471 * Actual main function.
474 * @param args remaining args, unused
475 * @param cfgfile name of the configuration
476 * @param cfg configuration handle
479 run (void *cls, char *const *args, const char *cf,
480 const struct GNUNET_CONFIGURATION_Handle *c)
482 struct GNUNET_CONFIGURATION_Handle *cfg;
485 if (GNUNET_OK != verify_args ())
490 cfg = GNUNET_CONFIGURATION_create ();
491 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_filename));
492 links = log (num_peers) * log (num_peers) * num_peers / 2;
493 GNUNET_CONFIGURATION_set_value_number ((struct GNUNET_CONFIGURATION_Handle *)
494 cfg, "TESTBED", "OVERLAY_RANDOM_LINKS",
495 (unsigned long long int) links);
496 GNUNET_TESTBED_run (NULL, cfg, num_peers, 0, NULL, NULL, &test_master, NULL);
497 GNUNET_CONFIGURATION_destroy (cfg);
504 * @return 0 on success
507 main (int argc, char *const *argv)
509 static struct GNUNET_GETOPT_CommandLineOption options[] = {
510 {'p', "peers", "COUNT", gettext_noop ("Number of peers to run"), GNUNET_YES,
511 &GNUNET_GETOPT_set_uint, &num_peers},
512 {'i', "sensors-interval", "INTERVAL",
513 gettext_noop ("Change the interval or running sensors to given value"),
514 GNUNET_YES, &GNUNET_GETOPT_set_uint, &sensors_interval},
515 GNUNET_GETOPT_OPTION_END
519 GNUNET_PROGRAM_run (argc, argv, "gnunet-sensor-profiler",
520 gettext_noop ("Profiler for sensor service"),
521 options, &run, NULL)) ? ok : 1;
524 /* end of gnunet-sensor-profiler.c */