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;
372 make_client_entry (client);
373 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
374 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
375 size, 2, &service, &name))
378 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "Received request for statistics on `%s:%s'\n",
384 strlen (service) ? service : "*", strlen (name) ? name : "*");
389 if (matches (pos, service, name))
390 transmit (client, pos);
393 end.size = htons (sizeof (struct GNUNET_MessageHeader));
394 end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
395 GNUNET_SERVER_notification_context_unicast (nc,
399 GNUNET_SERVER_receive_done (client,
405 notify_change (struct StatsEntry *se)
407 struct GNUNET_STATISTICS_WatchValueMessage wvm;
408 struct WatchEntry *pos;
413 if (pos->last_value != se->value)
415 wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
416 wvm.header.size = htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
417 wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
418 wvm.wid = htonl (pos->wid);
419 wvm.reserved = htonl (0);
420 wvm.value = GNUNET_htonll (se->value);
421 GNUNET_SERVER_notification_context_unicast (nc,
425 pos->last_value = se->value;
432 * Handle SET-message.
435 * @param client identification of the client
436 * @param message the actual message
439 handle_set (void *cls,
440 struct GNUNET_SERVER_Client *client,
441 const struct GNUNET_MessageHeader *message)
447 const struct GNUNET_STATISTICS_SetMessage *msg;
448 struct StatsEntry *pos;
449 struct StatsEntry *prev;
455 make_client_entry (client);
456 msize = ntohs (message->size);
457 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
463 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
464 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
466 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
467 size, 2, &service, &name))
470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
473 flags = ntohl (msg->flags);
474 value = GNUNET_ntohll (msg->value);
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
479 (unsigned int) flags,
480 (unsigned long long) value);
486 if (matches (pos, service, name))
488 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
490 changed = (pos->value != value);
495 delta = (int64_t) value;
496 if ((delta < 0) && (pos->value < -delta))
498 changed = (pos->value != 0);
503 changed = (delta != 0);
504 GNUNET_break ((delta <= 0) ||
505 (pos->value + delta > pos->value));
509 pos->msg->value = GNUNET_htonll (pos->value);
510 pos->msg->flags = msg->flags;
512 (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
515 /* move to front for faster setting next time! */
516 prev->next = pos->next;
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 "Statistic `%s:%s' updated to value %llu.\n",
523 service, name, pos->value);
527 GNUNET_SERVER_receive_done (client, GNUNET_OK);
533 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
535 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
536 (0 < (int64_t) GNUNET_ntohll (msg->value)))
537 pos->value = GNUNET_ntohll (msg->value);
539 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
540 pos->msg = (void *) &pos[1];
541 memcpy (pos->msg, message, ntohs (message->size));
542 pos->service = (const char *) &pos->msg[1];
543 pos->name = &pos->service[strlen (pos->service) + 1];
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 "New statistic on `%s:%s' with value %llu created.\n",
549 service, name, pos->value);
551 GNUNET_SERVER_receive_done (client, GNUNET_OK);
556 * Handle WATCH-message.
559 * @param client identification of the client
560 * @param message the actual message
563 handle_watch (void *cls,
564 struct GNUNET_SERVER_Client *client,
565 const struct GNUNET_MessageHeader *message)
571 struct StatsEntry *pos;
572 struct ClientEntry *ce;
573 struct WatchEntry *we;
576 ce = make_client_entry (client);
577 msize = ntohs (message->size);
578 if (msize < sizeof (struct GNUNET_MessageHeader))
581 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
584 size = msize - sizeof (struct GNUNET_MessageHeader);
585 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
586 size, 2, &service, &name))
589 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
594 "Received request to watch statistic on `%s:%s'\n",
600 if (matches (pos, service, name))
606 pos = GNUNET_malloc (sizeof (struct StatsEntry) +
607 sizeof (struct GNUNET_STATISTICS_SetMessage) +
611 pos->msg = (void *) &pos[1];
612 pos->msg->header.size = htons (sizeof (struct GNUNET_STATISTICS_SetMessage) +
614 pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
615 pos->service = (const char *) &pos->msg[1];
616 slen = strlen (service) + 1;
617 memcpy ((void*) pos->service, service, slen);
618 pos->name = &pos->service[slen];
619 memcpy ((void*) pos->name, name, strlen (name)+1);
622 we = GNUNET_malloc (sizeof (struct WatchEntry));
624 GNUNET_SERVER_client_keep (client);
625 we->wid = ce->max_wid++;
626 GNUNET_CONTAINER_DLL_insert (pos->we_head,
631 GNUNET_SERVER_receive_done (client,
637 * Task run during shutdown.
643 shutdown_task (void *cls,
644 const struct GNUNET_SCHEDULER_TaskContext *tc)
646 struct ClientEntry *ce;
647 struct WatchEntry *we;
648 struct StatsEntry *se;
651 GNUNET_SERVER_notification_context_destroy (nc);
653 while (NULL != (ce = client_head))
655 GNUNET_SERVER_client_drop (ce->client);
656 GNUNET_CONTAINER_DLL_remove (client_head,
661 while (NULL != (se = start))
664 while (NULL != (we = se->we_head))
666 GNUNET_SERVER_client_drop (we->client);
667 GNUNET_CONTAINER_DLL_remove (se->we_head,
678 * A client disconnected. Remove all of its data structure entries.
680 * @param cls closure, NULL
681 * @param client identification of the client
684 handle_client_disconnect (void *cls,
685 struct GNUNET_SERVER_Client
688 struct ClientEntry *ce;
689 struct WatchEntry *we;
690 struct WatchEntry *wen;
691 struct StatsEntry *se;
696 if (ce->client == client)
698 GNUNET_SERVER_client_drop (ce->client);
699 GNUNET_CONTAINER_DLL_remove (client_head,
711 while (NULL != (we = wen))
714 if (we->client != client)
716 GNUNET_SERVER_client_drop (we->client);
717 GNUNET_CONTAINER_DLL_remove (se->we_head,
728 * Process statistics requests.
731 * @param sched scheduler to use
732 * @param server the initialized server
733 * @param c configuration to use
737 struct GNUNET_SCHEDULER_Handle *sched,
738 struct GNUNET_SERVER_Handle *server,
739 const struct GNUNET_CONFIGURATION_Handle *c)
741 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
742 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
743 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
744 {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
748 GNUNET_SERVER_add_handlers (server, handlers);
749 nc = GNUNET_SERVER_notification_context_create (server, 16);
750 GNUNET_SERVER_disconnect_notify (server,
751 &handle_client_disconnect,
754 GNUNET_SCHEDULER_add_delayed (sched,
755 GNUNET_TIME_UNIT_FOREVER_REL,
762 * The main function for the statistics service.
764 * @param argc number of arguments from the command line
765 * @param argv command line arguments
766 * @return 0 ok, 1 on error
769 main (int argc, char *const *argv)
772 GNUNET_SERVICE_run (argc,
775 GNUNET_SERVICE_OPTION_NONE,
776 &run, NULL)) ? 0 : 1;
779 /* end of gnunet-service-statistics.c */