uncrustify as demanded.
[oweals/gnunet.git] / src / statistics / gnunet-service-statistics.c
index 18905f458a93017e1e634f487a09860432efde30..7d84f421e1fd57c5524d11d48d5aed72b9badfeb 100644 (file)
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Affero General Public License for more details.
-    
+
      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
      SPDX-License-Identifier: AGPL3.0-or-later
-*/
+ */
 
 /**
  * @file statistics/gnunet-service-statistics.c
@@ -38,9 +38,7 @@
 /**
  * Watch entry.
  */
-struct WatchEntry
-{
-
+struct WatchEntry {
   /**
    * Watch entries are kept in a linked list.
    */
@@ -84,8 +82,7 @@ struct SubsystemEntry;
 /**
  * Entry in the statistics list.
  */
-struct StatsEntry
-{
+struct StatsEntry {
   /**
    * This is a linked list.
    */
@@ -144,8 +141,7 @@ struct StatsEntry
  * We keep the statistics organized by subsystem for faster
  * lookup during SET operations.
  */
-struct SubsystemEntry
-{
+struct SubsystemEntry {
   /**
    * Subsystems are kept in a DLL.
    */
@@ -177,8 +173,7 @@ struct SubsystemEntry
 /**
  * Client entry.
  */
-struct ClientEntry
-{
+struct ClientEntry {
   /**
    * Corresponding server handle.
    */
@@ -241,7 +236,7 @@ static int in_shutdown;
  * Write persistent statistics to disk.
  */
 static void
-save ()
+save()
 {
   struct SubsystemEntry *se;
   struct StatsEntry *pos;
@@ -253,74 +248,74 @@ save ()
   size_t slen;
   struct GNUNET_STATISTICS_SetMessage *msg;
 
-  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
-                                                            "STATISTICS",
-                                                            "DATABASE",
-                                                            &fn))
-  {
-    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
-                               "STATISTICS",
-                               "DATABASE");
-    return;
-  }
-  (void) GNUNET_DISK_directory_create_for_file (fn);
-  wh = GNUNET_BIO_write_open (fn);
+  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg,
+                                                           "STATISTICS",
+                                                           "DATABASE",
+                                                           &fn))
+    {
+      GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
+                                "STATISTICS",
+                                "DATABASE");
+      return;
+    }
+  (void)GNUNET_DISK_directory_create_for_file(fn);
+  wh = GNUNET_BIO_write_open(fn);
   total = 0;
   while (NULL != (se = sub_head))
-  {
-    GNUNET_CONTAINER_DLL_remove (sub_head, sub_tail, se);
-    slen = strlen (se->service) + 1;
-    while (NULL != (pos = se->stat_head))
     {
-      GNUNET_CONTAINER_DLL_remove (se->stat_head, se->stat_tail, pos);
-      if ((pos->persistent) && (NULL != wh))
-      {
-        nlen = strlen (pos->name) + 1;
-        size = sizeof (struct GNUNET_STATISTICS_SetMessage) + nlen + slen;
-        GNUNET_assert (size < UINT16_MAX);
-        msg = GNUNET_malloc (size);
-
-        msg->header.size = htons ((uint16_t) size);
-        msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
-        GNUNET_assert (nlen + slen ==
-                       GNUNET_STRINGS_buffer_fill ((char *) &msg[1],
-                                                   nlen + slen,
-                                                   2,
-                                                   se->service,
-                                                   pos->name));
-        msg->flags =
-          htonl (pos->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
-        msg->value = GNUNET_htonll (pos->value);
-        if (GNUNET_OK != GNUNET_BIO_write (wh, msg, size))
+      GNUNET_CONTAINER_DLL_remove(sub_head, sub_tail, se);
+      slen = strlen(se->service) + 1;
+      while (NULL != (pos = se->stat_head))
         {
-          GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
-          if (GNUNET_OK != GNUNET_BIO_write_close (wh))
-            GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
-          wh = NULL;
+          GNUNET_CONTAINER_DLL_remove(se->stat_head, se->stat_tail, pos);
+          if ((pos->persistent) && (NULL != wh))
+            {
+              nlen = strlen(pos->name) + 1;
+              size = sizeof(struct GNUNET_STATISTICS_SetMessage) + nlen + slen;
+              GNUNET_assert(size < UINT16_MAX);
+              msg = GNUNET_malloc(size);
+
+              msg->header.size = htons((uint16_t)size);
+              msg->header.type = htons(GNUNET_MESSAGE_TYPE_STATISTICS_SET);
+              GNUNET_assert(nlen + slen ==
+                            GNUNET_STRINGS_buffer_fill((char *)&msg[1],
+                                                       nlen + slen,
+                                                       2,
+                                                       se->service,
+                                                       pos->name));
+              msg->flags =
+                htonl(pos->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
+              msg->value = GNUNET_htonll(pos->value);
+              if (GNUNET_OK != GNUNET_BIO_write(wh, msg, size))
+                {
+                  GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "write", fn);
+                  if (GNUNET_OK != GNUNET_BIO_write_close(wh))
+                    GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "close", fn);
+                  wh = NULL;
+                }
+              else
+                {
+                  total += size;
+                }
+              GNUNET_free(msg);
+            }
+          GNUNET_free(pos);
         }
-        else
-        {
-          total += size;
-        }
-        GNUNET_free (msg);
-      }
-      GNUNET_free (pos);
+      GNUNET_free(se);
     }
-    GNUNET_free (se);
-  }
   if (NULL != wh)
-  {
-    if (GNUNET_OK != GNUNET_BIO_write_close (wh))
-      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
-    if (0 == total)
-      GNUNET_break (0 == unlink (fn));
-    else
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                  ("Wrote %llu bytes of statistics to `%s'\n"),
-                  total,
-                  fn);
-  }
-  GNUNET_free_non_null (fn);
+    {
+      if (GNUNET_OK != GNUNET_BIO_write_close(wh))
+        GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "close", fn);
+      if (0 == total)
+        GNUNET_break(0 == unlink(fn));
+      else
+        GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+                   _("Wrote %llu bytes of statistics to `%s'\n"),
+                   total,
+                   fn);
+    }
+  GNUNET_free_non_null(fn);
 }
 
 
@@ -331,31 +326,31 @@ save ()
  * @param e value to transmit
  */
 static void
-transmit (struct ClientEntry *ce, const struct StatsEntry *e)
+transmit(struct ClientEntry *ce, const struct StatsEntry *e)
 {
   struct GNUNET_MQ_Envelope *env;
   struct GNUNET_STATISTICS_ReplyMessage *m;
   size_t size;
 
-  size = strlen (e->subsystem->service) + 1 + strlen (e->name) + 1;
-  GNUNET_assert (size < GNUNET_MAX_MESSAGE_SIZE);
-  env = GNUNET_MQ_msg_extra (m, size, GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
-  m->uid = htonl (e->uid);
+  size = strlen(e->subsystem->service) + 1 + strlen(e->name) + 1;
+  GNUNET_assert(size < GNUNET_MAX_MESSAGE_SIZE);
+  env = GNUNET_MQ_msg_extra(m, size, GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
+  m->uid = htonl(e->uid);
   if (e->persistent)
-    m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
-  m->value = GNUNET_htonll (e->value);
-  GNUNET_assert (size == GNUNET_STRINGS_buffer_fill ((char *) &m[1],
-                                                     size,
-                                                     2,
-                                                     e->subsystem->service,
-                                                     e->name));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Transmitting value for `%s:%s' (%d): %llu\n",
-              e->subsystem->service,
-              e->name,
-              e->persistent,
-              (unsigned long long) e->value);
-  GNUNET_MQ_send (ce->mq, env);
+    m->uid |= htonl(GNUNET_STATISTICS_PERSIST_BIT);
+  m->value = GNUNET_htonll(e->value);
+  GNUNET_assert(size == GNUNET_STRINGS_buffer_fill((char *)&m[1],
+                                                   size,
+                                                   2,
+                                                   e->subsystem->service,
+                                                   e->name));
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+             "Transmitting value for `%s:%s' (%d): %llu\n",
+             e->subsystem->service,
+             e->name,
+             e->persistent,
+             (unsigned long long)e->value);
+  GNUNET_MQ_send(ce->mq, env);
 }
 
 
@@ -368,17 +363,17 @@ transmit (struct ClientEntry *ce, const struct StatsEntry *e)
  * @return @a c
  */
 static void *
-client_connect_cb (void *cls,
-                   struct GNUNET_SERVICE_Client *c,
-                   struct GNUNET_MQ_Handle *mq)
+client_connect_cb(void *cls,
+                  struct GNUNET_SERVICE_Client *c,
+                  struct GNUNET_MQ_Handle *mq)
 {
   struct ClientEntry *ce;
 
-  ce = GNUNET_new (struct ClientEntry);
+  ce = GNUNET_new(struct ClientEntry);
   ce->client = c;
   ce->mq = mq;
   client_count++;
-  GNUNET_notification_context_add (nc, mq);
+  GNUNET_notification_context_add(nc, mq);
   return ce;
 }
 
@@ -391,22 +386,22 @@ client_connect_cb (void *cls,
  * @return #GNUNET_OK if @a message is well-formed
  */
 static int
-check_get (void *cls, const struct GNUNET_MessageHeader *message)
+check_get(void *cls, const struct GNUNET_MessageHeader *message)
 {
   const char *service;
   const char *name;
   size_t size;
 
-  size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
-  if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
-                                              size,
-                                              2,
-                                              &service,
-                                              &name))
-  {
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
-  }
+  size = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
+  if (size != GNUNET_STRINGS_buffer_tokenize((const char *)&message[1],
+                                             size,
+                                             2,
+                                             &service,
+                                             &name))
+    {
+      GNUNET_break(0);
+      return GNUNET_SYSERR;
+    }
   return GNUNET_OK;
 }
 
@@ -418,7 +413,7 @@ check_get (void *cls, const struct GNUNET_MessageHeader *message)
  * @param message the actual message
  */
 static void
-handle_get (void *cls, const struct GNUNET_MessageHeader *message)
+handle_get(void *cls, const struct GNUNET_MessageHeader *message)
 {
   struct ClientEntry *ce = cls;
   struct GNUNET_MQ_Envelope *env;
@@ -431,33 +426,33 @@ handle_get (void *cls, const struct GNUNET_MessageHeader *message)
   struct StatsEntry *pos;
   size_t size;
 
-  size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
-  GNUNET_assert (size ==
-                 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
-                                                 size,
-                                                 2,
-                                                 &service,
-                                                 &name));
-  slen = strlen (service);
-  nlen = strlen (name);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received request for statistics on `%s:%s'\n",
-              slen ? service : "*",
-              nlen ? name : "*");
+  size = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
+  GNUNET_assert(size ==
+                GNUNET_STRINGS_buffer_tokenize((const char *)&message[1],
+                                               size,
+                                               2,
+                                               &service,
+                                               &name));
+  slen = strlen(service);
+  nlen = strlen(name);
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+             "Received request for statistics on `%s:%s'\n",
+             slen ? service : "*",
+             nlen ? name : "*");
   for (se = sub_head; NULL != se; se = se->next)
-  {
-    if (! ((0 == slen) || (0 == strcmp (service, se->service))))
-      continue;
-    for (pos = se->stat_head; NULL != pos; pos = pos->next)
     {
-      if (! ((0 == nlen) || (0 == strcmp (name, pos->name))))
+      if (!((0 == slen) || (0 == strcmp(service, se->service))))
         continue;
-      transmit (ce, pos);
+      for (pos = se->stat_head; NULL != pos; pos = pos->next)
+        {
+          if (!((0 == nlen) || (0 == strcmp(name, pos->name))))
+            continue;
+          transmit(ce, pos);
+        }
     }
-  }
-  env = GNUNET_MQ_msg (end, GNUNET_MESSAGE_TYPE_STATISTICS_END);
-  GNUNET_MQ_send (ce->mq, env);
-  GNUNET_SERVICE_client_continue (ce->client);
+  env = GNUNET_MQ_msg(end, GNUNET_MESSAGE_TYPE_STATISTICS_END);
+  GNUNET_MQ_send(ce->mq, env);
+  GNUNET_SERVICE_client_continue(ce->client);
 }
 
 
@@ -467,32 +462,32 @@ handle_get (void *cls, const struct GNUNET_MessageHeader *message)
  * @param se value that changed
  */
 static void
-notify_change (struct StatsEntry *se)
+notify_change(struct StatsEntry *se)
 {
   struct GNUNET_MQ_Envelope *env;
   struct GNUNET_STATISTICS_WatchValueMessage *wvm;
   struct WatchEntry *pos;
 
   for (pos = se->we_head; NULL != pos; pos = pos->next)
-  {
-    if (GNUNET_YES == pos->last_value_set)
-    {
-      if (pos->last_value == se->value)
-        continue;
-    }
-    else
     {
-      pos->last_value_set = GNUNET_YES;
+      if (GNUNET_YES == pos->last_value_set)
+        {
+          if (pos->last_value == se->value)
+            continue;
+        }
+      else
+        {
+          pos->last_value_set = GNUNET_YES;
+        }
+      env = GNUNET_MQ_msg(wvm, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
+      wvm->flags =
+        htonl(se->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
+      wvm->wid = htonl(pos->wid);
+      wvm->reserved = htonl(0);
+      wvm->value = GNUNET_htonll(se->value);
+      GNUNET_MQ_send(pos->ce->mq, env);
+      pos->last_value = se->value;
     }
-    env = GNUNET_MQ_msg (wvm, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
-    wvm->flags =
-      htonl (se->persistent ? GNUNET_STATISTICS_SETFLAG_PERSISTENT : 0);
-    wvm->wid = htonl (pos->wid);
-    wvm->reserved = htonl (0);
-    wvm->value = GNUNET_htonll (se->value);
-    GNUNET_MQ_send (pos->ce->mq, env);
-    pos->last_value = se->value;
-  }
 }
 
 
@@ -505,7 +500,7 @@ notify_change (struct StatsEntry *se)
  * @return subsystem entry, never NULL (subsystem entry is created if necessary)
  */
 static struct SubsystemEntry *
-find_subsystem_entry (struct ClientEntry *ce, const char *service)
+find_subsystem_entry(struct ClientEntry *ce, const char *service)
 {
   size_t slen;
   struct SubsystemEntry *se;
@@ -514,24 +509,24 @@ find_subsystem_entry (struct ClientEntry *ce, const char *service)
     se = ce->subsystem;
   else
     se = NULL;
-  if ((NULL == se) || (0 != strcmp (service, se->service)))
-  {
-    for (se = sub_head; NULL != se; se = se->next)
-      if (0 == strcmp (service, se->service))
-        break;
-    if (NULL != ce)
-      ce->subsystem = se;
-  }
+  if ((NULL == se) || (0 != strcmp(service, se->service)))
+    {
+      for (se = sub_head; NULL != se; se = se->next)
+        if (0 == strcmp(service, se->service))
+          break;
+      if (NULL != ce)
+        ce->subsystem = se;
+    }
   if (NULL != se)
     return se;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Allocating new subsystem entry `%s'\n",
-              service);
-  slen = strlen (service) + 1;
-  se = GNUNET_malloc (sizeof (struct SubsystemEntry) + slen);
-  GNUNET_memcpy (&se[1], service, slen);
-  se->service = (const char *) &se[1];
-  GNUNET_CONTAINER_DLL_insert (sub_head, sub_tail, se);
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+             "Allocating new subsystem entry `%s'\n",
+             service);
+  slen = strlen(service) + 1;
+  se = GNUNET_malloc(sizeof(struct SubsystemEntry) + slen);
+  GNUNET_memcpy(&se[1], service, slen);
+  se->service = (const char *)&se[1];
+  GNUNET_CONTAINER_DLL_insert(sub_head, sub_tail, se);
   if (NULL != ce)
     ce->subsystem = se;
   return se;
@@ -546,12 +541,12 @@ find_subsystem_entry (struct ClientEntry *ce, const char *service)
  * @return statistis entry, or NULL if not found
  */
 static struct StatsEntry *
-find_stat_entry (struct SubsystemEntry *se, const char *name)
+find_stat_entry(struct SubsystemEntry *se, const char *name)
 {
   struct StatsEntry *pos;
 
   for (pos = se->stat_head; NULL != pos; pos = pos->next)
-    if (0 == strcmp (name, pos->name))
+    if (0 == strcmp(name, pos->name))
       return pos;
   return NULL;
 }
@@ -565,22 +560,22 @@ find_stat_entry (struct SubsystemEntry *se, const char *name)
  * @return #GNUNET_OK if message is well-formed
  */
 static int
-check_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
+check_set(void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
 {
   const char *service;
   const char *name;
   size_t msize;
 
-  msize = ntohs (msg->header.size) - sizeof (*msg);
-  if (msize != GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
-                                               msize,
-                                               2,
-                                               &service,
-                                               &name))
-  {
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
-  }
+  msize = ntohs(msg->header.size) - sizeof(*msg);
+  if (msize != GNUNET_STRINGS_buffer_tokenize((const char *)&msg[1],
+                                              msize,
+                                              2,
+                                              &service,
+                                              &name))
+    {
+      GNUNET_break(0);
+      return GNUNET_SYSERR;
+    }
   return GNUNET_OK;
 }
 
@@ -592,7 +587,7 @@ check_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
  * @param message the actual message
  */
 static void
-handle_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
+handle_set(void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
 {
   struct ClientEntry *ce = cls;
   const char *service;
@@ -608,95 +603,95 @@ handle_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
   int changed;
   int initial_set;
 
-  msize = ntohs (msg->header.size);
-  size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
-  GNUNET_assert (size == GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1],
-                                                         size,
-                                                         2,
-                                                         &service,
-                                                         &name));
-  se = find_subsystem_entry (ce, service);
-  flags = ntohl (msg->flags);
-  value = GNUNET_ntohll (msg->value);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
-              service,
-              name,
-              (unsigned int) flags,
-              (unsigned long long) value);
-  pos = find_stat_entry (se, name);
+  msize = ntohs(msg->header.size);
+  size = msize - sizeof(struct GNUNET_STATISTICS_SetMessage);
+  GNUNET_assert(size == GNUNET_STRINGS_buffer_tokenize((const char *)&msg[1],
+                                                       size,
+                                                       2,
+                                                       &service,
+                                                       &name));
+  se = find_subsystem_entry(ce, service);
+  flags = ntohl(msg->flags);
+  value = GNUNET_ntohll(msg->value);
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+             "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
+             service,
+             name,
+             (unsigned int)flags,
+             (unsigned long long)value);
+  pos = find_stat_entry(se, name);
   if (NULL != pos)
-  {
-    initial_set = 0;
-    if (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE))
     {
-      changed = (pos->value != value);
-      pos->value = value;
-    }
-    else
-    {
-      delta = (int64_t) value;
-      if ((delta < 0) && (pos->value < -delta))
-      {
-        changed = (0 != pos->value);
-        pos->value = 0;
-      }
+      initial_set = 0;
+      if (0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE))
+        {
+          changed = (pos->value != value);
+          pos->value = value;
+        }
       else
-      {
-        changed = (0 != delta);
-        GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
-        pos->value += delta;
-      }
+        {
+          delta = (int64_t)value;
+          if ((delta < 0) && (pos->value < -delta))
+            {
+              changed = (0 != pos->value);
+              pos->value = 0;
+            }
+          else
+            {
+              changed = (0 != delta);
+              GNUNET_break((delta <= 0) || (pos->value + delta > pos->value));
+              pos->value += delta;
+            }
+        }
+      if (GNUNET_NO == pos->set)
+        {
+          pos->set = GNUNET_YES;
+          initial_set = 1;
+        }
+      pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
+      if (pos != se->stat_head)
+        {
+          /* move to front for faster setting next time! */
+          GNUNET_CONTAINER_DLL_remove(se->stat_head, se->stat_tail, pos);
+          GNUNET_CONTAINER_DLL_insert(se->stat_head, se->stat_tail, pos);
+        }
+      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+                 "Statistic `%s:%s' updated to value %llu (%d).\n",
+                 service,
+                 name,
+                 (unsigned long long)pos->value,
+                 pos->persistent);
+      if ((changed) || (1 == initial_set))
+        notify_change(pos);
+      GNUNET_SERVICE_client_continue(ce->client);
+      return;
     }
-    if (GNUNET_NO == pos->set)
+  /* not found, create a new entry */
+  nlen = strlen(name) + 1;
+  pos = GNUNET_malloc(sizeof(struct StatsEntry) + nlen);
+  GNUNET_memcpy(&pos[1], name, nlen);
+  pos->name = (const char *)&pos[1];
+  pos->subsystem = se;
+  if ((0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE)) ||
+      (0 < (int64_t)GNUNET_ntohll(msg->value)))
     {
+      pos->value = GNUNET_ntohll(msg->value);
       pos->set = GNUNET_YES;
-      initial_set = 1;
     }
-    pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
-    if (pos != se->stat_head)
+  else
     {
-      /* move to front for faster setting next time! */
-      GNUNET_CONTAINER_DLL_remove (se->stat_head, se->stat_tail, pos);
-      GNUNET_CONTAINER_DLL_insert (se->stat_head, se->stat_tail, pos);
+      pos->set = GNUNET_NO;
     }
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Statistic `%s:%s' updated to value %llu (%d).\n",
-                service,
-                name,
-                (unsigned long long) pos->value,
-                pos->persistent);
-    if ((changed) || (1 == initial_set))
-      notify_change (pos);
-    GNUNET_SERVICE_client_continue (ce->client);
-    return;
-  }
-  /* not found, create a new entry */
-  nlen = strlen (name) + 1;
-  pos = GNUNET_malloc (sizeof (struct StatsEntry) + nlen);
-  GNUNET_memcpy (&pos[1], name, nlen);
-  pos->name = (const char *) &pos[1];
-  pos->subsystem = se;
-  if ((0 == (flags & GNUNET_STATISTICS_SETFLAG_RELATIVE)) ||
-      (0 < (int64_t) GNUNET_ntohll (msg->value)))
-  {
-    pos->value = GNUNET_ntohll (msg->value);
-    pos->set = GNUNET_YES;
-  }
-  else
-  {
-    pos->set = GNUNET_NO;
-  }
   pos->uid = uidgen++;
   pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
-  GNUNET_CONTAINER_DLL_insert (se->stat_head, se->stat_tail, pos);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "New statistic on `%s:%s' with value %llu created.\n",
-              service,
-              name,
-              (unsigned long long) pos->value);
+  GNUNET_CONTAINER_DLL_insert(se->stat_head, se->stat_tail, pos);
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+             "New statistic on `%s:%s' with value %llu created.\n",
+             service,
+             name,
+             (unsigned long long)pos->value);
   if (NULL != ce)
-    GNUNET_SERVICE_client_continue (ce->client);
+    GNUNET_SERVICE_client_continue(ce->client);
 }
 
 
@@ -708,22 +703,22 @@ handle_set (void *cls, const struct GNUNET_STATISTICS_SetMessage *msg)
  * @return #GNUNET_OK if message is well-formed
  */
 static int
-check_watch (void *cls, const struct GNUNET_MessageHeader *message)
+check_watch(void *cls, const struct GNUNET_MessageHeader *message)
 {
   size_t size;
   const char *service;
   const char *name;
 
-  size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
-  if (size != GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
-                                              size,
-                                              2,
-                                              &service,
-                                              &name))
-  {
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
-  }
+  size = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader);
+  if (size != GNUNET_STRINGS_buffer_tokenize((const char *)&message[1],
+                                             size,
+                                             2,
+                                             &service,
+                                             &name))
+    {
+      GNUNET_break(0);
+      return GNUNET_SYSERR;
+    }
   return GNUNET_OK;
 }
 
@@ -735,7 +730,7 @@ check_watch (void *cls, const struct GNUNET_MessageHeader *message)
  * @param message the actual message
  */
 static void
-handle_watch (void *cls, const struct GNUNET_MessageHeader *message)
+handle_watch(void *cls, const struct GNUNET_MessageHeader *message)
 {
   struct ClientEntry *ce = cls;
   const char *service;
@@ -748,49 +743,49 @@ handle_watch (void *cls, const struct GNUNET_MessageHeader *message)
   size_t nlen;
 
   if (NULL == nc)
-  {
-    GNUNET_SERVICE_client_drop (ce->client);
-    return;
-  }
-  GNUNET_SERVICE_client_mark_monitor (ce->client);
-  msize = ntohs (message->size);
-  size = msize - sizeof (struct GNUNET_MessageHeader);
-  GNUNET_assert (size ==
-                 GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1],
-                                                 size,
-                                                 2,
-                                                 &service,
-                                                 &name));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received request to watch statistic on `%s:%s'\n",
-              service,
-              name);
-  se = find_subsystem_entry (ce, service);
-  pos = find_stat_entry (se, name);
+    {
+      GNUNET_SERVICE_client_drop(ce->client);
+      return;
+    }
+  GNUNET_SERVICE_client_mark_monitor(ce->client);
+  msize = ntohs(message->size);
+  size = msize - sizeof(struct GNUNET_MessageHeader);
+  GNUNET_assert(size ==
+                GNUNET_STRINGS_buffer_tokenize((const char *)&message[1],
+                                               size,
+                                               2,
+                                               &service,
+                                               &name));
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+             "Received request to watch statistic on `%s:%s'\n",
+             service,
+             name);
+  se = find_subsystem_entry(ce, service);
+  pos = find_stat_entry(se, name);
   if (NULL == pos)
-  {
-    nlen = strlen (name) + 1;
-    pos = GNUNET_malloc (sizeof (struct StatsEntry) + nlen);
-    GNUNET_memcpy (&pos[1], name, nlen);
-    pos->name = (const char *) &pos[1];
-    pos->subsystem = se;
-    GNUNET_CONTAINER_DLL_insert (se->stat_head, se->stat_tail, pos);
-    pos->uid = uidgen++;
-    pos->set = GNUNET_NO;
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "New statistic on `%s:%s' with value %llu created.\n",
-                service,
-                name,
-                (unsigned long long) pos->value);
-  }
-  we = GNUNET_new (struct WatchEntry);
+    {
+      nlen = strlen(name) + 1;
+      pos = GNUNET_malloc(sizeof(struct StatsEntry) + nlen);
+      GNUNET_memcpy(&pos[1], name, nlen);
+      pos->name = (const char *)&pos[1];
+      pos->subsystem = se;
+      GNUNET_CONTAINER_DLL_insert(se->stat_head, se->stat_tail, pos);
+      pos->uid = uidgen++;
+      pos->set = GNUNET_NO;
+      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+                 "New statistic on `%s:%s' with value %llu created.\n",
+                 service,
+                 name,
+                 (unsigned long long)pos->value);
+    }
+  we = GNUNET_new(struct WatchEntry);
   we->ce = ce;
   we->last_value_set = GNUNET_NO;
   we->wid = ce->max_wid++;
-  GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
+  GNUNET_CONTAINER_DLL_insert(pos->we_head, pos->we_tail, we);
   if (0 != pos->value)
-    notify_change (pos);
-  GNUNET_SERVICE_client_continue (ce->client);
+    notify_change(pos);
+  GNUNET_SERVICE_client_continue(ce->client);
 }
 
 
@@ -803,15 +798,15 @@ handle_watch (void *cls, const struct GNUNET_MessageHeader *message)
  * @param message the actual message
  */
 static void
-handle_disconnect (void *cls, const struct GNUNET_MessageHeader *message)
+handle_disconnect(void *cls, const struct GNUNET_MessageHeader *message)
 {
   struct ClientEntry *ce = cls;
   struct GNUNET_MQ_Envelope *env;
   struct GNUNET_MessageHeader *msg;
 
-  env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM);
-  GNUNET_MQ_send (ce->mq, env);
-  GNUNET_SERVICE_client_continue (ce->client);
+  env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT_CONFIRM);
+  GNUNET_MQ_send(ce->mq, env);
+  GNUNET_SERVICE_client_continue(ce->client);
 }
 
 
@@ -819,7 +814,7 @@ handle_disconnect (void *cls, const struct GNUNET_MessageHeader *message)
  * Actually perform the shutdown.
  */
 static void
-do_shutdown ()
+do_shutdown()
 {
   struct WatchEntry *we;
   struct StatsEntry *pos;
@@ -827,26 +822,26 @@ do_shutdown ()
 
   if (NULL == nc)
     return;
-  save ();
-  GNUNET_notification_context_destroy (nc);
+  save();
+  GNUNET_notification_context_destroy(nc);
   nc = NULL;
-  GNUNET_assert (0 == client_count);
+  GNUNET_assert(0 == client_count);
   while (NULL != (se = sub_head))
-  {
-    GNUNET_CONTAINER_DLL_remove (sub_head, sub_tail, se);
-    while (NULL != (pos = se->stat_head))
     {
-      GNUNET_CONTAINER_DLL_remove (se->stat_head, se->stat_tail, pos);
-      while (NULL != (we = pos->we_head))
-      {
-        GNUNET_break (0);
-        GNUNET_CONTAINER_DLL_remove (pos->we_head, pos->we_tail, we);
-        GNUNET_free (we);
-      }
-      GNUNET_free (pos);
+      GNUNET_CONTAINER_DLL_remove(sub_head, sub_tail, se);
+      while (NULL != (pos = se->stat_head))
+        {
+          GNUNET_CONTAINER_DLL_remove(se->stat_head, se->stat_tail, pos);
+          while (NULL != (we = pos->we_head))
+            {
+              GNUNET_break(0);
+              GNUNET_CONTAINER_DLL_remove(pos->we_head, pos->we_tail, we);
+              GNUNET_free(we);
+            }
+          GNUNET_free(pos);
+        }
+      GNUNET_free(se);
     }
-    GNUNET_free (se);
-  }
 }
 
 
@@ -856,12 +851,12 @@ do_shutdown ()
  * @param cls unused
  */
 static void
-shutdown_task (void *cls)
+shutdown_task(void *cls)
 {
   in_shutdown = GNUNET_YES;
   if (0 != client_count)
     return;
-  do_shutdown ();
+  do_shutdown();
 }
 
 
@@ -873,9 +868,9 @@ shutdown_task (void *cls)
  * @param app_cls the `struct ClientEntry *`
  */
 static void
-client_disconnect_cb (void *cls,
-                      struct GNUNET_SERVICE_Client *client,
-                      void *app_cls)
+client_disconnect_cb(void *cls,
+                     struct GNUNET_SERVICE_Client *client,
+                     void *app_cls)
 {
   struct ClientEntry *ce = app_cls;
   struct WatchEntry *we;
@@ -885,23 +880,23 @@ client_disconnect_cb (void *cls,
 
   client_count--;
   for (se = sub_head; NULL != se; se = se->next)
-  {
-    for (pos = se->stat_head; NULL != pos; pos = pos->next)
     {
-      wen = pos->we_head;
-      while (NULL != (we = wen))
-      {
-        wen = we->next;
-        if (we->ce != ce)
-          continue;
-        GNUNET_CONTAINER_DLL_remove (pos->we_head, pos->we_tail, we);
-        GNUNET_free (we);
-      }
+      for (pos = se->stat_head; NULL != pos; pos = pos->next)
+        {
+          wen = pos->we_head;
+          while (NULL != (we = wen))
+            {
+              wen = we->next;
+              if (we->ce != ce)
+                continue;
+              GNUNET_CONTAINER_DLL_remove(pos->we_head, pos->we_tail, we);
+              GNUNET_free(we);
+            }
+        }
     }
-  }
-  GNUNET_free (ce);
+  GNUNET_free(ce);
   if ((0 == client_count) && (GNUNET_YES == in_shutdown))
-    do_shutdown ();
+    do_shutdown();
 }
 
 
@@ -917,19 +912,19 @@ client_disconnect_cb (void *cls,
  *    #GNUNET_SYSERR to stop further processing with error
  */
 static int
-inject_message (void *cls, const struct GNUNET_MessageHeader *message)
+inject_message(void *cls, const struct GNUNET_MessageHeader *message)
 {
-  uint16_t msize = ntohs (message->size);
+  uint16_t msize = ntohs(message->size);
   const struct GNUNET_STATISTICS_SetMessage *sm;
 
-  sm = (const struct GNUNET_STATISTICS_SetMessage *) message;
-  if ((sizeof (struct GNUNET_STATISTICS_SetMessage) > msize) ||
-      (GNUNET_OK != check_set (NULL, sm)))
-  {
-    GNUNET_break (0);
-    return GNUNET_SYSERR;
-  }
-  handle_set (NULL, sm);
+  sm = (const struct GNUNET_STATISTICS_SetMessage *)message;
+  if ((sizeof(struct GNUNET_STATISTICS_SetMessage) > msize) ||
+      (GNUNET_OK != check_set(NULL, sm)))
+    {
+      GNUNET_break(0);
+      return GNUNET_SYSERR;
+    }
+  handle_set(NULL, sm);
   return GNUNET_OK;
 }
 
@@ -939,7 +934,7 @@ inject_message (void *cls, const struct GNUNET_MessageHeader *message)
  * format that we also use for setting the values over the network.
  */
 static void
-load ()
+load()
 {
   char *fn;
   struct GNUNET_BIO_ReadHandle *rh;
@@ -947,51 +942,51 @@ load ()
   char *buf;
   struct GNUNET_MessageStreamTokenizer *mst;
 
-  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
-                                                            "STATISTICS",
-                                                            "DATABASE",
-                                                            &fn))
-  {
-    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
-                               "STATISTICS",
-                               "DATABASE");
-    return;
-  }
+  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename(cfg,
+                                                           "STATISTICS",
+                                                           "DATABASE",
+                                                           &fn))
+    {
+      GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
+                                "STATISTICS",
+                                "DATABASE");
+      return;
+    }
   if ((GNUNET_OK !=
-       GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) ||
+       GNUNET_DISK_file_size(fn, &fsize, GNUNET_NO, GNUNET_YES)) ||
       (0 == fsize))
-  {
-    GNUNET_free (fn);
-    return;
-  }
-  buf = GNUNET_malloc (fsize);
-  rh = GNUNET_BIO_read_open (fn);
-  if (! rh)
-  {
-    GNUNET_free (buf);
-    GNUNET_free (fn);
-    return;
-  }
-  if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
-  {
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
-    GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
-    GNUNET_free (buf);
-    GNUNET_free (fn);
-    return;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              _ ("Loading %llu bytes of statistics from `%s'\n"),
-              (unsigned long long) fsize,
-              fn);
-  mst = GNUNET_MST_create (&inject_message, NULL);
-  GNUNET_break (
+    {
+      GNUNET_free(fn);
+      return;
+    }
+  buf = GNUNET_malloc(fsize);
+  rh = GNUNET_BIO_read_open(fn);
+  if (!rh)
+    {
+      GNUNET_free(buf);
+      GNUNET_free(fn);
+      return;
+    }
+  if (GNUNET_OK != GNUNET_BIO_read(rh, fn, buf, fsize))
+    {
+      GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING, "read", fn);
+      GNUNET_break(GNUNET_OK == GNUNET_BIO_read_close(rh, NULL));
+      GNUNET_free(buf);
+      GNUNET_free(fn);
+      return;
+    }
+  GNUNET_log(GNUNET_ERROR_TYPE_INFO,
+             _("Loading %llu bytes of statistics from `%s'\n"),
+             (unsigned long long)fsize,
+             fn);
+  mst = GNUNET_MST_create(&inject_message, NULL);
+  GNUNET_break(
     GNUNET_OK ==
-    GNUNET_MST_from_buffer (mst, buf, (size_t) fsize, GNUNET_YES, GNUNET_NO));
-  GNUNET_MST_destroy (mst);
-  GNUNET_free (buf);
-  GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, NULL));
-  GNUNET_free (fn);
+    GNUNET_MST_from_buffer(mst, buf, (size_t)fsize, GNUNET_YES, GNUNET_NO));
+  GNUNET_MST_destroy(mst);
+  GNUNET_free(buf);
+  GNUNET_break(GNUNET_OK == GNUNET_BIO_read_close(rh, NULL));
+  GNUNET_free(fn);
 }
 
 
@@ -1003,44 +998,44 @@ load ()
  * @param service the initialized service
  */
 static void
-run (void *cls,
-     const struct GNUNET_CONFIGURATION_Handle *c,
-     struct GNUNET_SERVICE_Handle *service)
+run(void *cls,
+    const struct GNUNET_CONFIGURATION_Handle *c,
+    struct GNUNET_SERVICE_Handle *service)
 {
   cfg = c;
-  nc = GNUNET_notification_context_create (16);
-  load ();
-  GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
+  nc = GNUNET_notification_context_create(16);
+  load();
+  GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL);
 }
 
 
 /**
  * Define "main" method using service macro.
  */
-GNUNET_SERVICE_MAIN (
+GNUNET_SERVICE_MAIN(
   "statistics",
   GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN,
   &run,
   &client_connect_cb,
   &client_disconnect_cb,
   NULL,
-  GNUNET_MQ_hd_var_size (set,
-                         GNUNET_MESSAGE_TYPE_STATISTICS_SET,
-                         struct GNUNET_STATISTICS_SetMessage,
-                         NULL),
-  GNUNET_MQ_hd_var_size (get,
-                         GNUNET_MESSAGE_TYPE_STATISTICS_GET,
-                         struct GNUNET_MessageHeader,
-                         NULL),
-  GNUNET_MQ_hd_var_size (watch,
-                         GNUNET_MESSAGE_TYPE_STATISTICS_WATCH,
-                         struct GNUNET_MessageHeader,
-                         NULL),
-  GNUNET_MQ_hd_fixed_size (disconnect,
-                           GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT,
-                           struct GNUNET_MessageHeader,
-                           NULL),
-  GNUNET_MQ_handler_end ());
+  GNUNET_MQ_hd_var_size(set,
+                        GNUNET_MESSAGE_TYPE_STATISTICS_SET,
+                        struct GNUNET_STATISTICS_SetMessage,
+                        NULL),
+  GNUNET_MQ_hd_var_size(get,
+                        GNUNET_MESSAGE_TYPE_STATISTICS_GET,
+                        struct GNUNET_MessageHeader,
+                        NULL),
+  GNUNET_MQ_hd_var_size(watch,
+                        GNUNET_MESSAGE_TYPE_STATISTICS_WATCH,
+                        struct GNUNET_MessageHeader,
+                        NULL),
+  GNUNET_MQ_hd_fixed_size(disconnect,
+                          GNUNET_MESSAGE_TYPE_STATISTICS_DISCONNECT,
+                          struct GNUNET_MessageHeader,
+                          NULL),
+  GNUNET_MQ_handler_end());
 
 
 #if defined(LINUX) && defined(__GLIBC__)
@@ -1049,11 +1044,11 @@ GNUNET_SERVICE_MAIN (
 /**
  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
  */
-void __attribute__ ((constructor)) GNUNET_STATISTICS_memory_init ()
+void __attribute__ ((constructor)) GNUNET_STATISTICS_memory_init()
 {
-  mallopt (M_TRIM_THRESHOLD, 4 * 1024);
-  mallopt (M_TOP_PAD, 1 * 1024);
-  malloc_trim (0);
+  mallopt(M_TRIM_THRESHOLD, 4 * 1024);
+  mallopt(M_TOP_PAD, 1 * 1024);
+  malloc_trim(0);
 }
 #endif