2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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
467 reconnect_task (void *cls)
469 struct GNUNET_STATISTICS_Handle *h = cls;
471 h->backoff_task = NULL;
477 * Task used by 'reconnect_later' to shutdown the handle
479 * @param cls the statistics handle
482 do_destroy (void *cls)
484 struct GNUNET_STATISTICS_Handle *h = cls;
486 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
491 * Reconnect at a later time, respecting back-off.
493 * @param h statistics handle
496 reconnect_later (struct GNUNET_STATISTICS_Handle *h)
499 struct GNUNET_STATISTICS_GetHandle *gh;
501 GNUNET_assert (NULL == h->backoff_task);
502 if (GNUNET_YES == h->do_destroy)
504 /* So we are shutting down and the service is not reachable.
505 * Chances are that it's down for good and we are not going to connect to
507 * Give up and don't sync the rest of the data.
510 for (gh = h->action_head; NULL != gh; gh = gh->next)
511 if ( (gh->make_persistent) && (ACTION_SET == gh->type) )
513 if (GNUNET_YES == loss)
514 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
515 _("Could not save some persistent statistics\n"));
516 h->do_destroy = GNUNET_NO;
517 GNUNET_SCHEDULER_add_now (&do_destroy, h);
521 GNUNET_SCHEDULER_add_delayed (h->backoff, &reconnect_task, h);
522 h->backoff = GNUNET_TIME_STD_BACKOFF (h->backoff);
527 * Process a #GNUNET_MESSAGE_TYPE_STATISTICS_VALUE message.
529 * @param h statistics handle
530 * @param msg message received from the service, never NULL
531 * @return #GNUNET_OK if the message was well-formed
534 process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
535 const struct GNUNET_MessageHeader *msg)
539 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
542 if (h->current->aborted)
544 LOG (GNUNET_ERROR_TYPE_DEBUG,
545 "Iteration was aborted, ignoring VALUE\n");
546 return GNUNET_OK; /* don't bother */
548 size = ntohs (msg->size);
549 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
552 return GNUNET_SYSERR;
554 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
555 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
557 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], size, 2,
561 return GNUNET_SYSERR;
563 LOG (GNUNET_ERROR_TYPE_DEBUG,
564 "Received valid statistic on `%s:%s': %llu\n",
566 GNUNET_ntohll (smsg->value));
568 h->current->proc (h->current->cls, service, name,
569 GNUNET_ntohll (smsg->value),
571 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
573 LOG (GNUNET_ERROR_TYPE_DEBUG,
574 "Processing of remaining statistics aborted by client.\n");
575 h->current->aborted = GNUNET_YES;
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "VALUE processed successfully\n");
584 * We have received a watch value from the service. Process it.
586 * @param h statistics handle
587 * @param msg the watch value message
588 * @return #GNUNET_OK if the message was well-formed, #GNUNET_SYSERR if not,
589 * #GNUNET_NO if this watch has been cancelled
592 process_watch_value (struct GNUNET_STATISTICS_Handle *h,
593 const struct GNUNET_MessageHeader *msg)
595 const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
596 struct GNUNET_STATISTICS_WatchEntry *w;
599 if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) != ntohs (msg->size))
602 return GNUNET_SYSERR;
604 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
605 GNUNET_break (0 == ntohl (wvm->reserved));
606 wid = ntohl (wvm->wid);
607 if (wid >= h->watches_size)
610 return GNUNET_SYSERR;
615 (void) w->proc (w->proc_cls, w->subsystem, w->name,
616 GNUNET_ntohll (wvm->value),
617 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
623 * Task used to destroy the statistics handle.
625 * @param cls the `struct GNUNET_STATISTICS_Handle`
628 destroy_task (void *cls)
630 struct GNUNET_STATISTICS_Handle *h = cls;
632 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
637 * Function called with messages from stats service.
640 * @param msg message received, NULL on timeout or fatal error
643 receive_stats (void *cls,
644 const struct GNUNET_MessageHeader *msg)
646 struct GNUNET_STATISTICS_Handle *h = cls;
647 struct GNUNET_STATISTICS_GetHandle *c;
652 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
653 "Error receiving statistics from service, is the service running?\n");
658 switch (ntohs (msg->type))
660 case GNUNET_MESSAGE_TYPE_TEST:
661 if (GNUNET_SYSERR != h->do_destroy)
663 /* not in shutdown, why do we get 'TEST'? */
669 h->do_destroy = GNUNET_NO;
670 GNUNET_SCHEDULER_add_now (&destroy_task, h);
672 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
673 LOG (GNUNET_ERROR_TYPE_DEBUG,
674 "Received end of statistics marker\n");
675 if (NULL == (c = h->current))
682 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
683 if (h->watches_size > 0)
685 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
686 GNUNET_TIME_UNIT_FOREVER_REL);
690 h->receiving = GNUNET_NO;
696 c->cont (c->cls, GNUNET_OK);
699 free_action_item (c);
701 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
702 if (GNUNET_OK != process_statistics_value_message (h, msg))
708 /* finally, look for more! */
709 LOG (GNUNET_ERROR_TYPE_DEBUG,
710 "Processing VALUE done, now reading more\n");
711 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
712 GNUNET_TIME_absolute_get_remaining (h->
714 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
716 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
718 (ret = process_watch_value (h, msg)))
721 if (GNUNET_NO == ret)
722 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
726 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
727 GNUNET_assert (h->watches_size > 0);
728 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
729 GNUNET_TIME_UNIT_FOREVER_REL);
741 * Transmit a GET request (and if successful, start to receive
744 * @param handle statistics handle
745 * @param size how many bytes can we write to @a buf
746 * @param buf where to write requests to the service
747 * @return number of bytes written to @a buf
750 transmit_get (struct GNUNET_STATISTICS_Handle *handle,
754 struct GNUNET_STATISTICS_GetHandle *c;
755 struct GNUNET_MessageHeader *hdr;
760 GNUNET_assert (NULL != (c = handle->current));
763 /* timeout / error */
764 LOG (GNUNET_ERROR_TYPE_DEBUG,
765 "Transmission of request for statistics failed!\n");
766 do_disconnect (handle);
767 reconnect_later (handle);
770 slen1 = strlen (c->subsystem) + 1;
771 slen2 = strlen (c->name) + 1;
772 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
773 GNUNET_assert (msize <= size);
774 hdr = (struct GNUNET_MessageHeader *) buf;
775 hdr->size = htons (msize);
776 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
777 GNUNET_assert (slen1 + slen2 ==
778 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
781 if (GNUNET_YES != handle->receiving)
783 LOG (GNUNET_ERROR_TYPE_DEBUG,
784 "Transmission of GET done, now reading response\n");
785 handle->receiving = GNUNET_YES;
786 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
787 GNUNET_TIME_absolute_get_remaining (c->timeout));
794 * Transmit a WATCH request (and if successful, start to receive
797 * @param handle statistics handle
798 * @param size how many bytes can we write to @a buf
799 * @param buf where to write requests to the service
800 * @return number of bytes written to @a buf
803 transmit_watch (struct GNUNET_STATISTICS_Handle *handle,
807 struct GNUNET_MessageHeader *hdr;
814 /* timeout / error */
815 LOG (GNUNET_ERROR_TYPE_DEBUG,
816 "Transmission of request for statistics failed!\n");
817 do_disconnect (handle);
818 reconnect_later (handle);
821 LOG (GNUNET_ERROR_TYPE_DEBUG,
822 "Transmitting watch request for `%s'\n",
823 handle->current->name);
824 slen1 = strlen (handle->current->subsystem) + 1;
825 slen2 = strlen (handle->current->name) + 1;
826 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
827 GNUNET_assert (msize <= size);
828 hdr = (struct GNUNET_MessageHeader *) buf;
829 hdr->size = htons (msize);
830 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
831 GNUNET_assert (slen1 + slen2 ==
832 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
833 handle->current->subsystem,
834 handle->current->name));
835 if (GNUNET_YES != handle->receiving)
837 handle->receiving = GNUNET_YES;
838 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
839 GNUNET_TIME_UNIT_FOREVER_REL);
841 GNUNET_assert (NULL == handle->current->cont);
842 free_action_item (handle->current);
843 handle->current = NULL;
849 * Transmit a SET/UPDATE request.
851 * @param handle statistics handle
852 * @param size how many bytes can we write to @a buf
853 * @param buf where to write requests to the service
854 * @return number of bytes written to @a buf
857 transmit_set (struct GNUNET_STATISTICS_Handle *handle,
861 struct GNUNET_STATISTICS_SetMessage *r;
868 do_disconnect (handle);
869 reconnect_later (handle);
872 slen = strlen (handle->current->subsystem) + 1;
873 nlen = strlen (handle->current->name) + 1;
874 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
878 do_disconnect (handle);
879 reconnect_later (handle);
883 r->header.size = htons (nsize);
884 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
886 r->value = GNUNET_htonll (handle->current->value);
887 if (handle->current->make_persistent)
888 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
889 if (handle->current->type == ACTION_UPDATE)
890 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
891 GNUNET_assert (slen + nlen ==
892 GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
893 handle->current->subsystem,
894 handle->current->name));
895 GNUNET_assert (NULL == handle->current->cont);
896 free_action_item (handle->current);
897 handle->current = NULL;
898 update_memory_statistics (handle);
904 * Function called when we are ready to transmit a request to the service.
906 * @param cls the `struct GNUNET_STATISTICS_Handle`
907 * @param size how many bytes can we write to @a buf
908 * @param buf where to write requests to the service
909 * @return number of bytes written to @a buf
912 transmit_action (void *cls, size_t size, void *buf)
914 struct GNUNET_STATISTICS_Handle *h = cls;
919 if (NULL != h->current)
920 switch (h->current->type)
923 ret = transmit_get (h, size, buf);
927 ret = transmit_set (h, size, buf);
930 ret = transmit_watch (h, size, buf);
942 * Get handle for the statistics service.
944 * @param subsystem name of subsystem using the service
945 * @param cfg services configuration in use
946 * @return handle to use
948 struct GNUNET_STATISTICS_Handle *
949 GNUNET_STATISTICS_create (const char *subsystem,
950 const struct GNUNET_CONFIGURATION_Handle *cfg)
952 struct GNUNET_STATISTICS_Handle *ret;
955 GNUNET_CONFIGURATION_get_value_yesno (cfg, "statistics", "DISABLE"))
957 GNUNET_assert (NULL != subsystem);
958 GNUNET_assert (NULL != cfg);
959 ret = GNUNET_new (struct GNUNET_STATISTICS_Handle);
961 ret->subsystem = GNUNET_strdup (subsystem);
962 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
968 * Destroy a handle (free all state associated with
971 * @param h statistics handle to destroy
972 * @param sync_first set to #GNUNET_YES if pending SET requests should
976 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h,
979 struct GNUNET_STATISTICS_GetHandle *pos;
980 struct GNUNET_STATISTICS_GetHandle *next;
981 struct GNUNET_TIME_Relative timeout;
986 GNUNET_assert (GNUNET_NO == h->do_destroy); // Don't call twice.
987 if (NULL != h->backoff_task)
989 GNUNET_SCHEDULER_cancel (h->backoff_task);
990 h->backoff_task = NULL;
994 if (NULL != h->current)
996 if (ACTION_GET == h->current->type)
1000 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
1003 free_action_item (h->current);
1007 next = h->action_head;
1008 while (NULL != (pos = next))
1011 if (ACTION_GET == pos->type)
1013 GNUNET_CONTAINER_DLL_remove (h->action_head,
1016 free_action_item (pos);
1019 if ( (NULL == h->current) &&
1020 (NULL != (h->current = h->action_head)) )
1021 GNUNET_CONTAINER_DLL_remove (h->action_head,
1024 h->do_destroy = GNUNET_YES;
1025 if ((NULL != h->current) && (NULL == h->th) &&
1026 (NULL != h->client))
1028 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
1030 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1031 timeout, GNUNET_YES,
1032 &transmit_action, h);
1033 GNUNET_assert (NULL != h->th);
1036 return; /* do not finish destruction just yet */
1038 while (NULL != (pos = h->action_head))
1040 GNUNET_CONTAINER_DLL_remove (h->action_head,
1043 free_action_item (pos);
1046 for (i = 0; i < h->watches_size; i++)
1048 if (NULL == h->watches[i])
1050 GNUNET_free (h->watches[i]->subsystem);
1051 GNUNET_free (h->watches[i]->name);
1052 GNUNET_free (h->watches[i]);
1054 GNUNET_array_grow (h->watches, h->watches_size, 0);
1055 GNUNET_free (h->subsystem);
1061 * Function called to transmit TEST message to service to
1062 * confirm that the service has received all of our 'SET'
1063 * messages (during statistics disconnect/shutdown).
1065 * @param cls the `struct GNUNET_STATISTICS_Handle`
1066 * @param size how many bytes can we write to @a buf
1067 * @param buf where to write requests to the service
1068 * @return number of bytes written to @a buf
1071 transmit_test_on_shutdown (void *cls,
1075 struct GNUNET_STATISTICS_Handle *h = cls;
1076 struct GNUNET_MessageHeader hdr;
1081 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1082 _("Failed to receive acknowledgement from statistics service, some statistics might have been lost!\n"));
1083 h->do_destroy = GNUNET_NO;
1084 GNUNET_SCHEDULER_add_now (&destroy_task, h);
1087 hdr.type = htons (GNUNET_MESSAGE_TYPE_TEST);
1088 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
1089 memcpy (buf, &hdr, sizeof (hdr));
1090 if (GNUNET_YES != h->receiving)
1092 h->receiving = GNUNET_YES;
1093 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
1094 GNUNET_TIME_UNIT_FOREVER_REL);
1096 return sizeof (struct GNUNET_MessageHeader);
1101 * Schedule the next action to be performed.
1103 * @param h statistics handle
1106 schedule_action (struct GNUNET_STATISTICS_Handle *h)
1108 struct GNUNET_TIME_Relative timeout;
1110 if ( (NULL != h->th) ||
1111 (NULL != h->backoff_task) )
1112 return; /* action already pending */
1113 if (GNUNET_YES != try_connect (h))
1115 reconnect_later (h);
1118 if (NULL != h->current)
1119 return; /* action already pending */
1120 /* schedule next action */
1121 h->current = h->action_head;
1122 if (NULL == h->current)
1124 if (GNUNET_YES == h->do_destroy)
1126 h->do_destroy = GNUNET_SYSERR; /* in 'TEST' mode */
1127 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
1128 sizeof (struct GNUNET_MessageHeader),
1129 SET_TRANSMIT_TIMEOUT,
1131 &transmit_test_on_shutdown, h);
1135 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
1136 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
1139 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1140 timeout, GNUNET_YES,
1141 &transmit_action, h)))
1143 LOG (GNUNET_ERROR_TYPE_DEBUG,
1144 "Failed to transmit request to statistics service.\n");
1146 reconnect_later (h);
1152 * We have run into a timeout on a #GNUNET_STATISTICS_get() operation,
1153 * call the continuation.
1155 * @param cls the `struct GNUNET_STATISTICS_GetHandle`
1158 run_get_timeout (void *cls)
1160 struct GNUNET_STATISTICS_GetHandle *gh = cls;
1161 GNUNET_STATISTICS_Callback cont = gh->cont;
1162 void *cont_cls = gh->cls;
1164 gh->timeout_task = NULL;
1165 GNUNET_STATISTICS_get_cancel (gh);
1166 cont (cont_cls, GNUNET_SYSERR);
1171 * Get statistic from the peer.
1173 * @param handle identification of the statistics service
1174 * @param subsystem limit to the specified subsystem, NULL for our subsystem
1175 * @param name name of the statistic value, NULL for all values
1176 * @param timeout after how long should we give up (and call
1177 * cont with an error code)?
1178 * @param cont continuation to call when done (can be NULL)
1179 * This callback CANNOT destroy the statistics handle in the same call.
1180 * @param proc function to call on each value
1181 * @param cls closure for @a cont and @a proc
1182 * @return NULL on error
1184 struct GNUNET_STATISTICS_GetHandle *
1185 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
1186 const char *subsystem, const char *name,
1187 struct GNUNET_TIME_Relative timeout,
1188 GNUNET_STATISTICS_Callback cont,
1189 GNUNET_STATISTICS_Iterator proc, void *cls)
1193 struct GNUNET_STATISTICS_GetHandle *ai;
1197 GNUNET_assert (NULL != proc);
1198 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1199 if (NULL == subsystem)
1203 slen1 = strlen (subsystem) + 1;
1204 slen2 = strlen (name) + 1;
1205 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
1206 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1207 ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle);
1209 ai->subsystem = GNUNET_strdup (subsystem);
1210 ai->name = GNUNET_strdup (name);
1214 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1215 ai->type = ACTION_GET;
1216 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
1217 ai->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
1220 GNUNET_CONTAINER_DLL_insert_tail (handle->action_head,
1221 handle->action_tail,
1223 schedule_action (handle);
1229 * Cancel a 'get' request. Must be called before the 'cont'
1230 * function is called.
1232 * @param gh handle of the request to cancel
1235 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
1239 if (NULL != gh->timeout_task)
1241 GNUNET_SCHEDULER_cancel (gh->timeout_task);
1242 gh->timeout_task = NULL;
1245 if (gh->sh->current == gh)
1247 gh->aborted = GNUNET_YES;
1251 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail, gh);
1252 GNUNET_free (gh->name);
1253 GNUNET_free (gh->subsystem);
1260 * Watch statistics from the peer (be notified whenever they change).
1262 * @param handle identification of the statistics service
1263 * @param subsystem limit to the specified subsystem, never NULL
1264 * @param name name of the statistic value, never NULL
1265 * @param proc function to call on each value
1266 * @param proc_cls closure for @a proc
1267 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1270 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1271 const char *subsystem, const char *name,
1272 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1274 struct GNUNET_STATISTICS_WatchEntry *w;
1277 return GNUNET_SYSERR;
1278 w = GNUNET_new (struct GNUNET_STATISTICS_WatchEntry);
1279 w->subsystem = GNUNET_strdup (subsystem);
1280 w->name = GNUNET_strdup (name);
1282 w->proc_cls = proc_cls;
1283 GNUNET_array_append (handle->watches, handle->watches_size, w);
1284 schedule_watch_request (handle, w);
1290 * Stop watching statistics from the peer.
1292 * @param handle identification of the statistics service
1293 * @param subsystem limit to the specified subsystem, never NULL
1294 * @param name name of the statistic value, never NULL
1295 * @param proc function to call on each value
1296 * @param proc_cls closure for @a proc
1297 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (no such watch)
1300 GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
1301 const char *subsystem,
1303 GNUNET_STATISTICS_Iterator proc,
1306 struct GNUNET_STATISTICS_WatchEntry *w;
1310 return GNUNET_SYSERR;
1311 for (i=0;i<handle->watches_size;i++)
1313 w = handle->watches[i];
1316 if ( (w->proc == proc) &&
1317 (w->proc_cls == proc_cls) &&
1318 (0 == strcmp (w->name, name)) &&
1319 (0 == strcmp (w->subsystem, subsystem)) )
1321 GNUNET_free (w->name);
1322 GNUNET_free (w->subsystem);
1324 handle->watches[i] = NULL;
1328 return GNUNET_SYSERR;
1334 * Queue a request to change a statistic.
1336 * @param h statistics handle
1337 * @param name name of the value
1338 * @param make_persistent should the value be kept across restarts?
1339 * @param value new value or change
1340 * @param type type of the action (#ACTION_SET or #ACTION_UPDATE)
1343 add_setter_action (struct GNUNET_STATISTICS_Handle *h,
1345 int make_persistent,
1347 enum ActionType type)
1349 struct GNUNET_STATISTICS_GetHandle *ai;
1355 slen = strlen (h->subsystem) + 1;
1356 nlen = strlen (name) + 1;
1357 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1358 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1363 for (ai = h->action_head; NULL != ai; ai = ai->next)
1365 if (! ( (0 == strcmp (ai->subsystem, h->subsystem)) &&
1366 (0 == strcmp (ai->name, name)) &&
1367 ( (ACTION_UPDATE == ai->type) ||
1368 (ACTION_SET == ai->type) ) ) )
1370 if (ACTION_SET == ai->type)
1372 if (ACTION_UPDATE == type)
1374 delta = (int64_t) value;
1377 /* update old set by new delta */
1382 /* update old set by new delta, but never go negative */
1383 if (ai->value < -delta)
1391 /* new set overrides old set */
1397 if (ACTION_UPDATE == type)
1399 /* make delta cummulative */
1400 delta = (int64_t) value;
1405 /* drop old 'update', use new 'set' instead */
1410 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1411 ai->make_persistent = make_persistent;
1414 /* no existing entry matches, create a fresh one */
1415 ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle);
1417 ai->subsystem = GNUNET_strdup (h->subsystem);
1418 ai->name = GNUNET_strdup (name);
1419 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1420 ai->make_persistent = make_persistent;
1424 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
1426 schedule_action (h);
1431 * Set statistic value for the peer. Will always use our
1432 * subsystem (the argument used when "handle" was created).
1434 * @param handle identification of the statistics service
1435 * @param name name of the statistic value
1436 * @param value new value to set
1437 * @param make_persistent should the value be kept across restarts?
1440 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1443 int make_persistent)
1447 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1448 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
1453 * Set statistic value for the peer. Will always use our
1454 * subsystem (the argument used when "handle" was created).
1456 * @param handle identification of the statistics service
1457 * @param name name of the statistic value
1458 * @param delta change in value (added to existing value)
1459 * @param make_persistent should the value be kept across restarts?
1462 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1465 int make_persistent)
1471 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1472 add_setter_action (handle,
1480 /* end of statistics_api.c */