翼度科技»论坛 云主机 LINUX 查看内容

IO多路复用

10

主题

10

帖子

30

积分

新手上路

Rank: 1

积分
30
1.IO多路复用的概念


  • 单线程或单进程同时监测若干个文件描述符是否可以执行IO操作的能力。
2.为什么出现IO多路复用



  • 服务器需要维护N多个与客户端之间的socketfd;并且在receive之前需要知道数据知否出现---》组件IO多路复用技术出现---》解决检测服务器端N多个fd的状态

    • Tcp是有连接的,Udp是无连接---》上述情况出现在Tcp连接情况

  • IO多路复用的三种方案:select/poll/epoll

    • select(fds+1, rds, wds, timeout)
    • poll(fds, nfd, timeout)
    • epoll

      • epoll_create(size/flags)--》创建根节点---》epoll实例
      • epoll_ctl(fd, op, events)---》挂载fd节点
      • epoll_wait(fd, events, maxevents, timeout)---》检测


  • Tcp建立连接---》需要的fds放入IO多路复用中管理---》检测fd缓存状态---》receive

    • receive(fd,buff,sizeof(buff),0)

3.IO多路复用区别特点

selectpollepoll性能随IO处理数增加,性能逐渐下降;并且连接数有限制随IO处理数增加,性能逐渐下降随IO处理数增加,性能基本上不会下降连接数最大连接数1024;处理1024以上则需要修改宏FD_SETSIZE,重新编译无限制无限制数据结构不确定,有数组的可能,但是为线性结构不确定,有链表的可能,但是为线性结构红黑树,队列处理方式线性轮询线性轮询回调callback内存拷贝所有fds从用户空间和内核空间来回拷贝**所有fds从用户空间和内核空间来回拷贝epoll_wait()只需要从就绪队列上拷贝由内核空间到用户空间使用复杂度低低中时间复杂度O(n)O(n)O()

  • select/poll

    • 拷贝fds--》从用户空间至内核
    • for(;;)进行遍历判断

      • select:进行分类rds,wds,eds
      • poll:pollfd维护自身状态

    • 从内核拷贝至用户空间

  • epoll

    • epoll_create--->创建一个根节点
    • epoll_ctl---》挂载节点
    • epoll_wait---》从内核到用户只拷贝满足条件的节点,从就绪队列中拷贝

  • 在哪些场景下 select比epoll更合适?

    • IO数量不多,且使用多线程多进程的情况使用select比epoll更合适

      • epoll使用红黑树,必须需要加锁,消耗的资源更多


红黑树:
​        等待补充链接
3.IO多路复用源码


  • select:kern_select---》core_sys_select---》do_select---linux-6.0.2\fs\select.c

      1. static int kern_select(int n, fd_set __user *inp, fd_set __user *outp,fd_set __user *exp, struct __kernel_old_timeval __user *tvp){
      2.         struct timespec64 end_time, *to = NULL;
      3.         struct __kernel_old_timeval tv;
      4.         int ret;
      5.         if (tvp) {
      6.         //copy_from_user从用户空间拷贝fds
      7.                 if (copy_from_user(&tv, tvp, sizeof(tv)))
      8.                         return -EFAULT;
      9.                 to = &end_time;
      10.                 if (poll_select_set_timeout(to,
      11.                                 tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
      12.                                 (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
      13.                         return -EINVAL;
      14.         }
      15.     //调用select--》core_sys_select
      16.         ret = core_sys_select(n, inp, outp, exp, to);
      17.         return poll_select_finish(&end_time, tvp, PT_TIMEVAL, ret);
      18. }
      复制代码
      1. int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
      2.                            fd_set __user *exp, struct timespec64 *end_time)
      3. {
      4.         ......
      5.     //加读锁,读取文件数量
      6.     rcu_read_lock();
      7.         fdt = files_fdtable(current->files);
      8.         max_fds = fdt->max_fds;
      9.         rcu_read_unlock();
      10.     ......
      11.         //select底层处理---》do_select
      12.         ret = do_select(n, &fds, end_time);
      13.         if (ret < 0)
      14.                 goto out;
      15.         if (!ret) {
      16.                 ret = -ERESTARTNOHAND;
      17.                 if (signal_pending(current))
      18.                         goto out;
      19.                 ret = 0;
      20.         }
      21.         ......
      22. out_nofds:
      23.         return ret;
      24. }
      复制代码
      1. static int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
      2. {
      3.     .....
      4.     //读锁,读取文件数量
      5.         rcu_read_lock();
      6.         retval = max_select_fd(n, fds);
      7.         rcu_read_unlock();
      8.         .....
      9.     //通过循环遍历,主要流程流程
      10.         for (;;) {
      11.                 unsigned long *rinp, *routp, *rexp, *inp, *outp, *exp;
      12.                 bool can_busy_loop = false;
      13.                 inp = fds->in; outp = fds->out; exp = fds->ex;
      14.                 rinp = fds->res_in; routp = fds->res_out; rexp = fds->res_ex;
      15.                 for (i = 0; i < n; ++rinp, ++routp, ++rexp) {
      16.                         unsigned long in, out, ex, all_bits, bit = 1, j;
      17.                         unsigned long res_in = 0, res_out = 0, res_ex = 0;
      18.                         __poll_t mask;
      19.                         in = *inp++; out = *outp++; ex = *exp++;
      20.                         all_bits = in | out | ex;
      21.                         if (all_bits == 0) {
      22.                                 i += BITS_PER_LONG;
      23.                                 continue;
      24.                         }
      25.                         for (j = 0; j < BITS_PER_LONG; ++j, ++i, bit <<= 1) {
      26.                                 struct fd f;
      27.                                 if (i >= n)
      28.                                         break;
      29.                                 if (!(bit & all_bits))
      30.                                         continue;
      31.                                 mask = EPOLLNVAL;
      32.                                 f = fdget(i);
      33.                                 if (f.file) {
      34.                                         wait_key_set(wait, in, out, bit,
      35.                                                      busy_flag);
      36.                                         mask = vfs_poll(f.file, wait);
      37.                                         fdput(f);
      38.                                 }
      39.                                 if ((mask & POLLIN_SET) && (in & bit)) {
      40.                                         res_in |= bit;
      41.                                         retval++;
      42.                                         wait->_qproc = NULL;
      43.                                 }
      44.                                 if ((mask & POLLOUT_SET) && (out & bit)) {
      45.                                         res_out |= bit;
      46.                                         retval++;
      47.                                         wait->_qproc = NULL;
      48.                                 }
      49.                                 if ((mask & POLLEX_SET) && (ex & bit)) {
      50.                                         res_ex |= bit;
      51.                                         retval++;
      52.                                         wait->_qproc = NULL;
      53.                                 }
      54.                                 if (retval) {
      55.                                         can_busy_loop = false;
      56.                                         busy_flag = 0;
      57.                                 } else if (busy_flag & mask)
      58.                                         can_busy_loop = true;
      59.                         }
      60.                         if (res_in)
      61.                                 *rinp = res_in;
      62.                         if (res_out)
      63.                                 *routp = res_out;
      64.                         if (res_ex)
      65.                                 *rexp = res_ex;
      66.                         cond_resched();
      67.                 }
      68.                 wait->_qproc = NULL;
      69.                 if (retval || timed_out || signal_pending(current))
      70.                         break;
      71.                 if (table.error) {
      72.                         retval = table.error;
      73.                         break;
      74.                 }
      75.                 if (can_busy_loop && !need_resched()) {
      76.                         if (!busy_start) {
      77.                                 busy_start = busy_loop_current_time();
      78.                                 continue;
      79.                         }
      80.                         if (!busy_loop_timeout(busy_start))
      81.                                 continue;
      82.                 }
      83.                 busy_flag = 0;
      84.                 if (end_time && !to) {
      85.                         expire = timespec64_to_ktime(*end_time);
      86.                         to = &expire;
      87.                 }
      88.                 if (!poll_schedule_timeout(&table, TASK_INTERRUPTIBLE,
      89.                                            to, slack))
      90.                         timed_out = 1;
      91.         }
      92.         poll_freewait(&table);
      93.         return retval;
      94. }
      复制代码

  • poll:do_sys_poll---》do_poll---linux-6.0.2\fs\select.c

      1. static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,struct timespec64 *end_time)
      2. {
      3.     ......
      4.         len = min_t(unsigned int, nfds, N_STACK_PPS);
      5.         for (;;) {
      6.         .....
      7.         //copy_from_user从用户空间拷贝fds
      8.                 if (copy_from_user(walk->entries, ufds + nfds-todo,
      9.                                         sizeof(struct pollfd) * walk->len))
      10.                         goto out_fds;
      11.         .....
      12.         }
      13.         .....
      14. out_fds:
      15.         //没有拷贝成功
      16.     .....
      17.         return err;
      18.         .....
      19. }
      复制代码
      1. static int do_poll(struct poll_list *list, struct poll_wqueues *wait,struct timespec64 *end_time)
      2. {
      3.         ......
      4.     //poll底层处理流程
      5.         for (;;) {
      6.                 struct poll_list *walk;
      7.                 bool can_busy_loop = false;
      8.                 //这个写法,似乎是链表
      9.                 for (walk = list; walk != NULL; walk = walk->next) {
      10.                         struct pollfd * pfd, * pfd_end;
      11.                         pfd = walk->entries;
      12.                         pfd_end = pfd + walk->len;
      13.                         for (; pfd != pfd_end; pfd++) {
      14.                                 if (do_pollfd(pfd, pt, &can_busy_loop,
      15.                                               busy_flag)) {
      16.                                         count++;
      17.                                         pt->_qproc = NULL;
      18.                                         /* found something, stop busy polling */
      19.                                         busy_flag = 0;
      20.                                         can_busy_loop = false;
      21.                                 }
      22.                         }
      23.                 }
      24.                 pt->_qproc = NULL;
      25.                 if (!count) {
      26.                         count = wait->error;
      27.                         if (signal_pending(current))
      28.                                 count = -ERESTARTNOHAND;
      29.                 }
      30.                 if (count || timed_out)
      31.                         break;
      32.                 if (can_busy_loop && !need_resched()) {
      33.                         if (!busy_start) {
      34.                                 busy_start = busy_loop_current_time();
      35.                                 continue;
      36.                         }
      37.                         if (!busy_loop_timeout(busy_start))
      38.                                 continue;
      39.                 }
      40.                 busy_flag = 0;
      41.                 if (end_time && !to) {
      42.                         expire = timespec64_to_ktime(*end_time);
      43.                         to = &expire;
      44.                 }
      45.                 if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))
      46.                         timed_out = 1;
      47.         }
      48.         return count;
      49. }
      复制代码

  • epoll:do_epoll_wait---》ep_poll--linux-6.0.2\fs\eventpoll.c

      1. static int do_epoll_wait(int epfd, struct epoll_event __user *events,int maxevents, struct timespec64 *to)
      2. {
      3.     .....
      4.     //调用ep_poll
      5.         error = ep_poll(ep, events, maxevents, to);
      6. error_fput:
      7.         fdput(f);
      8.         return error;
      9. }
      复制代码
      1. static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,int maxevents, struct timespec64 *timeout)
      2. {
      3.         int res, eavail, timed_out = 0;
      4.         u64 slack = 0;
      5.         wait_queue_entry_t wait;
      6.         ktime_t expires, *to = NULL;
      7.         lockdep_assert_irqs_enabled();
      8.         if (timeout && (timeout->tv_sec | timeout->tv_nsec)) {
      9.                 slack = select_estimate_accuracy(timeout);
      10.                 to = &expires;
      11.                 *to = timespec64_to_ktime(*timeout);
      12.         } else if (timeout) {
      13.                 timed_out = 1;
      14.         }
      15.         eavail = ep_events_available(ep);
      16.         while (1) {
      17.                 if (eavail) {
      18.                         res = ep_send_events(ep, events, maxevents);
      19.                         if (res)
      20.                                 return res;
      21.                 }
      22.                 if (timed_out)
      23.                         return 0;
      24.                 eavail = ep_busy_loop(ep, timed_out);
      25.                 if (eavail)
      26.                         continue;
      27.                 if (signal_pending(current))
      28.                         return -EINTR;
      29.                 init_wait(&wait);
      30.                 wait.func = ep_autoremove_wake_function;
      31.                 write_lock_irq(&ep->lock);
      32.                 __set_current_state(TASK_INTERRUPTIBLE);
      33.                 eavail = ep_events_available(ep);
      34.                 if (!eavail)
      35.                         __add_wait_queue_exclusive(&ep->wq, &wait);
      36.                 write_unlock_irq(&ep->lock);
      37.                 if (!eavail)
      38.                         timed_out = !schedule_hrtimeout_range(to, slack,
      39.                                                               HRTIMER_MODE_ABS);
      40.                 __set_current_state(TASK_RUNNING);
      41.                 eavail = 1;
      42.                 if (!list_empty_careful(&wait.entry)) {
      43.                         write_lock_irq(&ep->lock);
      44.                         if (timed_out)
      45.                                 eavail = list_empty(&wait.entry);
      46.                         __remove_wait_queue(&ep->wq, &wait);
      47.                         write_unlock_irq(&ep->lock);
      48.                 }
      49.         }
      50. }
      复制代码

4.参考资料

一文搞懂select、poll和epoll区别 - 知乎 (zhihu.com)
深入浅出理解select、poll、epoll的实现 - 知乎 (zhihu.com)
Linux内核源代码查看及分析方法 - 知乎 (zhihu.com)

来源:https://www.cnblogs.com/zqurgy/p/17156664.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具