2 This file is part of GNUnet
3 (C) 2008--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 testbed/testbed_api_statistics.c
23 * @brief high-level statistics function
24 * @author Christian Grothoff
25 * @author Sree Harsha Totakura
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
31 #include "testbed_api_operations.h"
35 * Generic logging shorthand
37 #define LOG(kind,...) \
38 GNUNET_log_from (kind, "testbed-api-statistics", __VA_ARGS__)
41 * Debug logging shorthand
43 #define LOG_DEBUG(...) \
44 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
48 * Context information for use in GNUNET_TESTBED_get_statistics()
50 struct GetStatsContext
53 * The main operation we generate while creating this context
55 struct GNUNET_TESTBED_Operation *main_op;
58 * The service connect operations we create to open connection to the
59 * statistics service of each given peer
61 struct GNUNET_TESTBED_Operation **ops;
64 * The array of peers whose statistics services are to be accessed
66 struct GNUNET_TESTBED_Peer **peers;
69 * The subsystem of peers for which statistics are requested
74 * The particular statistics value of interest
79 * The iterator to call with statistics information
81 GNUNET_TESTBED_StatisticsIterator proc;
84 * The callback to call when we are done iterating through all peers'
87 GNUNET_TESTBED_OperationCompletionCallback cont;
90 * The closure for the above callbacks
95 * The task for calling the continuation callback
97 GNUNET_SCHEDULER_TaskIdentifier call_completion_task_id;
100 * The number of peers present in the peers array. This number also
101 * represents the number of service connect operations in the ops array
103 unsigned int num_peers;
106 * How many peers' statistics have we iterated through
108 unsigned int num_completed;
114 * Context information with respect to a particular peer
116 struct PeerGetStatsContext
119 * The GetStatsContext which is associated with this context
121 struct GetStatsContext *sc;
124 * The handle from GNUNET_STATISTICS_get()
126 struct GNUNET_STATISTICS_GetHandle *get_handle;
129 * Task to mark the statistics service connect operation as done
131 GNUNET_SCHEDULER_TaskIdentifier op_done_task_id;
134 * The index of this peer in the peers array of GetStatsContext
136 unsigned int peer_index;
141 * A no-wait operation queue
143 static struct OperationQueue *no_wait_queue;
147 * Call statistics operation completion. We call it in a separate task because
148 * the iteration_completion_cb() cannot destroy statistics handle which will be
149 * the case if the user calles GNUNET_TESTBED_operation_done() on the
150 * get_statistics operation.
152 * @param cls the GetStatsContext
153 * @param tc the scheduler task context
156 call_completion_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 struct GetStatsContext *sc = cls;
160 GNUNET_assert (sc->call_completion_task_id != GNUNET_SCHEDULER_NO_TASK);
161 sc->call_completion_task_id = GNUNET_SCHEDULER_NO_TASK;
162 LOG_DEBUG ("Calling get_statistics() continuation callback\n");
163 sc->cont (sc->cb_cls, sc->main_op, NULL);
168 * Task to mark statistics service connect operation as done. We call it here
169 * as we cannot destroy the statistics handle in iteration_completion_cb()
171 * @param cls the PeerGetStatsContext
172 * @param tc the scheduler task context
175 op_done_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
177 struct PeerGetStatsContext *peer_sc = cls;
178 struct GetStatsContext *sc;
179 struct GNUNET_TESTBED_Operation **op;
182 peer_sc->op_done_task_id = GNUNET_SCHEDULER_NO_TASK;
183 op = &sc->ops[peer_sc->peer_index];
184 GNUNET_assert (NULL != *op);
185 GNUNET_TESTBED_operation_done (*op);
191 * Continuation called by the "get_all" and "get" functions.
193 * @param cls the PeerGetStatsContext
194 * @param success GNUNET_OK if statistics were
195 * successfully obtained, GNUNET_SYSERR if not.
198 iteration_completion_cb (void *cls, int success)
200 struct PeerGetStatsContext *peer_sc = cls;
201 struct GetStatsContext *sc;
203 GNUNET_break (GNUNET_OK == success);
205 peer_sc->get_handle = NULL;
207 peer_sc->op_done_task_id = GNUNET_SCHEDULER_add_now (&op_done_task, peer_sc);
208 if (sc->num_completed == sc->num_peers)
210 LOG_DEBUG ("Scheduling to call iteration completion callback\n");
211 sc->call_completion_task_id =
212 GNUNET_SCHEDULER_add_now (&call_completion_task, sc);
218 * Callback function to process statistic values.
220 * @param cls the PeerGetStatsContext
221 * @param subsystem name of subsystem that created the statistic
222 * @param name the name of the datum
223 * @param value the current value
224 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
225 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
228 iterator_cb (void *cls, const char *subsystem,
229 const char *name, uint64_t value,
232 struct PeerGetStatsContext *peer_sc = cls;
233 struct GetStatsContext *sc;
234 struct GNUNET_TESTBED_Peer *peer;
238 peer = sc->peers[peer_sc->peer_index];
239 LOG_DEBUG ("Peer %u: [%s,%s] -> %lu\n", peer_sc->peer_index,
240 subsystem, name, (unsigned long) value);
241 ret = sc->proc (sc->cb_cls, peer,
242 subsystem, name, value, is_persistent);
243 if (GNUNET_SYSERR == ret)
244 LOG_DEBUG ("Aborting iteration for peer %u\n", peer_sc->peer_index);
250 * Called after opening a connection to the statistics service of a peer
252 * @param cls the PeerGetStatsContext
253 * @param op the operation that has been finished
254 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
255 * @param emsg error message in case the operation has failed; will be NULL if
256 * operation has executed successfully.
259 service_connect_comp (void *cls,
260 struct GNUNET_TESTBED_Operation *op,
264 struct PeerGetStatsContext *peer_sc = cls;
265 struct GNUNET_STATISTICS_Handle *h = ca_result;
267 LOG_DEBUG ("Retrieving statistics of peer %u\n", peer_sc->peer_index);
268 peer_sc->get_handle =
269 GNUNET_STATISTICS_get (h, peer_sc->sc->subsystem, peer_sc->sc->name,
270 GNUNET_TIME_UNIT_FOREVER_REL,
271 &iteration_completion_cb,
272 iterator_cb, peer_sc);
277 * Adapter function called to establish a connection to the statistics service
280 * @param cls the PeerGetStatsContext
281 * @param cfg configuration of the peer to connect to; will be available until
282 * GNUNET_TESTBED_operation_done() is called on the operation returned
283 * from GNUNET_TESTBED_service_connect()
284 * @return service handle to return in 'op_result', NULL on error
287 statistics_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
289 struct PeerGetStatsContext *peer_sc = cls;
291 LOG_DEBUG ("Connecting to statistics service of peer %u\n",
292 peer_sc->peer_index);
293 return GNUNET_STATISTICS_create ("<testbed-api>", cfg);
298 * Adapter function called to destroy statistics connection
300 * @param cls the PeerGetStatsContext
301 * @param op_result service handle returned from the connect adapter
304 statistics_da (void *cls, void *op_result)
306 struct PeerGetStatsContext *peer_sc = cls;
307 struct GNUNET_STATISTICS_Handle *sh = op_result;
309 if (NULL != peer_sc->get_handle)
311 GNUNET_STATISTICS_get_cancel (peer_sc->get_handle);
312 peer_sc->get_handle = NULL;
314 GNUNET_STATISTICS_destroy (sh, GNUNET_NO);
315 if (GNUNET_SCHEDULER_NO_TASK != peer_sc->op_done_task_id)
316 GNUNET_SCHEDULER_cancel (peer_sc->op_done_task_id);
317 GNUNET_free (peer_sc);
322 * Function called when get_statistics operation is ready
324 * @param cls the GetStatsContext
327 opstart_get_stats (void *cls)
329 struct GetStatsContext *sc = cls;
330 struct PeerGetStatsContext *peer_sc;
333 LOG_DEBUG ("Starting get_statistics operation\n");
334 sc->ops = GNUNET_malloc (sc->num_peers *
335 sizeof (struct GNUNET_TESTBED_Operation *));
336 for (peer = 0; peer < sc->num_peers; peer++)
338 if (NULL == sc->peers[peer])
343 peer_sc = GNUNET_new (struct PeerGetStatsContext);
345 peer_sc->peer_index = peer;
347 GNUNET_TESTBED_service_connect (sc, sc->peers[peer], "statistics",
348 &service_connect_comp,
358 * Function called when get_statistics operation is cancelled or marked as done
360 * @param cls the GetStatsContext
363 oprelease_get_stats (void *cls)
365 struct GetStatsContext *sc = cls;
368 LOG_DEBUG ("Cleaning up get_statistics operation\n");
369 if (GNUNET_SCHEDULER_NO_TASK != sc->call_completion_task_id)
370 GNUNET_SCHEDULER_cancel (sc->call_completion_task_id);
373 for (peer = 0; peer < sc->num_peers; peer++)
375 if (NULL != sc->ops[peer])
377 GNUNET_TESTBED_operation_done (sc->ops[peer]);
378 sc->ops[peer] = NULL;
381 GNUNET_free (sc->ops);
383 GNUNET_free_non_null (sc->subsystem);
384 GNUNET_free_non_null (sc->name);
387 GNUNET_TESTBED_operation_queue_destroy_empty_ (no_wait_queue))
388 no_wait_queue = NULL;
393 * Convenience method that iterates over all (running) peers
394 * and retrieves all statistics from each peer.
396 * @param num_peers number of peers to iterate over
397 * @param peers array of peers to iterate over
398 * @param subsystem limit to the specified subsystem, NULL for all subsystems
399 * @param name name of the statistic value, NULL for all values
400 * @param proc processing function for each statistic retrieved
401 * @param cont continuation to call once call is completed(?)
402 * @param cls closure to pass to proc and cont
403 * @return operation handle to cancel the operation
405 struct GNUNET_TESTBED_Operation *
406 GNUNET_TESTBED_get_statistics (unsigned int num_peers,
407 struct GNUNET_TESTBED_Peer **peers,
408 const char *subsystem, const char *name,
409 GNUNET_TESTBED_StatisticsIterator proc,
410 GNUNET_TESTBED_OperationCompletionCallback cont,
413 struct GetStatsContext *sc;
415 GNUNET_assert (NULL != proc);
416 GNUNET_assert (NULL != cont);
417 if (NULL == no_wait_queue)
418 no_wait_queue = GNUNET_TESTBED_operation_queue_create_
419 (OPERATION_QUEUE_TYPE_FIXED, UINT_MAX);
420 sc = GNUNET_new (struct GetStatsContext);
422 sc->subsystem = (NULL == subsystem) ? NULL : GNUNET_strdup (subsystem);
423 sc->name = (NULL == name) ? NULL : GNUNET_strdup (name);
427 sc->num_peers = num_peers;
429 GNUNET_TESTBED_operation_create_ (sc, &opstart_get_stats,
430 &oprelease_get_stats);
431 GNUNET_TESTBED_operation_queue_insert_ (no_wait_queue, sc->main_op);
432 GNUNET_TESTBED_operation_begin_wait_ (sc->main_op);
437 /* end of testbed_api_statistics.c */