fde.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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 05 Comm */
10 
11 #include "squid.h"
12 #include "comm/Read.h"
13 #include "Debug.h"
14 #include "fd.h"
15 #include "fde.h"
16 #include "globals.h"
17 #include "SquidTime.h"
18 #include "Store.h"
19 
20 fde *fde::Table = nullptr;
21 
22 void
24 {
25  assert(reader);
26  assert(writer);
27  assert(!flags.read_pending); // this method is only meant for new FDs
28 
29  readMethod_ = reader;
30  writeMethod_ = writer;
31 }
32 
33 void
35 {
36  debugs(5, 7, "old read_pending=" << flags.read_pending);
37 
38  // Some buffering readers are using external-to-them buffers (e.g., inBuf)
39  // and might leave true flags.read_pending behind without losing data. We
40  // must clear the flag here because default I/O methods do not know about it
41  // and will leave it set forever, resulting in I/O loops.
42  flags.read_pending = false;
43 
46 }
47 
49 void
50 fde::useBufferedIo(READ_HANDLER *bufferingReader, WRITE_HANDLER *bufferingWriter)
51 {
52  debugs(5, 7, "read_pending=" << flags.read_pending);
53 
54  assert(bufferingReader);
55  assert(bufferingWriter);
56  // flags.read_pending ought to be false here, but these buffering methods
57  // can handle a stale true flag so we do not check or reset it
58 
59  readMethod_ = bufferingReader;
60  writeMethod_ = bufferingWriter;
61 }
62 
63 bool
64 fde::readPending(int fdNumber) const
65 {
66  if (type == FD_SOCKET)
67  return Comm::MonitorsRead(fdNumber);
68 
69  return read_handler != nullptr;
70 }
71 
72 void
73 fde::dumpStats(StoreEntry &dumpEntry, int fdNumber) const
74 {
75  if (!flags.open)
76  return;
77 
78 #if _SQUID_WINDOWS_
79  storeAppendPrintf(&dumpEntry, "%4d 0x%-8lX %-6.6s %4d %7" PRId64 "%c %7" PRId64 "%c %-21s %s\n",
80  fdNumber,
81  win32.handle,
82 #else
83  storeAppendPrintf(&dumpEntry, "%4d %-6.6s %4d %7" PRId64 "%c %7" PRId64 "%c %-21s %s\n",
84  fdNumber,
85 #endif
86  fdTypeStr[type],
87  timeoutHandler ? (int) (timeout - squid_curtime) : 0,
88  bytes_read,
89  readPending(fdNumber) ? '*' : ' ',
91  write_handler ? '*' : ' ',
92  remoteAddr(),
93  desc);
94 }
95 
96 void
98 {
99  storeAppendPrintf(dumpEntry, "Active file descriptors:\n");
100 #if _SQUID_WINDOWS_
101  storeAppendPrintf(dumpEntry, "%-4s %-10s %-6s %-4s %-7s* %-7s* %-21s %s\n",
102  "File",
103  "Handle",
104 #else
105  storeAppendPrintf(dumpEntry, "%-4s %-6s %-4s %-7s* %-7s* %-21s %s\n",
106  "File",
107 #endif
108  "Type",
109  "Tout",
110  "Nread",
111  "Nwrite",
112  "Remote Address",
113  "Description");
114 #if _SQUID_WINDOWS_
115  storeAppendPrintf(dumpEntry, "---- ---------- ------ ---- -------- -------- --------------------- ------------------------------\n");
116 #else
117  storeAppendPrintf(dumpEntry, "---- ------ ---- -------- -------- --------------------- ------------------------------\n");
118 #endif
119 
120  for (int i = 0; i < Squid_MaxFD; ++i) {
121  fde::Table[i].dumpStats(*dumpEntry, i);
122  }
123 }
124 
125 char const *
127 {
128  static char buf[MAX_IPSTRLEN+7]; // 7 = length of ':port' strings
129  *buf = 0;
130 
131  if (type == FD_SOCKET) {
132  if (*ipaddr)
133  snprintf(buf, sizeof(buf), "%s:%u", ipaddr, remote_port);
134  else
135  local_addr.toUrl(buf, sizeof(buf)); // toHostStr does not include port.
136  }
137 
138  return buf;
139 }
140 
READ_HANDLER * readMethod_
imports bytes into Squid
Definition: fde.h:178
#define assert(EX)
Definition: assert.h:17
WRITE_HANDLER * writeMethod_
exports Squid bytes
Definition: fde.h:179
int Squid_MaxFD
void useDefaultIo()
Definition: fde.cc:34
struct fde::_fde_flags flags
static void DumpStats(StoreEntry *)
Definition: fde.cc:97
Ip::Address local_addr
Definition: fde.h:101
unsigned short remote_port
Definition: fde.h:99
int default_read_method(int, char *, int)
Definition: fd.cc:146
int i
Definition: membanger.c:49
#define PRId64
Definition: types.h:110
int64_t bytes_written
Definition: fde.h:128
unsigned int type
Definition: fde.h:98
const char * fdTypeStr[]
Definition: fd.cc:40
bool readPending(int) const
Definition: fde.cc:64
int default_write_method(int, const char *, int)
Definition: fd.cc:156
PF * write_handler
Definition: fde.h:144
time_t squid_curtime
Definition: stub_time.cc:17
char * toUrl(char *buf, unsigned int len) const
Definition: Address.cc:884
void dumpStats(StoreEntry &, int) const
Definition: fde.cc:73
int64_t bytes_read
Definition: fde.h:127
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:128
PF * read_handler
Definition: fde.h:142
bool MonitorsRead(int fd)
whether the FD socket is being monitored for read
Definition: Read.cc:29
int WRITE_HANDLER(int, const char *, int)
Definition: fde.h:37
void const char * buf
Definition: stub_helper.cc:16
int READ_HANDLER(int, char *, int)
Definition: fde.h:31
bool read_pending
buffering readMethod_ has data to give (regardless of socket state)
Definition: fde.h:122
AsyncCall::Pointer timeoutHandler
Definition: fde.h:146
bool open
Definition: fde.h:111
Definition: fde.h:51
time_t timeout
Definition: fde.h:147
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:23
char const * remoteAddr() const
Definition: fde.cc:126
char ipaddr[MAX_IPSTRLEN]
Definition: fde.h:107
char desc[FD_DESC_SZ]
Definition: fde.h:108
void setIo(READ_HANDLER *, WRITE_HANDLER *)
set I/O methods for a freshly opened descriptor
Definition: fde.cc:23
void storeAppendPrintf(StoreEntry *e, const char *fmt,...)
Definition: store.cc:881
void useBufferedIo(READ_HANDLER *, WRITE_HANDLER *)
use I/O methods that maintain an internal-to-them buffer
Definition: fde.cc:50

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors