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.
78 * Clients are kept in a linked list.
80 struct ClientEntry *next;
83 * Clients are kept in a linked list.
85 struct ClientEntry *prev;
88 * Corresponding server handle.
90 struct GNUNET_SERVER_Client *client;
93 * Maximum watch ID used by this client so far.
100 * Entry in the statistics list.
105 * This is a linked list.
107 struct StatsEntry *next;
110 * Name of the service, points into the
116 * Name for the value, points into
122 * Message that can be used to set this value,
123 * stored at the end of the memory used by
126 struct GNUNET_STATISTICS_SetMessage *msg;
129 * Watch context for changes to this
130 * value, or NULL for none.
132 struct WatchEntry *we_head;
135 * Watch context for changes to this
136 * value, or NULL for none.
138 struct WatchEntry *we_tail;
151 * Is this value persistent?
160 static const struct GNUNET_CONFIGURATION_Handle *cfg;
163 * Linked list of our active statistics.
165 static struct StatsEntry *start;
168 * Head of linked list of connected clients.
170 static struct ClientEntry *client_head;
173 * Tail of linked list of connected clients.
175 static struct ClientEntry *client_tail;
178 * Handle to our server.
180 static struct GNUNET_SERVER_Handle *srv;
183 * Our notification context.
185 static struct GNUNET_SERVER_NotificationContext *nc;
188 * Counter used to generate unique values.
190 static uint32_t uidgen;
193 * Set to YES if we are shutting down as soon as possible.
195 static int in_shutdown;
199 * Inject a message to our server with a client of 'NULL'.
201 * @param cls the 'struct GNUNET_SERVER_Handle'
202 * @param client unused
203 * @param msg message to inject
206 inject_message (void *cls, void *client, const struct GNUNET_MessageHeader *msg)
208 struct GNUNET_SERVER_Handle *server = cls;
210 GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
215 * Load persistent values from disk. Disk format is
216 * exactly the same format that we also use for
217 * setting the values over the network.
219 * @param server handle to the server context
222 load (struct GNUNET_SERVER_Handle *server)
225 struct GNUNET_BIO_ReadHandle *rh;
228 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
231 fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
235 if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
240 buf = GNUNET_malloc (sb.st_size);
241 rh = GNUNET_BIO_read_open (fn);
248 if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, sb.st_size))
250 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
251 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
253 GNUNET_free_non_null (emsg);
257 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
258 _("Loading %llu bytes of statistics from `%s'\n"),
259 (unsigned long long) sb.st_size, fn);
260 mst = GNUNET_SERVER_mst_create (&inject_message, server);
261 GNUNET_break (GNUNET_OK ==
262 GNUNET_SERVER_mst_receive (mst, NULL, buf, sb.st_size,
263 GNUNET_YES, GNUNET_NO));
264 GNUNET_SERVER_mst_destroy (mst);
266 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
267 GNUNET_free_non_null (emsg);
273 * Write persistent statistics to disk.
278 struct StatsEntry *pos;
280 struct GNUNET_BIO_WriteHandle *wh;
283 unsigned long long total;
286 fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
289 wh = GNUNET_BIO_write_open (fn);
291 while (NULL != (pos = start))
294 if ((pos->persistent) && (NULL != wh))
296 size = htons (pos->msg->header.size);
297 if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
299 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
300 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
301 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
311 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
312 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
314 GNUNET_break (0 == UNLINK (fn));
316 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
317 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
319 GNUNET_free_non_null (fn);
324 * Transmit the given stats value.
326 * @param client receiver of the value
327 * @param e value to transmit
330 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
332 struct GNUNET_STATISTICS_ReplyMessage *m;
336 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
337 strlen (e->name) + 1;
338 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
339 m = GNUNET_malloc (size);
340 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
341 m->header.size = htons (size);
342 m->uid = htonl (e->uid);
344 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
345 m->value = GNUNET_htonll (e->value);
346 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
347 GNUNET_assert (size ==
348 GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
349 e->service, e->name));
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351 "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
352 e->name, e->persistent, e->value);
353 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
360 * Does this entry match the request?
363 * @param service name of service to match
364 * @param name value to match
365 * @return 1 if they match, 0 if not
368 matches (const struct StatsEntry *e, const char *service, const char *name)
370 return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
371 ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
376 * Find a client entry for the given client handle, or create one.
378 * @param client handle to match
379 * @return corresponding client entry struct
381 static struct ClientEntry *
382 make_client_entry (struct GNUNET_SERVER_Client *client)
384 struct ClientEntry *ce;
386 GNUNET_assert (client != NULL);
390 if (ce->client == client)
394 ce = GNUNET_malloc (sizeof (struct ClientEntry));
396 GNUNET_SERVER_client_keep (client);
397 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
398 GNUNET_SERVER_notification_context_add (nc, client);
404 * Handle GET-message.
407 * @param client identification of the client
408 * @param message the actual message
409 * @return GNUNET_OK to keep the connection open,
410 * GNUNET_SYSERR to close it (signal serious error)
413 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
414 const struct GNUNET_MessageHeader *message)
416 struct GNUNET_MessageHeader end;
419 struct StatsEntry *pos;
423 make_client_entry (client);
424 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
426 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Received request for statistics on `%s:%s'\n",
435 strlen (service) ? service : "*", strlen (name) ? name : "*");
436 for (pos = start; NULL != pos; pos = pos->next)
437 if (matches (pos, service, name))
438 transmit (client, pos);
439 end.size = htons (sizeof (struct GNUNET_MessageHeader));
440 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
441 GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
442 GNUNET_SERVER_receive_done (client, GNUNET_OK);
447 * Notify all clients listening about a change to a value.
449 * @param se value that changed
452 notify_change (struct StatsEntry *se)
454 struct GNUNET_STATISTICS_WatchValueMessage wvm;
455 struct WatchEntry *pos;
457 for (pos = se->we_head; NULL != pos; pos = pos->next)
459 if (pos->last_value == se->value)
461 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
463 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
464 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
465 wvm.wid = htonl (pos->wid);
466 wvm.reserved = htonl (0);
467 wvm.value = GNUNET_htonll (se->value);
468 GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
470 pos->last_value = se->value;
475 * Handle SET-message.
478 * @param client identification of the client
479 * @param message the actual message
482 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
483 const struct GNUNET_MessageHeader *message)
489 const struct GNUNET_STATISTICS_SetMessage *msg;
490 struct StatsEntry *pos;
491 struct StatsEntry *prev;
498 make_client_entry (client);
499 msize = ntohs (message->size);
500 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
506 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
507 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
510 GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
514 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
517 flags = ntohl (msg->flags);
518 value = GNUNET_ntohll (msg->value);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
521 service, name, (unsigned int) flags, (unsigned long long) value);
526 if (matches (pos, service, name))
528 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
530 changed = (pos->value != value);
535 delta = (int64_t) value;
536 if ((delta < 0) && (pos->value < -delta))
538 changed = (pos->value != 0);
543 changed = (delta != 0);
544 GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
548 pos->msg->value = GNUNET_htonll (pos->value);
549 pos->msg->flags = msg->flags;
550 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
553 /* move to front for faster setting next time! */
554 prev->next = pos->next;
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 "Statistic `%s:%s' updated to value %llu.\n", service, name,
563 GNUNET_SERVER_receive_done (client, GNUNET_OK);
569 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
571 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
572 (0 < (int64_t) GNUNET_ntohll (msg->value)))
573 pos->value = GNUNET_ntohll (msg->value);
575 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
576 pos->msg = (void *) &pos[1];
577 memcpy (pos->msg, message, ntohs (message->size));
578 pos->service = (const char *) &pos->msg[1];
579 pos->name = &pos->service[strlen (pos->service) + 1];
582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
583 "New statistic on `%s:%s' with value %llu created.\n", service,
585 GNUNET_SERVER_receive_done (client, GNUNET_OK);
590 * Handle WATCH-message.
593 * @param client identification of the client
594 * @param message the actual message
597 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
598 const struct GNUNET_MessageHeader *message)
604 struct StatsEntry *pos;
605 struct ClientEntry *ce;
606 struct WatchEntry *we;
609 ce = make_client_entry (client);
610 msize = ntohs (message->size);
611 if (msize < sizeof (struct GNUNET_MessageHeader))
614 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
617 size = msize - sizeof (struct GNUNET_MessageHeader);
619 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "Received request to watch statistic on `%s:%s'\n", service,
632 if (matches (pos, service, name))
639 GNUNET_malloc (sizeof (struct StatsEntry) +
640 sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
643 pos->msg = (void *) &pos[1];
644 pos->msg->header.size =
645 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
646 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
647 pos->service = (const char *) &pos->msg[1];
648 slen = strlen (service) + 1;
649 memcpy ((void *) pos->service, service, slen);
650 pos->name = &pos->service[slen];
651 memcpy ((void *) pos->name, name, strlen (name) + 1);
654 we = GNUNET_malloc (sizeof (struct WatchEntry));
656 GNUNET_SERVER_client_keep (client);
657 we->wid = ce->max_wid++;
658 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
661 GNUNET_SERVER_receive_done (client, GNUNET_OK);
666 * Actually perform the shutdown.
671 struct WatchEntry *we;
672 struct StatsEntry *se;
675 GNUNET_SERVER_notification_context_destroy (nc);
677 GNUNET_assert (NULL == client_head);
678 while (NULL != (se = start))
681 while (NULL != (we = se->we_head))
683 GNUNET_SERVER_client_drop (we->client);
684 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
689 GNUNET_SERVER_destroy (srv);
694 * Task run during shutdown.
700 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
702 in_shutdown = GNUNET_YES;
703 if (NULL != client_head)
710 * A client disconnected. Remove all of its data structure entries.
712 * @param cls closure, NULL
713 * @param client identification of the client
716 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
718 struct ClientEntry *ce;
719 struct WatchEntry *we;
720 struct WatchEntry *wen;
721 struct StatsEntry *se;
726 if (ce->client == client)
728 GNUNET_SERVER_client_drop (ce->client);
729 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
739 while (NULL != (we = wen))
742 if (we->client != client)
744 GNUNET_SERVER_client_drop (we->client);
745 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
750 if ( (NULL == client_head) &&
751 (GNUNET_YES == in_shutdown) )
757 * Process statistics requests.
760 * @param server the initialized server
761 * @param c configuration to use
764 run (void *cls, struct GNUNET_SERVER_Handle *server,
765 const struct GNUNET_CONFIGURATION_Handle *c)
767 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
768 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
769 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
770 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
775 GNUNET_SERVER_add_handlers (server, handlers);
776 GNUNET_SERVER_ignore_shutdown (server, GNUNET_YES);
777 nc = GNUNET_SERVER_notification_context_create (server, 16);
778 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
780 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
786 * The main function for the statistics service.
788 * @param argc number of arguments from the command line
789 * @param argv command line arguments
790 * @return 0 ok, 1 on error
793 main (int argc, char *const *argv)
796 GNUNET_SERVICE_run (argc, argv, "statistics",
797 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
800 /* end of gnunet-service-statistics.c */