]>
Commit | Line | Data |
---|---|---|
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 | ||
a7870688 | 36 | #include "squid.h" |
528b2c61 | 37 | #include "fde.h" |
985c86bc | 38 | #include "SquidTime.h" |
f76d2f97 | 39 | #include "Debug.h" |
a7870688 | 40 | |
1f7c9178 | 41 | int default_read_method(int, char *, int); |
42 | int default_write_method(int, const char *, int); | |
0f15e632 | 43 | #ifdef _SQUID_MSWIN_ |
44 | int socket_read_method(int, char *, int); | |
45 | int socket_write_method(int, const char *, int); | |
46 | int file_read_method(int, char *, int); | |
47 | int file_write_method(int, const char *, int); | |
48 | #endif | |
1f7c9178 | 49 | |
26ac0430 AJ |
50 | const char *fdTypeStr[] = { |
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--; |
bf81adb4 | 106 | *F = fde(); |
a7870688 | 107 | } |
108 | ||
0f15e632 | 109 | #ifdef _SQUID_MSWIN_ |
110 | ||
111 | int | |
112 | socket_read_method(int fd, char *buf, int len) | |
113 | { | |
fe86a3bf | 114 | int i; |
0242b721 | 115 | PROF_start(recv); |
fe86a3bf | 116 | i = recv(fd, (void *) buf, len, 0); |
0242b721 | 117 | PROF_stop(recv); |
fe86a3bf | 118 | return i; |
0f15e632 | 119 | } |
120 | ||
121 | int | |
122 | file_read_method(int fd, char *buf, int len) | |
123 | { | |
fe86a3bf | 124 | int i; |
125 | PROF_start(read); | |
ce8db2ee | 126 | i = _read(fd, buf, len); |
fe86a3bf | 127 | PROF_stop(read); |
128 | return i; | |
0f15e632 | 129 | } |
130 | ||
131 | int | |
132 | socket_write_method(int fd, const char *buf, int len) | |
133 | { | |
fe86a3bf | 134 | int i; |
135 | PROF_start(send); | |
136 | i = send(fd, (const void *) buf, len, 0); | |
137 | PROF_stop(send); | |
138 | return i; | |
0f15e632 | 139 | } |
140 | ||
141 | int | |
142 | file_write_method(int fd, const char *buf, int len) | |
143 | { | |
0242b721 | 144 | int i; |
145 | PROF_start(write); | |
146 | i = (_write(fd, buf, len)); | |
147 | PROF_stop(write); | |
148 | return i; | |
0f15e632 | 149 | } |
62e76326 | 150 | |
0f15e632 | 151 | #else |
1f7c9178 | 152 | int |
153 | default_read_method(int fd, char *buf, int len) | |
154 | { | |
fe86a3bf | 155 | int i; |
156 | PROF_start(read); | |
157 | i = read(fd, buf, len); | |
158 | PROF_stop(read); | |
159 | return i; | |
1f7c9178 | 160 | } |
161 | ||
162 | int | |
163 | default_write_method(int fd, const char *buf, int len) | |
164 | { | |
fe86a3bf | 165 | int i; |
166 | PROF_start(write); | |
167 | i = write(fd, buf, len); | |
168 | PROF_stop(write); | |
169 | return i; | |
1f7c9178 | 170 | } |
62e76326 | 171 | |
0f15e632 | 172 | #endif |
1f7c9178 | 173 | |
a7870688 | 174 | void |
175 | fd_open(int fd, unsigned int type, const char *desc) | |
176 | { | |
4bc3a4cd | 177 | fde *F; |
ceab4c00 | 178 | assert(fd >= 0); |
4bc3a4cd | 179 | F = &fd_table[fd]; |
62e76326 | 180 | |
7197b20d | 181 | if (F->flags.open) { |
bf8fe701 | 182 | debugs(51, 1, "WARNING: Closing open FD " << std::setw(4) << fd); |
62e76326 | 183 | fd_close(fd); |
6cf028ab | 184 | } |
62e76326 | 185 | |
60c0b5a2 | 186 | assert(!F->flags.open); |
cc192b50 | 187 | debugs(51, 3, "fd_open() FD " << fd << " " << desc); |
76f87348 | 188 | F->type = type; |
60c0b5a2 | 189 | F->flags.open = 1; |
751406fe | 190 | F->epoll_state = 0; |
0f15e632 | 191 | #ifdef _SQUID_MSWIN_ |
62e76326 | 192 | |
629b5f75 | 193 | F->win32.handle = _get_osfhandle(fd); |
194 | ||
0f15e632 | 195 | switch (type) { |
62e76326 | 196 | |
0f15e632 | 197 | case FD_SOCKET: |
62e76326 | 198 | |
0f15e632 | 199 | case FD_PIPE: |
62e76326 | 200 | F->read_method = &socket_read_method; |
201 | F->write_method = &socket_write_method; | |
202 | break; | |
203 | ||
0f15e632 | 204 | case FD_FILE: |
62e76326 | 205 | |
0f15e632 | 206 | case FD_LOG: |
62e76326 | 207 | F->read_method = &file_read_method; |
208 | F->write_method = &file_write_method; | |
209 | break; | |
210 | ||
0f15e632 | 211 | default: |
62e76326 | 212 | fatalf("fd_open(): unknown FD type - FD#: %i, type: %u, desc %s\n", fd, type, desc); |
0f15e632 | 213 | } |
62e76326 | 214 | |
0f15e632 | 215 | #else |
1f7c9178 | 216 | F->read_method = &default_read_method; |
62e76326 | 217 | |
1f7c9178 | 218 | F->write_method = &default_write_method; |
62e76326 | 219 | |
0f15e632 | 220 | #endif |
62e76326 | 221 | |
60c0b5a2 | 222 | fdUpdateBiggest(fd, 1); |
62e76326 | 223 | |
a7870688 | 224 | if (desc) |
62e76326 | 225 | xstrncpy(F->desc, desc, FD_DESC_SZ); |
226 | ||
2524a4d5 | 227 | Number_FD++; |
a7870688 | 228 | } |
229 | ||
230 | void | |
231 | fd_note(int fd, const char *s) | |
232 | { | |
76f87348 | 233 | fde *F = &fd_table[fd]; |
234 | xstrncpy(F->desc, s, FD_DESC_SZ); | |
a7870688 | 235 | } |
4f92c80c | 236 | |
237 | void | |
238 | fd_bytes(int fd, int len, unsigned int type) | |
239 | { | |
76f87348 | 240 | fde *F = &fd_table[fd]; |
62e76326 | 241 | |
4f92c80c | 242 | if (len < 0) |
62e76326 | 243 | return; |
244 | ||
365e5b34 | 245 | assert(type == FD_READ || type == FD_WRITE); |
62e76326 | 246 | |
4f92c80c | 247 | if (type == FD_READ) |
62e76326 | 248 | F->bytes_read += len; |
4f92c80c | 249 | else |
62e76326 | 250 | F->bytes_written += len; |
4f92c80c | 251 | } |
f17936ab | 252 | |
9e4ad609 | 253 | void |
254 | fdDumpOpen(void) | |
255 | { | |
256 | int i; | |
76f87348 | 257 | fde *F; |
62e76326 | 258 | |
9e4ad609 | 259 | for (i = 0; i < Squid_MaxFD; i++) { |
62e76326 | 260 | F = &fd_table[i]; |
261 | ||
262 | if (!F->flags.open) | |
263 | continue; | |
264 | ||
265 | if (i == fileno(debug_log)) | |
266 | continue; | |
267 | ||
26ac0430 | 268 | debugs(51, 1, "Open FD "<< std::left<< std::setw(10) << |
bf8fe701 | 269 | (F->bytes_read && F->bytes_written ? "READ/WRITE" : |
26ac0430 | 270 | F->bytes_read ? "READING" : F->bytes_written ? "WRITING" : |
86905e1f | 271 | "UNSTARTED") << |
bf8fe701 | 272 | " "<< std::right << std::setw(4) << i << " " << F->desc); |
9e4ad609 | 273 | } |
f17936ab | 274 | } |
22f5d1ca | 275 | |
276 | int | |
277 | fdNFree(void) | |
278 | { | |
b6a2f15e | 279 | return Squid_MaxFD - Number_FD - Opening_FD; |
22f5d1ca | 280 | } |
4b23e114 | 281 | |
a1ca9253 | 282 | int |
283 | fdUsageHigh(void) | |
284 | { | |
285 | int nrfree = fdNFree(); | |
286 | ||
287 | if (nrfree < (RESERVED_FD << 1)) | |
288 | return 1; | |
289 | ||
290 | if (nrfree < (Number_FD >> 2)) | |
291 | return 1; | |
292 | ||
293 | return 0; | |
294 | } | |
295 | ||
4b23e114 | 296 | /* Called when we runs out of file descriptors */ |
297 | void | |
298 | fdAdjustReserved(void) | |
299 | { | |
e6ccf245 | 300 | int newReserve; |
4b23e114 | 301 | int x; |
302 | static time_t last = 0; | |
303 | /* | |
304 | * don't update too frequently | |
305 | */ | |
62e76326 | 306 | |
4b23e114 | 307 | if (last + 5 > squid_curtime) |
62e76326 | 308 | return; |
309 | ||
4b23e114 | 310 | /* |
311 | * Calculate a new reserve, based on current usage and a small extra | |
312 | */ | |
d85c3078 | 313 | newReserve = Squid_MaxFD - Number_FD + min(25, Squid_MaxFD / 16); |
62e76326 | 314 | |
e6ccf245 | 315 | if (newReserve <= RESERVED_FD) |
62e76326 | 316 | return; |
317 | ||
d85c3078 | 318 | x = Squid_MaxFD - 20 - min(25, Squid_MaxFD / 16); |
62e76326 | 319 | |
e6ccf245 | 320 | if (newReserve > x) { |
62e76326 | 321 | /* perhaps this should be fatal()? -DW */ |
bf8fe701 | 322 | debugs(51, 0, "WARNING: This machine has a serious shortage of filedescriptors."); |
62e76326 | 323 | newReserve = x; |
4b23e114 | 324 | } |
62e76326 | 325 | |
d85c3078 | 326 | if (Squid_MaxFD - newReserve < min(256, Squid_MaxFD / 2)) |
26ac0430 | 327 | fatalf("Too few filedescriptors available in the system (%d usable of %d).\n", Squid_MaxFD - newReserve, Squid_MaxFD); |
375295eb | 328 | |
bf8fe701 | 329 | debugs(51, 0, "Reserved FD adjusted from " << RESERVED_FD << " to " << newReserve << " due to failures"); |
e6ccf245 | 330 | RESERVED_FD = newReserve; |
4b23e114 | 331 | } |