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 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
249 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
259 if (GNUNET_OK != GNUNET_BIO_write_close (wh))
260 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
262 GNUNET_break (0 == UNLINK (fn));
264 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
265 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
267 GNUNET_free_non_null (fn);
272 * Transmit the given stats value.
275 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
277 struct GNUNET_STATISTICS_ReplyMessage *m;
281 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
282 strlen (e->name) + 1;
283 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
284 m = GNUNET_malloc (size);
285 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
286 m->header.size = htons (size);
287 m->uid = htonl (e->uid);
289 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
290 m->value = GNUNET_htonll (e->value);
291 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
292 GNUNET_assert (size ==
293 GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
294 e->service, e->name));
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
298 e->name, e->persistent, e->value);
300 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
307 * Does this entry match the request?
310 matches (const struct StatsEntry *e, const char *service, const char *name)
312 return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
313 ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
317 static struct ClientEntry *
318 make_client_entry (struct GNUNET_SERVER_Client *client)
320 struct ClientEntry *ce;
322 GNUNET_assert (client != NULL);
326 if (ce->client == client)
330 ce = GNUNET_malloc (sizeof (struct ClientEntry));
332 GNUNET_SERVER_client_keep (client);
333 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
334 GNUNET_SERVER_notification_context_add (nc, client);
340 * Handle GET-message.
343 * @param client identification of the client
344 * @param message the actual message
345 * @return GNUNET_OK to keep the connection open,
346 * GNUNET_SYSERR to close it (signal serious error)
349 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
350 const struct GNUNET_MessageHeader *message)
352 struct GNUNET_MessageHeader end;
355 struct StatsEntry *pos;
359 make_client_entry (client);
360 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
362 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
366 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Received request for statistics on `%s:%s'\n",
372 strlen (service) ? service : "*", strlen (name) ? name : "*");
377 if (matches (pos, service, name))
378 transmit (client, pos);
381 end.size = htons (sizeof (struct GNUNET_MessageHeader));
382 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
383 GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
384 GNUNET_SERVER_receive_done (client, GNUNET_OK);
389 notify_change (struct StatsEntry *se)
391 struct GNUNET_STATISTICS_WatchValueMessage wvm;
392 struct WatchEntry *pos;
397 if (pos->last_value != se->value)
399 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
401 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
402 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
403 wvm.wid = htonl (pos->wid);
404 wvm.reserved = htonl (0);
405 wvm.value = GNUNET_htonll (se->value);
406 GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
408 pos->last_value = se->value;
415 * Handle SET-message.
418 * @param client identification of the client
419 * @param message the actual message
422 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
423 const struct GNUNET_MessageHeader *message)
429 const struct GNUNET_STATISTICS_SetMessage *msg;
430 struct StatsEntry *pos;
431 struct StatsEntry *prev;
438 make_client_entry (client);
439 msize = ntohs (message->size);
440 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
446 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
447 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
450 GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
454 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
457 flags = ntohl (msg->flags);
458 value = GNUNET_ntohll (msg->value);
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
462 service, name, (unsigned int) flags, (unsigned long long) value);
468 if (matches (pos, service, name))
470 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
472 changed = (pos->value != value);
477 delta = (int64_t) value;
478 if ((delta < 0) && (pos->value < -delta))
480 changed = (pos->value != 0);
485 changed = (delta != 0);
486 GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
490 pos->msg->value = GNUNET_htonll (pos->value);
491 pos->msg->flags = msg->flags;
492 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
495 /* move to front for faster setting next time! */
496 prev->next = pos->next;
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Statistic `%s:%s' updated to value %llu.\n", service, name,
507 GNUNET_SERVER_receive_done (client, GNUNET_OK);
513 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
515 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
516 (0 < (int64_t) GNUNET_ntohll (msg->value)))
517 pos->value = GNUNET_ntohll (msg->value);
519 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
520 pos->msg = (void *) &pos[1];
521 memcpy (pos->msg, message, ntohs (message->size));
522 pos->service = (const char *) &pos->msg[1];
523 pos->name = &pos->service[strlen (pos->service) + 1];
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "New statistic on `%s:%s' with value %llu created.\n", service,
531 GNUNET_SERVER_receive_done (client, GNUNET_OK);
536 * Handle WATCH-message.
539 * @param client identification of the client
540 * @param message the actual message
543 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
544 const struct GNUNET_MessageHeader *message)
550 struct StatsEntry *pos;
551 struct ClientEntry *ce;
552 struct WatchEntry *we;
555 ce = make_client_entry (client);
556 msize = ntohs (message->size);
557 if (msize < sizeof (struct GNUNET_MessageHeader))
560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
563 size = msize - sizeof (struct GNUNET_MessageHeader);
565 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "Received request to watch statistic on `%s:%s'\n", service,
580 if (matches (pos, service, name))
587 GNUNET_malloc (sizeof (struct StatsEntry) +
588 sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
591 pos->msg = (void *) &pos[1];
592 pos->msg->header.size =
593 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
594 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
595 pos->service = (const char *) &pos->msg[1];
596 slen = strlen (service) + 1;
597 memcpy ((void *) pos->service, service, slen);
598 pos->name = &pos->service[slen];
599 memcpy ((void *) pos->name, name, strlen (name) + 1);
602 we = GNUNET_malloc (sizeof (struct WatchEntry));
604 GNUNET_SERVER_client_keep (client);
605 we->wid = ce->max_wid++;
606 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
609 GNUNET_SERVER_receive_done (client, GNUNET_OK);
614 * Task run during shutdown.
620 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
622 struct ClientEntry *ce;
623 struct WatchEntry *we;
624 struct StatsEntry *se;
627 GNUNET_SERVER_notification_context_destroy (nc);
629 while (NULL != (ce = client_head))
631 GNUNET_SERVER_client_drop (ce->client);
632 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
635 while (NULL != (se = start))
638 while (NULL != (we = se->we_head))
640 GNUNET_SERVER_client_drop (we->client);
641 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
650 * A client disconnected. Remove all of its data structure entries.
652 * @param cls closure, NULL
653 * @param client identification of the client
656 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
658 struct ClientEntry *ce;
659 struct WatchEntry *we;
660 struct WatchEntry *wen;
661 struct StatsEntry *se;
666 if (ce->client == client)
668 GNUNET_SERVER_client_drop (ce->client);
669 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
679 while (NULL != (we = wen))
682 if (we->client != client)
684 GNUNET_SERVER_client_drop (we->client);
685 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
694 * Process statistics requests.
697 * @param server the initialized server
698 * @param c configuration to use
701 run (void *cls, struct GNUNET_SERVER_Handle *server,
702 const struct GNUNET_CONFIGURATION_Handle *c)
704 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
705 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
706 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
707 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
711 GNUNET_SERVER_add_handlers (server, handlers);
712 nc = GNUNET_SERVER_notification_context_create (server, 16);
713 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
715 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
721 * The main function for the statistics service.
723 * @param argc number of arguments from the command line
724 * @param argv command line arguments
725 * @return 0 ok, 1 on error
728 main (int argc, char *const *argv)
731 GNUNET_SERVICE_run (argc, argv, "statistics",
732 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
735 /* end of gnunet-service-statistics.c */