guix-env: some update.
[oweals/gnunet.git] / src / testbed / testbed_api_statistics.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 GNUnet e.V.
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18       Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @file testbed/testbed_api_statistics.c
23  * @brief high-level statistics function
24  * @author Christian Grothoff
25  * @author Sree Harsha Totakura
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30
31 #include "testbed_api_operations.h"
32
33
34 /**
35  * Generic logging shorthand
36  */
37 #define LOG(kind,...)                           \
38   GNUNET_log_from (kind, "testbed-api-statistics", __VA_ARGS__)
39
40 /**
41  * Debug logging shorthand
42  */
43 #define LOG_DEBUG(...)                          \
44   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
45
46
47 /**
48  * Context information for use in GNUNET_TESTBED_get_statistics()
49  */
50 struct GetStatsContext
51 {
52   /**
53    * The main operation we generate while creating this context
54    */
55   struct GNUNET_TESTBED_Operation *main_op;
56
57   /**
58    * The service connect operations we create to open connection to the
59    * statistics service of each given peer
60    */
61   struct  GNUNET_TESTBED_Operation **ops;
62
63   /**
64    * The array of peers whose statistics services are to be accessed
65    */
66   struct GNUNET_TESTBED_Peer **peers;
67
68   /**
69    * The subsystem of peers for which statistics are requested
70    */
71   char *subsystem;
72
73   /**
74    * The particular statistics value of interest
75    */
76   char *name;
77
78   /**
79    * The iterator to call with statistics information
80    */
81   GNUNET_TESTBED_StatisticsIterator proc;
82
83   /**
84    * The callback to call when we are done iterating through all peers'
85    * statistics services
86    */
87   GNUNET_TESTBED_OperationCompletionCallback cont;
88
89   /**
90    * The closure for the above callbacks
91    */
92   void *cb_cls;
93
94   /**
95    * The task for calling the continuation callback
96    */
97   struct GNUNET_SCHEDULER_Task * call_completion_task_id;
98
99   /**
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
102    */
103   unsigned int num_peers;
104
105   /**
106    * How many peers' statistics have we iterated through
107    */
108   unsigned int num_completed;
109
110 };
111
112
113 /**
114  * Context information with respect to a particular peer
115  */
116 struct PeerGetStatsContext
117 {
118   /**
119    * The GetStatsContext which is associated with this context
120    */
121   struct GetStatsContext *sc;
122
123   /**
124    * The handle from GNUNET_STATISTICS_get()
125    */
126   struct GNUNET_STATISTICS_GetHandle *get_handle;
127
128   /**
129    * Task to mark the statistics service connect operation as done
130    */
131   struct GNUNET_SCHEDULER_Task * op_done_task_id;
132
133   /**
134    * The index of this peer in the peers array of GetStatsContext
135    */
136   unsigned int peer_index;
137 };
138
139
140 /**
141  * A no-wait operation queue
142  */
143 static struct OperationQueue *no_wait_queue;
144
145
146 /**
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.
151  *
152  * @param cls the GetStatsContext
153  */
154 static void
155 call_completion_task (void *cls)
156 {
157   struct GetStatsContext *sc = cls;
158
159   GNUNET_assert (sc->call_completion_task_id != NULL);
160   sc->call_completion_task_id = NULL;
161   LOG_DEBUG ("Calling get_statistics() continuation callback\n");
162   sc->cont (sc->cb_cls, sc->main_op, NULL);
163 }
164
165
166 /**
167  * Task to mark statistics service connect operation as done.  We call it here
168  * as we cannot destroy the statistics handle in iteration_completion_cb()
169  *
170  * @param cls the PeerGetStatsContext
171  */
172 static void
173 op_done_task (void *cls)
174 {
175   struct PeerGetStatsContext *peer_sc = cls;
176   struct GetStatsContext *sc;
177   struct GNUNET_TESTBED_Operation **op;
178
179   sc = peer_sc->sc;
180   peer_sc->op_done_task_id = NULL;
181   op = &sc->ops[peer_sc->peer_index];
182   GNUNET_assert (NULL != *op);
183   GNUNET_TESTBED_operation_done (*op);
184   *op = NULL;
185 }
186
187
188 /**
189  * Continuation called by the "get_all" and "get" functions.
190  *
191  * @param cls the PeerGetStatsContext
192  * @param success GNUNET_OK if statistics were
193  *        successfully obtained, GNUNET_SYSERR if not.
194  */
195 static void
196 iteration_completion_cb (void *cls, int success)
197 {
198   struct PeerGetStatsContext *peer_sc = cls;
199   struct GetStatsContext *sc;
200
201   GNUNET_break (GNUNET_OK == success);
202   sc = peer_sc->sc;
203   peer_sc->get_handle = NULL;
204   sc->num_completed++;
205   peer_sc->op_done_task_id = GNUNET_SCHEDULER_add_now (&op_done_task, peer_sc);
206   if (sc->num_completed == sc->num_peers)
207   {
208     LOG_DEBUG ("Scheduling to call iteration completion callback\n");
209     sc->call_completion_task_id =
210         GNUNET_SCHEDULER_add_now (&call_completion_task, sc);
211   }
212 }
213
214
215 /**
216  * Callback function to process statistic values.
217  *
218  * @param cls the PeerGetStatsContext
219  * @param subsystem name of subsystem that created the statistic
220  * @param name the name of the datum
221  * @param value the current value
222  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
223  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
224  */
225 static int
226 iterator_cb (void *cls, const char *subsystem,
227              const char *name, uint64_t value,
228              int is_persistent)
229 {
230   struct PeerGetStatsContext *peer_sc = cls;
231   struct GetStatsContext *sc;
232   struct GNUNET_TESTBED_Peer *peer;
233   int ret;
234
235   sc = peer_sc->sc;
236   peer = sc->peers[peer_sc->peer_index];
237   LOG_DEBUG ("Peer %u: [%s,%s] -> %lu\n", peer_sc->peer_index,
238              subsystem, name, (unsigned long) value);
239   ret = sc->proc (sc->cb_cls, peer,
240                   subsystem, name, value, is_persistent);
241   if (GNUNET_SYSERR == ret)
242     LOG_DEBUG ("Aborting iteration for peer %u\n", peer_sc->peer_index);
243   return ret;
244 }
245
246
247 /**
248  * Called after opening a connection to the statistics service of a peer
249  *
250  * @param cls the PeerGetStatsContext
251  * @param op the operation that has been finished
252  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
253  * @param emsg error message in case the operation has failed; will be NULL if
254  *          operation has executed successfully.
255  */
256 static void
257 service_connect_comp (void *cls,
258                       struct GNUNET_TESTBED_Operation *op,
259                       void *ca_result,
260                       const char *emsg)
261 {
262   struct PeerGetStatsContext *peer_sc = cls;
263   struct GNUNET_STATISTICS_Handle *h = ca_result;
264
265   LOG_DEBUG ("Retrieving statistics of peer %u\n",
266              peer_sc->peer_index);
267   peer_sc->get_handle =
268       GNUNET_STATISTICS_get (h, peer_sc->sc->subsystem,
269                              peer_sc->sc->name,
270                              &iteration_completion_cb,
271                              iterator_cb, peer_sc);
272 }
273
274
275 /**
276  * Adapter function called to establish a connection to the statistics service
277  * of a peer.
278  *
279  * @param cls the PeerGetStatsContext
280  * @param cfg configuration of the peer to connect to; will be available until
281  *          GNUNET_TESTBED_operation_done() is called on the operation returned
282  *          from GNUNET_TESTBED_service_connect()
283  * @return service handle to return in 'op_result', NULL on error
284  */
285 static void *
286 statistics_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
287 {
288   struct PeerGetStatsContext *peer_sc = cls;
289
290   LOG_DEBUG ("Connecting to statistics service of peer %u\n",
291              peer_sc->peer_index);
292   return GNUNET_STATISTICS_create ("<testbed-api>", cfg);
293 }
294
295
296 /**
297  * Adapter function called to destroy statistics connection
298  *
299  * @param cls the PeerGetStatsContext
300  * @param op_result service handle returned from the connect adapter
301  */
302 static void
303 statistics_da (void *cls, void *op_result)
304 {
305   struct PeerGetStatsContext *peer_sc = cls;
306   struct GNUNET_STATISTICS_Handle *sh = op_result;
307
308   if (NULL != peer_sc->get_handle)
309   {
310     GNUNET_STATISTICS_get_cancel (peer_sc->get_handle);
311     peer_sc->get_handle = NULL;
312   }
313   GNUNET_STATISTICS_destroy (sh, GNUNET_NO);
314   if (NULL != peer_sc->op_done_task_id)
315     GNUNET_SCHEDULER_cancel (peer_sc->op_done_task_id);
316   GNUNET_free (peer_sc);
317 }
318
319
320 /**
321  * Function called when get_statistics operation is ready
322  *
323  * @param cls the GetStatsContext
324  */
325 static void
326 opstart_get_stats (void *cls)
327 {
328   struct GetStatsContext *sc = cls;
329   struct PeerGetStatsContext *peer_sc;
330   unsigned int peer;
331
332   LOG_DEBUG ("Starting get_statistics operation\n");
333   sc->ops = GNUNET_malloc (sc->num_peers *
334                            sizeof (struct GNUNET_TESTBED_Operation *));
335   for (peer = 0; peer < sc->num_peers; peer++)
336   {
337     if (NULL == sc->peers[peer])
338     {
339       GNUNET_break (0);
340       continue;
341     }
342     peer_sc = GNUNET_new (struct PeerGetStatsContext);
343     peer_sc->sc = sc;
344     peer_sc->peer_index = peer;
345     sc->ops[peer] =
346         GNUNET_TESTBED_service_connect (sc, sc->peers[peer], "statistics",
347                                         &service_connect_comp,
348                                         peer_sc,
349                                         &statistics_ca,
350                                         &statistics_da,
351                                         peer_sc);
352   }
353 }
354
355
356 /**
357  * Function called when get_statistics operation is cancelled or marked as done
358  *
359  * @param cls the GetStatsContext
360  */
361 static void
362 oprelease_get_stats (void *cls)
363 {
364   struct GetStatsContext *sc = cls;
365   unsigned int peer;
366
367   LOG_DEBUG ("Cleaning up get_statistics operation\n");
368   if (NULL != sc->call_completion_task_id)
369     GNUNET_SCHEDULER_cancel (sc->call_completion_task_id);
370   if (NULL != sc->ops)
371   {
372     for (peer = 0; peer < sc->num_peers; peer++)
373     {
374       if (NULL != sc->ops[peer])
375       {
376         GNUNET_TESTBED_operation_done (sc->ops[peer]);
377         sc->ops[peer] = NULL;
378       }
379     }
380     GNUNET_free (sc->ops);
381   }
382   GNUNET_free_non_null (sc->subsystem);
383   GNUNET_free_non_null (sc->name);
384   GNUNET_free (sc);
385   if (GNUNET_YES ==
386       GNUNET_TESTBED_operation_queue_destroy_empty_ (no_wait_queue))
387     no_wait_queue = NULL;
388 }
389
390
391 /**
392  * Convenience method that iterates over all (running) peers
393  * and retrieves all statistics from each peer.
394  *
395  * @param num_peers number of peers to iterate over
396  * @param peers array of peers to iterate over
397  * @param subsystem limit to the specified subsystem, NULL for all subsystems
398  * @param name name of the statistic value, NULL for all values
399  * @param proc processing function for each statistic retrieved
400  * @param cont continuation to call once call is completed(?)
401  * @param cls closure to pass to proc and cont
402  * @return operation handle to cancel the operation
403  */
404 struct GNUNET_TESTBED_Operation *
405 GNUNET_TESTBED_get_statistics (unsigned int num_peers,
406                                struct GNUNET_TESTBED_Peer **peers,
407                                const char *subsystem, const char *name,
408                                GNUNET_TESTBED_StatisticsIterator proc,
409                                GNUNET_TESTBED_OperationCompletionCallback cont,
410                                void *cls)
411 {
412   struct GetStatsContext *sc;
413
414   GNUNET_assert (NULL != proc);
415   GNUNET_assert (NULL != cont);
416   if (NULL == no_wait_queue)
417     no_wait_queue = GNUNET_TESTBED_operation_queue_create_
418         (OPERATION_QUEUE_TYPE_FIXED, UINT_MAX);
419   sc = GNUNET_new (struct GetStatsContext);
420   sc->peers = peers;
421   sc->subsystem = (NULL == subsystem) ? NULL : GNUNET_strdup (subsystem);
422   sc->name = (NULL == name) ? NULL : GNUNET_strdup (name);
423   sc->proc = proc;
424   sc->cont = cont;
425   sc->cb_cls = cls;
426   sc->num_peers = num_peers;
427   sc->main_op =
428       GNUNET_TESTBED_operation_create_ (sc, &opstart_get_stats,
429                                         &oprelease_get_stats);
430   GNUNET_TESTBED_operation_queue_insert_ (no_wait_queue, sc->main_op);
431   GNUNET_TESTBED_operation_begin_wait_ (sc->main_op);
432   return sc->main_op;
433 }
434
435
436 /* end of testbed_api_statistics.c */