testbed_api_statistics.c 12 KB

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