io_submit、io_setupとio_geteventsの例


注:Hadoopテクノロジーフォーラムで発表された
io_submit、io_setupとio_geteventsとLINUX上のAIOシステム呼び出し.ここにはioに伝える非常に注意すべき点がありますsetupのaio_contextパラメータは0に初期化されなければなりません.manマニュアルには説明がありますが、無視されやすいので、私はこの間違いを犯しました.manは以下のように説明しています.
完全な例は、//必須ヘッダファイルを含む
   
   
   
   
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9. #include   
  10.  
  11. int main()  
  12. {  
  13.         io_context_t ctx;  
  14.         unsigned nr_events = 10;  
  15.         memset(&ctx, 0, sizeof(ctx));  // It's necessary,  
  16.         int errcode = io_setup(nr_events, &ctx);  
  17.         if (errcode == 0)  
  18.                 printf("io_setup success
    ");  
  19.         else 
  20.                 printf("io_setup error: :%d:%s
    ", errcode, strerror(-errcode));  
  21.  
  22.         //  O_DIRECT, io_submit read/write , LINUX  
  23.         //  O_DIRECT  
  24.         int fd = open("./direct.txt", O_CREAT|O_DIRECT|O_WRONLY, S_IRWXU|S_IRWXG|S_IROTH);  
  25.         printf("open: %s
    ", strerror(errno));  
  26.  
  27.         char* buf;  
  28.         errcode = posix_memalign((void**)&buf, sysconf(_SC_PAGESIZE), sysconf(_SC_PAGESIZE));  
  29.         printf("posix_memalign: %s
    ", strerror(errcode));  
  30.  
  31.         strcpy(buf, "hello xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");  
  32.  
  33.         struct iocb *iocbpp = (struct iocb *)malloc(sizeof(struct iocb));  
  34.         memset(iocbpp, 0, sizeof(struct iocb));  
  35.  
  36.         iocbpp[0].data           = buf;  
  37.         iocbpp[0].aio_lio_opcode = IO_CMD_PWRITE;  
  38.         iocbpp[0].aio_reqprio    = 0;  
  39.         iocbpp[0].aio_fildes     = fd;  
  40.  
  41.         iocbpp[0].u.c.buf    = buf;  
  42.         iocbpp[0].u.c.nbytes = page_size;//strlen(buf); //  512  
  43.         iocbpp[0].u.c.offset = 0; //  512  
  44.  
  45.         //  ,  
  46.         int n = io_submit(ctx, 1, &iocbpp);  
  47.         printf("==io_submit==: %d:%s
    ", n, strerror(-n));  
  48.  
  49.         struct io_event events[10];  
  50.         struct timespec timeout = {1, 100};  
  51.         //  , epoll_wait select 
  52.         n = io_getevents(ctx, 1, 10, events, &timeout);  
  53.         printf("io_getevents: %d:%s
    ", n, strerror(-n));  
  54.  
  55.         close(fd);  
  56.         io_destroy(ctx);  
  57.         return 0;  
  58. }  
  59.  
  60.  
  61. :Linux 2.6.16,SUSE Linux Enterprise Server 10 (x86_64)  
  62.  
  63. struct iocb {  
  64.        /* these are internal to the kernel/libc. */  
  65.        __u64   aio_data;       /* data to be returned in event\'s data */ IO , epoll ptr。  
  66.        __u32   PADDED(aio_key, aio_reserved1); /* the kernel sets aio_key to the req # */  
  67.        /* common fields */  
  68.        __u16   aio_lio_opcode; /* see IOCB_CMD_ above */  
  69.        __s16   aio_reqprio;      //   
  70.        __u32   aio_fildes;        //    
  71.        __u64   aio_buf;           //   
  72.        __u64   aio_nbytes;      //   
  73.        __s64   aio_offset;       //   
  74.  
  75.        /* extra parameters */  
  76.        __u64   aio_reserved2;  /* TODO: use this for a (struct sigevent *) */  
  77.        __u64   aio_reserved3;  
  78. }; /* 64 bytes */  
  79.  
  80. struct io_event {  
  81.        __u64           data;          /* the data field from the iocb */ //  epoll_event ptr  
  82.        __u64           obj;            /* what iocb this event came from */ //  iocb  
  83.        __s64           res;            /* result code for this event */ //  , read/write  
  84.        __s64           res2;          /* secondary result */  
  85. };  

 
システム呼び出し機能プロトタイプio_setupは現在のプロセスに非同期IOコンテキストint io_を初期化するsetup(unsigned nr_events,aio_context_t *ctxp);io_submitは、1つまたは複数の非同期IOオペレーションint io_をコミットするsubmit(aio_context_t ctx_id,long nr, struct iocb **iocbpp);io_getevents未完了の非同期IO操作の状態int io_を取得getevents(aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout);io_cancel未完了の非同期IO操作int ioをキャンセルcancel(aio_context_t ctx_id, struct iocb *iocb, struct io_event *result);io_destroy現在のプロセスから非同期IOコンテキストint ioを削除destroy(aio_context_t ctx);