]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fd.cc
Merged from parent (trunk r11240, circa 3.2.0.5+)
[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 #ifdef _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 if (F->type == FD_FILE) {
109 assert(F->read_handler == NULL);
110 assert(F->write_handler == NULL);
111 }
112
113 debugs(51, 3, "fd_close FD " << fd << " " << F->desc);
114 Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
115 Comm::SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
116 F->flags.open = 0;
117 fdUpdateBiggest(fd, 0);
118 Number_FD--;
119 *F = fde();
120 }
121
122 #ifdef _SQUID_MSWIN_
123
124 int
125 socket_read_method(int fd, char *buf, int len)
126 {
127 int i;
128 PROF_start(recv);
129 i = recv(fd, (void *) buf, len, 0);
130 PROF_stop(recv);
131 return i;
132 }
133
134 int
135 file_read_method(int fd, char *buf, int len)
136 {
137 int i;
138 PROF_start(read);
139 i = _read(fd, buf, len);
140 PROF_stop(read);
141 return i;
142 }
143
144 int
145 socket_write_method(int fd, const char *buf, int len)
146 {
147 int i;
148 PROF_start(send);
149 i = send(fd, (const void *) buf, len, 0);
150 PROF_stop(send);
151 return i;
152 }
153
154 int
155 file_write_method(int fd, const char *buf, int len)
156 {
157 int i;
158 PROF_start(write);
159 i = (_write(fd, buf, len));
160 PROF_stop(write);
161 return i;
162 }
163
164 #else
165 int
166 default_read_method(int fd, char *buf, int len)
167 {
168 int i;
169 PROF_start(read);
170 i = read(fd, buf, len);
171 PROF_stop(read);
172 return i;
173 }
174
175 int
176 default_write_method(int fd, const char *buf, int len)
177 {
178 int i;
179 PROF_start(write);
180 i = write(fd, buf, len);
181 PROF_stop(write);
182 return i;
183 }
184
185 int
186 msghdr_read_method(int fd, char *buf, int len)
187 {
188 PROF_start(read);
189 const int i = recvmsg(fd, reinterpret_cast<msghdr*>(buf), MSG_DONTWAIT);
190 PROF_stop(read);
191 return i;
192 }
193
194 int
195 msghdr_write_method(int fd, const char *buf, int len)
196 {
197 PROF_start(write);
198 const int i = sendmsg(fd, reinterpret_cast<const msghdr*>(buf), MSG_NOSIGNAL);
199 PROF_stop(write);
200 return i > 0 ? len : i; // len is imprecise but the caller expects a match
201 }
202
203 #endif
204
205 void
206 fd_open(int fd, unsigned int type, const char *desc)
207 {
208 fde *F;
209 assert(fd >= 0);
210 F = &fd_table[fd];
211
212 if (F->flags.open) {
213 debugs(51, 1, "WARNING: Closing open FD " << std::setw(4) << fd);
214 fd_close(fd);
215 }
216
217 assert(!F->flags.open);
218 debugs(51, 3, "fd_open() FD " << fd << " " << desc);
219 F->type = type;
220 F->flags.open = 1;
221 F->epoll_state = 0;
222 #ifdef _SQUID_MSWIN_
223
224 F->win32.handle = _get_osfhandle(fd);
225
226 switch (type) {
227
228 case FD_SOCKET:
229
230 case FD_PIPE:
231 F->read_method = &socket_read_method;
232 F->write_method = &socket_write_method;
233 break;
234
235 case FD_FILE:
236
237 case FD_LOG:
238 F->read_method = &file_read_method;
239 F->write_method = &file_write_method;
240 break;
241
242 default:
243 fatalf("fd_open(): unknown FD type - FD#: %i, type: %u, desc %s\n", fd, type, desc);
244 }
245
246 #else
247 switch (type) {
248
249 case FD_MSGHDR:
250 F->read_method = &msghdr_read_method;
251 F->write_method = &msghdr_write_method;
252 break;
253
254 default:
255 F->read_method = &default_read_method;
256 F->write_method = &default_write_method;
257 break;
258 }
259
260 #endif
261
262 fdUpdateBiggest(fd, 1);
263
264 if (desc)
265 xstrncpy(F->desc, desc, FD_DESC_SZ);
266
267 Number_FD++;
268 }
269
270 void
271 fd_note(int fd, const char *s)
272 {
273 fde *F = &fd_table[fd];
274 xstrncpy(F->desc, s, FD_DESC_SZ);
275 }
276
277 void
278 fd_bytes(int fd, int len, unsigned int type)
279 {
280 fde *F = &fd_table[fd];
281
282 if (len < 0)
283 return;
284
285 assert(type == FD_READ || type == FD_WRITE);
286
287 if (type == FD_READ)
288 F->bytes_read += len;
289 else
290 F->bytes_written += len;
291 }
292
293 void
294 fdDumpOpen(void)
295 {
296 int i;
297 fde *F;
298
299 for (i = 0; i < Squid_MaxFD; i++) {
300 F = &fd_table[i];
301
302 if (!F->flags.open)
303 continue;
304
305 if (i == fileno(debug_log))
306 continue;
307
308 debugs(51, 1, "Open FD "<< std::left<< std::setw(10) <<
309 (F->bytes_read && F->bytes_written ? "READ/WRITE" :
310 F->bytes_read ? "READING" : F->bytes_written ? "WRITING" :
311 "UNSTARTED") <<
312 " "<< std::right << std::setw(4) << i << " " << F->desc);
313 }
314 }
315
316 int
317 fdNFree(void)
318 {
319 return Squid_MaxFD - Number_FD - Opening_FD;
320 }
321
322 int
323 fdUsageHigh(void)
324 {
325 int nrfree = fdNFree();
326
327 if (nrfree < (RESERVED_FD << 1))
328 return 1;
329
330 if (nrfree < (Number_FD >> 2))
331 return 1;
332
333 return 0;
334 }
335
336 /* Called when we runs out of file descriptors */
337 void
338 fdAdjustReserved(void)
339 {
340 int newReserve;
341 int x;
342 static time_t last = 0;
343 /*
344 * don't update too frequently
345 */
346
347 if (last + 5 > squid_curtime)
348 return;
349
350 /*
351 * Calculate a new reserve, based on current usage and a small extra
352 */
353 newReserve = Squid_MaxFD - Number_FD + min(25, Squid_MaxFD / 16);
354
355 if (newReserve <= RESERVED_FD)
356 return;
357
358 x = Squid_MaxFD - 20 - min(25, Squid_MaxFD / 16);
359
360 if (newReserve > x) {
361 /* perhaps this should be fatal()? -DW */
362 debugs(51, 0, "WARNING: This machine has a serious shortage of filedescriptors.");
363 newReserve = x;
364 }
365
366 if (Squid_MaxFD - newReserve < min(256, Squid_MaxFD / 2))
367 fatalf("Too few filedescriptors available in the system (%d usable of %d).\n", Squid_MaxFD - newReserve, Squid_MaxFD);
368
369 debugs(51, 0, "Reserved FD adjusted from " << RESERVED_FD << " to " << newReserve << " due to failures");
370 RESERVED_FD = newReserve;
371 }