[PATCH 4/7] cr_tests: epoll: Avoid hitting rlimit on num file descriptors

Matt Helsley matthltc at us.ibm.com
Wed Oct 14 21:32:15 PDT 2009


Subtract the number of open file descriptors from the maximum number of
files the process can have open. Otherwise sk10k always fails with the
default number of sockets since we've got the log fd open.

Also:
	Cleanup option parsing
	Handle socketpair errors

Signed-off-by: Matt Helsley <matthltc at us.ibm.com>
---
 epoll/sk10k.c |   25 ++++++++++++++++++++++---
 1 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/epoll/sk10k.c b/epoll/sk10k.c
index 26647a3..a79d91e 100644
--- a/epoll/sk10k.c
+++ b/epoll/sk10k.c
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <limits.h>
 #include <getopt.h>
+#include <dirent.h> /* scandir() */
 
 /* open() */
 #include <sys/types.h>
@@ -50,6 +51,7 @@ void usage(FILE *pout)
 "\t-l\tWait for checkpoint at LABEL.\n"
 "\t-N\tPrint the maximum label number and exit.\n"
 "\t-n\tWait for checkpoint at NUM.\n"
+"\t-s\tNUM socket pairs to create. [Default: up to half of ulimit -n]\n"
 "\n"
 "You may only specify one LABEL or NUM and you may not specify both.\n"
 "Label numbers are integers in the range 0-%d\n"
@@ -68,11 +70,13 @@ const struct option long_options[] = {
 	{0, 0, 0, 0},
 };
 
-int num_sk = 1000;
+int num_sk = 400;
 
 void set_default_num_sk(void)
 {
 	struct rlimit lim;
+	int num_fds_open = 0;
+	struct dirent **dents;
 
 	/*
 	 * Get num_sk from hard rlimit. The goal of this default is to open
@@ -80,7 +84,17 @@ void set_default_num_sk(void)
 	 * checkpointed epoll items at one time.
 	 */
 	getrlimit(RLIMIT_NOFILE, &lim);
-	num_sk = lim.rlim_cur & ~1; /* round down to nearest multiple of 2 */
+	num_sk = lim.rlim_cur/2;
+
+	num_fds_open = scandir("/proc/self/fd", &dents, 0, alphasort);
+	if (num_fds_open < 0)
+		perror("scandir");
+	else {
+		free(dents);
+		num_fds_open -= 2;
+		num_sk -= (num_fds_open + 1)/2;
+	}
+	num_sk &= ~1; /* round down to nearest multiple of 2 */
 
 	/*
 	 * Of course if we're running as root then we may have an
@@ -90,6 +104,7 @@ void set_default_num_sk(void)
 	if (num_sk > 1000000)
 		num_sk = 1000000;
 }
+
 char *freezer = "1";
 
 void parse_args(int argc, char **argv)
@@ -209,6 +224,10 @@ label(create,
 label(open, ret, ret + 0);
 	for (i = 0; i < num_sk; i+=2) {
 		ret = socketpair(AF_UNIX, SOCK_SEQPACKET, 0, &sk[i]);
+		if (ret) {
+			log_error("socketpair");
+			goto out;
+		}
 		evs[i].data.fd = sk[i];
 		evs[i].events = EPOLLOUT;
 		evs[i + 1].data.fd = sk[i + 1];
@@ -299,7 +318,7 @@ out:
 	}
 	if (sk) {
 		for (i = 0; i < num_sk; i++) {
-			if (sk[i] >= 0) {
+			if (sk[i] > 0) {
 				close(sk[i]);
 				sk[i] = -1;
 			}
-- 
1.6.3.3




More information about the Containers mailing list