]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/fd.cc
3 * $Id: fd.cc,v 1.44 2001/12/24 15:33:43 adrian Exp $
5 * DEBUG: section 51 Filedescriptor Functions
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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.
38 int default_read_method(int, char *, int);
39 int default_write_method(int, const char *, int);
41 const char *fdTypeStr
[] =
51 static void fdUpdateBiggest(int fd
, int);
54 fdUpdateBiggest(int fd
, int opening
)
58 assert(fd
< Squid_MaxFD
);
59 if (fd
> Biggest_FD
) {
61 * assert that we are not closing a FD bigger than
62 * our known biggest FD
68 /* if we are here, then fd == Biggest_FD */
70 * assert that we are closing the biggest FD; we can't be
74 while (!fd_table
[Biggest_FD
].flags
.open
)
81 fde
*F
= &fd_table
[fd
];
82 if (F
->type
== FD_FILE
) {
83 assert(F
->read_handler
== NULL
);
84 assert(F
->write_handler
== NULL
);
86 debug(51, 3) ("fd_close FD %d %s\n", fd
, F
->desc
);
87 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
88 commSetSelect(fd
, COMM_SELECT_WRITE
, NULL
, NULL
, 0);
90 fdUpdateBiggest(fd
, 0);
92 memset(F
, '\0', sizeof(fde
));
97 default_read_method(int fd
, char *buf
, int len
)
99 return (read(fd
, buf
, len
));
103 default_write_method(int fd
, const char *buf
, int len
)
105 return (write(fd
, buf
, len
));
109 fd_open(int fd
, unsigned int type
, const char *desc
)
115 debug(51, 1) ("WARNING: Closing open FD %4d\n", fd
);
118 assert(!F
->flags
.open
);
119 debug(51, 3) ("fd_open FD %d %s\n", fd
, desc
);
122 F
->read_method
= &default_read_method
;
123 F
->write_method
= &default_write_method
;
124 fdUpdateBiggest(fd
, 1);
126 xstrncpy(F
->desc
, desc
, FD_DESC_SZ
);
131 fd_note(int fd
, const char *s
)
133 fde
*F
= &fd_table
[fd
];
134 xstrncpy(F
->desc
, s
, FD_DESC_SZ
);
138 fd_bytes(int fd
, int len
, unsigned int type
)
140 fde
*F
= &fd_table
[fd
];
143 assert(type
== FD_READ
|| type
== FD_WRITE
);
145 F
->bytes_read
+= len
;
147 F
->bytes_written
+= len
;
161 for (i
= 0; i
< Squid_MaxFD
; i
++) {
165 if (i
== fileno(debug_log
))
167 debug(51, 1) ("Open FD %-10s %4d %s\n",
168 F
->bytes_read
&& F
->bytes_written
? "READ/WRITE" :
169 F
->bytes_read
? "READING" :
170 F
->bytes_written
? "WRITING" : null_string
,
178 return Squid_MaxFD
- Number_FD
- Opening_FD
;
181 /* Called when we runs out of file descriptors */
183 fdAdjustReserved(void)
187 static time_t last
= 0;
189 * don't update too frequently
191 if (last
+ 5 > squid_curtime
)
194 * Calculate a new reserve, based on current usage and a small extra
196 new = Squid_MaxFD
- Number_FD
+ XMIN(25, Squid_MaxFD
/ 16);
197 if (new <= RESERVED_FD
)
199 x
= Squid_MaxFD
- 20 - XMIN(25, Squid_MaxFD
/ 16);
201 /* perhaps this should be fatal()? -DW */
202 debug(51, 0) ("WARNING: This machine has a serious shortage of filedescriptors.\n");
205 debug(51, 0) ("Reserved FD adjusted from %d to %d due to failures\n",