]> git.ipfire.org Git - thirdparty/squid.git/blob - src/fd.cc
Break out the comm code into a more "modular" format - poll code
[thirdparty/squid.git] / src / fd.cc
1
2 /*
3 * $Id: fd.cc,v 1.44 2001/12/24 15:33:43 adrian Exp $
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
38 int default_read_method(int, char *, int);
39 int default_write_method(int, const char *, int);
40
41 const char *fdTypeStr[] =
42 {
43 "None",
44 "Log",
45 "File",
46 "Socket",
47 "Pipe",
48 "Unknown"
49 };
50
51 static void fdUpdateBiggest(int fd, int);
52
53 static void
54 fdUpdateBiggest(int fd, int opening)
55 {
56 if (fd < Biggest_FD)
57 return;
58 assert(fd < Squid_MaxFD);
59 if (fd > Biggest_FD) {
60 /*
61 * assert that we are not closing a FD bigger than
62 * our known biggest FD
63 */
64 assert(opening);
65 Biggest_FD = fd;
66 return;
67 }
68 /* if we are here, then fd == Biggest_FD */
69 /*
70 * assert that we are closing the biggest FD; we can't be
71 * re-opening it
72 */
73 assert(!opening);
74 while (!fd_table[Biggest_FD].flags.open)
75 Biggest_FD--;
76 }
77
78 void
79 fd_close(int fd)
80 {
81 fde *F = &fd_table[fd];
82 if (F->type == FD_FILE) {
83 assert(F->read_handler == NULL);
84 assert(F->write_handler == NULL);
85 }
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);
89 F->flags.open = 0;
90 fdUpdateBiggest(fd, 0);
91 Number_FD--;
92 memset(F, '\0', sizeof(fde));
93 F->timeout = 0;
94 }
95
96 int
97 default_read_method(int fd, char *buf, int len)
98 {
99 return (read(fd, buf, len));
100 }
101
102 int
103 default_write_method(int fd, const char *buf, int len)
104 {
105 return (write(fd, buf, len));
106 }
107
108 void
109 fd_open(int fd, unsigned int type, const char *desc)
110 {
111 fde *F;
112 assert(fd >= 0);
113 F = &fd_table[fd];
114 if (F->flags.open) {
115 debug(51, 1) ("WARNING: Closing open FD %4d\n", fd);
116 fd_close(fd);
117 }
118 assert(!F->flags.open);
119 debug(51, 3) ("fd_open FD %d %s\n", fd, desc);
120 F->type = type;
121 F->flags.open = 1;
122 F->read_method = &default_read_method;
123 F->write_method = &default_write_method;
124 fdUpdateBiggest(fd, 1);
125 if (desc)
126 xstrncpy(F->desc, desc, FD_DESC_SZ);
127 Number_FD++;
128 }
129
130 void
131 fd_note(int fd, const char *s)
132 {
133 fde *F = &fd_table[fd];
134 xstrncpy(F->desc, s, FD_DESC_SZ);
135 }
136
137 void
138 fd_bytes(int fd, int len, unsigned int type)
139 {
140 fde *F = &fd_table[fd];
141 if (len < 0)
142 return;
143 assert(type == FD_READ || type == FD_WRITE);
144 if (type == FD_READ)
145 F->bytes_read += len;
146 else
147 F->bytes_written += len;
148 }
149
150 void
151 fdFreeMemory(void)
152 {
153 safe_free(fd_table);
154 }
155
156 void
157 fdDumpOpen(void)
158 {
159 int i;
160 fde *F;
161 for (i = 0; i < Squid_MaxFD; i++) {
162 F = &fd_table[i];
163 if (!F->flags.open)
164 continue;
165 if (i == fileno(debug_log))
166 continue;
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,
171 i, F->desc);
172 }
173 }
174
175 int
176 fdNFree(void)
177 {
178 return Squid_MaxFD - Number_FD - Opening_FD;
179 }
180
181 /* Called when we runs out of file descriptors */
182 void
183 fdAdjustReserved(void)
184 {
185 int new;
186 int x;
187 static time_t last = 0;
188 /*
189 * don't update too frequently
190 */
191 if (last + 5 > squid_curtime)
192 return;
193 /*
194 * Calculate a new reserve, based on current usage and a small extra
195 */
196 new = Squid_MaxFD - Number_FD + XMIN(25, Squid_MaxFD / 16);
197 if (new <= RESERVED_FD)
198 return;
199 x = Squid_MaxFD - 20 - XMIN(25, Squid_MaxFD / 16);
200 if (new > x) {
201 /* perhaps this should be fatal()? -DW */
202 debug(51, 0) ("WARNING: This machine has a serious shortage of filedescriptors.\n");
203 new = x;
204 }
205 debug(51, 0) ("Reserved FD adjusted from %d to %d due to failures\n",
206 RESERVED_FD, new);
207 RESERVED_FD = new;
208 }