]> git.ipfire.org Git - thirdparty/squid.git/blame - src/fd.cc
Adding a comment that tests/testUfs will fail when 'heap' is the only
[thirdparty/squid.git] / src / fd.cc
CommitLineData
be335c22 1
d892b155 2/*
ce8db2ee 3 * $Id: fd.cc,v 1.56 2007/04/06 15:39:06 serassio Exp $
d892b155 4 *
5 * DEBUG: section 51 Filedescriptor Functions
6 * AUTHOR: Duane Wessels
7 *
2b6662ba 8 * SQUID Web Proxy Cache http://www.squid-cache.org/
e25c139f 9 * ----------------------------------------------------------
d892b155 10 *
2b6662ba 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.
d892b155 19 *
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.
24 *
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.
29 *
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
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
d892b155 34 */
35
a7870688 36#include "squid.h"
528b2c61 37#include "fde.h"
985c86bc 38#include "SquidTime.h"
a7870688 39
1f7c9178 40int default_read_method(int, char *, int);
41int default_write_method(int, const char *, int);
0f15e632 42#ifdef _SQUID_MSWIN_
43int socket_read_method(int, char *, int);
44int socket_write_method(int, const char *, int);
45int file_read_method(int, char *, int);
46int file_write_method(int, const char *, int);
47#endif
1f7c9178 48
22f5d1ca 49const char *fdTypeStr[] =
62e76326 50 {
51 "None",
52 "Log",
53 "File",
54 "Socket",
55 "Pipe",
56 "Unknown"
57 };
22f5d1ca 58
60c0b5a2 59static void fdUpdateBiggest(int fd, int);
a7870688 60
61static void
60c0b5a2 62fdUpdateBiggest(int fd, int opening)
a7870688 63{
64 if (fd < Biggest_FD)
62e76326 65 return;
66
edd1e84c 67 assert(fd < Squid_MaxFD);
62e76326 68
a7870688 69 if (fd > Biggest_FD) {
62e76326 70 /*
71 * assert that we are not closing a FD bigger than
72 * our known biggest FD
73 */
74 assert(opening);
75 Biggest_FD = fd;
76 return;
a7870688 77 }
62e76326 78
a7870688 79 /* if we are here, then fd == Biggest_FD */
60c0b5a2 80 /*
81 * assert that we are closing the biggest FD; we can't be
82 * re-opening it
83 */
84 assert(!opening);
62e76326 85
a8214fc7 86 while (Biggest_FD >= 0 && !fd_table[Biggest_FD].flags.open)
62e76326 87 Biggest_FD--;
a7870688 88}
89
90void
4f92c80c 91fd_close(int fd)
a7870688 92{
76f87348 93 fde *F = &fd_table[fd];
62e76326 94
76f87348 95 if (F->type == FD_FILE) {
62e76326 96 assert(F->read_handler == NULL);
97 assert(F->write_handler == NULL);
9e4ad609 98 }
62e76326 99
d8326d6b 100 debug(51, 3) ("fd_close FD %d %s\n", fd, F->desc);
1b3db6d9 101 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
102 commSetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
60c0b5a2 103 F->flags.open = 0;
104 fdUpdateBiggest(fd, 0);
2524a4d5 105 Number_FD--;
76f87348 106 memset(F, '\0', sizeof(fde));
107 F->timeout = 0;
a7870688 108}
109
0f15e632 110#ifdef _SQUID_MSWIN_
111
112int
113socket_read_method(int fd, char *buf, int len)
114{
fe86a3bf 115 int i;
116 PROF_start(send);
117 i = recv(fd, (void *) buf, len, 0);
118 PROF_stop(send);
119 return i;
0f15e632 120}
121
122int
123file_read_method(int fd, char *buf, int len)
124{
fe86a3bf 125 int i;
126 PROF_start(read);
ce8db2ee 127 i = _read(fd, buf, len);
fe86a3bf 128 PROF_stop(read);
129 return i;
0f15e632 130}
131
132int
133socket_write_method(int fd, const char *buf, int len)
134{
fe86a3bf 135 int i;
136 PROF_start(send);
137 i = send(fd, (const void *) buf, len, 0);
138 PROF_stop(send);
139 return i;
0f15e632 140}
141
142int
143file_write_method(int fd, const char *buf, int len)
144{
145 return (_write(fd, buf, len));
146}
62e76326 147
0f15e632 148#else
1f7c9178 149int
150default_read_method(int fd, char *buf, int len)
151{
fe86a3bf 152 int i;
153 PROF_start(read);
154 i = read(fd, buf, len);
155 PROF_stop(read);
156 return i;
1f7c9178 157}
158
159int
160default_write_method(int fd, const char *buf, int len)
161{
fe86a3bf 162 int i;
163 PROF_start(write);
164 i = write(fd, buf, len);
165 PROF_stop(write);
166 return i;
1f7c9178 167}
62e76326 168
0f15e632 169#endif
1f7c9178 170
a7870688 171void
172fd_open(int fd, unsigned int type, const char *desc)
173{
4bc3a4cd 174 fde *F;
ceab4c00 175 assert(fd >= 0);
4bc3a4cd 176 F = &fd_table[fd];
62e76326 177
7197b20d 178 if (F->flags.open) {
62e76326 179 debug(51, 1) ("WARNING: Closing open FD %4d\n", fd);
180 fd_close(fd);
6cf028ab 181 }
62e76326 182
60c0b5a2 183 assert(!F->flags.open);
a7c05555 184 debug(51, 3) ("fd_open FD %d %s\n", fd, desc);
76f87348 185 F->type = type;
60c0b5a2 186 F->flags.open = 1;
751406fe 187 F->epoll_state = 0;
0f15e632 188#ifdef _SQUID_MSWIN_
62e76326 189
629b5f75 190 F->win32.handle = _get_osfhandle(fd);
191
0f15e632 192 switch (type) {
62e76326 193
0f15e632 194 case FD_SOCKET:
62e76326 195
0f15e632 196 case FD_PIPE:
62e76326 197 F->read_method = &socket_read_method;
198 F->write_method = &socket_write_method;
199 break;
200
0f15e632 201 case FD_FILE:
62e76326 202
0f15e632 203 case FD_LOG:
62e76326 204 F->read_method = &file_read_method;
205 F->write_method = &file_write_method;
206 break;
207
0f15e632 208 default:
62e76326 209 fatalf("fd_open(): unknown FD type - FD#: %i, type: %u, desc %s\n", fd, type, desc);
0f15e632 210 }
62e76326 211
0f15e632 212#else
1f7c9178 213 F->read_method = &default_read_method;
62e76326 214
1f7c9178 215 F->write_method = &default_write_method;
62e76326 216
0f15e632 217#endif
62e76326 218
60c0b5a2 219 fdUpdateBiggest(fd, 1);
62e76326 220
a7870688 221 if (desc)
62e76326 222 xstrncpy(F->desc, desc, FD_DESC_SZ);
223
2524a4d5 224 Number_FD++;
a7870688 225}
226
227void
228fd_note(int fd, const char *s)
229{
76f87348 230 fde *F = &fd_table[fd];
231 xstrncpy(F->desc, s, FD_DESC_SZ);
a7870688 232}
4f92c80c 233
234void
235fd_bytes(int fd, int len, unsigned int type)
236{
76f87348 237 fde *F = &fd_table[fd];
62e76326 238
4f92c80c 239 if (len < 0)
62e76326 240 return;
241
365e5b34 242 assert(type == FD_READ || type == FD_WRITE);
62e76326 243
4f92c80c 244 if (type == FD_READ)
62e76326 245 F->bytes_read += len;
4f92c80c 246 else
62e76326 247 F->bytes_written += len;
4f92c80c 248}
f17936ab 249
250void
251fdFreeMemory(void)
252{
9e4ad609 253 safe_free(fd_table);
254}
255
256void
257fdDumpOpen(void)
258{
259 int i;
76f87348 260 fde *F;
62e76326 261
9e4ad609 262 for (i = 0; i < Squid_MaxFD; i++) {
62e76326 263 F = &fd_table[i];
264
265 if (!F->flags.open)
266 continue;
267
268 if (i == fileno(debug_log))
269 continue;
270
271 debug(51, 1) ("Open FD %-10s %4d %s\n",
272 F->bytes_read && F->bytes_written ? "READ/WRITE" :
273 F->bytes_read ? "READING" :
274 F->bytes_written ? "WRITING" : null_string,
275 i, F->desc);
9e4ad609 276 }
f17936ab 277}
22f5d1ca 278
279int
280fdNFree(void)
281{
b6a2f15e 282 return Squid_MaxFD - Number_FD - Opening_FD;
22f5d1ca 283}
4b23e114 284
a1ca9253 285int
286fdUsageHigh(void)
287{
288 int nrfree = fdNFree();
289
290 if (nrfree < (RESERVED_FD << 1))
291 return 1;
292
293 if (nrfree < (Number_FD >> 2))
294 return 1;
295
296 return 0;
297}
298
4b23e114 299/* Called when we runs out of file descriptors */
300void
301fdAdjustReserved(void)
302{
e6ccf245 303 int newReserve;
4b23e114 304 int x;
305 static time_t last = 0;
306 /*
307 * don't update too frequently
308 */
62e76326 309
4b23e114 310 if (last + 5 > squid_curtime)
62e76326 311 return;
312
4b23e114 313 /*
314 * Calculate a new reserve, based on current usage and a small extra
315 */
e6ccf245 316 newReserve = Squid_MaxFD - Number_FD + XMIN(25, Squid_MaxFD / 16);
62e76326 317
e6ccf245 318 if (newReserve <= RESERVED_FD)
62e76326 319 return;
320
4b23e114 321 x = Squid_MaxFD - 20 - XMIN(25, Squid_MaxFD / 16);
62e76326 322
e6ccf245 323 if (newReserve > x) {
62e76326 324 /* perhaps this should be fatal()? -DW */
325 debug(51, 0) ("WARNING: This machine has a serious shortage of filedescriptors.\n");
326 newReserve = x;
4b23e114 327 }
62e76326 328
4b23e114 329 debug(51, 0) ("Reserved FD adjusted from %d to %d due to failures\n",
62e76326 330 RESERVED_FD, newReserve);
e6ccf245 331 RESERVED_FD = newReserve;
4b23e114 332}