2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2016 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 @e proc and @e 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 #ACTION_GET, #ACTION_SET, #ACTION_UPDATE or #ACTION_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 * Message queue to the service.
195 struct GNUNET_MQ_Handle *mq;
198 * Head of the linked list of pending actions (first action
201 struct GNUNET_STATISTICS_GetHandle *action_head;
204 * Tail of the linked list of actions (for fast append).
206 struct GNUNET_STATISTICS_GetHandle *action_tail;
209 * Action we are currently busy with (action request has been
210 * transmitted, we're now receiving the response from the
213 struct GNUNET_STATISTICS_GetHandle *current;
216 * Array of watch entries.
218 struct GNUNET_STATISTICS_WatchEntry **watches;
221 * Task doing exponential back-off trying to reconnect.
223 struct GNUNET_SCHEDULER_Task *backoff_task;
226 * Task for running #do_destroy().
228 struct GNUNET_SCHEDULER_Task *destroy_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 @e 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,
303 if (current_rss > h->peak_rss)
305 h->peak_rss = current_rss;
306 GNUNET_STATISTICS_set (h,
307 "# peak resident set size",
316 * Reconnect at a later time, respecting back-off.
318 * @param h statistics handle
321 reconnect_later (struct GNUNET_STATISTICS_Handle *h);
325 * Schedule the next action to be performed.
327 * @param cls statistics handle to reconnect
330 schedule_action (void *cls);
334 * Transmit request to service that we want to watch
335 * the development of a particular value.
337 * @param h statistics handle
338 * @param watch watch entry of the value to watch
341 schedule_watch_request (struct GNUNET_STATISTICS_Handle *h,
342 struct GNUNET_STATISTICS_WatchEntry *watch)
344 struct GNUNET_STATISTICS_GetHandle *ai;
349 slen = strlen (watch->subsystem) + 1;
350 nlen = strlen (watch->name) + 1;
351 nsize = sizeof (struct GNUNET_MessageHeader) + slen + nlen;
352 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
357 ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle);
359 ai->subsystem = GNUNET_strdup (watch->subsystem);
360 ai->name = GNUNET_strdup (watch->name);
361 ai->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
363 ai->type = ACTION_WATCH;
364 ai->proc = watch->proc;
365 ai->cls = watch->proc_cls;
366 GNUNET_CONTAINER_DLL_insert_tail (h->action_head,
374 * Free memory associated with the given action item.
376 * @param gh action item to free
379 free_action_item (struct GNUNET_STATISTICS_GetHandle *gh)
381 GNUNET_free_non_null (gh->subsystem);
382 GNUNET_free_non_null (gh->name);
388 * Disconnect from the statistics service.
390 * @param h statistics handle to disconnect from
393 do_disconnect (struct GNUNET_STATISTICS_Handle *h)
395 struct GNUNET_STATISTICS_GetHandle *c;
397 h->receiving = GNUNET_NO;
398 if (NULL != (c = h->current))
401 if ( (NULL != c->cont) &&
402 (GNUNET_YES != c->aborted) )
408 free_action_item (c);
412 GNUNET_MQ_destroy (h->mq);
419 * Process a #GNUNET_MESSAGE_TYPE_STATISTICS_VALUE message.
421 * @param cls statistics handle
422 * @param smsg message received from the service, never NULL
423 * @return #GNUNET_OK if the message was well-formed
426 check_statistics_value (void *cls,
427 const struct GNUNET_STATISTICS_ReplyMessage *smsg)
433 size = ntohs (smsg->header.size);
434 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
436 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1],
443 return GNUNET_SYSERR;
450 * Process a #GNUNET_MESSAGE_TYPE_STATISTICS_VALUE message.
452 * @param cls statistics handle
453 * @param msg message received from the service, never NULL
454 * @return #GNUNET_OK if the message was well-formed
457 handle_statistics_value (void *cls,
458 const struct GNUNET_STATISTICS_ReplyMessage *smsg)
460 struct GNUNET_STATISTICS_Handle *h = cls;
465 if (h->current->aborted)
466 return; /* iteration aborted, don't bother */
468 size = ntohs (smsg->header.size);
469 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
470 GNUNET_assert (size ==
471 GNUNET_STRINGS_buffer_tokenize ((const char *) &smsg[1],
476 LOG (GNUNET_ERROR_TYPE_DEBUG,
477 "Received valid statistic on `%s:%s': %llu\n",
479 GNUNET_ntohll (smsg->value));
481 h->current->proc (h->current->cls,
484 GNUNET_ntohll (smsg->value),
486 (ntohl (smsg->uid) & GNUNET_STATISTICS_PERSIST_BIT)))
488 LOG (GNUNET_ERROR_TYPE_DEBUG,
489 "Processing of remaining statistics aborted by client.\n");
490 h->current->aborted = GNUNET_YES;
496 * We have received a watch value from the service. Process it.
498 * @param cls statistics handle
499 * @param msg the watch value message
502 handle_statistics_watch_value (void *cls,
503 const struct GNUNET_STATISTICS_WatchValueMessage *wvm)
505 struct GNUNET_STATISTICS_Handle *h = cls;
506 struct GNUNET_STATISTICS_WatchEntry *w;
509 GNUNET_break (0 == ntohl (wvm->reserved));
510 wid = ntohl (wvm->wid);
511 if (wid >= h->watches_size)
520 (void) w->proc (w->proc_cls,
523 GNUNET_ntohll (wvm->value),
524 0 != (ntohl (wvm->flags) & GNUNET_STATISTICS_PERSIST_BIT));
529 * Generic error handler, called with the appropriate error code and
530 * the same closure specified at the creation of the message queue.
531 * Not every message queue implementation supports an error handler.
533 * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
534 * @param error error code
537 mq_error_handler (void *cls,
538 enum GNUNET_MQ_Error error)
540 struct GNUNET_STATISTICS_Handle *h = cls;
542 if (GNUNET_NO != h->do_destroy)
544 h->do_destroy = GNUNET_NO;
545 if (NULL != h->destroy_task)
547 GNUNET_SCHEDULER_cancel (h->destroy_task);
548 h->destroy_task = NULL;
550 GNUNET_STATISTICS_destroy (h,
560 * Task used to destroy the statistics handle.
562 * @param cls the `struct GNUNET_STATISTICS_Handle`
565 do_destroy (void *cls)
567 struct GNUNET_STATISTICS_Handle *h = cls;
569 h->destroy_task = NULL;
570 h->do_destroy = GNUNET_NO;
571 LOG (GNUNET_ERROR_TYPE_DEBUG,
572 "Running final destruction\n");
573 GNUNET_STATISTICS_destroy (h,
579 * Handle a #GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM
580 * message. We receive this message at the end of the shutdown when
581 * the service confirms that all data has been written to disk.
583 * @param cls our `struct GNUNET_STATISTICS_Handle *`
584 * @param msg the message
587 handle_disconnect_confirm (void *cls,
588 const struct GNUNET_MessageHeader *msg)
590 struct GNUNET_STATISTICS_Handle *h = cls;
592 if (GNUNET_SYSERR != h->do_destroy)
594 /* not in shutdown, why do we get 'TEST'? */
600 LOG (GNUNET_ERROR_TYPE_DEBUG,
601 "Received DISCONNNECT_CONFIRM message from statistics, can complete disconnect\n");
602 if (NULL != h->destroy_task)
603 GNUNET_SCHEDULER_cancel (h->destroy_task);
604 h->destroy_task = GNUNET_SCHEDULER_add_now (&do_destroy,
610 * Handle a #GNUNET_MESSAGE_TYPE_STATISTICS_END message. We receive
611 * this message in response to a query to indicate that there are no
612 * further matching results.
614 * @param cls our `struct GNUNET_STATISTICS_Handle *`
615 * @param msg the message
618 handle_statistics_end (void *cls,
619 const struct GNUNET_MessageHeader *msg)
621 struct GNUNET_STATISTICS_Handle *h = cls;
622 struct GNUNET_STATISTICS_GetHandle *c;
624 LOG (GNUNET_ERROR_TYPE_DEBUG,
625 "Received end of statistics marker\n");
626 if (NULL == (c = h->current))
633 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
642 free_action_item (c);
647 * Try to (re)connect to the statistics service.
649 * @param h statistics handle to reconnect
650 * @return #GNUNET_YES on success, #GNUNET_NO on failure.
653 try_connect (struct GNUNET_STATISTICS_Handle *h)
655 struct GNUNET_MQ_MessageHandler handlers[] = {
656 GNUNET_MQ_hd_fixed_size (disconnect_confirm,
657 GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM,
658 struct GNUNET_MessageHeader,
660 GNUNET_MQ_hd_fixed_size (statistics_end,
661 GNUNET_MESSAGE_TYPE_STATISTICS_END,
662 struct GNUNET_MessageHeader,
664 GNUNET_MQ_hd_var_size (statistics_value,
665 GNUNET_MESSAGE_TYPE_STATISTICS_VALUE,
666 struct GNUNET_STATISTICS_ReplyMessage,
668 GNUNET_MQ_hd_fixed_size (statistics_watch_value,
669 GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE,
670 struct GNUNET_STATISTICS_WatchValueMessage,
672 GNUNET_MQ_handler_end ()
674 struct GNUNET_STATISTICS_GetHandle *gh;
675 struct GNUNET_STATISTICS_GetHandle *gn;
677 if (NULL != h->backoff_task)
681 h->mq = GNUNET_CLIENT_connecT (h->cfg,
688 LOG (GNUNET_ERROR_TYPE_DEBUG,
689 "Failed to connect to statistics service!\n");
693 while (NULL != (gh = gn))
696 if (gh->type == ACTION_WATCH)
698 GNUNET_CONTAINER_DLL_remove (h->action_head,
701 free_action_item (gh);
704 for (unsigned int i = 0; i < h->watches_size; i++)
705 if (NULL != h->watches[i])
706 schedule_watch_request (h,
713 * We've waited long enough, reconnect now.
715 * @param cls the `struct GNUNET_STATISTICS_Handle` to reconnect
718 reconnect_task (void *cls)
720 struct GNUNET_STATISTICS_Handle *h = cls;
722 h->backoff_task = NULL;
728 * Reconnect at a later time, respecting back-off.
730 * @param h statistics handle
733 reconnect_later (struct GNUNET_STATISTICS_Handle *h)
736 struct GNUNET_STATISTICS_GetHandle *gh;
738 GNUNET_assert (NULL == h->backoff_task);
739 if (GNUNET_YES == h->do_destroy)
741 /* So we are shutting down and the service is not reachable.
742 * Chances are that it's down for good and we are not going to connect to
744 * Give up and don't sync the rest of the data.
747 for (gh = h->action_head; NULL != gh; gh = gh->next)
748 if ( (gh->make_persistent) &&
749 (ACTION_SET == gh->type) )
751 if (GNUNET_YES == loss)
752 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
753 _("Could not save some persistent statistics\n"));
754 if (NULL != h->destroy_task)
755 GNUNET_SCHEDULER_cancel (h->destroy_task);
756 h->destroy_task = GNUNET_SCHEDULER_add_now (&do_destroy,
761 = GNUNET_SCHEDULER_add_delayed (h->backoff,
764 h->backoff = GNUNET_TIME_STD_BACKOFF (h->backoff);
770 * Transmit a GET request (and if successful, start to receive
773 * @param handle statistics handle
776 transmit_get (struct GNUNET_STATISTICS_Handle *handle)
778 struct GNUNET_STATISTICS_GetHandle *c;
779 struct GNUNET_MessageHeader *hdr;
780 struct GNUNET_MQ_Envelope *env;
784 GNUNET_assert (NULL != (c = handle->current));
785 slen1 = strlen (c->subsystem) + 1;
786 slen2 = strlen (c->name) + 1;
787 env = GNUNET_MQ_msg_extra (hdr,
789 GNUNET_MESSAGE_TYPE_STATISTICS_GET);
790 GNUNET_assert (slen1 + slen2 ==
791 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1],
796 GNUNET_MQ_notify_sent (env,
799 GNUNET_MQ_send (handle->mq,
805 * Transmit a WATCH request (and if successful, start to receive
808 * @param handle statistics handle
811 transmit_watch (struct GNUNET_STATISTICS_Handle *handle)
813 struct GNUNET_MessageHeader *hdr;
814 struct GNUNET_MQ_Envelope *env;
818 LOG (GNUNET_ERROR_TYPE_DEBUG,
819 "Transmitting watch request for `%s'\n",
820 handle->current->name);
821 slen1 = strlen (handle->current->subsystem) + 1;
822 slen2 = strlen (handle->current->name) + 1;
823 env = GNUNET_MQ_msg_extra (hdr,
825 GNUNET_MESSAGE_TYPE_STATISTICS_WATCH);
826 GNUNET_assert (slen1 + slen2 ==
827 GNUNET_STRINGS_buffer_fill ((char *) &hdr[1],
830 handle->current->subsystem,
831 handle->current->name));
832 GNUNET_MQ_notify_sent (env,
835 GNUNET_MQ_send (handle->mq,
837 GNUNET_assert (NULL == handle->current->cont);
838 free_action_item (handle->current);
839 handle->current = NULL;
840 schedule_action (handle);
845 * Transmit a SET/UPDATE request.
847 * @param handle statistics handle
850 transmit_set (struct GNUNET_STATISTICS_Handle *handle)
852 struct GNUNET_STATISTICS_SetMessage *r;
853 struct GNUNET_MQ_Envelope *env;
857 slen = strlen (handle->current->subsystem) + 1;
858 nlen = strlen (handle->current->name) + 1;
859 env = GNUNET_MQ_msg_extra (r,
861 GNUNET_MESSAGE_TYPE_STATISTICS_SET);
863 r->value = GNUNET_htonll (handle->current->value);
864 if (handle->current->make_persistent)
865 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_PERSISTENT);
866 if (handle->current->type == ACTION_UPDATE)
867 r->flags |= htonl (GNUNET_STATISTICS_SETFLAG_RELATIVE);
868 GNUNET_assert (slen + nlen ==
869 GNUNET_STRINGS_buffer_fill ((char *) &r[1],
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);
878 GNUNET_MQ_notify_sent (env,
881 GNUNET_MQ_send (handle->mq,
887 * Get handle for the statistics service.
889 * @param subsystem name of subsystem using the service
890 * @param cfg services configuration in use
891 * @return handle to use
893 struct GNUNET_STATISTICS_Handle *
894 GNUNET_STATISTICS_create (const char *subsystem,
895 const struct GNUNET_CONFIGURATION_Handle *cfg)
897 struct GNUNET_STATISTICS_Handle *h;
900 GNUNET_CONFIGURATION_get_value_yesno (cfg,
904 h = GNUNET_new (struct GNUNET_STATISTICS_Handle);
906 h->subsystem = GNUNET_strdup (subsystem);
907 h->backoff = GNUNET_TIME_UNIT_MILLISECONDS;
913 * Destroy a handle (free all state associated with
916 * @param h statistics handle to destroy
917 * @param sync_first set to #GNUNET_YES if pending SET requests should
921 GNUNET_STATISTICS_destroy (struct GNUNET_STATISTICS_Handle *h,
924 struct GNUNET_STATISTICS_GetHandle *pos;
925 struct GNUNET_STATISTICS_GetHandle *next;
929 GNUNET_assert (GNUNET_NO == h->do_destroy); /* Don't call twice. */
932 (0 != GNUNET_MQ_get_length (h->mq)) )
934 if ( (NULL != h->current) &&
935 (ACTION_GET == h->current->type) )
936 h->current->aborted = GNUNET_YES;
937 next = h->action_head;
938 while (NULL != (pos = next))
941 if ( (ACTION_GET == pos->type) ||
942 (ACTION_WATCH == pos->type) )
944 GNUNET_CONTAINER_DLL_remove (h->action_head,
947 free_action_item (pos);
950 h->do_destroy = GNUNET_YES;
952 GNUNET_assert (NULL == h->destroy_task);
954 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (h->backoff,
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 "Deferring destruction\n");
960 return; /* do not finish destruction just yet */
962 /* do clean up all */
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Cleaning all up\n");
965 while (NULL != (pos = h->action_head))
967 GNUNET_CONTAINER_DLL_remove (h->action_head,
970 free_action_item (pos);
973 if (NULL != h->backoff_task)
975 GNUNET_SCHEDULER_cancel (h->backoff_task);
976 h->backoff_task = NULL;
978 if (NULL != h->destroy_task)
981 GNUNET_SCHEDULER_cancel (h->destroy_task);
982 h->destroy_task = NULL;
984 for (unsigned int i = 0; i < h->watches_size; i++)
986 if (NULL == h->watches[i])
988 GNUNET_free (h->watches[i]->subsystem);
989 GNUNET_free (h->watches[i]->name);
990 GNUNET_free (h->watches[i]);
992 GNUNET_array_grow (h->watches,
995 GNUNET_free (h->subsystem);
1001 * Schedule the next action to be performed.
1003 * @param cls statistics handle
1006 schedule_action (void *cls)
1008 struct GNUNET_STATISTICS_Handle *h = cls;
1010 if (NULL != h->backoff_task)
1011 return; /* action already pending */
1012 if (GNUNET_YES != try_connect (h))
1014 reconnect_later (h);
1017 if (0 < GNUNET_MQ_get_length (h->mq))
1018 return; /* Wait for queue to be reduced more */
1019 /* schedule next action */
1020 while (NULL == h->current)
1022 h->current = h->action_head;
1023 if (NULL == h->current)
1025 struct GNUNET_MessageHeader *hdr;
1026 struct GNUNET_MQ_Envelope *env;
1028 if (GNUNET_YES != h->do_destroy)
1029 return; /* nothing to do */
1030 /* let service know that we're done */
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "Notifying service that we are done\n");
1033 h->do_destroy = GNUNET_SYSERR; /* in 'TEST' mode */
1034 env = GNUNET_MQ_msg (hdr,
1035 GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT);
1036 GNUNET_MQ_notify_sent (env,
1039 GNUNET_MQ_send (h->mq,
1043 GNUNET_CONTAINER_DLL_remove (h->action_head,
1046 switch (h->current->type)
1067 * Get statistic from the peer.
1069 * @param handle identification of the statistics service
1070 * @param subsystem limit to the specified subsystem, NULL for our subsystem
1071 * @param name name of the statistic value, NULL for all values
1072 * @param cont continuation to call when done (can be NULL)
1073 * This callback CANNOT destroy the statistics handle in the same call.
1074 * @param proc function to call on each value
1075 * @param cls closure for @a cont and @a proc
1076 * @return NULL on error
1078 struct GNUNET_STATISTICS_GetHandle *
1079 GNUNET_STATISTICS_get (struct GNUNET_STATISTICS_Handle *handle,
1080 const char *subsystem,
1082 GNUNET_STATISTICS_Callback cont,
1083 GNUNET_STATISTICS_Iterator proc,
1088 struct GNUNET_STATISTICS_GetHandle *ai;
1092 GNUNET_assert (NULL != proc);
1093 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1094 if (NULL == subsystem)
1098 slen1 = strlen (subsystem) + 1;
1099 slen2 = strlen (name) + 1;
1100 GNUNET_assert (slen1 + slen2 + sizeof (struct GNUNET_MessageHeader) <
1101 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1102 ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle);
1104 ai->subsystem = GNUNET_strdup (subsystem);
1105 ai->name = GNUNET_strdup (name);
1109 ai->type = ACTION_GET;
1110 ai->msize = slen1 + slen2 + sizeof (struct GNUNET_MessageHeader);
1111 GNUNET_CONTAINER_DLL_insert_tail (handle->action_head,
1112 handle->action_tail,
1114 schedule_action (handle);
1120 * Cancel a 'get' request. Must be called before the 'cont'
1121 * function is called.
1123 * @param gh handle of the request to cancel
1126 GNUNET_STATISTICS_get_cancel (struct GNUNET_STATISTICS_GetHandle *gh)
1131 if (gh->sh->current == gh)
1133 gh->aborted = GNUNET_YES;
1136 GNUNET_CONTAINER_DLL_remove (gh->sh->action_head,
1137 gh->sh->action_tail,
1139 GNUNET_free (gh->name);
1140 GNUNET_free (gh->subsystem);
1146 * Watch statistics from the peer (be notified whenever they change).
1148 * @param handle identification of the statistics service
1149 * @param subsystem limit to the specified subsystem, never NULL
1150 * @param name name of the statistic value, never NULL
1151 * @param proc function to call on each value
1152 * @param proc_cls closure for @a proc
1153 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1156 GNUNET_STATISTICS_watch (struct GNUNET_STATISTICS_Handle *handle,
1157 const char *subsystem,
1159 GNUNET_STATISTICS_Iterator proc,
1162 struct GNUNET_STATISTICS_WatchEntry *w;
1165 return GNUNET_SYSERR;
1166 w = GNUNET_new (struct GNUNET_STATISTICS_WatchEntry);
1167 w->subsystem = GNUNET_strdup (subsystem);
1168 w->name = GNUNET_strdup (name);
1170 w->proc_cls = proc_cls;
1171 GNUNET_array_append (handle->watches,
1172 handle->watches_size,
1174 schedule_watch_request (handle,
1181 * Stop watching statistics from the peer.
1183 * @param handle identification of the statistics service
1184 * @param subsystem limit to the specified subsystem, never NULL
1185 * @param name name of the statistic value, never NULL
1186 * @param proc function to call on each value
1187 * @param proc_cls closure for @a proc
1188 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error (no such watch)
1191 GNUNET_STATISTICS_watch_cancel (struct GNUNET_STATISTICS_Handle *handle,
1192 const char *subsystem,
1194 GNUNET_STATISTICS_Iterator proc,
1197 struct GNUNET_STATISTICS_WatchEntry *w;
1200 return GNUNET_SYSERR;
1201 for (unsigned int i=0;i<handle->watches_size;i++)
1203 w = handle->watches[i];
1206 if ( (w->proc == proc) &&
1207 (w->proc_cls == proc_cls) &&
1208 (0 == strcmp (w->name,
1210 (0 == strcmp (w->subsystem,
1213 GNUNET_free (w->name);
1214 GNUNET_free (w->subsystem);
1216 handle->watches[i] = NULL;
1220 return GNUNET_SYSERR;
1225 * Queue a request to change a statistic.
1227 * @param h statistics handle
1228 * @param name name of the value
1229 * @param make_persistent should the value be kept across restarts?
1230 * @param value new value or change
1231 * @param type type of the action (#ACTION_SET or #ACTION_UPDATE)
1234 add_setter_action (struct GNUNET_STATISTICS_Handle *h,
1236 int make_persistent,
1238 enum ActionType type)
1240 struct GNUNET_STATISTICS_GetHandle *ai;
1246 slen = strlen (h->subsystem) + 1;
1247 nlen = strlen (name) + 1;
1248 nsize = sizeof (struct GNUNET_STATISTICS_SetMessage) + slen + nlen;
1249 if (nsize >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1254 for (ai = h->action_head; NULL != ai; ai = ai->next)
1256 if (! ( (0 == strcmp (ai->subsystem,
1258 (0 == strcmp (ai->name,
1260 ( (ACTION_UPDATE == ai->type) ||
1261 (ACTION_SET == ai->type) ) ) )
1263 if (ACTION_SET == ai->type)
1265 if (ACTION_UPDATE == type)
1267 delta = (int64_t) value;
1270 /* update old set by new delta */
1275 /* update old set by new delta, but never go negative */
1276 if (ai->value < -delta)
1284 /* new set overrides old set */
1290 if (ACTION_UPDATE == type)
1292 /* make delta cummulative */
1293 delta = (int64_t) value;
1298 /* drop old 'update', use new 'set' instead */
1304 = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1309 /* no existing entry matches, create a fresh one */
1310 ai = GNUNET_new (struct GNUNET_STATISTICS_GetHandle);
1312 ai->subsystem = GNUNET_strdup (h->subsystem);
1313 ai->name = GNUNET_strdup (name);
1314 ai->timeout = GNUNET_TIME_relative_to_absolute (SET_TRANSMIT_TIMEOUT);
1315 ai->make_persistent = make_persistent;
1319 GNUNET_CONTAINER_DLL_insert_tail (h->action_head,
1322 schedule_action (h);
1327 * Set statistic value for the peer. Will always use our
1328 * subsystem (the argument used when "handle" was created).
1330 * @param handle identification of the statistics service
1331 * @param name name of the statistic value
1332 * @param value new value to set
1333 * @param make_persistent should the value be kept across restarts?
1336 GNUNET_STATISTICS_set (struct GNUNET_STATISTICS_Handle *handle,
1339 int make_persistent)
1343 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1344 add_setter_action (handle,
1353 * Set statistic value for the peer. Will always use our
1354 * subsystem (the argument used when "handle" was created).
1356 * @param handle identification of the statistics service
1357 * @param name name of the statistic value
1358 * @param delta change in value (added to existing value)
1359 * @param make_persistent should the value be kept across restarts?
1362 GNUNET_STATISTICS_update (struct GNUNET_STATISTICS_Handle *handle,
1365 int make_persistent)
1371 GNUNET_assert (GNUNET_NO == handle->do_destroy);
1372 add_setter_action (handle,
1380 /* end of statistics_api.c */