zoukankan      html  css  js  c++  java
  • TCP源码—epoll源码及测试

    一、epoll_create & epoll_create1

    SYSCALL_DEFINE1(epoll_create, int, size)

    sys_epoll_create->sys_epoll_create1

    SYSCALL_DEFINE1(epoll_create1, int, flags)

    sys_epoll_create1(入参检测等)->ep_alloc(分配eventpoll,并初始化锁、等待队列等结构)->[sys_epoll_create1]get_unused_fd_flags(分配fd)->[sys_epoll_create1]anon_inode_getfile(分配file)->[sys_epoll_create1]fd_install(关联file和fd)


    anon_inode_getfile:

    1. 该函数创建的文件共享使用一个inode,节省内存避免代码重复,inode:anon_inode_inode、 sb:anon_inode_mnt->mnt_sb、 fs:anon_inode_fs_type,初始化位置[anon_inode_init]。

    2. 使用alloc_file分配一个文件,file->f_op = eventpoll_fops;

    3. 设置file->f_flags = (O_RDWR | (flags & O_CLOEXEC)) & (O_ACCMODE | O_NONBLOCK); file->private_data =ep;


        其中eventpoll_fops注册了ep_show_fdinfo函数,允许我们在proc中查看对应epfd的epi信息

      1. lybxin@Inspiron:~$more /proc/1469/task/1469/fdinfo/4
      2. pos: 0
      3. flags: 02000002
      4. mnt_id: 11
      5. tfd:        5 events:       19 data:     55b8247c8da0
      6. tfd:       13 events:       19 data:     55b8247ccc90
      7. tfd:       14 events:       19 data:     55b8248079a0
      8. tfd:        9 events:       19 data:     55b8247e9860
      9. tfd:       12 events:       1a data:     55b8247e9b40
      10. tfd:        8 events:       19 data:     55b8247caf90
      11. tfd:        7 events:       19 data:     55b824806490


    二、epoll_ctl

    SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,struct epoll_event __user *, event)


    sys_epoll_ctl:

    1. 如果不是EPOLL_CTL_DEL操作,则从用户空间复制epoll_event结构

    2. 获取fd结构,epfd->f,需要操作的fd->tf

    3. 目标fd对应的fd结构必须支持tf.file->f_op->poll操作

    4. 处理EPOLLWAKEUP标志

    5. 检验epfd对应有效的epoll文件描述符,且需要操作的fd与epfd不是对应同一个文件

    6. 通过ep_loop_check检测epfd是否构成闭环或者连续epfd的深度超过5,对应宏EP_MAX_NESTS[4]

    7. 通过ep_find查找这个epfd是否已经添加了目标fd文件

    8. ADD/MOD操作自动添加POLLERR | POLLHUP这两个标志位


    ep_loop_check:

    1. visited_list:表示已经处理过的节点,假设epfd1下挂epfd2和epfd3,而epfd2和epfd3又同时挂epfd4,那么保证epfd只处理一次

    2. tfile_check_list:保存非epoll文件的fd用于反向检查

    3. 从源码和下面的测试来看这个闭环和深度检测只能从添加的fd向下检测,而不能向上检测,因此并不是所有场景都能有效的检测出来,如下测试,另外还有一种场景因为会跳过已经visit的节点,所以visit的节点的最大深度也可能会超过5。

      1. ---------------test1 start---------------  //正向查找只检测target fd
      2. add epfd2 to epfd1:add 1 success
      3. add epfd3 to epfd2:add 2 success
      4. add epfd4 to epfd3:add 3 success
      5. add epfd5 to epfd4:add 4 success
      6. add epfd6 to epfd5:add 5 success
      7. add epfd7 to epfd6:add 6 success
      8. add epfd8 to epfd7:add 7 success
      9. add epfd9 to epfd8:add 8 success
      10. ---------------test1 end---------------
      11. ---------------test2 start---------------  //正向查找只检测target fd
      12. add epfd1 to epfd2:add 1 success
      13. add epfd2 to epfd3:add 2 success
      14. add epfd3 to epfd4:add 3 success
      15. add epfd4 to epfd5:add 4 success
      16. add epfd5 to epfd6:add 5 success
      17. add epfd6 to epfd7:epoll_ctl error:Too many levels of symbolic links(errno:40)
      18. add epfd7 to epfd8:add 6 success
      19. add epfd8 to epfd9:add 7 success
      20. ---------------test2 end---------------
      21. ---------------test3 start---------------  //正向查找形成闭环 操作失败
      22. add epfd2 to epfd1:add 1 success
      23. add epfd3 to epfd2:add 2 success
      24. add epfd1 to epfd3:epoll_ctl error:Too many levels of symbolic links(errno:40)
      25. ---------------test3 end---------------


    ep_insert:

    1. max_user_watches:/proc/sys/fs/epoll/max_user_watches 每个用户同时watch的最大fd数目

    2. 如果watch的总数超过max_user_watches,则返回ENOSPC

    3. 如果从epi_cache分配epitem失败,则返回ENOMEM

    4. 根据EPOLLWAKEUP标志注册wake up

    5. 通过ep_item_poll把item添加到poll钩子中,并获取当前revents。最终会通过ep_ptable_queue_proc函数把eppoll_entry添加到sk->sk_wq->wait的头部,并通过pwq->llink添加到epi->pwqlist的尾部。这里每个epi对应一个pwqlist链表的原因是poll一些文件的时候,需要添加两次等待队列,如/dev/bsg/目录下面的文件。

    6. 把epi插入到f_ep_links链表的尾部,list_add_tail_rcu(&epi->fllink, &tfile->f_ep_links);

    7. 把epi插入到ep的红黑树中,ep_rbtree_insert(ep, epi);

    8. 通过reverse_path_check进行反向检查

    9. 如果获取到的revents中有用户关注的事件,并且epi未在ready链表中,那么把epi插入ready链表尾部 list_add_tail(&epi->rdllink, &ep->rdllist);并尝试唤醒epoll_wait进程wake_up_locked(&ep->wq);以及file->poll()等待进程ep_poll_safewake(&ep->poll_wait)

    10. 自增ep->user->epoll_watches


    reverse_path_check:

    1. 对于第一层反向检查不限制数目。

    2. 对于第2-5层,限制引用数目分别为500、100、50、10,如下变量定义了上限,其中该变量第一个成员1000仅作占位使用,并不限制第一层引用总数,参考path_count_inc。static const int path_limits[PATH_ARR_SIZE] = { 1000, 500, 100, 50, 10 };

    3. 对于5层以上则直接返回错误

      1. ---------------test4 反向查找---------------  //反向查找层数超过5
      2. add epfd2 to epfd1:add 1 success
      3. add epfd3 to epfd2:add 2 success
      4. add epfd4 to epfd3:add 3 success
      5. add listen_fd to epfd4:add 4 success
      6. add epfd5 to epfd4:add 5 success
      7. add listen_fd to epfd5:add 6 success
      8. add epfd6 to epfd5:add 7 success
      9. add listen_fd to epfd6:epoll_ctl error:Invalid argument(errno:22)
      10. add epfd7 to epfd6:add 8 success
      11. add listen_fd to epfd7:epoll_ctl error:Invalid argument(errno:22)
      12. add epfd8 to epfd7:add 9 success
      13. add epfd9 to epfd8:add 10 success
      14. add listen_fd to epfd9:epoll_ctl error:Invalid argument(errno:22)
      15. ---------------test4 end---------------
      16. ---------------test5 反向查找 i:0 ---------------  //第一层反向查找直到fd数目的上限才会失败添加第一层
      17. 添加第一层 add error num:1021  error:Bad file descriptor(errno:9)
      18. ---------------test5 end i:1020 ---------------
      19. ---------------test6 反向查找 i:0 ---------------   //第二层反向查找的限制为path_limits[1]
      20. 第二层 add error i:501  error:Invalid argument(errno:22)
      21. ---------------test6 end i:500 ---------------

    ep_remove:

    1. 移除一个epi

    2. 从poll wait中移除

    3. 从file的f_ep_links链表移除

    4. 从红黑树中移除

    5. 从ready链表中移除

    6. 取消wakeup注册

    7. 自减ep->user->epoll_watches


    ep_modify:修改epi

    1. 更新epi->event.events和epi->event.data

    2. 根据EPOLLWAKEUP更新wake up

    3. 刷新内存屏障smp_mb

    4. 通过ep_item_poll获取revents,相比ep_insert差异在于并不会调用ep_ptable_queue_proc重新注册

    5. 如果获取到的revents中有用户关注的事件,并且epi未在ready链表中,那么把epi插入ready链表尾部 list_add_tail(&epi->rdllink, &ep->rdllist);并尝试唤醒epoll_wait进程wake_up_locked(&ep->wq);以及file->poll()等待进程ep_poll_safewake(&ep->poll_wait)



    三、epoll_wait&epoll_pwait

    SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,int, maxevents, int, timeout)

    sys_epoll_pwait->sys_epoll_wait

    sys_epoll_wait:主要做参数检查和epfd 的校验,然后通过ep_poll进行操作


    ep_poll:

    1. 根据入参估计超时时间to和slack,或者设置timed_out标志位

    2. 如果epoll_wait入参定时时间为0,那么直接通过ep_events_available判断当前是否有用户感兴趣的事件发生,如果有则通过ep_send_events进行处理

    3. 如果定时时间大于0,并且当前没有用户关注的事件发生,则进行休眠,并添加到ep->wq等待队列的头部。 对等待事件描述符设置WQ_FLAG_EXCLUSIVE标志

    4. ep_poll被事件唤醒后会重新检查是否有关注事件,如果对应的事件已经被抢走,那么ep_poll会继续休眠等待。

    1. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$./nesttest
    2. -----------test8 测试epollaccept同时等待的唤醒情况 epfd1:4,epfd:5,listen_fd:3-----------
    3. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$ss -tlnap | grep 9877
    4. LISTEN     0      128          *:9877                     *:*                   users:(("nesttest",pid=6895,fd=3),("nesttest",pid=6894,fd=3),("nesttest",pid=6893,fd=3))
    5. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$nc  127.0.0.1 9877
    6. epfd2 epoll_wait return:
    7. i:0,nfds:1,fd:3,sec:238
    8. epfd1 epoll_wait return:
    9. i:0,nfds:1,fd:3,sec:238
    10. accept return connfd:6,sec:238
    11. ^C
    12. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$nc  127.0.0.1 9877
    13. epfd2 epoll_wait return:
    14. accept return connfd:7,sec:240
    15. i:0,nfds:1,fd:3,sec:240
    16. ^C
    17. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$nc  127.0.0.1 9877
    18. epfd1 epoll_wait return:
    19. accept return connfd:8,sec:242
    20. i:0,nfds:1,fd:3,sec:242
    21. ^C
    22. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$nc  127.0.0.1 9877
    23. epfd2 epoll_wait return:
    24. accept return connfd:9,sec:244
    25. i:0,nfds:1,fd:3,sec:244
    26. ^C
    27. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$nc  127.0.0.1 9877
    28. epfd1 epoll_wait return:
    29. i:0,nfds:1,fd:3,sec:246
    30. epfd2 epoll_wait return:
    31. i:0,nfds:1,fd:3,sec:246
    32. accept return connfd:10,sec:246
    33. ^C


    select_estimate_accuracy:

    1. 估计slack,最大为MAX_SLACK(100ms),最小为current->timer_slack_ns(默认值为50000ns,即50 usec),timer_slack_ns可以通过prctl的PR_SET_TIMERSLACK选项设置

    2. nice 进程取定时时间的0.5%,普通进程取0.1%


    ep_events_available:

    1. 如果ready链ep->rdllist非空或者ep->ovflist有效,则表示当前有关注的event发生



    ep_scan_ready_list[ep_send_events]:

    1. epoll_wait的时候传递函数指针ep_send_events_proc给ep_scan_ready_list,epfd进行poll的时候则传递函数指针ep_read_events_proc

    2. 把ep->rdllist链接到txlist,并清空ep->rdllist,设置ep->ovflist = NULL,表示当前正在往用户空间发送数据,新事件触发的epi插入到ep->ovflist的头部,参考ep_send_events_proc函数的注释

    3. 调用传入的函数指针处理txlist

    4. 把ep->ovflist插入到ep->rdllist

    5. 设置ep->ovflist = EP_UNACTIVE_PTR; 表示当前需要往ready 链表插入事件epi

    6. 把txlist中剩余元素插入ep->rdllist

    7. 如果ready链表非空,尝试唤醒ep->wq和ep->poll_wait等待队列


    ep_send_events_proc[ep_scan_ready_list]:

    1. 读取txlist中已经ready的事件,获取事件的events,复制到用户空间,复制失败则把epi重新插入到ready链表

    2. 如果设置了EPOLLONESHOT标志位,则设置epi->event.events &= EP_PRIVATE_BITS,其定义如下#define EP_PRIVATE_BITS (EPOLLWAKEUP | EPOLLONESHOT | EPOLLET),后续根据EP_PRIVATE_BITS判断不再加入ep->rdllist或者ep->ovflist。注意设置了EPOLLONESHOT触发一次后并没有删除epi,因而通过epoll_ctl进行ADD操作后会提示File exists错误。

    3. 如果设置了水平触发(没有EPOLLET标志位),那么即使已经成功把事件传递到了用户空间也会把epi重新添加到ready链表尾部,这样下次进行epoll_wait的时候可以重新检查这个epi。注意EPOLLONESHOT优先于水平触发的处理,即同时设置水平触发和EPOLLONESHOT并不会把epi添加到ready链表。








  • 相关阅读:
    博客搬迁
    Android 集成FaceBook实现第三方登陆
    android-Xfermode模式详解
    android FileNotFoundException
    EditText 隐藏或者显示输入内容
    Fragment+ViewPager静止滑动,去掉默认的滑动效果
    Xcode 常用插件(持久更新)
    XMPP- JID 与 XMPP的命名空间
    android openfire 和 xmpp
    IOS-数据持久化的几种方式
  • 原文地址:https://www.cnblogs.com/lshs/p/6038904.html
Copyright © 2011-2022 走看看