pinba plugin: Use mutexes rather than R/W-locks.
[collectd.git] / src / pinba.c
index b0aa439..f5eb69d 100644 (file)
@@ -21,8 +21,6 @@
  *   Phoenix Kayo <kayo.k11.4 at gmail.com>
  **/
 
-#define _XOPEN_SOURCE 500
-
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
@@ -30,8 +28,8 @@
 
 #include <pthread.h>
 #include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#include <netdb.h>
+#include <poll.h>
 
 #include "pinba.pb-c.h"
 
@@ -54,6 +52,14 @@ typedef uint8_t u_char;
 # define PINBA_DEFAULT_PORT 12345 /* FIXME */
 #endif
 
+#ifndef PINBA_MAX_SOCKETS
+# define PINBA_MAX_SOCKETS 16
+#endif
+
+#ifndef NI_MAXSERV
+# define NI_MAXSERV 32
+#endif
+
 /*
  * Private data structures
  */
@@ -68,36 +74,29 @@ struct _pinba_statres_ {
   double mem_peak;
 };
 
-typedef struct _pinba_socket_ pinba_socket;
-struct _pinba_socket_ {
+struct pinba_socket_s {
   int listen_sock;
   struct event *accept_event;
+
+  struct pollfd fd[PINBA_MAX_SOCKETS];
+  nfds_t fd_num;
 };
+typedef struct pinba_socket_s pinba_socket_t;
 
 typedef double pinba_time_t;
 typedef uint32_t pinba_size_t;
 
-static pinba_time_t now (void)
-{
-  static struct timeval tv;
-  
-  gettimeofday (&tv, /* tz = */ NULL);
-  
-  return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
-}
-
-static pthread_rwlock_t temp_lock;
-
 static struct event_base *temp_base = NULL;
 
 static pinba_socket_t *temp_sock = NULL;
 
 static pthread_t temp_thrd;
 
-typedef struct _pinba_statnode_ pinba_statnode;
-struct _pinba_statnode_{
+typedef struct pinba_statnode_s pinba_statnode_t;
+struct pinba_statnode_s
+{
   /* collector name */
-  charname;
+  char *name;
   /* query data */
   char *host;
   char *server;
@@ -112,15 +111,24 @@ struct _pinba_statnode_{
   pinba_size_t mem_peak;
 };
 
-static unsigned int stat_nodes_count=0;
-
-static pinba_statnode *stat_nodes = NULL;
+static pinba_statnode_t *stat_nodes = NULL;
+static unsigned int stat_nodes_num = 0;
+static pthread_mutex_t stat_nodes_lock;
 
 char service_status=0;
 char *service_address = PINBA_DEFAULT_ADDRESS;
 unsigned int service_port=PINBA_DEFAULT_PORT;
 
-static void service_statnode_reset (pinba_statnode *node) /* {{{ */
+static pinba_time_t now (void) /* {{{ */
+{
+  static struct timeval tv;
+  
+  gettimeofday (&tv, /* tz = */ NULL);
+  
+  return (double)tv.tv_sec+((double)tv.tv_usec/(double)1000000);
+} /* }}} */
+
+static void service_statnode_reset (pinba_statnode_t *node) /* {{{ */
 {
   node->last_coll=now();
   node->req_count=0;
@@ -151,16 +159,16 @@ static void service_statnode_add(const char *name, /* {{{ */
     const char *server,
     const char *script)
 {
-  pinba_statnode *node;
+  pinba_statnode_t *node;
   DEBUG("adding node `%s' to collector { %s, %s, %s }", name, host?host:"", server?server:"", script?script:"");
   
-  stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode)*(stat_nodes_count+1));
+  stat_nodes=realloc(stat_nodes, sizeof(pinba_statnode_t)*(stat_nodes_num+1));
   if(!stat_nodes){
     ERROR("Realloc failed!");
     exit(-1);
   }
   
-  node=&stat_nodes[stat_nodes_count];
+  node=&stat_nodes[stat_nodes_num];
   
   /* reset stat data */
   service_statnode_reset(node);
@@ -178,17 +186,17 @@ static void service_statnode_add(const char *name, /* {{{ */
   strset(&node->script, script);
   
   /* increment counter */
-  stat_nodes_count++;
+  stat_nodes_num++;
 } /* }}} void service_statnode_add */
 
 static void service_statnode_free (void)
 {
   unsigned int i;
 
-  if(stat_nodes_count < 1)
+  if(stat_nodes_num < 1)
     return;
 
-  for (i = 0; i < stat_nodes_count; i++)
+  for (i = 0; i < stat_nodes_num; i++)
   {
     sfree (stat_nodes[i].name);
     sfree (stat_nodes[i].host);
@@ -197,9 +205,9 @@ static void service_statnode_free (void)
   }
 
   sfree (stat_nodes);
-  stat_nodes_count = 0;
+  stat_nodes_num = 0;
 
-  pthread_rwlock_destroy (&temp_lock);
+  pthread_mutex_destroy (&stat_nodes_lock);
 }
 
 static void service_statnode_init (void)
@@ -208,39 +216,39 @@ static void service_statnode_init (void)
   service_statnode_free();
   
   DEBUG("initializing collector..");
-  pthread_rwlock_init(&temp_lock, 0);
+  pthread_mutex_init(&stat_nodes_lock, 0);
 }
 
 static void service_statnode_begin (void)
 {
   service_statnode_init();
-  pthread_rwlock_wrlock(&temp_lock);
+  pthread_mutex_lock(&stat_nodes_lock);
   
   service_statnode_add("total", NULL, NULL, NULL);
 }
 
 static void service_statnode_end (void)
 {
-  pthread_rwlock_unlock(&temp_lock);
+  pthread_mutex_unlock(&stat_nodes_lock);
 }
 
 static unsigned int service_statnode_collect (pinba_statres *res, /* {{{ */
     unsigned int index)
 {
-  pinba_statnode* node;
+  pinba_statnode_t* node;
   pinba_time_t delta;
   
-  if (stat_nodes_count == 0)
+  if (stat_nodes_num == 0)
     return 0;
   
   /* begin collecting */
   if (index == 0)
-    pthread_rwlock_wrlock (&temp_lock);
+    pthread_mutex_lock (&stat_nodes_lock);
   
   /* end collecting */
-  if (index >= stat_nodes_count)
+  if (index >= stat_nodes_num)
   {
-    pthread_rwlock_unlock (&temp_lock);
+    pthread_mutex_unlock (&stat_nodes_lock);
     return 0;
   }
   
@@ -264,7 +272,7 @@ static unsigned int service_statnode_collect (pinba_statres *res, /* {{{ */
   return (index + 1);
 } /* }}} unsigned int service_statnode_collect */
 
-static void service_statnode_process (pinba_statnode *node,
+static void service_statnode_process (pinba_statnode_t *node,
     Pinba__Request* request)
 {
   node->req_count++;
@@ -279,9 +287,9 @@ static void service_process_request (Pinba__Request *request)
 {
   unsigned int i;
 
-  pthread_rwlock_wrlock (&temp_lock);
+  pthread_mutex_lock (&stat_nodes_lock);
   
-  for (i = 0; i < stat_nodes_count; i++)
+  for (i = 0; i < stat_nodes_num; i++)
   {
     if(stat_nodes[i].host && strcmp(request->hostname, stat_nodes[i].host))
       continue;
@@ -293,7 +301,7 @@ static void service_process_request (Pinba__Request *request)
     service_statnode_process(&stat_nodes[i], request);
   }
   
-  pthread_rwlock_unlock(&temp_lock);
+  pthread_mutex_unlock(&stat_nodes_lock);
 }
 
 static void *pinba_main (void *arg)
@@ -396,77 +404,122 @@ static void pinba_udp_read_callback_fn (int sock, short event, void *arg) /* {{{
   } /* while (42) */
 } /* }}} void pinba_udp_read_callback_fn */
 
-static pinba_socket_t *pinba_socket_open (const char *ip, /* {{{ */
-    int listen_port)
+static int pb_add_socket (pinba_socket_t *s, /* {{{ */
+    const struct addrinfo *ai)
 {
-  struct sockaddr_in addr;
-  pinba_socket_t *s;
-  int sfd, flags, yes = 1;
-  
-  if ((sfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
-    ERROR("socket() failed: %s (%d)", strerror(errno), errno);
-    return NULL;
+  int fd;
+  int tmp;
+  int status;
+
+  if (s->fd_num == PINBA_MAX_SOCKETS)
+  {
+    WARNING ("pinba plugin: Sorry, you have hit the built-in limit of "
+        "%i sockets. Please complain to the collectd developers so we can "
+        "raise the limit.", PINBA_MAX_SOCKETS);
+    return (-1);
   }
-  
-  if ((flags = fcntl(sfd, F_GETFL, 0)) < 0 || fcntl(sfd, F_SETFL, flags | O_NONBLOCK) < 0) {
-    close(sfd);
-    return NULL;
+
+  fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+  if (fd < 0)
+  {
+    char errbuf[1024];
+    ERROR ("pinba plugin: socket(2) failed: %s",
+        sstrerror (errno, errbuf, sizeof (errbuf)));
+    return (0);
   }
-  
-  if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
-    close(sfd);
-    return NULL;
+
+  tmp = 1;
+  status = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof (tmp));
+  if (status != 0)
+  {
+    char errbuf[1024];
+    WARNING ("pinba plugin: setsockopt(SO_REUSEADDR) failed: %s",
+        sstrerror (errno, errbuf, sizeof (errbuf)));
   }
-  
-  s = (pinba_socket_t *)calloc(1, sizeof(pinba_socket_t));
-  if (!s) {
-    return NULL;
+
+  status = bind (fd, ai->ai_addr, ai->ai_addrlen);
+  if (status != 0)
+  {
+    char errbuf[1024];
+    ERROR ("pinba plugin: bind(2) failed: %s",
+        sstrerror (errno, errbuf, sizeof (errbuf)));
+    return (0);
   }
-  s->listen_sock = sfd;
-  
-  memset(&addr, 0, sizeof(addr));
-  
-  addr.sin_family = AF_INET;
-  addr.sin_port = htons(listen_port);
-  addr.sin_addr.s_addr = htonl(INADDR_ANY);
-  
-  if (ip && *ip) {
-    struct in_addr tmp;
-    
-    if (inet_aton(ip, &tmp)) {
-      addr.sin_addr.s_addr = tmp.s_addr;
-    } else {
-      WARNING("inet_aton(%s) failed, listening on ANY IP-address", ip);
-    }
+
+  s->fd[s->fd_num].fd = fd;
+  s->fd[s->fd_num].events = POLLIN | POLLPRI;
+  s->fd[s->fd_num].revents = 0;
+  s->fd_num++;
+
+  return (0);
+} /* }}} int pb_add_socket */
+
+static pinba_socket_t *pinba_socket_open (const char *node, /* {{{ */
+    int listen_port)
+{
+  pinba_socket_t *s;
+  struct addrinfo *ai_list;
+  struct addrinfo *ai_ptr;
+  struct addrinfo  ai_hints;
+  int status;
+
+  char service[NI_MAXSERV]; /* FIXME */
+  snprintf (service, sizeof (service), "%i", listen_port);
+
+  memset (&ai_hints, 0, sizeof (ai_hints));
+  ai_hints.ai_flags = AI_PASSIVE;
+  ai_hints.ai_family = AF_UNSPEC;
+  ai_hints.ai_socktype = SOCK_DGRAM;
+  ai_hints.ai_addr = NULL;
+  ai_hints.ai_canonname = NULL;
+  ai_hints.ai_next = NULL;
+
+  ai_list = NULL;
+  status = getaddrinfo (node, service,
+      &ai_hints, &ai_list);
+  if (status != 0)
+  {
+    ERROR ("pinba plugin: getaddrinfo(3) failed: %s",
+        gai_strerror (status));
+    return (NULL);
   }
-  
-  if (bind(s->listen_sock, (struct sockaddr *)&addr, sizeof(addr))) {
-    pinba_socket_free(s);
-    ERROR("bind() failed: %s (%d)", strerror(errno), errno);
-    return NULL;
+  assert (ai_list != NULL);
+
+  s = malloc (sizeof (*s));
+  if (s != NULL)
+  {
+    freeaddrinfo (ai_list);
+    ERROR ("pinba plugin: malloc failed.");
+    return (NULL);
   }
+  memset (s, 0, sizeof (*s));
+
+  for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
+  {
+    status = pb_add_socket (s, ai_ptr);
+    if (status != 0)
+      break;
+  } /* for (ai_list) */
   
-  s->accept_event = (struct event *)calloc(1, sizeof(struct event));
-  if (!s->accept_event) {
-    ERROR("calloc() failed: %s (%d)", strerror(errno), errno);
-    pinba_socket_free(s);
-    return NULL;
+  freeaddrinfo (ai_list);
+
+  if (s->fd_num < 1)
+  {
+    WARNING ("pinba plugin: Unable to open socket for address %s.", node);
+    sfree (s);
+    s = NULL;
   }
-  
-  event_set(s->accept_event, s->listen_sock, EV_READ | EV_PERSIST, pinba_udp_read_callback_fn, s);
-  event_base_set(temp_base, s->accept_event);
-  event_add(s->accept_event, NULL);
-  
-  return s;
+
+  return (s);
 } /* }}} pinba_socket_open */
 
 static int service_cleanup (void)
 {
   DEBUG("closing socket..");
   if(temp_sock){
-    pthread_rwlock_wrlock(&temp_lock);
+    pthread_mutex_lock(&stat_nodes_lock);
     pinba_socket_free(temp_sock);
-    pthread_rwlock_unlock(&temp_lock);
+    pthread_mutex_unlock(&stat_nodes_lock);
   }
   
   DEBUG("shutdowning event..");
@@ -623,6 +676,7 @@ static int plugin_config (oconfig_item_t *ci)
   service_statnode_end();
   
   service_config(pinba_address, pinba_port);
+  return (0);
 } /* int pinba_config */
 
 static int plugin_init (void)