作者:Yu Feng
链接:http://blog.yufeng.info/archives/1981
# 命令1,管道导入shell> cat huge_dump.sql | mysql -uroot;
# 命令2,重定向导入shell> mysql -uroot < huge_dump.sql;
大家先看一下上面二个命令,假如huge_dump.sql文件很大,然后猜测一下哪种导入方式效率会更高一些?
以下来自@阿里褚霸的分享:
这个问题挺有意思的,我的第一反应是:
没比较过,应该是一样的,一个是cat负责打开文件,一个是bash
这种场景MySQL运维操作里面应该比较多,所以就花了点时间做了个比较和原理上的分析:
我们先构造场景:
首先准备一个程序b.out来模拟mysql对数据的消耗:
int main(int argc, char *argv[])
while(fread(buf, sizeof(buf), 1, stdin) > 0);
return 0;
}
$ gcc -o b.out b.c
$ ls|./b.out
再来写个systemtap脚本用来方便观察程序的行为。
$ cat test.stp
function should_log(){
return (execname() == "cat" ||
execname() == "b.out" ||
execname() == "bash") ;
}
probe syscall.open,
syscall.close,
syscall.read,
syscall.write,
syscall.pipe,
syscall.fork,
syscall.execve,
syscall.dup,
syscall.wait4
{
if (!should_log()) next;
printf("%s -> %s
", thread_indent(0), probefunc());
}
probe kernel.function("pipe_read"),
kernel.function("pipe_readv"),
kernel.function("pipe_write"),
kernel.function("pipe_writev")
{
if (!should_log()) next;
printf("%s -> %s: file ino %d
", thread_indent(0), probefunc(), __file_ino($filp));
}
probe begin { println(":~") }
这个脚本重点观察几个系统调用的顺序和pipe的读写情况,然后再准备个419M的大文件huge_dump.sql,我们几十G内存的机器很容易内存里放下:
$ sudo dd if=/dev/urandom of=huge_dump.sql bs=4096 count=102400102400+0 records in102400+0 records out419430400 bytes (419 MB) copied, 63.9886 seconds, 6.6 MB/s
因为这个文件是用bufferio写的,所以它的内容都cachepagecahce内存里面,不会涉及到磁盘。
好了,场景齐全了,我们接着来比较下二种情况下的速度,第一种管道:
# 第一种管道方式
$ time (cat huge_dump.sql|./b.out)
real 0m0.596s
user 0m0.001s
sys 0m0.919s
# 第二种重定向方式
$ time (./b.out huge_dump.sql$ sudo stap test.stp:~ 0 bash(26570): -> sys_read 0 bash(26570): -> sys_read 0 bash(26570): -> sys_write 0 bash(26570): -> sys_read 0 bash(26570): -> sys_write 0 bash(26570): -> sys_close 0 bash(26570): -> sys_pipe 0 bash(26570): -> sys_pipe 0 bash(26570): -> do_fork 0 bash(26570): -> sys_close 0 bash(26570): -> sys_close 0 bash(26570): -> do_fork 0 bash(13775): -> sys_close 0 bash(13775): -> sys_read 0 bash(13775): -> pipe_read: file ino 20906911 0 bash(13775): -> pipe_readv: file ino 20906911 0 bash(13776): -> sys_close 0 bash(13776): -> sys_close 0 bash(13776): -> sys_close 0 bash(13776): -> do_execve 0 bash(26570): -> sys_close 0 bash(26570): -> sys_close 0 bash(26570): -> sys_close 0 bash(13775): -> sys_close 0 bash(26570): -> sys_wait4 0 bash(13775): -> sys_close 0 bash(13775): -> sys_close 0 b.out(13776): -> sys_close 0 b.out(13776): -> sys_close 0 bash(13775): -> do_execve 0 b.out(13776): -> sys_open 0 b.out(13776): -> sys_close 0 b.out(13776): -> sys_open 0 b.out(13776): -> sys_read 0 b.out(13776): -> sys_close 0 cat(13775): -> sys_close 0 cat(13775): -> sys_close 0 b.out(13776): -> sys_read 0 b.out(13776): -> pipe_read: file ino 20906910 0 b.out(13776): -> pipe_readv: file ino 20906910 0 cat(13775): -> sys_open 0 cat(13775): -> sys_close 0 cat(13775): -> sys_open 0 cat(13775): -> sys_read 0 cat(13775): -> sys_close 0 cat(13775): -> sys_open 0 cat(13775): -> sys_close 0 cat(13775): -> sys_open 0 cat(13775): -> sys_read 0 cat(13775): -> sys_write 0 cat(13775): -> pipe_write: file ino 20906910 0 cat(13775): -> pipe_writev: file ino 20906910 0 cat(13775): -> sys_read 0 b.out(13776): -> sys_read 0 b.out(13776): -> pipe_read: file ino 20906910 0 b.out(13776): -> pipe_readv: file ino 20906910 0 cat(13775): -> sys_close 0 cat(13775): -> sys_close 0 bash(26570): -> sys_wait4 0 bash(26570): -> sys_close 0 bash(26570): -> sys_wait4 0 bash(26570): -> sys_write
stap收集数据了,我们另外一个窗口运行管道的情况:
$ cat huge_dump.sql|./b.out
我们从systemtap的日志可以看出:
bash fork了2个进程。
然后execve分别运行cat 和 b.out进程, 这二个进程用pipe通信。
数据从由cat从 huge_dump.sql读出,写到pipe,然后b.out从pipe读出处理。
那么再看下命令2重定向的情况:
$ ./b.out < huge_dump.sql
stap输出:
0 bash(26570): -> sys_read
0 bash(26570): -> sys_read
0 bash(26570): -> sys_write
0 bash(26570): -> sys_read
0 bash(26570): -> sys_write
0 bash(26570): -> sys_close
0 bash(26570): -> sys_pipe
0 bash(26570): -> do_fork
0 bash(28926): -> sys_close
0 bash(28926): -> sys_read
0 bash(28926): -> pipe_read: file ino 20920902
0 bash(28926): -> pipe_readv: file ino 20920902
0 bash(26570): -> sys_close
0 bash(26570): -> sys_close
0 bash(26570): -> sys_wait4
0 bash(28926): -> sys_close
0 bash(28926): -> sys_open
0 bash(28926): -> sys_close
0 bash(28926): -> do_execve
0 b.out(28926): -> sys_close
0 b.out(28926): -> sys_close
0 b.out(28926): -> sys_open
0 b.out(28926): -> sys_close
0 b.out(28926): -> sys_open
0 b.out(28926): -> sys_read
0 b.out(28926): -> sys_close
0 b.out(28926): -> sys_read
0 b.out(28926): -> sys_read
0 bash(26570): -> sys_wait4
0 bash(26570): -> sys_write
0 bash(26570): -> sys_read
bash fork了一个进程,打开数据文件。
然后把文件句柄搞到0句柄上,这个进程execve运行b.out。
然后b.out直接读取数据。
现就非常清楚为什么二种场景速度有3倍的差别:
命令1,管道方式: 读二次,写一次,外加一个进程上下文切换。
命令2,重定向方式:只读一次。
结论:Linux下大文件重定向效率更高。
PS: 按惯例,发推广都会发个红包,so,抢红包啦~
[恭喜发财,大吉大利] 抢红包喽~
好书推荐
深度学习是人工智能的一个分支,受到人类大脑的启发,致力于指导计算机用神经网络进行学习。线文本翻译、自动驾驶、商品推荐和智能语音助手等一系列令人兴奋的现代技术应用领域,都深度学习的辅助下取得了突破性进展。