about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-01-31 04:20:35 +0000
committerEric Wong <normalperson@yhbt.net>2013-01-31 04:35:39 +0000
commitdffe6b3dc226cafb0a6107443f9d7e23095dd789 (patch)
tree7f19987f07feda681f7c1785b99a46ab118ce588
parentd68b0ad231192c6ccf701e66be66b6bc956bed2b (diff)
downloadcmogstored-dffe6b3dc226cafb0a6107443f9d7e23095dd789.tar.gz
We do not need all the weight of sockaddr_storage or NI_MAXHOST.
cmogstored currently only supports IPv4 and IPv6[1] and (like
any respectable server) will not perform reverse DNS lookups.

This allows us to reduce our stack usage in some places and
keep caches hotter.

[1] MogileFS does not support IPv6, yet, even
-rw-r--r--cmogstored.h17
-rw-r--r--http_put.c9
-rw-r--r--inherit.c16
3 files changed, 25 insertions, 17 deletions
diff --git a/cmogstored.h b/cmogstored.h
index 01b1233..df1e4f4 100644
--- a/cmogstored.h
+++ b/cmogstored.h
@@ -532,3 +532,20 @@ unsigned mog_process_reaped(pid_t);
 /* upgrade.c */
 void mog_upgrade_prepare(int argc, char *argv[], char *envp[]);
 pid_t mog_upgrade_spawn(void);
+
+/*
+ * We only deal with ipv4 and ipv6 addresses (and no human-friendly
+ * hostnames/service names), so we can use smaller constants than the
+ * standard NI_MAXHOST/NI_MAXSERV values (1025 and 32 respectively).
+ * This reduces our per-thread stack usage and keeps caches hotter.
+ */
+#define MOG_NI_MAXHOST (INET6_ADDRSTRLEN)
+#define MOG_NI_MAXSERV (sizeof(":65536"))
+
+/* avoid sockaddr_storage since that bigger than we need */
+union mog_sockaddr {
+        struct sockaddr_in in;
+        struct sockaddr_in6 in6;
+        struct sockaddr sa;
+        unsigned char bytes[1];
+};
diff --git a/http_put.c b/http_put.c
index 9dfdce6..15ff9f7 100644
--- a/http_put.c
+++ b/http_put.c
@@ -440,12 +440,9 @@ static unsigned last_data_recv(int fd)
 
 MOG_NOINLINE static void read_err_dbg(struct mog_fd *mfd, ssize_t r)
 {
-        union {
-                struct sockaddr_storage ss;
-                struct sockaddr sa;
-        } any;
-        char addrbuf[INET6_ADDRSTRLEN];
-        char portbuf[sizeof(":65536")];
+        union mog_sockaddr any;
+        char addrbuf[MOG_NI_MAXHOST];
+        char portbuf[MOG_NI_MAXSERV];
         const char *addr;
         static const char no_ip[] = "unconnected";
         const char *path = "(unknown)";
diff --git a/inherit.c b/inherit.c
index 607046c..588ead1 100644
--- a/inherit.c
+++ b/inherit.c
@@ -6,13 +6,7 @@
 static Hash_table *listeners; /* yes, we'll scale to 10K listen sockets, L10K! */
 
 struct listener {
-        union {
-                struct sockaddr_storage ss;
-                struct sockaddr_in in;
-                struct sockaddr_in6 in6;
-                struct sockaddr sa;
-                unsigned char bytes[sizeof(struct sockaddr_storage)];
-        } as;
+        union mog_sockaddr as;
         socklen_t len;
         int fd;
 };
@@ -23,7 +17,7 @@ static bool listener_cmp(const void *a, const void *b)
         const struct listener *lb = b;
 
         return (la->len == lb->len) &&
-               (memcmp(&la->as.ss, &lb->as.ss, lb->len) == 0);
+               (memcmp(&la->as.sa, &lb->as.sa, lb->len) == 0);
 }
 
 static size_t listener_hash(const void *x, size_t tablesize)
@@ -43,11 +37,11 @@ static void register_listen_fd(int fd)
         struct listener tmp;
         struct listener *ins;
         int flags = NI_NUMERICSERV | NI_NUMERICSERV;
-        char hbuf[NI_MAXHOST];
-        char sbuf[NI_MAXSERV];
+        char hbuf[MOG_NI_MAXHOST];
+        char sbuf[MOG_NI_MAXSERV];
         int rc;
 
-        tmp.len = (socklen_t)sizeof(tmp.as.ss);
+        tmp.len = (socklen_t)sizeof(tmp.as);
         if (getsockname(fd, &tmp.as.sa, &tmp.len) != 0)
                 die_errno("getsockname(fd=%d) failed", fd);