2 This file is part of GNUnet.
3 (C) 2009 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 2, 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_disk_lib.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_service_lib.h"
34 #include "gnunet_statistics_service.h"
35 #include "gnunet_strings_lib.h"
36 #include "gnunet_time_lib.h"
37 #include "statistics.h"
40 * Entry in the statistics list.
45 * This is a linked list.
47 struct StatsEntry *next;
50 * Name of the service, points into the
56 * Name for the value, points into
62 * Message that can be used to set this value,
63 * stored at the end of the memory used by
66 struct GNUNET_STATISTICS_SetMessage *msg;
79 * Is this value persistent?
88 static const struct GNUNET_CONFIGURATION_Handle *cfg;
91 * Linked list of our active statistics.
93 static struct StatsEntry *start;
96 * Counter used to generate unique values.
98 static uint32_t uidgen;
101 * Load persistent values from disk. Disk format is
102 * exactly the same format that we also use for
103 * setting the values over the network.
105 * @param server handle to the server context
108 load (struct GNUNET_SERVER_Handle *server)
111 struct GNUNET_DISK_FileHandle *fh;
112 struct GNUNET_DISK_MapHandle *mh;
116 const struct GNUNET_MessageHeader *msg;
118 fn = GNUNET_DISK_get_home_filename (cfg,
119 "statistics", "statistics.data", NULL);
122 if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
127 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
128 GNUNET_DISK_PERM_NONE);
134 buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
137 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
138 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
142 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
143 _("Loading %llu bytes of statistics from `%s'\n"),
144 (unsigned long long) sb.st_size, fn);
146 while (off + sizeof (struct GNUNET_MessageHeader) < sb.st_size)
148 msg = (const struct GNUNET_MessageHeader *) &buf[off];
149 if ((ntohs (msg->size) + off > sb.st_size) ||
150 (GNUNET_OK != GNUNET_SERVER_inject (server, NULL, msg)))
155 off += ntohs (msg->size);
157 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
158 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
163 * Write persistent statistics to disk.
168 struct StatsEntry *pos;
170 struct GNUNET_DISK_FileHandle *fh;
172 unsigned long long total;
175 fn = GNUNET_DISK_get_home_filename (cfg,
176 "statistics", "statistics.data", NULL);
178 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
179 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
180 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
182 while (NULL != (pos = start))
185 if ((pos->persistent) && (NULL != fh))
187 size = htons (pos->msg->header.size);
188 if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
190 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
192 GNUNET_DISK_file_close (fh);
202 GNUNET_DISK_file_close (fh);
204 GNUNET_break (0 == UNLINK (fn));
206 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
207 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
209 GNUNET_free_non_null (fn);
214 * Transmit the given stats value.
217 transmit (struct GNUNET_SERVER_TransmitContext *tc,
218 const struct StatsEntry *e)
220 struct GNUNET_STATISTICS_ReplyMessage *m;
221 struct GNUNET_MessageHeader *h;
226 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
227 strlen (e->name) + 1;
228 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
229 msize = size - sizeof (struct GNUNET_MessageHeader);
230 m = GNUNET_malloc (size);
231 m->uid = htonl (e->uid);
233 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
234 m->value = GNUNET_htonll (e->value);
235 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
236 GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
238 2, e->service, e->name));
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Transmitting value for `%s:%s': %llu\n",
242 e->service, e->name, e->value);
245 GNUNET_SERVER_transmit_context_append (tc,
248 GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
254 * Does this entry match the request?
257 matches (const struct StatsEntry *e, const char *service, const char *name)
259 return ((0 == strlen (service)) ||
260 (0 == strcmp (service, e->service)))
261 && ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
266 * Handle GET-message.
269 * @param client identification of the client
270 * @param message the actual message
271 * @return GNUNET_OK to keep the connection open,
272 * GNUNET_SYSERR to close it (signal serious error)
275 handle_get (void *cls,
276 struct GNUNET_SERVER_Client *client,
277 const struct GNUNET_MessageHeader *message)
281 struct StatsEntry *pos;
282 struct GNUNET_SERVER_TransmitContext *tc;
285 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
286 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
287 size, 2, &service, &name))
290 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "Received request for statistics on `%s:%s'\n",
296 strlen (service) ? service : "*", strlen (name) ? name : "*");
298 tc = GNUNET_SERVER_transmit_context_create (client);
302 if (matches (pos, service, name))
306 GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
307 GNUNET_MESSAGE_TYPE_STATISTICS_END);
308 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
313 * Handle SET-message.
316 * @param client identification of the client
317 * @param message the actual message
320 handle_set (void *cls,
321 struct GNUNET_SERVER_Client *client,
322 const struct GNUNET_MessageHeader *message)
328 const struct GNUNET_STATISTICS_SetMessage *msg;
329 struct StatsEntry *pos;
330 struct StatsEntry *prev;
335 msize = ntohs (message->size);
336 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
342 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
343 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
345 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
346 size, 2, &service, &name))
349 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354 "Received request to update statistic on `%s:%s'\n",
357 flags = ntohl (msg->flags);
358 value = GNUNET_ntohll (msg->value);
363 if (matches (pos, service, name))
365 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
371 delta = (int64_t) value;
372 if ((delta < 0) && (pos->value < -delta))
378 GNUNET_break ((delta <= 0) ||
379 (pos->value + delta > pos->value));
383 pos->msg->value = GNUNET_htonll (pos->value);
384 pos->msg->flags = msg->flags;
386 (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
389 /* move to front for faster setting next time! */
390 prev->next = pos->next;
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Statistic `%s:%s' updated to value %llu.\n",
397 service, name, pos->value);
399 GNUNET_SERVER_receive_done (client, GNUNET_OK);
405 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
407 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
408 (0 < (int64_t) GNUNET_ntohll (msg->value)))
409 pos->value = GNUNET_ntohll (msg->value);
411 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
412 pos->msg = (void *) &pos[1];
413 memcpy (pos->msg, message, ntohs (message->size));
414 pos->service = (const char *) &pos->msg[1];
415 pos->name = &pos->service[strlen (pos->service) + 1];
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "New statistic on `%s:%s' with value %llu created.\n",
421 service, name, pos->value);
423 GNUNET_SERVER_receive_done (client, GNUNET_OK);
428 * List of handlers for the messages understood by this
431 static struct GNUNET_SERVER_MessageHandler handlers[] = {
432 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
433 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
439 * Task run during shutdown.
445 shutdown_task (void *cls,
446 const struct GNUNET_SCHEDULER_TaskContext *tc)
453 * Process statistics requests.
456 * @param sched scheduler to use
457 * @param server the initialized server
458 * @param c configuration to use
462 struct GNUNET_SCHEDULER_Handle *sched,
463 struct GNUNET_SERVER_Handle *server,
464 const struct GNUNET_CONFIGURATION_Handle *c)
467 GNUNET_SERVER_add_handlers (server, handlers);
469 GNUNET_SCHEDULER_add_delayed (sched,
470 GNUNET_TIME_UNIT_FOREVER_REL,
477 * The main function for the statistics service.
479 * @param argc number of arguments from the command line
480 * @param argv command line arguments
481 * @return 0 ok, 1 on error
484 main (int argc, char *const *argv)
487 GNUNET_SERVICE_run (argc,
489 "statistics", &run, NULL)) ? 0 : 1;
492 /* end of gnunet-service-statistics.c */