作者:董昊 (要转载的同学帮忙把名字和博客链接 http://donghao.org/uii/带上,多谢了!)
poll 和 epoll 的使用应该不用再多说了。当 fd 很多时,使用 epoll 比 poll 效率更高。我们通过内
核源码分析来看看到底是为什么。
poll 剖析
poll 系统调用:
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
内核 2.6.9 对应的实现代码为:
[fs/select.c -->sys_poll]
456 asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long
/* Do a sanity check on nfds ... */ /* 用户给的 nfds 数不可以超过一个 struct file
结构支持的最大 fd 数(默认是 256)*/
if (nfds > current->files->max_fdset && nfds > OPEN_MAX)
/* Careful about overflow in the intermediate values */
if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
timeout = (unsigned long)(timeout*HZ+999)/1000+1;
else /* Negative or overflow */
timeout = MAX_SCHEDULE_TIMEOUT;
其中 poll_initwait 较为关键,从字面上看,应该是初始化变量 table,注意此处 table 在整个执
行 poll 的过程中是很关键的变量。
而 struct poll_table 其实就只包含了一个函数指针:
[fs/poll.h]
16 /*
17 * structures and helpers for f_op->poll implementations
18 */
19 typedef void (*poll_queue_proc)(struct file *, wait_queue_head_t *, struct
poll_table_struct *);
20
timeout)
457 {
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
struct poll_wqueues table;
int fdcount, err;
unsigned int i;
struct poll_list *head;
struct poll_list *walk;
return -EINVAL;
if (timeout) {
}
poll_initwait(&table);
21 typedef struct poll_table_struct {
22
23 } poll_table;
poll_queue_proc qproc;
现在我们来看看 poll_initwait 到底在做些什么
[fs/select.c]
57 void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table
*p);
58
59 void poll_initwait(struct poll_wqueues *pwq)
60 {
61
62
63
64 }
&(pwq->pt)->qproc = __pollwait; /*此行已经被我“翻译”了,方便观看*/
pwq->error = 0;
pwq->table = NULL;
很明显,poll_initwait 的主要动作就是把 table 变量的成员 poll_table 对应的回调函数置为
__pollwait。这个__pollwait 不仅是 poll 系统调用需要,select 系统调用也一样是用这个
__pollwait,说白了,这是个操作系统的异步操作的“御用”回调函数。当然了,epoll 没有用这
个,它另外新增了一个回调函数,以达到其高效运转的目的,这是后话,暂且不表。
我们先不讨论__pollwait 的具体实现,还是继续看 sys_poll:
[fs/select.c -->sys_poll]
head = NULL;
walk = NULL;
i = nfds;
err = -ENOMEM;
while(i!=0) {
struct poll_list *pp;
pp = kmalloc(sizeof(struct poll_list)+
sizeof(struct pollfd)*
(i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
GFP_KERNEL);
if(pp==NULL)
goto out_fds;
pp->next=NULL;
pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
if (head == NULL)
walk = pp;
if (copy_from_user(pp->entries, ufds + nfds-i,
sizeof(struct pollfd)*pp->len)) {
head = pp;
else
walk->next = pp;
err = -EFAULT;
goto out_fds;
}
i -= pp->len;
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
}
fdcount = do_poll(nfds, head, &table, timeout);
这一大堆代码就是建立一个链表,每个链表的节点是一个 page 大小(通常是 4k),这链表节点
由一个指向 struct poll_list 的指针掌控,而众多的 struct pollfd 就通过 struct_list 的 entries
成员访问。上面的循环就是把用户态的 struct pollfd 拷进这些 entries 里。通常用户程序的 poll
调用就监控几个 fd,所以上面这个链表通常也就只需要一个节点,即操作系统的一页。但是,当
用户传入的 fd 很多时,由于 poll 系统调用每次都要把所有 struct pollfd 拷进内核,所以参数传
递和页分配此时就成了 poll 系统调用的性能瓶颈。
最后一句 do_poll,我们跟进去:
[fs/select.c-->sys_poll()-->do_poll()]
poll_table ** pwait, int *count)
int i;
for (i = 0; i < num; i++) {
int fd;
unsigned int mask;
struct pollfd *fdp;
mask = 0;
fdp = fdpage+i;
fd = fdp->fd;
if (fd >= 0) {
mask = DEFAULT_POLLMASK;
if (file->f_op && file->f_op->poll)
mask = file->f_op->poll(file, *pwait);
mask &= fdp->events | POLLERR | POLLHUP;
fput(file);
struct file * file = fget(fd);
mask = POLLNVAL;
if (file != NULL) {
395 static void do_pollfd(unsigned int num, struct pollfd * fdpage,
396
397 {
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425 }
426
427 static int do_poll(unsigned int nfds, struct poll_list *list,
428
429 {
430
431
432
433
434
435
436
437
438
439
440
struct poll_list *walk;
set_current_state(TASK_INTERRUPTIBLE);
walk = list;
while(walk != NULL) {
int count = 0;
poll_table* pt = &wait->pt;
if (!timeout)
pt = NULL;
for (;;) {
}
if (mask) {
*pwait = NULL;
(*count)++;
}
}
fdp->revents = mask;
}
struct poll_wqueues *wait, long timeout)
do_pollfd( walk->len, walk->entries, &pt, &count);
walk = walk->next;
}
pt = NULL;
if (count || !timeout || signal_pending(current))
break;
count = wait->error;
if (count)
break;
441
442
443
444
445
446
447
448
449
450
451
452
453
454 }
timeout = schedule_timeout(timeout); /* 让 current 挂起,别的进程跑,
timeout 到了以后再回来运行 current*/
}
__set_current_state(TASK_RUNNING);
return count;
注意 438 行的 set_current_state 和 445 行的 signal_pending,它们两句保障了当用户程序在
调用 poll 后挂起时,发信号可以让程序迅速推出 poll 调用,而通常的系统调用是不会被信号打断
的。
纵览 do_poll 函数,主要是在循环内等待,直到 count 大于 0 才跳出循环,而 count 主要是靠
do_pollfd 函数处理。
注意标红的 440-443 行,当用户传入的 fd 很多时(比如 1000 个),对 do_pollfd 就会调用很
多次,poll 效率瓶颈的另一原因就在这里。
do_pollfd 就是针对每个传进来的 fd,调用它们各自对应的 poll 函数,简化一下调用过程,如
下:
struct file* file = fget(fd);
file->f_op->poll(file, &(table->pt));
如果 fd 对应的是某个 socket,do_pollfd 调用的就是网络设备驱动实现的 poll;如果 fd 对应的
是某个 ext3 文件系统上的一个打开文件,那 do_pollfd 调用的就是 ext3 文件系统驱动实现的
poll。一句话,这个 file->f_op->poll 是设备驱动程序实现的,那设备驱动程序的 poll 实现通常
又是什么样子呢?其实,设备驱动程序的标准实现是:调用 poll_wait,即以设备自己的等待队列
为参数(通常设备都有自己的等待队列,不然一个不支持异步操作的设备会让人很郁闷)调用
struct poll_table 的回调函数。
作为驱动程序的代表,我们看看 socket 在使用 tcp 时的代码:
[net/ipv4/tcp.c-->tcp_poll]
329 unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
330 {
331
332
333
334
335
unsigned int mask;
struct sock *sk = sock->sk;
struct tcp_opt *tp = tcp_sk(sk);
poll_wait(file, sk->sk_sleep, wait);
代码就看这些,剩下的无非就是判断状态、返回状态值,tcp_poll 的核心实现就是 poll_wait,而
poll_wait 就是调用 struct poll_table 对应的回调函数,那 poll 系统调用对应的回调函数就是
__poll_wait,所以这里几乎就可以把 tcp_poll 理解为一个语句:
__poll_wait(file, sk->sk_sleep, wait);
由此也可以看出,每个 socket 自己都带有一个等待队列 sk_sleep,所以上面我们所说的“设备的
等待队列”其实不止一个。
这时候我们再看看__poll_wait 的实现:
[fs/select.c-->__poll_wait()]
89 void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table
*_p)
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 }
struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
struct poll_table_page *table = p->table;
if (!table || POLL_TABLE_FULL(table)) {
struct poll_table_page *new_table;
new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
if (!new_table) {
p->error = -ENOMEM;
__set_current_state(TASK_RUNNING);
return;
}
new_table->entry = new_table->entries;
new_table->next = table;
p->table = new_table;
table = new_table;
}
/* Add a new entry */
{
struct poll_table_entry * entry = table->entry;
table->entry = entry+1;
get_file(filp);
entry->filp = filp;
entry->wait_address = wait_address;
init_waitqueue_entry(&entry->wait, current);
add_wait_queue(wait_address,&entry->wait);
}
__poll_wait 的作用就是创建了上图所示的数据结构(一次__poll_wait 即一次设备 poll 调用只
创建一个 poll_table_entry),并通过 struct poll_table_entry 的 wait 成员,把 current 挂在
了设备的等待队列上,此处的等待队列是 wait_address,对应 tcp_poll 里的 sk->sk_sleep。
现在我们可以回顾一下 poll 系统调用的原理了:先注册回调函数__poll_wait,再初始化 table 变
量(类型为 struct poll_wqueues),接着拷贝用户传入的 struct pollfd(其实主要是 fd),然
后轮流调用所有 fd 对应的 poll(把 current 挂到各个 fd 对应的设备等待队列上)。在设备收到一
条消息(网络设备)或填写完文件数据(磁盘设备)后,会唤醒设备等待队列上的进程,这时
current 便被唤醒了。current 醒来后离开 sys_poll 的操作相对简单,这里就不逐行分析了。