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