Why return value from epoll_wait are always far lower than connections?

yucheng.tw nginx-forum at forum.nginx.org
Fri Jul 2 15:18:29 UTC 2021


Hello, I'm new to this forum and not familiar with mail list system. If
there're something I did wrong, please tell me. 

In short, I have a general method (batching system call) and I try to apply
into Nginx, then I found the weird behavior and can't 
explain it.

To linux, connections and requests will be handled in `ngx_epoll_module.c`,
following is snapshot about main job of the workers.
Workers will using epoll_wait to get the numbers of events which are
already, then handle them in the following `for` loop. In each
iteration, worker will do some thing like sending file (system call:
sendfile) to client.

```c
ngx_epoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t
flags)
{
    ...
    events = epoll_wait(ep, event_list, (int) nevents, timer);
    ...
    for (i = 0; i < events; i++) {
          // handle events
    ....
    }
```

As I observed, the value of `events` is relative to the numbers of
connections. For example, in wrk (benchmarking tool), when I
set `c` option to 50, the value of events will usually be close to 50. As
service time grow, the change will like:

```
1->1->1->50->50..... (close to 50)->50....
```

So far, it really make sense. Then, I apply my method: batching system call
in each iteration. Following is snapshot:

```
ngx_epoll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t
flags)
{
    ...
    events = epoll_wait(ep, event_list, (int) nevents, timer);
    ...

 my_batching_entry();
   
    for (i = 0; i < events; i++) {
          // handle events
    }

 my_batching_exit();

```

In above model, instead of executing system call (such as sendfile64) in
each iteration, all of them will be executed until `my_batching_exit()` was
called. It seems that it works correct (at least in wrk).

Ok, here is the part I found weird. Applying my method, I found that the
value (events) return from `epoll_wait` will always be 1 or some really low
value (far from 50). Are there any mechanism to cause it? (low numbers of
ready events) ,Did Nginx think the requests were served too slow , so
lowering my event numbers?

In my method, I only add two lines into Nginx (batching_entry and
batching_exit) and Makefile. I means I didn't do a lot changes with Nginx.

Are there any possible to cause the number of events are far lower than
connections number? Any suggestions will be appreciated!

Thanks in advance!


- Steven

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,291972,291972#msg-291972



More information about the nginx mailing list