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?
86 * Linked list of our active statistics.
88 static struct StatsEntry *start;
91 * Counter used to generate unique values.
93 static uint32_t uidgen;
96 * Load persistent values from disk. Disk format is
97 * exactly the same format that we also use for
98 * setting the values over the network.
101 load (struct GNUNET_SERVER_Handle *server,
102 const struct GNUNET_CONFIGURATION_Handle *cfg)
105 struct GNUNET_DISK_FileHandle *fh;
106 struct GNUNET_DISK_MapHandle *mh;
110 const struct GNUNET_MessageHeader *msg;
112 fn = GNUNET_DISK_get_home_filename (cfg,
113 "statistics", "statistics.data", NULL);
116 if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
121 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
122 GNUNET_DISK_PERM_NONE);
128 buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
131 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
132 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
136 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
137 _("Loading %llu bytes of statistics from `%s'\n"),
138 (unsigned long long) sb.st_size, fn);
140 while (off + sizeof (struct GNUNET_MessageHeader) < sb.st_size)
142 msg = (const struct GNUNET_MessageHeader *) &buf[off];
143 if ((ntohs (msg->size) + off > sb.st_size) ||
144 (GNUNET_OK != GNUNET_SERVER_inject (server, NULL, msg)))
149 off += ntohs (msg->size);
151 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
152 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
158 * Write persistent statistics to disk.
161 * @param cfg configuration to use
165 const struct GNUNET_CONFIGURATION_Handle *cfg)
167 struct StatsEntry *pos;
169 struct GNUNET_DISK_FileHandle *fh;
171 unsigned long long total;
174 fn = GNUNET_DISK_get_home_filename (cfg,
175 "statistics", "statistics.data", NULL);
177 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
178 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
179 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
181 while (NULL != (pos = start))
184 if ((pos->persistent) && (NULL != fh))
186 size = htons (pos->msg->header.size);
187 if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
189 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
191 GNUNET_DISK_file_close (fh);
201 GNUNET_DISK_file_close (fh);
203 GNUNET_break (0 == UNLINK (fn));
205 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
206 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
208 GNUNET_free_non_null (fn);
213 * Transmit the given stats value.
216 transmit (struct GNUNET_SERVER_TransmitContext *tc,
217 const struct StatsEntry *e)
219 struct GNUNET_STATISTICS_ReplyMessage *m;
220 struct GNUNET_MessageHeader *h;
225 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
226 strlen (e->name) + 1;
227 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
228 msize = size - sizeof (struct GNUNET_MessageHeader);
229 m = GNUNET_malloc (size);
230 m->uid = htonl (e->uid);
232 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
233 m->value = GNUNET_htonll (e->value);
234 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
235 GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
237 2, e->service, e->name));
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "Transmitting value for `%s:%s': %llu\n",
241 e->service, e->name, e->value);
244 GNUNET_SERVER_transmit_context_append (tc,
247 GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
253 * Does this entry match the request?
256 matches (const struct StatsEntry *e, const char *service, const char *name)
258 return ((0 == strlen (service)) ||
259 (0 == strcmp (service, e->service)))
260 && ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
265 * Handle GET-message.
268 * @param client identification of the client
269 * @param message the actual message
270 * @return GNUNET_OK to keep the connection open,
271 * GNUNET_SYSERR to close it (signal serious error)
274 handle_get (void *cls,
275 struct GNUNET_SERVER_Client *client,
276 const struct GNUNET_MessageHeader *message)
280 struct StatsEntry *pos;
281 struct GNUNET_SERVER_TransmitContext *tc;
284 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
285 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
286 size, 2, &service, &name))
289 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "Received request for statistics on `%s:%s'\n",
295 strlen (service) ? service : "*", strlen (name) ? name : "*");
297 tc = GNUNET_SERVER_transmit_context_create (client);
301 if (matches (pos, service, name))
305 GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
306 GNUNET_MESSAGE_TYPE_STATISTICS_END);
307 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
312 * Handle SET-message.
315 * @param client identification of the client
316 * @param message the actual message
319 handle_set (void *cls,
320 struct GNUNET_SERVER_Client *client,
321 const struct GNUNET_MessageHeader *message)
327 const struct GNUNET_STATISTICS_SetMessage *msg;
328 struct StatsEntry *pos;
329 struct StatsEntry *prev;
334 msize = ntohs (message->size);
335 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
338 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
341 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
342 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
344 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
345 size, 2, &service, &name))
348 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Received request to update statistic on `%s:%s'\n",
356 flags = ntohl (msg->flags);
357 value = GNUNET_ntohll (msg->value);
362 if (matches (pos, service, name))
364 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
370 delta = (int64_t) value;
371 if ((delta < 0) && (pos->value < -delta))
377 GNUNET_break ((delta <= 0) ||
378 (pos->value + delta > pos->value));
382 pos->msg->value = GNUNET_htonll (pos->value);
383 pos->msg->flags = msg->flags;
385 (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
388 /* move to front for faster setting next time! */
389 prev->next = pos->next;
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 "Statistic `%s:%s' updated to value %llu.\n",
396 service, name, pos->value);
398 GNUNET_SERVER_receive_done (client, GNUNET_OK);
404 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
406 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
407 (0 < (int64_t) GNUNET_ntohll (msg->value)))
408 pos->value = GNUNET_ntohll (msg->value);
410 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
411 pos->msg = (void *) &pos[1];
412 memcpy (pos->msg, message, ntohs (message->size));
413 pos->service = (const char *) &pos->msg[1];
414 pos->name = &pos->service[strlen (pos->service) + 1];
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "New statistic on `%s:%s' with value %llu created.\n",
420 service, name, pos->value);
422 GNUNET_SERVER_receive_done (client, GNUNET_OK);
427 * List of handlers for the messages understood by this
430 static struct GNUNET_SERVER_MessageHandler handlers[] = {
431 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
432 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
438 * Process statistics requests.
441 * @param sched scheduler to use
442 * @param server the initialized server
443 * @param cfg configuration to use
447 struct GNUNET_SCHEDULER_Handle *sched,
448 struct GNUNET_SERVER_Handle *server,
449 const struct GNUNET_CONFIGURATION_Handle *cfg)
451 GNUNET_SERVER_add_handlers (server, handlers);
457 * The main function for the statistics service.
459 * @param argc number of arguments from the command line
460 * @param argv command line arguments
461 * @return 0 ok, 1 on error
464 main (int argc, char *const *argv)
467 GNUNET_SERVICE_run (argc,
469 "statistics", &run, NULL, &save, NULL)) ? 0 : 1;
472 /* end of gnunet-service-statistics.c */