Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1795127
  • 博文数量: 286
  • 博客积分: 3713
  • 博客等级: 少校
  • 技术积分: 2275
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-11 09:47
个人简介

http://blog.chinaunix.net/uid/16979052.html

文章分类

全部博文(286)

文章存档

2018年(1)

2017年(16)

2016年(9)

2015年(17)

2014年(15)

2013年(112)

2012年(116)

分类: C/C++

2013-08-09 09:02:20

        epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。因为它会复用文件描述符集合来传递结果而不是迫使开发者每次等待事件之前都必须重新准备要被侦听的文件描述符集合,另一个原因就是获取事件的时候,它无须遍历整个被侦听的描述符集,只要遍历那些被内核IO事件异步唤醒而加入Ready队列的描述符集合就行了。epoll除了提供select\poll那种IO事件的电平触发(Level Triggered)外,还提供了边沿触发(Edge Triggered),这就使得用户空间程序有可能缓存IO状态,减少epoll_wait/epoll_pwait的调用,提供应用程序的效率。

     libaio和epoll的结合

在异步编程中,任何一个环节的阻塞都会导致整个程序的阻塞,所以一定要避免在io_getevents调用时阻塞式的等待。还记得io_iocb_common中的flags和resfd吗?看看libaio是如何提供io_getevents和事件循环的结合:

void io_set_eventfd(struct iocb *iocb, int eventfd)

{

iocb->u.c.flags |= (1 << 0) /* IOCB_FLAG_RESFD */;

iocb->u.c.resfd = eventfd;

}

这里的resfd是通过系统调用eventfd生成的。

 

int eventfd(unsigned int initval, int flags);

eventfd是linux 2.6.22内核之后加进来的syscall,作用是内核用来通知应用程序发生的事件的数量,从而使应用程序不用频繁地去轮询内核是否有时间发生,而是有内核将发生事件的数量写入到该fd,应用程序发现fd可读后,从fd读取该数值,并马上去内核读取。

 

有了eventfd,就可以很好地将libaio和epoll事件循环结合起来:

C代码
  1. 1. 创建一个eventfd  
  2. efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);  
  3. 2. 将eventfd设置到iocb中  
  4. io_set_eventfd(iocb, efd);  
  5. 3. 交接AIO请求  
  6. io_submit(ctx, NUM_EVENTS, iocb);  
  7. 4. 创建一个epollfd,并将eventfd加到epoll中  
  8. epfd = epoll_create(1);  
  9. epoll_ctl(epfd, EPOLL_CTL_ADD, efd, &epevent);  
  10. epoll_wait(epfd, &epevent, 1, -1);  
  11. 5. 当eventfd可读时,从eventfd读出完成IO请求的数量,并调用io_getevents获取这些IO  
  12. read(efd, &finished_aio, sizeof(finished_aio);  
  13. r = io_getevents(ctx, 1, NUM_EVENTS, events, &tms);  

 epoll 编程实例,测试通过:


Java代码
  1. #define _GNU_SOURCE  
  2. #define __STDC_FORMAT_MACROS  
  3.   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. #include   
  10. #include   
  11. #include   
  12. #include   
  13. #include   
  14. #include   
  15. #include   
  16.   
  17. #define TEST_FILE   "aio_test_file"  
  18. #define TEST_FILE_SIZE  (127 * 1024)  
  19. #define NUM_EVENTS  128  
  20. #define ALIGN_SIZE  512  
  21. #define RD_WR_SIZE  1024  
  22.   
  23. struct custom_iocb  
  24. {  
  25.     struct iocb iocb;  
  26.     int nth_request;  
  27. };  
  28.   
  29. void aio_callback(io_context_t ctx, struct iocb *iocb, long res, long res2)  
  30. {  
  31.     struct custom_iocb *iocbp = (struct custom_iocb *)iocb;  
  32.     printf("nth_request: %d, request_type: %s, offset: %lld, length: %lu, res: %ld, res2: %ld\n",   
  33.             iocbp->nth_request, (iocb->aio_lio_opcode == IO_CMD_PREAD) ? "READ" : "WRITE",  
  34.             iocb->u.c.offset, iocb->u.c.nbytes, res, res2);  
  35. }  
  36.   
  37. int main(int argc, char *argv[])  
  38. {  
  39.     int efd, fd, epfd;  
  40.     io_context_t ctx;  
  41.     struct timespec tms;  
  42.     struct io_event events[NUM_EVENTS];  
  43.     struct custom_iocb iocbs[NUM_EVENTS];  
  44.     struct iocb *iocbps[NUM_EVENTS];  
  45.     struct custom_iocb *iocbp;  
  46.     int i, j, r;  
  47.     void *buf;  
  48.     struct epoll_event epevent;  
  49.   
  50.     efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);  
  51.     if (efd == -1) {  
  52.         perror("eventfd");  
  53.         return 2;  
  54.     }  
  55.   
  56.     fd = open(TEST_FILE, O_RDWR | O_CREAT | O_DIRECT, 0644);  
  57.     if (fd == -1) {  
  58.         perror("open");  
  59.         return 3;  
  60.     }  
  61.     ftruncate(fd, TEST_FILE_SIZE);  
  62.       
  63.     ctx = 0;  
  64.     if (io_setup(8192, &ctx)) {  
  65.         perror("io_setup");  
  66.         return 4;  
  67.     }  
  68.   
  69.     if (posix_memalign(&buf, ALIGN_SIZE, RD_WR_SIZE)) {  
  70.         perror("posix_memalign");  
  71.         return 5;  
  72.     }  
  73.     printf("buf: %p\n", buf);  
  74.   
  75.     for (i = 0, iocbp = iocbs; i < NUM_EVENTS; ++i, ++iocbp) {  
  76.         iocbps[i] = &iocbp->iocb;  
  77.         io_prep_pread(&iocbp->iocb, fd, buf, RD_WR_SIZE, i * RD_WR_SIZE);  
  78.         io_set_eventfd(&iocbp->iocb, efd);  
  79.         io_set_callback(&iocbp->iocb, aio_callback);  
  80.         iocbp->nth_request = i + 1;  
  81.     }  
  82.   
  83.     if (io_submit(ctx, NUM_EVENTS, iocbps) != NUM_EVENTS) {  
  84.         perror("io_submit");  
  85.         return 6;  
  86.     }  
  87.   
  88.     epfd = epoll_create(1);  
  89.     if (epfd == -1) {  
  90.         perror("epoll_create");  
  91.         return 7;  
  92.     }  
  93.   
  94.     epevent.events = EPOLLIN | EPOLLET;  
  95.     epevent.data.ptr = NULL;  
  96.     if (epoll_ctl(epfd, EPOLL_CTL_ADD, efd, &epevent)) {  
  97.         perror("epoll_ctl");  
  98.         return 8;  
  99.     }  
  100.   
  101.     i = 0;  
  102.     while (i < NUM_EVENTS) {  
  103.         uint64_t finished_aio;  
  104.   
  105.         if (epoll_wait(epfd, &epevent, 1, -1) != 1) {  
  106.             perror("epoll_wait");  
  107.             return 9;  
  108.         }  
  109.   
  110.         if (read(efd, &finished_aio, sizeof(finished_aio)) != sizeof(finished_aio)) {  
  111.             perror("read");  
  112.             return 10;  
  113.         }  
  114.   
  115.         printf("finished io number: %"PRIu64"\n", finished_aio);  
  116.       
  117.         while (finished_aio > 0) {  
  118.             tms.tv_sec = 0;  
  119.             tms.tv_nsec = 0;  
  120.             r = io_getevents(ctx, 1, NUM_EVENTS, events, &tms);  
  121.             if (r > 0) {  
  122.                 for (j = 0; j < r; ++j) {  
  123.                     ((io_callback_t)(events[j].data))(ctx, events[j].obj, events[j].res, events[j].res2);  
  124.                 }  
  125.                 i += r;  
  126.                 finished_aio -= r;  
  127.             }  
  128.         }  
  129.     }  
  130.       
  131.     close(epfd);  
  132.     free(buf);  
  133.     io_destroy(ctx);  
  134.     close(fd);  
  135.     close(efd);  
  136.     remove(TEST_FILE);  
  137.   
  138.     return 0;  
  139. }  

 说明:

1. 在centos 6.2 (libaio-devel 0.3.107-10) 上运行通过

2. struct io_event中的res字段表示读到的字节数或者一个负数错误码。在后一种情况下,-res表示对应的

   errno。res2字段为0表示成功,否则失败

3. iocb在aio请求执行过程中必须是valid的

4. 在上面的程序中,通过扩展iocb结构来保存额外的信息(nth_request),并使用iocb.data

   来保存回调函数的地址。如果回调函数是固定的,那么也可以使用iocb.data来保存额外信息。

 

原文 :http://blog.sina.com.cn/s/blog_6b19f21d0100znza.html

阅读(9047) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~