Linux中異步IO等待無非就三個系統調用:select, poll和epoll。不少人沒法理解三種調用的區別,或不夠了解,今天就結合Linux kernel code詳細描述三個的區別!javascript
select:java
select 的限制就是最大1024個fd,能夠查看kernel中的posix_types.h,裏面定義了fdset數據結構,顯然select不適合poll大量fd的場景(如webserver)。 linux
include/linux/posix_types.h :web
- #undef __NFDBITS
- #define __NFDBITS (8 * sizeof(unsigned long))
-
- #undef __FD_SETSIZE
- #define __FD_SETSIZE 1024
-
- #undef __FDSET_LONGS
- #define __FDSET_LONGS (__FD_SETSIZE/__NFDBITS)
-
- #undef __FDELT
- #define __FDELT(d) ((d) / __NFDBITS)
-
- #undef __FDMASK
- #define __FDMASK(d) (1UL << ((d) % __NFDBITS))
-
- typedef struct {
- unsigned long fds_bits [__FDSET_LONGS];
- } __kernel_fd_set;
poll:數組
poll相對於select改進了fdset size的限制,poll沒有再使用fdset數組結構,反而使用了pollfd,這樣用戶能夠自定義很是大的pollfd數組,這個pollfd數組在kernel中的表現形式是poll_list鏈表,這樣就不存在了1024的限制了,除此以外poll相比select無太大區別。數據結構
- int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
- struct timespec *end_time)
- {
- struct poll_wqueues table;
- int err = -EFAULT, fdcount, len, size;
- /* Allocate small arguments on the stack to save memory and be
- faster - use long to make sure the buffer is aligned properly
- on 64 bit archs to avoid unaligned access */
- long stack_pps[POLL_STACK_ALLOC/sizeof(long)];
- struct poll_list *const head = (struct poll_list *)stack_pps;
- struct poll_list *walk = head;
- unsigned long todo = nfds;
-
- if (nfds > rlimit(RLIMIT_NOFILE))
- return -EINVAL;
-
- len = min_t(unsigned int, nfds, N_STACK_PPS);
- for (;;) {
- walk->next = NULL;
- walk->len = len;
- if (!len)
- break;
-
- if (copy_from_user(walk->entries, ufds + nfds-todo,
- sizeof(struct pollfd) * walk->len))
- goto out_fds;
-
- todo -= walk->len;
- if (!todo)
- break;
-
- len = min(todo, POLLFD_PER_PAGE);
- size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
- walk = walk->next = kmalloc(size, GFP_KERNEL);
- if (!walk) {
- err = -ENOMEM;
- goto out_fds;
- }
- }
epoll:app
select與poll的共同點是fd有數據後kernel會遍歷全部fd,找到有效fd後初始化相應的revents,用戶空間程序須再次遍歷整個fdset,以找到有效的fd,這樣實際上就遍歷了兩次fd數組表,對於極大量fd的狀況,這樣的性能很是很差,請看一下do_poll代碼:less
- static int do_poll(unsigned int nfds, struct poll_list *list,
- struct poll_wqueues *wait, struct timespec *end_time)
- {
- poll_table* pt = &wait->pt;
- ktime_t expire, *to = NULL;
- int timed_out = 0, count = 0;
- unsigned long slack = 0;
-
- /* Optimise the no-wait case */
- if (end_time && !end_time->tv_sec && !end_time->tv_nsec) {
- pt = NULL;
- timed_out = 1;
- }
-
- if (end_time && !timed_out)
- slack = select_estimate_accuracy(end_time);
-
- for (;;) {
- struct poll_list *walk;
-
- for (walk = list; walk != NULL; walk = walk->next) {
- struct pollfd * pfd, * pfd_end;
-
- pfd = walk->entries;
- pfd_end = pfd + walk->len;
- for (; pfd != pfd_end; pfd++) {
- /*
- * Fish for events. If we found one, record it
- * and kill the poll_table, so we don't
- * needlessly register any other waiters after
- * this. They'll get immediately deregistered
- * when we break out and return.
- */
- if (do_pollfd(pfd, pt)) {
- count++;
- pt = NULL;
- }
- }
- }
epoll的出現解決了這種問題,那麼epoll是如何作到的呢? 咱們知道select, poll和epoll都是使用waitqueue調用callback函數去wakeup你的異步等待線程的,若是設置了timeout的話就起一個hrtimer,select和poll的callback函數並無作什麼事情,但epoll的waitqueue callback函數把當前的有效fd加到ready list,而後喚醒異步等待進程,因此你的epoll函數返回的就是這個ready list, ready list中包含全部有效的fd,這樣一來kernel不用去遍歷全部的fd,用戶空間程序也不用遍歷全部的fd,而只是遍歷返回有效fd鏈表,因此epoll天然比select和poll更適合大數量fd的場景。異步
- static int ep_send_events(struct eventpoll *ep,
- struct epoll_event __user *events, int maxevents)
- {
- struct ep_send_events_data esed;
-
- esed.maxevents = maxevents;
- esed.events = events;
-
- return ep_scan_ready_list(ep, ep_send_events_proc, &esed);
- }
如今你們應該明白select, poll和epoll的區別了吧!有人問既然select和poll有這麼明顯的缺陷,爲何不改掉kernel中的實現呢? 緣由很簡單,後向ABI兼容,select和poll的ABI沒法返回ready list,只能返回整個fd數組,因此用戶只得再次遍歷整個fd數組以找到哪些fd是有數據的。函數
epoll還包括 「Level-Triggered」 和 「Edge-Triggered」,這兩個概念在這裏就很少贅述了,由於"man epoll"裏面解釋的很是詳細,還有使用epoll的example。
原文出自:http://bookjovi.iteye.com/blog/1186736