fd.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 51 Filedescriptor Functions */
10 
11 #include "squid.h"
12 #include "comm/Loops.h"
13 #include "Debug.h"
14 #include "fatal.h"
15 #include "fd.h"
16 #include "fde.h"
17 #include "globals.h"
18 #include "profiler/Profiler.h"
19 #include "SquidTime.h"
20 
21 // Solaris and possibly others lack MSG_NOSIGNAL optimization
22 // TODO: move this into compat/? Use a dedicated compat file to avoid dragging
23 // sys/socket.h into the rest of Squid??
24 #ifndef MSG_NOSIGNAL
25 #define MSG_NOSIGNAL 0
26 #endif
27 
28 int default_read_method(int, char *, int);
29 int default_write_method(int, const char *, int);
30 #if _SQUID_WINDOWS_
31 int socket_read_method(int, char *, int);
32 int socket_write_method(int, const char *, int);
33 int file_read_method(int, char *, int);
34 int file_write_method(int, const char *, int);
35 #else
36 int msghdr_read_method(int, char *, int);
37 int msghdr_write_method(int, const char *, int);
38 #endif
39 
40 const char *fdTypeStr[] = {
41  "None",
42  "Log",
43  "File",
44  "Socket",
45  "Pipe",
46  "MsgHdr",
47  "Unknown"
48 };
49 
50 static void fdUpdateBiggest(int fd, int);
51 
52 static void
53 fdUpdateBiggest(int fd, int opening)
54 {
55  if (fd < Biggest_FD)
56  return;
57 
58  assert(fd < Squid_MaxFD);
59 
60  if (fd > Biggest_FD) {
61  /*
62  * assert that we are not closing a FD bigger than
63  * our known biggest FD
64  */
65  assert(opening);
66  Biggest_FD = fd;
67  return;
68  }
69 
70  /* if we are here, then fd == Biggest_FD */
71  /*
72  * assert that we are closing the biggest FD; we can't be
73  * re-opening it
74  */
75  assert(!opening);
76 
77  while (Biggest_FD >= 0 && !fd_table[Biggest_FD].flags.open)
78  --Biggest_FD;
79 }
80 
81 void
82 fd_close(int fd)
83 {
84  fde *F = &fd_table[fd];
85 
86  assert(fd >= 0);
87  assert(F->flags.open);
88 
89  if (F->type == FD_FILE) {
90  assert(F->read_handler == NULL);
91  assert(F->write_handler == NULL);
92  }
93 
94  debugs(51, 3, "fd_close FD " << fd << " " << F->desc);
96  F->flags.open = false;
97  fdUpdateBiggest(fd, 0);
98  --Number_FD;
99  F->clear();
100 }
101 
102 #if _SQUID_WINDOWS_
103 
104 int
105 socket_read_method(int fd, char *buf, int len)
106 {
107  int i;
108  PROF_start(recv);
109  i = recv(fd, (void *) buf, len, 0);
110  PROF_stop(recv);
111  return i;
112 }
113 
114 int
115 file_read_method(int fd, char *buf, int len)
116 {
117  int i;
118  PROF_start(read);
119  i = _read(fd, buf, len);
120  PROF_stop(read);
121  return i;
122 }
123 
124 int
125 socket_write_method(int fd, const char *buf, int len)
126 {
127  int i;
128  PROF_start(send);
129  i = send(fd, (const void *) buf, len, 0);
130  PROF_stop(send);
131  return i;
132 }
133 
134 int
135 file_write_method(int fd, const char *buf, int len)
136 {
137  int i;
138  PROF_start(write);
139  i = (_write(fd, buf, len));
140  PROF_stop(write);
141  return i;
142 }
143 
144 #else
145 int
146 default_read_method(int fd, char *buf, int len)
147 {
148  int i;
149  PROF_start(read);
150  i = read(fd, buf, len);
151  PROF_stop(read);
152  return i;
153 }
154 
155 int
156 default_write_method(int fd, const char *buf, int len)
157 {
158  int i;
159  PROF_start(write);
160  i = write(fd, buf, len);
161  PROF_stop(write);
162  return i;
163 }
164 
165 int
166 msghdr_read_method(int fd, char *buf, int)
167 {
168  PROF_start(read);
169  const int i = recvmsg(fd, reinterpret_cast<msghdr*>(buf), MSG_DONTWAIT);
170  PROF_stop(read);
171  return i;
172 }
173 
174 int
175 msghdr_write_method(int fd, const char *buf, int len)
176 {
177  PROF_start(write);
178  const int i = sendmsg(fd, reinterpret_cast<const msghdr*>(buf), MSG_NOSIGNAL);
179  PROF_stop(write);
180  return i > 0 ? len : i; // len is imprecise but the caller expects a match
181 }
182 
183 #endif
184 
185 void
186 fd_open(int fd, unsigned int type, const char *desc)
187 {
188  fde *F;
189  assert(fd >= 0);
190  F = &fd_table[fd];
191 
192  if (F->flags.open) {
193  debugs(51, DBG_IMPORTANT, "WARNING: Closing open FD " << std::setw(4) << fd);
194  fd_close(fd);
195  }
196 
197  assert(!F->flags.open);
198  debugs(51, 3, "fd_open() FD " << fd << " " << desc);
199  F->type = type;
200  F->flags.open = true;
201  F->epoll_state = 0;
202 #if _SQUID_WINDOWS_
203 
204  F->win32.handle = _get_osfhandle(fd);
205 
206  switch (type) {
207 
208  case FD_SOCKET:
209 
210  case FD_PIPE:
211  F->read_method = &socket_read_method;
212  F->write_method = &socket_write_method;
213  break;
214 
215  case FD_FILE:
216 
217  case FD_LOG:
218  F->read_method = &file_read_method;
219  F->write_method = &file_write_method;
220  break;
221 
222  default:
223  fatalf("fd_open(): unknown FD type - FD#: %i, type: %u, desc %s\n", fd, type, desc);
224  }
225 
226 #else
227  switch (type) {
228 
229  case FD_MSGHDR:
232  break;
233 
234  default:
237  break;
238  }
239 
240 #endif
241 
242  fdUpdateBiggest(fd, 1);
243 
244  fd_note(fd, desc);
245 
246  ++Number_FD;
247 }
248 
249 void
250 fd_note(int fd, const char *s)
251 {
252  fde *F = &fd_table[fd];
253  if (s)
254  xstrncpy(F->desc, s, FD_DESC_SZ);
255  else
256  *(F->desc) = 0; // ""-string
257 }
258 
259 void
260 fd_bytes(int fd, int len, unsigned int type)
261 {
262  fde *F = &fd_table[fd];
263 
264  if (len < 0)
265  return;
266 
267  assert(type == FD_READ || type == FD_WRITE);
268 
269  if (type == FD_READ)
270  F->bytes_read += len;
271  else
272  F->bytes_written += len;
273 }
274 
275 void
277 {
278  int i;
279  fde *F;
280 
281  for (i = 0; i < Squid_MaxFD; ++i) {
282  F = &fd_table[i];
283 
284  if (!F->flags.open)
285  continue;
286 
287  if (i == fileno(debug_log))
288  continue;
289 
290  debugs(51, DBG_IMPORTANT, "Open FD "<< std::left<< std::setw(10) <<
291  (F->bytes_read && F->bytes_written ? "READ/WRITE" :
292  F->bytes_read ? "READING" : F->bytes_written ? "WRITING" :
293  "UNSTARTED") <<
294  " "<< std::right << std::setw(4) << i << " " << F->desc);
295  }
296 }
297 
298 int
299 fdNFree(void)
300 {
301  return Squid_MaxFD - Number_FD - Opening_FD;
302 }
303 
304 int
306 {
307  int nrfree = fdNFree();
308 
309  if (nrfree < (RESERVED_FD << 1))
310  return 1;
311 
312  if (nrfree < (Number_FD >> 2))
313  return 1;
314 
315  return 0;
316 }
317 
318 /* Called when we runs out of file descriptors */
319 void
321 {
322  int newReserve;
323  int x;
324  static time_t last = 0;
325  /*
326  * don't update too frequently
327  */
328 
329  if (last + 5 > squid_curtime)
330  return;
331 
332  /*
333  * Calculate a new reserve, based on current usage and a small extra
334  */
335  newReserve = Squid_MaxFD - Number_FD + min(25, Squid_MaxFD / 16);
336 
337  if (newReserve <= RESERVED_FD)
338  return;
339 
340  x = Squid_MaxFD - 20 - min(25, Squid_MaxFD / 16);
341 
342  if (newReserve > x) {
343  /* perhaps this should be fatal()? -DW */
344  debugs(51, DBG_CRITICAL, "WARNING: This machine has a serious shortage of filedescriptors.");
345  newReserve = x;
346  }
347 
348  if (Squid_MaxFD - newReserve < min(256, Squid_MaxFD / 2))
349  fatalf("Too few filedescriptors available in the system (%d usable of %d).\n", Squid_MaxFD - newReserve, Squid_MaxFD);
350 
351  debugs(51, DBG_CRITICAL, "Reserved FD adjusted from " << RESERVED_FD << " to " << newReserve <<
352  " due to failures (" << (Squid_MaxFD - newReserve) << "/" << Squid_MaxFD << " file descriptors available)");
353  RESERVED_FD = newReserve;
354 }
355 
int fdUsageHigh(void)
Definition: fd.cc:305
#define debug_log
change-avoidance macro; new code should call DebugStream() instead
Definition: Debug.h:110
#define fd_table
Definition: fde.h:157
#define assert(EX)
Definition: assert.h:17
Definition: enums.h:14
int Squid_MaxFD
struct fde::_fde_flags flags
void fd_note(int fd, const char *s)
Definition: fd.cc:250
int type
Definition: errorpage.cc:78
int default_read_method(int, char *, int)
Definition: fd.cc:146
int i
Definition: membanger.c:49
void fd_open(int fd, unsigned int type, const char *desc)
Definition: fd.cc:186
static void fdUpdateBiggest(int fd, int)
Definition: fd.cc:53
static char last
Definition: parse.c:479
int64_t bytes_written
Definition: fde.h:112
Definition: enums.h:24
unsigned int type
Definition: fde.h:83
#define DBG_CRITICAL
Definition: Debug.h:45
const char * fdTypeStr[]
Definition: fd.cc:40
int fdNFree(void)
Definition: fd.cc:299
int default_write_method(int, const char *, int)
Definition: fd.cc:156
void fdAdjustReserved(void)
Definition: fd.cc:320
Definition: enums.h:23
PF * write_handler
Definition: fde.h:128
time_t squid_curtime
Definition: stub_time.cc:17
#define MSG_NOSIGNAL
Definition: fd.cc:25
void fatalf(const char *fmt,...)
Definition: fatal.cc:68
int RESERVED_FD
void fd_bytes(int fd, int len, unsigned int type)
Definition: fd.cc:260
READ_HANDLER * read_method
read handler for half-closed fds
Definition: fde.h:136
int64_t bytes_read
Definition: fde.h:111
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:124
PF * read_handler
Definition: fde.h:126
#define DBG_IMPORTANT
Definition: Debug.h:46
#define FD_DESC_SZ
Definition: defines.h:46
void fdDumpOpen(void)
Definition: fd.cc:276
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37
int Number_FD
int Biggest_FD
void fd_close(int fd)
Definition: fd.cc:82
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
void const char * buf
Definition: stub_helper.cc:16
int msghdr_read_method(int, char *, int)
Definition: fd.cc:166
Definition: enums.h:17
unsigned epoll_state
Definition: fde.h:123
#define PROF_start(probename)
Definition: Profiler.h:62
void clear()
Clear the fde class back to NULL equivalent.
Definition: fde.h:63
int Opening_FD
WRITE_HANDLER * write_method
Definition: fde.h:137
bool open
Definition: fde.h:96
Definition: fde.h:49
Definition: enums.h:15
static uint32 F(uint32 X, uint32 Y, uint32 Z)
Definition: md4.c:46
int msghdr_write_method(int, const char *, int)
Definition: fd.cc:175
#define PROF_stop(probename)
Definition: Profiler.h:63
char desc[FD_DESC_SZ]
Definition: fde.h:93
void ResetSelect(int fd)
reset/undo/unregister the watch for an FD which was set by Comm::SetSelect()
Definition: Loops.h:30
#define NULL
Definition: types.h:166
A const & min(A const &lhs, A const &rhs)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors