-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdPollE.icc
282 lines (240 loc) · 10.6 KB
/
XrdPollE.icc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/******************************************************************************/
/* */
/* X r d P o l l E . i c c */
/* */
/* (c) 2008 by the Board of Trustees of the Leland Stanford, Jr., University */
/* All Rights Reserved */
/* Produced by Andrew Hanushevsky for Stanford University under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/******************************************************************************/
#ifndef __APPLE__
#include <malloc.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include "XrdSys/XrdSysError.hh"
#include "Xrd/XrdLink.hh"
#include "Xrd/XrdPollE.hh"
#include "Xrd/XrdScheduler.hh"
/******************************************************************************/
/* n e w P o l l e r */
/******************************************************************************/
XrdPoll *XrdPoll::newPoller(int pollid, int maxfd)
{
int pfd, bytes, alignment, pagsz = getpagesize();
struct epoll_event *pp;
// Open the /dev/poll driver
//
#ifndef EPOLL_CLOEXEC
if ((pfd = epoll_create(maxfd)) >= 0) fcntl(pfd, F_SETFD, FD_CLOEXEC);
else
#else
if ((pfd = epoll_create1(EPOLL_CLOEXEC)) < 0)
#endif
{XrdLog->Emsg("Poll", errno, "create epoll device"); return 0;}
// Calculate the size of the poll table and allocate it
//
bytes = maxfd * sizeof(struct epoll_event);
alignment = (bytes < pagsz ? 1024 : pagsz);
if (!(pp = (struct epoll_event *)memalign(alignment, bytes)))
{XrdLog->Emsg("Poll", ENOMEM, "create poll table");
close(pfd);
return 0;
}
// Create new poll object
//
memset((void *)pp, 0, bytes);
return (XrdPoll *)new XrdPollE(pp, maxfd, pfd);
}
/******************************************************************************/
/* D e s t r u c t o r */
/******************************************************************************/
XrdPollE::~XrdPollE()
{
if (PollTab) free(PollTab);
if (PollDfd >= 0) close(PollDfd);
}
/******************************************************************************/
/* D i s a b l e */
/******************************************************************************/
void XrdPollE::Disable(XrdLink *lp, const char *etxt)
{
// Simply return if the link is already disabled
//
if (!lp->isEnabled) return;
// If Linux 2.6.9 we use EPOLLONESHOT to automatically disable a polled fd.
// So, the Disable() method need not do anything. Prior kernels did not have
// this mechanism so we need to do this manually.
//
#ifndef EPOLLONESHOT
struct epoll_event myEvents = {0, (void *)lp};
// Enable this fd. Unlike solaris, epoll_ctl() does not block when the pollfd
// is being waited upon by another thread.
//
if (epoll_ctl(PollDfd, EPOLL_CTL_MOD, lp->FDnum(), &myEvents))
{XrdLog->Emsg("Poll", errno, "disable link", lp->ID); return;}
#endif
// Trace this event
//
lp->isEnabled = 0;
TRACEI(POLL, "Poller " <<PID <<" async disabling link " <<lp->FD);
// Check if this link needs to be rescheduled. If so, the caller better have
// the link opMutex lock held for this to work!
//
if (etxt && Finish(lp, etxt)) XrdSched->Schedule((XrdJob *)lp);
}
/******************************************************************************/
/* E n a b l e */
/******************************************************************************/
int XrdPollE::Enable(XrdLink *lp)
{
struct epoll_event myEvents = {ePollEvents, {(void *)lp}};
// Simply return if the link is already enabled
//
if (lp->isEnabled) return 1;
// Enable this fd. Unlike solaris, epoll_ctl() does not block when the pollfd
// is being waited upon by another thread.
//
lp->isEnabled = 1;
if (epoll_ctl(PollDfd, EPOLL_CTL_MOD, lp->FDnum(), &myEvents))
{XrdLog->Emsg("Poll", errno, "enable link", lp->ID);
lp->isEnabled = 0;
return 0;
}
// Do final processing
//
TRACE(POLL, "Poller " <<PID <<" enabled " <<lp->ID);
numEnabled++;
return 1;
}
/******************************************************************************/
/* E x c l u d e */
/******************************************************************************/
void XrdPollE::Exclude(XrdLink *lp)
{
// Make sure this link is not enabled
//
if (lp->isEnabled)
{XrdLog->Emsg("Poll", "Detach of enabled link", lp->ID);
Disable(lp);
}
}
/******************************************************************************/
/* I n c l u d e */
/******************************************************************************/
int XrdPollE::Include(XrdLink *lp)
{
struct epoll_event myEvent = {0, {(void *)lp}};
int rc;
// Add this fd to the poll set
//
if ((rc = epoll_ctl(PollDfd, EPOLL_CTL_ADD, lp->FDnum(), &myEvent)) < 0)
XrdLog->Emsg("Poll", errno, "include link", lp->ID);
// All done
//
return rc == 0;
}
/******************************************************************************/
/* r e m F D */
/******************************************************************************/
void XrdPollE::remFD(XrdLink *lp, unsigned int events)
{
struct epoll_event myEvents = {0, {(void *)lp}};
static const char *why;
static char buff[32];
// It works out that ONESHOT mode or even CTL_MOD requests do not necessarily
// prevent epoll_wait() from returning on an error event. So, we must manually
// remove the fd from the set and assume the logic was actually correct. If it
// wasn't then the client will eventually timeout and retry the request.
//
if (events & (EPOLLHUP | EPOLLRDHUP)) why = "Sever";
else if (events & EPOLLERR) why = "Error";
else why = "Disabled";
snprintf(buff, sizeof(buff), "%s (%x)", why, events);
XrdLog->Emsg("Poll", buff, "event occured for", lp->ID);
if (epoll_ctl(PollDfd, EPOLL_CTL_DEL, lp->FDnum(), &myEvents))
XrdLog->Emsg("Poll", errno, "exclude link", lp->ID);
}
/******************************************************************************/
/* S t a r t */
/******************************************************************************/
void XrdPollE::Start(XrdSysSemaphore *syncsem, int &retcode)
{
char eBuff[64];
int i, numpolled, num2sched;
XrdJob *jfirst, *jlast;
const short pollOK = EPOLLIN | EPOLLPRI;
XrdLink *lp;
// Indicate to the starting thread that all went well
//
retcode = 0;
syncsem->Post();
// Now start dispatching links that are ready
//
do {do {numpolled = epoll_wait(PollDfd, PollTab, PollMax, -1);}
while (numpolled < 0 && errno == EINTR);
if (numpolled == 0) continue;
if (numpolled < 0)
{XrdLog->Emsg("Poll", errno, "poll for events");
abort();
}
numEvents += numpolled;
// Checkout which links must be dispatched (no need to lock)
//
jfirst = jlast = 0; num2sched = 0;
for (i = 0; i < numpolled; i++)
{if ((lp = (XrdLink *)PollTab[i].data.ptr))
if (!(lp->isEnabled)) remFD(lp, PollTab[i].events);
else {lp->isEnabled = 0;
if (!(PollTab[i].events & pollOK))
Finish(lp, x2Text(PollTab[i].events, eBuff));
lp->NextJob = jfirst; jfirst = (XrdJob *)lp;
if (!jlast) jlast=(XrdJob *)lp;
num2sched++;
#ifndef EPOLLONESHOT
PollTab[i].events = 0;
if (epoll_ctl(PollDfd,EPOLL_CTL_MOD,lp->FDnum(),&PollTab[i]))
XrdLog->Emsg("Poll", errno, "disable link", lp->ID);
#endif
} else XrdLog->Emsg("Poll", "null link event!!!!");
}
// Schedule the polled links
//
if (num2sched == 1) XrdSched->Schedule(jfirst);
else if (num2sched) XrdSched->Schedule(num2sched, jfirst, jlast);
} while(1);
}
/******************************************************************************/
/* x 2 T e x t */
/******************************************************************************/
const char *XrdPollE::x2Text(unsigned int events, char *buff)
{
if (events & EPOLLERR) return "socket error";
if (events & (EPOLLHUP | EPOLLRDHUP)) return "client disconnected";
sprintf(buff, "unusual event (%.4x)", events);
return buff;
}