diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2013-04-26 00:46:47 +0200 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2013-04-29 01:46:24 +0200 |
commit | ba29a5590bc4479d74454f0b9fdaf007d9d80221 (patch) | |
tree | 3bcafc521d283c9076704dc1aa51400c631ed1eb /src/util.cpp | |
parent | 33029bcedd982f80f7e380bff586f3ab644decce (diff) |
Try to increase file descriptor rlimit if necessary
As the default can be too low, especially on OSX.
Diffstat (limited to 'src/util.cpp')
-rw-r--r-- | src/util.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/util.cpp b/src/util.cpp index 3fd624c42c..8b6d8b32c8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -10,6 +10,7 @@ #endif #include <fcntl.h> #include <sys/stat.h> +#include <sys/resource.h> #endif #include "util.h" @@ -1167,6 +1168,28 @@ bool TruncateFile(FILE *file, unsigned int length) { #endif } + +// this function tries to raise the file descriptor limit to the requested number. +// It returns the actual file descriptor limit (which may be more or less than nMinFD) +int RaiseFileDescriptorLimit(int nMinFD) { +#if defined(WIN32) + return 2048; +#else + struct rlimit limitFD; + if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) { + if (limitFD.rlim_cur < (rlim_t)nMinFD) { + limitFD.rlim_cur = nMinFD; + if (limitFD.rlim_cur > limitFD.rlim_max) + limitFD.rlim_cur = limitFD.rlim_max; + setrlimit(RLIMIT_NOFILE, &limitFD); + getrlimit(RLIMIT_NOFILE, &limitFD); + } + return limitFD.rlim_cur; + } + return nMinFD; // getrlimit failed, assume it's fine +#endif +} + // this function tries to make a particular range of a file allocated (corresponding to disk space) // it is advisory, and the range specified in the arguments will never contain live data void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) { |