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;
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 m = GNUNET_malloc (size);
228 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
229 m->header.size = htons (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);
243 GNUNET_SERVER_transmit_context_append_message (tc, &m->header);
249 * Does this entry match the request?
252 matches (const struct StatsEntry *e, const char *service, const char *name)
254 return ((0 == strlen (service)) ||
255 (0 == strcmp (service, e->service)))
256 && ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
261 * Handle GET-message.
264 * @param client identification of the client
265 * @param message the actual message
266 * @return GNUNET_OK to keep the connection open,
267 * GNUNET_SYSERR to close it (signal serious error)
270 handle_get (void *cls,
271 struct GNUNET_SERVER_Client *client,
272 const struct GNUNET_MessageHeader *message)
276 struct StatsEntry *pos;
277 struct GNUNET_SERVER_TransmitContext *tc;
280 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
281 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
282 size, 2, &service, &name))
285 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "Received request for statistics on `%s:%s'\n",
291 strlen (service) ? service : "*", strlen (name) ? name : "*");
293 tc = GNUNET_SERVER_transmit_context_create (client);
297 if (matches (pos, service, name))
301 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
302 GNUNET_MESSAGE_TYPE_STATISTICS_END);
303 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
308 * Handle SET-message.
311 * @param client identification of the client
312 * @param message the actual message
315 handle_set (void *cls,
316 struct GNUNET_SERVER_Client *client,
317 const struct GNUNET_MessageHeader *message)
323 const struct GNUNET_STATISTICS_SetMessage *msg;
324 struct StatsEntry *pos;
325 struct StatsEntry *prev;
330 msize = ntohs (message->size);
331 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
334 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
337 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
338 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
340 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
341 size, 2, &service, &name))
344 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 "Received request to update statistic on `%s:%s'\n",
352 flags = ntohl (msg->flags);
353 value = GNUNET_ntohll (msg->value);
358 if (matches (pos, service, name))
360 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
366 delta = (int64_t) value;
367 if ((delta < 0) && (pos->value < -delta))
373 GNUNET_break ((delta <= 0) ||
374 (pos->value + delta > pos->value));
378 pos->msg->value = GNUNET_htonll (pos->value);
379 pos->msg->flags = msg->flags;
381 (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
384 /* move to front for faster setting next time! */
385 prev->next = pos->next;
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Statistic `%s:%s' updated to value %llu.\n",
392 service, name, pos->value);
394 GNUNET_SERVER_receive_done (client, GNUNET_OK);
400 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
402 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
403 (0 < (int64_t) GNUNET_ntohll (msg->value)))
404 pos->value = GNUNET_ntohll (msg->value);
406 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
407 pos->msg = (void *) &pos[1];
408 memcpy (pos->msg, message, ntohs (message->size));
409 pos->service = (const char *) &pos->msg[1];
410 pos->name = &pos->service[strlen (pos->service) + 1];
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "New statistic on `%s:%s' with value %llu created.\n",
416 service, name, pos->value);
418 GNUNET_SERVER_receive_done (client, GNUNET_OK);
423 * List of handlers for the messages understood by this
426 static struct GNUNET_SERVER_MessageHandler handlers[] = {
427 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
428 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
434 * Task run during shutdown.
440 shutdown_task (void *cls,
441 const struct GNUNET_SCHEDULER_TaskContext *tc)
448 * Process statistics requests.
451 * @param sched scheduler to use
452 * @param server the initialized server
453 * @param c configuration to use
457 struct GNUNET_SCHEDULER_Handle *sched,
458 struct GNUNET_SERVER_Handle *server,
459 const struct GNUNET_CONFIGURATION_Handle *c)
462 GNUNET_SERVER_add_handlers (server, handlers);
464 GNUNET_SCHEDULER_add_delayed (sched,
465 GNUNET_TIME_UNIT_FOREVER_REL,
472 * The main function for the statistics service.
474 * @param argc number of arguments from the command line
475 * @param argv command line arguments
476 * @return 0 ok, 1 on error
479 main (int argc, char *const *argv)
482 GNUNET_SERVICE_run (argc,
485 GNUNET_SERVICE_OPTION_NONE,
486 &run, NULL)) ? 0 : 1;
489 /* end of gnunet-service-statistics.c */