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,
161 const struct GNUNET_MessageHeader *msg)
163 struct GNUNET_SERVER_Handle *server = cls;
165 GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
170 * Load persistent values from disk. Disk format is
171 * exactly the same format that we also use for
172 * setting the values over the network.
174 * @param server handle to the server context
177 load (struct GNUNET_SERVER_Handle *server)
180 struct GNUNET_DISK_FileHandle *fh;
181 struct GNUNET_DISK_MapHandle *mh;
184 struct GNUNET_SERVER_MessageStreamTokenizer *mst;
186 fn = GNUNET_DISK_get_home_filename (cfg,
187 "statistics", "statistics.data", NULL);
190 if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
195 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
196 GNUNET_DISK_PERM_NONE);
202 buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
205 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
206 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
210 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
211 _("Loading %llu bytes of statistics from `%s'\n"),
212 (unsigned long long) sb.st_size, fn);
213 mst = GNUNET_SERVER_mst_create (&inject_message,
215 GNUNET_break (GNUNET_OK ==
216 GNUNET_SERVER_mst_receive (mst,
222 GNUNET_SERVER_mst_destroy (mst);
223 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
224 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
229 * Write persistent statistics to disk.
234 struct StatsEntry *pos;
236 struct GNUNET_DISK_FileHandle *fh;
238 unsigned long long total;
241 fn = GNUNET_DISK_get_home_filename (cfg,
242 "statistics", "statistics.data", NULL);
244 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
245 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
246 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
248 while (NULL != (pos = start))
251 if ((pos->persistent) && (NULL != fh))
253 size = htons (pos->msg->header.size);
254 if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
256 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
258 GNUNET_DISK_file_close (fh);
268 GNUNET_DISK_file_close (fh);
270 GNUNET_break (0 == UNLINK (fn));
272 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
273 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
275 GNUNET_free_non_null (fn);
280 * Transmit the given stats value.
283 transmit (struct GNUNET_SERVER_Client *client,
284 const struct StatsEntry *e)
286 struct GNUNET_STATISTICS_ReplyMessage *m;
290 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
291 strlen (e->name) + 1;
292 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
293 m = GNUNET_malloc (size);
294 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
295 m->header.size = htons (size);
296 m->uid = htonl (e->uid);
298 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
299 m->value = GNUNET_htonll (e->value);
300 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
301 GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
303 2, e->service, e->name));
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "Transmitting value for `%s:%s' (%d): %llu\n",
308 e->persistent, e->value);
310 GNUNET_SERVER_notification_context_unicast (nc, client, &m->header, GNUNET_NO);
316 * Does this entry match the request?
319 matches (const struct StatsEntry *e, const char *service, const char *name)
321 return ((0 == strlen (service)) ||
322 (0 == strcmp (service, e->service)))
323 && ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
327 static struct ClientEntry *
328 make_client_entry (struct GNUNET_SERVER_Client *client)
330 struct ClientEntry *ce;
332 GNUNET_assert (client != NULL);
336 if (ce->client == client)
340 ce = GNUNET_malloc (sizeof (struct ClientEntry));
342 GNUNET_SERVER_client_keep (client);
343 GNUNET_CONTAINER_DLL_insert (client_head,
346 GNUNET_SERVER_notification_context_add (nc,
353 * Handle GET-message.
356 * @param client identification of the client
357 * @param message the actual message
358 * @return GNUNET_OK to keep the connection open,
359 * GNUNET_SYSERR to close it (signal serious error)
362 handle_get (void *cls,
363 struct GNUNET_SERVER_Client *client,
364 const struct GNUNET_MessageHeader *message)
366 struct GNUNET_MessageHeader end;
369 struct StatsEntry *pos;
373 make_client_entry (client);
374 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
375 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
376 size, 2, &service, &name))
379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Received request for statistics on `%s:%s'\n",
385 strlen (service) ? service : "*", strlen (name) ? name : "*");
390 if (matches (pos, service, name))
391 transmit (client, pos);
394 end.size = htons (sizeof (struct GNUNET_MessageHeader));
395 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
396 GNUNET_SERVER_notification_context_unicast (nc,
400 GNUNET_SERVER_receive_done (client,
406 notify_change (struct StatsEntry *se)
408 struct GNUNET_STATISTICS_WatchValueMessage wvm;
409 struct WatchEntry *pos;
414 if (pos->last_value != se->value)
416 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
417 wvm.header.size = htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
418 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
419 wvm.wid = htonl (pos->wid);
420 wvm.reserved = htonl (0);
421 wvm.value = GNUNET_htonll (se->value);
422 GNUNET_SERVER_notification_context_unicast (nc,
426 pos->last_value = se->value;
433 * Handle SET-message.
436 * @param client identification of the client
437 * @param message the actual message
440 handle_set (void *cls,
441 struct GNUNET_SERVER_Client *client,
442 const struct GNUNET_MessageHeader *message)
448 const struct GNUNET_STATISTICS_SetMessage *msg;
449 struct StatsEntry *pos;
450 struct StatsEntry *prev;
457 make_client_entry (client);
458 msize = ntohs (message->size);
459 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
462 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
465 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
466 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
468 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
469 size, 2, &service, &name))
472 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
475 flags = ntohl (msg->flags);
476 value = GNUNET_ntohll (msg->value);
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
481 (unsigned int) flags,
482 (unsigned long long) value);
488 if (matches (pos, service, name))
490 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
492 changed = (pos->value != value);
497 delta = (int64_t) value;
498 if ((delta < 0) && (pos->value < -delta))
500 changed = (pos->value != 0);
505 changed = (delta != 0);
506 GNUNET_break ((delta <= 0) ||
507 (pos->value + delta > pos->value));
511 pos->msg->value = GNUNET_htonll (pos->value);
512 pos->msg->flags = msg->flags;
514 (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
517 /* move to front for faster setting next time! */
518 prev->next = pos->next;
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "Statistic `%s:%s' updated to value %llu.\n",
525 service, name, pos->value);
529 GNUNET_SERVER_receive_done (client, GNUNET_OK);
535 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
537 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
538 (0 < (int64_t) GNUNET_ntohll (msg->value)))
539 pos->value = GNUNET_ntohll (msg->value);
541 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
542 pos->msg = (void *) &pos[1];
543 memcpy (pos->msg, message, ntohs (message->size));
544 pos->service = (const char *) &pos->msg[1];
545 pos->name = &pos->service[strlen (pos->service) + 1];
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 "New statistic on `%s:%s' with value %llu created.\n",
551 service, name, pos->value);
553 GNUNET_SERVER_receive_done (client, GNUNET_OK);
558 * Handle WATCH-message.
561 * @param client identification of the client
562 * @param message the actual message
565 handle_watch (void *cls,
566 struct GNUNET_SERVER_Client *client,
567 const struct GNUNET_MessageHeader *message)
573 struct StatsEntry *pos;
574 struct ClientEntry *ce;
575 struct WatchEntry *we;
578 ce = make_client_entry (client);
579 msize = ntohs (message->size);
580 if (msize < sizeof (struct GNUNET_MessageHeader))
583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
586 size = msize - sizeof (struct GNUNET_MessageHeader);
587 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
588 size, 2, &service, &name))
591 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
596 "Received request to watch statistic on `%s:%s'\n",
602 if (matches (pos, service, name))
608 pos = GNUNET_malloc (sizeof (struct StatsEntry) +
609 sizeof (struct GNUNET_STATISTICS_SetMessage) +
613 pos->msg = (void *) &pos[1];
614 pos->msg->header.size = htons (sizeof (struct GNUNET_STATISTICS_SetMessage) +
616 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
617 pos->service = (const char *) &pos->msg[1];
618 slen = strlen (service) + 1;
619 memcpy ((void*) pos->service, service, slen);
620 pos->name = &pos->service[slen];
621 memcpy ((void*) pos->name, name, strlen (name)+1);
624 we = GNUNET_malloc (sizeof (struct WatchEntry));
626 GNUNET_SERVER_client_keep (client);
627 we->wid = ce->max_wid++;
628 GNUNET_CONTAINER_DLL_insert (pos->we_head,
633 GNUNET_SERVER_receive_done (client,
639 * Task run during shutdown.
645 shutdown_task (void *cls,
646 const struct GNUNET_SCHEDULER_TaskContext *tc)
648 struct ClientEntry *ce;
649 struct WatchEntry *we;
650 struct StatsEntry *se;
653 GNUNET_SERVER_notification_context_destroy (nc);
655 while (NULL != (ce = client_head))
657 GNUNET_SERVER_client_drop (ce->client);
658 GNUNET_CONTAINER_DLL_remove (client_head,
663 while (NULL != (se = start))
666 while (NULL != (we = se->we_head))
668 GNUNET_SERVER_client_drop (we->client);
669 GNUNET_CONTAINER_DLL_remove (se->we_head,
680 * A client disconnected. Remove all of its data structure entries.
682 * @param cls closure, NULL
683 * @param client identification of the client
686 handle_client_disconnect (void *cls,
687 struct GNUNET_SERVER_Client
690 struct ClientEntry *ce;
691 struct WatchEntry *we;
692 struct WatchEntry *wen;
693 struct StatsEntry *se;
698 if (ce->client == client)
700 GNUNET_SERVER_client_drop (ce->client);
701 GNUNET_CONTAINER_DLL_remove (client_head,
713 while (NULL != (we = wen))
716 if (we->client != client)
718 GNUNET_SERVER_client_drop (we->client);
719 GNUNET_CONTAINER_DLL_remove (se->we_head,
730 * Process statistics requests.
733 * @param sched scheduler to use
734 * @param server the initialized server
735 * @param c configuration to use
739 struct GNUNET_SCHEDULER_Handle *sched,
740 struct GNUNET_SERVER_Handle *server,
741 const struct GNUNET_CONFIGURATION_Handle *c)
743 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
744 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
745 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
746 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
750 GNUNET_SERVER_add_handlers (server, handlers);
751 nc = GNUNET_SERVER_notification_context_create (server, 16);
752 GNUNET_SERVER_disconnect_notify (server,
753 &handle_client_disconnect,
756 GNUNET_SCHEDULER_add_delayed (sched,
757 GNUNET_TIME_UNIT_FOREVER_REL,
764 * The main function for the statistics service.
766 * @param argc number of arguments from the command line
767 * @param argv command line arguments
768 * @return 0 ok, 1 on error
771 main (int argc, char *const *argv)
774 GNUNET_SERVICE_run (argc,
777 GNUNET_SERVICE_OPTION_NONE,
778 &run, NULL)) ? 0 : 1;
781 /* end of gnunet-service-statistics.c */