Issue
I want to test the performance of the splice() syscall. I compare it with the traditional read/write.
/* wr.cpp
* it use read/write
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BUF_SIZE 4096
int main(int argc, char *argv[])
{
char buf[BUF_SIZE];
int in = open("1.rmvb",O_RDONLY);
int out = open("1.cp.rmvb",O_WRONLY|O_CREAT,0766);
ssize_t nread;
while( (nread = read(in,buf,BUF_SIZE)) > 0 )
{
write(out,buf,nread);
}
return 0;
}
//
/* splice.cpp
* it use splice
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BUF_SIZE 4096
int main(int argc, char *argv[])
{
int p[2];
pipe (p);
int in = open("1.rmvb",O_RDONLY);
int out = open("1.cp.rmvb",O_WRONLY|O_CREAT,0766);
ssize_t nread;
while( (nread = splice(in,NULL,p[1],NULL,BUF_SIZE,0)) > 0)
splice(p[0],NULL,out,NULL,BUF_SIZE,0);
return 0;
}
here is the result:
It seems that spilce() didn't improve the performance and didn't reduce the CPU time. Why? My kernel version is 2.6.35-28, ubuntu 10.10.
Solution
Are you sure, that one of your descriptors is actually a pipe?
Because, man splice
says:
... It transfers up to len bytes of data from the file descriptor fd_in to the file descriptor fd_out, where one of the descriptors must refer to a pipe.
Answered By - Zaur Nasibov Answer Checked By - Terry (WPSolving Volunteer)