]> git.ipfire.org Git - thirdparty/squid.git/blame - src/fd.cc
Again, fix bandwidth spikes seen when the origin server did not process
[thirdparty/squid.git] / src / fd.cc
CommitLineData
be335c22 1
d892b155 2/*
2b6662ba 3 * $Id: fd.cc,v 1.41 2001/01/12 00:37:17 wessels 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"
37
22f5d1ca 38const char *fdTypeStr[] =
39{
40 "None",
41 "Log",
42 "File",
43 "Socket",
44 "Pipe",
45 "Unknown"
46};
47
60c0b5a2 48static void fdUpdateBiggest(int fd, int);
a7870688 49
50static void
60c0b5a2 51fdUpdateBiggest(int fd, int opening)
a7870688 52{
53 if (fd < Biggest_FD)
54 return;
edd1e84c 55 assert(fd < Squid_MaxFD);
a7870688 56 if (fd > Biggest_FD) {
60c0b5a2 57 /*
58 * assert that we are not closing a FD bigger than
59 * our known biggest FD
60 */
61 assert(opening);
edd1e84c 62 Biggest_FD = fd;
a7870688 63 return;
64 }
65 /* if we are here, then fd == Biggest_FD */
60c0b5a2 66 /*
67 * assert that we are closing the biggest FD; we can't be
68 * re-opening it
69 */
70 assert(!opening);
71 while (!fd_table[Biggest_FD].flags.open)
a7870688 72 Biggest_FD--;
73}
74
75void
4f92c80c 76fd_close(int fd)
a7870688 77{
76f87348 78 fde *F = &fd_table[fd];
79 if (F->type == FD_FILE) {
80 assert(F->read_handler == NULL);
81 assert(F->write_handler == NULL);
9e4ad609 82 }
d8326d6b 83 debug(51, 3) ("fd_close FD %d %s\n", fd, F->desc);
60c0b5a2 84 F->flags.open = 0;
85 fdUpdateBiggest(fd, 0);
2524a4d5 86 Number_FD--;
d072e3a1 87 commUpdateReadBits(fd, NULL);
88 commUpdateWriteBits(fd, NULL);
76f87348 89 memset(F, '\0', sizeof(fde));
90 F->timeout = 0;
a7870688 91}
92
93void
94fd_open(int fd, unsigned int type, const char *desc)
95{
4bc3a4cd 96 fde *F;
ceab4c00 97 assert(fd >= 0);
4bc3a4cd 98 F = &fd_table[fd];
7197b20d 99 if (F->flags.open) {
6cf028ab 100 debug(51, 1) ("WARNING: Closing open FD %4d\n", fd);
101 fd_close(fd);
102 }
60c0b5a2 103 assert(!F->flags.open);
a7c05555 104 debug(51, 3) ("fd_open FD %d %s\n", fd, desc);
76f87348 105 F->type = type;
60c0b5a2 106 F->flags.open = 1;
107 fdUpdateBiggest(fd, 1);
a7870688 108 if (desc)
76f87348 109 xstrncpy(F->desc, desc, FD_DESC_SZ);
2524a4d5 110 Number_FD++;
a7870688 111}
112
113void
114fd_note(int fd, const char *s)
115{
76f87348 116 fde *F = &fd_table[fd];
117 xstrncpy(F->desc, s, FD_DESC_SZ);
a7870688 118}
4f92c80c 119
120void
121fd_bytes(int fd, int len, unsigned int type)
122{
76f87348 123 fde *F = &fd_table[fd];
4f92c80c 124 if (len < 0)
125 return;
365e5b34 126 assert(type == FD_READ || type == FD_WRITE);
4f92c80c 127 if (type == FD_READ)
76f87348 128 F->bytes_read += len;
4f92c80c 129 else
76f87348 130 F->bytes_written += len;
4f92c80c 131}
f17936ab 132
133void
134fdFreeMemory(void)
135{
9e4ad609 136 safe_free(fd_table);
137}
138
139void
140fdDumpOpen(void)
141{
142 int i;
76f87348 143 fde *F;
9e4ad609 144 for (i = 0; i < Squid_MaxFD; i++) {
76f87348 145 F = &fd_table[i];
60c0b5a2 146 if (!F->flags.open)
9e4ad609 147 continue;
148 if (i == fileno(debug_log))
149 continue;
7197b20d 150 debug(51, 1) ("Open FD %-10s %4d %s\n",
151 F->bytes_read && F->bytes_written ? "READ/WRITE" :
152 F->bytes_read ? "READING" :
153 F->bytes_written ? "WRITING" : null_string,
76e0060d 154 i, F->desc);
9e4ad609 155 }
f17936ab 156}
22f5d1ca 157
158int
159fdNFree(void)
160{
b6a2f15e 161 return Squid_MaxFD - Number_FD - Opening_FD;
22f5d1ca 162}
4b23e114 163
164/* Called when we runs out of file descriptors */
165void
166fdAdjustReserved(void)
167{
168 int new;
169 int x;
170 static time_t last = 0;
171 /*
172 * don't update too frequently
173 */
174 if (last + 5 > squid_curtime)
175 return;
176 /*
177 * Calculate a new reserve, based on current usage and a small extra
178 */
179 new = Squid_MaxFD - Number_FD + XMIN(25, Squid_MaxFD / 16);
180 if (new <= RESERVED_FD)
181 return;
182 x = Squid_MaxFD - 20 - XMIN(25, Squid_MaxFD / 16);
183 if (new > x) {
184 /* perhaps this should be fatal()? -DW */
185 debug(51, 0) ("WARNING: This machine has a serious shortage of filedescriptors.\n");
186 new = x;
187 }
188 debug(51, 0) ("Reserved FD adjusted from %d to %d due to failures\n",
189 RESERVED_FD, new);
190 RESERVED_FD = new;
191}