2 This file is part of GNUnet.
3 (C) 2009, 2010, 2012 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/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
84 * Clients are kept in a linked list.
86 struct ClientEntry *next;
89 * Clients are kept in a linked list.
91 struct ClientEntry *prev;
94 * Corresponding server handle.
96 struct GNUNET_SERVER_Client *client;
99 * Maximum watch ID used by this client so far.
106 * Entry in the statistics list.
111 * This is a linked list.
113 struct StatsEntry *next;
116 * Name of the service, points into the
122 * Name for the value, points into
128 * Message that can be used to set this value,
129 * stored at the end of the memory used by
132 struct GNUNET_STATISTICS_SetMessage *msg;
135 * Watch context for changes to this
136 * value, or NULL for none.
138 struct WatchEntry *we_head;
141 * Watch context for changes to this
142 * value, or NULL for none.
144 struct WatchEntry *we_tail;
157 * Is this value persistent?
163 * GNUNET_NO : value is n/a, GNUNET_YES: value is valid
172 static const struct GNUNET_CONFIGURATION_Handle *cfg;
175 * Linked list of our active statistics.
177 static struct StatsEntry *start;
180 * Head of linked list of connected clients.
182 static struct ClientEntry *client_head;
185 * Tail of linked list of connected clients.
187 static struct ClientEntry *client_tail;
190 * Handle to our server.
192 static struct GNUNET_SERVER_Handle *srv;
195 * Our notification context.
197 static struct GNUNET_SERVER_NotificationContext *nc;
200 * Counter used to generate unique values.
202 static uint32_t uidgen;
205 * Set to YES if we are shutting down as soon as possible.
207 static int in_shutdown;
211 * Inject a message to our server with a client of 'NULL'.
213 * @param cls the 'struct GNUNET_SERVER_Handle'
214 * @param client unused
215 * @param msg message to inject
218 inject_message (void *cls, void *client, const struct GNUNET_MessageHeader *msg)
220 struct GNUNET_SERVER_Handle *server = cls;
222 GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
228 * Load persistent values from disk. Disk format is
229 * exactly the same format that we also use for
230 * setting the values over the network.
232 * @param server handle to the server context
235 load (struct GNUNET_SERVER_Handle *server)
238 struct GNUNET_BIO_ReadHandle *rh;
241 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
245 GNUNET_CONFIGURATION_get_value_filename (cfg,
250 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
256 GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) ||
262 buf = GNUNET_malloc (fsize);
263 rh = GNUNET_BIO_read_open (fn);
270 if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
272 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
273 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
275 GNUNET_free_non_null (emsg);
279 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
280 _("Loading %llu bytes of statistics from `%s'\n"),
282 mst = GNUNET_SERVER_mst_create (&inject_message, server);
283 GNUNET_break (GNUNET_OK ==
284 GNUNET_SERVER_mst_receive (mst, NULL, buf, fsize,
285 GNUNET_YES, GNUNET_NO));
286 GNUNET_SERVER_mst_destroy (mst);
288 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
289 GNUNET_free_non_null (emsg);
295 * Write persistent statistics to disk.
300 struct StatsEntry *pos;
302 struct GNUNET_BIO_WriteHandle *wh;
304 unsigned long long total;
307 GNUNET_CONFIGURATION_get_value_filename (cfg,
312 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
317 (void) GNUNET_DISK_directory_create_for_file (fn);
318 wh = GNUNET_BIO_write_open (fn);
320 while (NULL != (pos = start))
323 if ((pos->persistent) && (NULL != wh))
325 size = htons (pos->msg->header.size);
326 if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
328 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
329 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
330 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
340 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
341 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
343 GNUNET_break (0 == UNLINK (fn));
345 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
346 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
348 GNUNET_free_non_null (fn);
353 * Transmit the given stats value.
355 * @param client receiver of the value
356 * @param e value to transmit
359 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
361 struct GNUNET_STATISTICS_ReplyMessage *m;
365 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
366 strlen (e->name) + 1;
367 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
368 m = GNUNET_malloc (size);
369 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
370 m->header.size = htons (size);
371 m->uid = htonl (e->uid);
373 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
374 m->value = GNUNET_htonll (e->value);
375 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
376 GNUNET_assert (size ==
377 GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
378 e->service, e->name));
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
381 e->name, e->persistent, e->value);
382 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
389 * Does this entry match the request?
392 * @param service name of service to match
393 * @param name value to match
394 * @return 1 if they match, 0 if not
397 matches (const struct StatsEntry *e, const char *service, const char *name)
399 return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
400 ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
405 * Find a client entry for the given client handle, or create one.
407 * @param client handle to match
408 * @return corresponding client entry struct
410 static struct ClientEntry *
411 make_client_entry (struct GNUNET_SERVER_Client *client)
413 struct ClientEntry *ce;
415 GNUNET_assert (client != NULL);
419 if (ce->client == client)
425 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
428 ce = GNUNET_new (struct ClientEntry);
430 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
431 GNUNET_SERVER_notification_context_add (nc, client);
437 * Handle GET-message.
440 * @param client identification of the client
441 * @param message the actual message
442 * @return GNUNET_OK to keep the connection open,
443 * GNUNET_SYSERR to close it (signal serious error)
446 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
447 const struct GNUNET_MessageHeader *message)
449 struct GNUNET_MessageHeader end;
452 struct StatsEntry *pos;
455 if ( (NULL != client) &&
456 (NULL == make_client_entry (client)) )
457 return; /* new client during shutdown */
458 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
460 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
464 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "Received request for statistics on `%s:%s'\n",
469 strlen (service) ? service : "*", strlen (name) ? name : "*");
470 for (pos = start; NULL != pos; pos = pos->next)
471 if (matches (pos, service, name))
472 transmit (client, pos);
473 end.size = htons (sizeof (struct GNUNET_MessageHeader));
474 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
475 GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
476 GNUNET_SERVER_receive_done (client, GNUNET_OK);
481 * Notify all clients listening about a change to a value.
483 * @param se value that changed
486 notify_change (struct StatsEntry *se)
488 struct GNUNET_STATISTICS_WatchValueMessage wvm;
489 struct WatchEntry *pos;
491 for (pos = se->we_head; NULL != pos; pos = pos->next)
493 if (GNUNET_YES == pos->last_value_set)
495 if (pos->last_value == se->value)
500 pos->last_value_set = GNUNET_YES;
502 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
504 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
505 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
506 wvm.wid = htonl (pos->wid);
507 wvm.reserved = htonl (0);
508 wvm.value = GNUNET_htonll (se->value);
509 GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
511 pos->last_value = se->value;
517 * Handle SET-message.
520 * @param client identification of the client
521 * @param message the actual message
524 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
525 const struct GNUNET_MessageHeader *message)
531 const struct GNUNET_STATISTICS_SetMessage *msg;
532 struct StatsEntry *pos;
533 struct StatsEntry *prev;
540 if ( (NULL != client) &&
541 (NULL == make_client_entry (client)) )
542 return; /* new client during shutdown */
543 msize = ntohs (message->size);
544 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
547 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
550 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
551 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
554 GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
558 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
561 flags = ntohl (msg->flags);
562 value = GNUNET_ntohll (msg->value);
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
565 service, name, (unsigned int) flags, (unsigned long long) value);
570 if (matches (pos, service, name))
573 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
575 changed = (pos->value != value);
580 delta = (int64_t) value;
581 if ((delta < 0) && (pos->value < -delta))
583 changed = (pos->value != 0);
588 changed = (delta != 0);
589 GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
593 if (GNUNET_NO == pos->set)
595 pos->set = GNUNET_YES;
598 pos->msg->value = GNUNET_htonll (pos->value);
599 pos->msg->flags = msg->flags;
600 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
603 /* move to front for faster setting next time! */
604 prev->next = pos->next;
608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
609 "Statistic `%s:%s' updated to value %llu.\n", service, name,
611 if ((changed) || (1 == initial_set))
613 GNUNET_SERVER_receive_done (client, GNUNET_OK);
619 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
621 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
622 (0 < (int64_t) GNUNET_ntohll (msg->value)))
624 pos->value = GNUNET_ntohll (msg->value);
625 pos->set = GNUNET_YES;
629 pos->set = GNUNET_NO;
632 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
633 pos->msg = (void *) &pos[1];
634 memcpy (pos->msg, message, ntohs (message->size));
635 pos->service = (const char *) &pos->msg[1];
636 pos->name = &pos->service[strlen (pos->service) + 1];
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "New statistic on `%s:%s' with value %llu created.\n", service,
642 GNUNET_SERVER_receive_done (client, GNUNET_OK);
647 * Handle WATCH-message.
650 * @param client identification of the client
651 * @param message the actual message
654 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
655 const struct GNUNET_MessageHeader *message)
661 struct StatsEntry *pos;
662 struct ClientEntry *ce;
663 struct WatchEntry *we;
668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
671 GNUNET_SERVER_client_mark_monitor (client);
672 ce = make_client_entry (client);
673 msize = ntohs (message->size);
674 if (msize < sizeof (struct GNUNET_MessageHeader))
677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
680 size = msize - sizeof (struct GNUNET_MessageHeader);
682 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
686 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Received request to watch statistic on `%s:%s'\n", service,
695 if (matches (pos, service, name))
702 GNUNET_malloc (sizeof (struct StatsEntry) +
703 sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
706 pos->set = GNUNET_NO;
707 pos->msg = (void *) &pos[1];
708 pos->msg->header.size =
709 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
710 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
711 pos->service = (const char *) &pos->msg[1];
712 slen = strlen (service) + 1;
713 memcpy ((void *) pos->service, service, slen);
714 pos->name = &pos->service[slen];
715 memcpy ((void *) pos->name, name, strlen (name) + 1);
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "New statistic on `%s:%s' with value %llu created.\n", service,
721 we = GNUNET_new (struct WatchEntry);
723 we->last_value_set = GNUNET_NO;
724 we->wid = ce->max_wid++;
725 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
728 GNUNET_SERVER_receive_done (client, GNUNET_OK);
733 * Actually perform the shutdown.
738 struct WatchEntry *we;
739 struct StatsEntry *se;
744 GNUNET_SERVER_notification_context_destroy (nc);
746 GNUNET_assert (NULL == client_head);
747 while (NULL != (se = start))
750 while (NULL != (we = se->we_head))
752 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
761 * Task run during shutdown.
767 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
769 in_shutdown = GNUNET_YES;
770 if (NULL != client_head)
777 * A client disconnected. Remove all of its data structure entries.
779 * @param cls closure, NULL
780 * @param client identification of the client
783 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
785 struct ClientEntry *ce;
786 struct WatchEntry *we;
787 struct WatchEntry *wen;
788 struct StatsEntry *se;
793 if (ce->client == client)
795 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
805 while (NULL != (we = wen))
808 if (we->client != client)
810 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
815 if ( (NULL == client_head) &&
816 (GNUNET_YES == in_shutdown) )
822 * Process statistics requests.
825 * @param server the initialized server
826 * @param c configuration to use
829 run (void *cls, struct GNUNET_SERVER_Handle *server,
830 const struct GNUNET_CONFIGURATION_Handle *c)
832 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
833 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
834 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
835 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
840 GNUNET_SERVER_add_handlers (server, handlers);
841 nc = GNUNET_SERVER_notification_context_create (server, 16);
842 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
844 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
850 * The main function for the statistics service.
852 * @param argc number of arguments from the command line
853 * @param argv command line arguments
854 * @return 0 ok, 1 on error
857 main (int argc, char *const *argv)
860 GNUNET_SERVICE_run (argc, argv, "statistics",
861 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, &run, NULL)) ? 0 : 1;
868 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
870 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
872 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
873 mallopt (M_TOP_PAD, 1 * 1024);
879 /* end of gnunet-service-statistics.c */