2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2012, 2014 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., 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 GNUNET_SERVER_Client *client;
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_SERVER_Client *client;
191 * Which subsystem is this client writing to (SET/UPDATE)?
193 struct SubsystemEntry *subsystem;
196 * Maximum watch ID used by this client so far.
206 static const struct GNUNET_CONFIGURATION_Handle *cfg;
209 * Head of linked list of subsystems with active statistics.
211 static struct SubsystemEntry *sub_head;
214 * Tail of linked list of subsystems with active statistics.
216 static struct SubsystemEntry *sub_tail;
219 * Number of connected clients.
221 static unsigned int client_count;
224 * Handle to our server.
226 static struct GNUNET_SERVER_Handle *srv;
229 * Our notification context.
231 static struct GNUNET_SERVER_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 * Inject a message to our server with a client of 'NULL'.
247 * @param cls the `struct GNUNET_SERVER_Handle`
248 * @param client unused
249 * @param msg message to inject
252 inject_message (void *cls,
254 const struct GNUNET_MessageHeader *msg)
256 struct GNUNET_SERVER_Handle *server = cls;
258 GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
264 * Load persistent values from disk. Disk format is exactly the same
265 * format that we also use for setting the values over the network.
267 * @param server handle to the server context
270 load (struct GNUNET_SERVER_Handle *server)
273 struct GNUNET_BIO_ReadHandle *rh;
276 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
279 GNUNET_CONFIGURATION_get_value_filename (cfg,
284 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
290 GNUNET_DISK_file_size (fn,
299 buf = GNUNET_malloc (fsize);
300 rh = GNUNET_BIO_read_open (fn);
313 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
316 GNUNET_break (GNUNET_OK ==
317 GNUNET_BIO_read_close (rh, NULL));
322 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
323 _("Loading %llu bytes of statistics from `%s'\n"),
325 mst = GNUNET_SERVER_mst_create (&inject_message,
327 GNUNET_break (GNUNET_OK ==
328 GNUNET_SERVER_mst_receive (mst, NULL,
332 GNUNET_SERVER_mst_destroy (mst);
334 GNUNET_break (GNUNET_OK ==
335 GNUNET_BIO_read_close (rh,
342 * Write persistent statistics to disk.
347 struct SubsystemEntry *se;
348 struct StatsEntry *pos;
350 struct GNUNET_BIO_WriteHandle *wh;
352 unsigned long long total;
355 struct GNUNET_STATISTICS_SetMessage *msg;
358 GNUNET_CONFIGURATION_get_value_filename (cfg,
363 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
368 (void) GNUNET_DISK_directory_create_for_file (fn);
369 wh = GNUNET_BIO_write_open (fn);
371 while (NULL != (se = sub_head))
373 GNUNET_CONTAINER_DLL_remove (sub_head,
376 slen = strlen (se->service) + 1;
377 while (NULL != (pos = se->stat_head))
379 GNUNET_CONTAINER_DLL_remove (se->stat_head,
382 if ((pos->persistent) && (NULL != wh))
384 nlen = strlen (pos->name) + 1;
385 size = sizeof (struct GNUNET_STATISTICS_SetMessage) + nlen + slen;
386 GNUNET_assert (size < UINT16_MAX);
387 msg = GNUNET_malloc (size);
389 msg->header.size = htons ((uint16_t) size);
390 msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
391 GNUNET_assert (nlen + slen ==
392 GNUNET_STRINGS_buffer_fill ((char *) &msg[1],
397 msg->flags = htonl (pos->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
398 msg->value = GNUNET_htonll (pos->value);
399 if (GNUNET_OK != GNUNET_BIO_write (wh,
403 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
406 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
407 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
425 GNUNET_BIO_write_close (wh))
426 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
433 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
434 _("Wrote %llu bytes of statistics to `%s'\n"),
438 GNUNET_free_non_null (fn);
443 * Transmit the given stats value.
445 * @param client receiver of the value
446 * @param e value to transmit
449 transmit (struct GNUNET_SERVER_Client *client,
450 const struct StatsEntry *e)
452 struct GNUNET_STATISTICS_ReplyMessage *m;
455 size = sizeof (struct GNUNET_STATISTICS_ReplyMessage) +
456 strlen (e->subsystem->service) + 1 +
457 strlen (e->name) + 1;
458 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
459 m = GNUNET_malloc (size);
460 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
461 m->header.size = htons (size);
462 m->uid = htonl (e->uid);
464 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
465 m->value = GNUNET_htonll (e->value);
466 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
467 GNUNET_assert (size ==
468 GNUNET_STRINGS_buffer_fill ((char *) &m[1],
471 e->subsystem->service,
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Transmitting value for `%s:%s' (%d): %llu\n",
475 e->subsystem->service,
479 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
486 * Find a client entry for the given client handle, or create one.
488 * @param client handle to match
489 * @return corresponding client entry struct
491 static struct ClientEntry *
492 make_client_entry (struct GNUNET_SERVER_Client *client)
494 struct ClientEntry *ce;
496 ce = GNUNET_SERVER_client_get_user_context (client,
502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
505 ce = GNUNET_new (struct ClientEntry);
507 GNUNET_SERVER_client_set_user_context (client, ce);
509 GNUNET_SERVER_notification_context_add (nc, client);
515 * Handle GET-message.
518 * @param client identification of the client
519 * @param message the actual message
520 * @return #GNUNET_OK to keep the connection open,
521 * #GNUNET_SYSERR to close it (signal serious error)
524 handle_get (void *cls,
525 struct GNUNET_SERVER_Client *client,
526 const struct GNUNET_MessageHeader *message)
528 struct GNUNET_MessageHeader end;
533 struct SubsystemEntry *se;
534 struct StatsEntry *pos;
537 if (NULL == make_client_entry (client))
538 return; /* new client during shutdown */
539 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
541 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
548 GNUNET_SERVER_receive_done (client,
552 slen = strlen (service);
553 nlen = strlen (name);
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Received request for statistics on `%s:%s'\n",
556 slen ? service : "*",
558 for (se = sub_head; NULL != se; se = se->next)
560 if (! ( (0 == slen) ||
561 (0 == strcmp (service, se->service))) )
563 for (pos = se->stat_head; NULL != pos; pos = pos->next)
565 if (! ( (0 == nlen) ||
566 (0 == strcmp (name, pos->name))) )
568 transmit (client, pos);
571 end.size = htons (sizeof (struct GNUNET_MessageHeader));
572 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
573 GNUNET_SERVER_notification_context_unicast (nc,
577 GNUNET_SERVER_receive_done (client, GNUNET_OK);
582 * Notify all clients listening about a change to a value.
584 * @param se value that changed
587 notify_change (struct StatsEntry *se)
589 struct GNUNET_STATISTICS_WatchValueMessage wvm;
590 struct WatchEntry *pos;
592 for (pos = se->we_head; NULL != pos; pos = pos->next)
594 if (GNUNET_YES == pos->last_value_set)
596 if (pos->last_value == se->value)
601 pos->last_value_set = GNUNET_YES;
603 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
605 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
606 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
607 wvm.wid = htonl (pos->wid);
608 wvm.reserved = htonl (0);
609 wvm.value = GNUNET_htonll (se->value);
610 GNUNET_SERVER_notification_context_unicast (nc,
614 pos->last_value = se->value;
620 * Find the subsystem entry of the given name for the specified client.
622 * @param ce client looking for the subsystem, may contain a hint
623 * to find the entry faster, can be NULL
624 * @param service name of the subsystem to look for
625 * @return subsystem entry, never NULL (subsystem entry is created if necessary)
627 static struct SubsystemEntry *
628 find_subsystem_entry (struct ClientEntry *ce,
632 struct SubsystemEntry *se;
639 (0 != strcmp (service,
642 for (se = sub_head; NULL != se; se = se->next)
643 if (0 == strcmp (service,
651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
652 "Allocating new subsystem entry `%s'\n",
654 slen = strlen (service) + 1;
655 se = GNUNET_malloc (sizeof (struct SubsystemEntry) +
660 se->service = (const char *) &se[1];
661 GNUNET_CONTAINER_DLL_insert (sub_head,
671 * Find the statistics entry of the given subsystem.
673 * @param subsystem subsystem to look in
674 * @param name name of the entry to look for
675 * @return statistis entry, or NULL if not found
677 static struct StatsEntry *
678 find_stat_entry (struct SubsystemEntry *se,
681 struct StatsEntry *pos;
683 for (pos = se->stat_head; NULL != pos; pos = pos->next)
684 if (0 == strcmp (name, pos->name))
691 * Handle SET-message.
694 * @param client identification of the client
695 * @param message the actual message
698 handle_set (void *cls,
699 struct GNUNET_SERVER_Client *client,
700 const struct GNUNET_MessageHeader *message)
707 const struct GNUNET_STATISTICS_SetMessage *msg;
708 struct SubsystemEntry *se;
709 struct ClientEntry *ce;
710 struct StatsEntry *pos;
718 if ( (NULL != client) &&
719 (NULL == (ce = make_client_entry (client))) )
720 return; /* new client during shutdown */
721 msize = ntohs (message->size);
722 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
725 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
728 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
729 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
731 GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
738 GNUNET_SERVER_receive_done (client,
742 se = find_subsystem_entry (ce, service);
743 flags = ntohl (msg->flags);
744 value = GNUNET_ntohll (msg->value);
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
749 (unsigned int) flags,
750 (unsigned long long) value);
751 pos = find_stat_entry (se, name);
755 if (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE))
757 changed = (pos->value != value);
762 delta = (int64_t) value;
763 if ((delta < 0) && (pos->value < -delta))
765 changed = (0 != pos->value);
770 changed = (0 != delta);
771 GNUNET_break ( (delta <= 0) ||
772 (pos->value + delta > pos->value) );
776 if (GNUNET_NO == pos->set)
778 pos->set = GNUNET_YES;
781 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
782 if (pos != se->stat_head)
784 /* move to front for faster setting next time! */
785 GNUNET_CONTAINER_DLL_remove (se->stat_head,
788 GNUNET_CONTAINER_DLL_insert (se->stat_head,
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793 "Statistic `%s:%s' updated to value %llu (%d).\n",
801 GNUNET_SERVER_receive_done (client,
805 /* not found, create a new entry */
806 nlen = strlen (name) + 1;
807 pos = GNUNET_malloc (sizeof (struct StatsEntry) + nlen);
811 pos->name = (const char *) &pos[1];
813 if ( (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE)) ||
814 (0 < (int64_t) GNUNET_ntohll (msg->value)) )
816 pos->value = GNUNET_ntohll (msg->value);
817 pos->set = GNUNET_YES;
821 pos->set = GNUNET_NO;
824 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
825 GNUNET_CONTAINER_DLL_insert (se->stat_head,
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "New statistic on `%s:%s' with value %llu created.\n",
833 GNUNET_SERVER_receive_done (client,
839 * Handle WATCH-message.
842 * @param client identification of the client
843 * @param message the actual message
846 handle_watch (void *cls,
847 struct GNUNET_SERVER_Client *client,
848 const struct GNUNET_MessageHeader *message)
854 struct SubsystemEntry *se;
855 struct StatsEntry *pos;
856 struct ClientEntry *ce;
857 struct WatchEntry *we;
862 GNUNET_SERVER_receive_done (client,
866 GNUNET_SERVER_client_mark_monitor (client);
867 ce = make_client_entry (client);
868 msize = ntohs (message->size);
869 if (msize < sizeof (struct GNUNET_MessageHeader))
872 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
875 size = msize - sizeof (struct GNUNET_MessageHeader);
877 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
884 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 "Received request to watch statistic on `%s:%s'\n",
891 se = find_subsystem_entry (ce, service);
892 pos = find_stat_entry (se, name);
895 nlen = strlen (name) + 1;
896 pos = GNUNET_malloc (sizeof (struct StatsEntry) +
898 memcpy (&pos[1], name, nlen);
899 pos->name = (const char *) &pos[1];
901 GNUNET_CONTAINER_DLL_insert (se->stat_head,
905 pos->set = GNUNET_NO;
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "New statistic on `%s:%s' with value %llu created.\n",
912 we = GNUNET_new (struct WatchEntry);
914 we->last_value_set = GNUNET_NO;
915 we->wid = ce->max_wid++;
916 GNUNET_CONTAINER_DLL_insert (pos->we_head,
921 GNUNET_SERVER_receive_done (client, GNUNET_OK);
926 * Actually perform the shutdown.
931 struct WatchEntry *we;
932 struct StatsEntry *pos;
933 struct SubsystemEntry *se;
938 GNUNET_SERVER_notification_context_destroy (nc);
940 GNUNET_assert (0 == client_count);
941 while (NULL != (se = sub_head))
943 GNUNET_CONTAINER_DLL_remove (sub_head,
946 while (NULL != (pos = se->stat_head))
948 GNUNET_CONTAINER_DLL_remove (se->stat_head,
951 while (NULL != (we = pos->we_head))
954 GNUNET_CONTAINER_DLL_remove (pos->we_head,
967 * Task run during shutdown.
973 shutdown_task (void *cls,
974 const struct GNUNET_SCHEDULER_TaskContext *tc)
976 in_shutdown = GNUNET_YES;
977 if (0 != client_count)
984 * A client disconnected. Remove all of its data structure entries.
986 * @param cls closure, NULL
987 * @param client identification of the client
990 handle_client_disconnect (void *cls,
991 struct GNUNET_SERVER_Client *client)
993 struct ClientEntry *ce;
994 struct WatchEntry *we;
995 struct WatchEntry *wen;
996 struct StatsEntry *pos;
997 struct SubsystemEntry *se;
1001 ce = GNUNET_SERVER_client_get_user_context (client,
1002 struct ClientEntry);
1005 GNUNET_SERVER_client_set_user_context (client,
1008 for (se = sub_head; NULL != se; se = se->next)
1010 for (pos = se->stat_head; NULL != pos; pos = pos->next)
1013 while (NULL != (we = wen))
1016 if (we->client != client)
1018 GNUNET_CONTAINER_DLL_remove (pos->we_head,
1025 if ( (0 == client_count) &&
1026 (GNUNET_YES == in_shutdown) )
1032 * Process statistics requests.
1034 * @param cls closure
1035 * @param server the initialized server
1036 * @param c configuration to use
1040 struct GNUNET_SERVER_Handle *server,
1041 const struct GNUNET_CONFIGURATION_Handle *c)
1043 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1044 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
1045 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
1046 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
1051 GNUNET_SERVER_add_handlers (server,
1053 nc = GNUNET_SERVER_notification_context_create (server, 16);
1054 GNUNET_SERVER_disconnect_notify (server,
1055 &handle_client_disconnect,
1058 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1065 * The main function for the statistics service.
1067 * @param argc number of arguments from the command line
1068 * @param argv command line arguments
1069 * @return 0 ok, 1 on error
1072 main (int argc, char *const *argv)
1074 return (GNUNET_OK ==
1075 GNUNET_SERVICE_run (argc, argv, "statistics",
1076 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN,
1077 &run, NULL)) ? 0 : 1;
1080 #if defined(LINUX) && defined(__GLIBC__)
1084 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1086 void __attribute__ ((constructor))
1087 GNUNET_STATISTICS_memory_init ()
1089 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1090 mallopt (M_TOP_PAD, 1 * 1024);
1096 /* end of gnunet-service-statistics.c */