--------------------- PatchSet 11518 Date: 2007/06/29 00:08:18 Author: hno Branch: HEAD Tag: (none) Log: Clean up filedescriptor limitations, and handle comm loop restrictions proper this patch splits setMaxFD in two parts: setMaxFD: figures out what to we can use for Squid_MaxFD setSystemLimits: Configures the system limitations to match our expectations which might be lower than what setMaxFD finds if the comm loop has additional restrictions Members: src/main.c:1.396->1.397 src/protos.h:1.530->1.531 src/tools.c:1.254->1.255 Index: squid/src/main.c =================================================================== RCS file: /cvsroot/squid/squid/src/main.c,v retrieving revision 1.396 retrieving revision 1.397 diff -u -r1.396 -r1.397 --- squid/src/main.c 17 Jun 2007 22:08:46 -0000 1.396 +++ squid/src/main.c 29 Jun 2007 00:08:18 -0000 1.397 @@ -1,6 +1,6 @@ /* - * $Id: main.c,v 1.396 2007/06/17 22:08:46 hno Exp $ + * $Id: main.c,v 1.397 2007/06/29 00:08:18 hno Exp $ * * DEBUG: section 1 Startup and Main Loop * AUTHOR: Harvest Derived @@ -552,6 +552,7 @@ debug(1, 0) ("Running on %s\n", WIN32_OS_string); #endif debug(1, 1) ("Process ID %d\n", (int) getpid()); + setSystemLimits(); debug(1, 1) ("With %d file descriptors available\n", Squid_MaxFD); #ifdef _SQUID_MSWIN_ debug(1, 1) ("With %d CRT stdio descriptors available\n", _getmaxstdio()); Index: squid/src/protos.h =================================================================== RCS file: /cvsroot/squid/squid/src/protos.h,v retrieving revision 1.530 retrieving revision 1.531 diff -u -r1.530 -r1.531 --- squid/src/protos.h 20 May 2007 13:45:11 -0000 1.530 +++ squid/src/protos.h 29 Jun 2007 00:08:18 -0000 1.531 @@ -1,6 +1,6 @@ /* - * $Id: protos.h,v 1.530 2007/05/20 13:45:11 adrian Exp $ + * $Id: protos.h,v 1.531 2007/06/29 00:08:18 hno Exp $ * * * SQUID Web Proxy Cache http://www.squid-cache.org/ @@ -1108,6 +1108,7 @@ extern void writePidFile(void); extern void setSocketShutdownLifetimes(int); extern void setMaxFD(void); +extern void setSystemLimits(void); extern time_t getCurrentTime(void); extern int percent(int, int); extern double dpercent(double, double); Index: squid/src/tools.c =================================================================== RCS file: /cvsroot/squid/squid/src/tools.c,v retrieving revision 1.254 retrieving revision 1.255 diff -u -r1.254 -r1.255 --- squid/src/tools.c 28 Jun 2007 23:22:01 -0000 1.254 +++ squid/src/tools.c 29 Jun 2007 00:08:18 -0000 1.255 @@ -1,6 +1,6 @@ /* - * $Id: tools.c,v 1.254 2007/06/28 23:22:01 hno Exp $ + * $Id: tools.c,v 1.255 2007/06/29 00:08:18 hno Exp $ * * DEBUG: section 21 Misc Functions * AUTHOR: Harvest Derived @@ -752,51 +752,62 @@ } +/* A little piece of glue for odd systems */ +#ifndef RLIMIT_NOFILE +#ifdef RLIMIT_OFILE +#define RLIMIT_NOFILE RLIMIT_OFILE +#endif +#endif + +/* Figure out the number of supported filedescriptors */ void setMaxFD(void) { -#if HAVE_SETRLIMIT - /* try to use as many file descriptors as possible */ - /* System V uses RLIMIT_NOFILE and BSD uses RLIMIT_OFILE */ +#if HAVE_SETRLIMIT && defined(RLIMIT_NOFILE) struct rlimit rl; -#if !defined(_SQUID_CYGWIN_) -#if defined(RLIMIT_NOFILE) - if (Config.max_filedescriptors > 0) { - Squid_MaxFD = rl.rlim_cur = rl.rlim_max = Config.max_filedescriptors; - if (setrlimit(RLIMIT_NOFILE, &rl)) + if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { + debug(50, 0) ("setrlimit: RLIMIT_NOFILE: %s\n", xstrerror()); + } else if (Config.max_filedescriptors > 0) { + rl.rlim_cur = Config.max_filedescriptors; + if (rl.rlim_cur > rl.rlim_max) + rl.rlim_max = rl.rlim_cur; + if (setrlimit(RLIMIT_NOFILE, &rl)) { debug(50, 0) ("setrlimit: RLIMIT_NOFILE: %s\n", xstrerror()); + getrlimit(RLIMIT_NOFILE, &rl); + rl.rlim_cur = rl.rlim_max; + if (setrlimit(RLIMIT_NOFILE, &rl)) { + debug(50, 0) ("setrlimit: RLIMIT_NOFILE: %s\n", xstrerror()); + } + } } if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { debug(50, 0) ("setrlimit: RLIMIT_NOFILE: %s\n", xstrerror()); } else { - rl.rlim_cur = Squid_MaxFD; - if (rl.rlim_cur > rl.rlim_max) - Squid_MaxFD = rl.rlim_cur = rl.rlim_max; - if (setrlimit(RLIMIT_NOFILE, &rl) < 0) { - snprintf(tmp_error_buf, ERROR_BUF_SZ, - "setrlimit: RLIMIT_NOFILE: %s", xstrerror()); - fatal_dump(tmp_error_buf); - } + Squid_MaxFD = rl.rlim_cur; } -#elif defined(RLIMIT_OFILE) - if (getrlimit(RLIMIT_OFILE, &rl) < 0) { +#endif /* HAVE_SETRLIMIT */ +} + +void +setSystemLimits(void) +{ +#if HAVE_SETRLIMIT && defined(RLIMIT_NOFILE) && !defined(_SQUID_CYGWIN_) + /* limit system filedescriptors to our own limit */ + struct rlimit rl; + if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { debug(50, 0) ("setrlimit: RLIMIT_NOFILE: %s\n", xstrerror()); } else { rl.rlim_cur = Squid_MaxFD; - if (rl.rlim_cur > rl.rlim_max) - Squid_MaxFD = rl.rlim_cur = rl.rlim_max; - if (setrlimit(RLIMIT_OFILE, &rl) < 0) { + if (setrlimit(RLIMIT_NOFILE, &rl) < 0) { snprintf(tmp_error_buf, ERROR_BUF_SZ, - "setrlimit: RLIMIT_OFILE: %s", xstrerror()); + "setrlimit: RLIMIT_NOFILE: %s", xstrerror()); fatal_dump(tmp_error_buf); } } -#endif -#endif -#else /* HAVE_SETRLIMIT */ - debug(21, 1) ("setMaxFD: Cannot increase: setrlimit() not supported on this system\n"); #endif /* HAVE_SETRLIMIT */ - + if (Config.max_filedescriptors > Squid_MaxFD) { + debug(50, 1) ("NOTICE: Could not increase the number of filedescriptors\n"); + } #if HAVE_SETRLIMIT && defined(RLIMIT_DATA) if (getrlimit(RLIMIT_DATA, &rl) < 0) { debug(50, 0) ("getrlimit: RLIMIT_DATA: %s\n", xstrerror());