io_submit、io_setupとio_geteventsの例
注:Hadoopテクノロジーフォーラムで発表された
io_submit、io_setupとio_geteventsとLINUX上のAIOシステム呼び出し.ここにはioに伝える非常に注意すべき点がありますsetupのaio_contextパラメータは0に初期化されなければなりません.manマニュアルには説明がありますが、無視されやすいので、私はこの間違いを犯しました.manは以下のように説明しています.
完全な例は、//必須ヘッダファイルを含む
システム呼び出し機能プロトタイプ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);
io_submit、io_setupとio_geteventsとLINUX上のAIOシステム呼び出し.ここにはioに伝える非常に注意すべき点がありますsetupのaio_contextパラメータは0に初期化されなければなりません.manマニュアルには説明がありますが、無視されやすいので、私はこの間違いを犯しました.manは以下のように説明しています.
完全な例は、//必須ヘッダファイルを含む
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- int main()
- {
- io_context_t ctx;
- unsigned nr_events = 10;
- memset(&ctx, 0, sizeof(ctx)); // It's necessary,
- int errcode = io_setup(nr_events, &ctx);
- if (errcode == 0)
- printf("io_setup success
");
- else
- printf("io_setup error: :%d:%s
", errcode, strerror(-errcode));
-
- // O_DIRECT, io_submit read/write , LINUX
- // O_DIRECT
- int fd = open("./direct.txt", O_CREAT|O_DIRECT|O_WRONLY, S_IRWXU|S_IRWXG|S_IROTH);
- printf("open: %s
", strerror(errno));
-
- char* buf;
- errcode = posix_memalign((void**)&buf, sysconf(_SC_PAGESIZE), sysconf(_SC_PAGESIZE));
- printf("posix_memalign: %s
", strerror(errcode));
-
- strcpy(buf, "hello xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
-
- struct iocb *iocbpp = (struct iocb *)malloc(sizeof(struct iocb));
- memset(iocbpp, 0, sizeof(struct iocb));
-
- iocbpp[0].data = buf;
- iocbpp[0].aio_lio_opcode = IO_CMD_PWRITE;
- iocbpp[0].aio_reqprio = 0;
- iocbpp[0].aio_fildes = fd;
-
- iocbpp[0].u.c.buf = buf;
- iocbpp[0].u.c.nbytes = page_size;//strlen(buf); // 512
- iocbpp[0].u.c.offset = 0; // 512
-
- // ,
- int n = io_submit(ctx, 1, &iocbpp);
- printf("==io_submit==: %d:%s
", n, strerror(-n));
-
- struct io_event events[10];
- struct timespec timeout = {1, 100};
- // , epoll_wait select
- n = io_getevents(ctx, 1, 10, events, &timeout);
- printf("io_getevents: %d:%s
", n, strerror(-n));
-
- close(fd);
- io_destroy(ctx);
- return 0;
- }
-
-
- :Linux 2.6.16,SUSE Linux Enterprise Server 10 (x86_64)
-
- struct iocb {
- /* these are internal to the kernel/libc. */
- __u64 aio_data; /* data to be returned in event\'s data */ IO , epoll ptr。
- __u32 PADDED(aio_key, aio_reserved1); /* the kernel sets aio_key to the req # */
- /* common fields */
- __u16 aio_lio_opcode; /* see IOCB_CMD_ above */
- __s16 aio_reqprio; //
- __u32 aio_fildes; //
- __u64 aio_buf; //
- __u64 aio_nbytes; //
- __s64 aio_offset; //
-
- /* extra parameters */
- __u64 aio_reserved2; /* TODO: use this for a (struct sigevent *) */
- __u64 aio_reserved3;
- }; /* 64 bytes */
-
- struct io_event {
- __u64 data; /* the data field from the iocb */ // epoll_event ptr
- __u64 obj; /* what iocb this event came from */ // iocb
- __s64 res; /* result code for this event */ // , read/write
- __s64 res2; /* secondary result */
- };
システム呼び出し機能プロトタイプ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);