[sheepdog] [PATCH] sheep: check resource limit at startup

Liu Yuan namei.unix at gmail.com
Mon Jan 28 07:47:41 CET 2013


From: Liu Yuan <tailai.ly at taobao.com>

Sheep daemon is FD hungry and can't survive for EMFILE.
1024 is default for NOFILE on most distributions, which is very
dangerous to run Sheepdog cluster. Let's give a warning on this.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 sheep/sheep.c |   29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/sheep/sheep.c b/sheep/sheep.c
index f2c7bcd..5c5bcc5 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -26,6 +26,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <arpa/inet.h>
+#include <sys/resource.h>
 
 #include "sheep_priv.h"
 #include "trace/trace.h"
@@ -385,6 +386,33 @@ static int init_work_queues(void)
 	return 0;
 }
 
+#define SD_RLIM_NOFILE 65536
+
+static void check_host_env(void)
+{
+	struct rlimit r;
+
+	if (getrlimit(RLIMIT_NOFILE, &r) < 0)
+		sd_eprintf("failed to get nofile %m\n");
+	/*
+	 * Sheep daemon is FD hungry and can't survive for EMFILE.
+	 * 1024 is default for NOFILE on most distributions, which is very
+	 * dangerous to run Sheepdog cluster.
+	 */
+	else if (r.rlim_cur == 1024)
+		sd_eprintf("WARN: Allowed open files 1024 too small, "
+			   "suggested %u\n", SD_RLIM_NOFILE);
+	else if (r.rlim_cur < SD_RLIM_NOFILE)
+		sd_iprintf("Allowed open files %lu, suggested %u\n",
+			   r.rlim_cur, SD_RLIM_NOFILE);
+
+	if (getrlimit(RLIMIT_CORE, &r) < 0)
+		sd_eprintf("failed to get core %m\n");
+	else if (r.rlim_cur < RLIM_INFINITY)
+		sd_iprintf("Allowed core file size %lu, suggested unlimited\n",
+			   r.rlim_cur);
+}
+
 int main(int argc, char **argv)
 {
 	int ch, longindex, ret, port = SD_LISTEN_PORT, io_port = SD_LISTEN_PORT;
@@ -653,6 +681,7 @@ int main(int argc, char **argv)
 	}
 
 	free(dir);
+	check_host_env();
 	sd_printf(SDOG_NOTICE, "sheepdog daemon (version %s) started\n",
 		PACKAGE_VERSION);
 
-- 
1.7.9.5




More information about the sheepdog mailing list