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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors