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 #include "gnunet_disk_lib.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_service_lib.h"
31 #include "gnunet_statistics_service.h"
32 #include "gnunet_strings_lib.h"
33 #include "gnunet_time_lib.h"
34 #include "statistics.h"
37 * Entry in the statistics list.
42 * This is a linked list.
44 struct StatsEntry *next;
47 * Name of the service, points into the
53 * Name for the value, points into
59 * Message that can be used to set this value,
60 * stored at the end of the memory used by
63 struct GNUNET_STATISTICS_SetMessage *msg;
76 * Is this value persistent?
83 * Linked list of our active statistics.
85 static struct StatsEntry *start;
88 * Counter used to generate unique values.
90 static uint32_t uidgen;
93 * Load persistent values from disk. Disk format is
94 * exactly the same format that we also use for
95 * setting the values over the network.
98 load (struct GNUNET_SERVER_Handle *server,
99 struct GNUNET_CONFIGURATION_Handle *cfg)
106 const struct GNUNET_MessageHeader *msg;
108 fn = GNUNET_DISK_get_home_filename (cfg,
109 "statistics", "statistics.data", NULL);
112 if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
117 fd = GNUNET_DISK_file_open (fn, O_RDONLY);
123 buf = mmap (NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
124 if (MAP_FAILED == buf)
126 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
127 GNUNET_break (0 == CLOSE (fd));
131 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
132 _("Loading %llu bytes of statistics from `%s'\n"),
133 (unsigned long long) sb.st_size, fn);
135 while (off + sizeof (struct GNUNET_MessageHeader) < sb.st_size)
137 msg = (const struct GNUNET_MessageHeader *) &buf[off];
138 if ((ntohs (msg->size) + off > sb.st_size) ||
139 (GNUNET_OK != GNUNET_SERVER_inject (server, NULL, msg)))
144 off += ntohs (msg->size);
146 GNUNET_break (0 == munmap (buf, sb.st_size));
147 GNUNET_break (0 == CLOSE (fd));
153 * Write persistent statistics to disk.
156 * @param cfg configuration to use
159 save (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
161 struct StatsEntry *pos;
165 unsigned long long total;
168 fn = GNUNET_DISK_get_home_filename (cfg,
169 "statistics", "statistics.data", NULL);
172 GNUNET_DISK_file_open (fn, O_WRONLY | O_CREAT | O_TRUNC,
175 while (NULL != (pos = start))
178 if ((pos->persistent) && (fd != -1))
180 size = htons (pos->msg->header.size);
181 if (size != WRITE (fd, pos->msg, size))
183 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
185 GNUNET_DISK_file_close (fn, fd);
195 GNUNET_DISK_file_close (fn, fd);
197 GNUNET_break (0 == UNLINK (fn));
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
200 _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
202 GNUNET_free_non_null (fn);
207 * Transmit the given stats value.
210 transmit (struct GNUNET_SERVER_TransmitContext *tc,
211 const struct StatsEntry *e)
213 struct GNUNET_STATISTICS_ReplyMessage *m;
214 struct GNUNET_MessageHeader *h;
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 msize = size - sizeof (struct GNUNET_MessageHeader);
223 m = GNUNET_malloc (size);
224 m->uid = htonl (e->uid);
226 m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
227 m->value = GNUNET_htonll (e->value);
228 size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
229 GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
231 2, e->service, e->name));
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "Transmitting value for `%s:%s': %llu\n",
235 e->service, e->name, e->value);
238 GNUNET_SERVER_transmit_context_append (tc,
241 GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
247 * Does this entry match the request?
250 matches (const struct StatsEntry *e, const char *service, const char *name)
252 return ((0 == strlen (service)) ||
253 (0 == strcmp (service, e->service)))
254 && ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
259 * Handle GET-message.
262 * @param client identification of the client
263 * @param message the actual message
264 * @return GNUNET_OK to keep the connection open,
265 * GNUNET_SYSERR to close it (signal serious error)
268 handle_get (void *cls,
269 struct GNUNET_SERVER_Client *client,
270 const struct GNUNET_MessageHeader *message)
274 struct StatsEntry *pos;
275 struct GNUNET_SERVER_TransmitContext *tc;
278 size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
279 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
280 size, 2, &service, &name))
283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "Received request for statistics on `%s:%s'\n",
289 strlen (service) ? service : "*", strlen (name) ? name : "*");
291 tc = GNUNET_SERVER_transmit_context_create (client);
295 if (matches (pos, service, name))
299 GNUNET_SERVER_transmit_context_append (tc, NULL, 0,
300 GNUNET_MESSAGE_TYPE_STATISTICS_END);
301 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
306 * Handle SET-message.
309 * @param client identification of the client
310 * @param message the actual message
313 handle_set (void *cls,
314 struct GNUNET_SERVER_Client *client,
315 const struct GNUNET_MessageHeader *message)
321 const struct GNUNET_STATISTICS_SetMessage *msg;
322 struct StatsEntry *pos;
323 struct StatsEntry *prev;
328 msize = ntohs (message->size);
329 if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
332 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
335 size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
336 msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
338 if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
339 size, 2, &service, &name))
342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Received request to update statistic on `%s:%s'\n",
350 flags = ntohl (msg->flags);
351 value = GNUNET_ntohll (msg->value);
356 if (matches (pos, service, name))
358 if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
364 delta = (int64_t) value;
365 if ((delta < 0) && (pos->value < -delta))
371 GNUNET_break ((delta <= 0) ||
372 (pos->value + delta > pos->value));
376 pos->msg->value = GNUNET_htonll (pos->value);
377 pos->msg->flags = msg->flags;
379 (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
382 /* move to front for faster setting next time! */
383 prev->next = pos->next;
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Statistic `%s:%s' updated to value %llu.\n",
390 service, name, pos->value);
392 GNUNET_SERVER_receive_done (client, GNUNET_OK);
398 pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
400 if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
401 (0 < (int64_t) GNUNET_ntohll (msg->value)))
402 pos->value = GNUNET_ntohll (msg->value);
404 pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
405 pos->msg = (void *) &pos[1];
406 memcpy (pos->msg, message, ntohs (message->size));
407 pos->service = (const char *) &pos->msg[1];
408 pos->name = &pos->service[strlen (pos->service) + 1];
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "New statistic on `%s:%s' with value %llu created.\n",
414 service, name, pos->value);
416 GNUNET_SERVER_receive_done (client, GNUNET_OK);
421 * List of handlers for the messages understood by this
424 static struct GNUNET_SERVER_MessageHandler handlers[] = {
425 {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
426 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
432 * Process statistics requests.
435 * @param sched scheduler to use
436 * @param server the initialized server
437 * @param cfg configuration to use
441 struct GNUNET_SCHEDULER_Handle *sched,
442 struct GNUNET_SERVER_Handle *server,
443 struct GNUNET_CONFIGURATION_Handle *cfg)
445 GNUNET_SERVER_add_handlers (server, handlers);
451 * The main function for the statistics service.
453 * @param argc number of arguments from the command line
454 * @param argv command line arguments
455 * @return 0 ok, 1 on error
458 main (int argc, char *const *argv)
461 GNUNET_SERVICE_run (argc,
463 "statistics", &run, NULL, &save, NULL)) ? 0 : 1;
466 /* end of gnunet-service-statistics.c */