V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
glogo
V2EX  ›  Linux

关于 Linux 直接 I/O 的问题

  •  
  •   glogo · 2016-09-20 17:02:45 +08:00 · 2760 次点击
    这是一个创建于 2766 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在看《 The Linux Programming Interface 》( Linux/Unix 系统编程手册)第 13 章,第 6 小节,其中的示例代码:

    /* direct_read.c
    
       Demonstrate the use of O_DIRECT to perform I/O bypassing the buffer cache
       ("direct I/O").
    
       Usage: direct_read file length [offset [alignment]]
    
       This program is Linux-specific.
    */
    #define _GNU_SOURCE     /* Obtain O_DIRECT definition from <fcntl.h> */
    #include <fcntl.h>
    #include <malloc.h>
    #include "tlpi_hdr.h"
    
    int
    main(int argc, char *argv[])
    {
        int fd;
        ssize_t numRead;
        size_t length, alignment;
        off_t offset;
        char *buf;
    
        if (argc < 3 || strcmp(argv[1], "--help") == 0)
            usageErr("%s file length [offset [alignment]]\n", argv[0]);
    
        length = getLong(argv[2], GN_ANY_BASE, "length");
        offset = (argc > 3) ? getLong(argv[3], GN_ANY_BASE, "offset") : 0;
        alignment = (argc > 4) ? getLong(argv[4], GN_ANY_BASE, "alignment") : 4096;
    
        fd = open(argv[1], O_RDONLY | O_DIRECT);
        if (fd == -1)
            errExit("open");
    
        /* memalign() allocates a block of memory aligned on an address that
           is a multiple of its first argument. By specifying this argument as
           2 * 'alignment' and then adding 'alignment' to the returned pointer,
           we ensure that 'buf' is aligned on a non-power-of-two multiple of
           'alignment'. We do this to ensure that if, for example, we ask
           for a 256-byte aligned buffer, we don't accidentally get
           a buffer that is also aligned on a 512-byte boundary. */
    
        buf = memalign(alignment * 2, length + alignment);
        if (buf == NULL)
            errExit("memalign");
    
        buf += alignment;
    
        if (lseek(fd, offset, SEEK_SET) == -1)
            errExit("lseek");
    
        numRead = read(fd, buf, length);
        if (numRead == -1)
            errExit("read");
        printf("Read %ld bytes\n", (long) numRead);
    
        exit(EXIT_SUCCESS);
    }
    

    是想展示以下三点:

    • 用于传递数据的缓冲区,其内存边界必须对其为块大小的整数倍。
    • 数据传输的开始的点,亦即文件和设备的偏移量,必须是块大小的整数倍。
    • 待传递数据的长度必须是块大小的整数倍。

    中间大块的注释里有一句话:

    we ensure that 'buf' is aligned on a non-power-of-two multiple of 'alignment'.

    请问这个用意是什么呢?(因为我感觉这是不必要的,为什么非得让 buf 的开始地址对其为非 2 的幂呢?)

    5 条回复    2016-09-20 19:17:07 +08:00
    blahgeek
        1
    blahgeek  
       2016-09-20 17:15:23 +08:00
    这个代码不希望指定 256 的时候分配一个其实是 512 对齐的内存,所以他分配一个 2*256 对齐,长度为 len+256 的内存,然后取偏移 256 后的地址,保证不是 512 对齐的
    glogo
        2
    glogo  
    OP
       2016-09-20 17:20:32 +08:00
    @blahgeek 指定 256 对齐的时候却实际出现 512 对齐了,这会影响什么呢?这个是我疑惑的原因。
    hitmanx
        3
    hitmanx  
       2016-09-20 17:46:40 +08:00
    有点意思,我搜了下,貌似还不止你一个人有这个疑问

    http://stackoverflow.com/questions/25233874/memory-alignment-requirement-for-data-transfer-with-direct-i-o
    skydiver
        4
    skydiver  
       2016-09-20 18:00:50 +08:00
    很简单啊,因为磁盘的块大小是 512 字节,这个代码是故意分配一个不按照 512 对齐的 buf ,来展示一下这样会报错。

    如果传 512 就不会报错,传 256 就会报错。

    所以分配 256 的时候就故意不让它是 512 整数倍,这样才能给你看报错。
    glogo
        5
    glogo  
    OP
       2016-09-20 19:17:07 +08:00
    @glogo GET ,多谢解答~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5870 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 02:42 · PVG 10:42 · LAX 19:42 · JFK 22:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.