ModEpoll.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 "comm/Loops.h"
36 #include "fde.h"
37 #include "globals.h"
38 #include "mgr/Registration.h"
39 #include "profiler/Profiler.h"
40 #include "SquidTime.h"
41 #include "StatCounters.h"
42 #include "StatHist.h"
43 #include "Store.h"
44 
45 #define DEBUG_EPOLL 0
46 
47 #include <cerrno>
48 #if HAVE_SYS_EPOLL_H
49 #include <sys/epoll.h>
50 #endif
51 
52 static int kdpfd = -1;
53 static int max_poll_time = 1000;
54 
55 static struct epoll_event *pevents;
56 
57 static void commEPollRegisterWithCacheManager(void);
58 
59 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
60 /* Public functions */
61 
62 /*
63  * This is a needed exported function which will be called to initialise
64  * the network loop code.
65  */
66 void
68 {
69  pevents = (struct epoll_event *) xmalloc(SQUID_MAXFD * sizeof(struct epoll_event));
70 
71  if (!pevents) {
72  int xerrno = errno;
73  fatalf("comm_select_init: xmalloc() failed: %s\n", xstrerr(xerrno));
74  }
75 
76  kdpfd = epoll_create(SQUID_MAXFD);
77 
78  if (kdpfd < 0) {
79  int xerrno = errno;
80  fatalf("comm_select_init: epoll_create(): %s\n", xstrerr(xerrno));
81  }
82 
84 }
85 
86 static const char* epolltype_atoi(int x)
87 {
88  switch (x) {
89 
90  case EPOLL_CTL_ADD:
91  return "EPOLL_CTL_ADD";
92 
93  case EPOLL_CTL_DEL:
94  return "EPOLL_CTL_DEL";
95 
96  case EPOLL_CTL_MOD:
97  return "EPOLL_CTL_MOD";
98 
99  default:
100  return "UNKNOWN_EPOLLCTL_OP";
101  }
102 }
103 
108 void
109 Comm::SetSelect(int fd, unsigned int type, PF * handler, void *client_data, time_t timeout)
110 {
111  fde *F = &fd_table[fd];
112  int epoll_ctl_type = 0;
113 
114  assert(fd >= 0);
115  debugs(5, 5, HERE << "FD " << fd << ", type=" << type <<
116  ", handler=" << handler << ", client_data=" << client_data <<
117  ", timeout=" << timeout);
118 
119  struct epoll_event ev;
120  memset(&ev, 0, sizeof(ev));
121  ev.data.fd = fd;
122 
123  if (!F->flags.open) {
124  epoll_ctl(kdpfd, EPOLL_CTL_DEL, fd, &ev);
125  return;
126  }
127 
128  // If read is an interest
129 
130  if (type & COMM_SELECT_READ) {
131  if (handler) {
132  // Hack to keep the events flowing if there is data immediately ready
133  if (F->flags.read_pending)
134  ev.events |= EPOLLOUT;
135  ev.events |= EPOLLIN;
136  }
137 
138  F->read_handler = handler;
139 
140  F->read_data = client_data;
141 
142  // Otherwise, use previously stored value
143  } else if (F->epoll_state & EPOLLIN) {
144  ev.events |= EPOLLIN;
145  }
146 
147  // If write is an interest
148  if (type & COMM_SELECT_WRITE) {
149  if (handler)
150  ev.events |= EPOLLOUT;
151 
152  F->write_handler = handler;
153 
154  F->write_data = client_data;
155 
156  // Otherwise, use previously stored value
157  } else if (F->epoll_state & EPOLLOUT) {
158  ev.events |= EPOLLOUT;
159  }
160 
161  if (ev.events)
162  ev.events |= EPOLLHUP | EPOLLERR;
163 
164  if (ev.events != F->epoll_state) {
165  if (F->epoll_state) // already monitoring something.
166  epoll_ctl_type = ev.events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL;
167  else
168  epoll_ctl_type = EPOLL_CTL_ADD;
169 
170  F->epoll_state = ev.events;
171 
172  if (epoll_ctl(kdpfd, epoll_ctl_type, fd, &ev) < 0) {
173  int xerrno = errno;
174  debugs(5, DEBUG_EPOLL ? 0 : 8, "epoll_ctl(," << epolltype_atoi(epoll_ctl_type) <<
175  ",,): failed on FD " << fd << ": " << xstrerr(xerrno));
176  }
177  }
178 
179  if (timeout)
180  F->timeout = squid_curtime + timeout;
181 }
182 
183 void
184 Comm::ResetSelect(int fd)
185 {
186  fde *F = &fd_table[fd];
187  F->epoll_state = 0;
188  SetSelect(fd, 0, NULL, NULL, 0);
189 }
190 
191 static void commIncomingStats(StoreEntry * sentry);
192 
193 static void
195 {
196  Mgr::RegisterAction("comm_epoll_incoming",
197  "comm_incoming() stats",
198  commIncomingStats, 0, 1);
199 }
200 
201 static void
203 {
205  storeAppendPrintf(sentry, "Total number of epoll(2) loops: %ld\n", statCounter.select_loops);
206  storeAppendPrintf(sentry, "Histogram of returned filedescriptors\n");
208 }
209 
221 Comm::DoSelect(int msec)
222 {
223  int num, i,fd;
224  fde *F;
225  PF *hdl;
226 
227  struct epoll_event *cevents;
228 
229  PROF_start(comm_check_incoming);
230 
231  if (msec > max_poll_time)
232  msec = max_poll_time;
233 
234  for (;;) {
235  num = epoll_wait(kdpfd, pevents, SQUID_MAXFD, msec);
237 
238  if (num >= 0)
239  break;
240 
241  if (ignoreErrno(errno))
242  break;
243 
244  getCurrentTime();
245 
246  PROF_stop(comm_check_incoming);
247 
248  return Comm::COMM_ERROR;
249  }
250 
251  PROF_stop(comm_check_incoming);
252  getCurrentTime();
253 
255 
256  if (num == 0)
257  return Comm::TIMEOUT; /* No error.. */
258 
259  PROF_start(comm_handle_ready_fd);
260 
261  for (i = 0, cevents = pevents; i < num; ++i, ++cevents) {
262  fd = cevents->data.fd;
263  F = &fd_table[fd];
264  debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "got FD " << fd << " events=" <<
265  std::hex << cevents->events << " monitoring=" << F->epoll_state <<
266  " F->read_handler=" << F->read_handler << " F->write_handler=" << F->write_handler);
267 
268  // TODO: add EPOLLPRI??
269 
270  if (cevents->events & (EPOLLIN|EPOLLHUP|EPOLLERR) || F->flags.read_pending) {
271  if ((hdl = F->read_handler) != NULL) {
272  debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling read handler on FD " << fd);
273  PROF_start(comm_write_handler);
274  F->flags.read_pending = 0;
275  F->read_handler = NULL;
276  hdl(fd, F->read_data);
277  PROF_stop(comm_write_handler);
279  } else {
280  debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no read handler for FD " << fd);
281  // remove interest since no handler exist for this event.
282  SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
283  }
284  }
285 
286  if (cevents->events & (EPOLLOUT|EPOLLHUP|EPOLLERR)) {
287  if ((hdl = F->write_handler) != NULL) {
288  debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "Calling write handler on FD " << fd);
289  PROF_start(comm_read_handler);
290  F->write_handler = NULL;
291  hdl(fd, F->write_data);
292  PROF_stop(comm_read_handler);
294  } else {
295  debugs(5, DEBUG_EPOLL ? 0 : 8, HERE << "no write handler for FD " << fd);
296  // remove interest since no handler exist for this event.
297  SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
298  }
299  }
300  }
301 
302  PROF_stop(comm_handle_ready_fd);
303 
304  return Comm::OK;
305 }
306 
307 void
309 {
310  max_poll_time = 10;
311 }
312 
313 #endif /* USE_EPOLL */
314 
void count(double val)
Definition: StatHist.cc:57
#define fd_table
Definition: fde.h:157
StatCounters statCounter
Definition: StatCounters.cc:12
#define assert(EX)
Definition: assert.h:17
void ResetSelect(int)
reset/undo/unregister the watch for an FD which was set by Comm::SetSelect()
Definition: ModDevPoll.cc:304
void SelectLoopInit(void)
Initialize the module on Squid startup.
Definition: ModDevPoll.cc:178
struct fde::_fde_flags flags
#define DEBUG_EPOLL
Definition: ModEpoll.cc:45
int i
Definition: membanger.c:49
Definition: Flag.h:16
PF * write_handler
Definition: fde.h:128
time_t squid_curtime
Definition: stub_time.cc:17
void fatalf(const char *fmt,...)
Definition: fatal.cc:79
const char * xstrerr(int error)
Definition: xstrerror.cc:83
Comm::Flag DoSelect(int)
Do poll and trigger callback functions as appropriate.
Definition: ModDevPoll.cc:324
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
PF * read_handler
Definition: fde.h:126
void SetSelect(int, unsigned int, PF *, void *, time_t)
Mark an FD to be watched for its IO status.
Definition: ModDevPoll.cc:225
void RegisterAction(char const *action, char const *desc, OBJH *handler, int pw_req_flag, int atomic)
Definition: Registration.cc:16
static int max_poll_time
Definition: ModEpoll.cc:53
void * write_data
Definition: fde.h:129
StatHist select_fds_hist
Definition: StatCounters.h:127
void PF(int, void *)
Definition: forward.h:18
void * read_data
Definition: fde.h:127
unsigned long int select_loops
Definition: StatCounters.h:118
static void handler(int signo)
Definition: purge.cc:860
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
Flag
Definition: Flag.h:15
static void commIncomingStats(StoreEntry *sentry)
Definition: ModEpoll.cc:202
int ignoreErrno(int ierrno)
Definition: comm.cc:1477
time_t getCurrentTime(void)
Get current time.
static struct epoll_event * pevents
Definition: ModEpoll.cc:55
bool read_pending
Definition: fde.h:106
unsigned epoll_state
Definition: fde.h:123
#define xmalloc
#define PROF_start(probename)
Definition: Profiler.h:62
bool open
Definition: fde.h:96
Definition: fde.h:49
time_t timeout
Definition: fde.h:131
static uint32 F(uint32 X, uint32 Y, uint32 Z)
Definition: md4.c:46
#define PROF_stop(probename)
Definition: Profiler.h:63
void dump(StoreEntry *sentry, StatHistBinDumper *bd) const
Definition: StatHist.cc:173
static int kdpfd
Definition: ModEpoll.cc:52
static void commEPollRegisterWithCacheManager(void)
Definition: ModEpoll.cc:194
StatHistBinDumper statHistIntDumper
Definition: StatHist.h:123
void storeAppendPrintf(StoreEntry *e, const char *fmt,...)
Definition: store.cc:904
static const char * epolltype_atoi(int x)
Definition: ModEpoll.cc:86
#define NULL
Definition: types.h:166
void QuickPollRequired(void)
Definition: ModDevPoll.cc:444

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors