- warn. Missing peers in the peer array is unusual
[oweals/gnunet.git] / src / testbed / testbed_api_statistics.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, 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_common.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 iterator to call with statistics information
70    */
71   GNUNET_TESTBED_StatisticsIterator proc;
72
73   /**
74    * The callback to call when we are done iterating through all peers'
75    * statistics services
76    */
77   GNUNET_TESTBED_OperationCompletionCallback cont;
78
79   /**
80    * The closure for the above callbacks
81    */
82   void *cb_cls;
83
84   /**
85    * The task for calling the continuation callback
86    */
87   GNUNET_SCHEDULER_TaskIdentifier call_completion_task_id;
88   
89   /**
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
92    */
93   unsigned int num_peers;
94
95   /**
96    * How many peers' statistics have we iterated through
97    */
98   unsigned int num_completed;
99
100 };
101
102
103 /**
104  * Context information with respect to a particular peer
105  */
106 struct PeerGetStatsContext
107 {
108   /**
109    * The GetStatsContext which is associated with this context
110    */
111   struct GetStatsContext *sc;
112
113   /**
114    * The handle from GNUNET_STATISTICS_get()
115    */
116   struct GNUNET_STATISTICS_GetHandle *get_handle;
117
118   /**
119    * Task to mark the statistics service connect operation as done
120    */
121   GNUNET_SCHEDULER_TaskIdentifier op_done_task_id;
122
123   /**
124    * The index of this peer in the peers array of GetStatsContext
125    */
126   unsigned int peer_index;
127 };
128
129
130 /**
131  * A no-wait operation queue
132  */
133 static struct OperationQueue *no_wait_queue;
134
135
136 /**
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.
141  *
142  * @param cls the GetStatsContext
143  * @param tc the scheduler task context
144  */
145 static void
146 call_completion_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 {
148   struct GetStatsContext *sc = cls;
149
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);
154 }
155
156
157 /**
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()
160  *
161  * @param cls the PeerGetStatsContext
162  * @param tc the scheduler task context
163  */
164 static void
165 op_done_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
166 {
167   struct PeerGetStatsContext *peer_sc = cls;
168   struct GetStatsContext *sc;
169   struct GNUNET_TESTBED_Operation **op;
170
171   sc = peer_sc->sc;
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);
176   *op = NULL;
177 }
178
179
180 /**
181  * Continuation called by the "get_all" and "get" functions.
182  *
183  * @param cls the PeerGetStatsContext
184  * @param success GNUNET_OK if statistics were
185  *        successfully obtained, GNUNET_SYSERR if not.
186  */
187 static void
188 iteration_completion_cb (void *cls, int success)
189 {
190   struct PeerGetStatsContext *peer_sc = cls;
191   struct GetStatsContext *sc;
192
193   if (NULL == peer_sc->get_handle)
194   {
195     /* We are being called synchronously in call to GNUNET_STATISTICS_destroy()
196        in statistics_da() after we cancelled the GetHandle */
197     GNUNET_assert (GNUNET_SYSERR == success);
198     return;
199   }
200   GNUNET_break (GNUNET_OK == success);
201   sc = peer_sc->sc;
202   peer_sc->get_handle = NULL; 
203   sc->num_completed++;
204   peer_sc->op_done_task_id = GNUNET_SCHEDULER_add_now (&op_done_task, peer_sc);
205   if (sc->num_completed == sc->num_peers)
206   {
207     LOG_DEBUG ("Scheduling to call iteration completion callback\n");
208     sc->call_completion_task_id =
209         GNUNET_SCHEDULER_add_now (&call_completion_task, sc);
210   }
211 }
212
213
214 /**
215  * Callback function to process statistic values.
216  *
217  * @param cls the PeerGetStatsContext
218  * @param subsystem name of subsystem that created the statistic
219  * @param name the name of the datum
220  * @param value the current value
221  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
222  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
223  */
224 static int
225 iterator_cb (void *cls, const char *subsystem,
226              const char *name, uint64_t value,
227              int is_persistent)
228 {
229   struct PeerGetStatsContext *peer_sc = cls;
230   struct GetStatsContext *sc;
231   struct GNUNET_TESTBED_Peer *peer;
232   int ret;
233
234   sc = peer_sc->sc;
235   peer = sc->peers[peer_sc->peer_index];
236   LOG_DEBUG ("Peer %u: [%s,%s] -> %lu\n", peer_sc->peer_index,
237              subsystem, name, (unsigned long) value);
238   ret = sc->proc (sc->cb_cls, peer,
239                   subsystem, name, value, is_persistent);  
240   if (GNUNET_SYSERR == ret)
241     LOG_DEBUG ("Aborting iteration for peer %u\n", peer_sc->peer_index);
242   return ret;
243 }
244
245
246 /**
247  * Called after opening a connection to the statistics service of a peer
248  *
249  * @param cls the PeerGetStatsContext
250  * @param op the operation that has been finished
251  * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
252  * @param emsg error message in case the operation has failed; will be NULL if
253  *          operation has executed successfully.
254  */
255 static void
256 service_connect_comp (void *cls,
257                       struct GNUNET_TESTBED_Operation *op,
258                       void *ca_result,
259                       const char *emsg)
260 {
261   struct PeerGetStatsContext *peer_sc = cls;
262   struct GNUNET_STATISTICS_Handle *h = ca_result;
263   
264   LOG_DEBUG ("Retrieving statistics of peer %u\n", peer_sc->peer_index);
265   peer_sc->get_handle =
266       GNUNET_STATISTICS_get (h, NULL, NULL,
267                              GNUNET_TIME_UNIT_FOREVER_REL,
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
305   if (NULL != peer_sc->get_handle)
306   {
307     GNUNET_STATISTICS_get_cancel (peer_sc->get_handle);
308     peer_sc->get_handle = NULL;
309   }
310   GNUNET_STATISTICS_destroy ((struct GNUNET_STATISTICS_Handle *) op_result,
311                              GNUNET_NO);
312   if (GNUNET_SCHEDULER_NO_TASK != 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_malloc (sizeof (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 (GNUNET_SCHEDULER_NO_TASK != 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 (sc);
381   if (GNUNET_YES == 
382       GNUNET_TESTBED_operation_queue_destroy_empty_ (no_wait_queue))
383     no_wait_queue = NULL;
384 }
385
386
387 /**
388  * Convenience method that iterates over all (running) peers
389  * and retrieves all statistics from each peer.
390  *
391  * @param num_peers number of peers to iterate over
392  * @param peers array of peers to iterate over
393  * @param proc processing function for each statistic retrieved
394  * @param cont continuation to call once call is completed(?)
395  * @param cls closure to pass to proc and cont
396  * @return operation handle to cancel the operation
397  */
398 struct GNUNET_TESTBED_Operation *
399 GNUNET_TESTBED_get_statistics (unsigned int num_peers,
400                                struct GNUNET_TESTBED_Peer **peers,
401                                GNUNET_TESTBED_StatisticsIterator proc,
402                                GNUNET_TESTBED_OperationCompletionCallback cont,
403                                void *cls)
404 {
405   struct GetStatsContext *sc;
406
407   GNUNET_assert (NULL != proc);
408   GNUNET_assert (NULL != cont);
409   if (NULL == no_wait_queue)
410     no_wait_queue =
411         GNUNET_TESTBED_operation_queue_create_ (UINT_MAX);
412   sc = GNUNET_malloc (sizeof (struct GetStatsContext));
413   sc->peers = peers;
414   sc->proc = proc;
415   sc->cont = cont;
416   sc->cb_cls = cls;
417   sc->num_peers = num_peers;
418   sc->main_op =
419       GNUNET_TESTBED_operation_create_ (sc, &opstart_get_stats,
420                                         &oprelease_get_stats);
421   GNUNET_TESTBED_operation_queue_insert_ (no_wait_queue, sc->main_op);
422   GNUNET_TESTBED_operation_begin_wait_ (sc->main_op);
423   return sc->main_op;
424 }
425
426
427 /* end of testbed_api_statistics.c */