Skip to contentSkip to conversation

tools/xenconsoled: Increase file descriptor limit

PATCH Superseded Andrew Cooper 2 participants 3 messages Feb 16, 2015 – Feb 17, 2015

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/xenconsoled: Increase file descriptor limit

XenServer's VM density testing uncovered a regression when moving from sysvinit to systemd where the file descriptor limit dropped from 4096 to 1024. (XenServer had previously inserted a ulimit statement into its initscripts.)

One solution is to use LimitNOFILE=4096 in xenconsoled.service to match the lost ulimit, but that is only a stopgap solution.

As Xenconsoled genuinely needs a large number of file descriptors if a large number of domains are running, and is well behaved with its descriptors, attempt to up the limit to the system maximum.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
---
 tools/console/daemon/main.c |   31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
tools/console/daemon/main.c +31 -0
index 92d2fc4..759c061 100644
@@ -26,6 +26,7 @@
 #include <string.h>
 #include <signal.h>
 #include <sys/types.h>
+#include <sys/resource.h>
 
 #include "xenctrl.h"
 
@@ -55,6 +56,34 @@ static void version(char *name)
 	printf("Xen Console Daemon 3.0\n");
 }
 
+/*
+ * Xenconsoled requires one file descriptor for each PV console, which can
+ * easily exceed the default of 1024 if many guests are running.  Try to set
+ * the fd limit to the system maximum, falling back to a default of 4096.
+ */
+static void increase_fd_limit(void)
+{
+	FILE *fs_nr_open;
+	struct rlimit lim;
+	long nr_open = 4096;
+	int rc;
+
+	rc = getrlimit(RLIMIT_NOFILE, &lim);
+	if (rc)
+		return;
+
+	fs_nr_open = fopen("/proc/sys/fs/nr_open", "r");
+	if (fs_nr_open) {
+		fscanf(fs_nr_open, "%ld", &nr_open);
+		fclose(fs_nr_open);
+	}
+
+	if ((nr_open > lim.rlim_cur) || (nr_open > lim.rlim_max)) {
+		lim.rlim_cur = lim.rlim_max = nr_open;
+		setrlimit(RLIMIT_NOFILE, &lim);
+	}
+}
+
 int main(int argc, char **argv)
 {
 	const char *sopts = "hVvit:o:";
@@ -154,6 +183,8 @@ int main(int argc, char **argv)
 	openlog("xenconsoled", syslog_option, LOG_DAEMON);
 	setlogmask(syslog_mask);
 
+	increase_fd_limit();
+
 	if (!is_interactive) {
 		daemonize(pidfile ? pidfile : "/var/run/xenconsoled.pid");
 	}
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 54 lines · Andrew Cooper

XenServer's VM density testing uncovered a regression when moving from sysvinit to systemd where the file descriptor limit dropped from 4096 to 1024. (XenServer had previously inserted a ulimit statement into its initscripts.)

One solution is to use LimitNOFILE=4096 in xenconsoled.service to match the lost ulimit, but that is only a stopgap solution.

As Xenconsoled genuinely needs a large number of file descriptors if a large number of domains are running, and is well behaved with its descriptors, attempt to up the limit to the system maximum.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
---
 tools/console/daemon/main.c |   31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
tools/console/daemon/main.c +19 -0
index 92d2fc4..759c061 100644
@@ -26,6 +26,7 @@
 #include <string.h>
 #include <signal.h>
 #include <sys/types.h>
+#include <sys/resource.h>
 
 #include "xenctrl.h"
 
@@ -55,6 +56,34 @@ static void version(char *name)
 	printf("Xen Console Daemon 3.0\n");
 }
 
+/*
+ * Xenconsoled requires one file descriptor for each PV console, which can
+ * easily exceed the default of 1024 if many guests are running.  Try to set
+ * the fd limit to the system maximum, falling back to a default of 4096.
+ */
+static void increase_fd_limit(void)
+{
+	FILE *fs_nr_open;
+	struct rlimit lim;
+	long nr_open = 4096;
+	int rc;
+
+	rc = getrlimit(RLIMIT_NOFILE, &lim);
+	if (rc)
+		return;
+
+	fs_nr_open = fopen("/proc/sys/fs/nr_open", "r");
+	if (fs_nr_open) {

tools/console/daemon/main.c static void version(char *name) · line 86

Quoted 10 lines
tools/console/daemon/main.c +10 -0
+		fscanf(fs_nr_open, "%ld", &nr_open);
+		fclose(fs_nr_open);
+	}
+
+	if ((nr_open > lim.rlim_cur) || (nr_open > lim.rlim_max)) {
+		lim.rlim_cur = lim.rlim_max = nr_open;
+		setrlimit(RLIMIT_NOFILE, &lim);
+	}
+}
+

I am not sure that Xenconsoled is required to have the CAP_SYS_RESOURCE capability. With out this, the "soft" (rlim_cur) will most like not be changed. Does it make sense to include:

    lim.rlim_cur = lim.rlim_max;
    rc = setrlimit(RLIMIT_NOFILE, &lim);
    if (rc)
        return;

before "fs_nr_open = fopen(..."?

-Don Slutz

Quoted 13 lines
tools/console/daemon/main.c +2 -0
 int main(int argc, char **argv)
 {
 	const char *sopts = "hVvit:o:";
@@ -154,6 +183,8 @@ int main(int argc, char **argv)
 	openlog("xenconsoled", syslog_option, LOG_DAEMON);
 	setlogmask(syslog_mask);
 
+	increase_fd_limit();
+
 	if (!is_interactive) {
 		daemonize(pidfile ? pidfile : "/var/run/xenconsoled.pid");
 	}
#2 lore raw
Quoted 69 lines · Don Slutz
Quoted 54 lines · Andrew Cooper

XenServer's VM density testing uncovered a regression when moving from sysvinit to systemd where the file descriptor limit dropped from 4096 to 1024. (XenServer had previously inserted a ulimit statement into its initscripts.)

One solution is to use LimitNOFILE=4096 in xenconsoled.service to match the lost ulimit, but that is only a stopgap solution.

As Xenconsoled genuinely needs a large number of file descriptors if a large number of domains are running, and is well behaved with its descriptors, attempt to up the limit to the system maximum.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
---
 tools/console/daemon/main.c |   31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
tools/console/daemon/main.c +19 -0
index 92d2fc4..759c061 100644
@@ -26,6 +26,7 @@
 #include <string.h>
 #include <signal.h>
 #include <sys/types.h>
+#include <sys/resource.h>
 
 #include "xenctrl.h"
 
@@ -55,6 +56,34 @@ static void version(char *name)
 	printf("Xen Console Daemon 3.0\n");
 }
 
+/*
+ * Xenconsoled requires one file descriptor for each PV console, which can
+ * easily exceed the default of 1024 if many guests are running.  Try to set
+ * the fd limit to the system maximum, falling back to a default of 4096.
+ */
+static void increase_fd_limit(void)
+{
+	FILE *fs_nr_open;
+	struct rlimit lim;
+	long nr_open = 4096;
+	int rc;
+
+	rc = getrlimit(RLIMIT_NOFILE, &lim);
+	if (rc)
+		return;
+
+	fs_nr_open = fopen("/proc/sys/fs/nr_open", "r");
+	if (fs_nr_open) {

tools/console/daemon/main.c static void version(char *name) · line 86

Quoted 10 lines
tools/console/daemon/main.c +10 -0
+		fscanf(fs_nr_open, "%ld", &nr_open);
+		fclose(fs_nr_open);
+	}
+
+	if ((nr_open > lim.rlim_cur) || (nr_open > lim.rlim_max)) {
+		lim.rlim_cur = lim.rlim_max = nr_open;
+		setrlimit(RLIMIT_NOFILE, &lim);
+	}
+}
+

I am not sure that Xenconsoled is required to have the CAP_SYS_RESOURCE capability. With out this, the "soft" (rlim_cur) will most like not be changed. Does it make sense to include:

Quoted 6 lines
    lim.rlim_cur = lim.rlim_max;
    rc = setrlimit(RLIMIT_NOFILE, &lim);
    if (rc)
        return;

before "fs_nr_open = fopen(..."?

It probably does make sense, although xenconsoled cannot realistically run without privilege because of privcmd and /dev/ptmx.

I shall respin.

~Andrew