2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file statistics/statistics_api.c
23 * @brief API of the statistics service
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_statistics_service.h"
31 #include "statistics.h"
34 * How long do we wait until a statistics request for setting
35 * a value times out? (The update will be lost if the
36 * service does not react within this timeframe).
38 #define SET_TRANSMIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
40 #define LOG(kind,...) GNUNET_log_from (kind, "statistics-api",__VA_ARGS__)
70 * Entry kept for each value we are watching.
72 struct GNUNET_STATISTICS_WatchEntry
76 * What subsystem is this action about? (never NULL)
81 * What value is this action about? (never NULL)
88 GNUNET_STATISTICS_Iterator proc;
99 * Linked list of things we still need to do.
101 struct GNUNET_STATISTICS_GetHandle
105 * This is a doubly linked list.
107 struct GNUNET_STATISTICS_GetHandle *next;
110 * This is a doubly linked list.
112 struct GNUNET_STATISTICS_GetHandle *prev;
115 * Main statistics handle.
117 struct GNUNET_STATISTICS_Handle *sh;
120 * What subsystem is this action about? (can be NULL)
125 * What value is this action about? (can be NULL)
130 * Continuation to call once action is complete.
132 GNUNET_STATISTICS_Callback cont;
135 * Function to call (for GET actions only).
137 GNUNET_STATISTICS_Iterator proc;
140 * Closure for proc and cont.
145 * Timeout for this action.
147 struct GNUNET_TIME_Absolute timeout;
150 * Task run on timeout.
152 struct GNUNET_SCHEDULER_Task * timeout_task;
160 * Flag for SET/UPDATE actions.
165 * Has the current iteration been aborted; for GET actions.
170 * Is this a GET, SET, UPDATE or WATCH?
172 enum ActionType type;
175 * Size of the message that we will be transmitting.
183 * Handle for the service.
185 struct GNUNET_STATISTICS_Handle
188 * Name of our subsystem.
193 * Configuration to use.
195 const struct GNUNET_CONFIGURATION_Handle *cfg;
198 * Socket (if available).
200 struct GNUNET_CLIENT_Connection *client;
203 * Currently pending transmission request.
205 struct GNUNET_CLIENT_TransmitHandle *th;
208 * Head of the linked list of pending actions (first action
211 struct GNUNET_STATISTICS_GetHandle *action_head;
214 * Tail of the linked list of actions (for fast append).
216 struct GNUNET_STATISTICS_GetHandle *action_tail;
219 * Action we are currently busy with (action request has been
220 * transmitted, we're now receiving the response from the
223 struct GNUNET_STATISTICS_GetHandle *current;
226 * Array of watch entries.
228 struct GNUNET_STATISTICS_WatchEntry **watches;
231 * Task doing exponential back-off trying to reconnect.
233 struct GNUNET_SCHEDULER_Task * backoff_task;
236 * Time for next connect retry.
238 struct GNUNET_TIME_Relative backoff;
241 * Maximum heap size observed so far (if available).
243 uint64_t peak_heap_size;
246 * Maximum resident set side observed so far (if available).
251 * Size of the 'watches' array.
253 unsigned int watches_size;
256 * Should this handle auto-destruct once all actions have
262 * Are we currently receiving from the service?
270 * Obtain statistics about this process's memory consumption and
271 * report those as well (if they changed).
274 update_memory_statistics (struct GNUNET_STATISTICS_Handle *h)
276 #if ENABLE_HEAP_STATISTICS
277 uint64_t current_heap_size = 0;
278 uint64_t current_rss = 0;
280 if (GNUNET_NO != h->do_destroy)
287 current_heap_size = mi.uordblks + mi.fordblks;
294 if (0 == getrusage (RUSAGE_SELF, &ru))
296 current_rss = 1024LL * ru.ru_maxrss;
300 if (current_heap_size > h->peak_heap_size)
302 h->peak_heap_size = current_heap_size;
303 GNUNET_STATISTICS_set (h, "# peak heap size", current_heap_size, GNUNET_NO);
305 if (current_rss > h->peak_rss)
307 h->peak_rss = current_rss;
308 GNUNET_STATISTICS_set (h, "# peak resident set size", current_rss, GNUNET_NO);
315 * Schedule the next action to be performed.
317 * @param h statistics handle to reconnect
320 schedule_action (struct GNUNET_STATISTICS_Handle *h);
324 * Transmit request to service that we want to watch
325 * the development of a particular value.
327 * @param h statistics handle
328 * @param watch watch entry of the value to watch
331 schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
332 struct GNUNET_STATISTICS_WatchEntry *watch)
334 struct GNUNET_STATISTICS_GetHandle *ai;
339 slen = strlen (watch->subsystem) + 1;
340 nlen = strlen (watch->name) + 1;
341 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
342 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
347 ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle);
349 ai->subsystem = GNUNET_strdup (watch->subsystem);
350 ai->name = GNUNET_strdup (watch->name);
351 ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
353 ai->type = ACTION_WATCH;
354 ai->proc = watch->proc;
355 ai->cls = watch->proc_cls;
356 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
363 * Free memory associated with the given action item.
365 * @param gh action item to free
368 free_action_item (struct GNUNET_STATISTICS_GetHandle *gh)
370 if (NULL != gh->timeout_task)
372 GNUNET_SCHEDULER_cancel (gh->timeout_task);
373 gh->timeout_task = NULL;
375 GNUNET_free_non_null (gh->subsystem);
376 GNUNET_free_non_null (gh->name);
382 * Disconnect from the statistics service.
384 * @param h statistics handle to disconnect from
387 do_disconnect (struct GNUNET_STATISTICS_Handle *h)
389 struct GNUNET_STATISTICS_GetHandle *c;
393 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
396 h->receiving = GNUNET_NO;
397 if (NULL != (c = h->current))
400 if ( (NULL != c->cont) &&
401 (GNUNET_YES != c->aborted) )
403 c->cont (c->cls, GNUNET_SYSERR);
406 free_action_item (c);
408 if (NULL != h->client)
410 GNUNET_CLIENT_disconnect (h->client);
417 * Try to (re)connect to the statistics service.
419 * @param h statistics handle to reconnect
420 * @return #GNUNET_YES on success, #GNUNET_NO on failure.
423 try_connect (struct GNUNET_STATISTICS_Handle *h)
425 struct GNUNET_STATISTICS_GetHandle *gh;
426 struct GNUNET_STATISTICS_GetHandle *gn;
429 if (NULL != h->backoff_task)
431 if (NULL != h->client)
433 h->client = GNUNET_CLIENT_connect ("statistics", h->cfg);
434 if (NULL != h->client)
437 while (NULL != (gh = gn))
440 if (gh->type == ACTION_WATCH)
442 GNUNET_CONTAINER_DLL_remove (h->action_head,
445 free_action_item (gh);
448 for (i = 0; i < h->watches_size; i++)
450 if (NULL != h->watches[i])
451 schedule_watch_request (h, h->watches[i]);
455 LOG (GNUNET_ERROR_TYPE_DEBUG,
456 "Failed to connect to statistics service!\n");
462 * We've waited long enough, reconnect now.
464 * @param cls the `struct GNUNET_STATISTICS_Handle` to reconnect
465 * @param tc scheduler context (unused)
468 reconnect_task (void *cls,
469 const struct GNUNET_SCHEDULER_TaskContext *tc)
471 struct GNUNET_STATISTICS_Handle *h = cls;
473 h->backoff_task = NULL;
479 * Task used by 'reconnect_later' to shutdown the handle
481 * @param cls the statistics handle
482 * @param tc scheduler context
485 do_destroy (void *cls,
486 const struct GNUNET_SCHEDULER_TaskContext *tc)
488 struct GNUNET_STATISTICS_Handle *h = cls;
490 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
495 * Reconnect at a later time, respecting back-off.
497 * @param h statistics handle
500 reconnect_later (struct GNUNET_STATISTICS_Handle *h)
503 struct GNUNET_STATISTICS_GetHandle *gh;
505 GNUNET_assert (NULL == h->backoff_task);
506 if (GNUNET_YES == h->do_destroy)
508 /* So we are shutting down and the service is not reachable.
509 * Chances are that it's down for good and we are not going to connect to
511 * Give up and don't sync the rest of the data.
514 for (gh = h->action_head; NULL != gh; gh = gh->next)
515 if ( (gh->make_persistent) && (ACTION_SET == gh->type) )
517 if (GNUNET_YES == loss)
518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
519 _("Could not save some persistent statistics\n"));
520 h->do_destroy = GNUNET_NO;
521 GNUNET_SCHEDULER_add_continuation (&do_destroy, h,
522 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
526 GNUNET_SCHEDULER_add_delayed (h->backoff, &reconnect_task, h);
527 h->backoff = GNUNET_TIME_STD_BACKOFF (h->backoff);
532 * Process a #GNUNET_MESSAGE_TYPE_STATISTICS_VALUE message.
534 * @param h statistics handle
535 * @param msg message received from the service, never NULL
536 * @return #GNUNET_OK if the message was well-formed
539 process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
540 const struct GNUNET_MessageHeader *msg)
544 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
547 if (h->current->aborted)
549 LOG (GNUNET_ERROR_TYPE_DEBUG,
550 "Iteration was aborted, ignoring VALUE\n");
551 return GNUNET_OK; /* don't bother */
553 size = ntohs (msg->size);
554 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
557 return GNUNET_SYSERR;
559 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
560 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
562 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], size, 2,
566 return GNUNET_SYSERR;
568 LOG (GNUNET_ERROR_TYPE_DEBUG,
569 "Received valid statistic on `%s:%s': %llu\n",
571 GNUNET_ntohll (smsg->value));
573 h->current->proc (h->current->cls, service, name,
574 GNUNET_ntohll (smsg->value),
576 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
578 LOG (GNUNET_ERROR_TYPE_DEBUG,
579 "Processing of remaining statistics aborted by client.\n");
580 h->current->aborted = GNUNET_YES;
582 LOG (GNUNET_ERROR_TYPE_DEBUG,
583 "VALUE processed successfully\n");
589 * We have received a watch value from the service. Process it.
591 * @param h statistics handle
592 * @param msg the watch value message
593 * @return #GNUNET_OK if the message was well-formed, #GNUNET_SYSERR if not,
594 * #GNUNET_NO if this watch has been cancelled
597 process_watch_value (struct GNUNET_STATISTICS_Handle *h,
598 const struct GNUNET_MessageHeader *msg)
600 const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
601 struct GNUNET_STATISTICS_WatchEntry *w;
604 if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) != ntohs (msg->size))
607 return GNUNET_SYSERR;
609 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
610 GNUNET_break (0 == ntohl (wvm->reserved));
611 wid = ntohl (wvm->wid);
612 if (wid >= h->watches_size)
615 return GNUNET_SYSERR;
620 (void) w->proc (w->proc_cls, w->subsystem, w->name,
621 GNUNET_ntohll (wvm->value),
622 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
628 * Task used to destroy the statistics handle.
630 * @param cls the `struct GNUNET_STATISTICS_Handle`
631 * @param tc the scheduler context
634 destroy_task (void *cls,
635 const struct GNUNET_SCHEDULER_TaskContext *tc)
637 struct GNUNET_STATISTICS_Handle *h = cls;
639 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
644 * Function called with messages from stats service.
647 * @param msg message received, NULL on timeout or fatal error
650 receive_stats (void *cls,
651 const struct GNUNET_MessageHeader *msg)
653 struct GNUNET_STATISTICS_Handle *h = cls;
654 struct GNUNET_STATISTICS_GetHandle *c;
659 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
660 "Error receiving statistics from service, is the service running?\n");
665 switch (ntohs (msg->type))
667 case GNUNET_MESSAGE_TYPE_TEST:
668 if (GNUNET_SYSERR != h->do_destroy)
670 /* not in shutdown, why do we get 'TEST'? */
676 h->do_destroy = GNUNET_NO;
677 GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
678 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
680 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
681 LOG (GNUNET_ERROR_TYPE_DEBUG,
682 "Received end of statistics marker\n");
683 if (NULL == (c = h->current))
690 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
691 if (h->watches_size > 0)
693 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
694 GNUNET_TIME_UNIT_FOREVER_REL);
698 h->receiving = GNUNET_NO;
704 c->cont (c->cls, GNUNET_OK);
707 free_action_item (c);
709 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
710 if (GNUNET_OK != process_statistics_value_message (h, msg))
716 /* finally, look for more! */
717 LOG (GNUNET_ERROR_TYPE_DEBUG,
718 "Processing VALUE done, now reading more\n");
719 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
720 GNUNET_TIME_absolute_get_remaining (h->
722 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
724 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
726 (ret = process_watch_value (h, msg)))
729 if (GNUNET_NO == ret)
730 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
734 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
735 GNUNET_assert (h->watches_size > 0);
736 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
737 GNUNET_TIME_UNIT_FOREVER_REL);
749 * Transmit a GET request (and if successful, start to receive
752 * @param handle statistics handle
753 * @param size how many bytes can we write to @a buf
754 * @param buf where to write requests to the service
755 * @return number of bytes written to @a buf
758 transmit_get (struct GNUNET_STATISTICS_Handle *handle,
762 struct GNUNET_STATISTICS_GetHandle *c;
763 struct GNUNET_MessageHeader *hdr;
768 GNUNET_assert (NULL != (c = handle->current));
771 /* timeout / error */
772 LOG (GNUNET_ERROR_TYPE_DEBUG,
773 "Transmission of request for statistics failed!\n");
774 do_disconnect (handle);
775 reconnect_later (handle);
778 slen1 = strlen (c->subsystem) + 1;
779 slen2 = strlen (c->name) + 1;
780 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
781 GNUNET_assert (msize <= size);
782 hdr = (struct GNUNET_MessageHeader *) buf;
783 hdr->size = htons (msize);
784 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
785 GNUNET_assert (slen1 + slen2 ==
786 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
789 if (GNUNET_YES != handle->receiving)
791 LOG (GNUNET_ERROR_TYPE_DEBUG,
792 "Transmission of GET done, now reading response\n");
793 handle->receiving = GNUNET_YES;
794 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
795 GNUNET_TIME_absolute_get_remaining (c->timeout));
802 * Transmit a WATCH request (and if successful, start to receive
805 * @param handle statistics handle
806 * @param size how many bytes can we write to @a buf
807 * @param buf where to write requests to the service
808 * @return number of bytes written to @a buf
811 transmit_watch (struct GNUNET_STATISTICS_Handle *handle,
815 struct GNUNET_MessageHeader *hdr;
822 /* timeout / error */
823 LOG (GNUNET_ERROR_TYPE_DEBUG,
824 "Transmission of request for statistics failed!\n");
825 do_disconnect (handle);
826 reconnect_later (handle);
829 LOG (GNUNET_ERROR_TYPE_DEBUG,
830 "Transmitting watch request for `%s'\n",
831 handle->current->name);
832 slen1 = strlen (handle->current->subsystem) + 1;
833 slen2 = strlen (handle->current->name) + 1;
834 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
835 GNUNET_assert (msize <= size);
836 hdr = (struct GNUNET_MessageHeader *) buf;
837 hdr->size = htons (msize);
838 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
839 GNUNET_assert (slen1 + slen2 ==
840 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
841 handle->current->subsystem,
842 handle->current->name));
843 if (GNUNET_YES != handle->receiving)
845 handle->receiving = GNUNET_YES;
846 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
847 GNUNET_TIME_UNIT_FOREVER_REL);
849 GNUNET_assert (NULL == handle->current->cont);
850 free_action_item (handle->current);
851 handle->current = NULL;
857 * Transmit a SET/UPDATE request.
859 * @param handle statistics handle
860 * @param size how many bytes can we write to @a buf
861 * @param buf where to write requests to the service
862 * @return number of bytes written to @a buf
865 transmit_set (struct GNUNET_STATISTICS_Handle *handle,
869 struct GNUNET_STATISTICS_SetMessage *r;
876 do_disconnect (handle);
877 reconnect_later (handle);
880 slen = strlen (handle->current->subsystem) + 1;
881 nlen = strlen (handle->current->name) + 1;
882 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
886 do_disconnect (handle);
887 reconnect_later (handle);
891 r->header.size = htons (nsize);
892 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
894 r->value = GNUNET_htonll (handle->current->value);
895 if (handle->current->make_persistent)
896 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
897 if (handle->current->type == ACTION_UPDATE)
898 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
899 GNUNET_assert (slen + nlen ==
900 GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
901 handle->current->subsystem,
902 handle->current->name));
903 GNUNET_assert (NULL == handle->current->cont);
904 free_action_item (handle->current);
905 handle->current = NULL;
906 update_memory_statistics (handle);
912 * Function called when we are ready to transmit a request to the service.
914 * @param cls the `struct GNUNET_STATISTICS_Handle`
915 * @param size how many bytes can we write to @a buf
916 * @param buf where to write requests to the service
917 * @return number of bytes written to @a buf
920 transmit_action (void *cls, size_t size, void *buf)
922 struct GNUNET_STATISTICS_Handle *h = cls;
927 if (NULL != h->current)
928 switch (h->current->type)
931 ret = transmit_get (h, size, buf);
935 ret = transmit_set (h, size, buf);
938 ret = transmit_watch (h, size, buf);
950 * Get handle for the statistics service.
952 * @param subsystem name of subsystem using the service
953 * @param cfg services configuration in use
954 * @return handle to use
956 struct GNUNET_STATISTICS_Handle *
957 GNUNET_STATISTICS_create (const char *subsystem,
958 const struct GNUNET_CONFIGURATION_Handle *cfg)
960 struct GNUNET_STATISTICS_Handle *ret;
963 GNUNET_CONFIGURATION_get_value_yesno (cfg, "statistics", "DISABLE"))
965 GNUNET_assert (NULL != subsystem);
966 GNUNET_assert (NULL != cfg);
967 ret = GNUNET_new (struct GNUNET_STATISTICS_Handle);
969 ret->subsystem = GNUNET_strdup (subsystem);
970 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
976 * Destroy a handle (free all state associated with
979 * @param h statistics handle to destroy
980 * @param sync_first set to #GNUNET_YES if pending SET requests should
984 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h,
987 struct GNUNET_STATISTICS_GetHandle *pos;
988 struct GNUNET_STATISTICS_GetHandle *next;
989 struct GNUNET_TIME_Relative timeout;
994 GNUNET_assert (GNUNET_NO == h->do_destroy); // Don't call twice.
995 if (NULL != h->backoff_task)
997 GNUNET_SCHEDULER_cancel (h->backoff_task);
998 h->backoff_task = NULL;
1002 if (NULL != h->current)
1004 if (ACTION_GET == h->current->type)
1008 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
1011 free_action_item (h->current);
1015 next = h->action_head;
1016 while (NULL != (pos = next))
1019 if (ACTION_GET == pos->type)
1021 GNUNET_CONTAINER_DLL_remove (h->action_head,
1024 free_action_item (pos);
1027 if ( (NULL == h->current) &&
1028 (NULL != (h->current = h->action_head)) )
1029 GNUNET_CONTAINER_DLL_remove (h->action_head,
1032 h->do_destroy = GNUNET_YES;
1033 if ((NULL != h->current) && (NULL == h->th) &&
1034 (NULL != h->client))
1036 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
1038 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1039 timeout, GNUNET_YES,
1040 &transmit_action, h);
1041 GNUNET_assert (NULL != h->th);
1044 return; /* do not finish destruction just yet */
1046 while (NULL != (pos = h->action_head))
1048 GNUNET_CONTAINER_DLL_remove (h->action_head,
1051 free_action_item (pos);
1054 for (i = 0; i < h->watches_size; i++)
1056 if (NULL == h->watches[i])
1058 GNUNET_free (h->watches[i]->subsystem);
1059 GNUNET_free (h->watches[i]->name);
1060 GNUNET_free (h->watches[i]);
1062 GNUNET_array_grow (h->watches, h->watches_size, 0);
1063 GNUNET_free (h->subsystem);
1069 * Function called to transmit TEST message to service to
1070 * confirm that the service has received all of our 'SET'
1071 * messages (during statistics disconnect/shutdown).
1073 * @param cls the `struct GNUNET_STATISTICS_Handle`
1074 * @param size how many bytes can we write to @a buf
1075 * @param buf where to write requests to the service
1076 * @return number of bytes written to @a buf
1079 transmit_test_on_shutdown (void *cls,
1083 struct GNUNET_STATISTICS_Handle *h = cls;
1084 struct GNUNET_MessageHeader hdr;
1089 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1090 _("Failed to receive acknowledgement from statistics service, some statistics might have been lost!\n"));
1091 h->do_destroy = GNUNET_NO;
1092 GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
1093 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1096 hdr.type = htons (GNUNET_MESSAGE_TYPE_TEST);
1097 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
1098 memcpy (buf, &hdr, sizeof (hdr));
1099 if (GNUNET_YES != h->receiving)
1101 h->receiving = GNUNET_YES;
1102 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
1103 GNUNET_TIME_UNIT_FOREVER_REL);
1105 return sizeof (struct GNUNET_MessageHeader);
1110 * Schedule the next action to be performed.
1112 * @param h statistics handle
1115 schedule_action (struct GNUNET_STATISTICS_Handle *h)
1117 struct GNUNET_TIME_Relative timeout;
1119 if ( (NULL != h->th) ||
1120 (NULL != h->backoff_task) )
1121 return; /* action already pending */
1122 if (GNUNET_YES != try_connect (h))
1124 reconnect_later (h);
1127 if (NULL != h->current)
1128 return; /* action already pending */
1129 /* schedule next action */
1130 h->current = h->action_head;
1131 if (NULL == h->current)
1133 if (GNUNET_YES == h->do_destroy)
1135 h->do_destroy = GNUNET_SYSERR; /* in 'TEST' mode */
1136 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
1137 sizeof (struct GNUNET_MessageHeader),
1138 SET_TRANSMIT_TIMEOUT,
1140 &transmit_test_on_shutdown, h);
1144 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
1145 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
1148 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1149 timeout, GNUNET_YES,
1150 &transmit_action, h)))
1152 LOG (GNUNET_ERROR_TYPE_DEBUG,
1153 "Failed to transmit request to statistics service.\n");
1155 reconnect_later (h);
1161 * We have run into a timeout on a #GNUNET_STATISTICS_get() operation,
1162 * call the continuation.
1164 * @param cls the `struct GNUNET_STATISTICS_GetHandle`
1165 * @param tc scheduler context
1168 run_get_timeout (void *cls,
1169 const struct GNUNET_SCHEDULER_TaskContext *tc)
1171 struct GNUNET_STATISTICS_GetHandle *gh = cls;
1172 GNUNET_STATISTICS_Callback cont = gh->cont;
1173 void *cont_cls = gh->cls;
1175 gh->timeout_task = NULL;
1176 GNUNET_STATISTICS_get_cancel (gh);
1177 cont (cont_cls, GNUNET_SYSERR);
1182 * Get statistic from the peer.
1184 * @param handle identification of the statistics service
1185 * @param subsystem limit to the specified subsystem, NULL for our subsystem
1186 * @param name name of the statistic value, NULL for all values
1187 * @param timeout after how long should we give up (and call
1188 * cont with an error code)?
1189 * @param cont continuation to call when done (can be NULL)
1190 * This callback CANNOT destroy the statistics handle in the same call.
1191 * @param proc function to call on each value
1192 * @param cls closure for @a cont and @a proc
1193 * @return NULL on error
1195 struct GNUNET_STATISTICS_GetHandle *
1196 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
1197 const char *subsystem, const char *name,
1198 struct GNUNET_TIME_Relative timeout,
1199 GNUNET_STATISTICS_Callback cont,
1200 GNUNET_STATISTICS_Iterator proc, void *cls)
1204 struct GNUNET_STATISTICS_GetHandle *ai;
1208 GNUNET_assert (NULL != proc);
1209 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1210 if (NULL == subsystem)
1214 slen1 = strlen (subsystem) + 1;
1215 slen2 = strlen (name) + 1;
1216 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
1217 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1218 ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle);
1220 ai->subsystem = GNUNET_strdup (subsystem);
1221 ai->name = GNUNET_strdup (name);
1225 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1226 ai->type = ACTION_GET;
1227 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
1228 ai->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
1231 GNUNET_CONTAINER_DLL_insert_tail (handle->action_head, handle->action_tail,
1233 schedule_action (handle);
1239 * Cancel a 'get' request. Must be called before the 'cont'
1240 * function is called.
1242 * @param gh handle of the request to cancel
1245 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
1249 if (NULL != gh->timeout_task)
1251 GNUNET_SCHEDULER_cancel (gh->timeout_task);
1252 gh->timeout_task = NULL;
1255 if (gh->sh->current == gh)
1257 gh->aborted = GNUNET_YES;
1261 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail, gh);
1262 GNUNET_free (gh->name);
1263 GNUNET_free (gh->subsystem);
1270 * Watch statistics from the peer (be notified whenever they change).
1272 * @param handle identification of the statistics service
1273 * @param subsystem limit to the specified subsystem, never NULL
1274 * @param name name of the statistic value, never NULL
1275 * @param proc function to call on each value
1276 * @param proc_cls closure for @a proc
1277 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1280 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1281 const char *subsystem, const char *name,
1282 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1284 struct GNUNET_STATISTICS_WatchEntry *w;
1287 return GNUNET_SYSERR;
1288 w = GNUNET_new (struct GNUNET_STATISTICS_WatchEntry);
1289 w->subsystem = GNUNET_strdup (subsystem);
1290 w->name = GNUNET_strdup (name);
1292 w->proc_cls = proc_cls;
1293 GNUNET_array_append (handle->watches, handle->watches_size, w);
1294 schedule_watch_request (handle, w);
1300 * Stop watching statistics from the peer.
1302 * @param handle identification of the statistics service
1303 * @param subsystem limit to the specified subsystem, never NULL
1304 * @param name name of the statistic value, never NULL
1305 * @param proc function to call on each value
1306 * @param proc_cls closure for @a proc
1307 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (no such watch)
1310 GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
1311 const char *subsystem,
1313 GNUNET_STATISTICS_Iterator proc,
1316 struct GNUNET_STATISTICS_WatchEntry *w;
1320 return GNUNET_SYSERR;
1321 for (i=0;i<handle->watches_size;i++)
1323 w = handle->watches[i];
1326 if ( (w->proc == proc) &&
1327 (w->proc_cls == proc_cls) &&
1328 (0 == strcmp (w->name, name)) &&
1329 (0 == strcmp (w->subsystem, subsystem)) )
1331 GNUNET_free (w->name);
1332 GNUNET_free (w->subsystem);
1334 handle->watches[i] = NULL;
1338 return GNUNET_SYSERR;
1344 * Queue a request to change a statistic.
1346 * @param h statistics handle
1347 * @param name name of the value
1348 * @param make_persistent should the value be kept across restarts?
1349 * @param value new value or change
1350 * @param type type of the action (#ACTION_SET or #ACTION_UPDATE)
1353 add_setter_action (struct GNUNET_STATISTICS_Handle *h,
1355 int make_persistent,
1357 enum ActionType type)
1359 struct GNUNET_STATISTICS_GetHandle *ai;
1365 slen = strlen (h->subsystem) + 1;
1366 nlen = strlen (name) + 1;
1367 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1368 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1373 for (ai = h->action_head; NULL != ai; ai = ai->next)
1375 if (! ( (0 == strcmp (ai->subsystem, h->subsystem)) &&
1376 (0 == strcmp (ai->name, name)) &&
1377 ( (ACTION_UPDATE == ai->type) ||
1378 (ACTION_SET == ai->type) ) ) )
1380 if (ACTION_SET == ai->type)
1382 if (ACTION_UPDATE == type)
1384 delta = (int64_t) value;
1387 /* update old set by new delta */
1392 /* update old set by new delta, but never go negative */
1393 if (ai->value < -delta)
1401 /* new set overrides old set */
1407 if (ACTION_UPDATE == type)
1409 /* make delta cummulative */
1410 delta = (int64_t) value;
1415 /* drop old 'update', use new 'set' instead */
1420 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1421 ai->make_persistent = make_persistent;
1424 /* no existing entry matches, create a fresh one */
1425 ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle);
1427 ai->subsystem = GNUNET_strdup (h->subsystem);
1428 ai->name = GNUNET_strdup (name);
1429 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1430 ai->make_persistent = make_persistent;
1434 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
1436 schedule_action (h);
1441 * Set statistic value for the peer. Will always use our
1442 * subsystem (the argument used when "handle" was created).
1444 * @param handle identification of the statistics service
1445 * @param name name of the statistic value
1446 * @param value new value to set
1447 * @param make_persistent should the value be kept across restarts?
1450 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1453 int make_persistent)
1457 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1458 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
1463 * Set statistic value for the peer. Will always use our
1464 * subsystem (the argument used when "handle" was created).
1466 * @param handle identification of the statistics service
1467 * @param name name of the statistic value
1468 * @param delta change in value (added to existing value)
1469 * @param make_persistent should the value be kept across restarts?
1472 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1475 int make_persistent)
1481 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1482 add_setter_action (handle,
1490 /* end of statistics_api.c */