{"id":1961,"date":"2019-09-24T08:30:43","date_gmt":"2019-09-24T00:30:43","guid":{"rendered":"https:\/\/coderbee.net\/?p=1961"},"modified":"2019-09-23T19:14:35","modified_gmt":"2019-09-23T11:14:35","slug":"linux-select-poll-epoll-%e5%8e%9f%e7%90%86%ef%bc%88%e4%b8%89%ef%bc%89poll-%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/linux\/20190924\/1961","title":{"rendered":"Linux select\/poll\/epoll \u539f\u7406\uff08\u4e09\uff09poll \u5b9e\u73b0"},"content":{"rendered":"<p>\u9488\u5bf9 select \u7cfb\u7edf\u8c03\u7528\u7684\u4e09\u4e2a\u4e0d\u8db3\uff0cpoll \u89e3\u51b3\u7684\u662f\u7b2c\u4e00\u4e2a\u3001\u6700\u591a 1024 \u4e2a FD \u9650\u5236\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5176\u5b9e\u73b0\u601d\u8def\u662f\uff1a<br \/>\n1. \u4e0d\u518d\u4f7f\u7528\u4f4d\u56fe\u6765\u4f20\u9012\u4e8b\u4ef6\u548c\u7ed3\u679c\uff0c\u800c\u662f\u4f7f\u7528 pollfd \u3002 \u7ed3\u6784\u4f53\u6570\u7ec4\u6765\u4f20\u9012\u3002<br \/>\n2. \u5728\u5185\u90e8\u5b9e\u73b0\u65f6\uff0c\u4ee5 poll_list \u94fe\u8868\u7684\u5f62\u5f0f\u6765\u5206\u6279\u6b21\u4fdd\u5b58 pollfd \u3002\u4e0d\u50cf select \u90a3\u6837\u4e00\u6b21\u7533\u8bf7\u5b8c\u6574\u7684\u4e00\u5927\u5757\u5185\u5b58\u3002<br \/>\n3. \u901a\u8fc7\u4ece\u8fdb\u7a0b\u7684\u4fe1\u53f7\u91cf\u91cc\u83b7\u53d6\u80fd\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u91cf\uff0c\u89e3\u51b3 1024 \u4e2a\u9650\u5236\u7684\u95ee\u9898\u3002<\/p>\n<h1>0. \u57fa\u672c\u6570\u636e\u7ed3\u6784<\/h1>\n<pre><code class=\"c\">\/\/ \u6e90\u7801\u4f4d\u7f6e\uff1ainclude\/uapi\/asm-generic\/poll.h\nstruct pollfd {\n    int fd;         \/\/ FD\n    short events;   \/\/ \u8f93\u5165\u7684\u6562\u5174\u8da3\u4e8b\u4ef6\n    short revents;  \/\/ \u8f93\u51fa\u7684\u7ed3\u679c\n};\n\n\/\/ \u6e90\u7801\u4f4d\u7f6e\uff1afs\/select.c\nstruct poll_list {\n    struct poll_list *next;\n\n    \/\/ entries \u6307\u5411\u7684\u6570\u7ec4\u91cc pollfd \u7684\u6570\u91cf\n    int len;\n\n    \/\/ \u6307\u5411 pollfd \u6570\u7ec4\u7684\u6307\u9488\n    struct pollfd entries[0];\n};\n<\/code><\/pre>\n<p><code>pollfd<\/code> \u7ed3\u6784\u4f53\u7528\u6765\u4f20\u9012\u5355\u4e2aFD\u7684\u8f93\u5165\u4e8b\u4ef6\u3001\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<p><code>poll_list<\/code> \u662f\u4e00\u4e2a\u94fe\u8868\uff0c\u5176\u8282\u70b9\u6307\u5411 pollfd \u7ed3\u6784\u4f53\u7684\u6570\u7ec4\uff0c\u8fd9\u4e2a\u6570\u7ec4\u8981\u4e48\u662f\u5728\u6808\u4e0a\u9884\u5206\u914d\u3001\u8981\u4e48\u662f\u6309\u5185\u5b58\u9875\u5206\u914d(\u4fdd\u6301\u9875\u5bf9\u9f50)\u3002<\/p>\n<p><!--more--><\/p>\n<h1>1. poll \u7cfb\u7edf\u8c03\u7528\u4e3b\u903b\u8f91<\/h1>\n<pre><code class=\"c\">\/\/ \u6e90\u7801\u4f4d\u7f6e\uff1afs\/select.c\nSYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,\n        int, timeout_msecs)\n{\n    struct timespec64 end_time, *to = NULL;\n    int ret;\n\n    if (timeout_msecs &gt;= 0) {\n        to = &amp;end_time;\n        poll_select_set_timeout(to, timeout_msecs \/ MSEC_PER_SEC,\n            NSEC_PER_MSEC * (timeout_msecs % MSEC_PER_SEC));\n    }\n\n    ret = do_sys_poll(ufds, nfds, to);\n\n    if (ret == -EINTR) {\n        struct restart_block *restart_block;\n\n        restart_block = &amp;current-&gt;restart_block;\n        restart_block-&gt;fn = do_restart_poll;\n        restart_block-&gt;poll.ufds = ufds;\n        restart_block-&gt;poll.nfds = nfds;\n\n        if (timeout_msecs &gt;= 0) {\n            restart_block-&gt;poll.tv_sec = end_time.tv_sec;\n            restart_block-&gt;poll.tv_nsec = end_time.tv_nsec;\n            restart_block-&gt;poll.has_timeout = 1;\n        } else\n            restart_block-&gt;poll.has_timeout = 0;\n\n        ret = -ERESTART_RESTARTBLOCK;\n    }\n    return ret;\n}\n\nstatic int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,\n        struct timespec64 *end_time)\n{\n    struct poll_wqueues table;\n    int err = -EFAULT, fdcount, len, size;\n    \/* Allocate small arguments on the stack to save memory and be\n       faster - use long to make sure the buffer is aligned properly\n       on 64 bit archs to avoid unaligned access *\/\n    \/\/ \u521b\u5efa 256 \u4e2a\u5b57\u8282\u5927\u5c0f\u7684\u6570\u7ec4\n    long stack_pps[POLL_STACK_ALLOC\/sizeof(long)];\n    struct poll_list *const head = (struct poll_list *)stack_pps;\n    struct poll_list *walk = head;\n    unsigned long todo = nfds;\n\n    \/\/ \u5982\u679c\u8d85\u8fc7\u80fd\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u5219\u8fd4\u56de\n    \/\/ \u8fd9\u4e2a rlimit \u901a\u8fc7\u5224\u7b49\u8fdb\u7a0b\u4fe1\u606f\u91cc\u7684\u4fe1\u53f7\u91cf\u6765\u5b9e\u73b0\u7684\n    \/\/ \u56e0\u6b64\u4fee\u6539\u6587\u4ef6\u80fd\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u4e0d\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\uff0c\u53ef\u4ee5\u5b9e\u65f6\u4fee\u6539\n    if (nfds &gt; rlimit(RLIMIT_NOFILE))\n        return -EINVAL;\n\n    \/\/ N_STACK_PPS \u662f\u7528\u4e8e\u8ba1\u7b97 stack_pps \u91cc\u80fd\u5b58\u653e\u591a\u5c11\u4e2a pollfd \u7ed3\u6784\u4f53\n    len = min_t(unsigned int, nfds, N_STACK_PPS);\n\n    \/\/ \u4ece\u7528\u6237\u7a7a\u95f4\u62f7\u8d1d pollfd \u6570\u7ec4\u5230\u5185\u6838\u7a7a\u95f4\n    \/\/ \u5206\u6279\u62f7\u8d1d\uff0c\u4e0d\u540c\u6279\u6b21\u4e4b\u95f4\u7528 poll_list \u94fe\u8868\u7ef4\u62a4\u8d77\u6765\n    for (;;) {\n        walk-&gt;next = NULL;\n        walk-&gt;len = len;\n        if (!len)\n            break;\n\n        if (copy_from_user(walk-&gt;entries, ufds + nfds-todo,\n                    sizeof(struct pollfd) * walk-&gt;len))\n            goto out_fds;\n\n        todo -= walk-&gt;len;\n        if (!todo)\n            break;\n\n        \/\/ POLLFD_PER_PAGE \u662f\u4e00\u4e2a\u9875\u9762\u80fd\u5b58\u653e\u591a\u5c11\u4e2a pollfd \u7ed3\u6784\u4f53\n        len = min(todo, POLLFD_PER_PAGE);\n        size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;\n        walk = walk-&gt;next = kmalloc(size, GFP_KERNEL);\n        if (!walk) {\n            err = -ENOMEM;\n            goto out_fds;\n        }\n    }\n\n    \/\/ \u521d\u59cb\u5316 poll_wqueues\uff0c\u8bbe\u7f6e\u961f\u5217\u5904\u7406\u51fd\u6570\u7b49\n    poll_initwait(&amp;table);\n\n    \/\/ \u4e3b\u903b\u8f91\uff1a\u8c03\u7528\u76ee\u6807\u6587\u4ef6\u7684 poll \u51fd\u6570\n    fdcount = do_poll(head, &amp;table, end_time);\n\n    \/\/ \u5220\u9664\u4e3b\u903b\u8f91\u91cc\u6dfb\u52a0\u5230\u76ee\u6807\u6587\u4ef6\u7684\u7b49\u5f85\u8282\u70b9\n    poll_freewait(&amp;table);\n\n    \/\/ \u628a\u7ed3\u679c\u62f7\u8d1d\u5230\u7528\u6237\u7a7a\u95f4\n    for (walk = head; walk; walk = walk-&gt;next) {\n        struct pollfd *fds = walk-&gt;entries;\n        int j;\n\n        for (j = 0; j &lt; walk-&gt;len; j++, ufds++)\n            if (__put_user(fds[j].revents, &amp;ufds-&gt;revents))\n                goto out_fds;\n    }\n\n    err = fdcount;\nout_fds:\n    \/\/ \u91ca\u653e\u7533\u8bf7\u7684\u5185\u5b58\n    walk = head-&gt;next;\n    while (walk) {\n        struct poll_list *pos = walk;\n        walk = walk-&gt;next;\n        kfree(pos);\n    }\n\n    return err;\n}\n\nstatic int do_poll(struct poll_list *list, struct poll_wqueues *wait,\n           struct timespec64 *end_time)\n{\n    poll_table* pt = &amp;wait-&gt;pt;\n    ktime_t expire, *to = NULL;\n    int timed_out = 0, count = 0;\n    u64 slack = 0;\n    unsigned int busy_flag = net_busy_loop_on() ? POLL_BUSY_LOOP : 0;\n    unsigned long busy_start = 0;\n\n    \/* Optimise the no-wait case *\/\n    if (end_time &amp;&amp; !end_time-&gt;tv_sec &amp;&amp; !end_time-&gt;tv_nsec) {\n        pt-&gt;_qproc = NULL;\n        timed_out = 1;\n    }\n\n    if (end_time &amp;&amp; !timed_out)\n        slack = select_estimate_accuracy(end_time);\n\n    for (;;) {\n        struct poll_list *walk;\n        bool can_busy_loop = false;\n\n        \/\/ \u904d\u5386\u94fe\u8868\n        for (walk = list; walk != NULL; walk = walk-&gt;next) {\n            struct pollfd * pfd, * pfd_end;\n\n            pfd = walk-&gt;entries;\n            pfd_end = pfd + walk-&gt;len;\n            \/\/ \u904d\u5386\u8282\u70b9\u91cc\u7684\u6570\u7ec4\n            for (; pfd != pfd_end; pfd++) {\n                \/*\n                 * Fish for events. If we found one, record it\n                 * and kill poll_table-&gt;_qproc, so we don't\n                 * needlessly register any other waiters after\n                 * this. They'll get immediately deregistered\n                 * when we break out and return.\n                 *\/\n                \/\/ \u5904\u7406\u5355\u4e2a pollfd\n                if (do_pollfd(pfd, pt, &amp;can_busy_loop,\n                          busy_flag)) {\n                    \/\/ \u6709\u4e8b\u4ef6\u53d1\u751f\u4e86\n                    count++;\n\n                    \/\/ \u540e\u7eed\u7684\u6587\u4ef6\u5373\u4f7f\u6ca1\u6709\u4e8b\u4ef6\u53d1\u751f\u4e5f\u4e0d\u9700\u8981\u7b49\u5f85\u4e86\u3002\n                    pt-&gt;_qproc = NULL;\n                    \/* found something, stop busy polling *\/\n                    busy_flag = 0;\n                    can_busy_loop = false;\n                }\n            }\n        }\n        \/\/ \u6ce8\u610f\uff1a\u4e0a\u9762\u7684\u904d\u5386\u5faa\u73af\u91cc\uff0c\u5e76\u6ca1\u6709\u50cf select \u90a3\u6837\uff0c\u5728\u5c0f\u6279\u6b21poll\u540e\u8fdb\u884c\u7761\u7720\u3002\n        \/*\n         * All waiters have already been registered, so don't provide\n         * a poll_table-&gt;_qproc to them on the next loop iteration.\n         *\/\n        pt-&gt;_qproc = NULL;\n        if (!count) {\n            count = wait-&gt;error;\n            if (signal_pending(current))\n                count = -EINTR;\n        }\n        if (count || timed_out)\n            break;\n\n        \/* only if found POLL_BUSY_LOOP sockets &amp;&amp; not out of time *\/\n        if (can_busy_loop &amp;&amp; !need_resched()) {\n            if (!busy_start) {\n                busy_start = busy_loop_current_time();\n                continue;\n            }\n            if (!busy_loop_timeout(busy_start))\n                continue;\n        }\n        busy_flag = 0;\n\n        \/*\n         * If this is the first loop and we have a timeout\n         * given, then we convert to ktime_t and set the to\n         * pointer to the expiry value.\n         *\/\n        if (end_time &amp;&amp; !to) {\n            expire = timespec64_to_ktime(*end_time);\n            to = &amp;expire;\n        }\n\n        \/\/ \u7761\u7720\u7b49\u5f85\u76f4\u81f3\u8d85\u65f6\u6216\u88ab\u5524\u9192\n        if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack))\n            timed_out = 1;\n    }\n    return count;\n}\n\nstatic inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait,\n                     bool *can_busy_poll,\n                     unsigned int busy_flag)\n{\n    unsigned int mask;\n    int fd;\n\n    mask = 0;\n    fd = pollfd-&gt;fd;\n    if (fd &gt;= 0) {\n        struct fd f = fdget(fd);\n        mask = POLLNVAL;\n        if (f.file) {\n            mask = DEFAULT_POLLMASK;\n            if (f.file-&gt;f_op-&gt;poll) {\n                pwait-&gt;_key = pollfd-&gt;events|POLLERR|POLLHUP;\n                pwait-&gt;_key |= busy_flag;\n                mask = f.file-&gt;f_op-&gt;poll(f.file, pwait);\n                if (mask &amp; busy_flag)\n                    *can_busy_poll = true;\n            }\n            \/* Mask out unneeded events. *\/\n            \/\/ \u6e05\u9664\u4e0d\u9700\u8981\u7684\u4e8b\u4ef6\n            mask &amp;= pollfd-&gt;events | POLLERR | POLLHUP;\n            fdput(f);\n        }\n    }\n    pollfd-&gt;revents = mask;\n\n    return mask;\n}\n<\/code><\/pre>\n<p>\u7531\u4e8e\u91c7\u7528 pollfd \u7ed3\u6784\u4f53\u6765\u4f20\u9012\u6587\u4ef6\u7684\u4e8b\u4ef6\uff0c<code>do_poll<\/code> \u904d\u5386\u6240\u6709\u6587\u4ef6\u65f6\u7684\u903b\u8f91\u66f4\u6e05\u6670\u4e9b\u3001\u6709\u5c42\u6b21\uff0c\u5bf9\u67d0\u4e2a pollfd \u7684\u5904\u7406\u63d0\u53d6\u6210 <code>do_pollfd<\/code> \u51fd\u6570\u3002<\/p>\n<h1>2. \u7b49\u5f85\u4e0e\u5524\u9192<\/h1>\n<p>poll \u7cfb\u7edf\u8c03\u7528\u7684\u7b49\u5f85\u4e0e\u5524\u9192\u903b\u8f91\u4e0e <a href=\"https:\/\/coderbee.net\/?p=1951\">select \u7cfb\u7edf\u8c03\u7528<\/a>\u662f\u4e00\u6837\u7684\uff0c\u8c03\u7528\u7684\u4ee3\u7801\u662f\u4e00\u6837\u7684\u3002<\/p>\n<h1>3. \u5c0f\u7ed3<\/h1>\n<ol>\n<li>poll \u7cfb\u7edf\u8c03\u7528\u89e3\u51b3\u4e86 select \u7cfb\u7edf\u8c03\u7528\u6700\u591a 1024 \u4e2a\u6587\u4ef6\u7684\u9650\u5236\u3002<\/li>\n<li>\u6bcf\u6b21\u8c03\u7528\u4ecd\u7136\u9700\u8981\u62f7\u8d1d\u6240\u6709\u6587\u4ef6\u7684\u4e8b\u4ef6\u3002<\/li>\n<li>\u5176\u5b9e\u73b0\u4e0a\uff0c\u6bcf\u6b21\u5524\u9192\u540e\u4ecd\u7136\u9700\u8981 poll \u6240\u6709\u6587\u4ef6\uff0c\u6548\u7387\u4f9d\u8d56\u4e8e poll \u7684\u6587\u4ef6\u6570\u91cf\u3002\u5728\u7f51\u7edc\u7684\u670d\u52a1\u7aef\u5e94\u7528\u91cc\uff0c\u8981\u76d1\u542c\u7684 socket \u8fde\u63a5\u7684\u4e8b\u4ef6\u662f\u6bd4\u8f83\u7a33\u5b9a\u7684\u3001\u5176\u6570\u91cf\u4e5f\u662f\u6bd4\u8f83\u7a33\u5b9a\u7684\uff0c\u4e0d\u4f1a\u6bcf\u6b21 <code>select\/poll<\/code> \u90fd\u4f1a\u53d1\u751f\u53d8\u5316\uff0c\u56e0\u6b64\u7b2c2\u30013\u70b9\u7684\u95ee\u9898\u6709\u5f88\u5927\u7684\u4f18\u5316\u7a7a\u95f4\uff0c\u8fd9\u5728 <code>epoll<\/code> \u7cfb\u7edf\u8c03\u7528\u91cc\u89e3\u51b3\u3002<\/li>\n<\/ol>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9488\u5bf9 select \u7cfb\u7edf\u8c03\u7528\u7684\u4e09\u4e2a\u4e0d\u8db3\uff0cpoll \u89e3\u51b3\u7684\u662f\u7b2c\u4e00\u4e2a\u3001\u6700\u591a 1024 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/linux\/20190924\/1961\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[230,324,323],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1961"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1961"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1961\/revisions"}],"predecessor-version":[{"id":1963,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1961\/revisions\/1963"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}