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);
127 buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
130 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
131 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
135 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
136 _("Loading %llu bytes of statistics from `%s'\n"),
137 (unsigned long long) sb.st_size, fn);
139 while (off + sizeof (struct GNUNET_MessageHeader) < sb.st_size)
141 msg = (const struct GNUNET_MessageHeader *) &buf[off];
142 if ((ntohs (msg->size) + off > sb.st_size) ||
143 (GNUNET_OK != GNUNET_SERVER_inject (server, NULL, msg)))
148 off += ntohs (msg->size);
150 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
151 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
157 * Write persistent statistics to disk.
160 * @param cfg configuration to use
164 const struct GNUNET_CONFIGURATION_Handle *cfg)
166 struct StatsEntry *pos;
168 struct GNUNET_DISK_FileHandle *fh;
170 unsigned long long total;
173 fn = GNUNET_DISK_get_home_filename (cfg,
174 "statistics", "statistics.data", NULL);
176 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
177 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
178 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
180 while (NULL != (pos = start))
183 if ((pos->persistent) && (NULL != fh))
185 size = htons (pos->msg->header.size);
186 if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
188 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
190 GNUNET_DISK_file_close (fh);
200 GNUNET_DISK_file_close (fh);
202 GNUNET_break (0 == UNLINK (fn));
204 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
205 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
207 GNUNET_free_non_null (fn);
212 * Transmit the given stats value.
215 transmit (struct GNUNET_SERVER_TransmitContext *tc,
216 const struct StatsEntry *e)
218 struct GNUNET_STATISTICS_ReplyMessage *m;
219 struct GNUNET_MessageHeader *h;
224 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
225 strlen (e->name) + 1;
226 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
227 msize = size - sizeof (struct GNUNET_MessageHeader);
228 m = GNUNET_malloc (size);
229 m->uid = htonl (e->uid);
231 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
232 m->value = GNUNET_htonll (e->value);
233 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
234 GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
236 2, e->service, e->name));
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "Transmitting value for `%s:%s': %llu\n",
240 e->service, e->name, e->value);
243 GNUNET_SERVER_transmit_context_append (tc,
246 GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
252 * Does this entry match the request?
255 matches (const struct StatsEntry *e, const char *service, const char *name)
257 return ((0 == strlen (service)) ||
258 (0 == strcmp (service, e->service)))
259 && ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
264 * Handle GET-message.
267 * @param client identification of the client
268 * @param message the actual message
269 * @return GNUNET_OK to keep the connection open,
270 * GNUNET_SYSERR to close it (signal serious error)
273 handle_get (void *cls,
274 struct GNUNET_SERVER_Client *client,
275 const struct GNUNET_MessageHeader *message)
279 struct StatsEntry *pos;
280 struct GNUNET_SERVER_TransmitContext *tc;
283 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
284 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
285 size, 2, &service, &name))
288 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Received request for statistics on `%s:%s'\n",
294 strlen (service) ? service : "*", strlen (name) ? name : "*");
296 tc = GNUNET_SERVER_transmit_context_create (client);
300 if (matches (pos, service, name))
304 GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
305 GNUNET_MESSAGE_TYPE_STATISTICS_END);
306 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
311 * Handle SET-message.
314 * @param client identification of the client
315 * @param message the actual message
318 handle_set (void *cls,
319 struct GNUNET_SERVER_Client *client,
320 const struct GNUNET_MessageHeader *message)
326 const struct GNUNET_STATISTICS_SetMessage *msg;
327 struct StatsEntry *pos;
328 struct StatsEntry *prev;
333 msize = ntohs (message->size);
334 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
340 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
341 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
343 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
344 size, 2, &service, &name))
347 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Received request to update statistic on `%s:%s'\n",
355 flags = ntohl (msg->flags);
356 value = GNUNET_ntohll (msg->value);
361 if (matches (pos, service, name))
363 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
369 delta = (int64_t) value;
370 if ((delta < 0) && (pos->value < -delta))
376 GNUNET_break ((delta <= 0) ||
377 (pos->value + delta > pos->value));
381 pos->msg->value = GNUNET_htonll (pos->value);
382 pos->msg->flags = msg->flags;
384 (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
387 /* move to front for faster setting next time! */
388 prev->next = pos->next;
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Statistic `%s:%s' updated to value %llu.\n",
395 service, name, pos->value);
397 GNUNET_SERVER_receive_done (client, GNUNET_OK);
403 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
405 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
406 (0 < (int64_t) GNUNET_ntohll (msg->value)))
407 pos->value = GNUNET_ntohll (msg->value);
409 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
410 pos->msg = (void *) &pos[1];
411 memcpy (pos->msg, message, ntohs (message->size));
412 pos->service = (const char *) &pos->msg[1];
413 pos->name = &pos->service[strlen (pos->service) + 1];
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "New statistic on `%s:%s' with value %llu created.\n",
419 service, name, pos->value);
421 GNUNET_SERVER_receive_done (client, GNUNET_OK);
426 * List of handlers for the messages understood by this
429 static struct GNUNET_SERVER_MessageHandler handlers[] = {
430 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
431 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
437 * Process statistics requests.
440 * @param sched scheduler to use
441 * @param server the initialized server
442 * @param cfg configuration to use
446 struct GNUNET_SCHEDULER_Handle *sched,
447 struct GNUNET_SERVER_Handle *server,
448 const struct GNUNET_CONFIGURATION_Handle *cfg)
450 GNUNET_SERVER_add_handlers (server, handlers);
456 * The main function for the statistics service.
458 * @param argc number of arguments from the command line
459 * @param argv command line arguments
460 * @return 0 ok, 1 on error
463 main (int argc, char *const *argv)
466 GNUNET_SERVICE_run (argc,
468 "statistics", &run, NULL, &save, NULL)) ? 0 : 1;
471 /* end of gnunet-service-statistics.c */