2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2012, 2014, 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/gnunet-service-statistics.c
23 * @brief program that tracks statistics
24 * @author Christian Grothoff
27 #include "gnunet_bio_lib.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_disk_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_service_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_strings_lib.h"
35 #include "gnunet_time_lib.h"
36 #include "statistics.h"
45 * Watch entries are kept in a linked list.
47 struct WatchEntry *next;
50 * Watch entries are kept in a linked list.
52 struct WatchEntry *prev;
55 * For which client is this watch entry?
57 struct ClientEntry *ce;
60 * Last value we communicated to the client for this watch entry.
65 * Unique watch number for this client and this watched value.
71 * #GNUNET_NO : last_value is n/a, #GNUNET_YES: last_value is valid
79 * We keep the statistics organized by subsystem for faster
80 * lookup during SET operations.
82 struct SubsystemEntry;
86 * Entry in the statistics list.
91 * This is a linked list.
93 struct StatsEntry *next;
96 * This is a linked list.
98 struct StatsEntry *prev;
101 * Subsystem this entry belongs to.
103 struct SubsystemEntry *subsystem;
106 * Name for the value stored by this entry, allocated at the end of
112 * Watch context for changes to this value, or NULL for none.
114 struct WatchEntry *we_head;
117 * Watch context for changes to this value, or NULL for none.
119 struct WatchEntry *we_tail;
132 * Is this value persistent?
138 * #GNUNET_NO: value is n/a, #GNUNET_YES: value is valid
146 * We keep the statistics organized by subsystem for faster
147 * lookup during SET operations.
149 struct SubsystemEntry
152 * Subsystems are kept in a DLL.
154 struct SubsystemEntry *next;
157 * Subsystems are kept in a DLL.
159 struct SubsystemEntry *prev;
162 * Head of list of values kept for this subsystem.
164 struct StatsEntry *stat_head;
167 * Tail of list of values kept for this subsystem.
169 struct StatsEntry *stat_tail;
172 * Name of the subsystem this entry is for, allocated at
173 * the end of this struct, do not free().
186 * Corresponding server handle.
188 struct GNUNET_SERVICE_Client *client;
191 * Corresponding message queue.
193 struct GNUNET_MQ_Handle *mq;
196 * Which subsystem is this client writing to (SET/UPDATE)?
198 struct SubsystemEntry *subsystem;
201 * Maximum watch ID used by this client so far.
211 static const struct GNUNET_CONFIGURATION_Handle *cfg;
214 * Head of linked list of subsystems with active statistics.
216 static struct SubsystemEntry *sub_head;
219 * Tail of linked list of subsystems with active statistics.
221 static struct SubsystemEntry *sub_tail;
224 * Number of connected clients.
226 static unsigned int client_count;
229 * Our notification context.
231 static struct GNUNET_NotificationContext *nc;
234 * Counter used to generate unique values.
236 static uint32_t uidgen;
239 * Set to #GNUNET_YES if we are shutting down as soon as possible.
241 static int in_shutdown;
245 * Write persistent statistics to disk.
250 struct SubsystemEntry *se;
251 struct StatsEntry *pos;
253 struct GNUNET_BIO_WriteHandle *wh;
255 unsigned long long total;
258 struct GNUNET_STATISTICS_SetMessage *msg;
261 GNUNET_CONFIGURATION_get_value_filename (cfg,
266 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
271 (void) GNUNET_DISK_directory_create_for_file (fn);
272 wh = GNUNET_BIO_write_open (fn);
274 while (NULL != (se = sub_head))
276 GNUNET_CONTAINER_DLL_remove (sub_head,
279 slen = strlen (se->service) + 1;
280 while (NULL != (pos = se->stat_head))
282 GNUNET_CONTAINER_DLL_remove (se->stat_head,
285 if ( (pos->persistent) &&
288 nlen = strlen (pos->name) + 1;
289 size = sizeof (struct GNUNET_STATISTICS_SetMessage) + nlen + slen;
290 GNUNET_assert (size < UINT16_MAX);
291 msg = GNUNET_malloc (size);
293 msg->header.size = htons ((uint16_t) size);
294 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
295 GNUNET_assert (nlen + slen ==
296 GNUNET_STRINGS_buffer_fill ((char *) &msg[1],
301 msg->flags = htonl (pos->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
302 msg->value = GNUNET_htonll (pos->value);
303 if (GNUNET_OK != GNUNET_BIO_write (wh,
307 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
310 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
311 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
329 GNUNET_BIO_write_close (wh))
330 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
337 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
338 _("Wrote %llu bytes of statistics to `%s'\n"),
342 GNUNET_free_non_null (fn);
347 * Transmit the given stats value.
349 * @param client receiver of the value
350 * @param e value to transmit
353 transmit (struct ClientEntry *ce,
354 const struct StatsEntry *e)
356 struct GNUNET_MQ_Envelope *env;
357 struct GNUNET_STATISTICS_ReplyMessage *m;
360 size = strlen (e->subsystem->service) + 1 +
361 strlen (e->name) + 1;
362 GNUNET_assert (size < GNUNET_MAX_MESSAGE_SIZE);
363 env = GNUNET_MQ_msg_extra (m,
365 GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
366 m->uid = htonl (e->uid);
368 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
369 m->value = GNUNET_htonll (e->value);
370 GNUNET_assert (size ==
371 GNUNET_STRINGS_buffer_fill ((char *) &m[1],
374 e->subsystem->service,
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Transmitting value for `%s:%s' (%d): %llu\n",
378 e->subsystem->service,
381 (unsigned long long) e->value);
382 GNUNET_MQ_send (ce->mq,
388 * Callback called when a client connects to the service.
390 * @param cls closure for the service
391 * @param c the new client that connected to the service
392 * @param mq the message queue used to send messages to the client
396 client_connect_cb (void *cls,
397 struct GNUNET_SERVICE_Client *c,
398 struct GNUNET_MQ_Handle *mq)
400 struct ClientEntry *ce;
402 ce = GNUNET_new (struct ClientEntry);
406 GNUNET_notification_context_add (nc,
413 * Check integrity of GET-message.
415 * @param cls identification of the client
416 * @param message the actual message
417 * @return #GNUNET_OK if @a message is well-formed
420 check_get (void *cls,
421 const struct GNUNET_MessageHeader *message)
427 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
429 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
436 return GNUNET_SYSERR;
443 * Handle GET-message.
445 * @param cls identification of the client
446 * @param message the actual message
449 handle_get (void *cls,
450 const struct GNUNET_MessageHeader *message)
452 struct ClientEntry *ce = cls;
453 struct GNUNET_MQ_Envelope *env;
454 struct GNUNET_MessageHeader *end;
459 struct SubsystemEntry *se;
460 struct StatsEntry *pos;
463 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
464 GNUNET_assert (size ==
465 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
470 slen = strlen (service);
471 nlen = strlen (name);
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Received request for statistics on `%s:%s'\n",
474 slen ? service : "*",
476 for (se = sub_head; NULL != se; se = se->next)
478 if (! ( (0 == slen) ||
479 (0 == strcmp (service, se->service))) )
481 for (pos = se->stat_head; NULL != pos; pos = pos->next)
483 if (! ( (0 == nlen) ||
491 env = GNUNET_MQ_msg (end,
492 GNUNET_MESSAGE_TYPE_STATISTICS_END);
493 GNUNET_MQ_send (ce->mq,
495 GNUNET_SERVICE_client_continue (ce->client);
500 * Notify all clients listening about a change to a value.
502 * @param se value that changed
505 notify_change (struct StatsEntry *se)
507 struct GNUNET_MQ_Envelope *env;
508 struct GNUNET_STATISTICS_WatchValueMessage *wvm;
509 struct WatchEntry *pos;
511 for (pos = se->we_head; NULL != pos; pos = pos->next)
513 if (GNUNET_YES == pos->last_value_set)
515 if (pos->last_value == se->value)
520 pos->last_value_set = GNUNET_YES;
522 env = GNUNET_MQ_msg (wvm,
523 GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
524 wvm->flags = htonl (se->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
525 wvm->wid = htonl (pos->wid);
526 wvm->reserved = htonl (0);
527 wvm->value = GNUNET_htonll (se->value);
528 GNUNET_MQ_send (pos->ce->mq,
530 pos->last_value = se->value;
536 * Find the subsystem entry of the given name for the specified client.
538 * @param ce client looking for the subsystem, may contain a hint
539 * to find the entry faster, can be NULL
540 * @param service name of the subsystem to look for
541 * @return subsystem entry, never NULL (subsystem entry is created if necessary)
543 static struct SubsystemEntry *
544 find_subsystem_entry (struct ClientEntry *ce,
548 struct SubsystemEntry *se;
555 (0 != strcmp (service,
558 for (se = sub_head; NULL != se; se = se->next)
559 if (0 == strcmp (service,
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568 "Allocating new subsystem entry `%s'\n",
570 slen = strlen (service) + 1;
571 se = GNUNET_malloc (sizeof (struct SubsystemEntry) +
573 GNUNET_memcpy (&se[1],
576 se->service = (const char *) &se[1];
577 GNUNET_CONTAINER_DLL_insert (sub_head,
587 * Find the statistics entry of the given subsystem.
589 * @param subsystem subsystem to look in
590 * @param name name of the entry to look for
591 * @return statistis entry, or NULL if not found
593 static struct StatsEntry *
594 find_stat_entry (struct SubsystemEntry *se,
597 struct StatsEntry *pos;
599 for (pos = se->stat_head; NULL != pos; pos = pos->next)
600 if (0 == strcmp (name, pos->name))
607 * Check format of SET-message.
609 * @param cls the `struct ClientEntry`
610 * @param message the actual message
611 * @return #GNUNET_OK if message is well-formed
614 check_set (void *cls,
615 const struct GNUNET_STATISTICS_SetMessage *msg)
621 msize = ntohs (msg->header.size) - sizeof (*msg);
623 GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
630 return GNUNET_SYSERR;
637 * Handle SET-message.
639 * @param cls the `struct ClientEntry`
640 * @param message the actual message
643 handle_set (void *cls,
644 const struct GNUNET_STATISTICS_SetMessage *msg)
646 struct ClientEntry *ce = cls;
652 struct SubsystemEntry *se;
653 struct StatsEntry *pos;
660 msize = ntohs (msg->header.size);
661 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
662 GNUNET_assert (size ==
663 GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
668 se = find_subsystem_entry (ce,
670 flags = ntohl (msg->flags);
671 value = GNUNET_ntohll (msg->value);
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
676 (unsigned int) flags,
677 (unsigned long long) value);
678 pos = find_stat_entry (se,
683 if (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE))
685 changed = (pos->value != value);
690 delta = (int64_t) value;
691 if ((delta < 0) && (pos->value < -delta))
693 changed = (0 != pos->value);
698 changed = (0 != delta);
699 GNUNET_break ( (delta <= 0) ||
700 (pos->value + delta > pos->value) );
704 if (GNUNET_NO == pos->set)
706 pos->set = GNUNET_YES;
709 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
710 if (pos != se->stat_head)
712 /* move to front for faster setting next time! */
713 GNUNET_CONTAINER_DLL_remove (se->stat_head,
716 GNUNET_CONTAINER_DLL_insert (se->stat_head,
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721 "Statistic `%s:%s' updated to value %llu (%d).\n",
724 (unsigned long long) pos->value,
729 GNUNET_SERVICE_client_continue (ce->client);
732 /* not found, create a new entry */
733 nlen = strlen (name) + 1;
734 pos = GNUNET_malloc (sizeof (struct StatsEntry) + nlen);
735 GNUNET_memcpy (&pos[1],
738 pos->name = (const char *) &pos[1];
740 if ( (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE)) ||
741 (0 < (int64_t) GNUNET_ntohll (msg->value)) )
743 pos->value = GNUNET_ntohll (msg->value);
744 pos->set = GNUNET_YES;
748 pos->set = GNUNET_NO;
751 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
752 GNUNET_CONTAINER_DLL_insert (se->stat_head,
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "New statistic on `%s:%s' with value %llu created.\n",
759 (unsigned long long) pos->value);
761 GNUNET_SERVICE_client_continue (ce->client);
766 * Check integrity of WATCH-message.
768 * @param cls the `struct ClientEntry *`
769 * @param message the actual message
770 * @return #GNUNET_OK if message is well-formed
773 check_watch (void *cls,
774 const struct GNUNET_MessageHeader *message)
780 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
782 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
789 return GNUNET_SYSERR;
796 * Handle WATCH-message.
798 * @param cls the `struct ClientEntry *`
799 * @param message the actual message
802 handle_watch (void *cls,
803 const struct GNUNET_MessageHeader *message)
805 struct ClientEntry *ce = cls;
810 struct SubsystemEntry *se;
811 struct StatsEntry *pos;
812 struct WatchEntry *we;
817 GNUNET_SERVICE_client_drop (ce->client);
820 GNUNET_SERVICE_client_mark_monitor (ce->client);
821 msize = ntohs (message->size);
822 size = msize - sizeof (struct GNUNET_MessageHeader);
823 GNUNET_assert (size ==
824 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830 "Received request to watch statistic on `%s:%s'\n",
833 se = find_subsystem_entry (ce,
835 pos = find_stat_entry (se,
839 nlen = strlen (name) + 1;
840 pos = GNUNET_malloc (sizeof (struct StatsEntry) +
842 GNUNET_memcpy (&pos[1],
845 pos->name = (const char *) &pos[1];
847 GNUNET_CONTAINER_DLL_insert (se->stat_head,
851 pos->set = GNUNET_NO;
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 "New statistic on `%s:%s' with value %llu created.\n",
856 (unsigned long long) pos->value);
858 we = GNUNET_new (struct WatchEntry);
860 we->last_value_set = GNUNET_NO;
861 we->wid = ce->max_wid++;
862 GNUNET_CONTAINER_DLL_insert (pos->we_head,
867 GNUNET_SERVICE_client_continue (ce->client);
872 * Handle DISCONNECT-message. Sync to disk and send
873 * back a #GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM
876 * @param cls the `struct ClientEntry *`
877 * @param message the actual message
880 handle_disconnect (void *cls,
881 const struct GNUNET_MessageHeader *message)
883 struct ClientEntry *ce = cls;
884 struct GNUNET_MQ_Envelope *env;
885 struct GNUNET_MessageHeader *msg;
887 env = GNUNET_MQ_msg (msg,
888 GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM);
889 GNUNET_MQ_send (ce->mq,
891 GNUNET_SERVICE_client_continue (ce->client);
896 * Actually perform the shutdown.
901 struct WatchEntry *we;
902 struct StatsEntry *pos;
903 struct SubsystemEntry *se;
908 GNUNET_notification_context_destroy (nc);
910 GNUNET_assert (0 == client_count);
911 while (NULL != (se = sub_head))
913 GNUNET_CONTAINER_DLL_remove (sub_head,
916 while (NULL != (pos = se->stat_head))
918 GNUNET_CONTAINER_DLL_remove (se->stat_head,
921 while (NULL != (we = pos->we_head))
924 GNUNET_CONTAINER_DLL_remove (pos->we_head,
937 * Task run during shutdown.
942 shutdown_task (void *cls)
944 in_shutdown = GNUNET_YES;
945 if (0 != client_count)
952 * A client disconnected. Remove all of its data structure entries.
954 * @param cls closure, NULL
955 * @param client identification of the client
956 * @param app_cls the `struct ClientEntry *`
959 client_disconnect_cb (void *cls,
960 struct GNUNET_SERVICE_Client *client,
963 struct ClientEntry *ce = app_cls;
964 struct WatchEntry *we;
965 struct WatchEntry *wen;
966 struct StatsEntry *pos;
967 struct SubsystemEntry *se;
970 for (se = sub_head; NULL != se; se = se->next)
972 for (pos = se->stat_head; NULL != pos; pos = pos->next)
975 while (NULL != (we = wen))
980 GNUNET_CONTAINER_DLL_remove (pos->we_head,
988 if ( (0 == client_count) &&
989 (GNUNET_YES == in_shutdown) )
995 * We've read a `struct GNUNET_STATISTICS_SetMessage *` from
996 * disk. Check that it is well-formed, and if so pass it to
997 * the handler for set messages.
1000 * @param message the message found on disk
1001 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
1004 inject_message (void *cls,
1005 const struct GNUNET_MessageHeader *message)
1007 uint16_t msize = ntohs (message->size);
1008 const struct GNUNET_STATISTICS_SetMessage *sm;
1010 sm = (const struct GNUNET_STATISTICS_SetMessage *) message;
1011 if ( (sizeof (struct GNUNET_STATISTICS_SetMessage) > msize) ||
1017 return GNUNET_SYSERR;
1026 * Load persistent values from disk. Disk format is exactly the same
1027 * format that we also use for setting the values over the network.
1033 struct GNUNET_BIO_ReadHandle *rh;
1036 struct GNUNET_MessageStreamTokenizer *mst;
1039 GNUNET_CONFIGURATION_get_value_filename (cfg,
1044 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1050 GNUNET_DISK_file_size (fn,
1059 buf = GNUNET_malloc (fsize);
1060 rh = GNUNET_BIO_read_open (fn);
1068 GNUNET_BIO_read (rh,
1073 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1076 GNUNET_break (GNUNET_OK ==
1077 GNUNET_BIO_read_close (rh,
1083 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1084 _("Loading %llu bytes of statistics from `%s'\n"),
1085 (unsigned long long) fsize,
1087 mst = GNUNET_MST_create (&inject_message,
1089 GNUNET_break (GNUNET_OK ==
1090 GNUNET_MST_from_buffer (mst,
1095 GNUNET_MST_destroy (mst);
1097 GNUNET_break (GNUNET_OK ==
1098 GNUNET_BIO_read_close (rh,
1105 * Process statistics requests.
1107 * @param cls closure
1108 * @param c configuration to use
1109 * @param service the initialized service
1113 const struct GNUNET_CONFIGURATION_Handle *c,
1114 struct GNUNET_SERVICE_Handle *service)
1117 nc = GNUNET_notification_context_create (16);
1119 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1125 * Define "main" method using service macro.
1129 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN,
1132 &client_disconnect_cb,
1134 GNUNET_MQ_hd_var_size (set,
1135 GNUNET_MESSAGE_TYPE_STATISTICS_SET,
1136 struct GNUNET_STATISTICS_SetMessage,
1138 GNUNET_MQ_hd_var_size (get,
1139 GNUNET_MESSAGE_TYPE_STATISTICS_GET,
1140 struct GNUNET_MessageHeader,
1142 GNUNET_MQ_hd_var_size (watch,
1143 GNUNET_MESSAGE_TYPE_STATISTICS_WATCH,
1144 struct GNUNET_MessageHeader,
1146 GNUNET_MQ_hd_fixed_size (disconnect,
1147 GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT,
1148 struct GNUNET_MessageHeader,
1150 GNUNET_MQ_handler_end ());
1153 #if defined(LINUX) && defined(__GLIBC__)
1157 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1159 void __attribute__ ((constructor))
1160 GNUNET_STATISTICS_memory_init ()
1162 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1163 mallopt (M_TOP_PAD, 1 * 1024);
1169 /* end of gnunet-service-statistics.c */