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"
44 struct WatchEntry *next;
46 struct WatchEntry *prev;
48 struct GNUNET_SERVER_Client *client;
63 struct ClientEntry *next;
65 struct ClientEntry *prev;
67 struct GNUNET_SERVER_Client *client;
74 * Entry in the statistics list.
79 * This is a linked list.
81 struct StatsEntry *next;
84 * Name of the service, points into the
90 * Name for the value, points into
96 * Message that can be used to set this value,
97 * stored at the end of the memory used by
100 struct GNUNET_STATISTICS_SetMessage *msg;
103 * Watch context for changes to this
104 * value, or NULL for none.
106 struct WatchEntry *we_head;
109 * Watch context for changes to this
110 * value, or NULL for none.
112 struct WatchEntry *we_tail;
125 * Is this value persistent?
134 static const struct GNUNET_CONFIGURATION_Handle *cfg;
137 * Linked list of our active statistics.
139 static struct StatsEntry *start;
141 static struct ClientEntry *client_head;
143 static struct ClientEntry *client_tail;
146 * Our notification context.
148 static struct GNUNET_SERVER_NotificationContext *nc;
151 * Counter used to generate unique values.
153 static uint32_t uidgen;
157 inject_message (void *cls, void *client, const struct GNUNET_MessageHeader *msg)
159 struct GNUNET_SERVER_Handle *server = cls;
161 GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
166 * Load persistent values from disk. Disk format is
167 * exactly the same format that we also use for
168 * setting the values over the network.
170 * @param server handle to the server context
173 load (struct GNUNET_SERVER_Handle *server)
176 struct GNUNET_BIO_ReadHandle *rh;
179 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
182 fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
186 if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
191 buf = GNUNET_malloc (sb.st_size);
192 rh = GNUNET_BIO_read_open (fn);
199 if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, sb.st_size))
201 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
202 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
204 GNUNET_free_non_null (emsg);
208 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
209 _("Loading %llu bytes of statistics from `%s'\n"),
210 (unsigned long long) sb.st_size, fn);
211 mst = GNUNET_SERVER_mst_create (&inject_message, server);
212 GNUNET_break (GNUNET_OK ==
213 GNUNET_SERVER_mst_receive (mst, NULL, buf, sb.st_size,
214 GNUNET_YES, GNUNET_NO));
215 GNUNET_SERVER_mst_destroy (mst);
217 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
218 GNUNET_free_non_null (emsg);
223 * Write persistent statistics to disk.
228 struct StatsEntry *pos;
230 struct GNUNET_BIO_WriteHandle *wh;
233 unsigned long long total;
236 fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
239 wh = GNUNET_BIO_write_open (fn);
241 while (NULL != (pos = start))
244 if ((pos->persistent) && (NULL != wh))
246 size = htons (pos->msg->header.size);
247 if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
249 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
250 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
251 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
261 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
262 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
264 GNUNET_break (0 == UNLINK (fn));
266 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
267 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
269 GNUNET_free_non_null (fn);
274 * Transmit the given stats value.
277 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
279 struct GNUNET_STATISTICS_ReplyMessage *m;
283 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
284 strlen (e->name) + 1;
285 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
286 m = GNUNET_malloc (size);
287 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
288 m->header.size = htons (size);
289 m->uid = htonl (e->uid);
291 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
292 m->value = GNUNET_htonll (e->value);
293 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
294 GNUNET_assert (size ==
295 GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
296 e->service, e->name));
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
300 e->name, e->persistent, e->value);
302 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
309 * Does this entry match the request?
312 matches (const struct StatsEntry *e, const char *service, const char *name)
314 return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
315 ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
319 static struct ClientEntry *
320 make_client_entry (struct GNUNET_SERVER_Client *client)
322 struct ClientEntry *ce;
324 GNUNET_assert (client != NULL);
328 if (ce->client == client)
332 ce = GNUNET_malloc (sizeof (struct ClientEntry));
334 GNUNET_SERVER_client_keep (client);
335 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
336 GNUNET_SERVER_notification_context_add (nc, client);
342 * Handle GET-message.
345 * @param client identification of the client
346 * @param message the actual message
347 * @return GNUNET_OK to keep the connection open,
348 * GNUNET_SYSERR to close it (signal serious error)
351 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
352 const struct GNUNET_MessageHeader *message)
354 struct GNUNET_MessageHeader end;
357 struct StatsEntry *pos;
361 make_client_entry (client);
362 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
364 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
368 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Received request for statistics on `%s:%s'\n",
374 strlen (service) ? service : "*", strlen (name) ? name : "*");
379 if (matches (pos, service, name))
380 transmit (client, pos);
383 end.size = htons (sizeof (struct GNUNET_MessageHeader));
384 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
385 GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
386 GNUNET_SERVER_receive_done (client, GNUNET_OK);
391 notify_change (struct StatsEntry *se)
393 struct GNUNET_STATISTICS_WatchValueMessage wvm;
394 struct WatchEntry *pos;
399 if (pos->last_value != se->value)
401 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
403 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
404 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
405 wvm.wid = htonl (pos->wid);
406 wvm.reserved = htonl (0);
407 wvm.value = GNUNET_htonll (se->value);
408 GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
410 pos->last_value = se->value;
417 * Handle SET-message.
420 * @param client identification of the client
421 * @param message the actual message
424 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
425 const struct GNUNET_MessageHeader *message)
431 const struct GNUNET_STATISTICS_SetMessage *msg;
432 struct StatsEntry *pos;
433 struct StatsEntry *prev;
440 make_client_entry (client);
441 msize = ntohs (message->size);
442 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
445 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
448 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
449 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
452 GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
459 flags = ntohl (msg->flags);
460 value = GNUNET_ntohll (msg->value);
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
464 service, name, (unsigned int) flags, (unsigned long long) value);
470 if (matches (pos, service, name))
472 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
474 changed = (pos->value != value);
479 delta = (int64_t) value;
480 if ((delta < 0) && (pos->value < -delta))
482 changed = (pos->value != 0);
487 changed = (delta != 0);
488 GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
492 pos->msg->value = GNUNET_htonll (pos->value);
493 pos->msg->flags = msg->flags;
494 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
497 /* move to front for faster setting next time! */
498 prev->next = pos->next;
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "Statistic `%s:%s' updated to value %llu.\n", service, name,
509 GNUNET_SERVER_receive_done (client, GNUNET_OK);
515 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
517 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
518 (0 < (int64_t) GNUNET_ntohll (msg->value)))
519 pos->value = GNUNET_ntohll (msg->value);
521 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
522 pos->msg = (void *) &pos[1];
523 memcpy (pos->msg, message, ntohs (message->size));
524 pos->service = (const char *) &pos->msg[1];
525 pos->name = &pos->service[strlen (pos->service) + 1];
529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
530 "New statistic on `%s:%s' with value %llu created.\n", service,
533 GNUNET_SERVER_receive_done (client, GNUNET_OK);
538 * Handle WATCH-message.
541 * @param client identification of the client
542 * @param message the actual message
545 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
546 const struct GNUNET_MessageHeader *message)
552 struct StatsEntry *pos;
553 struct ClientEntry *ce;
554 struct WatchEntry *we;
557 ce = make_client_entry (client);
558 msize = ntohs (message->size);
559 if (msize < sizeof (struct GNUNET_MessageHeader))
562 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
565 size = msize - sizeof (struct GNUNET_MessageHeader);
567 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
571 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "Received request to watch statistic on `%s:%s'\n", service,
582 if (matches (pos, service, name))
589 GNUNET_malloc (sizeof (struct StatsEntry) +
590 sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
593 pos->msg = (void *) &pos[1];
594 pos->msg->header.size =
595 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
596 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
597 pos->service = (const char *) &pos->msg[1];
598 slen = strlen (service) + 1;
599 memcpy ((void *) pos->service, service, slen);
600 pos->name = &pos->service[slen];
601 memcpy ((void *) pos->name, name, strlen (name) + 1);
604 we = GNUNET_malloc (sizeof (struct WatchEntry));
606 GNUNET_SERVER_client_keep (client);
607 we->wid = ce->max_wid++;
608 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
611 GNUNET_SERVER_receive_done (client, GNUNET_OK);
616 * Task run during shutdown.
622 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
624 struct ClientEntry *ce;
625 struct WatchEntry *we;
626 struct StatsEntry *se;
629 GNUNET_SERVER_notification_context_destroy (nc);
631 while (NULL != (ce = client_head))
633 GNUNET_SERVER_client_drop (ce->client);
634 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
637 while (NULL != (se = start))
640 while (NULL != (we = se->we_head))
642 GNUNET_SERVER_client_drop (we->client);
643 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
652 * A client disconnected. Remove all of its data structure entries.
654 * @param cls closure, NULL
655 * @param client identification of the client
658 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
660 struct ClientEntry *ce;
661 struct WatchEntry *we;
662 struct WatchEntry *wen;
663 struct StatsEntry *se;
668 if (ce->client == client)
670 GNUNET_SERVER_client_drop (ce->client);
671 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
681 while (NULL != (we = wen))
684 if (we->client != client)
686 GNUNET_SERVER_client_drop (we->client);
687 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
696 * Process statistics requests.
699 * @param server the initialized server
700 * @param c configuration to use
703 run (void *cls, struct GNUNET_SERVER_Handle *server,
704 const struct GNUNET_CONFIGURATION_Handle *c)
706 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
707 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
708 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
709 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
713 GNUNET_SERVER_add_handlers (server, handlers);
714 nc = GNUNET_SERVER_notification_context_create (server, 16);
715 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
717 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
723 * The main function for the statistics service.
725 * @param argc number of arguments from the command line
726 * @param argv command line arguments
727 * @return 0 ok, 1 on error
730 main (int argc, char *const *argv)
733 GNUNET_SERVICE_run (argc, argv, "statistics",
734 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
737 /* end of gnunet-service-statistics.c */