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_common.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 iterator to call with statistics information
71 GNUNET_TESTBED_StatisticsIterator proc;
74 * The callback to call when we are done iterating through all peers'
77 GNUNET_TESTBED_OperationCompletionCallback cont;
80 * The closure for the above callbacks
85 * The task for calling the continuation callback
87 GNUNET_SCHEDULER_TaskIdentifier call_completion_task_id;
90 * The number of peers present in the peers array. This number also
91 * represents the number of service connect operations in the ops array
93 unsigned int num_peers;
96 * How many peers' statistics have we iterated through
98 unsigned int num_completed;
104 * Context information with respect to a particular peer
106 struct PeerGetStatsContext
109 * The GetStatsContext which is associated with this context
111 struct GetStatsContext *sc;
114 * The handle from GNUNET_STATISTICS_get()
116 struct GNUNET_STATISTICS_GetHandle *get_handle;
119 * Task to mark the statistics service connect operation as done
121 GNUNET_SCHEDULER_TaskIdentifier op_done_task_id;
124 * The index of this peer in the peers array of GetStatsContext
126 unsigned int peer_index;
131 * A no-wait operation queue
133 static struct OperationQueue *no_wait_queue;
137 * Call statistics operation completion. We call it in a separate task because
138 * the iteration_completion_cb() cannot destroy statistics handle which will be
139 * the case if the user calles GNUNET_TESTBED_operation_done() on the
140 * get_statistics operation.
142 * @param cls the GetStatsContext
143 * @param tc the scheduler task context
146 call_completion_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
148 struct GetStatsContext *sc = cls;
150 GNUNET_assert (sc->call_completion_task_id != GNUNET_SCHEDULER_NO_TASK);
151 sc->call_completion_task_id = GNUNET_SCHEDULER_NO_TASK;
152 LOG_DEBUG ("Calling get_statistics() continuation callback\n");
153 sc->cont (sc->cb_cls, sc->main_op, NULL);
158 * Task to mark statistics service connect operation as done. We call it here
159 * as we cannot destroy the statistics handle in iteration_completion_cb()
161 * @param cls the PeerGetStatsContext
162 * @param tc the scheduler task context
165 op_done_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
167 struct PeerGetStatsContext *peer_sc = cls;
168 struct GetStatsContext *sc;
169 struct GNUNET_TESTBED_Operation **op;
172 peer_sc->op_done_task_id = GNUNET_SCHEDULER_NO_TASK;
173 op = &sc->ops[peer_sc->peer_index];
174 GNUNET_assert (NULL != *op);
175 GNUNET_TESTBED_operation_done (*op);
181 * Continuation called by the "get_all" and "get" functions.
183 * @param cls the PeerGetStatsContext
184 * @param success GNUNET_OK if statistics were
185 * successfully obtained, GNUNET_SYSERR if not.
188 iteration_completion_cb (void *cls, int success)
190 struct PeerGetStatsContext *peer_sc = cls;
191 struct GetStatsContext *sc;
193 GNUNET_break (GNUNET_OK == success);
195 peer_sc->get_handle = NULL;
197 peer_sc->op_done_task_id = GNUNET_SCHEDULER_add_now (&op_done_task, peer_sc);
198 if (sc->num_completed == sc->num_peers)
200 LOG_DEBUG ("Scheduling to call iteration completion callback\n");
201 sc->call_completion_task_id =
202 GNUNET_SCHEDULER_add_now (&call_completion_task, sc);
208 * Callback function to process statistic values.
210 * @param cls the PeerGetStatsContext
211 * @param subsystem name of subsystem that created the statistic
212 * @param name the name of the datum
213 * @param value the current value
214 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
215 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
218 iterator_cb (void *cls, const char *subsystem,
219 const char *name, uint64_t value,
222 struct PeerGetStatsContext *peer_sc = cls;
223 struct GetStatsContext *sc;
224 struct GNUNET_TESTBED_Peer *peer;
228 peer = sc->peers[peer_sc->peer_index];
229 LOG_DEBUG ("Peer %u: [%s,%s] -> %lu\n", peer_sc->peer_index,
230 subsystem, name, (unsigned long) value);
231 ret = sc->proc (sc->cb_cls, peer,
232 subsystem, name, value, is_persistent);
233 if (GNUNET_SYSERR == ret)
234 LOG_DEBUG ("Aborting iteration for peer %u\n", peer_sc->peer_index);
240 * Called after opening a connection to the statistics service of a peer
242 * @param cls the PeerGetStatsContext
243 * @param op the operation that has been finished
244 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
245 * @param emsg error message in case the operation has failed; will be NULL if
246 * operation has executed successfully.
249 service_connect_comp (void *cls,
250 struct GNUNET_TESTBED_Operation *op,
254 struct PeerGetStatsContext *peer_sc = cls;
255 struct GNUNET_STATISTICS_Handle *h = ca_result;
257 LOG_DEBUG ("Retrieving statistics of peer %u\n", peer_sc->peer_index);
258 peer_sc->get_handle =
259 GNUNET_STATISTICS_get (h, NULL, NULL,
260 GNUNET_TIME_UNIT_FOREVER_REL,
261 &iteration_completion_cb,
262 iterator_cb, peer_sc);
267 * Adapter function called to establish a connection to the statistics service
270 * @param cls the PeerGetStatsContext
271 * @param cfg configuration of the peer to connect to; will be available until
272 * GNUNET_TESTBED_operation_done() is called on the operation returned
273 * from GNUNET_TESTBED_service_connect()
274 * @return service handle to return in 'op_result', NULL on error
277 statistics_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
279 struct PeerGetStatsContext *peer_sc = cls;
281 LOG_DEBUG ("Connecting to statistics service of peer %u\n",
282 peer_sc->peer_index);
283 return GNUNET_STATISTICS_create ("<testbed-api>", cfg);
288 * Adapter function called to destroy statistics connection
290 * @param cls the PeerGetStatsContext
291 * @param op_result service handle returned from the connect adapter
294 statistics_da (void *cls, void *op_result)
296 struct PeerGetStatsContext *peer_sc = cls;
297 struct GNUNET_STATISTICS_Handle *sh = op_result;
299 if (NULL != peer_sc->get_handle)
301 GNUNET_STATISTICS_get_cancel (peer_sc->get_handle);
302 peer_sc->get_handle = NULL;
304 GNUNET_STATISTICS_destroy (sh, GNUNET_NO);
305 if (GNUNET_SCHEDULER_NO_TASK != peer_sc->op_done_task_id)
306 GNUNET_SCHEDULER_cancel (peer_sc->op_done_task_id);
307 GNUNET_free (peer_sc);
312 * Function called when get_statistics operation is ready
314 * @param cls the GetStatsContext
317 opstart_get_stats (void *cls)
319 struct GetStatsContext *sc = cls;
320 struct PeerGetStatsContext *peer_sc;
323 LOG_DEBUG ("Starting get_statistics operation\n");
324 sc->ops = GNUNET_malloc (sc->num_peers *
325 sizeof (struct GNUNET_TESTBED_Operation *));
326 for (peer = 0; peer < sc->num_peers; peer++)
328 if (NULL == sc->peers[peer])
333 peer_sc = GNUNET_malloc (sizeof (struct PeerGetStatsContext));
335 peer_sc->peer_index = peer;
337 GNUNET_TESTBED_service_connect (sc, sc->peers[peer], "statistics",
338 &service_connect_comp,
348 * Function called when get_statistics operation is cancelled or marked as done
350 * @param cls the GetStatsContext
353 oprelease_get_stats (void *cls)
355 struct GetStatsContext *sc = cls;
358 LOG_DEBUG ("Cleaning up get_statistics operation\n");
359 if (GNUNET_SCHEDULER_NO_TASK != sc->call_completion_task_id)
360 GNUNET_SCHEDULER_cancel (sc->call_completion_task_id);
363 for (peer = 0; peer < sc->num_peers; peer++)
365 if (NULL != sc->ops[peer])
367 GNUNET_TESTBED_operation_done (sc->ops[peer]);
368 sc->ops[peer] = NULL;
371 GNUNET_free (sc->ops);
375 GNUNET_TESTBED_operation_queue_destroy_empty_ (no_wait_queue))
376 no_wait_queue = NULL;
381 * Convenience method that iterates over all (running) peers
382 * and retrieves all statistics from each peer.
384 * @param num_peers number of peers to iterate over
385 * @param peers array of peers to iterate over
386 * @param proc processing function for each statistic retrieved
387 * @param cont continuation to call once call is completed(?)
388 * @param cls closure to pass to proc and cont
389 * @return operation handle to cancel the operation
391 struct GNUNET_TESTBED_Operation *
392 GNUNET_TESTBED_get_statistics (unsigned int num_peers,
393 struct GNUNET_TESTBED_Peer **peers,
394 GNUNET_TESTBED_StatisticsIterator proc,
395 GNUNET_TESTBED_OperationCompletionCallback cont,
398 struct GetStatsContext *sc;
400 GNUNET_assert (NULL != proc);
401 GNUNET_assert (NULL != cont);
402 if (NULL == no_wait_queue)
404 GNUNET_TESTBED_operation_queue_create_ (UINT_MAX);
405 sc = GNUNET_malloc (sizeof (struct GetStatsContext));
410 sc->num_peers = num_peers;
412 GNUNET_TESTBED_operation_create_ (sc, &opstart_get_stats,
413 &oprelease_get_stats);
414 GNUNET_TESTBED_operation_queue_insert_ (no_wait_queue, sc->main_op);
415 GNUNET_TESTBED_operation_begin_wait_ (sc->main_op);
420 /* end of testbed_api_statistics.c */