fix pointer indentation
[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      SPDX-License-Identifier: AGPL3.0-or-later
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  * Context information with respect to a particular peer
114  */
115 struct PeerGetStatsContext
116 {
117   /**
118    * The GetStatsContext which is associated with this context
119    */
120   struct GetStatsContext *sc;
121
122   /**
123    * The handle from GNUNET_STATISTICS_get()
124    */
125   struct GNUNET_STATISTICS_GetHandle *get_handle;
126
127   /**
128    * Task to mark the statistics service connect operation as done
129    */
130   struct GNUNET_SCHEDULER_Task *op_done_task_id;
131
132   /**
133    * The index of this peer in the peers array of GetStatsContext
134    */
135   unsigned int peer_index;
136 };
137
138
139 /**
140  * A no-wait operation queue
141  */
142 static struct OperationQueue *no_wait_queue;
143
144
145 /**
146  * Call statistics operation completion.  We call it in a separate task because
147  * the iteration_completion_cb() cannot destroy statistics handle which will be
148  * the case if the user calles GNUNET_TESTBED_operation_done() on the
149  * get_statistics operation.
150  *
151  * @param cls the GetStatsContext
152  */
153 static void
154 call_completion_task (void *cls)
155 {
156   struct GetStatsContext *sc = cls;
157
158   GNUNET_assert (sc->call_completion_task_id != NULL);
159   sc->call_completion_task_id = NULL;
160   LOG_DEBUG ("Calling get_statistics() continuation callback\n");
161   sc->cont (sc->cb_cls, sc->main_op, NULL);
162 }
163
164
165 /**
166  * Task to mark statistics service connect operation as done.  We call it here
167  * as we cannot destroy the statistics handle in iteration_completion_cb()
168  *
169  * @param cls the PeerGetStatsContext
170  */
171 static void
172 op_done_task (void *cls)
173 {
174   struct PeerGetStatsContext *peer_sc = cls;
175   struct GetStatsContext *sc;
176   struct GNUNET_TESTBED_Operation **op;
177
178   sc = peer_sc->sc;
179   peer_sc->op_done_task_id = NULL;
180   op = &sc->ops[peer_sc->peer_index];
181   GNUNET_assert (NULL != *op);
182   GNUNET_TESTBED_operation_done (*op);
183   *op = NULL;
184 }
185
186
187 /**
188  * Continuation called by the "get_all" and "get" functions.
189  *
190  * @param cls the PeerGetStatsContext
191  * @param success GNUNET_OK if statistics were
192  *        successfully obtained, GNUNET_SYSERR if not.
193  */
194 static void
195 iteration_completion_cb (void *cls, int success)
196 {
197   struct PeerGetStatsContext *peer_sc = cls;
198   struct GetStatsContext *sc;
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",
265              peer_sc->peer_index);
266   peer_sc->get_handle =
267     GNUNET_STATISTICS_get (h, peer_sc->sc->subsystem,
268                            peer_sc->sc->name,
269                            &iteration_completion_cb,
270                            iterator_cb, peer_sc);
271 }
272
273
274 /**
275  * Adapter function called to establish a connection to the statistics service
276  * of a peer.
277  *
278  * @param cls the PeerGetStatsContext
279  * @param cfg configuration of the peer to connect to; will be available until
280  *          GNUNET_TESTBED_operation_done() is called on the operation returned
281  *          from GNUNET_TESTBED_service_connect()
282  * @return service handle to return in 'op_result', NULL on error
283  */
284 static void *
285 statistics_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
286 {
287   struct PeerGetStatsContext *peer_sc = cls;
288
289   LOG_DEBUG ("Connecting to statistics service of peer %u\n",
290              peer_sc->peer_index);
291   return GNUNET_STATISTICS_create ("<testbed-api>", cfg);
292 }
293
294
295 /**
296  * Adapter function called to destroy statistics connection
297  *
298  * @param cls the PeerGetStatsContext
299  * @param op_result service handle returned from the connect adapter
300  */
301 static void
302 statistics_da (void *cls, void *op_result)
303 {
304   struct PeerGetStatsContext *peer_sc = cls;
305   struct GNUNET_STATISTICS_Handle *sh = op_result;
306
307   if (NULL != peer_sc->get_handle)
308   {
309     GNUNET_STATISTICS_get_cancel (peer_sc->get_handle);
310     peer_sc->get_handle = NULL;
311   }
312   GNUNET_STATISTICS_destroy (sh, GNUNET_NO);
313   if (NULL != peer_sc->op_done_task_id)
314     GNUNET_SCHEDULER_cancel (peer_sc->op_done_task_id);
315   GNUNET_free (peer_sc);
316 }
317
318
319 /**
320  * Function called when get_statistics operation is ready
321  *
322  * @param cls the GetStatsContext
323  */
324 static void
325 opstart_get_stats (void *cls)
326 {
327   struct GetStatsContext *sc = cls;
328   struct PeerGetStatsContext *peer_sc;
329   unsigned int peer;
330
331   LOG_DEBUG ("Starting get_statistics operation\n");
332   sc->ops = GNUNET_malloc (sc->num_peers
333                            * sizeof(struct GNUNET_TESTBED_Operation *));
334   for (peer = 0; peer < sc->num_peers; peer++)
335   {
336     if (NULL == sc->peers[peer])
337     {
338       GNUNET_break (0);
339       continue;
340     }
341     peer_sc = GNUNET_new (struct PeerGetStatsContext);
342     peer_sc->sc = sc;
343     peer_sc->peer_index = peer;
344     sc->ops[peer] =
345       GNUNET_TESTBED_service_connect (sc, sc->peers[peer], "statistics",
346                                       &service_connect_comp,
347                                       peer_sc,
348                                       &statistics_ca,
349                                       &statistics_da,
350                                       peer_sc);
351   }
352 }
353
354
355 /**
356  * Function called when get_statistics operation is cancelled or marked as done
357  *
358  * @param cls the GetStatsContext
359  */
360 static void
361 oprelease_get_stats (void *cls)
362 {
363   struct GetStatsContext *sc = cls;
364   unsigned int peer;
365
366   LOG_DEBUG ("Cleaning up get_statistics operation\n");
367   if (NULL != sc->call_completion_task_id)
368     GNUNET_SCHEDULER_cancel (sc->call_completion_task_id);
369   if (NULL != sc->ops)
370   {
371     for (peer = 0; peer < sc->num_peers; peer++)
372     {
373       if (NULL != sc->ops[peer])
374       {
375         GNUNET_TESTBED_operation_done (sc->ops[peer]);
376         sc->ops[peer] = NULL;
377       }
378     }
379     GNUNET_free (sc->ops);
380   }
381   GNUNET_free_non_null (sc->subsystem);
382   GNUNET_free_non_null (sc->name);
383   GNUNET_free (sc);
384   if (GNUNET_YES ==
385       GNUNET_TESTBED_operation_queue_destroy_empty_ (no_wait_queue))
386     no_wait_queue = NULL;
387 }
388
389
390 /**
391  * Convenience method that iterates over all (running) peers
392  * and retrieves all statistics from each peer.
393  *
394  * @param num_peers number of peers to iterate over
395  * @param peers array of peers to iterate over
396  * @param subsystem limit to the specified subsystem, NULL for all subsystems
397  * @param name name of the statistic value, NULL for all values
398  * @param proc processing function for each statistic retrieved
399  * @param cont continuation to call once call is completed(?)
400  * @param cls closure to pass to proc and cont
401  * @return operation handle to cancel the operation
402  */
403 struct GNUNET_TESTBED_Operation *
404 GNUNET_TESTBED_get_statistics (unsigned int num_peers,
405                                struct GNUNET_TESTBED_Peer **peers,
406                                const char *subsystem, const char *name,
407                                GNUNET_TESTBED_StatisticsIterator proc,
408                                GNUNET_TESTBED_OperationCompletionCallback cont,
409                                void *cls)
410 {
411   struct GetStatsContext *sc;
412
413   GNUNET_assert (NULL != proc);
414   GNUNET_assert (NULL != cont);
415   if (NULL == no_wait_queue)
416     no_wait_queue = GNUNET_TESTBED_operation_queue_create_
417                       (OPERATION_QUEUE_TYPE_FIXED, UINT_MAX);
418   sc = GNUNET_new (struct GetStatsContext);
419   sc->peers = peers;
420   sc->subsystem = (NULL == subsystem) ? NULL : GNUNET_strdup (subsystem);
421   sc->name = (NULL == name) ? NULL : GNUNET_strdup (name);
422   sc->proc = proc;
423   sc->cont = cont;
424   sc->cb_cls = cls;
425   sc->num_peers = num_peers;
426   sc->main_op =
427     GNUNET_TESTBED_operation_create_ (sc, &opstart_get_stats,
428                                       &oprelease_get_stats);
429   GNUNET_TESTBED_operation_queue_insert_ (no_wait_queue, sc->main_op);
430   GNUNET_TESTBED_operation_begin_wait_ (sc->main_op);
431   return sc->main_op;
432 }
433
434
435 /* end of testbed_api_statistics.c */