]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fd.cc
36a26b8adfa9d8a40e63075e2af5098c86c00f2f
[thirdparty/squid.git] / src / fd.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 51 Filedescriptor Functions
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
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
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37 #include "comm/Loops.h"
38 #include "fde.h"
39 #include "SquidTime.h"
40 #include "Debug.h"
41
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
50 int default_read_method(int, char *, int);
51 int default_write_method(int, const char *, int);
52 #if _SQUID_MSWIN_
53 int socket_read_method(int, char *, int);
54 int socket_write_method(int, const char *, int);
55 int file_read_method(int, char *, int);
56 int file_write_method(int, const char *, int);
57 #else
58 int msghdr_read_method(int, char *, int);
59 int msghdr_write_method(int, const char *, int);
60 #endif
61
62 const char *fdTypeStr[] = {
63 "None",
64 "Log",
65 "File",
66 "Socket",
67 "Pipe",
68 "MsgHdr",
69 "Unknown"
70 };
71
72 static void fdUpdateBiggest(int fd, int);
73
74 static void
75 fdUpdateBiggest(int fd, int opening)
76 {
77 if (fd < Biggest_FD)
78 return;
79
80 assert(fd < Squid_MaxFD);
81
82 if (fd > Biggest_FD) {
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;
90 }
91
92 /* if we are here, then fd == Biggest_FD */
93 /*
94 * assert that we are closing the biggest FD; we can't be
95 * re-opening it
96 */
97 assert(!opening);
98
99 while (Biggest_FD >= 0 && !fd_table[Biggest_FD].flags.open)
100 Biggest_FD--;
101 }
102
103 void
104 fd_close(int fd)
105 {
106 fde *F = &fd_table[fd];
107
108 assert(fd >= 0);
109 assert(F->flags.open == 1);
110
111 if (F->type == FD_FILE) {
112 assert(F->read_handler == NULL);
113 assert(F->write_handler == NULL);
114 }
115
116 debugs(51, 3, "fd_close FD " << fd << " " << F->desc);
117 Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
118 Comm::SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
119 F->flags.open = 0;
120 fdUpdateBiggest(fd, 0);
121 Number_FD--;
122 *F = fde();
123 }
124
125 #if _SQUID_MSWIN_
126
127 int
128 socket_read_method(int fd, char *buf, int len)
129 {
130 int i;
131 PROF_start(recv);
132 i = recv(fd, (void *) buf, len, 0);
133 PROF_stop(recv);
134 return i;
135 }
136
137 int
138 file_read_method(int fd, char *buf, int len)
139 {
140 int i;
141 PROF_start(read);
142 i = _read(fd, buf, len);
143 PROF_stop(read);
144 return i;
145 }
146
147 int
148 socket_write_method(int fd, const char *buf, int len)
149 {
150 int i;
151 PROF_start(send);
152 i = send(fd, (const void *) buf, len, 0);
153 PROF_stop(send);
154 return i;
155 }
156
157 int
158 file_write_method(int fd, const char *buf, int len)
159 {
160 int i;
161 PROF_start(write);
162 i = (_write(fd, buf, len));
163 PROF_stop(write);
164 return i;
165 }
166
167 #else
168 int
169 default_read_method(int fd, char *buf, int len)
170 {
171 int i;
172 PROF_start(read);
173 i = read(fd, buf, len);
174 PROF_stop(read);
175 return i;
176 }
177
178 int
179 default_write_method(int fd, const char *buf, int len)
180 {
181 int i;
182 PROF_start(write);
183 i = write(fd, buf, len);
184 PROF_stop(write);
185 return i;
186 }
187
188 int
189 msghdr_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
197 int
198 msghdr_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
206 #endif
207
208 void
209 fd_open(int fd, unsigned int type, const char *desc)
210 {
211 fde *F;
212 assert(fd >= 0);
213 F = &fd_table[fd];
214
215 if (F->flags.open) {
216 debugs(51, 1, "WARNING: Closing open FD " << std::setw(4) << fd);
217 fd_close(fd);
218 }
219
220 assert(!F->flags.open);
221 debugs(51, 3, "fd_open() FD " << fd << " " << desc);
222 F->type = type;
223 F->flags.open = 1;
224 F->epoll_state = 0;
225 #if _SQUID_MSWIN_
226
227 F->win32.handle = _get_osfhandle(fd);
228
229 switch (type) {
230
231 case FD_SOCKET:
232
233 case FD_PIPE:
234 F->read_method = &socket_read_method;
235 F->write_method = &socket_write_method;
236 break;
237
238 case FD_FILE:
239
240 case FD_LOG:
241 F->read_method = &file_read_method;
242 F->write_method = &file_write_method;
243 break;
244
245 default:
246 fatalf("fd_open(): unknown FD type - FD#: %i, type: %u, desc %s\n", fd, type, desc);
247 }
248
249 #else
250 switch (type) {
251
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 }
262
263 #endif
264
265 fdUpdateBiggest(fd, 1);
266
267 if (desc)
268 xstrncpy(F->desc, desc, FD_DESC_SZ);
269
270 Number_FD++;
271 }
272
273 void
274 fd_note(int fd, const char *s)
275 {
276 fde *F = &fd_table[fd];
277 xstrncpy(F->desc, s, FD_DESC_SZ);
278 }
279
280 void
281 fd_bytes(int fd, int len, unsigned int type)
282 {
283 fde *F = &fd_table[fd];
284
285 if (len < 0)
286 return;
287
288 assert(type == FD_READ || type == FD_WRITE);
289
290 if (type == FD_READ)
291 F->bytes_read += len;
292 else
293 F->bytes_written += len;
294 }
295
296 void
297 fdDumpOpen(void)
298 {
299 int i;
300 fde *F;
301
302 for (i = 0; i < Squid_MaxFD; i++) {
303 F = &fd_table[i];
304
305 if (!F->flags.open)
306 continue;
307
308 if (i == fileno(debug_log))
309 continue;
310
311 debugs(51, 1, "Open FD "<< std::left<< std::setw(10) <<
312 (F->bytes_read && F->bytes_written ? "READ/WRITE" :
313 F->bytes_read ? "READING" : F->bytes_written ? "WRITING" :
314 "UNSTARTED") <<
315 " "<< std::right << std::setw(4) << i << " " << F->desc);
316 }
317 }
318
319 int
320 fdNFree(void)
321 {
322 return Squid_MaxFD - Number_FD - Opening_FD;
323 }
324
325 int
326 fdUsageHigh(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
339 /* Called when we runs out of file descriptors */
340 void
341 fdAdjustReserved(void)
342 {
343 int newReserve;
344 int x;
345 static time_t last = 0;
346 /*
347 * don't update too frequently
348 */
349
350 if (last + 5 > squid_curtime)
351 return;
352
353 /*
354 * Calculate a new reserve, based on current usage and a small extra
355 */
356 newReserve = Squid_MaxFD - Number_FD + min(25, Squid_MaxFD / 16);
357
358 if (newReserve <= RESERVED_FD)
359 return;
360
361 x = Squid_MaxFD - 20 - min(25, Squid_MaxFD / 16);
362
363 if (newReserve > x) {
364 /* perhaps this should be fatal()? -DW */
365 debugs(51, 0, "WARNING: This machine has a serious shortage of filedescriptors.");
366 newReserve = x;
367 }
368
369 if (Squid_MaxFD - newReserve < min(256, Squid_MaxFD / 2))
370 fatalf("Too few filedescriptors available in the system (%d usable of %d).\n", Squid_MaxFD - newReserve, Squid_MaxFD);
371
372 debugs(51, 0, "Reserved FD adjusted from " << RESERVED_FD << " to " << newReserve << " due to failures");
373 RESERVED_FD = newReserve;
374 }