为什么 splice() 在我的系统上表现如此糟糕?
我想测试 splice() 系统调用的性能。我将它与传统的读/写进行了比较。
/* 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;
}
这是结果:
spilce() 似乎并没有提高性能,也没有减少 CPU 时间。为什么?我的内核版本是2.6.35-28,ubuntu 10.10。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定您的描述符之一实际上是管道吗?
因为,
man splice
说:Are you sure, that one of your descriptors is actually a pipe?
Because,
man splice
says: