]>
Commit | Line | Data |
---|---|---|
be335c22 | 1 | |
d892b155 | 2 | /* |
cc192b50 | 3 | * $Id: fd.cc,v 1.60 2007/12/14 23:11:46 amosjeffries 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 | 40 | int default_read_method(int, char *, int); |
41 | int default_write_method(int, const char *, int); | |
0f15e632 | 42 | #ifdef _SQUID_MSWIN_ |
43 | int socket_read_method(int, char *, int); | |
44 | int socket_write_method(int, const char *, int); | |
45 | int file_read_method(int, char *, int); | |
46 | int file_write_method(int, const char *, int); | |
47 | #endif | |
1f7c9178 | 48 | |
22f5d1ca | 49 | const char *fdTypeStr[] = |
62e76326 | 50 | { |
51 | "None", | |
52 | "Log", | |
53 | "File", | |
54 | "Socket", | |
55 | "Pipe", | |
56 | "Unknown" | |
57 | }; | |
22f5d1ca | 58 | |
60c0b5a2 | 59 | static void fdUpdateBiggest(int fd, int); |
a7870688 | 60 | |
61 | static void | |
60c0b5a2 | 62 | fdUpdateBiggest(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 | ||
90 | void | |
4f92c80c | 91 | fd_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 | |
bf8fe701 | 100 | debugs(51, 3, "fd_close FD " << 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--; |
cc192b50 | 106 | F->clear(); |
76f87348 | 107 | F->timeout = 0; |
a7870688 | 108 | } |
109 | ||
0f15e632 | 110 | #ifdef _SQUID_MSWIN_ |
111 | ||
112 | int | |
113 | socket_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 | ||
122 | int | |
123 | file_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 | ||
132 | int | |
133 | socket_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 | ||
142 | int | |
143 | file_write_method(int fd, const char *buf, int len) | |
144 | { | |
145 | return (_write(fd, buf, len)); | |
146 | } | |
62e76326 | 147 | |
0f15e632 | 148 | #else |
1f7c9178 | 149 | int |
150 | default_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 | ||
159 | int | |
160 | default_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 | 171 | void |
172 | fd_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) { |
bf8fe701 | 179 | debugs(51, 1, "WARNING: Closing open FD " << std::setw(4) << fd); |
62e76326 | 180 | fd_close(fd); |
6cf028ab | 181 | } |
62e76326 | 182 | |
60c0b5a2 | 183 | assert(!F->flags.open); |
cc192b50 | 184 | debugs(51, 3, "fd_open() FD " << 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 | ||
227 | void | |
228 | fd_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 | |
234 | void | |
235 | fd_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 | |
9e4ad609 | 250 | void |
251 | fdDumpOpen(void) | |
252 | { | |
253 | int i; | |
76f87348 | 254 | fde *F; |
62e76326 | 255 | |
9e4ad609 | 256 | for (i = 0; i < Squid_MaxFD; i++) { |
62e76326 | 257 | F = &fd_table[i]; |
258 | ||
259 | if (!F->flags.open) | |
260 | continue; | |
261 | ||
262 | if (i == fileno(debug_log)) | |
263 | continue; | |
264 | ||
bf8fe701 | 265 | debugs(51, 1, "Open FD "<< std::left<< std::setw(10) << |
266 | (F->bytes_read && F->bytes_written ? "READ/WRITE" : | |
267 | F->bytes_read ? "READING" : F->bytes_written ? "WRITING" : | |
268 | null_string) << | |
269 | " "<< std::right << std::setw(4) << i << " " << F->desc); | |
9e4ad609 | 270 | } |
f17936ab | 271 | } |
22f5d1ca | 272 | |
273 | int | |
274 | fdNFree(void) | |
275 | { | |
b6a2f15e | 276 | return Squid_MaxFD - Number_FD - Opening_FD; |
22f5d1ca | 277 | } |
4b23e114 | 278 | |
a1ca9253 | 279 | int |
280 | fdUsageHigh(void) | |
281 | { | |
282 | int nrfree = fdNFree(); | |
283 | ||
284 | if (nrfree < (RESERVED_FD << 1)) | |
285 | return 1; | |
286 | ||
287 | if (nrfree < (Number_FD >> 2)) | |
288 | return 1; | |
289 | ||
290 | return 0; | |
291 | } | |
292 | ||
4b23e114 | 293 | /* Called when we runs out of file descriptors */ |
294 | void | |
295 | fdAdjustReserved(void) | |
296 | { | |
e6ccf245 | 297 | int newReserve; |
4b23e114 | 298 | int x; |
299 | static time_t last = 0; | |
300 | /* | |
301 | * don't update too frequently | |
302 | */ | |
62e76326 | 303 | |
4b23e114 | 304 | if (last + 5 > squid_curtime) |
62e76326 | 305 | return; |
306 | ||
4b23e114 | 307 | /* |
308 | * Calculate a new reserve, based on current usage and a small extra | |
309 | */ | |
e6ccf245 | 310 | newReserve = Squid_MaxFD - Number_FD + XMIN(25, Squid_MaxFD / 16); |
62e76326 | 311 | |
e6ccf245 | 312 | if (newReserve <= RESERVED_FD) |
62e76326 | 313 | return; |
314 | ||
4b23e114 | 315 | x = Squid_MaxFD - 20 - XMIN(25, Squid_MaxFD / 16); |
62e76326 | 316 | |
e6ccf245 | 317 | if (newReserve > x) { |
62e76326 | 318 | /* perhaps this should be fatal()? -DW */ |
bf8fe701 | 319 | debugs(51, 0, "WARNING: This machine has a serious shortage of filedescriptors."); |
62e76326 | 320 | newReserve = x; |
4b23e114 | 321 | } |
62e76326 | 322 | |
375295eb | 323 | if (Squid_MaxFD - newReserve < XMIN(256, Squid_MaxFD / 2)) |
324 | fatalf("Too few filedescriptors available in the system (%d usable of %d).\n", Squid_MaxFD - newReserve, Squid_MaxFD); | |
325 | ||
bf8fe701 | 326 | debugs(51, 0, "Reserved FD adjusted from " << RESERVED_FD << " to " << newReserve << " due to failures"); |
e6ccf245 | 327 | RESERVED_FD = newReserve; |
4b23e114 | 328 | } |