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);
198 if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, sb.st_size))
200 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
201 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
202 GNUNET_free_non_null (emsg);
206 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
207 _("Loading %llu bytes of statistics from `%s'\n"),
208 (unsigned long long) sb.st_size, fn);
209 mst = GNUNET_SERVER_mst_create (&inject_message, server);
210 GNUNET_break (GNUNET_OK ==
211 GNUNET_SERVER_mst_receive (mst, NULL, buf, sb.st_size,
212 GNUNET_YES, GNUNET_NO));
213 GNUNET_SERVER_mst_destroy (mst);
215 GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
216 GNUNET_free_non_null (emsg);
221 * Write persistent statistics to disk.
226 struct StatsEntry *pos;
228 struct GNUNET_BIO_WriteHandle *wh;
231 unsigned long long total;
234 fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
237 wh = GNUNET_BIO_write_open (fn);
239 while (NULL != (pos = start))
242 if ((pos->persistent) && (NULL != wh))
244 size = htons (pos->msg->header.size);
245 if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
247 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
248 GNUNET_BIO_write_close (wh);
258 GNUNET_BIO_write_close (wh);
260 GNUNET_break (0 == UNLINK (fn));
262 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
263 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
265 GNUNET_free_non_null (fn);
270 * Transmit the given stats value.
273 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
275 struct GNUNET_STATISTICS_ReplyMessage *m;
279 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
280 strlen (e->name) + 1;
281 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
282 m = GNUNET_malloc (size);
283 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
284 m->header.size = htons (size);
285 m->uid = htonl (e->uid);
287 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
288 m->value = GNUNET_htonll (e->value);
289 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
290 GNUNET_assert (size ==
291 GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
292 e->service, e->name));
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
296 e->name, e->persistent, e->value);
298 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
305 * Does this entry match the request?
308 matches (const struct StatsEntry *e, const char *service, const char *name)
310 return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
311 ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
315 static struct ClientEntry *
316 make_client_entry (struct GNUNET_SERVER_Client *client)
318 struct ClientEntry *ce;
320 GNUNET_assert (client != NULL);
324 if (ce->client == client)
328 ce = GNUNET_malloc (sizeof (struct ClientEntry));
330 GNUNET_SERVER_client_keep (client);
331 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
332 GNUNET_SERVER_notification_context_add (nc, client);
338 * Handle GET-message.
341 * @param client identification of the client
342 * @param message the actual message
343 * @return GNUNET_OK to keep the connection open,
344 * GNUNET_SYSERR to close it (signal serious error)
347 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
348 const struct GNUNET_MessageHeader *message)
350 struct GNUNET_MessageHeader end;
353 struct StatsEntry *pos;
357 make_client_entry (client);
358 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
360 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
364 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Received request for statistics on `%s:%s'\n",
370 strlen (service) ? service : "*", strlen (name) ? name : "*");
375 if (matches (pos, service, name))
376 transmit (client, pos);
379 end.size = htons (sizeof (struct GNUNET_MessageHeader));
380 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
381 GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
382 GNUNET_SERVER_receive_done (client, GNUNET_OK);
387 notify_change (struct StatsEntry *se)
389 struct GNUNET_STATISTICS_WatchValueMessage wvm;
390 struct WatchEntry *pos;
395 if (pos->last_value != se->value)
397 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
399 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
400 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
401 wvm.wid = htonl (pos->wid);
402 wvm.reserved = htonl (0);
403 wvm.value = GNUNET_htonll (se->value);
404 GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
406 pos->last_value = se->value;
413 * Handle SET-message.
416 * @param client identification of the client
417 * @param message the actual message
420 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
421 const struct GNUNET_MessageHeader *message)
427 const struct GNUNET_STATISTICS_SetMessage *msg;
428 struct StatsEntry *pos;
429 struct StatsEntry *prev;
436 make_client_entry (client);
437 msize = ntohs (message->size);
438 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
444 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
445 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
448 GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
452 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
455 flags = ntohl (msg->flags);
456 value = GNUNET_ntohll (msg->value);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
460 service, name, (unsigned int) flags, (unsigned long long) value);
466 if (matches (pos, service, name))
468 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
470 changed = (pos->value != value);
475 delta = (int64_t) value;
476 if ((delta < 0) && (pos->value < -delta))
478 changed = (pos->value != 0);
483 changed = (delta != 0);
484 GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
488 pos->msg->value = GNUNET_htonll (pos->value);
489 pos->msg->flags = msg->flags;
490 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
493 /* move to front for faster setting next time! */
494 prev->next = pos->next;
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
500 "Statistic `%s:%s' updated to value %llu.\n", service, name,
505 GNUNET_SERVER_receive_done (client, GNUNET_OK);
511 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
513 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
514 (0 < (int64_t) GNUNET_ntohll (msg->value)))
515 pos->value = GNUNET_ntohll (msg->value);
517 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
518 pos->msg = (void *) &pos[1];
519 memcpy (pos->msg, message, ntohs (message->size));
520 pos->service = (const char *) &pos->msg[1];
521 pos->name = &pos->service[strlen (pos->service) + 1];
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "New statistic on `%s:%s' with value %llu created.\n", service,
529 GNUNET_SERVER_receive_done (client, GNUNET_OK);
534 * Handle WATCH-message.
537 * @param client identification of the client
538 * @param message the actual message
541 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
542 const struct GNUNET_MessageHeader *message)
548 struct StatsEntry *pos;
549 struct ClientEntry *ce;
550 struct WatchEntry *we;
553 ce = make_client_entry (client);
554 msize = ntohs (message->size);
555 if (msize < sizeof (struct GNUNET_MessageHeader))
558 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
561 size = msize - sizeof (struct GNUNET_MessageHeader);
563 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
567 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
572 "Received request to watch statistic on `%s:%s'\n", service,
578 if (matches (pos, service, name))
585 GNUNET_malloc (sizeof (struct StatsEntry) +
586 sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
589 pos->msg = (void *) &pos[1];
590 pos->msg->header.size =
591 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
592 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
593 pos->service = (const char *) &pos->msg[1];
594 slen = strlen (service) + 1;
595 memcpy ((void *) pos->service, service, slen);
596 pos->name = &pos->service[slen];
597 memcpy ((void *) pos->name, name, strlen (name) + 1);
600 we = GNUNET_malloc (sizeof (struct WatchEntry));
602 GNUNET_SERVER_client_keep (client);
603 we->wid = ce->max_wid++;
604 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
607 GNUNET_SERVER_receive_done (client, GNUNET_OK);
612 * Task run during shutdown.
618 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
620 struct ClientEntry *ce;
621 struct WatchEntry *we;
622 struct StatsEntry *se;
625 GNUNET_SERVER_notification_context_destroy (nc);
627 while (NULL != (ce = client_head))
629 GNUNET_SERVER_client_drop (ce->client);
630 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
633 while (NULL != (se = start))
636 while (NULL != (we = se->we_head))
638 GNUNET_SERVER_client_drop (we->client);
639 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
648 * A client disconnected. Remove all of its data structure entries.
650 * @param cls closure, NULL
651 * @param client identification of the client
654 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
656 struct ClientEntry *ce;
657 struct WatchEntry *we;
658 struct WatchEntry *wen;
659 struct StatsEntry *se;
664 if (ce->client == client)
666 GNUNET_SERVER_client_drop (ce->client);
667 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
677 while (NULL != (we = wen))
680 if (we->client != client)
682 GNUNET_SERVER_client_drop (we->client);
683 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
692 * Process statistics requests.
695 * @param server the initialized server
696 * @param c configuration to use
699 run (void *cls, struct GNUNET_SERVER_Handle *server,
700 const struct GNUNET_CONFIGURATION_Handle *c)
702 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
703 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
704 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
705 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
709 GNUNET_SERVER_add_handlers (server, handlers);
710 nc = GNUNET_SERVER_notification_context_create (server, 16);
711 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
713 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
719 * The main function for the statistics service.
721 * @param argc number of arguments from the command line
722 * @param argv command line arguments
723 * @return 0 ok, 1 on error
726 main (int argc, char *const *argv)
729 GNUNET_SERVICE_run (argc, argv, "statistics",
730 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
733 /* end of gnunet-service-statistics.c */