src/rrdd.[ch]: Implemented flushing of dead values once in a while.
[rrdd.git] / src / rrdd.c
index 042b331..03b9bbc 100644 (file)
@@ -22,6 +22,8 @@
 #define RRDD_DEBUG 1
 
 #include "rrdd.h"
+#include <glib-2.0/glib.h>
+#include <rrd.h>
 
 #if RRDD_DEBUG
 # define RRDD_LOG(severity, ...) do { fprintf (stderr, __VA_ARGS__); fprintf (stderr, "\n"); } while (0)
@@ -69,7 +71,7 @@ static pthread_mutex_t connetion_threads_lock = PTHREAD_MUTEX_INITIALIZER;
 static int connetion_threads_num = 0;
 
 /* Cache stuff */
-static avl_tree_t     *cache_tree = NULL;
+static GTree          *cache_tree = NULL;
 static cache_item_t   *cache_queue_head = NULL;
 static cache_item_t   *cache_queue_tail = NULL;
 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
@@ -94,46 +96,114 @@ static void sig_term_handler (int signal) /* {{{ */
   do_shutdown++;
 } /* }}} void sig_term_handler */
 
-static int cache_tree_compare (const void *v0, const void *v1) /* {{{ */
+/*
+ * enqueue_cache_item:
+ * `cache_lock' must be acquired before calling this function!
+ */
+static int enqueue_cache_item (cache_item_t *ci) /* {{{ */
 {
-  cache_item_t *c0 = (cache_item_t *) v0;
-  cache_item_t *c1 = (cache_item_t *) v1;
+  RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
+      ci->file);
 
-  assert (c0->file != NULL);
-  assert (c1->file != NULL);
+  if (ci == NULL)
+    return (-1);
 
-  return (strcmp (c0->file, c1->file));
-} /* }}} int cache_tree_compare */
+  if ((ci->flags & CI_FLAGS_IN_QUEUE) != 0)
+    return (-1);
+
+  assert (ci->next == NULL);
+
+  if (cache_queue_tail == NULL)
+    cache_queue_head = ci;
+  else
+    cache_queue_tail->next = ci;
+  cache_queue_tail = ci;
 
-static void cache_tree_free (void *v) /* {{{ */
+  return (0);
+} /* }}} int enqueue_cache_item */
+
+/*
+ * tree_callback_flush:
+ * Called via `g_tree_foreach' in `queue_thread_main'. `cache_lock' is held
+ * while this is in progress.
+ */
+static gboolean tree_callback_flush (gpointer key, gpointer value, /* {{{ */
+    gpointer data)
 {
-  cache_item_t *c = (cache_item_t *) v;
+  cache_item_t *ci;
+  time_t now;
 
-  assert (c->values_num == 0);
-  assert ((c->flags & CI_FLAGS_IN_TREE) != 0);
-  assert ((c->flags & CI_FLAGS_IN_QUEUE) == 0);
+  ci = (cache_item_t *) value;
+  now = *((time_t *) data);
 
-  free (c->file);
-  c->file = NULL;
-  free (c);
-} /* }}} void cache_tree_free */
+  if (((now - ci->last_flush_time) >= config_write_interval)
+      && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
+    enqueue_cache_item (ci);
+
+  return (TRUE);
+} /* }}} gboolean tree_callback_flush */
 
 static void *queue_thread_main (void *args) /* {{{ */
 {
+  struct timeval now;
+  struct timespec next_flush;
+
+  gettimeofday (&now, NULL);
+  next_flush.tv_sec = now.tv_sec + config_flush_interval;
+  next_flush.tv_nsec = 1000 * now.tv_usec;
+
   pthread_mutex_lock (&cache_lock);
   while ((do_shutdown == 0) || (cache_queue_head != NULL))
   {
     cache_item_t *ci;
-
     char *file;
     char **values;
     int values_num;
     int status;
     int i;
 
+    /* First, check if it's time to do the cache flush. */
+    gettimeofday (&now, NULL);
+    if ((now.tv_sec > next_flush.tv_sec)
+        || ((now.tv_sec == next_flush.tv_sec)
+          && ((1000 * now.tv_usec) > next_flush.tv_nsec)))
+    {
+      time_t time_now;
+
+      /* Pass the current time as user data so that we don't need to call
+       * `time' for each node. */
+      time_now = time (NULL);
+
+      g_tree_foreach (cache_tree, tree_callback_flush, (gpointer) &time_now);
+
+      /* Determine the time of the next cache flush. */
+      while (next_flush.tv_sec < now.tv_sec)
+        next_flush.tv_sec += config_flush_interval;
+    }
+
+    /* Now, check if there's something to store away. If not, wait until
+     * something comes in or it's time to do the cache flush. */
     if (cache_queue_head == NULL)
-      pthread_cond_wait (&cache_cond, &cache_lock);
+    {
+      struct timespec timeout;
 
+      timeout.tv_sec = next_flush.tv_sec - now.tv_sec;
+      if (next_flush.tv_nsec < (1000 * now.tv_usec))
+      {
+        timeout.tv_sec--;
+        timeout.tv_nsec = 1000000000 + next_flush.tv_nsec
+          - (1000 * now.tv_usec);
+      }
+      else
+      {
+        timeout.tv_nsec = next_flush.tv_nsec - (1000 * now.tv_usec);
+      }
+
+      pthread_cond_timedwait (&cache_cond, &cache_lock, &timeout);
+    }
+
+    /* Check if a value has arrived. This may be NULL if we timed out or there
+     * was an interrupt such as a signal. */
     if (cache_queue_head == NULL)
       continue;
 
@@ -198,10 +268,7 @@ static int handle_request_update (int fd, /* {{{ */
 
   time_t now;
 
-  avl_node_t *node;
-  cache_item_t ci_temp;
   cache_item_t *ci;
-
   char answer[4096];
 
   now = time (NULL);
@@ -214,12 +281,10 @@ static int handle_request_update (int fd, /* {{{ */
   file = buffer_ptr;
   buffer_ptr += strlen (file) + 1;
 
-  ci_temp.file = file;
-
   pthread_mutex_lock (&cache_lock);
 
-  node = avl_search (cache_tree, (void *) &ci_temp);
-  if (node == NULL)
+  ci = g_tree_lookup (cache_tree, file);
+  if (ci == NULL)
   {
     ci = (cache_item_t *) malloc (sizeof (cache_item_t));
     if (ci == NULL)
@@ -244,22 +309,11 @@ static int handle_request_update (int fd, /* {{{ */
     ci->last_flush_time = now;
     ci->flags = CI_FLAGS_IN_TREE;
 
-    if (avl_insert (cache_tree, (void *) ci) == NULL)
-    {
-      pthread_mutex_unlock (&cache_lock);
-      RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed.");
-      free (ci->file);
-      free (ci);
-      return (-1);
-    }
+    g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
 
-    RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.",
+    RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
         ci->file);
   }
-  else /* if (ci != NULL) */
-  {
-    ci = (cache_item_t *) node->item;
-  }
   assert (ci != NULL);
 
   while (*buffer_ptr != 0)
@@ -292,17 +346,7 @@ static int handle_request_update (int fd, /* {{{ */
   if (((now - ci->last_flush_time) >= config_write_interval)
       && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
   {
-    RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
-        ci->file);
-
-    assert (ci->next == NULL);
-
-    if (cache_queue_tail == NULL)
-      cache_queue_head = ci;
-    else
-      cache_queue_tail->next = ci;
-    cache_queue_tail = ci;
-
+    enqueue_cache_item (ci);
     pthread_cond_signal (&cache_cond);
   }
 
@@ -546,7 +590,6 @@ static int open_listen_socket (const char *addr) /* {{{ */
   for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
   {
     int fd;
-    struct sockaddr_storage sa;
     listen_socket_t *temp;
 
     temp = (listen_socket_t *) realloc (listen_fds,
@@ -611,7 +654,6 @@ static int close_listen_sockets (void) /* {{{ */
 
 static void *listen_thread_main (void *args) /* {{{ */
 {
-  char buffer[4096];
   struct pollfd *pollfds;
   int pollfds_num;
   int status;
@@ -624,6 +666,9 @@ static void *listen_thread_main (void *args) /* {{{ */
     open_listen_socket (config_listen_address_list[i]);
   }
 
+  if (config_listen_address_list_len < 1)
+    open_listen_socket (RRDD_SOCK_PATH);
+
   if (listen_fds_num < 1)
   {
     RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
@@ -736,7 +781,9 @@ static void *listen_thread_main (void *args) /* {{{ */
 
 static int daemonize (void) /* {{{ */
 {
+#if !RRDD_DEBUG
   pid_t child;
+#endif
   int status;
 
 #if !RRDD_DEBUG
@@ -785,10 +832,10 @@ static int daemonize (void) /* {{{ */
 
   openlog ("rrdd", LOG_PID, LOG_DAEMON);
 
-  cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free);
+  cache_tree = g_tree_new ((GCompareFunc) strcmp);
   if (cache_tree == NULL)
   {
-    RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed.");
+    RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
     return (-1);
   }