ModEpoll.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 Socket Functions */
10
11/*
12 * The idea for this came from these two websites:
13 * http://www.xmailserver.org/linux-patches/nio-improve.html
14 * http://www.kegel.com/c10k.html
15 *
16 * This is to support the epoll sysctl being added to the linux 2.5
17 * kernel tree. The new sys_epoll is an event based poller without
18 * most of the fuss of rtsignals.
19 *
20 * -- David Nicklay <dnicklay@web.turner.com>
21 */
22
23/*
24 * XXX Currently not implemented / supported by this module XXX
25 *
26 * - delay pools
27 * - deferred reads
28 *
29 */
30
31#include "squid.h"
32
33#if USE_EPOLL
34
35#include "base/CodeContext.h"
36#include "comm/Loops.h"
37#include "fde.h"
38#include "globals.h"
39#include "mgr/Registration.h"
40#include "StatCounters.h"
41#include "StatHist.h"
42#include "Store.h"
43
44#define DEBUG_EPOLL 0
45
46#include <cerrno>
47#if HAVE_SYS_EPOLL_H
48#include <sys/epoll.h>
49#endif
50
51static int kdpfd = -1;
52static int max_poll_time = 1000;
53
54static struct epoll_event *pevents;
55
56static void commEPollRegisterWithCacheManager(void);
57
58/* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
59/* Public functions */
60
61/*
62 * This is a needed exported function which will be called to initialise
63 * the network loop code.
64 */
65void
67{
68 pevents = (struct epoll_event *) xmalloc(SQUID_MAXFD * sizeof(struct epoll_event));
69
70 if (!pevents) {
71 int xerrno = errno;
72 fatalf("comm_select_init: xmalloc() failed: %s\n", xstrerr(xerrno));
73 }
74
75 kdpfd = epoll_create(SQUID_MAXFD);
76
77 if (kdpfd < 0) {
78 int xerrno = errno;
79 fatalf("comm_select_init: epoll_create(): %s\n", xstrerr(xerrno));
80 }
81
83}
84
85static const char* epolltype_atoi(int x)
86{
87 switch (x) {
88
89 case EPOLL_CTL_ADD:
90 return "EPOLL_CTL_ADD";
91
92 case EPOLL_CTL_DEL:
93 return "EPOLL_CTL_DEL";
94
95 case EPOLL_CTL_MOD:
96 return "EPOLL_CTL_MOD";
97
98 default:
99 return "UNKNOWN_EPOLLCTL_OP";
100 }
101}
102
107void
108Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout)
109{
110 fde *F = &fd_table[fd];
111 int epoll_ctl_type = 0;
112
113 assert(fd >= 0);
114 debugs(5, 5, "FD " << fd << ", type=" << type <<
115 ", handler=" << handler << ", client_data=" << client_data <<
116 ", timeout=" << timeout);
117
118 struct epoll_event ev;
119 memset(&ev, 0, sizeof(ev));
120 ev.data.fd = fd;
121
122 if (!F->flags.open) {
123 epoll_ctl(kdpfd, EPOLL_CTL_DEL, fd, &ev);
124 return;
125 }
126
127 // If read is an interest
128
129 if (type & COMM_SELECT_READ) {
130 if (handler) {
131 // Hack to keep the events flowing if there is data immediately ready
132 if (F->flags.read_pending)
133 ev.events |= EPOLLOUT;
134 ev.events |= EPOLLIN;
135 }
136
137 F->read_handler = handler;
138
139 F->read_data = client_data;
140
141 // Otherwise, use previously stored value
142 } else if (F->epoll_state & EPOLLIN) {
143 ev.events |= EPOLLIN;
144 }
145
146 // If write is an interest
147 if (type & COMM_SELECT_WRITE) {
148 if (handler)
149 ev.events |= EPOLLOUT;
150
151 F->write_handler = handler;
152
153 F->write_data = client_data;
154
155 // Otherwise, use previously stored value
156 } else if (F->epoll_state & EPOLLOUT) {
157 ev.events |= EPOLLOUT;
158 }
159
160 if (ev.events)
161 ev.events |= EPOLLHUP | EPOLLERR;
162
163 if (ev.events != F->epoll_state) {
164 if (F->epoll_state) // already monitoring something.
165 epoll_ctl_type = ev.events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL;
166 else
167 epoll_ctl_type = EPOLL_CTL_ADD;
168
169 F->epoll_state = ev.events;
170
171 if (epoll_ctl(kdpfd, epoll_ctl_type, fd, &ev) < 0) {
172 int xerrno = errno;
173 debugs(5, DEBUG_EPOLL ? 0 : 8, "ERROR: epoll_ctl(," << epolltype_atoi(epoll_ctl_type) <<
174 ",,): failed on FD " << fd << ": " << xstrerr(xerrno));
175 }
176 }
177
178 if (timeout)
179 F->timeout = squid_curtime + timeout;
180
181 if (timeout || handler) // all non-cleanup requests
182 F->codeContext = CodeContext::Current(); // TODO: Avoid clearing if set?
183 else if (!ev.events) // full cleanup: no more FD-associated work expected
184 F->codeContext = nullptr;
185 // else: direction-specific/timeout cleanup requests preserve F->codeContext
186}
187
188static void commIncomingStats(StoreEntry * sentry);
189
190static void
192{
193 Mgr::RegisterAction("comm_epoll_incoming",
194 "comm_incoming() stats",
195 commIncomingStats, 0, 1);
196}
197
198static void
200{
202 storeAppendPrintf(sentry, "Total number of epoll(2) loops: %ld\n", statCounter.select_loops);
203 storeAppendPrintf(sentry, "Histogram of returned filedescriptors\n");
205}
206
218Comm::DoSelect(int msec)
219{
220 int num, i,fd;
221 fde *F;
222 PF *hdl;
223
224 struct epoll_event *cevents;
225
226 if (msec > max_poll_time)
227 msec = max_poll_time;
228
229 for (;;) {
230 num = epoll_wait(kdpfd, pevents, SQUID_MAXFD, msec);
232
233 if (num >= 0)
234 break;
235
236 if (ignoreErrno(errno))
237 break;
238
240
241 return Comm::COMM_ERROR;
242 }
243
245
247
248 if (num == 0)
249 return Comm::TIMEOUT; /* No error.. */
250
251 for (i = 0, cevents = pevents; i < num; ++i, ++cevents) {
252 fd = cevents->data.fd;
253 F = &fd_table[fd];
254 CodeContext::Reset(F->codeContext);
255 debugs(5, DEBUG_EPOLL ? 0 : 8, "got FD " << fd << " events=" <<
256 std::hex << cevents->events << " monitoring=" << F->epoll_state <<
257 " F->read_handler=" << F->read_handler << " F->write_handler=" << F->write_handler);
258
259 // TODO: add EPOLLPRI??
260
261 if (cevents->events & (EPOLLIN|EPOLLHUP|EPOLLERR) || F->flags.read_pending) {
262 if ((hdl = F->read_handler) != nullptr) {
263 debugs(5, DEBUG_EPOLL ? 0 : 8, "Calling read handler on FD " << fd);
264 F->read_handler = nullptr;
265 hdl(fd, F->read_data);
267 } else {
268 debugs(5, DEBUG_EPOLL ? 0 : 8, "no read handler for FD " << fd);
269 // remove interest since no handler exist for this event.
270 SetSelect(fd, COMM_SELECT_READ, nullptr, nullptr, 0);
271 }
272 }
273
274 if (cevents->events & (EPOLLOUT|EPOLLHUP|EPOLLERR)) {
275 if ((hdl = F->write_handler) != nullptr) {
276 debugs(5, DEBUG_EPOLL ? 0 : 8, "Calling write handler on FD " << fd);
277 F->write_handler = nullptr;
278 hdl(fd, F->write_data);
280 } else {
281 debugs(5, DEBUG_EPOLL ? 0 : 8, "no write handler for FD " << fd);
282 // remove interest since no handler exist for this event.
283 SetSelect(fd, COMM_SELECT_WRITE, nullptr, nullptr, 0);
284 }
285 }
286 }
287
289
290 return Comm::OK;
291}
292
293void
295{
296 max_poll_time = 10;
297}
298
299#endif /* USE_EPOLL */
300
static int max_poll_time
Definition: ModEpoll.cc:52
static int kdpfd
Definition: ModEpoll.cc:51
static struct epoll_event * pevents
Definition: ModEpoll.cc:54
static void commIncomingStats(StoreEntry *sentry)
Definition: ModEpoll.cc:199
static void commEPollRegisterWithCacheManager(void)
Definition: ModEpoll.cc:191
#define DEBUG_EPOLL
Definition: ModEpoll.cc:44
static const char * epolltype_atoi(int x)
Definition: ModEpoll.cc:85
time_t squid_curtime
Definition: stub_libtime.cc:20
StatCounters statCounter
Definition: StatCounters.cc:12
StatHistBinDumper statHistIntDumper
Definition: StatHist.h:119
#define assert(EX)
Definition: assert.h:17
static const Pointer & Current()
Definition: CodeContext.cc:33
static void Reset()
forgets the current context, setting it to nil/unknown
Definition: CodeContext.cc:77
StatHist select_fds_hist
Definition: StatCounters.h:127
unsigned long int select_loops
Definition: StatCounters.h:118
void count(double val)
Definition: StatHist.cc:55
void dump(StoreEntry *sentry, StatHistBinDumper *bd) const
Definition: StatHist.cc:171
Definition: fde.h:52
void PF(int, void *)
Definition: forward.h:18
int ignoreErrno(int ierrno)
Definition: comm.cc:1440
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
#define COMM_SELECT_READ
Definition: defines.h:24
#define COMM_SELECT_WRITE
Definition: defines.h:25
void fatalf(const char *fmt,...)
Definition: fatal.cc:68
#define fd_table
Definition: fde.h:189
static uint32 F(uint32 X, uint32 Y, uint32 Z)
Definition: md4.c:46
void QuickPollRequired(void)
Definition: ModDevPoll.cc:417
Flag
Definition: Flag.h:15
@ OK
Definition: Flag.h:16
@ TIMEOUT
Definition: Flag.h:18
@ COMM_ERROR
Definition: Flag.h:17
Comm::Flag DoSelect(int)
Do poll and trigger callback functions as appropriate.
Definition: ModDevPoll.cc:311
void SelectLoopInit(void)
Initialize the module on Squid startup.
Definition: ModDevPoll.cc:176
void SetSelect(int, unsigned int, PF *, void *, time_t)
Mark an FD to be watched for its IO status.
Definition: ModDevPoll.cc:223
void RegisterAction(char const *action, char const *desc, OBJH *handler, int pw_req_flag, int atomic)
Definition: Registration.cc:16
#define xmalloc
static void handler(int signo)
Definition: purge.cc:858
void storeAppendPrintf(StoreEntry *e, const char *fmt,...)
Definition: store.cc:841
time_t getCurrentTime() STUB_RETVAL(0) int tvSubUsec(struct timeval
const char * xstrerr(int error)
Definition: xstrerror.cc:83

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors