我正在测试内核异步io函数(而不是posix aio),并试图弄清楚它是如何工作的。下面的代码是一个完整的程序,我只需将一个数组重复写入使用O_DIRECT打开的文件。我在回调函数“write missed bytes expect 1024 got 0”中看到错误(请参阅work_done()中的fprintf语句)。linux内核aio功能

对于那些不熟悉内核的AIO,下面的代码执行以下操作:

初始化一些结构

准备AIO(io_prep_pwrite)

提交的IO请求(io_submit)

检查事件完成(io_getevents)

调用回调函数以查看一切是否正常。

我在步骤5得到一个错误。如果我不使用O_DIRECT打开文件,事情工作正常,但它击败了异步写入的目的。 有人能告诉我我做错了什么吗?这是内核aio的正确用法,例如,我使用回调是否正确?对O_DIRECT的使用有任何限制吗?

我编译使用 'GCC -Wall test.c的-laio' 提前

感谢。

/*

* File: myaiocp.c

* Author: kmehta

*

* Created on July 11, 2011, 12:50 PM

*

*

* Testing kernel aio.

* Program creates a 2D matrix and writes it multiple times to create a file of desired size.

* Writes are performed using kernel aio functions (io_prep_pwrite, io_submit, etc.)

*/

#define _GNU_SOURCE

#define _XOPEN_SOURCE 600

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

char ** buf;

long seg_size;

int seg_rows;

double total_size;

char * filename;

static int wait_count = 0;

void io_task();

void cleanup();

void allocate_2D_matrix(int[]);

int file_open(char *);

void wr_done(io_context_t ctx, struct iocb* iocb, long res, long res2);

int main(int argc, char **argv) {

total_size = 1048576; //1MB

seg_size = 1024; //1kB

seg_rows = 1024;

filename = "aio.out";

int dims[] = {seg_rows, seg_size};

allocate_2D_matrix(dims); //Creates 2D matrix

io_task();

cleanup();

return 0;

}

/*

* Create a 2D matrix

*/

void allocate_2D_matrix(int dims[2]) {

int i;

char *data;

//create the matrix

data = (char *) calloc(1, dims[0] * dims[1] * sizeof (char));

if (data == NULL) {

printf("\nCould not allocate memory for matrix.\n");

exit(1);

}

buf = (char **) malloc(dims[0] * sizeof (char *));

if (buf == NULL) {

printf("\nCould not allocate memory for matrix.\n");

exit(1);

}

for (i = 0; i < dims[0]; i++) {

buf[i] = &(data[i * dims[1]]);

}

}

static void io_error(const char *func, int rc)

{

if (rc == -ENOSYS)

fprintf(stderr, "AIO not in this kernel\n");

else if (rc < 0)

fprintf(stderr, "%s: %s\n", func, strerror(-rc));

else

fprintf(stderr, "%s: error %d\n", func, rc);

exit(1);

}

/*

* Callback function

*/

static void work_done(io_context_t ctx, struct iocb *iocb, long res, long res2)

{

if (res2 != 0) {

io_error("aio write", res2);

}

if (res != iocb->u.c.nbytes) {

fprintf(stderr, "write missed bytes expect %lu got %ld\n",

iocb->u.c.nbytes, res2);

exit(1);

}

wait_count --;

printf("%d ", wait_count);

}

/*

* Wait routine. Get events and call the callback function work_done()

*/

int io_wait_run(io_context_t ctx, long iter)

{

struct io_event events[iter];

struct io_event *ep;

int ret, n;

/*

* get up to aio_maxio events at a time.

*/

ret = n = io_getevents(ctx, iter, iter, events, NULL);

printf("got %d events\n", n);

/*

* Call the callback functions for each event.

*/

for (ep = events ; n-- > 0 ; ep++) {

io_callback_t cb = (io_callback_t)ep->data ; struct iocb *iocb = ep->obj ; cb(ctx, iocb, ep->res, ep->res2);

}

return ret;

}

void io_task() {

long offset = 0;

int bufIndex = 0;

//Open file

int fd = file_open(filename);

//Initialize structures

long i;

long iter = total_size/seg_size; //No. of iterations to reach desired file size (total_size)

io_context_t myctx;

if(0 != io_queue_init(iter, &myctx))

{

perror("Could not initialize io queue");

exit(EXIT_FAILURE);

}

struct iocb * ioq[iter];

//loop through iter times to reach desired file size

for (i = 0; i < iter; i++) {

struct iocb *io = (struct iocb*) malloc(sizeof (struct iocb));

io_prep_pwrite(io, fd, buf[bufIndex], seg_size, offset);

io_set_callback(io, work_done);

ioq[i] = io;

offset += seg_size;

bufIndex ++;

if (bufIndex > seg_rows - 1) //If entire matrix written, start again from index 0

bufIndex = 0;

}

printf("done preparing. Now submitting..\n");

if(iter != io_submit(myctx, iter, ioq))

{

perror("Failure on submit");

exit(EXIT_FAILURE);

}

printf("now awaiting completion..\n");

wait_count = iter;

int res;

while (wait_count) {

res = io_wait_run(myctx, iter);

if (res < 0)

io_error("io_wait_run", res);

}

close(fd);

}

void cleanup() {

free(buf[0]);

free(buf);

}

int file_open(char *filename) {

int fd;

if (-1 == (fd = open(filename, O_DIRECT | O_CREAT | O_WRONLY | O_TRUNC, 0666))) {

printf("\nError opening file. \n");

exit(-1);

}

return fd;

}

2011-08-02

jitihsk

linux aio参数,linux内核aio功能相关推荐

  1. linux aio参数,LINUX AIO

    从Oracle9iR2开始支持Linux上的异步IO,但是Oracle9iR2和Oracle10gR1中的AIO模块默认是disable的,如果要启用必须relink一下 cd $ORACLE_HOM ...

  2. linux aio参数,Linux 异步 IO 之 Native AIO

    Linux Native AIO 来看看 Linux 提供的 AIO 系统调用(自行封装的头文件 native_aio.h): #ifndef __NATIVE_AIO_H__ #define __N ...

  3. linux方法参数,Linux的sysctl 命令 参数

    Linux内核通过/proc虚拟文件系统向用户导出内核信息,用户也可以通过/proc文件系统或通过sysctl命令动态配置内核.比如,如果我们想启动NAT,除了加载模块.配置防火墙外,还需要启动内核转 ...

  4. linux 启动 参数,Linux启动参数

    Linux启动参数 发布时间:2008-09-03 15:46:31来源:红联作者:Sednol linux noapic (跳过硬件检测) linux pci=noapic (跳过PCI卡部分硬件检 ...

  5. linux 内存 参数,linux free命令参数及用法详解(linux查看内存命令)

    linux free命令参数及用法详解(linux查看内存命令) 2019年05月31日 | 萬仟网科技 | 我要评论 free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内存区段 ...

  6. linux getline参数,Linux下的getline函数

    最近在做国嵌的mp3项目,在mp3主控程序中用到了这个函数,挺好使的,在这里记录一下.注意是linux下的,不是C++中的. 函数原型 ssize_t getline(char **lineptr, ...

  7. linux cpu参数,Linux查看CPU详细信息

    1.查看CPU详细信息 在Linux服务器上查看CPU详细信息: cat /proc/cpuinfo 输出结果: processor : 0 vendor_id : GenuineIntel cpu ...

  8. linux fg 参数,Linux的bg和fg命令简单介绍

    我们都知道,在 Windows 上面,我们要么让一个程序作为服务在后台一直运行,要么停止这个服务.而不能让程序在前台后台之间切换.而 Linux 提供了 fg 和 bg 命令,让我们轻松调度正在运行的 ...

  9. linux kvm参数,Linux KVM总结

    1.桥接配置文件如下: ifcfg-br0//第一个桥br0设置ip主要来管理宿主机. DEVICE=br0 //设备为br0 TYPE=Bridge //总类为桥接 BOOTPROTO=static ...

最新文章

  1. php vendor 删除,yii2我删除了vendor目录,然后重新composer install composer update就不行了。。。...
  2. StrongOD快捷键说明及其例子
  3. 修改mysql数据库导入大小限制
  4. Python 中的Pyc文件
  5. my_atio()代码出错原因,完全代码
  6. 虚拟机访问本地mysql_虚拟机访问主机Mysql
  7. Oracle的10046事件
  8. 【elasticsearch】文档 CRUD 增删改查 以及 相关 参数
  9. vue router-link子级返回父级页面
  10. stl的multiset和set和priority_queue区别
  11. Django中Form组件的使用
  12. 敏捷开发系列之旅 第四站(透明的Crystal水晶方法) .
  13. Vue项目部署到服务器上路由无法访问的问题
  14. 2019年贝莱德13亿美元收购另类投资服务商eFront
  15. 写给五笔初学者,一家之言,欢迎拍砖
  16. VC++6.0软件安装教程(win10亲测可用)
  17. 【解决方法】iOS 开发小技巧(一)
  18. php 算生存曲线,生存曲线(三):统计分析方法这么多,到底选哪个?
  19. 从一个html页面传值到另一个页面,两个html之间的值传递(js location.search用法)
  20. OPPO R2017线刷刷机包 可解账户锁 刷机教程

热门文章

  1. java会导致蓝屏么_原来有这么多原因会导致电脑蓝屏啊
  2. postgresql计算两点距离
  3. 毕业论文评审意见范例
  4. 【肖四出了】考研政治肖秀荣预测四套卷已出!
  5. Ngrok的注册使用
  6. 关于Google地图路线偏移的问题
  7. 原生JS灵魂之问(中),看看你是否熟悉JavaScript?
  8. 想要学习视频剪辑?可以从什么剪辑软件开始?
  9. iOS 证书管理、验证、打包流程
  10. FPGA学习-m序列信号发生器