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));
227 * Load persistent values from disk. Disk format is
228 * exactly the same format that we also use for
229 * setting the values over the network.
231 * @param server handle to the server context
234 load (struct GNUNET_SERVER_Handle *server)
237 struct GNUNET_BIO_ReadHandle *rh;
240 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
243 fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
247 if ((GNUNET_OK != GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) || (fsize == 0))
252 buf = GNUNET_malloc (fsize);
253 rh = GNUNET_BIO_read_open (fn);
260 if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
262 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
263 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
265 GNUNET_free_non_null (emsg);
269 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
270 _("Loading %llu bytes of statistics from `%s'\n"),
272 mst = GNUNET_SERVER_mst_create (&inject_message, server);
273 GNUNET_break (GNUNET_OK ==
274 GNUNET_SERVER_mst_receive (mst, NULL, buf, fsize,
275 GNUNET_YES, GNUNET_NO));
276 GNUNET_SERVER_mst_destroy (mst);
278 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
279 GNUNET_free_non_null (emsg);
285 * Write persistent statistics to disk.
290 struct StatsEntry *pos;
292 struct GNUNET_BIO_WriteHandle *wh;
295 unsigned long long total;
298 fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
301 wh = GNUNET_BIO_write_open (fn);
303 while (NULL != (pos = start))
306 if ((pos->persistent) && (NULL != wh))
308 size = htons (pos->msg->header.size);
309 if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
311 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
312 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
313 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
323 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
324 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
326 GNUNET_break (0 == UNLINK (fn));
328 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
329 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
331 GNUNET_free_non_null (fn);
336 * Transmit the given stats value.
338 * @param client receiver of the value
339 * @param e value to transmit
342 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
344 struct GNUNET_STATISTICS_ReplyMessage *m;
348 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
349 strlen (e->name) + 1;
350 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
351 m = GNUNET_malloc (size);
352 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
353 m->header.size = htons (size);
354 m->uid = htonl (e->uid);
356 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
357 m->value = GNUNET_htonll (e->value);
358 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
359 GNUNET_assert (size ==
360 GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
361 e->service, e->name));
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
364 e->name, e->persistent, e->value);
365 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
372 * Does this entry match the request?
375 * @param service name of service to match
376 * @param name value to match
377 * @return 1 if they match, 0 if not
380 matches (const struct StatsEntry *e, const char *service, const char *name)
382 return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
383 ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
388 * Find a client entry for the given client handle, or create one.
390 * @param client handle to match
391 * @return corresponding client entry struct
393 static struct ClientEntry *
394 make_client_entry (struct GNUNET_SERVER_Client *client)
396 struct ClientEntry *ce;
398 GNUNET_assert (client != NULL);
402 if (ce->client == client)
408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
411 ce = GNUNET_malloc (sizeof (struct ClientEntry));
413 GNUNET_SERVER_client_keep (client);
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;
500 * Handle SET-message.
503 * @param client identification of the client
504 * @param message the actual message
507 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
508 const struct GNUNET_MessageHeader *message)
514 const struct GNUNET_STATISTICS_SetMessage *msg;
515 struct StatsEntry *pos;
516 struct StatsEntry *prev;
523 if ( (NULL != client) &&
524 (NULL == make_client_entry (client)) )
525 return; /* new client during shutdown */
526 msize = ntohs (message->size);
527 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
530 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
533 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
534 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
537 GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
541 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
544 flags = ntohl (msg->flags);
545 value = GNUNET_ntohll (msg->value);
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
548 service, name, (unsigned int) flags, (unsigned long long) value);
553 if (matches (pos, service, name))
556 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
558 changed = (pos->value != value);
563 delta = (int64_t) value;
564 if ((delta < 0) && (pos->value < -delta))
566 changed = (pos->value != 0);
571 changed = (delta != 0);
572 GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
576 if (GNUNET_NO == pos->set)
578 pos->set = GNUNET_YES;
581 pos->msg->value = GNUNET_htonll (pos->value);
582 pos->msg->flags = msg->flags;
583 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
586 /* move to front for faster setting next time! */
587 prev->next = pos->next;
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "Statistic `%s:%s' updated to value %llu.\n", service, name,
594 if ((changed) || (1 == initial_set))
596 GNUNET_SERVER_receive_done (client, GNUNET_OK);
602 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
604 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
605 (0 < (int64_t) GNUNET_ntohll (msg->value)))
607 pos->value = GNUNET_ntohll (msg->value);
608 pos->set = GNUNET_YES;
612 pos->set = GNUNET_NO;
615 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
616 pos->msg = (void *) &pos[1];
617 memcpy (pos->msg, message, ntohs (message->size));
618 pos->service = (const char *) &pos->msg[1];
619 pos->name = &pos->service[strlen (pos->service) + 1];
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623 "New statistic on `%s:%s' with value %llu created.\n", service,
625 GNUNET_SERVER_receive_done (client, GNUNET_OK);
630 * Handle WATCH-message.
633 * @param client identification of the client
634 * @param message the actual message
637 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
638 const struct GNUNET_MessageHeader *message)
644 struct StatsEntry *pos;
645 struct ClientEntry *ce;
646 struct WatchEntry *we;
651 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
654 ce = make_client_entry (client);
655 msize = ntohs (message->size);
656 if (msize < sizeof (struct GNUNET_MessageHeader))
659 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
662 size = msize - sizeof (struct GNUNET_MessageHeader);
664 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
672 "Received request to watch statistic on `%s:%s'\n", service,
677 if (matches (pos, service, name))
684 GNUNET_malloc (sizeof (struct StatsEntry) +
685 sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
688 pos->set = GNUNET_NO;
689 pos->msg = (void *) &pos[1];
690 pos->msg->header.size =
691 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
692 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
693 pos->service = (const char *) &pos->msg[1];
694 slen = strlen (service) + 1;
695 memcpy ((void *) pos->service, service, slen);
696 pos->name = &pos->service[slen];
697 memcpy ((void *) pos->name, name, strlen (name) + 1);
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 "New statistic on `%s:%s' with value %llu created.\n", service,
703 we = GNUNET_malloc (sizeof (struct WatchEntry));
705 we->last_value_set = GNUNET_NO;
706 GNUNET_SERVER_client_keep (client);
707 we->wid = ce->max_wid++;
708 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
711 GNUNET_SERVER_receive_done (client, GNUNET_OK);
716 * Actually perform the shutdown.
721 struct WatchEntry *we;
722 struct StatsEntry *se;
727 GNUNET_SERVER_notification_context_destroy (nc);
729 GNUNET_assert (NULL == client_head);
730 while (NULL != (se = start))
733 while (NULL != (we = se->we_head))
735 GNUNET_SERVER_client_drop (we->client);
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_SERVER_client_drop (ce->client);
780 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
790 while (NULL != (we = wen))
793 if (we->client != client)
795 GNUNET_SERVER_client_drop (we->client);
796 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
801 if ( (NULL == client_head) &&
802 (GNUNET_YES == in_shutdown) )
808 * Process statistics requests.
811 * @param server the initialized server
812 * @param c configuration to use
815 run (void *cls, struct GNUNET_SERVER_Handle *server,
816 const struct GNUNET_CONFIGURATION_Handle *c)
818 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
819 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
820 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
821 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
826 GNUNET_SERVER_add_handlers (server, handlers);
827 GNUNET_SERVER_ignore_shutdown (server, GNUNET_YES);
828 nc = GNUNET_SERVER_notification_context_create (server, 16);
829 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
831 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
837 * The main function for the statistics service.
839 * @param argc number of arguments from the command line
840 * @param argv command line arguments
841 * @return 0 ok, 1 on error
844 main (int argc, char *const *argv)
847 GNUNET_SERVICE_run (argc, argv, "statistics",
848 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
851 /* end of gnunet-service-statistics.c */