]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/fd.cc
3 * $Id: fd.cc,v 1.57 2007/04/28 22:26:37 hno Exp $
5 * DEBUG: section 51 Filedescriptor Functions
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
38 #include "SquidTime.h"
40 int default_read_method(int, char *, int);
41 int default_write_method(int, const char *, int);
43 int socket_read_method(int, char *, int);
44 int socket_write_method(int, const char *, int);
45 int file_read_method(int, char *, int);
46 int file_write_method(int, const char *, int);
49 const char *fdTypeStr
[] =
59 static void fdUpdateBiggest(int fd
, int);
62 fdUpdateBiggest(int fd
, int opening
)
67 assert(fd
< Squid_MaxFD
);
69 if (fd
> Biggest_FD
) {
71 * assert that we are not closing a FD bigger than
72 * our known biggest FD
79 /* if we are here, then fd == Biggest_FD */
81 * assert that we are closing the biggest FD; we can't be
86 while (Biggest_FD
>= 0 && !fd_table
[Biggest_FD
].flags
.open
)
93 fde
*F
= &fd_table
[fd
];
95 if (F
->type
== FD_FILE
) {
96 assert(F
->read_handler
== NULL
);
97 assert(F
->write_handler
== NULL
);
100 debugs(51, 3, "fd_close FD " << fd
<< " " << F
->desc
);
101 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
102 commSetSelect(fd
, COMM_SELECT_WRITE
, NULL
, NULL
, 0);
104 fdUpdateBiggest(fd
, 0);
106 memset(F
, '\0', sizeof(fde
));
113 socket_read_method(int fd
, char *buf
, int len
)
117 i
= recv(fd
, (void *) buf
, len
, 0);
123 file_read_method(int fd
, char *buf
, int len
)
127 i
= _read(fd
, buf
, len
);
133 socket_write_method(int fd
, const char *buf
, int len
)
137 i
= send(fd
, (const void *) buf
, len
, 0);
143 file_write_method(int fd
, const char *buf
, int len
)
145 return (_write(fd
, buf
, len
));
150 default_read_method(int fd
, char *buf
, int len
)
154 i
= read(fd
, buf
, len
);
160 default_write_method(int fd
, const char *buf
, int len
)
164 i
= write(fd
, buf
, len
);
172 fd_open(int fd
, unsigned int type
, const char *desc
)
179 debugs(51, 1, "WARNING: Closing open FD " << std::setw(4) << fd
);
183 assert(!F
->flags
.open
);
184 debugs(51, 3, "fd_open FD " << fd
<< " " << desc
);
190 F
->win32
.handle
= _get_osfhandle(fd
);
197 F
->read_method
= &socket_read_method
;
198 F
->write_method
= &socket_write_method
;
204 F
->read_method
= &file_read_method
;
205 F
->write_method
= &file_write_method
;
209 fatalf("fd_open(): unknown FD type - FD#: %i, type: %u, desc %s\n", fd
, type
, desc
);
213 F
->read_method
= &default_read_method
;
215 F
->write_method
= &default_write_method
;
219 fdUpdateBiggest(fd
, 1);
222 xstrncpy(F
->desc
, desc
, FD_DESC_SZ
);
228 fd_note(int fd
, const char *s
)
230 fde
*F
= &fd_table
[fd
];
231 xstrncpy(F
->desc
, s
, FD_DESC_SZ
);
235 fd_bytes(int fd
, int len
, unsigned int type
)
237 fde
*F
= &fd_table
[fd
];
242 assert(type
== FD_READ
|| type
== FD_WRITE
);
245 F
->bytes_read
+= len
;
247 F
->bytes_written
+= len
;
262 for (i
= 0; i
< Squid_MaxFD
; i
++) {
268 if (i
== fileno(debug_log
))
271 debugs(51, 1, "Open FD "<< std::left
<< std::setw(10) <<
272 (F
->bytes_read
&& F
->bytes_written
? "READ/WRITE" :
273 F
->bytes_read
? "READING" : F
->bytes_written
? "WRITING" :
275 " "<< std::right
<< std::setw(4) << i
<< " " << F
->desc
);
282 return Squid_MaxFD
- Number_FD
- Opening_FD
;
288 int nrfree
= fdNFree();
290 if (nrfree
< (RESERVED_FD
<< 1))
293 if (nrfree
< (Number_FD
>> 2))
299 /* Called when we runs out of file descriptors */
301 fdAdjustReserved(void)
305 static time_t last
= 0;
307 * don't update too frequently
310 if (last
+ 5 > squid_curtime
)
314 * Calculate a new reserve, based on current usage and a small extra
316 newReserve
= Squid_MaxFD
- Number_FD
+ XMIN(25, Squid_MaxFD
/ 16);
318 if (newReserve
<= RESERVED_FD
)
321 x
= Squid_MaxFD
- 20 - XMIN(25, Squid_MaxFD
/ 16);
323 if (newReserve
> x
) {
324 /* perhaps this should be fatal()? -DW */
325 debugs(51, 0, "WARNING: This machine has a serious shortage of filedescriptors.");
329 debugs(51, 0, "Reserved FD adjusted from " << RESERVED_FD
<< " to " << newReserve
<< " due to failures");
330 RESERVED_FD
= newReserve
;