如何有效地在文件中写入大量的NULL字节序列?

时间:2013-11-07 15:35:23

标签: c performance system-calls

我有一个文件描述符 fd ,一个偏移量和一个长度,我需要从 offset length NULL个字节>在 fd 描述的文件中(注意:它永远不会出现在文件的末尾)。

除了使用填充了NULL的缓冲区并重复将其写入循环之外,是否有一种有效的方法可以做到这一点? NULL s的序列可能会达到16Mo,我目前使用的是512大小的缓冲区(= write(2)的约30k)。

5 个答案:

答案 0 :(得分:4)

您可以尝试mmap处于所需偏移量的文件,并按照所需大小进行映射,然后只需调用memset

编辑:基于@jthill发布的代码,这是一个简单的例子,演示如何进行比较..

#define _GNU_SOURCE
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void create(int fsize)
{
  FILE *fd = fopen("data", "wb");
  fseek(fd, fsize - 1, SEEK_SET);
  fputc(0, fd);
  fclose(fd);
}

void seek_write(const char* data, int wsize, int seek, int dsize)
{
  int fd = open("data", O_RDWR);
  // Now seek_write
  if (lseek(fd, seek, SEEK_SET) != seek)
    perror("seek?"), abort();
  // Now write in requested blocks..
  for (int c = dsize / wsize; c--;)
    if (write(fd, data, wsize) != wsize)
      perror("write?"), abort();
  close(fd);
}

void mmap_memset(int wsize, int seek, int dsize)
{
  int fd = open("data", O_RDWR);
  void* map = mmap(0, dsize + seek, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED)
    perror("mmap?"), abort();
  memset((char*)map + seek, 0, dsize);
  munmap(map, dsize);
  close(fd);
}

int main(int c, char **v)
{
  struct timeval start, end;
  long long ts1, ts2;
  int wsize = c>1 ? atoi(*++v) : 512;
  int seek  = c>2 ? atoi(*++v) : 0;
  int reps  = c>3 ? atoi(*++v) : 1000;
  int dsize = c>4 ? atoi(*++v) : 16*1024*1024;
  int fsize = c>5 ? atoi(*++v) : 32*1024*1024;

  // Create the file and grow...
  create(fsize);

  char *data = mmap(0, wsize, PROT_READ, MAP_ANON | MAP_PRIVATE, 0, 0);

  printf("Starting write...\n");
  gettimeofday(&start, NULL);
  for (int i = 0;i < reps; ++i)
    seek_write(data, wsize, seek, dsize);
  gettimeofday(&end, NULL);

  ts1 = ((end.tv_sec - start.tv_sec) * 1000000) + (end.tv_usec - start.tv_usec);

  printf("Starting mmap...\n");
  gettimeofday(&start, NULL);
  for (int i = 0;i < reps; ++i)
    mmap_memset(wsize, seek, dsize);
  gettimeofday(&end, NULL);

  ts2 = ((end.tv_sec - start.tv_sec) * 1000000) + (end.tv_usec - start.tv_usec);

  printf("write: %lld us, %f us\nmmap: %lld us, %f us", ts1, (double)ts1/reps, ts2, (double)ts2/reps);
}

注意:mmap如果提供的偏移未对齐(通常在页面边界上),则不喜欢它,因此,如果您可以在长度+偏移中进行映射并且只是从偏移(或者,如果你可以保证一个很好的对齐偏移,这也会起作用..)

如您所见,这两项操作之间的差异是lseekmap + seek),然后是writememset)。我认为这是一个公平的比较(如果有人想要解决任何问题,请随意。)

我也使用MAP_SHARED而不是MAP_PRIVATE,两者之间存在显着差异,后者执行写入时复制,这可能会慢得多!

在我不那么强大的系统上,我得到:

> ./fwrite 4096 1234
> Starting write...
> Starting mmap...
> write: 14767898 us, 14767.898000 us
> mmap: 6619623 us, 6619.623000 us

我认为这表明mmap + memset更快?

答案 1 :(得分:2)

如果您正在运行Linux并且文件系统支持稀疏文件,您可以尝试使用带有fallocate(2)标志的FALLOC_FL_PUNCH_HOLE在文件中打孔。我希望这很快,虽然我没有测试它。

答案 2 :(得分:1)

在Linux上,您可以使用splice(2)/dev/zero复制数据。

这非常有效,因为大多数工作都是在内核中完成的。

其他操作系统可能提供类似的功能(即sendfile)。

<强>更新

我忘记了可以在文件中间打孔的fallocate(2)

答案 3 :(得分:1)

从下面看,16M的I / O做得很糟,只有一次,是> 20ms。这本身就是可感知的。

Takeaways:

  • I / O值得关注,因为
  • 做很多事情会造成可悲的延误。
  • 512字节写入伤害了很多。
  • 4096字节写入不。

在内存或磁盘上的偏移都没有那么重要,所以calloc应该做的任何事都可以(你可以试试)。

#define _GNU_SOURCE
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>

int main(int c, char **v)
{
    int wsiz = c>1 ? atoi(*++v) : 512;
    int seek = c>2 ? atoi(*++v) : 0;
    int woff = c>3 ? atoi(*++v) : 0;
    int fsiz = c>4 ? atoi(*++v) : 16 * 1024 * 1024;
    int reps = c>5 ? atoi(*++v) : 1000;

    printf("writesize %d, seek  %d, align %d, filesize %d, reps %d\n",
           wsiz, seek, woff, fsiz, reps);

    if (wsiz<=0|seek<0|woff<0|fsiz<wsiz)
        return 1;

    int fd = open("data", O_CREAT | O_RDWR, 0700);
    char *data = mmap(0, 2*wsiz, PROT_READ, MAP_ANON | MAP_PRIVATE, 0, 0);

    for (int t = reps; t--; lseek(fd, seek, 0))
        for (int c = fsiz / wsiz; c--;)
            if (write(fd, data+woff, wsiz) != wsiz)
                perror("write?"), abort();

    return close(fd);
}
cc  -o bin/wipetest -g -O   --std=c11 -march=native -pipe -Wall -Wno-parentheses    wipetest.c
------------------------------------------------------
writesize 512, seek  0, align 0, filesize 16777216, reps 1000

real    0m20.727s
user    0m0.513s
sys 0m20.220s
------------------------------------------------------
writesize 4096, seek  0, align 0, filesize 16777216, reps 1000

real    0m3.889s
user    0m0.077s
sys 0m3.687s
------------------------------------------------------
writesize 16777216, seek  0, align 0, filesize 16777216, reps 1000

real    0m3.205s
user    0m0.000s
sys 0m3.203s
------------------------------------------------------
writesize 512, seek  500, align 0, filesize 16777216, reps 1000

real    0m23.829s
user    0m0.463s
sys 0m23.247s
------------------------------------------------------
writesize 4096, seek  500, align 0, filesize 16777216, reps 1000

real    0m5.531s
user    0m0.053s
sys 0m5.480s
------------------------------------------------------
writesize 16777216, seek  500, align 0, filesize 16777216, reps 1000

real    0m3.435s
user    0m0.000s
sys 0m3.433s
------------------------------------------------------
writesize 512, seek  0, align 12, filesize 16777216, reps 1000

real    0m21.478s
user    0m0.537s
sys 0m20.820s
------------------------------------------------------
writesize 4096, seek  0, align 12, filesize 16777216, reps 1000

real    0m3.722s
user    0m0.057s
sys 0m3.667s
------------------------------------------------------
writesize 16777216, seek  0, align 12, filesize 16777216, reps 1000

real    0m3.232s
user    0m0.000s
sys 0m3.233s
------------------------------------------------------
writesize 512, seek  500, align 12, filesize 16777216, reps 1000

real    0m23.775s
user    0m0.550s
sys 0m23.113s
------------------------------------------------------
writesize 4096, seek  500, align 12, filesize 16777216, reps 1000

real    0m5.566s
user    0m0.050s
sys 0m5.517s
------------------------------------------------------
writesize 16777216, seek  500, align 12, filesize 16777216, reps 1000

real    0m3.277s
user    0m0.000s
sys 0m3.277s

答案 4 :(得分:0)

lseek()到位置,write()一个大缓冲区(或多次缓冲区)。

相关问题