Hello,
I'm trying to measure the perf gain by using splice.For now I'm trying to copy a 1G file using splice.(In real scenario, the driver will DMA the data to some buffer(which is mmap'd).The app will then write the newly-DMA'd data to the disk while some other thread is crunching the same buffer.The buffer is guaranteed to not be modified.To avoid copying I was thinking of : splice-IN-mmap'd-buffer->pipe and splice-OUT-pipe->file.)
PS - I've inlined some sloppy code that I cooked up.
Case1) read from input_file and write(O_DIRECT so no buff-cache is involved but it doesn't work) to dest_file.We can talk about the buff-cache later.
(csh#)time ./splice_to_splice
0.004u 1.451s 0:02.16 67.1% 0+0k 2097152+2097152io 0pf+0w
#define KILO_BYTE (1024)
#define PIPE_SIZE (64 * KILO_BYTE)
int filedes [2];
pipe (filedes);
fd_from = open(filename_from,O_RDWR|O_LARGEFILE|O_DIRECT),0777);
fd_to = open(filename_to,(O_WRONLY|O_CREAT|O_LARGEFILE|O_DIRECT),0777);
to_write = 2048 * 512 * KILO_BYTE;
while (to_write) {
ret = splice (fd_from, &from_offset, filedes [1], NULL, PIPE_SIZE,
SPLICE_F_MORE | SPLICE_F_MOVE);
if (ret < 0) {
printf("Error: LINE:%d ret:%d\n",__LINE__,ret);
goto error;
} else {
ret = splice (filedes [0], NULL, fd_to,
&to_offset, PIPE_SIZE/*should be ret,but ...*/,
SPLICE_F_MORE | SPLICE_F_MOVE);
if (ret < 0) {
printf("Error: LINE:%d ret:%d\n",__LINE__);
goto error;
}
to_write -= ret;
}
}
Case 2) directly reading and writing:
Case2.1) copy 64K blocks
(csh#)time ./file_to_file 64
0.015u 1.066s 0:04.04 26.4% 0+0k 2097152+2097152io 0pf+0w
#define KILO_BYTE (1024)
#define MEGA_BYTE (1024 * (KILO_BYTE))
#define BUFF_SIZE (64 * MEGA_BYTE)
posix_memalign((void**)&buff,4096,BUFF_SIZE);
fd_from = ...