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,
185 "statistics", "statistics.data", NULL);
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,
215 sb.st_size, GNUNET_YES, GNUNET_NO));
216 GNUNET_SERVER_mst_destroy (mst);
217 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
218 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
223 * Write persistent statistics to disk.
228 struct StatsEntry *pos;
230 struct GNUNET_DISK_FileHandle *fh;
232 unsigned long long total;
235 fn = GNUNET_DISK_get_home_filename (cfg,
236 "statistics", "statistics.data", NULL);
238 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
239 | GNUNET_DISK_OPEN_CREATE |
240 GNUNET_DISK_OPEN_TRUNCATE,
241 GNUNET_DISK_PERM_USER_READ |
242 GNUNET_DISK_PERM_USER_WRITE);
244 while (NULL != (pos = start))
247 if ((pos->persistent) && (NULL != fh))
249 size = htons (pos->msg->header.size);
250 if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
252 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
253 GNUNET_DISK_file_close (fh);
263 GNUNET_DISK_file_close (fh);
265 GNUNET_break (0 == UNLINK (fn));
267 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
268 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
270 GNUNET_free_non_null (fn);
275 * Transmit the given stats value.
278 transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
280 struct GNUNET_STATISTICS_ReplyMessage *m;
284 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
285 strlen (e->name) + 1;
286 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
287 m = GNUNET_malloc (size);
288 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
289 m->header.size = htons (size);
290 m->uid = htonl (e->uid);
292 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
293 m->value = GNUNET_htonll (e->value);
294 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
295 GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
297 2, e->service, e->name));
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Transmitting value for `%s:%s' (%d): %llu\n",
301 e->service, e->name, e->persistent, e->value);
303 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
310 * Does this entry match the request?
313 matches (const struct StatsEntry *e, const char *service, const char *name)
315 return ((0 == strlen (service)) ||
316 (0 == strcmp (service, e->service)))
317 && ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
321 static struct ClientEntry *
322 make_client_entry (struct GNUNET_SERVER_Client *client)
324 struct ClientEntry *ce;
326 GNUNET_assert (client != NULL);
330 if (ce->client == client)
334 ce = GNUNET_malloc (sizeof (struct ClientEntry));
336 GNUNET_SERVER_client_keep (client);
337 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
338 GNUNET_SERVER_notification_context_add (nc, client);
344 * Handle GET-message.
347 * @param client identification of the client
348 * @param message the actual message
349 * @return GNUNET_OK to keep the connection open,
350 * GNUNET_SYSERR to close it (signal serious error)
353 handle_get (void *cls,
354 struct GNUNET_SERVER_Client *client,
355 const struct GNUNET_MessageHeader *message)
357 struct GNUNET_MessageHeader end;
360 struct StatsEntry *pos;
364 make_client_entry (client);
365 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
366 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
367 size, 2, &service, &name))
370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375 "Received request for statistics on `%s:%s'\n",
376 strlen (service) ? service : "*", strlen (name) ? name : "*");
381 if (matches (pos, service, name))
382 transmit (client, pos);
385 end.size = htons (sizeof (struct GNUNET_MessageHeader));
386 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
387 GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
388 GNUNET_SERVER_receive_done (client, GNUNET_OK);
393 notify_change (struct StatsEntry *se)
395 struct GNUNET_STATISTICS_WatchValueMessage wvm;
396 struct WatchEntry *pos;
401 if (pos->last_value != se->value)
403 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
405 htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
406 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
407 wvm.wid = htonl (pos->wid);
408 wvm.reserved = htonl (0);
409 wvm.value = GNUNET_htonll (se->value);
410 GNUNET_SERVER_notification_context_unicast (nc,
412 &wvm.header, GNUNET_NO);
413 pos->last_value = se->value;
420 * Handle SET-message.
423 * @param client identification of the client
424 * @param message the actual message
427 handle_set (void *cls,
428 struct GNUNET_SERVER_Client *client,
429 const struct GNUNET_MessageHeader *message)
435 const struct GNUNET_STATISTICS_SetMessage *msg;
436 struct StatsEntry *pos;
437 struct StatsEntry *prev;
444 make_client_entry (client);
445 msize = ntohs (message->size);
446 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
449 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
452 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
453 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
455 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
456 size, 2, &service, &name))
459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
462 flags = ntohl (msg->flags);
463 value = GNUNET_ntohll (msg->value);
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
467 service, name, (unsigned int) flags, (unsigned long long) value);
473 if (matches (pos, service, name))
475 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
477 changed = (pos->value != value);
482 delta = (int64_t) value;
483 if ((delta < 0) && (pos->value < -delta))
485 changed = (pos->value != 0);
490 changed = (delta != 0);
491 GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
495 pos->msg->value = GNUNET_htonll (pos->value);
496 pos->msg->flags = msg->flags;
497 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
500 /* move to front for faster setting next time! */
501 prev->next = pos->next;
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "Statistic `%s:%s' updated to value %llu.\n",
508 service, name, pos->value);
512 GNUNET_SERVER_receive_done (client, GNUNET_OK);
518 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
520 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
521 (0 < (int64_t) GNUNET_ntohll (msg->value)))
522 pos->value = GNUNET_ntohll (msg->value);
524 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
525 pos->msg = (void *) &pos[1];
526 memcpy (pos->msg, message, ntohs (message->size));
527 pos->service = (const char *) &pos->msg[1];
528 pos->name = &pos->service[strlen (pos->service) + 1];
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "New statistic on `%s:%s' with value %llu created.\n",
534 service, name, pos->value);
536 GNUNET_SERVER_receive_done (client, GNUNET_OK);
541 * Handle WATCH-message.
544 * @param client identification of the client
545 * @param message the actual message
548 handle_watch (void *cls,
549 struct GNUNET_SERVER_Client *client,
550 const struct GNUNET_MessageHeader *message)
556 struct StatsEntry *pos;
557 struct ClientEntry *ce;
558 struct WatchEntry *we;
561 ce = make_client_entry (client);
562 msize = ntohs (message->size);
563 if (msize < sizeof (struct GNUNET_MessageHeader))
566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
569 size = msize - sizeof (struct GNUNET_MessageHeader);
570 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
571 size, 2, &service, &name))
574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579 "Received request to watch statistic on `%s:%s'\n",
585 if (matches (pos, service, name))
591 pos = GNUNET_malloc (sizeof (struct StatsEntry) +
592 sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
595 pos->msg = (void *) &pos[1];
596 pos->msg->header.size =
597 htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
598 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
599 pos->service = (const char *) &pos->msg[1];
600 slen = strlen (service) + 1;
601 memcpy ((void *) pos->service, service, slen);
602 pos->name = &pos->service[slen];
603 memcpy ((void *) pos->name, name, strlen (name) + 1);
606 we = GNUNET_malloc (sizeof (struct WatchEntry));
608 GNUNET_SERVER_client_keep (client);
609 we->wid = ce->max_wid++;
610 GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
613 GNUNET_SERVER_receive_done (client, GNUNET_OK);
618 * Task run during shutdown.
624 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
626 struct ClientEntry *ce;
627 struct WatchEntry *we;
628 struct StatsEntry *se;
631 GNUNET_SERVER_notification_context_destroy (nc);
633 while (NULL != (ce = client_head))
635 GNUNET_SERVER_client_drop (ce->client);
636 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
639 while (NULL != (se = start))
642 while (NULL != (we = se->we_head))
644 GNUNET_SERVER_client_drop (we->client);
645 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
654 * A client disconnected. Remove all of its data structure entries.
656 * @param cls closure, NULL
657 * @param client identification of the client
660 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
662 struct ClientEntry *ce;
663 struct WatchEntry *we;
664 struct WatchEntry *wen;
665 struct StatsEntry *se;
670 if (ce->client == client)
672 GNUNET_SERVER_client_drop (ce->client);
673 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
683 while (NULL != (we = wen))
686 if (we->client != client)
688 GNUNET_SERVER_client_drop (we->client);
689 GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
698 * Process statistics requests.
701 * @param server the initialized server
702 * @param c configuration to use
706 struct GNUNET_SERVER_Handle *server,
707 const struct GNUNET_CONFIGURATION_Handle *c)
709 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
710 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
711 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
712 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
716 GNUNET_SERVER_add_handlers (server, handlers);
717 nc = GNUNET_SERVER_notification_context_create (server, 16);
718 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
720 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
721 &shutdown_task, NULL);
726 * The main function for the statistics service.
728 * @param argc number of arguments from the command line
729 * @param argv command line arguments
730 * @return 0 ok, 1 on error
733 main (int argc, char *const *argv)
736 GNUNET_SERVICE_run (argc,
739 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
742 /* end of gnunet-service-statistics.c */