Hello guys,

As you probably know, we have one RH specific patch to squid. It's for dynamic configuration of filedescriptors number, via. squid.conf. I reviewed it for the 2.6 branch, fixed some bugs (which comes with squid reconfiguration) so I think it's stable enough now.

If you find it useful, put it please to http://devel.squid-cache.org/ or I can make place for it on http://people.redhat.com/stransky/squid/, it's up to you.

Regards,

Martin
--- squid-2.6.STABLE1/configure.fd	2006-07-18 10:09:15.000000000 +0200
+++ squid-2.6.STABLE1/configure	2006-07-18 10:09:15.000000000 +0200
@@ -2970,6 +2970,34 @@
   AMDEP_FALSE=
 fi
 
+if false; then
+  USE_FD_CONFIG_TRUE=
+  USE_FD_CONFIG_FALSE='#'
+else
+  USE_FD_CONFIG_TRUE='#'
+  USE_FD_CONFIG_FALSE=
+fi
+# Check whether --enable-fd-config or --disable-fd-config was given.
+if test "${enable_fd_config+set}" = set; then
+  enableval="$enable_fd_config"
+   if test "$enableval" = "yes" ; then
+    echo "File descriptor config enabled"
+    cat >> confdefs.h <<\EOF
+#define FD_CONFIG 1
+EOF
+
+    
+
+if true; then
+  USE_FD_CONFIG_TRUE=
+  USE_FD_CONFIG_FALSE='#'
+else
+  USE_FD_CONFIG_TRUE='#'
+  USE_FD_CONFIG_FALSE=
+fi
+  fi
+
+fi
 
 
 
--- squid-2.6.STABLE1/include/autoconf.h.in.fd	2006-07-18 10:09:15.000000000 +0200
+++ squid-2.6.STABLE1/include/autoconf.h.in	2006-07-18 10:09:15.000000000 +0200
@@ -36,6 +36,9 @@
 /* Traffic management via "delay pools". */
 #undef DELAY_POOLS
 
+/* Filedesc managment */
+#undef FD_CONFIG
+
 /* Enable following X-Forwarded-For headers */
 #undef FOLLOW_X_FORWARDED_FOR
 
--- squid-2.6.STABLE1/configure.in.fd	2006-07-18 10:09:15.000000000 +0200
+++ squid-2.6.STABLE1/configure.in	2006-07-18 10:09:15.000000000 +0200
@@ -501,6 +501,16 @@
   fi
 ])
 
+AM_CONDITIONAL(USE_FD_CONFIG, false)
+AC_ARG_ENABLE(fd-config,
+[  --enable-fd-config    Enable filedesc config to configure maximal number of used filedescriptors],
+[ if test "$enableval" = "yes" ; then
+    echo "Filedesc config enabled"
+    AC_DEFINE(FD_CONFIG)
+    AM_CONDITIONAL(USE_FD_CONFIG, true)
+  fi
+])
+
 dnl This is a developer only option. Developers know how to set defines
 dnl
 dnl AC_ARG_ENABLE(mem-gen-trace,
--- squid-2.6.STABLE1/src/fd.c.fd	2006-06-11 19:06:25.000000000 +0200
+++ squid-2.6.STABLE1/src/fd.c	2006-07-18 10:09:15.000000000 +0200
@@ -61,7 +61,7 @@
 {
     if (fd < Biggest_FD)
 	return;
-    assert(fd < Squid_MaxFD);
+    assert(fd < SQUID_NUMFD);
     if (fd > Biggest_FD) {
 	/*
 	 * assert that we are not closing a FD bigger than
@@ -206,7 +206,7 @@
 {
     int i;
     fde *F;
-    for (i = 0; i < Squid_MaxFD; i++) {
+    for (i = 0; i < SQUID_NUMFD; i++) {
 	F = &fd_table[i];
 	if (!F->flags.open)
 	    continue;
@@ -223,7 +223,7 @@
 int
 fdNFree(void)
 {
-    return Squid_MaxFD - Number_FD - Opening_FD;
+    return SQUID_NUMFD - Number_FD - Opening_FD;
 }
 
 int
@@ -252,10 +252,10 @@
     /*
      * Calculate a new reserve, based on current usage and a small extra
      */
-    new = Squid_MaxFD - Number_FD + XMIN(25, Squid_MaxFD / 16);
+    new = SQUID_NUMFD - Number_FD + XMIN(25, SQUID_NUMFD / 16);
     if (new <= RESERVED_FD)
 	return;
-    x = Squid_MaxFD - 20 - XMIN(25, Squid_MaxFD / 16);
+    x = SQUID_NUMFD - 20 - XMIN(25, SQUID_NUMFD / 16);
     if (new > x) {
 	/* perhaps this should be fatal()? -DW */
 	debug(51, 0) ("WARNING: This machine has a serious shortage of filedescriptors.\n");
--- squid-2.6.STABLE1/src/mem.c.fd	2006-05-20 23:51:49.000000000 +0200
+++ squid-2.6.STABLE1/src/mem.c	2006-07-18 10:09:15.000000000 +0200
@@ -335,13 +335,13 @@
     memDataInit(MEM_HTTP_HDR_CONTENT_RANGE, "HttpHdrContRange", sizeof(HttpHdrContRange), 0);
     memDataInit(MEM_INTLIST, "intlist", sizeof(intlist), 0);
     memDataInit(MEM_MEMOBJECT, "MemObject", sizeof(MemObject),
-	Squid_MaxFD >> 3);
+	SQUID_NUMFD >> 3);
     memDataInit(MEM_MEM_NODE, "mem_node", sizeof(mem_node), 0);
     memDataInit(MEM_NETDBENTRY, "netdbEntry", sizeof(netdbEntry), 0);
     memDataInit(MEM_NET_DB_NAME, "net_db_name", sizeof(net_db_name), 0);
     memDataInit(MEM_RELIST, "relist", sizeof(relist), 0);
     memDataInit(MEM_REQUEST_T, "request_t", sizeof(request_t),
-	Squid_MaxFD >> 3);
+	SQUID_NUMFD >> 3);
     memDataInit(MEM_STOREENTRY, "StoreEntry", sizeof(StoreEntry), 0);
     memDataInit(MEM_WORDLIST, "wordlist", sizeof(wordlist), 0);
     memDataInit(MEM_CLIENT_INFO, "ClientInfo", sizeof(ClientInfo), 0);
--- squid-2.6.STABLE1/src/cf.data.pre.fd	2006-07-18 10:09:15.000000000 +0200
+++ squid-2.6.STABLE1/src/cf.data.pre	2006-07-18 10:09:15.000000000 +0200
@@ -5027,4 +5027,16 @@
 	or response to be rejected.
 DOC_END
 
+NAME: max_filedesc
+IFDEF: FD_CONFIG
+TYPE: int
+DEFAULT: 1024
+LOC: Config.max_filedesc
+DOC_START
+	The maximum number of open file descriptors.
+        
+	WARNING: Changes of this value isn't respected by reconfigure 
+	command. This value should be changed only if there isn't 
+	any active squid process.
+DOC_END
 EOF
--- squid-2.6.STABLE1/src/ipc.c.fd	2006-06-24 11:41:36.000000000 +0200
+++ squid-2.6.STABLE1/src/ipc.c	2006-07-18 10:09:15.000000000 +0200
@@ -298,7 +298,7 @@
     close(t3);
 #if 0
     /* Make sure all other filedescriptors are closed */
-    for (x = 3; x < Squid_MaxFD; x++)
+    for (x = 3; x < SQUID_NUMFD; x++)
 	close(x);
 #endif
 #if HAVE_SETSID
--- squid-2.6.STABLE1/src/protos.h.fd	2006-07-18 10:09:15.000000000 +0200
+++ squid-2.6.STABLE1/src/protos.h	2006-07-18 10:09:15.000000000 +0200
@@ -1264,6 +1264,8 @@
 
 #if DELAY_POOLS
 extern void delayPoolsInit(void);
+extern void delayPoolsAllocate(void);
+extern void delayPoolsFree(void);
 extern void delayInitDelayData(unsigned short pools);
 extern void delayFreeDelayData(unsigned short pools);
 extern void delayCreateDelayPool(unsigned short pool, u_char class);
--- squid-2.6.STABLE1/src/snmp_agent.c.fd	2006-05-16 03:08:30.000000000 +0200
+++ squid-2.6.STABLE1/src/snmp_agent.c	2006-07-18 10:09:15.000000000 +0200
@@ -326,7 +326,7 @@
 	break;
     case PERF_SYS_CURUNUSED_FD:
 	Answer = snmp_var_new_integer(Var->name, Var->name_length,
-	    (snint) Squid_MaxFD - Number_FD,
+	    (snint) SQUID_NUMFD - Number_FD,
 	    SMI_GAUGE32);
 	break;
     case PERF_SYS_CURRESERVED_FD:
--- squid-2.6.STABLE1/src/comm_poll.c.fd	2006-06-25 17:53:14.000000000 +0200
+++ squid-2.6.STABLE1/src/comm_poll.c	2006-07-18 10:17:11.000000000 +0200
@@ -152,13 +152,31 @@
 }
 
 #if DELAY_POOLS
+
+static int *slowfdarr = NULL;
+static int slowfdmax;
 static int slowfdcnt = 0;
-static int slowfdarr[SQUID_MAXFD];
+
+void
+comm_slow_fd_init(int fd_num)
+{
+    slowfdarr = xmalloc(sizeof(slowfdarr[0])*fd_num);
+    slowfdmax = fd_num;
+    slowfdcnt = 0;
+}
+
+void
+comm_slow_fd_destroy(void)
+{
+    xfree(slowfdarr);
+    slowfdarr = NULL;
+    slowfdmax = slowfdcnt = 0;
+}
 
 static void
 commAddSlowFd(int fd)
 {
-    assert(slowfdcnt < SQUID_MAXFD);
+    assert(slowfdcnt < slowfdmax);
     slowfdarr[slowfdcnt++] = fd;
 }
 
@@ -286,9 +304,9 @@
 int
 comm_select(int msec)
 {
-    struct pollfd pfds[SQUID_MAXFD];
+    struct pollfd pfds[SQUID_NUMFD];
 #if DELAY_POOLS
-    char slowfds[SQUID_MAXFD];
+    BIT_ARRAY slowfds;
 #endif
     int fd;
     unsigned int i;
@@ -300,6 +318,9 @@
     int calldns = 0;
     static time_t last_timeout = 0;
     double timeout = current_dtime + (msec / 1000.0);
+#if DELAY_POOLS
+    BA_INIT(slowfds,SQUID_NUMFD);
+#endif
     do {
 	double start;
 	getCurrentTime();
@@ -307,7 +328,7 @@
 	/* Handle any fs callbacks that need doing */
 	storeDirCallback();
 #if DELAY_POOLS
-	memset(slowfds, 0, Biggest_FD);
+	BA_ZERO(slowfds);
 #endif
 	if (commCheckICPIncoming)
 	    comm_poll_icp_incoming();
@@ -333,7 +354,7 @@
 		    break;
 #if DELAY_POOLS
 		case -1:
-		    slowfds[i] = 1;
+		    BA_SET(i, slowfds);
 		    break;
 #endif
 		default:
@@ -384,6 +405,9 @@
 	}
 	if (nfds == 0) {
 	    assert(shutting_down);
+#if DELAY_POOLS
+            BA_FREE(slowfds);
+#endif
 	    return COMM_SHUTDOWN;
 	}
 	if (npending)
@@ -396,6 +420,9 @@
 	if (num < 0 && !ignoreErrno(errno)) {
 	    debug(5, 0) ("comm_select: poll failure: %s\n", xstrerror());
 	    assert(errno != EINVAL);
+#if DELAY_POOLS
+            BA_FREE(slowfds);
+#endif
 	    return COMM_ERROR;
 	    /* NOTREACHED */
 	}
@@ -462,7 +489,7 @@
 		if (hdl == NULL)
 		    (void) 0;	/* Nothing to do */
 #if DELAY_POOLS
-		else if (slowfds[i])
+		else if (BA_ISSET(fd, slowfds))
 		    commAddSlowFd(fd);
 #endif
 		else {
@@ -546,10 +573,16 @@
 #endif
 	getCurrentTime();
 	statCounter.select_time += (current_dtime - start);
+#if DELAY_POOLS
+        BA_FREE(slowfds);
+#endif
 	return COMM_OK;
     }
     while (timeout > current_dtime);
     debug(5, 8) ("comm_select: time out: %ld.\n", (long int) squid_curtime);
+#if DELAY_POOLS
+    BA_FREE(slowfds);
+#endif
     return COMM_TIMEOUT;
 }
 
--- squid-2.6.STABLE1/src/comm_epoll.c.fd	2006-06-27 15:09:43.000000000 +0200
+++ squid-2.6.STABLE1/src/comm_epoll.c	2006-07-18 10:09:15.000000000 +0200
@@ -68,13 +68,13 @@
 void
 comm_select_init()
 {
-    kdpfd = epoll_create(Squid_MaxFD);
+    kdpfd = epoll_create(SQUID_NUMFD);
     if (kdpfd < 0)
 	fatalf("comm_select_init: epoll_create(): %s\n", xstrerror());
     fd_open(kdpfd, FD_UNKNOWN, "epoll ctl");
     commSetCloseOnExec(kdpfd);
 
-    epoll_state = xcalloc(Squid_MaxFD, sizeof(*epoll_state));
+    epoll_state = xcalloc(SQUID_NUMFD, sizeof(*epoll_state));
 }
 
 void
@@ -86,6 +86,21 @@
     safe_free(epoll_state);
 }
 
+#if DELAY_POOLS
+
+/* dummy functions... */
+void
+comm_slow_fd_init(int fd_num)
+{
+}
+
+void
+comm_slow_fd_destroy(void)
+{
+}
+
+#endif
+
 void
 commSetEvents(int fd, int need_read, int need_write)
 {
--- squid-2.6.STABLE1/src/ident.c.fd	2006-05-13 00:08:37.000000000 +0200
+++ squid-2.6.STABLE1/src/ident.c	2006-07-18 10:09:15.000000000 +0200
@@ -239,7 +239,7 @@
 identInit(void)
 {
     ident_hash = hash_create((HASHCMP *) strcmp,
-	hashPrime(Squid_MaxFD / 8),
+	hashPrime(SQUID_NUMFD / 8),
 	hash4);
 }
 
--- squid-2.6.STABLE1/src/main.c.fd	2006-07-18 10:09:15.000000000 +0200
+++ squid-2.6.STABLE1/src/main.c	2006-07-18 10:11:06.000000000 +0200
@@ -556,7 +556,7 @@
 	debug(1, 0) ("Running on %s\n", WIN32_OS_string);
 #endif
     debug(1, 1) ("Process ID %d\n", (int) getpid());
-    debug(1, 1) ("With %d file descriptors available\n", Squid_MaxFD);
+    debug(1, 1) ("With %d file descriptors available\n", SQUID_NUMFD);
 #ifdef _SQUID_MSWIN_
     debug(1, 1) ("With %d CRT stdio descriptors available\n", _getmaxstdio());
     if (WIN32_Socks_initialized)
@@ -611,6 +611,9 @@
 #if DELAY_POOLS
 	delayPoolsInit();
 #endif
+#if DELAY_POOLS
+	comm_slow_fd_init(SQUID_NUMFD);
+#endif
 	fwdInit();
     }
 #if USE_WCCP
@@ -674,8 +677,6 @@
 #endif
 
     debug_log = stderr;
-    if (FD_SETSIZE < Squid_MaxFD)
-	Squid_MaxFD = FD_SETSIZE;
 
 #ifdef _SQUID_WIN32_
     if ((WIN32_init_err = WIN32_Subsystem_Init(&argc, &argv)))
@@ -758,6 +759,8 @@
     /* Make sure the OS allows core dumps if enabled in squid.conf */
     enableCoredumps();
 
+    setMaxFD();
+
 #if TEST_ACCESS
     comm_init();
     comm_select_init();
@@ -791,7 +794,6 @@
     }
     if (!opt_no_daemon)
 	watch_child(argv);
-    setMaxFD();
 
     /* init comm module */
     comm_init();
@@ -1096,6 +1098,9 @@
 #endif
     releaseServerSockets();
     commCloseAllSockets();
+#if DELAY_POOLS
+    comm_slow_fd_destroy();
+#endif
     authenticateShutdown();
 #if USE_UNLINKD
     unlinkdClose();
--- squid-2.6.STABLE1/src/structs.h.fd	2006-07-18 10:09:15.000000000 +0200
+++ squid-2.6.STABLE1/src/structs.h	2006-07-18 10:09:15.000000000 +0200
@@ -805,6 +805,9 @@
 #endif
     time_t refresh_stale_window;
     int umask;
+#if FD_CONFIG
+    int max_filedesc;
+#endif
 };
 
 struct _SquidConfig2 {
--- squid-2.6.STABLE1/src/delay_pools.c.fd	2006-06-08 21:36:36.000000000 +0200
+++ squid-2.6.STABLE1/src/delay_pools.c	2006-07-18 10:10:11.000000000 +0200
@@ -89,7 +89,7 @@
 typedef union _delayPool delayPool;
 
 static delayPool *delay_data = NULL;
-static char *delay_no_delay;
+static char *delay_no_delay = NULL;
 static time_t delay_pools_last_update = 0;
 static hash_table *delay_id_ptr_hash = NULL;
 static long memory_used = 0;
@@ -134,7 +134,7 @@
 delayPoolsInit(void)
 {
     delay_pools_last_update = getCurrentTime();
-    delay_no_delay = xcalloc(1, Squid_MaxFD);
+    delay_no_delay = xcalloc(1, SQUID_NUMFD);
     cachemgrRegister("delay", "Delay Pool Levels", delayPoolStats, 0, 1);
 }
 
--- squid-2.6.STABLE1/src/tools.c.fd	2006-06-30 23:23:05.000000000 +0200
+++ squid-2.6.STABLE1/src/tools.c	2006-07-18 10:09:15.000000000 +0200
@@ -753,6 +753,12 @@
 void
 setMaxFD(void)
 {
+
+/* Set up number of used filedescriptors from config file */
+#if FD_CONFIG
+   SQUID_NUMFD = Config.max_filedesc;
+#endif
+
 #if HAVE_SETRLIMIT
     /* try to use as many file descriptors as possible */
     /* System V uses RLIMIT_NOFILE and BSD uses RLIMIT_OFILE */
@@ -762,9 +768,11 @@
     if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
 	debug(50, 0) ("setrlimit: RLIMIT_NOFILE: %s\n", xstrerror());
     } else {
-	rl.rlim_cur = Squid_MaxFD;
+	rl.rlim_max = rl.rlim_cur = SQUID_NUMFD;
+/*
 	if (rl.rlim_cur > rl.rlim_max)
-	    Squid_MaxFD = rl.rlim_cur = rl.rlim_max;
+	    rl.rlim_cur = rl.rlim_max;
+*/
 	if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
 	    snprintf(tmp_error_buf, ERROR_BUF_SZ,
 		"setrlimit: RLIMIT_NOFILE: %s", xstrerror());
@@ -775,9 +783,11 @@
     if (getrlimit(RLIMIT_OFILE, &rl) < 0) {
 	debug(50, 0) ("setrlimit: RLIMIT_NOFILE: %s\n", xstrerror());
     } else {
-	rl.rlim_cur = Squid_MaxFD;
+	rl.rlim_max = rl.rlim_cur = SQUID_NUMFD;
+/*
 	if (rl.rlim_cur > rl.rlim_max)
-	    Squid_MaxFD = rl.rlim_cur = rl.rlim_max;
+	    rl.rlim_cur = rl.rlim_max;
+*/
 	if (setrlimit(RLIMIT_OFILE, &rl) < 0) {
 	    snprintf(tmp_error_buf, ERROR_BUF_SZ,
 		"setrlimit: RLIMIT_OFILE: %s", xstrerror());
--- squid-2.6.STABLE1/src/comm_select.c.fd	2006-06-08 14:53:20.000000000 +0200
+++ squid-2.6.STABLE1/src/comm_select.c	2006-07-18 10:09:15.000000000 +0200
@@ -61,8 +61,8 @@
 static void comm_select_dns_incoming(void);
 
 static struct timeval zero_tv;
-static fd_set global_readfds;
-static fd_set global_writefds;
+static BIT_ARRAY global_readfds;
+static BIT_ARRAY global_writefds;
 static int nreadfds;
 static int nwritefds;
 
@@ -157,13 +157,31 @@
 }
 
 #if DELAY_POOLS
+
+static int *slowfdarr = NULL;
+static int slowfdmax;
 static int slowfdcnt = 0;
-static int slowfdarr[SQUID_MAXFD];
+
+void
+comm_slow_fd_init(int fd_num)
+{
+    slowfdarr = xmalloc(sizeof(slowfdarr[0])*fd_num);
+    slowfdmax = fd_num;
+    slowfdcnt = 0;
+}
+
+void
+comm_slow_fd_destroy(void)
+{
+    xfree(slowfdarr);
+    slowfdarr = NULL;
+    slowfdmax = slowfdcnt = 0;
+}
 
 static void
 commAddSlowFd(int fd)
 {
-    assert(slowfdcnt < SQUID_MAXFD);
+    assert(slowfdcnt < slowfdmax);
     slowfdarr[slowfdcnt++] = fd;
 }
 
@@ -294,7 +312,7 @@
     fd_set readfds;
     fd_set writefds;
 #if DELAY_POOLS
-    fd_set slowfds;
+    BIT_ARRAY slowfds;
 #endif
     int fd;
     int maxfd;
@@ -310,12 +328,15 @@
     static time_t last_timeout = 0;
     struct timeval poll_time;
     double timeout = current_dtime + (msec / 1000.0);
+#if DELAY_POOLS
+    BA_INIT(slowfds,SQUID_NUMFD);
+#endif
     do {
 	double start;
 	getCurrentTime();
 	start = current_dtime;
 #if DELAY_POOLS
-	FD_ZERO(&slowfds);
+	BA_ZERO(slowfds);
 #endif
 	/* Handle any fs callbacks that need doing */
 	storeDirCallback();
@@ -327,10 +348,12 @@
 	    comm_select_http_incoming();
 	callicp = calldns = callhttp = 0;
 	maxfd = Biggest_FD + 1;
-	xmemcpy(&readfds, &global_readfds,
-	    howmany(maxfd, FD_MASK_BITS) * FD_MASK_BYTES);
-	xmemcpy(&writefds, &global_writefds,
-	    howmany(maxfd, FD_MASK_BITS) * FD_MASK_BYTES);
+#if !FD_CONFIG
+        xmemcpy(&readfds, &global_readfds,
+            howmany(maxfd, FD_MASK_BITS) * FD_MASK_BYTES);
+        xmemcpy(&writefds, &global_writefds,
+            howmany(maxfd, FD_MASK_BITS) * FD_MASK_BYTES);
+#endif
 	/* remove stalled FDs, and deal with pending descriptors */
 	maxindex = howmany(maxfd, FD_MASK_BITS);
 	/* Note: To simplify logics we are cheating a little on pending fds
@@ -357,7 +380,7 @@
 			break;
 #if DELAY_POOLS
 		    case -1:
-			FD_SET(fd, &slowfds);
+			BA_SET(fd, slowfds);
 			break;
 #endif
 		    default:
@@ -368,6 +391,9 @@
 	}
 	if (nreadfds + nwritefds == 0) {
 	    assert(shutting_down);
+#if DELAY_POOLS
+            BA_FREE(slowfds);
+#endif
 	    return COMM_SHUTDOWN;
 	}
 	if (msec > MAX_POLL_TIME)
@@ -385,6 +411,9 @@
 	    debug(5, 0) ("comm_select: select failure: %s\n",
 		xstrerror());
 	    examine_select(&readfds, &writefds);
+#if DELAY_POOLS
+            BA_FREE(slowfds);
+#endif
 	    return COMM_ERROR;
 	    /* NOTREACHED */
 	}
@@ -448,7 +477,7 @@
 			break;
 		    }
 #if DELAY_POOLS
-		    if (do_read && FD_ISSET(fd, &slowfds))
+		    if (do_read && BA_ISSET(fd, slowfds))
 			commAddSlowFd(fd);
 		    else
 #endif
@@ -525,10 +554,16 @@
 #endif
 	getCurrentTime();
 	statCounter.select_time += (current_dtime - start);
+#if DELAY_POOLS
+        BA_FREE(slowfds);
+#endif
 	return COMM_OK;
     }
     while (timeout > current_dtime);
     debug(5, 8) ("comm_select: time out: %d\n", (int) squid_curtime);
+#if DELAY_POOLS
+    BA_FREE(slowfds);
+#endif
     return COMM_TIMEOUT;
 }
 
@@ -563,8 +598,12 @@
     cachemgrRegister("comm_incoming",
 	"comm_incoming() stats",
 	commIncomingStats, 0, 1);
-    FD_ZERO(&global_readfds);
-    FD_ZERO(&global_writefds);
+
+    BA_INIT(global_readfds,SQUID_NUMFD);
+    BA_INIT(global_writefds,SQUID_NUMFD);
+
+    BA_ZERO(global_readfds);
+    BA_ZERO(global_writefds);
     nreadfds = nwritefds = 0;
 }
 
@@ -594,7 +633,7 @@
     fde *F = NULL;
     struct stat sb;
     debug(5, 0) ("examine_select: Examining open file descriptors...\n");
-    for (fd = 0; fd < Squid_MaxFD; fd++) {
+    for (fd = 0; fd < SQUID_NUMFD; fd++) {
 	FD_ZERO(&read_x);
 	FD_ZERO(&write_x);
 	tv.tv_sec = tv.tv_usec = 0;
@@ -662,18 +701,18 @@
 void
 commSetEvents(int fd, int need_read, int need_write)
 {
-    if (need_read && !FD_ISSET(fd, &global_readfds)) {
-	FD_SET(fd, &global_readfds);
-	nreadfds++;
-    } else if (!need_read && FD_ISSET(fd, &global_readfds)) {
-	FD_CLR(fd, &global_readfds);
-	nreadfds--;
+    if (need_read && !BA_ISSET(fd, global_readfds)) {
+	BA_SET(fd, global_readfds);
+        nreadfds++;
+    } else if (!need_read && BA_ISSET(fd, global_readfds)) {
+	BA_CLR(fd, global_readfds);
+      	nreadfds--;
     }
-    if (need_write && !FD_ISSET(fd, &global_writefds)) {
-	FD_SET(fd, &global_writefds);
+    if (need_write && !BA_ISSET(fd, global_writefds)) {
+	BA_SET(fd, global_writefds);
 	nwritefds++;
-    } else if (!need_write && FD_ISSET(fd, &global_writefds)) {
-	FD_CLR(fd, &global_writefds);
+    } else if (!need_write && BA_ISSET(fd, global_writefds)) {
+	BA_CLR(fd, global_writefds);
 	nwritefds--;
     }
 }
--- squid-2.6.STABLE1/src/comm.c.fd	2006-06-27 13:14:22.000000000 +0200
+++ squid-2.6.STABLE1/src/comm.c	2006-07-18 10:09:15.000000000 +0200
@@ -242,7 +242,7 @@
 }
 
 /*
- * NOTE: set the listen queue to Squid_MaxFD/4 and rely on the kernel to      
+ * NOTE: set the listen queue to Squid_MAXFD/4 and rely on the kernel to      
  * impose an upper limit.  Solaris' listen(3n) page says it has   
  * no limit on this parameter, but sys/socket.h sets SOMAXCONN 
  * to 5.  HP-UX currently has a limit of 20.  SunOS is 5 and
@@ -252,9 +252,9 @@
 comm_listen(int sock)
 {
     int x;
-    if ((x = listen(sock, Squid_MaxFD >> 2)) < 0) {
+    if ((x = listen(sock, SQUID_NUMFD >> 2)) < 0) {
 	debug(5, 0) ("comm_listen: listen(%d, %d): %s\n",
-	    Squid_MaxFD >> 2,
+	    SQUID_NUMFD >> 2,
 	    sock, xstrerror());
 	return x;
     }
@@ -454,7 +454,7 @@
     fde *F;
     debug(5, 3) ("commSetTimeout: FD %d timeout %d\n", fd, timeout);
     assert(fd >= 0);
-    assert(fd < Squid_MaxFD);
+    assert(fd < SQUID_NUMFD);
     F = &fd_table[fd];
     assert(F->flags.open);
     if (timeout < 0) {
@@ -721,7 +721,7 @@
 
     debug(5, 5) ("comm_close: FD %d\n", fd);
     assert(fd >= 0);
-    assert(fd < Squid_MaxFD);
+    assert(fd < SQUID_NUMFD);
 
     /* XXX This down to the cavium block below needs to be split and
      * also called once on lingering close. In addition the ssl_shutdown
@@ -1026,12 +1026,12 @@
 void
 comm_init(void)
 {
-    fd_table = xcalloc(Squid_MaxFD, sizeof(fde));
+    fd_table = xcalloc(SQUID_NUMFD, sizeof(fde));
     /* XXX account fd_table */
     /* Keep a few file descriptors free so that we don't run out of FD's
      * after accepting a client but before it opens a socket or a file.
-     * Since Squid_MaxFD can be as high as several thousand, don't waste them */
-    RESERVED_FD = XMIN(100, Squid_MaxFD / 4);
+     * Since SQUID_NUMFD can be as high as several thousand, don't waste them */
+    RESERVED_FD = XMIN(100, SQUID_NUMFD / 4);
     CBDATA_INIT_TYPE(ConnectStateData);
     comm_write_pool = memPoolCreate("CommWriteStateData", sizeof(CommWriteStateData));
     conn_close_pool = memPoolCreate("close_handler", sizeof(close_handler));
--- squid-2.6.STABLE1/src/squid.h.fd	2006-06-08 14:53:20.000000000 +0200
+++ squid-2.6.STABLE1/src/squid.h	2006-07-18 10:09:15.000000000 +0200
@@ -409,6 +409,18 @@
 #include "cache_snmp.h"
 #endif
 
+#if !USE_POLL && !USE_EPOLL
+#undef FD_CONFIG
+#endif
+
+#if FD_CONFIG
+#define BIT_ARRAY_ACTIVE 1
+#else
+#define BIT_ARRAY_ACTIVE 0
+#endif
+
+#include "bit_array.h"
+
 #include "hash.h"
 #include "rfc1035.h"
 
--- /dev/null	2006-07-17 08:51:40.641022250 +0200
+++ squid-2.6.STABLE1/src/bit_array.h	2006-07-18 10:09:15.000000000 +0200
@@ -0,0 +1,90 @@
+/*
+ *
+ * DEBUG:
+ * AUTHOR: Martin Stransky <[EMAIL PROTECTED]>
+ *
+ * SQUID Web Proxy Cache          http://www.squid-cache.org/
+ * ----------------------------------------------------------
+ *
+ *  Squid is the result of efforts by numerous individuals from
+ *  the Internet community; see the CONTRIBUTORS file for full
+ *  details.   Many organizations have provided support for Squid's
+ *  development; see the SPONSORS file for full details.  Squid is
+ *  Copyrighted (C) 2001 by the Regents of the University of
+ *  California; see the COPYRIGHT file for full details.  Squid
+ *  incorporates software developed and/or copyrighted by other
+ *  sources; see the CREDITS file for full details.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ *
+ */
+
+#ifndef __BIT_ARRAY_H__
+#define __BIT_ARRAY_H__
+
+#if !BIT_ARRAY_ACTIVE
+
+#define BIT_ARRAY             fd_set
+#define BA_INIT(arr,len)
+#define BA_FREE(arr)       
+
+#define BA_ZERO(arr)          FD_ZERO(&arr)
+#define BA_SET(fd,arr)        FD_SET(fd,&arr)
+#define BA_CLR(fd,arr)        FD_CLR(fd,&arr)
+#define BA_ISSET(fd,arr)      FD_ISSET(fd,&arr)
+
+#else
+
+typedef struct _BIT_ARRAY {
+
+  int *p_pool;
+  int  len;
+  int  offset_mask;
+  int  base_shift;
+
+} BIT_ARRAY;
+
+#define BA_INIT(arr,lenght)                                                 \
+{                                                                           \
+  int i,tmp = sizeof((arr).p_pool[0])*8;                                    \
+                                                                            \
+  for(i = 2, (arr).base_shift = 1; i < tmp; i *= 2)                         \
+    (arr).base_shift++;                                                     \
+  (arr).offset_mask = (i-1);                                                \
+                                                                            \
+  (arr).len = lenght / (sizeof((arr).p_pool[0])*8) + 1;                     \
+  (arr).p_pool = xmalloc(sizeof((arr).p_pool[0])*(arr).len);                \
+}                                                                           \
+
+
+#define BA_FREE(arr)         \
+{                            \
+  if((arr).p_pool) {         \
+    xfree((arr).p_pool);     \
+    (arr).p_pool = NULL;     \
+  }                          \
+  (arr).len = 0;             \
+}                            \
+
+
+#define BA_ZERO(arr)        { memset((arr).p_pool, 0, sizeof((arr).p_pool[0])*(arr).len); }
+#define BA_SET(fdsp, arr)   { (arr).p_pool[(fdsp)>>(arr).base_shift]|=(0x1<<((fdsp)&(arr).offset_mask)); }
+#define BA_CLR(fdsp, arr)   { (arr).p_pool[(fdsp)>>(arr).base_shift]&=(~(0x1<<((fdsp)&(arr).offset_mask))); }
+#define BA_ISSET(fdsp, arr) ((arr).p_pool[(fdsp)>>(arr).base_shift]&(0x1<<((fdsp)&(arr).offset_mask)))
+
+#endif // BIT_ARRAY_ACTIVE
+
+#endif //__BIT_ARRAY_H__
+
--- squid-2.6.STABLE1/src/globals.h.fd	2006-06-30 23:23:05.000000000 +0200
+++ squid-2.6.STABLE1/src/globals.h	2006-07-18 10:09:15.000000000 +0200
@@ -74,7 +74,7 @@
 extern int NDnsServersAlloc;	/* 0 */
 extern int NHttpSockets;	/* 0 */
 extern int RESERVED_FD;
-extern int Squid_MaxFD;		/* SQUID_MAXFD */
+extern int SQUID_NUMFD;		/* SQUID_MAXFD */
 extern int config_lineno;	/* 0 */
 extern int debugLevels[MAX_DEBUG_SECTIONS];
 extern int do_mallinfo;		/* 0 */
--- squid-2.6.STABLE1/src/stat.c.fd	2006-07-18 10:09:15.000000000 +0200
+++ squid-2.6.STABLE1/src/stat.c	2006-07-18 10:09:15.000000000 +0200
@@ -449,7 +449,7 @@
 	"Remote Address",
 	"Description");
     storeAppendPrintf(sentry, "---- ------ ---- -------- -------- --------------------- ------------------------------\n");
-    for (i = 0; i < Squid_MaxFD; i++) {
+    for (i = 0; i < SQUID_NUMFD; i++) {
 	f = &fd_table[i];
 	if (!f->flags.open)
 	    continue;
@@ -640,7 +640,7 @@
 
     storeAppendPrintf(sentry, "File descriptor usage for %s:\n", appname);
     storeAppendPrintf(sentry, "\tMaximum number of file descriptors:   %4d\n",
-	Squid_MaxFD);
+	SQUID_NUMFD);
     storeAppendPrintf(sentry, "\tLargest file desc currently in use:   %4d\n",
 	Biggest_FD);
     storeAppendPrintf(sentry, "\tNumber of file desc currently in use: %4d\n",
@@ -1031,7 +1031,7 @@
     statHistEnumInit(&C->comm_icp_incoming, INCOMING_ICP_MAX);
     statHistEnumInit(&C->comm_dns_incoming, INCOMING_DNS_MAX);
     statHistEnumInit(&C->comm_http_incoming, INCOMING_HTTP_MAX);
-    statHistIntInit(&C->select_fds_hist, 256);	/* was SQUID_MAXFD, but it is way too much. It is OK to crop this statistics */
+    statHistIntInit(&C->select_fds_hist, 256);	/* was SQUID_NUMFD, but it is way too much. It is OK to crop this statistics */
 }
 
 /* add special cases here as they arrive */

Reply via email to