Linux提供了選擇、輪詢和epoll接口來實(shí)現(xiàn)IO重用,下文是武林技術(shù)頻道小編為大家介紹的詳解基于select、poll、epoll的區(qū)別,希望對(duì)你學(xué)習(xí)這方面知識(shí)有所幫助。?
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
?
select、poll、epoll_wait參數(shù)及實(shí)現(xiàn)對(duì)比
1.select的第一個(gè)參數(shù)nfds為fdset集合中最大描述符值加1,fdset是一個(gè)位數(shù)組,其大小限制為__FD_SETSIZE(1024),位數(shù)組的每一位代表其對(duì)應(yīng)的描述符是否需要被檢查。
select的第二三四個(gè)參數(shù)表示需要關(guān)注讀、寫、錯(cuò)誤事件的文件描述符位數(shù)組,這些參數(shù)既是輸入?yún)?shù)也是輸出參數(shù),可能會(huì)被內(nèi)核修改用于標(biāo)示哪些描述符上發(fā)生了關(guān)注的事件。所以每次調(diào)用select前都需要重新初始化fdset。
timeout參數(shù)為超時(shí)時(shí)間,該結(jié)構(gòu)會(huì)被內(nèi)核修改,其值為超時(shí)剩余的時(shí)間。
?
select對(duì)應(yīng)于內(nèi)核中的sys_select調(diào)用,sys_select首先將第二三四個(gè)參數(shù)指向的fd_set拷貝到內(nèi)核,然后對(duì)每個(gè)被SET的描述符調(diào)用進(jìn)行poll,并記錄在臨時(shí)結(jié)果中(fdset),如果有事件發(fā)生,select會(huì)將臨時(shí)結(jié)果寫到用戶空間并返回;當(dāng)輪詢一遍后沒有任何事件發(fā)生時(shí),如果指定了超時(shí)時(shí)間,則select會(huì)睡眠到超時(shí),睡眠結(jié)束后再進(jìn)行一次輪詢,并將臨時(shí)結(jié)果寫到用戶空間,然后返回。
select返回后,需要逐一檢查關(guān)注的描述符是否被SET(事件是否發(fā)生)。
2.poll與select不同,通過一個(gè)pollfd數(shù)組向內(nèi)核傳遞需要關(guān)注的事件,故沒有描述符個(gè)數(shù)的限制,pollfd中的events字段和revents分別用于標(biāo)示關(guān)注的事件和發(fā)生的事件,故pollfd數(shù)組只需要被初始化一次。
poll的實(shí)現(xiàn)機(jī)制與select類似,其對(duì)應(yīng)內(nèi)核中的sys_poll,只不過poll向內(nèi)核傳遞pollfd數(shù)組,然后對(duì)pollfd中的每個(gè)描述符進(jìn)行poll,相比處理fdset來說,poll效率更高。
poll返回后,需要對(duì)pollfd中的每個(gè)元素檢查其revents值,來得指事件是否發(fā)生。
3.epoll通過epoll_create創(chuàng)建一個(gè)用于epoll輪詢的描述符,通過epoll_ctl添加/修改/刪除事件,通過epoll_wait檢查事件,epoll_wait的第二個(gè)參數(shù)用于存放結(jié)果。
epoll與select、poll不同,首先,其不用每次調(diào)用都向內(nèi)核拷貝事件描述信息,在第一次調(diào)用后,事件信息就會(huì)與對(duì)應(yīng)的epoll描述符關(guān)聯(lián)起來。另外epoll不是通過輪詢,而是通過在等待的描述符上注冊(cè)回調(diào)函數(shù),當(dāng)事件發(fā)生時(shí),回調(diào)函數(shù)負(fù)責(zé)把發(fā)生的事件存儲(chǔ)在就緒事件鏈表中,最后寫到用戶空間。
epoll返回后,該參數(shù)指向的緩沖區(qū)中即為發(fā)生的事件,對(duì)緩沖區(qū)中每個(gè)元素進(jìn)行處理即可,而不需要像poll、select那樣進(jìn)行輪詢檢查。
select、poll、epoll_wait性能對(duì)比
select、poll的內(nèi)部實(shí)現(xiàn)機(jī)制相似,性能差別主要在于向內(nèi)核傳遞參數(shù)以及對(duì)fdset的位操作上,另外,select存在描述符數(shù)的硬限制,不能處理很大的描述符集合。這里主要考察poll與epoll在不同大小描述符集合的情況下性能的差異。
測(cè)試程序會(huì)統(tǒng)計(jì)在不同的文件描述符集合的情況下,1s內(nèi)poll與epoll調(diào)用的次數(shù)。統(tǒng)計(jì)結(jié)果如下,從結(jié)果可以看出,對(duì)poll而言,每秒鐘內(nèi)的系統(tǒng)調(diào)用數(shù)目雖集合增大而很快降低,而epoll基本保持不變,具有很好的擴(kuò)展性。
?
?
| 描述符集合大小 | poll | epoll |
| 1 | 331598 | 258604 |
| 10 | 330648 | 297033 |
| 100 | 91199 | 288784 |
| 1000 | 27411 | 296357 |
| 5000 | 5943 | 288671 |
| 10000 | 2893 | 292397 |
| 25000 | 1041 | 285905 |
| 50000 | 536 | 293033 |
| 100000 | 224 | 285825 |
一、連接數(shù)
我本人也曾經(jīng)在項(xiàng)目中用過select和epoll,對(duì)于select,感觸最深的是linux下select最大數(shù)目限制(windows 下似乎沒有限制),每個(gè)進(jìn)程的select最多能處理FD_SETSIZE個(gè)FD(文件句柄),
如果要處理超過1024個(gè)句柄,只能采用多進(jìn)程了。
常見的使用slect的多進(jìn)程模型是這樣的: 一個(gè)進(jìn)程專門accept,成功后將fd通過unix socket傳遞給子進(jìn)程處理,父進(jìn)程可以根據(jù)子進(jìn)程負(fù)載分派。曾經(jīng)用過1個(gè)父進(jìn)程+4個(gè)子進(jìn)程 承載了超過4000個(gè)的負(fù)載。
這種模型在我們當(dāng)時(shí)的業(yè)務(wù)運(yùn)行的非常好。epoll在連接數(shù)方面沒有限制,當(dāng)然可能需要用戶調(diào)用API重現(xiàn)設(shè)置進(jìn)程的資源限制。
二、IO差別
1、select的實(shí)現(xiàn)
這段可以結(jié)合linux內(nèi)核代碼描述了,我使用的是2.6.28,其他2.6的代碼應(yīng)該差不多吧。
先看看select:
select系統(tǒng)調(diào)用的代碼在fs/Select.c下,
?
?
?
?
?
asmlinkage long sys_select(int n, fd_set __user *inp, fd_set __user *outp,
??????????? fd_set __user *exp, struct timeval __user *tvp)
{
??? struct timespec end_time, *to = NULL;
??? struct timeval tv;
??? int ret;
??? if (tvp) {
??????? if (copy_from_user(&tv, tvp, sizeof(tv)))
??????????? return -EFAULT;
??????? to = &end_time;
??????? if (poll_select_set_timeout(to,
??????????????? tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
??????????????? (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
??????????? return -EINVAL;
??? }
??? ret = core_sys_select(n, inp, outp, exp, to);
??? ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
??? return ret;
}
前面是從用戶控件拷貝各個(gè)fd_set到內(nèi)核空間,接下來的具體工作在core_sys_select中,
core_sys_select->do_select,真正的核心內(nèi)容在do_select里:
?
?
?
int do_select(int n, fd_set_bits *fds, struct timespec *end_time)
{
??? ktime_t expire, *to = NULL;
??? struct poll_wqueues table;
??? poll_table *wait;
??? int retval, i, timed_out = 0;
??? unsigned long slack = 0;
??? rcu_read_lock();
??? retval = max_select_fd(n, fds);
??? rcu_read_unlock();
??? if (retval < 0)
??????? return retval;
??? n = retval;
??? poll_initwait(&table);
??? wait = &table.pt;
??? if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
??????? wait = NULL;
??????? timed_out = 1;
??? }
??? if (end_time && !timed_out)
??????? slack = estimate_accuracy(end_time);
??? retval = 0;
??? for (;;) {
??????? unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
??????? set_current_state(TASK_INTERRUPTIBLE);
??????? inp = fds->in; outp = fds->out; exp = fds->ex;
??????? rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
??????? for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
??????????? unsigned long in, out, ex, all_bits, bit = 1, mask, j;
??????????? unsigned long res_in = 0, res_out = 0, res_ex = 0;
??????????? const struct file_operations *f_op = NULL;
??????????? struct file *file = NULL;
??????????? in = *inp++; out = *outp++; ex = *exp++;
??????????? all_bits = in | out | ex;
??????????? if (all_bits == 0) {
??????????????? i += __NFDBITS;
??????????????? continue;
??????????? }
??????????? for (j = 0; j < __NFDBITS; ++j, ++i, bit <<= 1) {
??????????????? int fput_needed;
??????????????? if (i >= n)
??????????????????? break;
??????????????? if (!(bit & all_bits))
??????????????????? continue;
??????????????? file = fget_light(i, &fput_needed);
??????????????? if (file) {
??????????????????? f_op = file->f_op;
??????????????????? mask = DEFAULT_POLLMASK;
??????????????????? if (f_op && f_op->poll)
??????????????????????? mask = (*f_op->poll)(file, retval ? NULL : wait);
??????????????????? fput_light(file, fput_needed);
??????????????????? if ((mask & POLLIN_SET) && (in & bit)) {
??????????????????????? res_in |= bit;
??????????????????????? retval++;
??????????????????? }
??????????????????? if ((mask & POLLOUT_SET) && (out & bit)) {
??????????????????????? res_out |= bit;
??????????????????????? retval++;
??????????????????? }
??????????????????? if ((mask & POLLEX_SET) && (ex & bit)) {
??????????????????????? res_ex |= bit;
??????????????????????? retval++;
??????????????????? }
??????????????? }
??????????? }
??????????? if (res_in)
??????????????? *rinp = res_in;
??????????? if (res_out)
??????????????? *routp = res_out;
??????????? if (res_ex)
??????????????? *rexp = res_ex;
??????????? cond_resched();
??????? }
??????? wait = NULL;
??????? if (retval || timed_out || signal_pending(current))
??????????? break;
??????? if (table.error) {
??????????? retval = table.error;
??????????? break;
??????? }
??????? /*
???????? * If this is the first loop and we have a timeout
???????? * given, then we convert to ktime_t and set the to
???????? * pointer to the expiry value.
???????? */
??????? if (end_time && !to) {
??????????? expire = timespec_to_ktime(*end_time);
??????????? to = &expire;
??????? }
??????? if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS))
??????????? timed_out = 1;
??? }
??? __set_current_state(TASK_RUNNING);
??? poll_freewait(&table);
??? return retval;
}
上面的代碼很多,其實(shí)真正關(guān)鍵的代碼是這一句:
?
?
?
mask = (*f_op->poll)(file, retval ? NULL : wait);
這個(gè)是調(diào)用文件系統(tǒng)的 poll函數(shù),不同的文件系統(tǒng)poll函數(shù)自然不同,由于我們這里關(guān)注的是tcp連接,而socketfs的注冊(cè)在 net/Socket.c里。
register_filesystem(&sock_fs_type);
socket文件系統(tǒng)的函數(shù)也是在net/Socket.c里:
static const struct file_operations socket_file_ops = {
??? .owner =??? THIS_MODULE,
??? .llseek =??? no_llseek,
??? .aio_read =??? sock_aio_read,
??? .aio_write =??? sock_aio_write,
??? .poll =??????? sock_poll,
??? .unlocked_ioctl = sock_ioctl,
#ifdef CONFIG_COMPAT
??? .compat_ioctl = compat_sock_ioctl,
#endif
??? .mmap =??????? sock_mmap,
??? .open =??????? sock_no_open,??? /* special open code to disallow open via /proc */
??? .release =??? sock_close,
??? .fasync =??? sock_fasync,
??? .sendpage =??? sock_sendpage,
??? .splice_write = generic_splice_sendpage,
??? .splice_read =??? sock_splice_read,
};
從sock_poll跟隨下去,
最后可以到 net/ipv4/tcp.c的
unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
這個(gè)是最終的查詢函數(shù),
也就是說select 的核心功能是調(diào)用tcp文件系統(tǒng)的poll函數(shù),不停的查詢,如果沒有想要的數(shù)據(jù),主動(dòng)執(zhí)行一次調(diào)度(防止一直占用cpu),直到有一個(gè)連接有想要的消息為止。
從這里可以看出select的執(zhí)行方式基本就是不同的調(diào)用poll,直到有需要的消息為止,如果select 處理的socket很多,這其實(shí)對(duì)整個(gè)機(jī)器的性能也是一個(gè)消耗。
2、epoll的實(shí)現(xiàn)
epoll的實(shí)現(xiàn)代碼在 fs/EventPoll.c下,
由于epoll涉及到幾個(gè)系統(tǒng)調(diào)用,這里不逐個(gè)分析了,僅僅分析幾個(gè)關(guān)鍵點(diǎn),
第一個(gè)關(guān)鍵點(diǎn)在
static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
???????????? struct file *tfile, int fd)
這是在我們調(diào)用sys_epoll_ctl 添加一個(gè)被管理socket的時(shí)候調(diào)用的函數(shù),關(guān)鍵的幾行如下:
?
?
?
epq.epi = epi;
??? init_poll_funcptr(&epq.pt, ep_ptable_queue_proc);
??? /*
???? * Attach the item to the poll hooks and get current event bits.
???? * We can safely use the file* here because its usage count has
???? * been increased by the caller of this function. Note that after
???? * this operation completes, the poll callback can start hitting
???? * the new item.
???? */
??? revents = tfile->f_op->poll(tfile, &epq.pt);
這里也是調(diào)用文件系統(tǒng)的poll函數(shù),不過這次初始化了一個(gè)結(jié)構(gòu),這個(gè)結(jié)構(gòu)會(huì)帶有一個(gè)poll函數(shù)的callback函數(shù):ep_ptable_queue_proc,
在調(diào)用poll函數(shù)的時(shí)候,會(huì)執(zhí)行這個(gè)callback,這個(gè)callback的功能就是將當(dāng)前進(jìn)程添加到 socket的等待進(jìn)程上。
?
?
?
static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
???????????????? poll_table *pt)
{
??? struct epitem *epi = ep_item_from_epqueue(pt);
??? struct eppoll_entry *pwq;
??? if (epi->nwait >= 0 && (pwq = kmem_cache_alloc(pwq_cache, GFP_KERNEL))) {
??????? init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
??????? pwq->whead = whead;
??????? pwq->base = epi;
??????? add_wait_queue(whead, &pwq->wait);
??????? list_add_tail(&pwq->llink, &epi->pwqlist);
??????? epi->nwait++;
??? } else {
??????? /* We have to signal that an error occurred */
??????? epi->nwait = -1;
??? }
}?
注意到參數(shù) whead 實(shí)際上是 sk->sleep,其實(shí)就是將當(dāng)前進(jìn)程添加到sk的等待隊(duì)列里,當(dāng)該socket收到數(shù)據(jù)或者其他事件觸發(fā)時(shí),會(huì)調(diào)用
sock_def_readable 或者sock_def_write_space 通知函數(shù)來喚醒等待進(jìn)程,這2個(gè)函數(shù)都是在socket創(chuàng)建的時(shí)候填充在sk結(jié)構(gòu)里的。
從前面的分析來看,epoll確實(shí)是比select聰明的多、輕松的多,不用再苦哈哈的去輪詢了。
以上就是武林技術(shù)頻道小編給大家介紹的詳解基于select、poll、epoll的區(qū)別,在以上的內(nèi)容中,以上的全部?jī)?nèi)容供大家參考,請(qǐng)多關(guān)注武林技術(shù)頻道!