Browse Source

Increase fd limits as much as possible at startup

Luke Dashjr 12 years ago
parent
commit
c4105a63d1
3 changed files with 35 additions and 0 deletions
  1. 2 0
      configure.ac
  2. 5 0
      logging.h
  3. 28 0
      miner.c

+ 2 - 0
configure.ac

@@ -403,6 +403,8 @@ AC_SUBST(LIBUSB_CFLAGS)
 PKG_CHECK_MODULES([LIBCURL], [libcurl >= 7.18.2], ,[AC_MSG_ERROR([Missing required libcurl dev >= 7.18.2])])
 PKG_CHECK_MODULES([LIBCURL], [libcurl >= 7.18.2], ,[AC_MSG_ERROR([Missing required libcurl dev >= 7.18.2])])
 AC_SUBST(LIBCURL_LIBS)
 AC_SUBST(LIBCURL_LIBS)
 
 
+AC_CHECK_FUNCS([setrlimit])
+
 dnl CCAN wants to know a lot of vars.
 dnl CCAN wants to know a lot of vars.
 # All the configuration checks.  Regrettably, the __attribute__ checks will
 # All the configuration checks.  Regrettably, the __attribute__ checks will
 # give false positives on old GCCs, since they just cause warnings.  But that's
 # give false positives on old GCCs, since they just cause warnings.  But that's

+ 5 - 0
logging.h

@@ -31,6 +31,11 @@ extern int opt_log_level;
 extern void vapplog(int prio, const char *fmt, va_list ap);
 extern void vapplog(int prio, const char *fmt, va_list ap);
 extern void applog(int prio, const char *fmt, ...);
 extern void applog(int prio, const char *fmt, ...);
 
 
+#define applogr(rv, prio, ...)  do {  \
+	applog(prio, __VA_ARGS__);  \
+	return rv;  \
+} while (0)
+
 /* high-level logging functions with implicit priority */
 /* high-level logging functions with implicit priority */
 extern void log_error(const char *fmt, ...);
 extern void log_error(const char *fmt, ...);
 extern void log_warning(const char *fmt, ...);
 extern void log_warning(const char *fmt, ...);

+ 28 - 0
miner.c

@@ -7374,6 +7374,32 @@ static bool my_blkmaker_sha256_callback(void *digest, const void *buffer, size_t
 	return true;
 	return true;
 }
 }
 
 
+static void raise_fd_limits(void)
+{
+#ifdef HAVE_SETRLIMIT
+	struct rlimit fdlimit;
+	unsigned long old_soft_limit;
+	
+	if (getrlimit(RLIMIT_NOFILE, &fdlimit))
+		applogr(, LOG_DEBUG, "setrlimit: Failed to getrlimit(RLIMIT_NOFILE)");
+	
+	if (fdlimit.rlim_cur == RLIM_INFINITY)
+		applogr(, LOG_DEBUG, "setrlimit: Soft fd limit already infinite");
+	
+	if (fdlimit.rlim_cur == fdlimit.rlim_max)
+		applogr(, LOG_DEBUG, "setrlimit: Soft fd limit already identical to hard limit (%lu)", (unsigned long)fdlimit.rlim_max);
+	
+	old_soft_limit = fdlimit.rlim_cur;
+	fdlimit.rlim_cur = fdlimit.rlim_max;
+	if (setrlimit(RLIMIT_NOFILE, &fdlimit))
+		applogr(, LOG_DEBUG, "setrlimit: Failed to increase soft fd limit from %lu to hard limit of %lu", old_soft_limit, (unsigned long)fdlimit.rlim_max);
+	
+	applog(LOG_DEBUG, "setrlimit: Increased soft fd limit from %lu to hard limit of %lu", old_soft_limit, (unsigned long)fdlimit.rlim_max);
+#else
+	applog(LOG_DEBUG, "setrlimit: Not supported by platform");
+#endif
+}
+
 int main(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
 {
 	bool pools_active = false;
 	bool pools_active = false;
@@ -7501,6 +7527,8 @@ int main(int argc, char *argv[])
 	if (!config_loaded)
 	if (!config_loaded)
 		load_default_config();
 		load_default_config();
 
 
+	raise_fd_limits();
+	
 	if (opt_benchmark) {
 	if (opt_benchmark) {
 		struct pool *pool;
 		struct pool *pool;