fixes
[oweals/gnunet.git] / src / statistics / gnunet-service-statistics.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file statistics/gnunet-service-statistics.c
23  * @brief program that tracks statistics
24  * @author Christian Grothoff
25  * 
26  * TODO:
27  * - use BIO for IO operations
28  */
29 #include "platform.h"
30 #include "gnunet_container_lib.h"
31 #include "gnunet_disk_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_service_lib.h"
35 #include "gnunet_statistics_service.h"
36 #include "gnunet_strings_lib.h"
37 #include "gnunet_time_lib.h"
38 #include "statistics.h"
39
40 /**
41  * Watch entry.
42  */
43 struct WatchEntry
44 {
45
46   struct WatchEntry *next;
47
48   struct WatchEntry *prev;
49
50   struct GNUNET_SERVER_Client *client;
51
52   uint64_t last_value;
53   
54   uint32_t wid;
55
56 };
57
58
59 /**
60  * Client entry.
61  */
62 struct ClientEntry
63 {
64
65   struct ClientEntry *next;
66
67   struct ClientEntry *prev;
68
69   struct GNUNET_SERVER_Client *client;
70   
71   uint32_t max_wid;
72
73 };
74
75 /**
76  * Entry in the statistics list.
77  */
78 struct StatsEntry
79 {
80   /**
81    * This is a linked list.
82    */
83   struct StatsEntry *next;
84
85   /**
86    * Name of the service, points into the
87    * middle of msg.
88    */
89   const char *service;
90
91   /**
92    * Name for the value, points into
93    * the middle of msg.
94    */
95   const char *name;
96
97   /**
98    * Message that can be used to set this value,
99    * stored at the end of the memory used by
100    * this struct.
101    */
102   struct GNUNET_STATISTICS_SetMessage *msg;
103
104   /**
105    * Watch context for changes to this
106    * value, or NULL for none.
107    */
108   struct WatchEntry *we_head;
109
110   /**
111    * Watch context for changes to this
112    * value, or NULL for none.
113    */
114   struct WatchEntry *we_tail;
115
116   /**
117    * Our value.
118    */
119   uint64_t value;
120
121   /**
122    * Unique ID.
123    */
124   uint32_t uid;
125
126   /**
127    * Is this value persistent?
128    */
129   int persistent;
130
131 };
132
133 /**
134  * Our configuration.
135  */
136 static const struct GNUNET_CONFIGURATION_Handle *cfg;
137
138 /**
139  * Linked list of our active statistics.
140  */
141 static struct StatsEntry *start;
142
143 static struct ClientEntry *client_head;
144
145 static struct ClientEntry *client_tail;
146
147 /**
148  * Our notification context.
149  */
150 static struct GNUNET_SERVER_NotificationContext *nc;
151
152 /**
153  * Counter used to generate unique values.
154  */
155 static uint32_t uidgen;
156
157
158 static void
159 inject_message (void *cls,
160                 void *client,
161                 const struct GNUNET_MessageHeader *msg)
162 {
163   struct GNUNET_SERVER_Handle *server = cls;
164
165   GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
166 }
167
168
169 /**
170  * Load persistent values from disk.  Disk format is
171  * exactly the same format that we also use for
172  * setting the values over the network.
173  *
174  * @param server handle to the server context
175  */
176 static void
177 load (struct GNUNET_SERVER_Handle *server)
178 {
179   char *fn;
180   struct GNUNET_DISK_FileHandle *fh;
181   struct GNUNET_DISK_MapHandle *mh;
182   struct stat sb;
183   char *buf;
184   struct GNUNET_SERVER_MessageStreamTokenizer *mst;
185
186   fn = GNUNET_DISK_get_home_filename (cfg,
187                                       "statistics", "statistics.data", NULL);
188   if (fn == NULL)
189     return;
190   if ((0 != stat (fn, &sb)) || (sb.st_size == 0))
191     {
192       GNUNET_free (fn);
193       return;
194     }
195   fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
196                               GNUNET_DISK_PERM_NONE);
197   if (!fh)
198     {
199       GNUNET_free (fn);
200       return;
201     }
202   buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
203   if (NULL == buf)
204     {
205       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
206       GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
207       GNUNET_free (fn);
208       return;
209     }
210   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
211               _("Loading %llu bytes of statistics from `%s'\n"),
212               (unsigned long long) sb.st_size, fn);
213   mst = GNUNET_SERVER_mst_create (&inject_message,
214                                   server);
215   GNUNET_break (GNUNET_OK ==
216                 GNUNET_SERVER_mst_receive (mst,
217                                            NULL,
218                                            buf,
219                                            sb.st_size,
220                                            GNUNET_YES,
221                                            GNUNET_NO));
222   GNUNET_SERVER_mst_destroy (mst);
223   GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
224   GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
225   GNUNET_free (fn);
226 }
227
228 /**
229  * Write persistent statistics to disk.
230  */
231 static void
232 save ()       
233 {
234   struct StatsEntry *pos;
235   char *fn;
236   struct GNUNET_DISK_FileHandle *fh;
237   uint16_t size;
238   unsigned long long total;
239
240   fh = NULL;
241   fn = GNUNET_DISK_get_home_filename (cfg,
242                                       "statistics", "statistics.data", NULL);
243   if (fn != NULL)
244     fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
245         | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
246         GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
247   total = 0;
248   while (NULL != (pos = start))
249     {
250       start = pos->next;
251       if ((pos->persistent) && (NULL != fh))
252         {
253           size = htons (pos->msg->header.size);
254           if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
255             {
256               GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
257                                         "write", fn);
258               GNUNET_DISK_file_close (fh);
259               fh = NULL;
260             }
261           else
262             total += size;
263         }
264       GNUNET_free (pos);
265     }
266   if (NULL != fh)
267     {
268       GNUNET_DISK_file_close (fh);
269       if (total == 0)
270         GNUNET_break (0 == UNLINK (fn));
271       else
272         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
273                     _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
274     }
275   GNUNET_free_non_null (fn);
276 }
277
278
279 /**
280  * Transmit the given stats value.
281  */
282 static void
283 transmit (struct GNUNET_SERVER_Client *client,
284           const struct StatsEntry *e)
285 {
286   struct GNUNET_STATISTICS_ReplyMessage *m;
287   size_t size;
288
289   size =
290     sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
291     strlen (e->name) + 1;
292   GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
293   m = GNUNET_malloc (size);
294   m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
295   m->header.size = htons (size);
296   m->uid = htonl (e->uid);
297   if (e->persistent)
298     m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
299   m->value = GNUNET_htonll (e->value);
300   size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
301   GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
302                                                      size,
303                                                      2, e->service, e->name));
304 #if DEBUG_STATISTICS
305   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306               "Transmitting value for `%s:%s' (%d): %llu\n",
307               e->service, e->name,
308               e->persistent, e->value);
309 #endif
310   GNUNET_SERVER_notification_context_unicast (nc, client, &m->header, GNUNET_NO);
311   GNUNET_free (m);
312 }
313
314
315 /**
316  * Does this entry match the request?
317  */
318 static int
319 matches (const struct StatsEntry *e, const char *service, const char *name)
320 {
321   return ((0 == strlen (service)) ||
322           (0 == strcmp (service, e->service)))
323     && ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
324 }
325
326
327 static struct ClientEntry *
328 make_client_entry (struct GNUNET_SERVER_Client *client)
329 {
330   struct ClientEntry *ce;
331
332   if (client == NULL)
333     return NULL;
334   ce = client_head;
335   while (ce != NULL)
336     {
337       if (ce->client == client)
338         return ce;
339       ce = ce->next;
340     }
341   ce = GNUNET_malloc (sizeof (struct ClientEntry));
342   ce->client = client;
343   GNUNET_SERVER_client_keep (client);
344   GNUNET_CONTAINER_DLL_insert (client_head,
345                                client_tail,
346                                ce);
347   GNUNET_SERVER_notification_context_add (nc,
348                                           client);
349   return ce;
350 }
351
352
353 /**
354  * Handle GET-message.
355  *
356  * @param cls closure
357  * @param client identification of the client
358  * @param message the actual message
359  * @return GNUNET_OK to keep the connection open,
360  *         GNUNET_SYSERR to close it (signal serious error)
361  */
362 static void
363 handle_get (void *cls,
364             struct GNUNET_SERVER_Client *client,
365             const struct GNUNET_MessageHeader *message)
366 {
367   struct GNUNET_MessageHeader end;
368   char *service;
369   char *name;
370   struct StatsEntry *pos;
371   size_t size;
372
373   make_client_entry (client);
374   size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
375   if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
376                                               size, 2, &service, &name))
377     {
378       GNUNET_break (0);
379       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
380       return;
381     }
382 #if DEBUG_STATISTICS
383   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384               "Received request for statistics on `%s:%s'\n",
385               strlen (service) ? service : "*", strlen (name) ? name : "*");
386 #endif
387   pos = start;
388   while (pos != NULL)
389     {
390       if (matches (pos, service, name))
391         transmit (client, pos);
392       pos = pos->next;
393     }
394   end.size = htons (sizeof (struct GNUNET_MessageHeader));
395   end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
396   GNUNET_SERVER_notification_context_unicast (nc,
397                                               client,
398                                               &end,
399                                               GNUNET_NO);
400   GNUNET_SERVER_receive_done (client,
401                               GNUNET_OK);
402 }
403
404
405 static void
406 notify_change (struct StatsEntry *se)
407 {
408   struct GNUNET_STATISTICS_WatchValueMessage wvm;
409   struct WatchEntry *pos;
410
411   pos = se->we_head;
412   while (pos != NULL)
413     {
414       if (pos->last_value != se->value)
415         {
416           wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
417           wvm.header.size = htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
418           wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
419           wvm.wid = htonl (pos->wid);
420           wvm.reserved = htonl (0);
421           wvm.value = GNUNET_htonll (se->value);
422           GNUNET_SERVER_notification_context_unicast (nc,
423                                                       pos->client,
424                                                       &wvm.header,
425                                                       GNUNET_NO);
426           pos->last_value = se->value;
427         }
428       pos = pos->next;
429     }
430 }
431
432 /**
433  * Handle SET-message.
434  *
435  * @param cls closure
436  * @param client identification of the client
437  * @param message the actual message
438  */
439 static void
440 handle_set (void *cls,
441             struct GNUNET_SERVER_Client *client,
442             const struct GNUNET_MessageHeader *message)
443 {
444   char *service;
445   char *name;
446   uint16_t msize;
447   uint16_t size;
448   const struct GNUNET_STATISTICS_SetMessage *msg;
449   struct StatsEntry *pos;
450   struct StatsEntry *prev;
451   uint32_t flags;
452   uint64_t value;
453   int64_t delta;
454   int changed;
455
456   make_client_entry (client);
457   msize = ntohs (message->size);
458   if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
459     {
460       GNUNET_break (0);
461       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
462       return;
463     }
464   size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
465   msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
466
467   if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
468                                               size, 2, &service, &name))
469     {
470       GNUNET_break (0);
471       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
472       return;
473     }
474   flags = ntohl (msg->flags);
475   value = GNUNET_ntohll (msg->value);
476 #if DEBUG_STATISTICS
477   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
478               "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
479               service, name,
480               (unsigned int) flags,
481               (unsigned long long) value);
482 #endif
483   pos = start;
484   prev = NULL;
485   while (pos != NULL)
486     {
487       if (matches (pos, service, name))
488         {
489           if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
490             {
491               changed = (pos->value != value);
492               pos->value = value;
493             }
494           else
495             {
496               delta = (int64_t) value;
497               if ((delta < 0) && (pos->value < -delta))
498                 {
499                   changed = (pos->value != 0);
500                   pos->value = 0;
501                 }
502               else
503                 {
504                   changed = (delta != 0);
505                   GNUNET_break ((delta <= 0) ||
506                                 (pos->value + delta > pos->value));
507                   pos->value += delta;
508                 }
509             }
510           pos->msg->value = GNUNET_htonll (pos->value);
511           pos->msg->flags = msg->flags;
512           pos->persistent =
513             (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
514           if (prev != NULL)
515             {
516               /* move to front for faster setting next time! */
517               prev->next = pos->next;
518               pos->next = start;
519               start = pos;
520             }
521 #if DEBUG_STATISTICS
522           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523                       "Statistic `%s:%s' updated to value %llu.\n",
524                       service, name, pos->value);
525 #endif
526           if (changed) 
527             notify_change (pos);
528           GNUNET_SERVER_receive_done (client, GNUNET_OK);
529           return;
530         }
531       prev = pos;
532       pos = pos->next;
533     }
534   pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
535   pos->next = start;
536   if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
537       (0 < (int64_t) GNUNET_ntohll (msg->value)))
538     pos->value = GNUNET_ntohll (msg->value);
539   pos->uid = uidgen++;
540   pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
541   pos->msg = (void *) &pos[1];
542   memcpy (pos->msg, message, ntohs (message->size));
543   pos->service = (const char *) &pos->msg[1];
544   pos->name = &pos->service[strlen (pos->service) + 1];
545
546   start = pos;
547 #if DEBUG_STATISTICS
548   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549               "New statistic on `%s:%s' with value %llu created.\n",
550               service, name, pos->value);
551 #endif
552   GNUNET_SERVER_receive_done (client, GNUNET_OK);
553 }
554
555
556 /**
557  * Handle WATCH-message.
558  *
559  * @param cls closure
560  * @param client identification of the client
561  * @param message the actual message
562  */
563 static void
564 handle_watch (void *cls,
565               struct GNUNET_SERVER_Client *client,
566               const struct GNUNET_MessageHeader *message)
567 {
568   char *service;
569   char *name;
570   uint16_t msize;
571   uint16_t size;
572   struct StatsEntry *pos;
573   struct ClientEntry *ce;
574   struct WatchEntry *we;
575   size_t slen;
576
577   ce = make_client_entry (client);
578   msize = ntohs (message->size);
579   if (msize < sizeof (struct GNUNET_MessageHeader))
580     {
581       GNUNET_break (0);
582       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
583       return;
584     }
585   size = msize - sizeof (struct GNUNET_MessageHeader);
586   if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
587                                               size, 2, &service, &name))
588     {
589       GNUNET_break (0);
590       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
591       return;
592     }
593 #if DEBUG_STATISTICS
594   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
595               "Received request to watch statistic on `%s:%s'\n",
596               service, name);
597 #endif
598   pos = start;
599   while (pos != NULL)
600     {
601       if (matches (pos, service, name))
602         break;
603       pos = pos->next;
604     }
605   if (pos == NULL)
606     {
607       pos = GNUNET_malloc (sizeof (struct StatsEntry) + 
608                            sizeof (struct GNUNET_STATISTICS_SetMessage) + 
609                            size);
610       pos->next = start;
611       pos->uid = uidgen++;
612       pos->msg = (void *) &pos[1];
613       pos->msg->header.size = htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + 
614                                      size);
615       pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
616       pos->service = (const char *) &pos->msg[1];
617       slen = strlen (service) + 1;
618       memcpy ((void*) pos->service, service, slen);
619       pos->name = &pos->service[slen];
620       memcpy ((void*) pos->name, name, strlen (name)+1);
621       start = pos;
622     }
623   we = GNUNET_malloc (sizeof (struct WatchEntry));
624   we->client = client;
625   GNUNET_SERVER_client_keep (client);
626   we->wid = ce->max_wid++;
627   GNUNET_CONTAINER_DLL_insert (pos->we_head,
628                                pos->we_tail,
629                                we);
630   if (pos->value != 0)
631     notify_change (pos);
632   GNUNET_SERVER_receive_done (client,
633                               GNUNET_OK);
634 }
635
636
637 /**
638  * Task run during shutdown.
639  *
640  * @param cls unused
641  * @param tc unused
642  */
643 static void
644 shutdown_task (void *cls,
645                const struct GNUNET_SCHEDULER_TaskContext *tc)
646 {
647   struct ClientEntry *ce;
648   struct WatchEntry *we;
649   struct StatsEntry *se;
650
651   save ();
652   GNUNET_SERVER_notification_context_destroy (nc);
653   nc = NULL;
654   while (NULL != (ce = client_head))
655     {
656       GNUNET_SERVER_client_drop (ce->client);
657       GNUNET_CONTAINER_DLL_remove (client_head,
658                                    client_tail,
659                                    ce);
660       GNUNET_free (ce);
661     }
662   while (NULL != (se = start))
663     {
664       start = se->next;
665       while (NULL != (we = se->we_head))
666         {
667           GNUNET_SERVER_client_drop (we->client);
668           GNUNET_CONTAINER_DLL_remove (se->we_head,
669                                        se->we_tail,
670                                        we);
671           GNUNET_free (we);
672         }
673       GNUNET_free (se);
674     }
675 }
676
677
678 /**
679  * A client disconnected.  Remove all of its data structure entries.
680  *
681  * @param cls closure, NULL
682  * @param client identification of the client
683  */
684 static void
685 handle_client_disconnect (void *cls,
686                           struct GNUNET_SERVER_Client
687                           * client)
688 {
689   struct ClientEntry *ce;
690   struct WatchEntry *we;
691   struct WatchEntry *wen;
692   struct StatsEntry *se;
693   
694   ce = client_head;
695   while (NULL != ce)
696     {
697       if (ce->client == client)
698         {
699           GNUNET_SERVER_client_drop (ce->client);
700           GNUNET_CONTAINER_DLL_remove (client_head,
701                                        client_tail,
702                                        ce);
703           GNUNET_free (ce);
704           break;
705         }
706       ce = ce->next;
707     }
708   se = start;
709   while (NULL != se)
710     {
711       wen = se->we_head;
712       while (NULL != (we = wen))
713         {
714           wen = we->next;
715           if (we->client != client)
716             continue;
717           GNUNET_SERVER_client_drop (we->client);
718           GNUNET_CONTAINER_DLL_remove (se->we_head,
719                                        se->we_tail,
720                                        we);
721           GNUNET_free (we);
722         }
723       se = se->next;
724     }
725 }
726
727
728 /**
729  * Process statistics requests.
730  *
731  * @param cls closure
732  * @param sched scheduler to use
733  * @param server the initialized server
734  * @param c configuration to use
735  */
736 static void
737 run (void *cls,
738      struct GNUNET_SCHEDULER_Handle *sched,
739      struct GNUNET_SERVER_Handle *server,
740      const struct GNUNET_CONFIGURATION_Handle *c)
741 {
742   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
743     {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
744     {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
745     {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
746     {NULL, NULL, 0, 0}
747   };
748   cfg = c;
749   GNUNET_SERVER_add_handlers (server, handlers);
750   nc = GNUNET_SERVER_notification_context_create (server, 16);
751   GNUNET_SERVER_disconnect_notify (server, 
752                                    &handle_client_disconnect,
753                                    NULL);
754   load (server);
755   GNUNET_SCHEDULER_add_delayed (sched,
756                                 GNUNET_TIME_UNIT_FOREVER_REL,
757                                 &shutdown_task,
758                                 NULL);
759 }
760
761
762 /**
763  * The main function for the statistics service.
764  *
765  * @param argc number of arguments from the command line
766  * @param argv command line arguments
767  * @return 0 ok, 1 on error
768  */
769 int
770 main (int argc, char *const *argv)
771 {
772   return (GNUNET_OK ==
773           GNUNET_SERVICE_run (argc,
774                               argv,
775                               "statistics",
776                               GNUNET_SERVICE_OPTION_NONE,
777                               &run, NULL)) ? 0 : 1;
778 }
779
780 /* end of gnunet-service-statistics.c */