2 This file is part of GNUnet.
3 (C) 2009, 2010 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 * - use BIO for IO operations
30 #include "gnunet_container_lib.h"
31 #include "gnunet_disk_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_service_lib.h"
35 #include "gnunet_statistics_service.h"
36 #include "gnunet_strings_lib.h"
37 #include "gnunet_time_lib.h"
38 #include "statistics.h"
46 struct WatchEntry *next;
48 struct WatchEntry *prev;
50 struct GNUNET_SERVER_Client *client;
65 struct ClientEntry *next;
67 struct ClientEntry *prev;
69 struct GNUNET_SERVER_Client *client;
76 * Entry in the statistics list.
81 * This is a linked list.
83 struct StatsEntry *next;
86 * Name of the service, points into the
92 * Name for the value, points into
98 * Message that can be used to set this value,
99 * stored at the end of the memory used by
102 struct GNUNET_STATISTICS_SetMessage *msg;
105 * Watch context for changes to this
106 * value, or NULL for none.
108 struct WatchEntry *we_head;
111 * Watch context for changes to this
112 * value, or NULL for none.
114 struct WatchEntry *we_tail;
127 * Is this value persistent?
136 static const struct GNUNET_CONFIGURATION_Handle *cfg;
139 * Linked list of our active statistics.
141 static struct StatsEntry *start;
143 static struct ClientEntry *client_head;
145 static struct ClientEntry *client_tail;
148 * Our notification context.
150 static struct GNUNET_SERVER_NotificationContext *nc;
153 * Counter used to generate unique values.
155 static uint32_t uidgen;
159 * Load persistent values from disk. Disk format is
160 * exactly the same format that we also use for
161 * setting the values over the network.
163 * @param server handle to the server context
166 load (struct GNUNET_SERVER_Handle *server)
169 struct GNUNET_DISK_FileHandle *fh;
170 struct GNUNET_DISK_MapHandle *mh;
174 const struct GNUNET_MessageHeader *msg;
176 fn = GNUNET_DISK_get_home_filename (cfg,
177 "statistics", "statistics.data", NULL);
180 if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
185 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
186 GNUNET_DISK_PERM_NONE);
192 buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
195 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
196 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
200 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
201 _("Loading %llu bytes of statistics from `%s'\n"),
202 (unsigned long long) sb.st_size, fn);
204 while (off + sizeof (struct GNUNET_MessageHeader) < sb.st_size)
206 msg = (const struct GNUNET_MessageHeader *) &buf[off];
207 if ((ntohs (msg->size) + off > sb.st_size) ||
208 (GNUNET_OK != GNUNET_SERVER_inject (server, NULL, msg)))
213 off += ntohs (msg->size);
215 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
216 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
221 * Write persistent statistics to disk.
226 struct StatsEntry *pos;
228 struct GNUNET_DISK_FileHandle *fh;
230 unsigned long long total;
233 fn = GNUNET_DISK_get_home_filename (cfg,
234 "statistics", "statistics.data", NULL);
236 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
237 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
238 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
240 while (NULL != (pos = start))
243 if ((pos->persistent) && (NULL != fh))
245 size = htons (pos->msg->header.size);
246 if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
248 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
250 GNUNET_DISK_file_close (fh);
260 GNUNET_DISK_file_close (fh);
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,
276 const struct StatsEntry *e)
278 struct GNUNET_STATISTICS_ReplyMessage *m;
282 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
283 strlen (e->name) + 1;
284 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
285 m = GNUNET_malloc (size);
286 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
287 m->header.size = htons (size);
288 m->uid = htonl (e->uid);
290 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
291 m->value = GNUNET_htonll (e->value);
292 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
293 GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
295 2, e->service, e->name));
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Transmitting value for `%s:%s' (%d): %llu\n",
300 e->persistent, e->value);
302 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header, GNUNET_NO);
308 * Does this entry match the request?
311 matches (const struct StatsEntry *e, const char *service, const char *name)
313 return ((0 == strlen (service)) ||
314 (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;
329 if (ce->client == client)
333 ce = GNUNET_malloc (sizeof (struct ClientEntry));
335 GNUNET_SERVER_client_keep (client);
336 GNUNET_CONTAINER_DLL_insert (client_head,
339 GNUNET_SERVER_notification_context_add (nc,
346 * Handle GET-message.
349 * @param client identification of the client
350 * @param message the actual message
351 * @return GNUNET_OK to keep the connection open,
352 * GNUNET_SYSERR to close it (signal serious error)
355 handle_get (void *cls,
356 struct GNUNET_SERVER_Client *client,
357 const struct GNUNET_MessageHeader *message)
359 struct GNUNET_MessageHeader end;
362 struct StatsEntry *pos;
365 make_client_entry (client);
366 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
367 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
368 size, 2, &service, &name))
371 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Received request for statistics on `%s:%s'\n",
377 strlen (service) ? service : "*", strlen (name) ? name : "*");
382 if (matches (pos, service, name))
383 transmit (client, pos);
386 end.size = htons (sizeof (struct GNUNET_MessageHeader));
387 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
388 GNUNET_SERVER_notification_context_unicast (nc,
392 GNUNET_SERVER_receive_done (client,
398 notify_change (struct StatsEntry *se)
400 struct GNUNET_STATISTICS_WatchValueMessage wvm;
401 struct WatchEntry *pos;
406 if (pos->last_value != se->value)
408 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
409 wvm.header.size = htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
410 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
411 wvm.wid = htonl (pos->wid);
412 wvm.reserved = htonl (0);
413 wvm.value = GNUNET_htonll (se->value);
414 GNUNET_SERVER_notification_context_unicast (nc,
418 pos->last_value = se->value;
425 * Handle SET-message.
428 * @param client identification of the client
429 * @param message the actual message
432 handle_set (void *cls,
433 struct GNUNET_SERVER_Client *client,
434 const struct GNUNET_MessageHeader *message)
440 const struct GNUNET_STATISTICS_SetMessage *msg;
441 struct StatsEntry *pos;
442 struct StatsEntry *prev;
448 make_client_entry (client);
449 msize = ntohs (message->size);
450 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
453 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
456 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
457 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
459 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
460 size, 2, &service, &name))
463 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
466 flags = ntohl (msg->flags);
467 value = GNUNET_ntohll (msg->value);
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
472 (unsigned int) flags,
473 (unsigned long long) value);
479 if (matches (pos, service, name))
481 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
483 changed = (pos->value != value);
488 delta = (int64_t) value;
489 if ((delta < 0) && (pos->value < -delta))
491 changed = (pos->value != 0);
496 changed = (delta != 0);
497 GNUNET_break ((delta <= 0) ||
498 (pos->value + delta > pos->value));
502 pos->msg->value = GNUNET_htonll (pos->value);
503 pos->msg->flags = msg->flags;
505 (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
508 /* move to front for faster setting next time! */
509 prev->next = pos->next;
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Statistic `%s:%s' updated to value %llu.\n",
516 service, name, pos->value);
520 GNUNET_SERVER_receive_done (client, GNUNET_OK);
526 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
528 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
529 (0 < (int64_t) GNUNET_ntohll (msg->value)))
530 pos->value = GNUNET_ntohll (msg->value);
532 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
533 pos->msg = (void *) &pos[1];
534 memcpy (pos->msg, message, ntohs (message->size));
535 pos->service = (const char *) &pos->msg[1];
536 pos->name = &pos->service[strlen (pos->service) + 1];
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "New statistic on `%s:%s' with value %llu created.\n",
542 service, name, pos->value);
544 GNUNET_SERVER_receive_done (client, GNUNET_OK);
549 * Handle WATCH-message.
552 * @param client identification of the client
553 * @param message the actual message
556 handle_watch (void *cls,
557 struct GNUNET_SERVER_Client *client,
558 const struct GNUNET_MessageHeader *message)
564 struct StatsEntry *pos;
565 struct ClientEntry *ce;
566 struct WatchEntry *we;
568 ce = make_client_entry (client);
569 msize = ntohs (message->size);
570 if (msize < sizeof (struct GNUNET_MessageHeader))
573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
576 size = msize - sizeof (struct GNUNET_MessageHeader);
577 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
578 size, 2, &service, &name))
581 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "Received request to watch statistic on `%s:%s'\n",
592 if (matches (pos, service, name))
598 pos = GNUNET_malloc (sizeof (struct StatsEntry) +
599 sizeof (struct GNUNET_STATISTICS_SetMessage) +
603 pos->msg = (void *) &pos[1];
604 pos->msg->header.size = htons (sizeof (struct GNUNET_STATISTICS_SetMessage) +
606 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
607 memcpy (pos->msg, message, ntohs (message->size));
608 pos->service = (const char *) &pos->msg[1];
609 memcpy (&pos->msg[1], service, strlen (service)+1);
610 pos->name = &pos->service[strlen (pos->service) + 1];
611 memcpy ((void*) pos->name, name, strlen (name)+1);
614 we = GNUNET_malloc (sizeof (struct WatchEntry));
616 GNUNET_SERVER_client_keep (client);
617 we->wid = ce->max_wid++;
618 GNUNET_CONTAINER_DLL_insert (pos->we_head,
623 GNUNET_SERVER_receive_done (client,
629 * Task run during shutdown.
635 shutdown_task (void *cls,
636 const struct GNUNET_SCHEDULER_TaskContext *tc)
638 struct ClientEntry *ce;
639 struct WatchEntry *we;
640 struct StatsEntry *se;
643 GNUNET_SERVER_notification_context_destroy (nc);
645 while (NULL != (ce = client_head))
647 GNUNET_SERVER_client_drop (ce->client);
648 GNUNET_CONTAINER_DLL_remove (client_head,
653 while (NULL != (se = start))
656 while (NULL != (we = se->we_head))
658 GNUNET_SERVER_client_drop (we->client);
659 GNUNET_CONTAINER_DLL_remove (se->we_head,
670 * A client disconnected. Remove all of its data structure entries.
672 * @param cls closure, NULL
673 * @param client identification of the client
676 handle_client_disconnect (void *cls,
677 struct GNUNET_SERVER_Client
680 struct ClientEntry *ce;
681 struct WatchEntry *we;
682 struct WatchEntry *wen;
683 struct StatsEntry *se;
688 if (ce->client == client)
690 GNUNET_SERVER_client_drop (ce->client);
691 GNUNET_CONTAINER_DLL_remove (client_head,
703 while (NULL != (we = wen))
706 if (we->client != client)
708 GNUNET_SERVER_client_drop (we->client);
709 GNUNET_CONTAINER_DLL_remove (se->we_head,
720 * Process statistics requests.
723 * @param sched scheduler to use
724 * @param server the initialized server
725 * @param c configuration to use
729 struct GNUNET_SCHEDULER_Handle *sched,
730 struct GNUNET_SERVER_Handle *server,
731 const struct GNUNET_CONFIGURATION_Handle *c)
733 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
734 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
735 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
736 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
740 GNUNET_SERVER_add_handlers (server, handlers);
741 nc = GNUNET_SERVER_notification_context_create (server, 16);
742 GNUNET_SERVER_disconnect_notify (server,
743 &handle_client_disconnect,
746 GNUNET_SCHEDULER_add_delayed (sched,
747 GNUNET_TIME_UNIT_FOREVER_REL,
754 * The main function for the statistics service.
756 * @param argc number of arguments from the command line
757 * @param argv command line arguments
758 * @return 0 ok, 1 on error
761 main (int argc, char *const *argv)
764 GNUNET_SERVICE_run (argc,
767 GNUNET_SERVICE_OPTION_NONE,
768 &run, NULL)) ? 0 : 1;
771 /* end of gnunet-service-statistics.c */