small API change: do no longer pass rarely needed GNUNET_SCHEDULER_TaskContext to...
[oweals/gnunet.git] / src / statistics / gnunet-service-statistics.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2012, 2014 GNUnet e.V.
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file statistics/gnunet-service-statistics.c
23  * @brief program that tracks statistics
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_bio_lib.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_disk_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_service_lib.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_strings_lib.h"
35 #include "gnunet_time_lib.h"
36 #include "statistics.h"
37
38 /**
39  * Watch entry.
40  */
41 struct WatchEntry
42 {
43
44   /**
45    * Watch entries are kept in a linked list.
46    */
47   struct WatchEntry *next;
48
49   /**
50    * Watch entries are kept in a linked list.
51    */
52   struct WatchEntry *prev;
53
54   /**
55    * For which client is this watch entry?
56    */
57   struct GNUNET_SERVER_Client *client;
58
59   /**
60    * Last value we communicated to the client for this watch entry.
61    */
62   uint64_t last_value;
63
64   /**
65    * Unique watch number for this client and this watched value.
66    */
67   uint32_t wid;
68
69   /**
70    * Is last_value valid
71    * #GNUNET_NO : last_value is n/a, #GNUNET_YES: last_value is valid
72    */
73   int last_value_set;
74
75 };
76
77
78 /**
79  * We keep the statistics organized by subsystem for faster
80  * lookup during SET operations.
81  */
82 struct SubsystemEntry;
83
84
85 /**
86  * Entry in the statistics list.
87  */
88 struct StatsEntry
89 {
90   /**
91    * This is a linked list.
92    */
93   struct StatsEntry *next;
94
95   /**
96    * This is a linked list.
97    */
98   struct StatsEntry *prev;
99
100   /**
101    * Subsystem this entry belongs to.
102    */
103   struct SubsystemEntry *subsystem;
104
105   /**
106    * Name for the value stored by this entry, allocated at the end of
107    * this struct.
108    */
109   const char *name;
110
111   /**
112    * Watch context for changes to this value, or NULL for none.
113    */
114   struct WatchEntry *we_head;
115
116   /**
117    * Watch context for changes to this value, or NULL for none.
118    */
119   struct WatchEntry *we_tail;
120
121   /**
122    * Our value.
123    */
124   uint64_t value;
125
126   /**
127    * Unique ID.
128    */
129   uint32_t uid;
130
131   /**
132    * Is this value persistent?
133    */
134   int persistent;
135
136   /**
137    * Is this value set?
138    * #GNUNET_NO: value is n/a, #GNUNET_YES: value is valid
139    */
140   int set;
141
142 };
143
144
145 /**
146  * We keep the statistics organized by subsystem for faster
147  * lookup during SET operations.
148  */
149 struct SubsystemEntry
150 {
151   /**
152    * Subsystems are kept in a DLL.
153    */
154   struct SubsystemEntry *next;
155
156   /**
157    * Subsystems are kept in a DLL.
158    */
159   struct SubsystemEntry *prev;
160
161   /**
162    * Head of list of values kept for this subsystem.
163    */
164   struct StatsEntry *stat_head;
165
166   /**
167    * Tail of list of values kept for this subsystem.
168    */
169   struct StatsEntry *stat_tail;
170
171   /**
172    * Name of the subsystem this entry is for, allocated at
173    * the end of this struct, do not free().
174    */
175   const char *service;
176
177 };
178
179
180 /**
181  * Client entry.
182  */
183 struct ClientEntry
184 {
185   /**
186    * Corresponding server handle.
187    */
188   struct GNUNET_SERVER_Client *client;
189
190   /**
191    * Which subsystem is this client writing to (SET/UPDATE)?
192    */
193   struct SubsystemEntry *subsystem;
194
195   /**
196    * Maximum watch ID used by this client so far.
197    */
198   uint32_t max_wid;
199
200 };
201
202
203 /**
204  * Our configuration.
205  */
206 static const struct GNUNET_CONFIGURATION_Handle *cfg;
207
208 /**
209  * Head of linked list of subsystems with active statistics.
210  */
211 static struct SubsystemEntry *sub_head;
212
213 /**
214  * Tail of linked list of subsystems with active statistics.
215  */
216 static struct SubsystemEntry *sub_tail;
217
218 /**
219  * Number of connected clients.
220  */
221 static unsigned int client_count;
222
223 /**
224  * Handle to our server.
225  */
226 static struct GNUNET_SERVER_Handle *srv;
227
228 /**
229  * Our notification context.
230  */
231 static struct GNUNET_SERVER_NotificationContext *nc;
232
233 /**
234  * Counter used to generate unique values.
235  */
236 static uint32_t uidgen;
237
238 /**
239  * Set to #GNUNET_YES if we are shutting down as soon as possible.
240  */
241 static int in_shutdown;
242
243
244 /**
245  * Inject a message to our server with a client of 'NULL'.
246  *
247  * @param cls the `struct GNUNET_SERVER_Handle`
248  * @param client unused
249  * @param msg message to inject
250  */
251 static int
252 inject_message (void *cls,
253                 void *client,
254                 const struct GNUNET_MessageHeader *msg)
255 {
256   struct GNUNET_SERVER_Handle *server = cls;
257
258   GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
259   return GNUNET_OK;
260 }
261
262
263 /**
264  * Load persistent values from disk.  Disk format is exactly the same
265  * format that we also use for setting the values over the network.
266  *
267  * @param server handle to the server context
268  */
269 static void
270 load (struct GNUNET_SERVER_Handle *server)
271 {
272   char *fn;
273   struct GNUNET_BIO_ReadHandle *rh;
274   uint64_t fsize;
275   char *buf;
276   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
277
278   if (GNUNET_OK !=
279       GNUNET_CONFIGURATION_get_value_filename (cfg,
280                                                "STATISTICS",
281                                                "DATABASE",
282                                                &fn))
283   {
284     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
285                                "STATISTICS",
286                                "DATABASE");
287     return;
288   }
289   if ( (GNUNET_OK !=
290         GNUNET_DISK_file_size (fn,
291                                &fsize,
292                                GNUNET_NO,
293                                GNUNET_YES)) ||
294        (0 == fsize) )
295   {
296     GNUNET_free (fn);
297     return;
298   }
299   buf = GNUNET_malloc (fsize);
300   rh = GNUNET_BIO_read_open (fn);
301   if (!rh)
302   {
303     GNUNET_free (buf);
304     GNUNET_free (fn);
305     return;
306   }
307   if (GNUNET_OK !=
308       GNUNET_BIO_read (rh,
309                        fn,
310                        buf,
311                        fsize))
312   {
313     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
314                               "read",
315                               fn);
316     GNUNET_break (GNUNET_OK ==
317                   GNUNET_BIO_read_close (rh, NULL));
318     GNUNET_free (buf);
319     GNUNET_free (fn);
320     return;
321   }
322   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
323               _("Loading %llu bytes of statistics from `%s'\n"),
324               fsize, fn);
325   mst = GNUNET_SERVER_mst_create (&inject_message,
326                                   server);
327   GNUNET_break (GNUNET_OK ==
328                 GNUNET_SERVER_mst_receive (mst, NULL,
329                                            buf, fsize,
330                                            GNUNET_YES,
331                                            GNUNET_NO));
332   GNUNET_SERVER_mst_destroy (mst);
333   GNUNET_free (buf);
334   GNUNET_break (GNUNET_OK ==
335                 GNUNET_BIO_read_close (rh,
336                                        NULL));
337   GNUNET_free (fn);
338 }
339
340
341 /**
342  * Write persistent statistics to disk.
343  */
344 static void
345 save ()
346 {
347   struct SubsystemEntry *se;
348   struct StatsEntry *pos;
349   char *fn;
350   struct GNUNET_BIO_WriteHandle *wh;
351   uint16_t size;
352   unsigned long long total;
353   size_t nlen;
354   size_t slen;
355   struct GNUNET_STATISTICS_SetMessage *msg;
356
357   if (GNUNET_OK !=
358       GNUNET_CONFIGURATION_get_value_filename (cfg,
359                                                "STATISTICS",
360                                                "DATABASE",
361                                                &fn))
362   {
363     GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
364                                "STATISTICS",
365                                "DATABASE");
366     return;
367   }
368   (void) GNUNET_DISK_directory_create_for_file (fn);
369   wh = GNUNET_BIO_write_open (fn);
370   total = 0;
371   while (NULL != (se = sub_head))
372   {
373     GNUNET_CONTAINER_DLL_remove (sub_head,
374                                  sub_tail,
375                                  se);
376     slen = strlen (se->service) + 1;
377     while (NULL != (pos = se->stat_head))
378     {
379       GNUNET_CONTAINER_DLL_remove (se->stat_head,
380                                    se->stat_tail,
381                                    pos);
382       if ((pos->persistent) && (NULL != wh))
383       {
384         nlen = strlen (pos->name) + 1;
385         size = sizeof (struct GNUNET_STATISTICS_SetMessage) + nlen + slen;
386         GNUNET_assert (size < UINT16_MAX);
387         msg = GNUNET_malloc (size);
388
389         msg->header.size = htons ((uint16_t) size);
390         msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
391         GNUNET_assert (nlen + slen ==
392                        GNUNET_STRINGS_buffer_fill ((char *) &msg[1],
393                                                    nlen + slen,
394                                                    2,
395                                                    se->service,
396                                                    pos->name));
397         msg->flags = htonl (pos->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
398         msg->value = GNUNET_htonll (pos->value);
399         if (GNUNET_OK != GNUNET_BIO_write (wh,
400                                            msg,
401                                            size))
402         {
403           GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
404                                     "write",
405                                     fn);
406           if (GNUNET_OK != GNUNET_BIO_write_close (wh))
407             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
408                                       "close",
409                                       fn);
410           wh = NULL;
411         }
412         else
413         {
414           total += size;
415         }
416         GNUNET_free (msg);
417       }
418       GNUNET_free (pos);
419     }
420     GNUNET_free (se);
421   }
422   if (NULL != wh)
423   {
424     if (GNUNET_OK !=
425         GNUNET_BIO_write_close (wh))
426       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
427                                 "close",
428                                 fn);
429     if (0 == total)
430       GNUNET_break (0 ==
431                     UNLINK (fn));
432     else
433       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
434                   _("Wrote %llu bytes of statistics to `%s'\n"),
435                   total,
436                   fn);
437   }
438   GNUNET_free_non_null (fn);
439 }
440
441
442 /**
443  * Transmit the given stats value.
444  *
445  * @param client receiver of the value
446  * @param e value to transmit
447  */
448 static void
449 transmit (struct GNUNET_SERVER_Client *client,
450           const struct StatsEntry *e)
451 {
452   struct GNUNET_STATISTICS_ReplyMessage *m;
453   size_t size;
454
455   size = sizeof (struct GNUNET_STATISTICS_ReplyMessage) +
456     strlen (e->subsystem->service) + 1 +
457     strlen (e->name) + 1;
458   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
459   m = GNUNET_malloc (size);
460   m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
461   m->header.size = htons (size);
462   m->uid = htonl (e->uid);
463   if (e->persistent)
464     m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
465   m->value = GNUNET_htonll (e->value);
466   size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
467   GNUNET_assert (size ==
468                  GNUNET_STRINGS_buffer_fill ((char *) &m[1],
469                                              size,
470                                              2,
471                                              e->subsystem->service,
472                                              e->name));
473   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474               "Transmitting value for `%s:%s' (%d): %llu\n",
475               e->subsystem->service,
476               e->name,
477               e->persistent,
478               e->value);
479   GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
480                                               GNUNET_NO);
481   GNUNET_free (m);
482 }
483
484
485 /**
486  * Find a client entry for the given client handle, or create one.
487  *
488  * @param client handle to match
489  * @return corresponding client entry struct
490  */
491 static struct ClientEntry *
492 make_client_entry (struct GNUNET_SERVER_Client *client)
493 {
494   struct ClientEntry *ce;
495
496   ce = GNUNET_SERVER_client_get_user_context (client,
497                                               struct ClientEntry);
498   if (NULL != ce)
499     return ce;
500   if (NULL == nc)
501   {
502     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
503     return NULL;
504   }
505   ce = GNUNET_new (struct ClientEntry);
506   ce->client = client;
507   GNUNET_SERVER_client_set_user_context (client, ce);
508   client_count++;
509   GNUNET_SERVER_notification_context_add (nc, client);
510   return ce;
511 }
512
513
514 /**
515  * Handle GET-message.
516  *
517  * @param cls closure
518  * @param client identification of the client
519  * @param message the actual message
520  * @return #GNUNET_OK to keep the connection open,
521  *         #GNUNET_SYSERR to close it (signal serious error)
522  */
523 static void
524 handle_get (void *cls,
525             struct GNUNET_SERVER_Client *client,
526             const struct GNUNET_MessageHeader *message)
527 {
528   struct GNUNET_MessageHeader end;
529   const char *service;
530   const char *name;
531   size_t slen;
532   size_t nlen;
533   struct SubsystemEntry *se;
534   struct StatsEntry *pos;
535   size_t size;
536
537   if (NULL == make_client_entry (client))
538     return; /* new client during shutdown */
539   size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
540   if (size !=
541       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
542                                       size,
543                                       2,
544                                       &service,
545                                       &name))
546   {
547     GNUNET_break (0);
548     GNUNET_SERVER_receive_done (client,
549                                 GNUNET_SYSERR);
550     return;
551   }
552   slen = strlen (service);
553   nlen = strlen (name);
554   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555               "Received request for statistics on `%s:%s'\n",
556               slen ? service : "*",
557               nlen ? name : "*");
558   for (se = sub_head; NULL != se; se = se->next)
559   {
560     if (! ( (0 == slen) ||
561             (0 == strcmp (service, se->service))) )
562       continue;
563     for (pos = se->stat_head; NULL != pos; pos = pos->next)
564     {
565       if  (! ( (0 == nlen) ||
566                (0 == strcmp (name, pos->name))) )
567         continue;
568       transmit (client, pos);
569     }
570   }
571   end.size = htons (sizeof (struct GNUNET_MessageHeader));
572   end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
573   GNUNET_SERVER_notification_context_unicast (nc,
574                                               client,
575                                               &end,
576                                               GNUNET_NO);
577   GNUNET_SERVER_receive_done (client, GNUNET_OK);
578 }
579
580
581 /**
582  * Notify all clients listening about a change to a value.
583  *
584  * @param se value that changed
585  */
586 static void
587 notify_change (struct StatsEntry *se)
588 {
589   struct GNUNET_STATISTICS_WatchValueMessage wvm;
590   struct WatchEntry *pos;
591
592   for (pos = se->we_head; NULL != pos; pos = pos->next)
593   {
594     if (GNUNET_YES == pos->last_value_set)
595     {
596       if (pos->last_value == se->value)
597         continue;
598     }
599     else
600     {
601       pos->last_value_set = GNUNET_YES;
602     }
603     wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
604     wvm.header.size =
605       htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
606     wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
607     wvm.wid = htonl (pos->wid);
608     wvm.reserved = htonl (0);
609     wvm.value = GNUNET_htonll (se->value);
610     GNUNET_SERVER_notification_context_unicast (nc,
611                                                 pos->client,
612                                                 &wvm.header,
613                                                 GNUNET_NO);
614     pos->last_value = se->value;
615   }
616 }
617
618
619 /**
620  * Find the subsystem entry of the given name for the specified client.
621  *
622  * @param ce client looking for the subsystem, may contain a hint
623  *           to find the entry faster, can be NULL
624  * @param service name of the subsystem to look for
625  * @return subsystem entry, never NULL (subsystem entry is created if necessary)
626  */
627 static struct SubsystemEntry *
628 find_subsystem_entry (struct ClientEntry *ce,
629                       const char *service)
630 {
631   size_t slen;
632   struct SubsystemEntry *se;
633
634   if (NULL != ce)
635     se = ce->subsystem;
636   else
637     se = NULL;
638   if ( (NULL == se) ||
639        (0 != strcmp (service,
640                      se->service)) )
641   {
642     for (se = sub_head; NULL != se; se = se->next)
643       if (0 == strcmp (service,
644                        se->service))
645         break;
646     if (NULL != ce)
647       ce->subsystem = se;
648   }
649   if (NULL != se)
650     return se;
651   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
652               "Allocating new subsystem entry `%s'\n",
653               service);
654   slen = strlen (service) + 1;
655   se = GNUNET_malloc (sizeof (struct SubsystemEntry) +
656                       slen);
657   memcpy (&se[1],
658           service,
659           slen);
660   se->service = (const char *) &se[1];
661   GNUNET_CONTAINER_DLL_insert (sub_head,
662                                sub_tail,
663                                se);
664   if (NULL != ce)
665     ce->subsystem = se;
666   return se;
667 }
668
669
670 /**
671  * Find the statistics entry of the given subsystem.
672  *
673  * @param subsystem subsystem to look in
674  * @param name name of the entry to look for
675  * @return statistis entry, or NULL if not found
676  */
677 static struct StatsEntry *
678 find_stat_entry (struct SubsystemEntry *se,
679                  const char *name)
680 {
681   struct StatsEntry *pos;
682
683   for (pos = se->stat_head; NULL != pos; pos = pos->next)
684     if  (0 == strcmp (name, pos->name))
685       return pos;
686   return NULL;
687 }
688
689
690 /**
691  * Handle SET-message.
692  *
693  * @param cls closure
694  * @param client identification of the client
695  * @param message the actual message
696  */
697 static void
698 handle_set (void *cls,
699             struct GNUNET_SERVER_Client *client,
700             const struct GNUNET_MessageHeader *message)
701 {
702   const char *service;
703   const char *name;
704   size_t nlen;
705   uint16_t msize;
706   uint16_t size;
707   const struct GNUNET_STATISTICS_SetMessage *msg;
708   struct SubsystemEntry *se;
709   struct ClientEntry *ce;
710   struct StatsEntry *pos;
711   uint32_t flags;
712   uint64_t value;
713   int64_t delta;
714   int changed;
715   int initial_set;
716
717   ce = NULL;
718   if ( (NULL != client) &&
719        (NULL == (ce = make_client_entry (client))) )
720     return; /* new client during shutdown */
721   msize = ntohs (message->size);
722   if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
723   {
724     GNUNET_break (0);
725     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
726     return;
727   }
728   size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
729   msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
730   if (size !=
731       GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
732                                       size,
733                                       2,
734                                       &service,
735                                       &name))
736   {
737     GNUNET_break (0);
738     GNUNET_SERVER_receive_done (client,
739                                 GNUNET_SYSERR);
740     return;
741   }
742   se = find_subsystem_entry (ce, service);
743   flags = ntohl (msg->flags);
744   value = GNUNET_ntohll (msg->value);
745   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746               "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
747               service,
748               name,
749               (unsigned int) flags,
750               (unsigned long long) value);
751   pos = find_stat_entry (se, name);
752   if (NULL != pos)
753   {
754     initial_set = 0;
755     if (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE))
756     {
757       changed = (pos->value != value);
758       pos->value = value;
759     }
760     else
761     {
762       delta = (int64_t) value;
763       if ((delta < 0) && (pos->value < -delta))
764       {
765         changed = (0 != pos->value);
766         pos->value = 0;
767       }
768       else
769       {
770         changed = (0 != delta);
771         GNUNET_break ( (delta <= 0) ||
772                        (pos->value + delta > pos->value) );
773         pos->value += delta;
774       }
775     }
776     if (GNUNET_NO == pos->set)
777     {
778       pos->set = GNUNET_YES;
779       initial_set = 1;
780     }
781     pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
782     if (pos != se->stat_head)
783     {
784       /* move to front for faster setting next time! */
785       GNUNET_CONTAINER_DLL_remove (se->stat_head,
786                                    se->stat_tail,
787                                    pos);
788       GNUNET_CONTAINER_DLL_insert (se->stat_head,
789                                    se->stat_tail,
790                                    pos);
791     }
792     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793                 "Statistic `%s:%s' updated to value %llu (%d).\n",
794                 service,
795                 name,
796                 pos->value,
797                 pos->persistent);
798     if ( (changed) ||
799          (1 == initial_set) )
800       notify_change (pos);
801     GNUNET_SERVER_receive_done (client,
802                                 GNUNET_OK);
803     return;
804   }
805   /* not found, create a new entry */
806   nlen = strlen (name) + 1;
807   pos = GNUNET_malloc (sizeof (struct StatsEntry) + nlen);
808   memcpy (&pos[1],
809           name,
810           nlen);
811   pos->name = (const char *) &pos[1];
812   pos->subsystem = se;
813   if ( (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE)) ||
814        (0 < (int64_t) GNUNET_ntohll (msg->value)) )
815   {
816     pos->value = GNUNET_ntohll (msg->value);
817     pos->set = GNUNET_YES;
818   }
819   else
820   {
821     pos->set = GNUNET_NO;
822   }
823   pos->uid = uidgen++;
824   pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
825   GNUNET_CONTAINER_DLL_insert (se->stat_head,
826                                se->stat_tail,
827                                pos);
828   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829               "New statistic on `%s:%s' with value %llu created.\n",
830               service,
831               name,
832               pos->value);
833   GNUNET_SERVER_receive_done (client,
834                               GNUNET_OK);
835 }
836
837
838 /**
839  * Handle WATCH-message.
840  *
841  * @param cls closure
842  * @param client identification of the client
843  * @param message the actual message
844  */
845 static void
846 handle_watch (void *cls,
847               struct GNUNET_SERVER_Client *client,
848               const struct GNUNET_MessageHeader *message)
849 {
850   const char *service;
851   const char *name;
852   uint16_t msize;
853   uint16_t size;
854   struct SubsystemEntry *se;
855   struct StatsEntry *pos;
856   struct ClientEntry *ce;
857   struct WatchEntry *we;
858   size_t nlen;
859
860   if (NULL == nc)
861   {
862     GNUNET_SERVER_receive_done (client,
863                                 GNUNET_SYSERR);
864     return;
865   }
866   GNUNET_SERVER_client_mark_monitor (client);
867   ce = make_client_entry (client);
868   msize = ntohs (message->size);
869   if (msize < sizeof (struct GNUNET_MessageHeader))
870   {
871     GNUNET_break (0);
872     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
873     return;
874   }
875   size = msize - sizeof (struct GNUNET_MessageHeader);
876   if (size !=
877       GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
878                                       size,
879                                       2,
880                                       &service,
881                                       &name))
882   {
883     GNUNET_break (0);
884     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
885     return;
886   }
887   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888               "Received request to watch statistic on `%s:%s'\n",
889               service,
890               name);
891   se = find_subsystem_entry (ce, service);
892   pos = find_stat_entry (se, name);
893   if (NULL == pos)
894   {
895     nlen = strlen (name) + 1;
896     pos = GNUNET_malloc (sizeof (struct StatsEntry) +
897                          nlen);
898     memcpy (&pos[1], name, nlen);
899     pos->name = (const char *) &pos[1];
900     pos->subsystem = se;
901     GNUNET_CONTAINER_DLL_insert (se->stat_head,
902                                  se->stat_tail,
903                                  pos);
904     pos->uid = uidgen++;
905     pos->set = GNUNET_NO;
906     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907                 "New statistic on `%s:%s' with value %llu created.\n",
908                 service,
909                 name,
910                 pos->value);
911   }
912   we = GNUNET_new (struct WatchEntry);
913   we->client = client;
914   we->last_value_set = GNUNET_NO;
915   we->wid = ce->max_wid++;
916   GNUNET_CONTAINER_DLL_insert (pos->we_head,
917                                pos->we_tail,
918                                we);
919   if (0 != pos->value)
920     notify_change (pos);
921   GNUNET_SERVER_receive_done (client, GNUNET_OK);
922 }
923
924
925 /**
926  * Actually perform the shutdown.
927  */
928 static void
929 do_shutdown ()
930 {
931   struct WatchEntry *we;
932   struct StatsEntry *pos;
933   struct SubsystemEntry *se;
934
935   if (NULL == nc)
936     return;
937   save ();
938   GNUNET_SERVER_notification_context_destroy (nc);
939   nc = NULL;
940   GNUNET_assert (0 == client_count);
941   while (NULL != (se = sub_head))
942   {
943     GNUNET_CONTAINER_DLL_remove (sub_head,
944                                  sub_tail,
945                                  se);
946     while (NULL != (pos = se->stat_head))
947     {
948       GNUNET_CONTAINER_DLL_remove (se->stat_head,
949                                    se->stat_tail,
950                                    pos);
951       while (NULL != (we = pos->we_head))
952       {
953         GNUNET_break (0);
954         GNUNET_CONTAINER_DLL_remove (pos->we_head,
955                                      pos->we_tail,
956                                      we);
957         GNUNET_free (we);
958       }
959       GNUNET_free (pos);
960     }
961     GNUNET_free (se);
962   }
963 }
964
965
966 /**
967  * Task run during shutdown.
968  *
969  * @param cls unused
970  */
971 static void
972 shutdown_task (void *cls)
973 {
974   in_shutdown = GNUNET_YES;
975   if (0 != client_count)
976     return;
977   do_shutdown ();
978 }
979
980
981 /**
982  * A client disconnected.  Remove all of its data structure entries.
983  *
984  * @param cls closure, NULL
985  * @param client identification of the client
986  */
987 static void
988 handle_client_disconnect (void *cls,
989                           struct GNUNET_SERVER_Client *client)
990 {
991   struct ClientEntry *ce;
992   struct WatchEntry *we;
993   struct WatchEntry *wen;
994   struct StatsEntry *pos;
995   struct SubsystemEntry *se;
996
997   if (NULL == client)
998     return;
999   ce = GNUNET_SERVER_client_get_user_context (client,
1000                                               struct ClientEntry);
1001   if (NULL == ce)
1002     return;
1003   GNUNET_SERVER_client_set_user_context (client,
1004                                          NULL);
1005   client_count--;
1006   for (se = sub_head; NULL != se; se = se->next)
1007   {
1008     for (pos = se->stat_head; NULL != pos; pos = pos->next)
1009     {
1010       wen = pos->we_head;
1011       while (NULL != (we = wen))
1012       {
1013         wen = we->next;
1014         if (we->client != client)
1015           continue;
1016         GNUNET_CONTAINER_DLL_remove (pos->we_head,
1017                                      pos->we_tail,
1018                                      we);
1019         GNUNET_free (we);
1020       }
1021     }
1022   }
1023   if ( (0 == client_count) &&
1024        (GNUNET_YES == in_shutdown) )
1025     do_shutdown ();
1026 }
1027
1028
1029 /**
1030  * Process statistics requests.
1031  *
1032  * @param cls closure
1033  * @param server the initialized server
1034  * @param c configuration to use
1035  */
1036 static void
1037 run (void *cls,
1038      struct GNUNET_SERVER_Handle *server,
1039      const struct GNUNET_CONFIGURATION_Handle *c)
1040 {
1041   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
1042     {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
1043     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
1044     {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
1045     {NULL, NULL, 0, 0}
1046   };
1047   cfg = c;
1048   srv = server;
1049   GNUNET_SERVER_add_handlers (server,
1050                               handlers);
1051   nc = GNUNET_SERVER_notification_context_create (server, 16);
1052   GNUNET_SERVER_disconnect_notify (server,
1053                                    &handle_client_disconnect,
1054                                    NULL);
1055   load (server);
1056   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1057                                 &shutdown_task,
1058                                 NULL);
1059 }
1060
1061
1062 /**
1063  * The main function for the statistics service.
1064  *
1065  * @param argc number of arguments from the command line
1066  * @param argv command line arguments
1067  * @return 0 ok, 1 on error
1068  */
1069 int
1070 main (int argc, char *const *argv)
1071 {
1072   return (GNUNET_OK ==
1073           GNUNET_SERVICE_run (argc, argv, "statistics",
1074                               GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN,
1075                               &run, NULL)) ? 0 : 1;
1076 }
1077
1078 #if defined(LINUX) && defined(__GLIBC__)
1079 #include <malloc.h>
1080
1081 /**
1082  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1083  */
1084 void __attribute__ ((constructor))
1085 GNUNET_STATISTICS_memory_init ()
1086 {
1087   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1088   mallopt (M_TOP_PAD, 1 * 1024);
1089   malloc_trim (0);
1090 }
1091 #endif
1092
1093
1094 /* end of gnunet-service-statistics.c */