]> git.ipfire.org Git - thirdparty/squid.git/blame - src/fd.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / fd.cc
CommitLineData
be335c22 1
d892b155 2/*
262a0e14 3 * $Id$
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.
26ac0430 24 *
d892b155 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.
26ac0430 29 *
d892b155 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
f7f3304a 36#include "squid-old.h"
d841c88d 37#include "comm/Loops.h"
528b2c61 38#include "fde.h"
985c86bc 39#include "SquidTime.h"
f76d2f97 40#include "Debug.h"
a7870688 41
2a857320
AR
42
43// Solaris and possibly others lack MSG_NOSIGNAL optimization
44// TODO: move this into compat/? Use a dedicated compat file to avoid dragging
45// sys/types.h and sys/socket.h into the rest of Squid??
46#ifndef MSG_NOSIGNAL
47#define MSG_NOSIGNAL 0
48#endif
49
1f7c9178 50int default_read_method(int, char *, int);
51int default_write_method(int, const char *, int);
1191b93b 52#if _SQUID_MSWIN_
0f15e632 53int socket_read_method(int, char *, int);
54int socket_write_method(int, const char *, int);
55int file_read_method(int, char *, int);
56int file_write_method(int, const char *, int);
1bac0258
AR
57#else
58int msghdr_read_method(int, char *, int);
59int msghdr_write_method(int, const char *, int);
0f15e632 60#endif
1f7c9178 61
26ac0430
AJ
62const char *fdTypeStr[] = {
63 "None",
64 "Log",
65 "File",
66 "Socket",
67 "Pipe",
1bac0258 68 "MsgHdr",
26ac0430
AJ
69 "Unknown"
70};
22f5d1ca 71
60c0b5a2 72static void fdUpdateBiggest(int fd, int);
a7870688 73
74static void
60c0b5a2 75fdUpdateBiggest(int fd, int opening)
a7870688 76{
77 if (fd < Biggest_FD)
62e76326 78 return;
79
edd1e84c 80 assert(fd < Squid_MaxFD);
62e76326 81
a7870688 82 if (fd > Biggest_FD) {
62e76326 83 /*
84 * assert that we are not closing a FD bigger than
85 * our known biggest FD
86 */
87 assert(opening);
88 Biggest_FD = fd;
89 return;
a7870688 90 }
62e76326 91
a7870688 92 /* if we are here, then fd == Biggest_FD */
60c0b5a2 93 /*
94 * assert that we are closing the biggest FD; we can't be
95 * re-opening it
96 */
97 assert(!opening);
62e76326 98
a8214fc7 99 while (Biggest_FD >= 0 && !fd_table[Biggest_FD].flags.open)
62e76326 100 Biggest_FD--;
a7870688 101}
102
103void
4f92c80c 104fd_close(int fd)
a7870688 105{
76f87348 106 fde *F = &fd_table[fd];
62e76326 107
78611e17
AJ
108 assert(fd >= 0);
109 assert(F->flags.open == 1);
110
76f87348 111 if (F->type == FD_FILE) {
62e76326 112 assert(F->read_handler == NULL);
113 assert(F->write_handler == NULL);
9e4ad609 114 }
62e76326 115
bf8fe701 116 debugs(51, 3, "fd_close FD " << fd << " " << F->desc);
d841c88d
AJ
117 Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
118 Comm::SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
60c0b5a2 119 F->flags.open = 0;
120 fdUpdateBiggest(fd, 0);
2524a4d5 121 Number_FD--;
bf81adb4 122 *F = fde();
a7870688 123}
124
1191b93b 125#if _SQUID_MSWIN_
0f15e632 126
127int
128socket_read_method(int fd, char *buf, int len)
129{
fe86a3bf 130 int i;
0242b721 131 PROF_start(recv);
fe86a3bf 132 i = recv(fd, (void *) buf, len, 0);
0242b721 133 PROF_stop(recv);
fe86a3bf 134 return i;
0f15e632 135}
136
137int
138file_read_method(int fd, char *buf, int len)
139{
fe86a3bf 140 int i;
141 PROF_start(read);
ce8db2ee 142 i = _read(fd, buf, len);
fe86a3bf 143 PROF_stop(read);
144 return i;
0f15e632 145}
146
147int
148socket_write_method(int fd, const char *buf, int len)
149{
fe86a3bf 150 int i;
151 PROF_start(send);
152 i = send(fd, (const void *) buf, len, 0);
153 PROF_stop(send);
154 return i;
0f15e632 155}
156
157int
158file_write_method(int fd, const char *buf, int len)
159{
0242b721 160 int i;
161 PROF_start(write);
162 i = (_write(fd, buf, len));
163 PROF_stop(write);
164 return i;
0f15e632 165}
62e76326 166
0f15e632 167#else
1f7c9178 168int
169default_read_method(int fd, char *buf, int len)
170{
fe86a3bf 171 int i;
172 PROF_start(read);
173 i = read(fd, buf, len);
174 PROF_stop(read);
175 return i;
1f7c9178 176}
177
178int
179default_write_method(int fd, const char *buf, int len)
180{
fe86a3bf 181 int i;
182 PROF_start(write);
183 i = write(fd, buf, len);
184 PROF_stop(write);
185 return i;
1f7c9178 186}
62e76326 187
1bac0258
AR
188int
189msghdr_read_method(int fd, char *buf, int len)
190{
191 PROF_start(read);
192 const int i = recvmsg(fd, reinterpret_cast<msghdr*>(buf), MSG_DONTWAIT);
193 PROF_stop(read);
194 return i;
195}
196
197int
198msghdr_write_method(int fd, const char *buf, int len)
199{
200 PROF_start(write);
201 const int i = sendmsg(fd, reinterpret_cast<const msghdr*>(buf), MSG_NOSIGNAL);
202 PROF_stop(write);
203 return i > 0 ? len : i; // len is imprecise but the caller expects a match
204}
205
0f15e632 206#endif
1f7c9178 207
a7870688 208void
209fd_open(int fd, unsigned int type, const char *desc)
210{
4bc3a4cd 211 fde *F;
ceab4c00 212 assert(fd >= 0);
4bc3a4cd 213 F = &fd_table[fd];
62e76326 214
7197b20d 215 if (F->flags.open) {
bf8fe701 216 debugs(51, 1, "WARNING: Closing open FD " << std::setw(4) << fd);
62e76326 217 fd_close(fd);
6cf028ab 218 }
62e76326 219
60c0b5a2 220 assert(!F->flags.open);
cc192b50 221 debugs(51, 3, "fd_open() FD " << fd << " " << desc);
76f87348 222 F->type = type;
60c0b5a2 223 F->flags.open = 1;
751406fe 224 F->epoll_state = 0;
1191b93b 225#if _SQUID_MSWIN_
62e76326 226
629b5f75 227 F->win32.handle = _get_osfhandle(fd);
228
0f15e632 229 switch (type) {
62e76326 230
0f15e632 231 case FD_SOCKET:
62e76326 232
0f15e632 233 case FD_PIPE:
62e76326 234 F->read_method = &socket_read_method;
235 F->write_method = &socket_write_method;
236 break;
237
0f15e632 238 case FD_FILE:
62e76326 239
0f15e632 240 case FD_LOG:
62e76326 241 F->read_method = &file_read_method;
242 F->write_method = &file_write_method;
243 break;
244
0f15e632 245 default:
62e76326 246 fatalf("fd_open(): unknown FD type - FD#: %i, type: %u, desc %s\n", fd, type, desc);
0f15e632 247 }
62e76326 248
0f15e632 249#else
1bac0258 250 switch (type) {
62e76326 251
1bac0258
AR
252 case FD_MSGHDR:
253 F->read_method = &msghdr_read_method;
254 F->write_method = &msghdr_write_method;
255 break;
256
257 default:
258 F->read_method = &default_read_method;
259 F->write_method = &default_write_method;
260 break;
261 }
62e76326 262
0f15e632 263#endif
62e76326 264
60c0b5a2 265 fdUpdateBiggest(fd, 1);
62e76326 266
a7870688 267 if (desc)
62e76326 268 xstrncpy(F->desc, desc, FD_DESC_SZ);
269
2524a4d5 270 Number_FD++;
a7870688 271}
272
273void
274fd_note(int fd, const char *s)
275{
76f87348 276 fde *F = &fd_table[fd];
277 xstrncpy(F->desc, s, FD_DESC_SZ);
a7870688 278}
4f92c80c 279
280void
281fd_bytes(int fd, int len, unsigned int type)
282{
76f87348 283 fde *F = &fd_table[fd];
62e76326 284
4f92c80c 285 if (len < 0)
62e76326 286 return;
287
365e5b34 288 assert(type == FD_READ || type == FD_WRITE);
62e76326 289
4f92c80c 290 if (type == FD_READ)
62e76326 291 F->bytes_read += len;
4f92c80c 292 else
62e76326 293 F->bytes_written += len;
4f92c80c 294}
f17936ab 295
9e4ad609 296void
297fdDumpOpen(void)
298{
299 int i;
76f87348 300 fde *F;
62e76326 301
9e4ad609 302 for (i = 0; i < Squid_MaxFD; i++) {
62e76326 303 F = &fd_table[i];
304
305 if (!F->flags.open)
306 continue;
307
308 if (i == fileno(debug_log))
309 continue;
310
26ac0430 311 debugs(51, 1, "Open FD "<< std::left<< std::setw(10) <<
bf8fe701 312 (F->bytes_read && F->bytes_written ? "READ/WRITE" :
26ac0430 313 F->bytes_read ? "READING" : F->bytes_written ? "WRITING" :
86905e1f 314 "UNSTARTED") <<
bf8fe701 315 " "<< std::right << std::setw(4) << i << " " << F->desc);
9e4ad609 316 }
f17936ab 317}
22f5d1ca 318
319int
320fdNFree(void)
321{
b6a2f15e 322 return Squid_MaxFD - Number_FD - Opening_FD;
22f5d1ca 323}
4b23e114 324
a1ca9253 325int
326fdUsageHigh(void)
327{
328 int nrfree = fdNFree();
329
330 if (nrfree < (RESERVED_FD << 1))
331 return 1;
332
333 if (nrfree < (Number_FD >> 2))
334 return 1;
335
336 return 0;
337}
338
4b23e114 339/* Called when we runs out of file descriptors */
340void
341fdAdjustReserved(void)
342{
e6ccf245 343 int newReserve;
4b23e114 344 int x;
345 static time_t last = 0;
346 /*
347 * don't update too frequently
348 */
62e76326 349
4b23e114 350 if (last + 5 > squid_curtime)
62e76326 351 return;
352
4b23e114 353 /*
354 * Calculate a new reserve, based on current usage and a small extra
355 */
d85c3078 356 newReserve = Squid_MaxFD - Number_FD + min(25, Squid_MaxFD / 16);
62e76326 357
e6ccf245 358 if (newReserve <= RESERVED_FD)
62e76326 359 return;
360
d85c3078 361 x = Squid_MaxFD - 20 - min(25, Squid_MaxFD / 16);
62e76326 362
e6ccf245 363 if (newReserve > x) {
62e76326 364 /* perhaps this should be fatal()? -DW */
bf8fe701 365 debugs(51, 0, "WARNING: This machine has a serious shortage of filedescriptors.");
62e76326 366 newReserve = x;
4b23e114 367 }
62e76326 368
d85c3078 369 if (Squid_MaxFD - newReserve < min(256, Squid_MaxFD / 2))
26ac0430 370 fatalf("Too few filedescriptors available in the system (%d usable of %d).\n", Squid_MaxFD - newReserve, Squid_MaxFD);
375295eb 371
bf8fe701 372 debugs(51, 0, "Reserved FD adjusted from " << RESERVED_FD << " to " << newReserve << " due to failures");
e6ccf245 373 RESERVED_FD = newReserve;
4b23e114 374}