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