2 This file is part of GNUnet.
3 (C) 2010-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.
21 * @file ats/perf_ats.c
22 * @brief ats benchmark: start peers and modify preferences, monitor change over time
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
32 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33 #define TESTNAME_PREFIX "perf_ats_"
37 * Information we track for a peer in the testbed.
42 * Handle with testbed.
44 struct GNUNET_TESTBED_Peer *peer;
48 struct GNUNET_PeerIdentity id;
50 struct GNUNET_CORE_Handle *ch;
53 * Testbed operation to connect to ATS performance service
55 struct GNUNET_TESTBED_Operation *ats_perf_op;
58 * Testbed operation to get peer information
60 struct GNUNET_TESTBED_Operation *info_op;
63 * Testbed operation to connect peers
65 struct GNUNET_TESTBED_Operation *connect_op;
68 * Testbed operation to connect to core
70 struct GNUNET_TESTBED_Operation *core_op;
73 * ATS performance handle
75 struct GNUNET_ATS_PerformanceHandle *p_handle;
80 struct BenchmarkPeer *ph;
84 int connected_ATS_SRV;
85 int connected_CORE_SRV;
89 int *core_connections;
92 static struct BenchmarkState state;
97 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
101 static char *preference;
106 core_connect_completion_cb (void *cls,
107 struct GNUNET_TESTBED_Operation *op,
115 * @param tc the task context
118 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 shutdown_task = GNUNET_SCHEDULER_NO_TASK;
123 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Benchmarking done\n"));
124 for (c_p = 0; c_p < peers; c_p++)
126 if (NULL != ph[c_p].ats_perf_op)
128 GNUNET_TESTBED_operation_done (ph[c_p].ats_perf_op);
129 ph[c_p].ats_perf_op = NULL;
132 if (NULL != ph[c_p].core_op)
134 GNUNET_TESTBED_operation_done (ph[c_p].core_op);
135 ph[c_p].core_op = NULL;
138 if (NULL != ph[c_p].info_op)
141 GNUNET_TESTBED_operation_done (ph[c_p].info_op);
142 ph[c_p].info_op = NULL;
144 if (NULL != ph[c_p].connect_op)
147 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect peer 0 and %u\n"), c_p);
148 GNUNET_TESTBED_operation_done (ph[c_p].connect_op);
149 ph[c_p].connect_op = NULL;
154 GNUNET_SCHEDULER_shutdown();
159 ats_performance_info_cb (void *cls,
160 const struct GNUNET_HELLO_Address *address,
162 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
163 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
164 const struct GNUNET_ATS_Information *ats,
167 struct BenchmarkPeer *p = cls;
171 return; /* print only master peer */
172 peer_id = GNUNET_strdup (GNUNET_i2s (&p->id));
173 for (c_a = 0; c_a < ats_count; c_a++)
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("%s: %s %s %u\n"),
177 GNUNET_i2s (&address->peer),
178 GNUNET_ATS_print_property_type(ntohl(ats[c_a].type)),
179 ntohl(ats[c_a].value));
181 GNUNET_free (peer_id);
188 if ((state.connected_ATS_SRV == GNUNET_NO) ||
189 (state.connected_CORE_SRV == GNUNET_NO) ||
190 (state.connected_PEERS == GNUNET_NO) ||
191 (state.connected_CORE == GNUNET_NO))
194 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
195 _("BENCHMARKING\n"));
200 connect_completion_callback (void *cls,
201 struct GNUNET_TESTBED_Operation *op,
204 static int connections = 0;
205 struct BenchmarkPeer *p = cls;
209 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
210 _("Connected peer 0 with peer %p\n"), p->peer);
214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
215 _("Failed to connect peer 0 with peer %p\n"), p->peer);
217 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
218 GNUNET_SCHEDULER_cancel(shutdown_task);
219 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL);
221 GNUNET_TESTBED_operation_done(op);
222 p->connect_op = NULL;
224 if (connections == peers -1)
226 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
227 "All peers connected, start benchmarking \n");
228 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL);
229 state.connected_PEERS = GNUNET_YES;
235 do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
239 if ((state.connected_ATS_SRV == GNUNET_NO) ||
240 (state.connected_CORE_SRV == GNUNET_NO))
243 for (c_p = 1; c_p < peers; c_p ++)
245 ph[c_p].connect_op = GNUNET_TESTBED_overlay_connect( NULL,
246 &connect_completion_callback, &ph[c_p], ph[0].peer, ph[c_p].peer);
247 if (NULL == ph[c_p].connect_op)
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 _("Could not connect peer 0 and peer %u\n"), c_p);
252 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
253 GNUNET_SCHEDULER_cancel(shutdown_task);
254 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL);
262 * Controller event callback
265 * @param event the controller event
268 controller_event_cb (void *cls,
269 const struct GNUNET_TESTBED_EventInformation *event)
271 //struct BenchmarkPeer *p = cls;
274 case GNUNET_TESTBED_ET_CONNECT:
276 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
281 GNUNET_SCHEDULER_cancel (shutdown_task);
282 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
288 * Called to open a connection to the peer's ATS performance
290 * @param cls peer context
291 * @param cfg configuration of the peer to connect to; will be available until
292 * GNUNET_TESTBED_operation_done() is called on the operation returned
293 * from GNUNET_TESTBED_service_connect()
294 * @return service handle to return in 'op_result', NULL on error
297 ats_perf_connect_adapter (void *cls,
298 const struct GNUNET_CONFIGURATION_Handle *cfg)
300 struct BenchmarkPeer *peer = cls;
302 peer->p_handle = GNUNET_ATS_performance_init (cfg, &ats_performance_info_cb, peer);
303 if (NULL == peer->p_handle)
304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create ATS performance handle \n");
305 return peer->p_handle;
310 * Called to disconnect from peer's statistics service
312 * @param cls peer context
313 * @param op_result service handle returned from the connect adapter
316 ats_perf_disconnect_adapter (void *cls, void *op_result)
318 struct BenchmarkPeer *peer = cls;
320 GNUNET_ATS_performance_done(peer->p_handle);
321 peer->p_handle = NULL;
326 * Callback to be called when a service connect operation is completed
328 * @param cls the callback closure from functions generating an operation
329 * @param op the operation that has been finished
330 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
331 * @param emsg error message in case the operation has failed; will be NULL if
332 * operation has executed successfully.
335 ats_connect_completion_cb (void *cls,
336 struct GNUNET_TESTBED_Operation *op,
340 static int op_done = 0;
341 if ((NULL != emsg) || (NULL == ca_result))
343 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
344 _("Initialization failed, shutdown\n"));
346 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
347 GNUNET_SCHEDULER_cancel(shutdown_task);
348 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL);
353 if (op_done == peers)
355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
356 _("Initialization done, connecting peers\n"));
357 state.connected_ATS_SRV = GNUNET_YES;
358 GNUNET_SCHEDULER_add_now (&do_connect, NULL);
364 * Method called whenever a given peer connects.
367 * @param peer peer identity this notification is about
370 core_connect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
372 struct BenchmarkPeer *p = cls;
375 p->core_connections ++;
376 id = GNUNET_strdup (GNUNET_i2s (&p->id));
377 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
378 "%s connected to %s \n",
379 id, GNUNET_i2s (peer));
380 if (p->core_connections == peers)
382 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
383 "%s connected all peers\n",
386 if ((p->core_connections == peers) && (p == &ph[0]))
388 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
389 "Master peer %s connected all peers on CORE level\n", id, GNUNET_i2s (peer));
390 state.connected_CORE = GNUNET_YES;
391 GNUNET_SCHEDULER_add_now (&do_benchmark, NULL);
398 * Method called whenever a peer disconnects.
401 * @param peer peer identity this notification is about
404 core_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity * peer)
406 struct BenchmarkPeer *p = cls;
409 id = GNUNET_strdup (GNUNET_i2s (&p->id));
410 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
411 "%s disconnected from %s \n", id, GNUNET_i2s (peer));
417 * Called to open a connection to the peer's ATS performance
419 * @param cls peer context
420 * @param cfg configuration of the peer to connect to; will be available until
421 * GNUNET_TESTBED_operation_done() is called on the operation returned
422 * from GNUNET_TESTBED_service_connect()
423 * @return service handle to return in 'op_result', NULL on error
426 core_connect_adapter (void *cls,
427 const struct GNUNET_CONFIGURATION_Handle *cfg)
429 struct BenchmarkPeer *peer = cls;
431 peer->ch = GNUNET_CORE_connect(cfg, peer, NULL,
434 NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
435 if (NULL == peer->ch)
436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
437 "Failed to create core connection \n");
443 * Callback to be called when a service connect operation is completed
445 * @param cls the callback closure from functions generating an operation
446 * @param op the operation that has been finished
447 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
448 * @param emsg error message in case the operation has failed; will be NULL if
449 * operation has executed successfully.
452 core_connect_completion_cb (void *cls,
453 struct GNUNET_TESTBED_Operation *op,
457 static int core_done = 0;
458 if ((NULL != emsg) || (NULL == ca_result))
460 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
461 _("Initialization failed, shutdown\n"));
463 if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
464 GNUNET_SCHEDULER_cancel(shutdown_task);
465 shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL);
469 if (core_done == peers)
471 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
472 "Connected to all CORE services\n");
473 state.connected_CORE_SRV = GNUNET_YES;
474 GNUNET_SCHEDULER_add_now (&do_connect, NULL);
480 * Called to disconnect from peer's statistics service
482 * @param cls peer context
483 * @param op_result service handle returned from the connect adapter
486 core_disconnect_adapter (void *cls, void *op_result)
488 struct BenchmarkPeer *peer = cls;
490 GNUNET_CORE_disconnect (peer->ch);
496 * Callback to be called when the requested peer information is available
498 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
499 * @param op the operation this callback corresponds to
500 * @param pinfo the result; will be NULL if the operation has failed
501 * @param emsg error message if the operation has failed; will be NULL if the
502 * operation is successfull
505 peerinformation_cb (void *cb_cls,
506 struct GNUNET_TESTBED_Operation *op,
507 const struct GNUNET_TESTBED_PeerInformation*pinfo,
510 struct BenchmarkPeer *p = cb_cls;
512 if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
514 p->id = *pinfo->result.id;
515 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
517 p->no, GNUNET_i2s (&p->id));
523 GNUNET_TESTBED_operation_done (op);
529 * Signature of a main function for a testcase.
532 * @param num_peers number of peers in 'peers'
533 * @param peers_ handle to peers run in the testbed
534 * @param links_succeeded the number of overlay link connection attempts that
536 * @param links_failed the number of overlay link connection attempts that
540 test_main (void *cls, unsigned int num_peers,
541 struct GNUNET_TESTBED_Peer **peers_,
542 unsigned int links_succeeded,
543 unsigned int links_failed)
547 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
548 _("Benchmarking solver `%s' on preference `%s' with %u peers\n"),
549 solver, preference, peers);
551 shutdown_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &do_shutdown, NULL);
553 GNUNET_assert (NULL == cls);
554 GNUNET_assert (peers == num_peers);
555 GNUNET_assert (NULL != peers_);
557 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
558 _("Initializing... \n"));
560 for (c_p = 0; c_p < num_peers; c_p++)
562 GNUNET_assert (NULL != peers_[c_p]);
564 /* Connect to ATS performance service */
565 ph[c_p].peer = peers_[c_p];
567 ph[c_p].info_op = GNUNET_TESTBED_peer_get_information (ph[c_p].peer,
568 GNUNET_TESTBED_PIT_IDENTITY,
569 &peerinformation_cb, &ph[c_p]);
571 ph[c_p].core_op = GNUNET_TESTBED_service_connect (NULL,
573 core_connect_completion_cb, NULL,
574 &core_connect_adapter,
575 &core_disconnect_adapter,
578 ph[c_p].ats_perf_op = GNUNET_TESTBED_service_connect (NULL,
580 ats_connect_completion_cb, NULL,
581 &ats_perf_connect_adapter,
582 &ats_perf_disconnect_adapter,
589 main (int argc, char *argv[])
601 /* figure out testname */
602 tmp = strstr (argv[0], TESTNAME_PREFIX);
605 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
606 return GNUNET_SYSERR;
608 tmp += strlen(TESTNAME_PREFIX);
609 solver = GNUNET_strdup (tmp);
610 if (NULL != (dotexe = strstr (solver, ".exe")) &&
613 tmp_sep = strchr (solver, '_');
616 fprintf (stderr, "Unable to parse test name `%s'\n", argv[0]);
617 GNUNET_free (solver);
618 return GNUNET_SYSERR;
621 preference = GNUNET_strdup(tmp_sep + 1);
623 GNUNET_asprintf(&conf_name, "%s%s_%s.conf", TESTNAME_PREFIX, solver, preference);
624 GNUNET_asprintf(&test_name, "%s%s_%s", TESTNAME_PREFIX, solver, preference);
626 for (c = 0; c < (argc -1); c++)
628 if (0 == strcmp(argv[c], "-c"))
633 if ((0L != (peers = strtol (argv[c + 1], NULL, 10))) && (peers >= 2))
634 fprintf (stderr, "Starting %u peers\n", peers);
641 ph = GNUNET_malloc (peers * sizeof (struct BenchmarkPeer));
642 state.connected_ATS_SRV = GNUNET_NO;
643 state.connected_CORE_SRV = GNUNET_NO;
644 state.connected_PEERS = GNUNET_NO;
648 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
649 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
650 (void) GNUNET_TESTBED_test_run (test_name,
652 event_mask, &controller_event_cb, NULL,
655 GNUNET_free (solver);
656 GNUNET_free (preference);
657 GNUNET_free (conf_name);
658 GNUNET_free (test_name);
664 /* end of file perf_ats.c */