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))
403 c->cont (c->cls, GNUNET_SYSERR);
404 free_action_item (c);
410 * Try to (re)connect to the statistics service.
412 * @param h statistics handle to reconnect
413 * @return GNUNET_YES on success, GNUNET_NO on failure.
416 try_connect (struct GNUNET_STATISTICS_Handle *h)
418 struct GNUNET_STATISTICS_GetHandle *gh;
419 struct GNUNET_STATISTICS_GetHandle *gn;
422 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
424 if (NULL != h->client)
426 h->client = GNUNET_CLIENT_connect ("statistics", h->cfg);
427 if (NULL != h->client)
430 while (NULL != (gh = gn))
433 if (gh->type == ACTION_WATCH)
435 GNUNET_CONTAINER_DLL_remove (h->action_head,
438 free_action_item (gh);
441 for (i = 0; i < h->watches_size; i++)
443 if (NULL != h->watches[i])
444 schedule_watch_request (h, h->watches[i]);
448 LOG (GNUNET_ERROR_TYPE_DEBUG,
449 "Failed to connect to statistics service!\n");
455 * We've waited long enough, reconnect now.
457 * @param cls the 'struct GNUNET_STATISTICS_Handle' to reconnect
458 * @param tc scheduler context (unused)
461 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
463 struct GNUNET_STATISTICS_Handle *h = cls;
465 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
471 * Task used by 'reconnect_later' to shutdown the handle
473 * @param cls the statistics handle
474 * @param tc scheduler context
477 do_destroy (void *cls,
478 const struct GNUNET_SCHEDULER_TaskContext *tc)
480 struct GNUNET_STATISTICS_Handle *h = cls;
482 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
487 * Reconnect at a later time, respecting back-off.
489 * @param h statistics handle
492 reconnect_later (struct GNUNET_STATISTICS_Handle *h)
495 struct GNUNET_STATISTICS_GetHandle *gh;
497 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->backoff_task);
498 if (GNUNET_YES == h->do_destroy)
500 /* So we are shutting down and the service is not reachable.
501 * Chances are that it's down for good and we are not going to connect to
503 * Give up and don't sync the rest of the data.
506 for (gh = h->action_head; NULL != gh; gh = gh->next)
507 if ( (gh->make_persistent) && (ACTION_SET == gh->type) )
509 if (GNUNET_YES == loss)
510 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
511 _("Could not save some persistent statistics\n"));
512 h->do_destroy = GNUNET_NO;
513 GNUNET_SCHEDULER_add_continuation (&do_destroy, h,
514 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
518 GNUNET_SCHEDULER_add_delayed (h->backoff, &reconnect_task, h);
519 h->backoff = GNUNET_TIME_STD_BACKOFF (h->backoff);
524 * Process a 'GNUNET_MESSAGE_TYPE_STATISTICS_VALUE' message.
526 * @param h statistics handle
527 * @param msg message received from the service, never NULL
528 * @return GNUNET_OK if the message was well-formed
531 process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
532 const struct GNUNET_MessageHeader *msg)
536 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
539 if (h->current->aborted)
541 LOG (GNUNET_ERROR_TYPE_DEBUG, "Iteration was aborted, ignoring VALUE\n");
542 return GNUNET_OK; /* don't bother */
544 size = ntohs (msg->size);
545 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
548 return GNUNET_SYSERR;
550 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
551 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
553 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], size, 2,
557 return GNUNET_SYSERR;
559 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received valid statistic on `%s:%s': %llu\n",
560 service, name, GNUNET_ntohll (smsg->value));
562 h->current->proc (h->current->cls, service, name,
563 GNUNET_ntohll (smsg->value),
565 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
567 LOG (GNUNET_ERROR_TYPE_DEBUG,
568 "Processing of remaining statistics aborted by client.\n");
569 h->current->aborted = GNUNET_YES;
571 LOG (GNUNET_ERROR_TYPE_DEBUG, "VALUE processed successfully\n");
577 * We have received a watch value from the service. Process it.
579 * @param h statistics handle
580 * @param msg the watch value message
581 * @return GNUNET_OK if the message was well-formed, GNUNET_SYSERR if not,
582 * GNUNET_NO if this watch has been cancelled
585 process_watch_value (struct GNUNET_STATISTICS_Handle *h,
586 const struct GNUNET_MessageHeader *msg)
588 const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
589 struct GNUNET_STATISTICS_WatchEntry *w;
592 if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) != ntohs (msg->size))
595 return GNUNET_SYSERR;
597 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
598 GNUNET_break (0 == ntohl (wvm->reserved));
599 wid = ntohl (wvm->wid);
600 if (wid >= h->watches_size)
603 return GNUNET_SYSERR;
608 (void) w->proc (w->proc_cls, w->subsystem, w->name,
609 GNUNET_ntohll (wvm->value),
610 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
616 destroy_task (void *cls,
617 const struct GNUNET_SCHEDULER_TaskContext *tc)
619 struct GNUNET_STATISTICS_Handle *h = cls;
621 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
626 * Function called with messages from stats service.
629 * @param msg message received, NULL on timeout or fatal error
632 receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
634 struct GNUNET_STATISTICS_Handle *h = cls;
635 struct GNUNET_STATISTICS_GetHandle *c;
640 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
641 "Error receiving statistics from service, is the service running?\n");
646 switch (ntohs (msg->type))
648 case GNUNET_MESSAGE_TYPE_TEST:
649 if (GNUNET_SYSERR != h->do_destroy)
651 /* not in shutdown, why do we get 'TEST'? */
657 h->do_destroy = GNUNET_NO;
658 GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
659 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
661 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
662 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received end of statistics marker\n");
663 if (NULL == (c = h->current))
670 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
671 if (h->watches_size > 0)
673 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
674 GNUNET_TIME_UNIT_FOREVER_REL);
678 h->receiving = GNUNET_NO;
683 c->cont (c->cls, GNUNET_OK);
684 free_action_item (c);
686 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
687 if (GNUNET_OK != process_statistics_value_message (h, msg))
693 /* finally, look for more! */
694 LOG (GNUNET_ERROR_TYPE_DEBUG,
695 "Processing VALUE done, now reading more\n");
696 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
697 GNUNET_TIME_absolute_get_remaining (h->
699 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
701 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
703 (ret = process_watch_value (h, msg)))
706 if (GNUNET_NO == ret)
707 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
711 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
712 GNUNET_assert (h->watches_size > 0);
713 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
714 GNUNET_TIME_UNIT_FOREVER_REL);
726 * Transmit a GET request (and if successful, start to receive
729 * @param handle statistics handle
730 * @param size how many bytes can we write to buf
731 * @param buf where to write requests to the service
732 * @return number of bytes written to buf
735 transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
737 struct GNUNET_STATISTICS_GetHandle *c;
738 struct GNUNET_MessageHeader *hdr;
743 GNUNET_assert (NULL != (c = handle->current));
746 /* timeout / error */
747 LOG (GNUNET_ERROR_TYPE_DEBUG,
748 "Transmission of request for statistics failed!\n");
749 do_disconnect (handle);
750 reconnect_later (handle);
753 slen1 = strlen (c->subsystem) + 1;
754 slen2 = strlen (c->name) + 1;
755 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
756 GNUNET_assert (msize <= size);
757 hdr = (struct GNUNET_MessageHeader *) buf;
758 hdr->size = htons (msize);
759 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
760 GNUNET_assert (slen1 + slen2 ==
761 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
764 if (GNUNET_YES != handle->receiving)
766 LOG (GNUNET_ERROR_TYPE_DEBUG,
767 "Transmission of GET done, now reading response\n");
768 handle->receiving = GNUNET_YES;
769 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
770 GNUNET_TIME_absolute_get_remaining (c->timeout));
777 * Transmit a WATCH request (and if successful, start to receive
780 * @param handle statistics handle
781 * @param size how many bytes can we write to buf
782 * @param buf where to write requests to the service
783 * @return number of bytes written to buf
786 transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
788 struct GNUNET_MessageHeader *hdr;
795 /* timeout / error */
796 LOG (GNUNET_ERROR_TYPE_DEBUG,
797 "Transmission of request for statistics failed!\n");
798 do_disconnect (handle);
799 reconnect_later (handle);
802 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting watch request for `%s'\n",
803 handle->current->name);
804 slen1 = strlen (handle->current->subsystem) + 1;
805 slen2 = strlen (handle->current->name) + 1;
806 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
807 GNUNET_assert (msize <= size);
808 hdr = (struct GNUNET_MessageHeader *) buf;
809 hdr->size = htons (msize);
810 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
811 GNUNET_assert (slen1 + slen2 ==
812 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
813 handle->current->subsystem,
814 handle->current->name));
815 if (GNUNET_YES != handle->receiving)
817 handle->receiving = GNUNET_YES;
818 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
819 GNUNET_TIME_UNIT_FOREVER_REL);
821 GNUNET_assert (NULL == handle->current->cont);
822 free_action_item (handle->current);
823 handle->current = NULL;
829 * Transmit a SET/UPDATE request.
831 * @param handle statistics handle
832 * @param size how many bytes can we write to buf
833 * @param buf where to write requests to the service
834 * @return number of bytes written to buf
837 transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
839 struct GNUNET_STATISTICS_SetMessage *r;
846 do_disconnect (handle);
847 reconnect_later (handle);
850 slen = strlen (handle->current->subsystem) + 1;
851 nlen = strlen (handle->current->name) + 1;
852 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
856 do_disconnect (handle);
857 reconnect_later (handle);
861 r->header.size = htons (nsize);
862 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
864 r->value = GNUNET_htonll (handle->current->value);
865 if (handle->current->make_persistent)
866 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
867 if (handle->current->type == ACTION_UPDATE)
868 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
869 GNUNET_assert (slen + nlen ==
870 GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
871 handle->current->subsystem,
872 handle->current->name));
873 GNUNET_assert (NULL == handle->current->cont);
874 free_action_item (handle->current);
875 handle->current = NULL;
876 update_memory_statistics (handle);
882 * Function called when we are ready to transmit a request to the service.
884 * @param cls the 'struct GNUNET_STATISTICS_Handle'
885 * @param size how many bytes can we write to buf
886 * @param buf where to write requests to the service
887 * @return number of bytes written to buf
890 transmit_action (void *cls, size_t size, void *buf)
892 struct GNUNET_STATISTICS_Handle *h = cls;
897 if (NULL != h->current)
898 switch (h->current->type)
901 ret = transmit_get (h, size, buf);
905 ret = transmit_set (h, size, buf);
908 ret = transmit_watch (h, size, buf);
920 * Get handle for the statistics service.
922 * @param subsystem name of subsystem using the service
923 * @param cfg services configuration in use
924 * @return handle to use
926 struct GNUNET_STATISTICS_Handle *
927 GNUNET_STATISTICS_create (const char *subsystem,
928 const struct GNUNET_CONFIGURATION_Handle *cfg)
930 struct GNUNET_STATISTICS_Handle *ret;
933 GNUNET_CONFIGURATION_get_value_yesno (cfg, "statistics", "DISABLE"))
935 GNUNET_assert (NULL != subsystem);
936 GNUNET_assert (NULL != cfg);
937 ret = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_Handle));
939 ret->subsystem = GNUNET_strdup (subsystem);
940 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
946 * Destroy a handle (free all state associated with
949 * @param h statistics handle to destroy
950 * @param sync_first set to GNUNET_YES if pending SET requests should
954 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
956 struct GNUNET_STATISTICS_GetHandle *pos;
957 struct GNUNET_STATISTICS_GetHandle *next;
958 struct GNUNET_TIME_Relative timeout;
963 GNUNET_assert (GNUNET_NO == h->do_destroy); // Don't call twice.
964 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
966 GNUNET_SCHEDULER_cancel (h->backoff_task);
967 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
971 if (NULL != h->current)
973 if (ACTION_GET == h->current->type)
975 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
977 free_action_item (h->current);
981 next = h->action_head;
982 while (NULL != (pos = next))
985 if (ACTION_GET == pos->type)
987 GNUNET_CONTAINER_DLL_remove (h->action_head,
990 free_action_item (pos);
993 if ( (NULL == h->current) &&
994 (NULL != (h->current = h->action_head)) )
995 GNUNET_CONTAINER_DLL_remove (h->action_head,
998 h->do_destroy = GNUNET_YES;
999 if ((NULL != h->current) && (NULL == h->th) &&
1000 (NULL != h->client))
1002 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
1004 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1005 timeout, GNUNET_YES,
1006 &transmit_action, h);
1007 GNUNET_assert (NULL != h->th);
1010 return; /* do not finish destruction just yet */
1012 while (NULL != (pos = h->action_head))
1014 GNUNET_CONTAINER_DLL_remove (h->action_head,
1017 free_action_item (pos);
1020 for (i = 0; i < h->watches_size; i++)
1022 if (NULL == h->watches[i])
1024 GNUNET_free (h->watches[i]->subsystem);
1025 GNUNET_free (h->watches[i]->name);
1026 GNUNET_free (h->watches[i]);
1028 GNUNET_array_grow (h->watches, h->watches_size, 0);
1029 GNUNET_free (h->subsystem);
1035 * Function called to transmit TEST message to service to
1036 * confirm that the service has received all of our 'SET'
1037 * messages (during statistics disconnect/shutdown).
1039 * @param cls the 'struct GNUNET_STATISTICS_Handle'
1040 * @param size how many bytes can we write to buf
1041 * @param buf where to write requests to the service
1042 * @return number of bytes written to buf
1045 transmit_test_on_shutdown (void *cls,
1049 struct GNUNET_STATISTICS_Handle *h = cls;
1050 struct GNUNET_MessageHeader hdr;
1055 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1056 _("Failed to receive acknowledgement from statistics service, some statistics might have been lost!\n"));
1057 h->do_destroy = GNUNET_NO;
1058 GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
1059 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1062 hdr.type = htons (GNUNET_MESSAGE_TYPE_TEST);
1063 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
1064 memcpy (buf, &hdr, sizeof (hdr));
1065 if (GNUNET_YES != h->receiving)
1067 h->receiving = GNUNET_YES;
1068 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
1069 GNUNET_TIME_UNIT_FOREVER_REL);
1071 return sizeof (struct GNUNET_MessageHeader);
1076 * Schedule the next action to be performed.
1078 * @param h statistics handle
1081 schedule_action (struct GNUNET_STATISTICS_Handle *h)
1083 struct GNUNET_TIME_Relative timeout;
1085 if ( (NULL != h->th) ||
1086 (GNUNET_SCHEDULER_NO_TASK != h->backoff_task) )
1087 return; /* action already pending */
1088 if (GNUNET_YES != try_connect (h))
1090 reconnect_later (h);
1093 if (NULL != h->current)
1094 return; /* action already pending */
1095 /* schedule next action */
1096 h->current = h->action_head;
1097 if (NULL == h->current)
1099 if (GNUNET_YES == h->do_destroy)
1101 h->do_destroy = GNUNET_SYSERR; /* in 'TEST' mode */
1102 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
1103 sizeof (struct GNUNET_MessageHeader),
1104 SET_TRANSMIT_TIMEOUT,
1106 &transmit_test_on_shutdown, h);
1110 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
1111 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
1114 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1115 timeout, GNUNET_YES,
1116 &transmit_action, h)))
1118 LOG (GNUNET_ERROR_TYPE_DEBUG,
1119 "Failed to transmit request to statistics service.\n");
1121 reconnect_later (h);
1127 * Get statistic from the peer.
1129 * @param handle identification of the statistics service
1130 * @param subsystem limit to the specified subsystem, NULL for our subsystem
1131 * @param name name of the statistic value, NULL for all values
1132 * @param timeout after how long should we give up (and call
1133 * cont with an error code)?
1134 * @param cont continuation to call when done (can be NULL)
1135 * This callback CANNOT destroy the statistics handle in the same call.
1136 * @param proc function to call on each value
1137 * @param cls closure for cont and proc
1138 * @return NULL on error
1140 struct GNUNET_STATISTICS_GetHandle *
1141 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
1142 const char *subsystem, const char *name,
1143 struct GNUNET_TIME_Relative timeout,
1144 GNUNET_STATISTICS_Callback cont,
1145 GNUNET_STATISTICS_Iterator proc, void *cls)
1149 struct GNUNET_STATISTICS_GetHandle *ai;
1153 GNUNET_assert (NULL != proc);
1154 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1155 if (NULL == subsystem)
1159 slen1 = strlen (subsystem) + 1;
1160 slen2 = strlen (name) + 1;
1161 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
1162 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1163 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1165 ai->subsystem = GNUNET_strdup (subsystem);
1166 ai->name = GNUNET_strdup (name);
1170 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1171 ai->type = ACTION_GET;
1172 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
1173 GNUNET_CONTAINER_DLL_insert_tail (handle->action_head, handle->action_tail,
1175 schedule_action (handle);
1181 * Cancel a 'get' request. Must be called before the 'cont'
1182 * function is called.
1184 * @param gh handle of the request to cancel
1187 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
1191 if (gh->sh->current == gh)
1193 gh->aborted = GNUNET_YES;
1197 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail, gh);
1198 GNUNET_free (gh->name);
1199 GNUNET_free (gh->subsystem);
1206 * Watch statistics from the peer (be notified whenever they change).
1208 * @param handle identification of the statistics service
1209 * @param subsystem limit to the specified subsystem, never NULL
1210 * @param name name of the statistic value, never NULL
1211 * @param proc function to call on each value
1212 * @param proc_cls closure for proc
1213 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1216 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1217 const char *subsystem, const char *name,
1218 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1220 struct GNUNET_STATISTICS_WatchEntry *w;
1223 return GNUNET_SYSERR;
1224 w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry));
1225 w->subsystem = GNUNET_strdup (subsystem);
1226 w->name = GNUNET_strdup (name);
1228 w->proc_cls = proc_cls;
1229 GNUNET_array_append (handle->watches, handle->watches_size, w);
1230 schedule_watch_request (handle, w);
1236 * Stop watching statistics from the peer.
1238 * @param handle identification of the statistics service
1239 * @param subsystem limit to the specified subsystem, never NULL
1240 * @param name name of the statistic value, never NULL
1241 * @param proc function to call on each value
1242 * @param proc_cls closure for proc
1243 * @return GNUNET_OK on success, GNUNET_SYSERR on error (no such watch)
1246 GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
1247 const char *subsystem, const char *name,
1248 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1250 struct GNUNET_STATISTICS_WatchEntry *w;
1254 return GNUNET_SYSERR;
1255 for (i=0;i<handle->watches_size;i++)
1257 w = handle->watches[i];
1260 if ( (w->proc == proc) &&
1261 (w->proc_cls == proc_cls) &&
1262 (0 == strcmp (w->name, name)) &&
1263 (0 == strcmp (w->subsystem, subsystem)) )
1265 GNUNET_free (w->name);
1266 GNUNET_free (w->subsystem);
1268 handle->watches[i] = NULL;
1272 return GNUNET_SYSERR;
1278 * Queue a request to change a statistic.
1280 * @param h statistics handle
1281 * @param name name of the value
1282 * @param make_persistent should the value be kept across restarts?
1283 * @param value new value or change
1284 * @param type type of the action (ACTION_SET or ACTION_UPDATE)
1287 add_setter_action (struct GNUNET_STATISTICS_Handle *h, const char *name,
1288 int make_persistent, uint64_t value, enum ActionType type)
1290 struct GNUNET_STATISTICS_GetHandle *ai;
1296 GNUNET_assert (NULL != h);
1297 GNUNET_assert (NULL != name);
1298 slen = strlen (h->subsystem) + 1;
1299 nlen = strlen (name) + 1;
1300 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1301 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1306 for (ai = h->action_head; NULL != ai; ai = ai->next)
1308 if (! ( (0 == strcmp (ai->subsystem, h->subsystem)) &&
1309 (0 == strcmp (ai->name, name)) &&
1310 ( (ACTION_UPDATE == ai->type) ||
1311 (ACTION_SET == ai->type) ) ) )
1313 if (ACTION_SET == ai->type)
1315 if (ACTION_UPDATE == type)
1317 delta = (int64_t) value;
1320 /* update old set by new delta */
1325 /* update old set by new delta, but never go negative */
1326 if (ai->value < -delta)
1334 /* new set overrides old set */
1340 if (ACTION_UPDATE == type)
1342 /* make delta cummulative */
1343 delta = (int64_t) value;
1348 /* drop old 'update', use new 'set' instead */
1353 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1354 ai->make_persistent = make_persistent;
1357 /* no existing entry matches, create a fresh one */
1358 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1360 ai->subsystem = GNUNET_strdup (h->subsystem);
1361 ai->name = GNUNET_strdup (name);
1362 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1363 ai->make_persistent = make_persistent;
1367 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
1369 schedule_action (h);
1374 * Set statistic value for the peer. Will always use our
1375 * subsystem (the argument used when "handle" was created).
1377 * @param handle identification of the statistics service
1378 * @param name name of the statistic value
1379 * @param value new value to set
1380 * @param make_persistent should the value be kept across restarts?
1383 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1384 const char *name, uint64_t value, int make_persistent)
1388 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1389 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
1394 * Set statistic value for the peer. Will always use our
1395 * subsystem (the argument used when "handle" was created).
1397 * @param handle identification of the statistics service
1398 * @param name name of the statistic value
1399 * @param delta change in value (added to existing value)
1400 * @param make_persistent should the value be kept across restarts?
1403 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1404 const char *name, int64_t delta, int make_persistent)
1410 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1411 add_setter_action (handle, name, make_persistent, (uint64_t) delta,
1416 /* end of statistics_api.c */