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;
244 fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
248 if ((GNUNET_OK != GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) || (fsize == 0))
253 buf = GNUNET_malloc (fsize);
254 rh = GNUNET_BIO_read_open (fn);
261 if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
263 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
264 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
266 GNUNET_free_non_null (emsg);
270 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
271 _("Loading %llu bytes of statistics from `%s'\n"),
273 mst = GNUNET_SERVER_mst_create (&inject_message, server);
274 GNUNET_break (GNUNET_OK ==
275 GNUNET_SERVER_mst_receive (mst, NULL, buf, fsize,
276 GNUNET_YES, GNUNET_NO));
277 GNUNET_SERVER_mst_destroy (mst);
279 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
280 GNUNET_free_non_null (emsg);
286 * Write persistent statistics to disk.
291 struct StatsEntry *pos;
293 struct GNUNET_BIO_WriteHandle *wh;
296 unsigned long long total;
299 fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
302 wh = GNUNET_BIO_write_open (fn);
304 while (NULL != (pos = start))
307 if ((pos->persistent) && (NULL != wh))
309 size = htons (pos->msg->header.size);
310 if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
312 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
313 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
314 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
324 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
325 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
327 GNUNET_break (0 == UNLINK (fn));
329 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
330 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
332 GNUNET_free_non_null (fn);
337 * Transmit the given stats value.
339 * @param client receiver of the value
340 * @param e value to transmit
343 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
345 struct GNUNET_STATISTICS_ReplyMessage *m;
349 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
350 strlen (e->name) + 1;
351 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
352 m = GNUNET_malloc (size);
353 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
354 m->header.size = htons (size);
355 m->uid = htonl (e->uid);
357 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
358 m->value = GNUNET_htonll (e->value);
359 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
360 GNUNET_assert (size ==
361 GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
362 e->service, e->name));
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364 "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
365 e->name, e->persistent, e->value);
366 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
373 * Does this entry match the request?
376 * @param service name of service to match
377 * @param name value to match
378 * @return 1 if they match, 0 if not
381 matches (const struct StatsEntry *e, const char *service, const char *name)
383 return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
384 ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
389 * Find a client entry for the given client handle, or create one.
391 * @param client handle to match
392 * @return corresponding client entry struct
394 static struct ClientEntry *
395 make_client_entry (struct GNUNET_SERVER_Client *client)
397 struct ClientEntry *ce;
399 GNUNET_assert (client != NULL);
403 if (ce->client == client)
409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
412 ce = GNUNET_new (struct ClientEntry);
414 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
415 GNUNET_SERVER_notification_context_add (nc, client);
421 * Handle GET-message.
424 * @param client identification of the client
425 * @param message the actual message
426 * @return GNUNET_OK to keep the connection open,
427 * GNUNET_SYSERR to close it (signal serious error)
430 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
431 const struct GNUNET_MessageHeader *message)
433 struct GNUNET_MessageHeader end;
436 struct StatsEntry *pos;
439 if ( (NULL != client) &&
440 (NULL == make_client_entry (client)) )
441 return; /* new client during shutdown */
442 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
444 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
448 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "Received request for statistics on `%s:%s'\n",
453 strlen (service) ? service : "*", strlen (name) ? name : "*");
454 for (pos = start; NULL != pos; pos = pos->next)
455 if (matches (pos, service, name))
456 transmit (client, pos);
457 end.size = htons (sizeof (struct GNUNET_MessageHeader));
458 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
459 GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
460 GNUNET_SERVER_receive_done (client, GNUNET_OK);
465 * Notify all clients listening about a change to a value.
467 * @param se value that changed
470 notify_change (struct StatsEntry *se)
472 struct GNUNET_STATISTICS_WatchValueMessage wvm;
473 struct WatchEntry *pos;
475 for (pos = se->we_head; NULL != pos; pos = pos->next)
477 if (GNUNET_YES == pos->last_value_set)
479 if (pos->last_value == se->value)
484 pos->last_value_set = GNUNET_YES;
486 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
488 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
489 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
490 wvm.wid = htonl (pos->wid);
491 wvm.reserved = htonl (0);
492 wvm.value = GNUNET_htonll (se->value);
493 GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
495 pos->last_value = se->value;
501 * Handle SET-message.
504 * @param client identification of the client
505 * @param message the actual message
508 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
509 const struct GNUNET_MessageHeader *message)
515 const struct GNUNET_STATISTICS_SetMessage *msg;
516 struct StatsEntry *pos;
517 struct StatsEntry *prev;
524 if ( (NULL != client) &&
525 (NULL == make_client_entry (client)) )
526 return; /* new client during shutdown */
527 msize = ntohs (message->size);
528 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
534 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
535 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
538 GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
545 flags = ntohl (msg->flags);
546 value = GNUNET_ntohll (msg->value);
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
549 service, name, (unsigned int) flags, (unsigned long long) value);
554 if (matches (pos, service, name))
557 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
559 changed = (pos->value != value);
564 delta = (int64_t) value;
565 if ((delta < 0) && (pos->value < -delta))
567 changed = (pos->value != 0);
572 changed = (delta != 0);
573 GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
577 if (GNUNET_NO == pos->set)
579 pos->set = GNUNET_YES;
582 pos->msg->value = GNUNET_htonll (pos->value);
583 pos->msg->flags = msg->flags;
584 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
587 /* move to front for faster setting next time! */
588 prev->next = pos->next;
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "Statistic `%s:%s' updated to value %llu.\n", service, name,
595 if ((changed) || (1 == initial_set))
597 GNUNET_SERVER_receive_done (client, GNUNET_OK);
603 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
605 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
606 (0 < (int64_t) GNUNET_ntohll (msg->value)))
608 pos->value = GNUNET_ntohll (msg->value);
609 pos->set = GNUNET_YES;
613 pos->set = GNUNET_NO;
616 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
617 pos->msg = (void *) &pos[1];
618 memcpy (pos->msg, message, ntohs (message->size));
619 pos->service = (const char *) &pos->msg[1];
620 pos->name = &pos->service[strlen (pos->service) + 1];
623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
624 "New statistic on `%s:%s' with value %llu created.\n", service,
626 GNUNET_SERVER_receive_done (client, GNUNET_OK);
631 * Handle WATCH-message.
634 * @param client identification of the client
635 * @param message the actual message
638 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
639 const struct GNUNET_MessageHeader *message)
645 struct StatsEntry *pos;
646 struct ClientEntry *ce;
647 struct WatchEntry *we;
652 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
655 GNUNET_SERVER_client_mark_monitor (client);
656 ce = make_client_entry (client);
657 msize = ntohs (message->size);
658 if (msize < sizeof (struct GNUNET_MessageHeader))
661 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
664 size = msize - sizeof (struct GNUNET_MessageHeader);
666 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674 "Received request to watch statistic on `%s:%s'\n", service,
679 if (matches (pos, service, name))
686 GNUNET_malloc (sizeof (struct StatsEntry) +
687 sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
690 pos->set = GNUNET_NO;
691 pos->msg = (void *) &pos[1];
692 pos->msg->header.size =
693 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
694 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
695 pos->service = (const char *) &pos->msg[1];
696 slen = strlen (service) + 1;
697 memcpy ((void *) pos->service, service, slen);
698 pos->name = &pos->service[slen];
699 memcpy ((void *) pos->name, name, strlen (name) + 1);
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "New statistic on `%s:%s' with value %llu created.\n", service,
705 we = GNUNET_new (struct WatchEntry);
707 we->last_value_set = GNUNET_NO;
708 we->wid = ce->max_wid++;
709 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
712 GNUNET_SERVER_receive_done (client, GNUNET_OK);
717 * Actually perform the shutdown.
722 struct WatchEntry *we;
723 struct StatsEntry *se;
728 GNUNET_SERVER_notification_context_destroy (nc);
730 GNUNET_assert (NULL == client_head);
731 while (NULL != (se = start))
734 while (NULL != (we = se->we_head))
736 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
745 * Task run during shutdown.
751 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
753 in_shutdown = GNUNET_YES;
754 if (NULL != client_head)
761 * A client disconnected. Remove all of its data structure entries.
763 * @param cls closure, NULL
764 * @param client identification of the client
767 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
769 struct ClientEntry *ce;
770 struct WatchEntry *we;
771 struct WatchEntry *wen;
772 struct StatsEntry *se;
777 if (ce->client == client)
779 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
789 while (NULL != (we = wen))
792 if (we->client != client)
794 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
799 if ( (NULL == client_head) &&
800 (GNUNET_YES == in_shutdown) )
806 * Process statistics requests.
809 * @param server the initialized server
810 * @param c configuration to use
813 run (void *cls, struct GNUNET_SERVER_Handle *server,
814 const struct GNUNET_CONFIGURATION_Handle *c)
816 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
817 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
818 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
819 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
824 GNUNET_SERVER_add_handlers (server, handlers);
825 nc = GNUNET_SERVER_notification_context_create (server, 16);
826 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
828 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
834 * The main function for the statistics service.
836 * @param argc number of arguments from the command line
837 * @param argv command line arguments
838 * @return 0 ok, 1 on error
841 main (int argc, char *const *argv)
844 GNUNET_SERVICE_run (argc, argv, "statistics",
845 GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, &run, NULL)) ? 0 : 1;
852 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
854 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
856 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
857 mallopt (M_TOP_PAD, 1 * 1024);
863 /* end of gnunet-service-statistics.c */