Skip to content

Commit ce01bcc

Browse files
committed
The epoll handle of hooked can be shared between different fibers; Optimize codes.
1 parent 6485676 commit ce01bcc

File tree

19 files changed

+205
-50
lines changed

19 files changed

+205
-50
lines changed

c/include/fiber/fiber_base.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,15 @@ FIBER_API int acl_fiber_gettimeofday(struct timeval *tv, struct timezone *tz);
428428

429429
FIBER_API void acl_fiber_memstat(void);
430430

431+
#if defined(__linux__)
432+
/**
433+
* If multiple fibers of the same thread can share one epoll handle when
434+
* using epoll, default is no.
435+
* @param yes {int}
436+
*/
437+
FIBER_API void acl_fiber_share_epoll(int yes);
438+
#endif
439+
431440
/****************************************************************************/
432441

433442
#ifdef __cplusplus

c/src/common/htable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void htable_walk(HTABLE *table, void (*walk_fn) (HTABLE_INFO *, void *), void *a
152152
int htable_size(const HTABLE *table);
153153

154154
/**
155-
* 返回哈希表当前的窗口中所含元素个数
155+
* 返回哈希表当前的容器中所含元素个数
156156
* @param table 哈希表指针
157157
* @return 哈希表中元素个数
158158
*/

c/src/common/open_limit.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ int open_limit(int limit)
5454
}
5555

5656
if (rl.rlim_max <= 0) {
57-
rl.rlim_max = 204800;
57+
rl.rlim_max = 10240;
58+
} else if (rl.rlim_max > 10240000) {
59+
rl.rlim_max = 12024000;
5860
}
61+
5962
rlim_cur = (int) rl.rlim_cur;
6063

6164
if (limit > 0) {
@@ -87,7 +90,6 @@ int open_limit(int limit)
8790
} else {
8891
return (int) rl.rlim_cur;
8992
}
90-
9193
#else
9294
rlim_cur = getdtablesize();
9395
if (rlim_cur < 0) {

c/src/event.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ struct FILE_EVENT {
184184
#define EVENT_ERR (unsigned) (1 << 4)
185185
#define EVENT_HUP (unsigned) (1 << 5)
186186
#define EVENT_NVAL (unsigned) (1 << 6)
187+
#define EVENT_ONCE (unsigned) (1 << 7)
187188

188189
#ifdef HAS_IO_URING
189190

@@ -389,9 +390,15 @@ struct EVENT {
389390

390391
long long stamp; // the stamp of the current fiber scheduler
391392
unsigned flag;
392-
#define EVENT_F_IOCP (1 << 0)
393-
#define EVENT_F_IO_URING (1 << 1)
394-
#define EVENT_IS_IOCP(x) ((x)->flag & EVENT_F_IOCP)
393+
#define EVENT_F_USE_ONCE (1 << 0)
394+
#define EVENT_F_IOCP (1 << 1)
395+
#define EVENT_F_IO_URING (1 << 2)
396+
#define EVENT_F_POLL (1 << 3)
397+
#define EVENT_F_SELECT (1 << 4)
398+
#define EVENT_F_EPOLL (1 << 5)
399+
#define EVENT_F_KQUEUE (1 << 6)
400+
401+
#define EVENT_IS_IOCP(x) ((x)->flag & EVENT_F_IOCP)
395402
#define EVENT_IS_IO_URING(x) ((x)->flag & EVENT_F_IO_URING)
396403

397404
#ifdef HAS_POLL

c/src/event/event_epoll.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ static int epoll_add_read(EVENT_EPOLL *ep, FILE_EVENT *fe)
7878
struct epoll_event ee;
7979
int op, n;
8080

81-
if ((fe->mask & EVENT_READ))
81+
if ((fe->mask & EVENT_READ)) {
8282
return 0;
83+
}
8384

8485
ee.events = 0;
8586
ee.data.u32 = 0;
@@ -251,6 +252,7 @@ static int epoll_event_wait(EVENT *ev, int timeout)
251252
array_append(ep->r_ready, fe);
252253
#else
253254
fe->r_proc(ev, fe);
255+
fe->mask |= EVENT_ONCE;
254256
#endif
255257
}
256258

@@ -269,6 +271,10 @@ static int epoll_event_wait(EVENT *ev, int timeout)
269271
fe->w_proc(ev, fe);
270272
#endif
271273
}
274+
275+
#ifndef DELAY_CALL
276+
fe->mask &= ~EVENT_ONCE;
277+
#endif
272278
}
273279

274280
#ifdef DELAY_CALL
@@ -345,6 +351,11 @@ EVENT *event_epoll_create(int size)
345351
ep->event.name = epoll_name;
346352
ep->event.handle = (acl_handle_t (*)(EVENT *)) epoll_handle;
347353
ep->event.free = epoll_free;
354+
#ifdef DELAY_CALL
355+
ep->event.flag = EVENT_F_EPOLL;
356+
#else
357+
ep->event.flag = EVENT_F_EPOLL | EVENT_F_USE_ONCE;
358+
#endif
348359

349360
ep->event.event_wait = epoll_event_wait;
350361
ep->event.checkfd = (event_oper *) epoll_checkfd;

c/src/event/event_kqueue.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ static int kqueue_wait(EVENT *ev, int timeout)
198198
array_append(ek->r_ready, fe);
199199
#else
200200
fe->r_proc(ev, fe);
201+
fe->mask |= EVENT_ONCE;
201202
#endif
202203
}
203204

@@ -209,6 +210,10 @@ static int kqueue_wait(EVENT *ev, int timeout)
209210
fe->w_proc(ev, fe);
210211
#endif
211212
}
213+
214+
#ifndef DELAY_CALL
215+
fe->mask &= ~EVENT_ONCE;
216+
#endif
212217
}
213218

214219
#ifdef DELAY_CALL
@@ -275,6 +280,11 @@ EVENT *event_kqueue_create(int size)
275280
ek->event.name = kqueue_name;
276281
ek->event.handle = (acl_handle_t (*)(EVENT *)) kqueue_handle;
277282
ek->event.free = kqueue_free;
283+
#ifdef DELAY_CALL
284+
ek->event.flag = EVENT_F_KQUEUE;
285+
#else
286+
ek->event.flag = EVENT_F_KQUEUE | EVENT_F_USE_ONCE;
287+
#endif
278288

279289
ek->event.event_fflush = (int (*)(EVENT*)) kqueue_fflush;
280290
ek->event.event_wait = kqueue_wait;

c/src/event/event_poll.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ static int poll_wait(EVENT *ev, int timeout)
191191
array_append(ep->r_ready, fe);
192192
#else
193193
fe->r_proc(ev, fe);
194+
fe->mask |= EVENT_ONCE;
194195
#endif
195196
}
196197

@@ -212,6 +213,10 @@ static int poll_wait(EVENT *ev, int timeout)
212213
fe->w_proc(ev, fe);
213214
#endif
214215
}
216+
217+
#ifndef DELAY_CALL
218+
fe->mask &= ~EVENT_ONCE;
219+
#endif
215220
}
216221

217222
#ifdef DELAY_CALL
@@ -272,6 +277,11 @@ EVENT *event_poll_create(int size)
272277
ep->event.name = poll_name;
273278
ep->event.handle = poll_handle;
274279
ep->event.free = poll_free;
280+
#ifdef DELAY_CALL
281+
ep->event.flag = EVENT_F_POLL;
282+
#else
283+
ep->event.flag = EVENT_F_POLL | EVENT_F_USE_ONCE;
284+
#endif
275285

276286
ep->event.event_wait = poll_wait;
277287
ep->event.checkfd = (event_oper *) poll_checkfd;

c/src/event/event_select.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ static int select_event_wait(EVENT *ev, int timeout)
178178
array_append(es->r_ready, fe);
179179
#else
180180
fe->r_proc(ev, fe);
181+
fe->mask |= EVENT_ONCE;
181182
#endif
182183
}
183184
if (FD_ISSET(fe->fd, &es->wset) && fe->w_proc) {
@@ -195,6 +196,7 @@ static int select_event_wait(EVENT *ev, int timeout)
195196
array_append(es->r_ready, fe);
196197
#else
197198
fe->r_proc(ev, fe);
199+
fe->mask |= EVENT_ONCE;
198200
#endif
199201
}
200202
if (FD_ISSET(fe->fd, &wset) && fe->w_proc) {
@@ -206,6 +208,10 @@ static int select_event_wait(EVENT *ev, int timeout)
206208
#endif
207209
}
208210
}
211+
212+
#ifndef DELAY_CALL
213+
fe->mask &= ~EVENT_ONCE;
214+
#endif
209215
}
210216

211217
#ifdef DELAY_CALL
@@ -271,6 +277,11 @@ EVENT *event_select_create(int size)
271277
es->event.name = select_name;
272278
es->event.handle = select_handle;
273279
es->event.free = select_free;
280+
#ifdef DELAY_CALL
281+
es->event.flag = EVENT_F_SELECT;
282+
#else
283+
es->event.flag = EVENT_F_SELECT | EVENT_F_USE_ONCE;
284+
#endif
274285

275286
es->event.event_wait = select_event_wait;
276287
es->event.checkfd = (event_oper *) select_checkfd;

c/src/fiber.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,11 @@ int acl_fiber_yield(void)
552552

553553
// Reset the current fiber's status in order to be added to
554554
// ready queue again.
555-
__thread_fiber->running->status = FIBER_STATUS_NONE;
556-
FIBER_READY(__thread_fiber->running);
557-
acl_fiber_switch();
555+
if (__thread_fiber->running != NULL) {
556+
__thread_fiber->running->status = FIBER_STATUS_NONE;
557+
FIBER_READY(__thread_fiber->running);
558+
acl_fiber_switch();
559+
}
558560

559561
return 1;
560562
}

0 commit comments

Comments
 (0)