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;
155 * Flag for SET/UPDATE actions.
160 * Has the current iteration been aborted; for GET actions.
165 * Is this a GET, SET, UPDATE or WATCH?
167 enum ActionType type;
170 * Size of the message that we will be transmitting.
178 * Handle for the service.
180 struct GNUNET_STATISTICS_Handle
183 * Name of our subsystem.
188 * Configuration to use.
190 const struct GNUNET_CONFIGURATION_Handle *cfg;
193 * Socket (if available).
195 struct GNUNET_CLIENT_Connection *client;
198 * Currently pending transmission request.
200 struct GNUNET_CLIENT_TransmitHandle *th;
203 * Head of the linked list of pending actions (first action
206 struct GNUNET_STATISTICS_GetHandle *action_head;
209 * Tail of the linked list of actions (for fast append).
211 struct GNUNET_STATISTICS_GetHandle *action_tail;
214 * Action we are currently busy with (action request has been
215 * transmitted, we're now receiving the response from the
218 struct GNUNET_STATISTICS_GetHandle *current;
221 * Array of watch entries.
223 struct GNUNET_STATISTICS_WatchEntry **watches;
226 * Task doing exponential back-off trying to reconnect.
228 GNUNET_SCHEDULER_TaskIdentifier backoff_task;
231 * Time for next connect retry.
233 struct GNUNET_TIME_Relative backoff;
236 * Maximum heap size observed so far (if available).
238 uint64_t peak_heap_size;
241 * Maximum resident set side observed so far (if available).
246 * Size of the 'watches' array.
248 unsigned int watches_size;
251 * Should this handle auto-destruct once all actions have
257 * Are we currently receiving from the service?
265 * Obtain statistics about this process's memory consumption and
266 * report those as well (if they changed).
269 update_memory_statistics (struct GNUNET_STATISTICS_Handle *h)
271 #if ENABLE_HEAP_STATISTICS
272 uint64_t current_heap_size = 0;
273 uint64_t current_rss = 0;
275 if (GNUNET_NO != h->do_destroy)
282 current_heap_size = mi.uordblks + mi.fordblks;
289 if (0 == getrusage (RUSAGE_SELF, &ru))
291 current_rss = 1024LL * ru.ru_maxrss;
295 if (current_heap_size > h->peak_heap_size)
297 h->peak_heap_size = current_heap_size;
298 GNUNET_STATISTICS_set (h, "# peak heap size", current_heap_size, GNUNET_NO);
300 if (current_rss > h->peak_rss)
302 h->peak_rss = current_rss;
303 GNUNET_STATISTICS_set (h, "# peak resident set size", current_rss, GNUNET_NO);
310 * Schedule the next action to be performed.
312 * @param h statistics handle to reconnect
315 schedule_action (struct GNUNET_STATISTICS_Handle *h);
319 * Transmit request to service that we want to watch
320 * the development of a particular value.
322 * @param h statistics handle
323 * @param watch watch entry of the value to watch
326 schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
327 struct GNUNET_STATISTICS_WatchEntry *watch)
330 struct GNUNET_STATISTICS_GetHandle *ai;
335 GNUNET_assert (NULL != h);
336 GNUNET_assert (NULL != watch);
338 slen = strlen (watch->subsystem) + 1;
339 nlen = strlen (watch->name) + 1;
340 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
341 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
346 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
348 ai->subsystem = GNUNET_strdup (watch->subsystem);
349 ai->name = GNUNET_strdup (watch->name);
350 ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
352 ai->type = ACTION_WATCH;
353 ai->proc = watch->proc;
354 ai->cls = watch->proc_cls;
355 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
362 * Free memory associated with the given action item.
364 * @param gh action item to free
367 free_action_item (struct GNUNET_STATISTICS_GetHandle *gh)
369 GNUNET_free_non_null (gh->subsystem);
370 GNUNET_free_non_null (gh->name);
376 * Disconnect from the statistics service.
378 * @param h statistics handle to disconnect from
381 do_disconnect (struct GNUNET_STATISTICS_Handle *h)
383 struct GNUNET_STATISTICS_GetHandle *c;
387 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
390 h->receiving = GNUNET_NO;
391 if (NULL != (c = h->current))
394 if ( (NULL != c->cont) &&
395 (GNUNET_YES != c->aborted) )
396 c->cont (c->cls, GNUNET_SYSERR);
397 free_action_item (c);
399 if (NULL != h->client)
401 GNUNET_CLIENT_disconnect (h->client);
408 * Try to (re)connect to the statistics service.
410 * @param h statistics handle to reconnect
411 * @return #GNUNET_YES on success, #GNUNET_NO on failure.
414 try_connect (struct GNUNET_STATISTICS_Handle *h)
416 struct GNUNET_STATISTICS_GetHandle *gh;
417 struct GNUNET_STATISTICS_GetHandle *gn;
420 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
422 if (NULL != h->client)
424 h->client = GNUNET_CLIENT_connect ("statistics", h->cfg);
425 if (NULL != h->client)
428 while (NULL != (gh = gn))
431 if (gh->type == ACTION_WATCH)
433 GNUNET_CONTAINER_DLL_remove (h->action_head,
436 free_action_item (gh);
439 for (i = 0; i < h->watches_size; i++)
441 if (NULL != h->watches[i])
442 schedule_watch_request (h, h->watches[i]);
446 LOG (GNUNET_ERROR_TYPE_DEBUG,
447 "Failed to connect to statistics service!\n");
453 * We've waited long enough, reconnect now.
455 * @param cls the 'struct GNUNET_STATISTICS_Handle' to reconnect
456 * @param tc scheduler context (unused)
459 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
461 struct GNUNET_STATISTICS_Handle *h = cls;
463 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
469 * Task used by 'reconnect_later' to shutdown the handle
471 * @param cls the statistics handle
472 * @param tc scheduler context
475 do_destroy (void *cls,
476 const struct GNUNET_SCHEDULER_TaskContext *tc)
478 struct GNUNET_STATISTICS_Handle *h = cls;
480 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
485 * Reconnect at a later time, respecting back-off.
487 * @param h statistics handle
490 reconnect_later (struct GNUNET_STATISTICS_Handle *h)
493 struct GNUNET_STATISTICS_GetHandle *gh;
495 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->backoff_task);
496 if (GNUNET_YES == h->do_destroy)
498 /* So we are shutting down and the service is not reachable.
499 * Chances are that it's down for good and we are not going to connect to
501 * Give up and don't sync the rest of the data.
504 for (gh = h->action_head; NULL != gh; gh = gh->next)
505 if ( (gh->make_persistent) && (ACTION_SET == gh->type) )
507 if (GNUNET_YES == loss)
508 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
509 _("Could not save some persistent statistics\n"));
510 h->do_destroy = GNUNET_NO;
511 GNUNET_SCHEDULER_add_continuation (&do_destroy, h,
512 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
516 GNUNET_SCHEDULER_add_delayed (h->backoff, &reconnect_task, h);
517 h->backoff = GNUNET_TIME_STD_BACKOFF (h->backoff);
522 * Process a 'GNUNET_MESSAGE_TYPE_STATISTICS_VALUE' message.
524 * @param h statistics handle
525 * @param msg message received from the service, never NULL
526 * @return GNUNET_OK if the message was well-formed
529 process_statistics_value_message (struct GNUNET_STATISTICS_Handle *h,
530 const struct GNUNET_MessageHeader *msg)
534 const struct GNUNET_STATISTICS_ReplyMessage *smsg;
537 if (h->current->aborted)
539 LOG (GNUNET_ERROR_TYPE_DEBUG, "Iteration was aborted, ignoring VALUE\n");
540 return GNUNET_OK; /* don't bother */
542 size = ntohs (msg->size);
543 if (size < sizeof (struct GNUNET_STATISTICS_ReplyMessage))
546 return GNUNET_SYSERR;
548 smsg = (const struct GNUNET_STATISTICS_ReplyMessage *) msg;
549 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
551 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1], size, 2,
555 return GNUNET_SYSERR;
557 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received valid statistic on `%s:%s': %llu\n",
558 service, name, GNUNET_ntohll (smsg->value));
560 h->current->proc (h->current->cls, service, name,
561 GNUNET_ntohll (smsg->value),
563 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
565 LOG (GNUNET_ERROR_TYPE_DEBUG,
566 "Processing of remaining statistics aborted by client.\n");
567 h->current->aborted = GNUNET_YES;
569 LOG (GNUNET_ERROR_TYPE_DEBUG, "VALUE processed successfully\n");
575 * We have received a watch value from the service. Process it.
577 * @param h statistics handle
578 * @param msg the watch value message
579 * @return GNUNET_OK if the message was well-formed, GNUNET_SYSERR if not,
580 * GNUNET_NO if this watch has been cancelled
583 process_watch_value (struct GNUNET_STATISTICS_Handle *h,
584 const struct GNUNET_MessageHeader *msg)
586 const struct GNUNET_STATISTICS_WatchValueMessage *wvm;
587 struct GNUNET_STATISTICS_WatchEntry *w;
590 if (sizeof (struct GNUNET_STATISTICS_WatchValueMessage) != ntohs (msg->size))
593 return GNUNET_SYSERR;
595 wvm = (const struct GNUNET_STATISTICS_WatchValueMessage *) msg;
596 GNUNET_break (0 == ntohl (wvm->reserved));
597 wid = ntohl (wvm->wid);
598 if (wid >= h->watches_size)
601 return GNUNET_SYSERR;
606 (void) w->proc (w->proc_cls, w->subsystem, w->name,
607 GNUNET_ntohll (wvm->value),
608 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
614 destroy_task (void *cls,
615 const struct GNUNET_SCHEDULER_TaskContext *tc)
617 struct GNUNET_STATISTICS_Handle *h = cls;
619 GNUNET_STATISTICS_destroy (h, GNUNET_NO);
624 * Function called with messages from stats service.
627 * @param msg message received, NULL on timeout or fatal error
630 receive_stats (void *cls, const struct GNUNET_MessageHeader *msg)
632 struct GNUNET_STATISTICS_Handle *h = cls;
633 struct GNUNET_STATISTICS_GetHandle *c;
638 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
639 "Error receiving statistics from service, is the service running?\n");
644 switch (ntohs (msg->type))
646 case GNUNET_MESSAGE_TYPE_TEST:
647 if (GNUNET_SYSERR != h->do_destroy)
649 /* not in shutdown, why do we get 'TEST'? */
655 h->do_destroy = GNUNET_NO;
656 GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
657 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
659 case GNUNET_MESSAGE_TYPE_STATISTICS_END:
660 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received end of statistics marker\n");
661 if (NULL == (c = h->current))
668 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
669 if (h->watches_size > 0)
671 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
672 GNUNET_TIME_UNIT_FOREVER_REL);
676 h->receiving = GNUNET_NO;
681 c->cont (c->cls, GNUNET_OK);
682 free_action_item (c);
684 case GNUNET_MESSAGE_TYPE_STATISTICS_VALUE:
685 if (GNUNET_OK != process_statistics_value_message (h, msg))
691 /* finally, look for more! */
692 LOG (GNUNET_ERROR_TYPE_DEBUG,
693 "Processing VALUE done, now reading more\n");
694 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
695 GNUNET_TIME_absolute_get_remaining (h->
697 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
699 case GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE:
701 (ret = process_watch_value (h, msg)))
704 if (GNUNET_NO == ret)
705 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
709 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
710 GNUNET_assert (h->watches_size > 0);
711 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
712 GNUNET_TIME_UNIT_FOREVER_REL);
724 * Transmit a GET request (and if successful, start to receive
727 * @param handle statistics handle
728 * @param size how many bytes can we write to buf
729 * @param buf where to write requests to the service
730 * @return number of bytes written to buf
733 transmit_get (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
735 struct GNUNET_STATISTICS_GetHandle *c;
736 struct GNUNET_MessageHeader *hdr;
741 GNUNET_assert (NULL != (c = handle->current));
744 /* timeout / error */
745 LOG (GNUNET_ERROR_TYPE_DEBUG,
746 "Transmission of request for statistics failed!\n");
747 do_disconnect (handle);
748 reconnect_later (handle);
751 slen1 = strlen (c->subsystem) + 1;
752 slen2 = strlen (c->name) + 1;
753 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
754 GNUNET_assert (msize <= size);
755 hdr = (struct GNUNET_MessageHeader *) buf;
756 hdr->size = htons (msize);
757 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_GET);
758 GNUNET_assert (slen1 + slen2 ==
759 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
762 if (GNUNET_YES != handle->receiving)
764 LOG (GNUNET_ERROR_TYPE_DEBUG,
765 "Transmission of GET done, now reading response\n");
766 handle->receiving = GNUNET_YES;
767 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
768 GNUNET_TIME_absolute_get_remaining (c->timeout));
775 * Transmit a WATCH request (and if successful, start to receive
778 * @param handle statistics handle
779 * @param size how many bytes can we write to buf
780 * @param buf where to write requests to the service
781 * @return number of bytes written to buf
784 transmit_watch (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
786 struct GNUNET_MessageHeader *hdr;
793 /* timeout / error */
794 LOG (GNUNET_ERROR_TYPE_DEBUG,
795 "Transmission of request for statistics failed!\n");
796 do_disconnect (handle);
797 reconnect_later (handle);
800 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting watch request for `%s'\n",
801 handle->current->name);
802 slen1 = strlen (handle->current->subsystem) + 1;
803 slen2 = strlen (handle->current->name) + 1;
804 msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
805 GNUNET_assert (msize <= size);
806 hdr = (struct GNUNET_MessageHeader *) buf;
807 hdr->size = htons (msize);
808 hdr->type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
809 GNUNET_assert (slen1 + slen2 ==
810 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1], slen1 + slen2, 2,
811 handle->current->subsystem,
812 handle->current->name));
813 if (GNUNET_YES != handle->receiving)
815 handle->receiving = GNUNET_YES;
816 GNUNET_CLIENT_receive (handle->client, &receive_stats, handle,
817 GNUNET_TIME_UNIT_FOREVER_REL);
819 GNUNET_assert (NULL == handle->current->cont);
820 free_action_item (handle->current);
821 handle->current = NULL;
827 * Transmit a SET/UPDATE request.
829 * @param handle statistics handle
830 * @param size how many bytes can we write to buf
831 * @param buf where to write requests to the service
832 * @return number of bytes written to buf
835 transmit_set (struct GNUNET_STATISTICS_Handle *handle, size_t size, void *buf)
837 struct GNUNET_STATISTICS_SetMessage *r;
844 do_disconnect (handle);
845 reconnect_later (handle);
848 slen = strlen (handle->current->subsystem) + 1;
849 nlen = strlen (handle->current->name) + 1;
850 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
854 do_disconnect (handle);
855 reconnect_later (handle);
859 r->header.size = htons (nsize);
860 r->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
862 r->value = GNUNET_htonll (handle->current->value);
863 if (handle->current->make_persistent)
864 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
865 if (handle->current->type == ACTION_UPDATE)
866 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
867 GNUNET_assert (slen + nlen ==
868 GNUNET_STRINGS_buffer_fill ((char *) &r[1], slen + nlen, 2,
869 handle->current->subsystem,
870 handle->current->name));
871 GNUNET_assert (NULL == handle->current->cont);
872 free_action_item (handle->current);
873 handle->current = NULL;
874 update_memory_statistics (handle);
880 * Function called when we are ready to transmit a request to the service.
882 * @param cls the 'struct GNUNET_STATISTICS_Handle'
883 * @param size how many bytes can we write to buf
884 * @param buf where to write requests to the service
885 * @return number of bytes written to buf
888 transmit_action (void *cls, size_t size, void *buf)
890 struct GNUNET_STATISTICS_Handle *h = cls;
895 if (NULL != h->current)
896 switch (h->current->type)
899 ret = transmit_get (h, size, buf);
903 ret = transmit_set (h, size, buf);
906 ret = transmit_watch (h, size, buf);
918 * Get handle for the statistics service.
920 * @param subsystem name of subsystem using the service
921 * @param cfg services configuration in use
922 * @return handle to use
924 struct GNUNET_STATISTICS_Handle *
925 GNUNET_STATISTICS_create (const char *subsystem,
926 const struct GNUNET_CONFIGURATION_Handle *cfg)
928 struct GNUNET_STATISTICS_Handle *ret;
931 GNUNET_CONFIGURATION_get_value_yesno (cfg, "statistics", "DISABLE"))
933 GNUNET_assert (NULL != subsystem);
934 GNUNET_assert (NULL != cfg);
935 ret = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_Handle));
937 ret->subsystem = GNUNET_strdup (subsystem);
938 ret->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
944 * Destroy a handle (free all state associated with
947 * @param h statistics handle to destroy
948 * @param sync_first set to GNUNET_YES if pending SET requests should
952 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h, int sync_first)
954 struct GNUNET_STATISTICS_GetHandle *pos;
955 struct GNUNET_STATISTICS_GetHandle *next;
956 struct GNUNET_TIME_Relative timeout;
961 GNUNET_assert (GNUNET_NO == h->do_destroy); // Don't call twice.
962 if (GNUNET_SCHEDULER_NO_TASK != h->backoff_task)
964 GNUNET_SCHEDULER_cancel (h->backoff_task);
965 h->backoff_task = GNUNET_SCHEDULER_NO_TASK;
969 if (NULL != h->current)
971 if (ACTION_GET == h->current->type)
973 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
975 free_action_item (h->current);
979 next = h->action_head;
980 while (NULL != (pos = next))
983 if (ACTION_GET == pos->type)
985 GNUNET_CONTAINER_DLL_remove (h->action_head,
988 free_action_item (pos);
991 if ( (NULL == h->current) &&
992 (NULL != (h->current = h->action_head)) )
993 GNUNET_CONTAINER_DLL_remove (h->action_head,
996 h->do_destroy = GNUNET_YES;
997 if ((NULL != h->current) && (NULL == h->th) &&
1000 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
1002 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1003 timeout, GNUNET_YES,
1004 &transmit_action, h);
1005 GNUNET_assert (NULL != h->th);
1008 return; /* do not finish destruction just yet */
1010 while (NULL != (pos = h->action_head))
1012 GNUNET_CONTAINER_DLL_remove (h->action_head,
1015 free_action_item (pos);
1018 for (i = 0; i < h->watches_size; i++)
1020 if (NULL == h->watches[i])
1022 GNUNET_free (h->watches[i]->subsystem);
1023 GNUNET_free (h->watches[i]->name);
1024 GNUNET_free (h->watches[i]);
1026 GNUNET_array_grow (h->watches, h->watches_size, 0);
1027 GNUNET_free (h->subsystem);
1033 * Function called to transmit TEST message to service to
1034 * confirm that the service has received all of our 'SET'
1035 * messages (during statistics disconnect/shutdown).
1037 * @param cls the 'struct GNUNET_STATISTICS_Handle'
1038 * @param size how many bytes can we write to buf
1039 * @param buf where to write requests to the service
1040 * @return number of bytes written to buf
1043 transmit_test_on_shutdown (void *cls,
1047 struct GNUNET_STATISTICS_Handle *h = cls;
1048 struct GNUNET_MessageHeader hdr;
1053 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1054 _("Failed to receive acknowledgement from statistics service, some statistics might have been lost!\n"));
1055 h->do_destroy = GNUNET_NO;
1056 GNUNET_SCHEDULER_add_continuation (&destroy_task, h,
1057 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1060 hdr.type = htons (GNUNET_MESSAGE_TYPE_TEST);
1061 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
1062 memcpy (buf, &hdr, sizeof (hdr));
1063 if (GNUNET_YES != h->receiving)
1065 h->receiving = GNUNET_YES;
1066 GNUNET_CLIENT_receive (h->client, &receive_stats, h,
1067 GNUNET_TIME_UNIT_FOREVER_REL);
1069 return sizeof (struct GNUNET_MessageHeader);
1074 * Schedule the next action to be performed.
1076 * @param h statistics handle
1079 schedule_action (struct GNUNET_STATISTICS_Handle *h)
1081 struct GNUNET_TIME_Relative timeout;
1083 if ( (NULL != h->th) ||
1084 (GNUNET_SCHEDULER_NO_TASK != h->backoff_task) )
1085 return; /* action already pending */
1086 if (GNUNET_YES != try_connect (h))
1088 reconnect_later (h);
1091 if (NULL != h->current)
1092 return; /* action already pending */
1093 /* schedule next action */
1094 h->current = h->action_head;
1095 if (NULL == h->current)
1097 if (GNUNET_YES == h->do_destroy)
1099 h->do_destroy = GNUNET_SYSERR; /* in 'TEST' mode */
1100 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
1101 sizeof (struct GNUNET_MessageHeader),
1102 SET_TRANSMIT_TIMEOUT,
1104 &transmit_test_on_shutdown, h);
1108 GNUNET_CONTAINER_DLL_remove (h->action_head, h->action_tail, h->current);
1109 timeout = GNUNET_TIME_absolute_get_remaining (h->current->timeout);
1112 GNUNET_CLIENT_notify_transmit_ready (h->client, h->current->msize,
1113 timeout, GNUNET_YES,
1114 &transmit_action, h)))
1116 LOG (GNUNET_ERROR_TYPE_DEBUG,
1117 "Failed to transmit request to statistics service.\n");
1119 reconnect_later (h);
1125 * Get statistic from the peer.
1127 * @param handle identification of the statistics service
1128 * @param subsystem limit to the specified subsystem, NULL for our subsystem
1129 * @param name name of the statistic value, NULL for all values
1130 * @param timeout after how long should we give up (and call
1131 * cont with an error code)?
1132 * @param cont continuation to call when done (can be NULL)
1133 * This callback CANNOT destroy the statistics handle in the same call.
1134 * @param proc function to call on each value
1135 * @param cls closure for cont and proc
1136 * @return NULL on error
1138 struct GNUNET_STATISTICS_GetHandle *
1139 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
1140 const char *subsystem, const char *name,
1141 struct GNUNET_TIME_Relative timeout,
1142 GNUNET_STATISTICS_Callback cont,
1143 GNUNET_STATISTICS_Iterator proc, void *cls)
1147 struct GNUNET_STATISTICS_GetHandle *ai;
1151 GNUNET_assert (NULL != proc);
1152 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1153 if (NULL == subsystem)
1157 slen1 = strlen (subsystem) + 1;
1158 slen2 = strlen (name) + 1;
1159 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
1160 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1161 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1163 ai->subsystem = GNUNET_strdup (subsystem);
1164 ai->name = GNUNET_strdup (name);
1168 ai->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1169 ai->type = ACTION_GET;
1170 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
1171 GNUNET_CONTAINER_DLL_insert_tail (handle->action_head, handle->action_tail,
1173 schedule_action (handle);
1179 * Cancel a 'get' request. Must be called before the 'cont'
1180 * function is called.
1182 * @param gh handle of the request to cancel
1185 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
1190 if (gh->sh->current == gh)
1192 gh->aborted = GNUNET_YES;
1196 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head, gh->sh->action_tail, gh);
1197 GNUNET_free (gh->name);
1198 GNUNET_free (gh->subsystem);
1205 * Watch statistics from the peer (be notified whenever they change).
1207 * @param handle identification of the statistics service
1208 * @param subsystem limit to the specified subsystem, never NULL
1209 * @param name name of the statistic value, never NULL
1210 * @param proc function to call on each value
1211 * @param proc_cls closure for proc
1212 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1215 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1216 const char *subsystem, const char *name,
1217 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1219 struct GNUNET_STATISTICS_WatchEntry *w;
1222 return GNUNET_SYSERR;
1223 w = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_WatchEntry));
1224 w->subsystem = GNUNET_strdup (subsystem);
1225 w->name = GNUNET_strdup (name);
1227 w->proc_cls = proc_cls;
1228 GNUNET_array_append (handle->watches, handle->watches_size, w);
1229 schedule_watch_request (handle, w);
1235 * Stop watching statistics from the peer.
1237 * @param handle identification of the statistics service
1238 * @param subsystem limit to the specified subsystem, never NULL
1239 * @param name name of the statistic value, never NULL
1240 * @param proc function to call on each value
1241 * @param proc_cls closure for proc
1242 * @return GNUNET_OK on success, GNUNET_SYSERR on error (no such watch)
1245 GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
1246 const char *subsystem, const char *name,
1247 GNUNET_STATISTICS_Iterator proc, void *proc_cls)
1249 struct GNUNET_STATISTICS_WatchEntry *w;
1253 return GNUNET_SYSERR;
1254 for (i=0;i<handle->watches_size;i++)
1256 w = handle->watches[i];
1259 if ( (w->proc == proc) &&
1260 (w->proc_cls == proc_cls) &&
1261 (0 == strcmp (w->name, name)) &&
1262 (0 == strcmp (w->subsystem, subsystem)) )
1264 GNUNET_free (w->name);
1265 GNUNET_free (w->subsystem);
1267 handle->watches[i] = NULL;
1271 return GNUNET_SYSERR;
1277 * Queue a request to change a statistic.
1279 * @param h statistics handle
1280 * @param name name of the value
1281 * @param make_persistent should the value be kept across restarts?
1282 * @param value new value or change
1283 * @param type type of the action (ACTION_SET or ACTION_UPDATE)
1286 add_setter_action (struct GNUNET_STATISTICS_Handle *h, const char *name,
1287 int make_persistent, uint64_t value, enum ActionType type)
1289 struct GNUNET_STATISTICS_GetHandle *ai;
1295 GNUNET_assert (NULL != h);
1296 GNUNET_assert (NULL != name);
1297 slen = strlen (h->subsystem) + 1;
1298 nlen = strlen (name) + 1;
1299 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1300 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1305 for (ai = h->action_head; NULL != ai; ai = ai->next)
1307 if (! ( (0 == strcmp (ai->subsystem, h->subsystem)) &&
1308 (0 == strcmp (ai->name, name)) &&
1309 ( (ACTION_UPDATE == ai->type) ||
1310 (ACTION_SET == ai->type) ) ) )
1312 if (ACTION_SET == ai->type)
1314 if (ACTION_UPDATE == type)
1316 delta = (int64_t) value;
1319 /* update old set by new delta */
1324 /* update old set by new delta, but never go negative */
1325 if (ai->value < -delta)
1333 /* new set overrides old set */
1339 if (ACTION_UPDATE == type)
1341 /* make delta cummulative */
1342 delta = (int64_t) value;
1347 /* drop old 'update', use new 'set' instead */
1352 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1353 ai->make_persistent = make_persistent;
1356 /* no existing entry matches, create a fresh one */
1357 ai = GNUNET_malloc (sizeof (struct GNUNET_STATISTICS_GetHandle));
1359 ai->subsystem = GNUNET_strdup (h->subsystem);
1360 ai->name = GNUNET_strdup (name);
1361 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1362 ai->make_persistent = make_persistent;
1366 GNUNET_CONTAINER_DLL_insert_tail (h->action_head, h->action_tail,
1368 schedule_action (h);
1373 * Set statistic value for the peer. Will always use our
1374 * subsystem (the argument used when "handle" was created).
1376 * @param handle identification of the statistics service
1377 * @param name name of the statistic value
1378 * @param value new value to set
1379 * @param make_persistent should the value be kept across restarts?
1382 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1383 const char *name, uint64_t value, int make_persistent)
1387 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1388 add_setter_action (handle, name, make_persistent, value, ACTION_SET);
1393 * Set statistic value for the peer. Will always use our
1394 * subsystem (the argument used when "handle" was created).
1396 * @param handle identification of the statistics service
1397 * @param name name of the statistic value
1398 * @param delta change in value (added to existing value)
1399 * @param make_persistent should the value be kept across restarts?
1402 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1403 const char *name, int64_t delta, int make_persistent)
1409 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1410 add_setter_action (handle, name, make_persistent, (uint64_t) delta,
1415 /* end of statistics_api.c */