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 inject_message (void *cls, void *client, const struct GNUNET_MessageHeader *msg)
161 struct GNUNET_SERVER_Handle *server = cls;
163 GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
168 * Load persistent values from disk. Disk format is
169 * exactly the same format that we also use for
170 * setting the values over the network.
172 * @param server handle to the server context
175 load (struct GNUNET_SERVER_Handle *server)
178 struct GNUNET_DISK_FileHandle *fh;
179 struct GNUNET_DISK_MapHandle *mh;
182 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
184 fn = GNUNET_DISK_get_home_filename (cfg, "statistics", "statistics.data",
188 if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
193 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
199 buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
202 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
203 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
207 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
208 _("Loading %llu bytes of statistics from `%s'\n"),
209 (unsigned long long) sb.st_size, fn);
210 mst = GNUNET_SERVER_mst_create (&inject_message, server);
211 GNUNET_break (GNUNET_OK ==
212 GNUNET_SERVER_mst_receive (mst, NULL, buf, sb.st_size,
213 GNUNET_YES, GNUNET_NO));
214 GNUNET_SERVER_mst_destroy (mst);
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, "statistics", "statistics.data",
236 fh = GNUNET_DISK_file_open (fn,
237 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
238 | GNUNET_DISK_OPEN_TRUNCATE,
239 GNUNET_DISK_PERM_USER_READ |
240 GNUNET_DISK_PERM_USER_WRITE);
242 while (NULL != (pos = start))
245 if ((pos->persistent) && (NULL != fh))
247 size = htons (pos->msg->header.size);
248 if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
250 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
251 GNUNET_DISK_file_close (fh);
261 GNUNET_DISK_file_close (fh);
263 GNUNET_break (0 == UNLINK (fn));
265 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
266 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
268 GNUNET_free_non_null (fn);
273 * Transmit the given stats value.
276 transmit (struct GNUNET_SERVER_Client *client, 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 ==
294 GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
295 e->service, e->name));
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
299 e->name, e->persistent, e->value);
301 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
308 * Does this entry match the request?
311 matches (const struct StatsEntry *e, const char *service, const char *name)
313 return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
314 ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
318 static struct ClientEntry *
319 make_client_entry (struct GNUNET_SERVER_Client *client)
321 struct ClientEntry *ce;
323 GNUNET_assert (client != NULL);
327 if (ce->client == client)
331 ce = GNUNET_malloc (sizeof (struct ClientEntry));
333 GNUNET_SERVER_client_keep (client);
334 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
335 GNUNET_SERVER_notification_context_add (nc, client);
341 * Handle GET-message.
344 * @param client identification of the client
345 * @param message the actual message
346 * @return GNUNET_OK to keep the connection open,
347 * GNUNET_SYSERR to close it (signal serious error)
350 handle_get (void *cls, struct GNUNET_SERVER_Client *client,
351 const struct GNUNET_MessageHeader *message)
353 struct GNUNET_MessageHeader end;
356 struct StatsEntry *pos;
360 make_client_entry (client);
361 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
363 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
367 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "Received request for statistics on `%s:%s'\n",
373 strlen (service) ? service : "*", strlen (name) ? name : "*");
378 if (matches (pos, service, name))
379 transmit (client, pos);
382 end.size = htons (sizeof (struct GNUNET_MessageHeader));
383 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
384 GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
385 GNUNET_SERVER_receive_done (client, GNUNET_OK);
390 notify_change (struct StatsEntry *se)
392 struct GNUNET_STATISTICS_WatchValueMessage wvm;
393 struct WatchEntry *pos;
398 if (pos->last_value != se->value)
400 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
402 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
403 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
404 wvm.wid = htonl (pos->wid);
405 wvm.reserved = htonl (0);
406 wvm.value = GNUNET_htonll (se->value);
407 GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
409 pos->last_value = se->value;
416 * Handle SET-message.
419 * @param client identification of the client
420 * @param message the actual message
423 handle_set (void *cls, struct GNUNET_SERVER_Client *client,
424 const struct GNUNET_MessageHeader *message)
430 const struct GNUNET_STATISTICS_SetMessage *msg;
431 struct StatsEntry *pos;
432 struct StatsEntry *prev;
439 make_client_entry (client);
440 msize = ntohs (message->size);
441 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
447 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
448 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
451 GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
458 flags = ntohl (msg->flags);
459 value = GNUNET_ntohll (msg->value);
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
463 service, name, (unsigned int) flags, (unsigned long long) value);
469 if (matches (pos, service, name))
471 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
473 changed = (pos->value != value);
478 delta = (int64_t) value;
479 if ((delta < 0) && (pos->value < -delta))
481 changed = (pos->value != 0);
486 changed = (delta != 0);
487 GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
491 pos->msg->value = GNUNET_htonll (pos->value);
492 pos->msg->flags = msg->flags;
493 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
496 /* move to front for faster setting next time! */
497 prev->next = pos->next;
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Statistic `%s:%s' updated to value %llu.\n", service, name,
508 GNUNET_SERVER_receive_done (client, GNUNET_OK);
514 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
516 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
517 (0 < (int64_t) GNUNET_ntohll (msg->value)))
518 pos->value = GNUNET_ntohll (msg->value);
520 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
521 pos->msg = (void *) &pos[1];
522 memcpy (pos->msg, message, ntohs (message->size));
523 pos->service = (const char *) &pos->msg[1];
524 pos->name = &pos->service[strlen (pos->service) + 1];
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529 "New statistic on `%s:%s' with value %llu created.\n", service,
532 GNUNET_SERVER_receive_done (client, GNUNET_OK);
537 * Handle WATCH-message.
540 * @param client identification of the client
541 * @param message the actual message
544 handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
545 const struct GNUNET_MessageHeader *message)
551 struct StatsEntry *pos;
552 struct ClientEntry *ce;
553 struct WatchEntry *we;
556 ce = make_client_entry (client);
557 msize = ntohs (message->size);
558 if (msize < sizeof (struct GNUNET_MessageHeader))
561 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
564 size = msize - sizeof (struct GNUNET_MessageHeader);
566 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
570 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "Received request to watch statistic on `%s:%s'\n", service,
581 if (matches (pos, service, name))
588 GNUNET_malloc (sizeof (struct StatsEntry) +
589 sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
592 pos->msg = (void *) &pos[1];
593 pos->msg->header.size =
594 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
595 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
596 pos->service = (const char *) &pos->msg[1];
597 slen = strlen (service) + 1;
598 memcpy ((void *) pos->service, service, slen);
599 pos->name = &pos->service[slen];
600 memcpy ((void *) pos->name, name, strlen (name) + 1);
603 we = GNUNET_malloc (sizeof (struct WatchEntry));
605 GNUNET_SERVER_client_keep (client);
606 we->wid = ce->max_wid++;
607 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
610 GNUNET_SERVER_receive_done (client, GNUNET_OK);
615 * Task run during shutdown.
621 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
623 struct ClientEntry *ce;
624 struct WatchEntry *we;
625 struct StatsEntry *se;
628 GNUNET_SERVER_notification_context_destroy (nc);
630 while (NULL != (ce = client_head))
632 GNUNET_SERVER_client_drop (ce->client);
633 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
636 while (NULL != (se = start))
639 while (NULL != (we = se->we_head))
641 GNUNET_SERVER_client_drop (we->client);
642 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
651 * A client disconnected. Remove all of its data structure entries.
653 * @param cls closure, NULL
654 * @param client identification of the client
657 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
659 struct ClientEntry *ce;
660 struct WatchEntry *we;
661 struct WatchEntry *wen;
662 struct StatsEntry *se;
667 if (ce->client == client)
669 GNUNET_SERVER_client_drop (ce->client);
670 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
680 while (NULL != (we = wen))
683 if (we->client != client)
685 GNUNET_SERVER_client_drop (we->client);
686 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
695 * Process statistics requests.
698 * @param server the initialized server
699 * @param c configuration to use
702 run (void *cls, struct GNUNET_SERVER_Handle *server,
703 const struct GNUNET_CONFIGURATION_Handle *c)
705 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
706 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
707 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
708 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
712 GNUNET_SERVER_add_handlers (server, handlers);
713 nc = GNUNET_SERVER_notification_context_create (server, 16);
714 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
716 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
722 * The main function for the statistics service.
724 * @param argc number of arguments from the command line
725 * @param argv command line arguments
726 * @return 0 ok, 1 on error
729 main (int argc, char *const *argv)
732 GNUNET_SERVICE_run (argc, argv, "statistics",
733 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
736 /* end of gnunet-service-statistics.c */