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 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_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 * 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.
100 * @param server handle to the server context
103 load (struct GNUNET_SERVER_Handle *server)
106 struct GNUNET_DISK_FileHandle *fh;
107 struct GNUNET_DISK_MapHandle *mh;
111 const struct GNUNET_MessageHeader *msg;
113 fn = GNUNET_DISK_get_home_filename (cfg,
114 "statistics", "statistics.data", NULL);
117 if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
122 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
123 GNUNET_DISK_PERM_NONE);
129 buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
132 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
133 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
137 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
138 _("Loading %llu bytes of statistics from `%s'\n"),
139 (unsigned long long) sb.st_size, fn);
141 while (off + sizeof (struct GNUNET_MessageHeader) < sb.st_size)
143 msg = (const struct GNUNET_MessageHeader *) &buf[off];
144 if ((ntohs (msg->size) + off > sb.st_size) ||
145 (GNUNET_OK != GNUNET_SERVER_inject (server, NULL, msg)))
150 off += ntohs (msg->size);
152 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
153 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
158 * Write persistent statistics to disk.
163 struct StatsEntry *pos;
165 struct GNUNET_DISK_FileHandle *fh;
167 unsigned long long total;
170 fn = GNUNET_DISK_get_home_filename (cfg,
171 "statistics", "statistics.data", NULL);
173 fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
174 | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
175 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
177 while (NULL != (pos = start))
180 if ((pos->persistent) && (NULL != fh))
182 size = htons (pos->msg->header.size);
183 if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
185 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
187 GNUNET_DISK_file_close (fh);
197 GNUNET_DISK_file_close (fh);
199 GNUNET_break (0 == UNLINK (fn));
201 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
202 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
204 GNUNET_free_non_null (fn);
209 * Transmit the given stats value.
212 transmit (struct GNUNET_SERVER_TransmitContext *tc,
213 const struct StatsEntry *e)
215 struct GNUNET_STATISTICS_ReplyMessage *m;
219 sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
220 strlen (e->name) + 1;
221 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
222 m = GNUNET_malloc (size);
223 m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
224 m->header.size = htons (size);
225 m->uid = htonl (e->uid);
227 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
228 m->value = GNUNET_htonll (e->value);
229 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
230 GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
232 2, e->service, e->name));
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "Transmitting value for `%s:%s': %llu\n",
236 e->service, e->name, e->value);
238 GNUNET_SERVER_transmit_context_append_message (tc, &m->header);
244 * Does this entry match the request?
247 matches (const struct StatsEntry *e, const char *service, const char *name)
249 return ((0 == strlen (service)) ||
250 (0 == strcmp (service, e->service)))
251 && ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
256 * Handle GET-message.
259 * @param client identification of the client
260 * @param message the actual message
261 * @return GNUNET_OK to keep the connection open,
262 * GNUNET_SYSERR to close it (signal serious error)
265 handle_get (void *cls,
266 struct GNUNET_SERVER_Client *client,
267 const struct GNUNET_MessageHeader *message)
271 struct StatsEntry *pos;
272 struct GNUNET_SERVER_TransmitContext *tc;
275 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
276 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
277 size, 2, &service, &name))
280 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Received request for statistics on `%s:%s'\n",
286 strlen (service) ? service : "*", strlen (name) ? name : "*");
288 tc = GNUNET_SERVER_transmit_context_create (client);
292 if (matches (pos, service, name))
296 GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0,
297 GNUNET_MESSAGE_TYPE_STATISTICS_END);
298 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
303 * Handle SET-message.
306 * @param client identification of the client
307 * @param message the actual message
310 handle_set (void *cls,
311 struct GNUNET_SERVER_Client *client,
312 const struct GNUNET_MessageHeader *message)
315 * Counter used to generate unique values.
317 static uint32_t uidgen;
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 * Task run during shutdown.
429 shutdown_task (void *cls,
430 const struct GNUNET_SCHEDULER_TaskContext *tc)
437 * Process statistics requests.
440 * @param sched scheduler to use
441 * @param server the initialized server
442 * @param c configuration to use
446 struct GNUNET_SCHEDULER_Handle *sched,
447 struct GNUNET_SERVER_Handle *server,
448 const struct GNUNET_CONFIGURATION_Handle *c)
450 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
451 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
452 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
456 GNUNET_SERVER_add_handlers (server, handlers);
458 GNUNET_SCHEDULER_add_delayed (sched,
459 GNUNET_TIME_UNIT_FOREVER_REL,
466 * The main function for the statistics service.
468 * @param argc number of arguments from the command line
469 * @param argv command line arguments
470 * @return 0 ok, 1 on error
473 main (int argc, char *const *argv)
476 GNUNET_SERVICE_run (argc,
479 GNUNET_SERVICE_OPTION_NONE,
480 &run, NULL)) ? 0 : 1;
483 /* end of gnunet-service-statistics.c */