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 2, 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_client_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_server_lib.h"
32 #include "gnunet_statistics_service.h"
33 #include "gnunet_strings_lib.h"
34 #include "statistics.h"
37 * How long do we wait until a statistics request for setting
38 * a value times out? (The update will be lost if the
39 * service does not react within this timeframe).
41 #define SET_TRANSMIT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
43 #define LOG(kind,...) GNUNET_log_from (kind, "statistics-api",__VA_ARGS__)
73 * Entry kept for each value we are watching.
75 struct GNUNET_STATISTICS_WatchEntry
79 * What subsystem is this action about? (never NULL)
84 * What value is this action about? (never NULL)
91 GNUNET_STATISTICS_Iterator proc;
102 * Linked list of things we still need to do.
104 struct GNUNET_STATISTICS_GetHandle
108 * This is a doubly linked list.
110 struct GNUNET_STATISTICS_GetHandle *next;
113 * This is a doubly linked list.
115 struct GNUNET_STATISTICS_GetHandle *prev;
118 * Main statistics handle.
120 struct GNUNET_STATISTICS_Handle *sh;
123 * What subsystem is this action about? (can be NULL)
128 * What value is this action about? (can be NULL)
133 * Continuation to call once action is complete.
135 GNUNET_STATISTICS_Callback cont;
138 * Function to call (for GET actions only).
140 GNUNET_STATISTICS_Iterator proc;
143 * Closure for proc and cont.
148 * Timeout for this action.
150 struct GNUNET_TIME_Absolute timeout;
158 * Flag for SET/UPDATE actions.
163 * Has the current iteration been aborted; for GET actions.
168 * Is this a GET, SET, UPDATE or WATCH?
170 enum ActionType type;
173 * Size of the message that we will be transmitting.
181 * Handle for the service.
183 struct GNUNET_STATISTICS_Handle
186 * Name of our subsystem.
191 * Configuration to use.
193 const struct GNUNET_CONFIGURATION_Handle *cfg;
196 * Socket (if available).
198 struct GNUNET_CLIENT_Connection *client;
201 * Currently pending transmission request.
203 struct GNUNET_CLIENT_TransmitHandle *th;
206 * Head of the linked list of pending actions (first action
209 struct GNUNET_STATISTICS_GetHandle *action_head;
212 * Tail of the linked list of actions (for fast append).
214 struct GNUNET_STATISTICS_GetHandle *action_tail;
217 * Action we are currently busy with (action request has been
218 * transmitted, we're now receiving the response from the
221 struct GNUNET_STATISTICS_GetHandle *current;
224 * Array of watch entries.
226 struct GNUNET_STATISTICS_WatchEntry **watches;
229 * Task doing exponential back-off trying to reconnect.
231 GNUNET_SCHEDULER_TaskIdentifier backoff_task;
234 * Time for next connect retry.
236 struct GNUNET_TIME_Relative backoff;
239 * Maximum heap size observed so far (if available).
241 uint64_t peak_heap_size;
244 * Maximum resident set side observed so far (if available).
249 * Size of the 'watches' array.
251 unsigned int watches_size;
254 * Should this handle auto-destruct once all actions have
260 * Are we currently receiving from the service?
268 * Obtain statistics about this process's memory consumption and
269 * report those as well (if they changed).
272 update_memory_statistics (struct GNUNET_STATISTICS_Handle *h)
274 #if ENABLE_HEAP_STATISTICS
275 uint64_t current_heap_size = 0;
276 uint64_t current_rss = 0;
278 if (GNUNET_NO != h->do_destroy)
285 current_heap_size = mi.uordblks + mi.fordblks;
292 if (0 == getrusage (RUSAGE_SELF, &ru))
294 current_rss = 1024LL * ru.ru_maxrss;
298 if (current_heap_size > h->peak_heap_size)
300 h->peak_heap_size = current_heap_size;
301 GNUNET_STATISTICS_set (h, "# peak heap size", current_heap_size, GNUNET_NO);
303 if (current_rss > h->peak_rss)
305 h->peak_rss = current_rss;
306 GNUNET_STATISTICS_set (h, "# peak resident set size", current_rss, GNUNET_NO);
313 * Schedule the next action to be performed.
315 * @param h statistics handle to reconnect
318 schedule_action (struct GNUNET_STATISTICS_Handle *h);
322 * Transmit request to service that we want to watch
323 * the development of a particular value.
325 * @param h statistics handle
326 * @param watch watch entry of the value to watch
329 schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
330 struct GNUNET_STATISTICS_WatchEntry *watch)
333 struct GNUNET_STATISTICS_GetHandle *ai;
338 GNUNET_assert (NULL != h);
339 GNUNET_assert (NULL != watch);
341 slen = strlen (watch->subsystem) + 1;
342 nlen = strlen (watch->name) + 1;
343 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
344 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
349 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
351 ai->subsystem = GNUNET_strdup (watch->subsystem);
352 ai->name = GNUNET_strdup (watch->name);
353 ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
355 ai->type = ACTION_WATCH;
356 ai->proc = watch->proc;
357 ai->cls = watch->proc_cls;
358 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
365 * Free memory associated with the given action item.
367 * @param gh action item to free
370 free_action_item (struct GNUNET_STATISTICS_GetHandle *gh)
372 GNUNET_free_non_null (gh->subsystem);
373 GNUNET_free_non_null (gh->name);
379 * Disconnect from the statistics service.
381 * @param h statistics handle to disconnect from
384 do_disconnect (struct GNUNET_STATISTICS_Handle *h)
386 struct GNUNET_STATISTICS_GetHandle *c;
390 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
393 if (NULL != h->client)
395 GNUNET_CLIENT_disconnect (h->client);
398 h->receiving = GNUNET_NO;
399 if (NULL != (c = h->current))
402 if ( (NULL != c->cont) &&
403 (GNUNET_YES != c->aborted) )
404 c->cont (c->cls, GNUNET_SYSERR);
405 free_action_item (c);
411 * Try to (re)connect to the statistics service.
413 * @param h statistics handle to reconnect
414 * @return GNUNET_YES on success, GNUNET_NO on failure.
417 try_connect (struct GNUNET_STATISTICS_Handle *h)
419 struct GNUNET_STATISTICS_GetHandle *gh;
420 struct GNUNET_STATISTICS_GetHandle *gn;
423 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
425 if (NULL != h->client)
427 h->client = GNUNET_CLIENT_connect ("statistics", h->cfg);
428 if (NULL != h->client)
431 while (NULL != (gh = gn))
434 if (gh->type == ACTION_WATCH)
436 GNUNET_CONTAINER_DLL_remove (h->action_head,
439 free_action_item (gh);
442 for (i = 0; i < h->watches_size; i++)
444 if (NULL != h->watches[i])
445 schedule_watch_request (h, h->watches[i]);
449 LOG (GNUNET_ERROR_TYPE_DEBUG,
450 "Failed to connect to statistics service!\n");
456 * We've waited long enough, reconnect now.
458 * @param cls the 'struct GNUNET_STATISTICS_Handle' to reconnect
459 * @param tc scheduler context (unused)
462 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
464 struct GNUNET_STATISTICS_Handle *h = cls;
466 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
472 * Task used by 'reconnect_later' to shutdown the handle
474 * @param cls the statistics handle
475 * @param tc scheduler context
478 do_destroy (void *cls,
479 const struct GNUNET_SCHEDULER_TaskContext *tc)
481 struct GNUNET_STATISTICS_Handle *h = cls;
483 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
488 * Reconnect at a later time, respecting back-off.
490 * @param h statistics handle
493 reconnect_later (struct GNUNET_STATISTICS_Handle *h)
496 struct GNUNET_STATISTICS_GetHandle *gh;
498 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->backoff_task);
499 if (GNUNET_YES == h->do_destroy)
501 /* So we are shutting down and the service is not reachable.
502 * Chances are that it's down for good and we are not going to connect to
504 * Give up and don't sync the rest of the data.
507 for (gh = h->action_head; NULL != gh; gh = gh->next)
508 if ( (gh->make_persistent) && (ACTION_SET == gh->type) )
510 if (GNUNET_YES == loss)
511 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
512 _("Could not save some persistent statistics\n"));
513 h->do_destroy = GNUNET_NO;
514 GNUNET_SCHEDULER_add_continuation (&do_destroy, h,
515 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
519 GNUNET_SCHEDULER_add_delayed (h->backoff, &reconnect_task, h);
520 h->backoff = GNUNET_TIME_STD_BACKOFF (h->backoff);
525 * Process a 'GNUNET_MESSAGE_TYPE_STATISTICS_VALUE' message.
527 * @param h statistics handle
528 * @param msg message received from the service, never NULL
529 * @return GNUNET_OK if the message was well-formed
532 process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
533 const struct GNUNET_MessageHeader *msg)
537 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
540 if (h->current->aborted)
542 LOG (GNUNET_ERROR_TYPE_DEBUG, "Iteration was aborted, ignoring VALUE\n");
543 return GNUNET_OK; /* don't bother */
545 size = ntohs (msg->size);
546 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
549 return GNUNET_SYSERR;
551 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
552 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
554 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], size, 2,
558 return GNUNET_SYSERR;
560 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received valid statistic on `%s:%s': %llu\n",
561 service, name, GNUNET_ntohll (smsg->value));
563 h->current->proc (h->current->cls, service, name,
564 GNUNET_ntohll (smsg->value),
566 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
568 LOG (GNUNET_ERROR_TYPE_DEBUG,
569 "Processing of remaining statistics aborted by client.\n");
570 h->current->aborted = GNUNET_YES;
572 LOG (GNUNET_ERROR_TYPE_DEBUG, "VALUE processed successfully\n");
578 * We have received a watch value from the service. Process it.
580 * @param h statistics handle
581 * @param msg the watch value message
582 * @return GNUNET_OK if the message was well-formed, GNUNET_SYSERR if not,
583 * GNUNET_NO if this watch has been cancelled
586 process_watch_value (struct GNUNET_STATISTICS_Handle *h,
587 const struct GNUNET_MessageHeader *msg)
589 const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
590 struct GNUNET_STATISTICS_WatchEntry *w;
593 if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) != ntohs (msg->size))
596 return GNUNET_SYSERR;
598 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
599 GNUNET_break (0 == ntohl (wvm->reserved));
600 wid = ntohl (wvm->wid);
601 if (wid >= h->watches_size)
604 return GNUNET_SYSERR;
609 (void) w->proc (w->proc_cls, w->subsystem, w->name,
610 GNUNET_ntohll (wvm->value),
611 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
617 destroy_task (void *cls,
618 const struct GNUNET_SCHEDULER_TaskContext *tc)
620 struct GNUNET_STATISTICS_Handle *h = cls;
622 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
627 * Function called with messages from stats service.
630 * @param msg message received, NULL on timeout or fatal error
633 receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
635 struct GNUNET_STATISTICS_Handle *h = cls;
636 struct GNUNET_STATISTICS_GetHandle *c;
641 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
642 "Error receiving statistics from service, is the service running?\n");
647 switch (ntohs (msg->type))
649 case GNUNET_MESSAGE_TYPE_TEST:
650 if (GNUNET_SYSERR != h->do_destroy)
652 /* not in shutdown, why do we get 'TEST'? */
658 h->do_destroy = GNUNET_NO;
659 GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
660 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
662 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
663 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received end of statistics marker\n");
664 if (NULL == (c = h->current))
671 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
672 if (h->watches_size > 0)
674 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
675 GNUNET_TIME_UNIT_FOREVER_REL);
679 h->receiving = GNUNET_NO;
684 c->cont (c->cls, GNUNET_OK);
685 free_action_item (c);
687 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
688 if (GNUNET_OK != process_statistics_value_message (h, msg))
694 /* finally, look for more! */
695 LOG (GNUNET_ERROR_TYPE_DEBUG,
696 "Processing VALUE done, now reading more\n");
697 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
698 GNUNET_TIME_absolute_get_remaining (h->
700 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
702 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
704 (ret = process_watch_value (h, msg)))
707 if (GNUNET_NO == ret)
708 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
712 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
713 GNUNET_assert (h->watches_size > 0);
714 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
715 GNUNET_TIME_UNIT_FOREVER_REL);
727 * Transmit a GET request (and if successful, start to receive
730 * @param handle statistics handle
731 * @param size how many bytes can we write to buf
732 * @param buf where to write requests to the service
733 * @return number of bytes written to buf
736 transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
738 struct GNUNET_STATISTICS_GetHandle *c;
739 struct GNUNET_MessageHeader *hdr;
744 GNUNET_assert (NULL != (c = handle->current));
747 /* timeout / error */
748 LOG (GNUNET_ERROR_TYPE_DEBUG,
749 "Transmission of request for statistics failed!\n");
750 do_disconnect (handle);
751 reconnect_later (handle);
754 slen1 = strlen (c->subsystem) + 1;
755 slen2 = strlen (c->name) + 1;
756 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
757 GNUNET_assert (msize <= size);
758 hdr = (struct GNUNET_MessageHeader *) buf;
759 hdr->size = htons (msize);
760 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
761 GNUNET_assert (slen1 + slen2 ==
762 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
765 if (GNUNET_YES != handle->receiving)
767 LOG (GNUNET_ERROR_TYPE_DEBUG,
768 "Transmission of GET done, now reading response\n");
769 handle->receiving = GNUNET_YES;
770 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
771 GNUNET_TIME_absolute_get_remaining (c->timeout));
778 * Transmit a WATCH request (and if successful, start to receive
781 * @param handle statistics handle
782 * @param size how many bytes can we write to buf
783 * @param buf where to write requests to the service
784 * @return number of bytes written to buf
787 transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
789 struct GNUNET_MessageHeader *hdr;
796 /* timeout / error */
797 LOG (GNUNET_ERROR_TYPE_DEBUG,
798 "Transmission of request for statistics failed!\n");
799 do_disconnect (handle);
800 reconnect_later (handle);
803 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting watch request for `%s'\n",
804 handle->current->name);
805 slen1 = strlen (handle->current->subsystem) + 1;
806 slen2 = strlen (handle->current->name) + 1;
807 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
808 GNUNET_assert (msize <= size);
809 hdr = (struct GNUNET_MessageHeader *) buf;
810 hdr->size = htons (msize);
811 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
812 GNUNET_assert (slen1 + slen2 ==
813 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
814 handle->current->subsystem,
815 handle->current->name));
816 if (GNUNET_YES != handle->receiving)
818 handle->receiving = GNUNET_YES;
819 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
820 GNUNET_TIME_UNIT_FOREVER_REL);
822 GNUNET_assert (NULL == handle->current->cont);
823 free_action_item (handle->current);
824 handle->current = NULL;
830 * Transmit a SET/UPDATE request.
832 * @param handle statistics handle
833 * @param size how many bytes can we write to buf
834 * @param buf where to write requests to the service
835 * @return number of bytes written to buf
838 transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
840 struct GNUNET_STATISTICS_SetMessage *r;
847 do_disconnect (handle);
848 reconnect_later (handle);
851 slen = strlen (handle->current->subsystem) + 1;
852 nlen = strlen (handle->current->name) + 1;
853 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
857 do_disconnect (handle);
858 reconnect_later (handle);
862 r->header.size = htons (nsize);
863 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
865 r->value = GNUNET_htonll (handle->current->value);
866 if (handle->current->make_persistent)
867 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
868 if (handle->current->type == ACTION_UPDATE)
869 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
870 GNUNET_assert (slen + nlen ==
871 GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
872 handle->current->subsystem,
873 handle->current->name));
874 GNUNET_assert (NULL == handle->current->cont);
875 free_action_item (handle->current);
876 handle->current = NULL;
877 update_memory_statistics (handle);
883 * Function called when we are ready to transmit a request to the service.
885 * @param cls the 'struct GNUNET_STATISTICS_Handle'
886 * @param size how many bytes can we write to buf
887 * @param buf where to write requests to the service
888 * @return number of bytes written to buf
891 transmit_action (void *cls, size_t size, void *buf)
893 struct GNUNET_STATISTICS_Handle *h = cls;
898 if (NULL != h->current)
899 switch (h->current->type)
902 ret = transmit_get (h, size, buf);
906 ret = transmit_set (h, size, buf);
909 ret = transmit_watch (h, size, buf);
921 * Get handle for the statistics service.
923 * @param subsystem name of subsystem using the service
924 * @param cfg services configuration in use
925 * @return handle to use
927 struct GNUNET_STATISTICS_Handle *
928 GNUNET_STATISTICS_create (const char *subsystem,
929 const struct GNUNET_CONFIGURATION_Handle *cfg)
931 struct GNUNET_STATISTICS_Handle *ret;
934 GNUNET_CONFIGURATION_get_value_yesno (cfg, "statistics", "DISABLE"))
936 GNUNET_assert (NULL != subsystem);
937 GNUNET_assert (NULL != cfg);
938 ret = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_Handle));
940 ret->subsystem = GNUNET_strdup (subsystem);
941 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
947 * Destroy a handle (free all state associated with
950 * @param h statistics handle to destroy
951 * @param sync_first set to GNUNET_YES if pending SET requests should
955 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
957 struct GNUNET_STATISTICS_GetHandle *pos;
958 struct GNUNET_STATISTICS_GetHandle *next;
959 struct GNUNET_TIME_Relative timeout;
964 GNUNET_assert (GNUNET_NO == h->do_destroy); // Don't call twice.
965 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
967 GNUNET_SCHEDULER_cancel (h->backoff_task);
968 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
972 if (NULL != h->current)
974 if (ACTION_GET == h->current->type)
976 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
978 free_action_item (h->current);
982 next = h->action_head;
983 while (NULL != (pos = next))
986 if (ACTION_GET == pos->type)
988 GNUNET_CONTAINER_DLL_remove (h->action_head,
991 free_action_item (pos);
994 if ( (NULL == h->current) &&
995 (NULL != (h->current = h->action_head)) )
996 GNUNET_CONTAINER_DLL_remove (h->action_head,
999 h->do_destroy = GNUNET_YES;
1000 if ((NULL != h->current) && (NULL == h->th) &&
1001 (NULL != h->client))
1003 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
1005 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1006 timeout, GNUNET_YES,
1007 &transmit_action, h);
1008 GNUNET_assert (NULL != h->th);
1011 return; /* do not finish destruction just yet */
1013 while (NULL != (pos = h->action_head))
1015 GNUNET_CONTAINER_DLL_remove (h->action_head,
1018 free_action_item (pos);
1021 for (i = 0; i < h->watches_size; i++)
1023 if (NULL == h->watches[i])
1025 GNUNET_free (h->watches[i]->subsystem);
1026 GNUNET_free (h->watches[i]->name);
1027 GNUNET_free (h->watches[i]);
1029 GNUNET_array_grow (h->watches, h->watches_size, 0);
1030 GNUNET_free (h->subsystem);
1036 * Function called to transmit TEST message to service to
1037 * confirm that the service has received all of our 'SET'
1038 * messages (during statistics disconnect/shutdown).
1040 * @param cls the 'struct GNUNET_STATISTICS_Handle'
1041 * @param size how many bytes can we write to buf
1042 * @param buf where to write requests to the service
1043 * @return number of bytes written to buf
1046 transmit_test_on_shutdown (void *cls,
1050 struct GNUNET_STATISTICS_Handle *h = cls;
1051 struct GNUNET_MessageHeader hdr;
1056 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1057 _("Failed to receive acknowledgement from statistics service, some statistics might have been lost!\n"));
1058 h->do_destroy = GNUNET_NO;
1059 GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
1060 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1063 hdr.type = htons (GNUNET_MESSAGE_TYPE_TEST);
1064 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
1065 memcpy (buf, &hdr, sizeof (hdr));
1066 if (GNUNET_YES != h->receiving)
1068 h->receiving = GNUNET_YES;
1069 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
1070 GNUNET_TIME_UNIT_FOREVER_REL);
1072 return sizeof (struct GNUNET_MessageHeader);
1077 * Schedule the next action to be performed.
1079 * @param h statistics handle
1082 schedule_action (struct GNUNET_STATISTICS_Handle *h)
1084 struct GNUNET_TIME_Relative timeout;
1086 if ( (NULL != h->th) ||
1087 (GNUNET_SCHEDULER_NO_TASK != h->backoff_task) )
1088 return; /* action already pending */
1089 if (GNUNET_YES != try_connect (h))
1091 reconnect_later (h);
1094 if (NULL != h->current)
1095 return; /* action already pending */
1096 /* schedule next action */
1097 h->current = h->action_head;
1098 if (NULL == h->current)
1100 if (GNUNET_YES == h->do_destroy)
1102 h->do_destroy = GNUNET_SYSERR; /* in 'TEST' mode */
1103 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
1104 sizeof (struct GNUNET_MessageHeader),
1105 SET_TRANSMIT_TIMEOUT,
1107 &transmit_test_on_shutdown, h);
1111 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
1112 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
1115 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1116 timeout, GNUNET_YES,
1117 &transmit_action, h)))
1119 LOG (GNUNET_ERROR_TYPE_DEBUG,
1120 "Failed to transmit request to statistics service.\n");
1122 reconnect_later (h);
1128 * Get statistic from the peer.
1130 * @param handle identification of the statistics service
1131 * @param subsystem limit to the specified subsystem, NULL for our subsystem
1132 * @param name name of the statistic value, NULL for all values
1133 * @param timeout after how long should we give up (and call
1134 * cont with an error code)?
1135 * @param cont continuation to call when done (can be NULL)
1136 * This callback CANNOT destroy the statistics handle in the same call.
1137 * @param proc function to call on each value
1138 * @param cls closure for cont and proc
1139 * @return NULL on error
1141 struct GNUNET_STATISTICS_GetHandle *
1142 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
1143 const char *subsystem, const char *name,
1144 struct GNUNET_TIME_Relative timeout,
1145 GNUNET_STATISTICS_Callback cont,
1146 GNUNET_STATISTICS_Iterator proc, void *cls)
1150 struct GNUNET_STATISTICS_GetHandle *ai;
1154 GNUNET_assert (NULL != proc);
1155 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1156 if (NULL == subsystem)
1160 slen1 = strlen (subsystem) + 1;
1161 slen2 = strlen (name) + 1;
1162 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
1163 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1164 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1166 ai->subsystem = GNUNET_strdup (subsystem);
1167 ai->name = GNUNET_strdup (name);
1171 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1172 ai->type = ACTION_GET;
1173 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
1174 GNUNET_CONTAINER_DLL_insert_tail (handle->action_head, handle->action_tail,
1176 schedule_action (handle);
1182 * Cancel a 'get' request. Must be called before the 'cont'
1183 * function is called.
1185 * @param gh handle of the request to cancel
1188 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
1193 if (gh->sh->current == gh)
1195 gh->aborted = GNUNET_YES;
1199 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail, gh);
1200 GNUNET_free (gh->name);
1201 GNUNET_free (gh->subsystem);
1208 * Watch statistics from the peer (be notified whenever they change).
1210 * @param handle identification of the statistics service
1211 * @param subsystem limit to the specified subsystem, never NULL
1212 * @param name name of the statistic value, never NULL
1213 * @param proc function to call on each value
1214 * @param proc_cls closure for proc
1215 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1218 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1219 const char *subsystem, const char *name,
1220 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1222 struct GNUNET_STATISTICS_WatchEntry *w;
1225 return GNUNET_SYSERR;
1226 w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry));
1227 w->subsystem = GNUNET_strdup (subsystem);
1228 w->name = GNUNET_strdup (name);
1230 w->proc_cls = proc_cls;
1231 GNUNET_array_append (handle->watches, handle->watches_size, w);
1232 schedule_watch_request (handle, w);
1238 * Stop watching statistics from the peer.
1240 * @param handle identification of the statistics service
1241 * @param subsystem limit to the specified subsystem, never NULL
1242 * @param name name of the statistic value, never NULL
1243 * @param proc function to call on each value
1244 * @param proc_cls closure for proc
1245 * @return GNUNET_OK on success, GNUNET_SYSERR on error (no such watch)
1248 GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
1249 const char *subsystem, const char *name,
1250 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1252 struct GNUNET_STATISTICS_WatchEntry *w;
1256 return GNUNET_SYSERR;
1257 for (i=0;i<handle->watches_size;i++)
1259 w = handle->watches[i];
1262 if ( (w->proc == proc) &&
1263 (w->proc_cls == proc_cls) &&
1264 (0 == strcmp (w->name, name)) &&
1265 (0 == strcmp (w->subsystem, subsystem)) )
1267 GNUNET_free (w->name);
1268 GNUNET_free (w->subsystem);
1270 handle->watches[i] = NULL;
1274 return GNUNET_SYSERR;
1280 * Queue a request to change a statistic.
1282 * @param h statistics handle
1283 * @param name name of the value
1284 * @param make_persistent should the value be kept across restarts?
1285 * @param value new value or change
1286 * @param type type of the action (ACTION_SET or ACTION_UPDATE)
1289 add_setter_action (struct GNUNET_STATISTICS_Handle *h, const char *name,
1290 int make_persistent, uint64_t value, enum ActionType type)
1292 struct GNUNET_STATISTICS_GetHandle *ai;
1298 GNUNET_assert (NULL != h);
1299 GNUNET_assert (NULL != name);
1300 slen = strlen (h->subsystem) + 1;
1301 nlen = strlen (name) + 1;
1302 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1303 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1308 for (ai = h->action_head; NULL != ai; ai = ai->next)
1310 if (! ( (0 == strcmp (ai->subsystem, h->subsystem)) &&
1311 (0 == strcmp (ai->name, name)) &&
1312 ( (ACTION_UPDATE == ai->type) ||
1313 (ACTION_SET == ai->type) ) ) )
1315 if (ACTION_SET == ai->type)
1317 if (ACTION_UPDATE == type)
1319 delta = (int64_t) value;
1322 /* update old set by new delta */
1327 /* update old set by new delta, but never go negative */
1328 if (ai->value < -delta)
1336 /* new set overrides old set */
1342 if (ACTION_UPDATE == type)
1344 /* make delta cummulative */
1345 delta = (int64_t) value;
1350 /* drop old 'update', use new 'set' instead */
1355 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1356 ai->make_persistent = make_persistent;
1359 /* no existing entry matches, create a fresh one */
1360 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1362 ai->subsystem = GNUNET_strdup (h->subsystem);
1363 ai->name = GNUNET_strdup (name);
1364 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1365 ai->make_persistent = make_persistent;
1369 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
1371 schedule_action (h);
1376 * Set statistic value for the peer. Will always use our
1377 * subsystem (the argument used when "handle" was created).
1379 * @param handle identification of the statistics service
1380 * @param name name of the statistic value
1381 * @param value new value to set
1382 * @param make_persistent should the value be kept across restarts?
1385 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1386 const char *name, uint64_t value, int make_persistent)
1390 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1391 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
1396 * Set statistic value for the peer. Will always use our
1397 * subsystem (the argument used when "handle" was created).
1399 * @param handle identification of the statistics service
1400 * @param name name of the statistic value
1401 * @param delta change in value (added to existing value)
1402 * @param make_persistent should the value be kept across restarts?
1405 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1406 const char *name, int64_t delta, int make_persistent)
1412 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1413 add_setter_action (handle, name, make_persistent, (uint64_t) delta,
1418 /* end of statistics_api.c */