linux管道 日志,Linux大文件重定向和管道的效率对比

183965069_2_20200227042015222作者: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文件很大,然后猜测一下哪种导入方式效率会更高一些?

以下来自@阿里褚霸的分享:183965069_3_20200227042015503

这个问题挺有意思的,我的第一反应是:没比较过,应该是一样的,一个是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.stpfunction 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\n', 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\n', 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写的,所以它的内容都cache在pagecahce内存里面,不会涉及到磁盘。

好了,场景齐全了,我们接着来比较下二种情况下的速度,第一种管道:# 第一种管道方式$ time (cat huge_dump.sql|./b.out)real 0m0.596suser 0m0.001ssys 0m0.919s# 第二种重定向方式$ time (./b.out

从执行时间数看出来速度有3倍左右的差别了,第二种明显快很多。

是不是有点奇怪?好吧我们来从原来上面分析下,还是继续用数据说话:

这次准备个很小的数据文件,方便观察然后在一个窗口运行stap$ echo hello > huge_dump.sql$ sudo stap test.stp:~0 bash(26570): -> sys_read0 bash(26570): -> sys_read0 bash(26570): -> sys_write0 bash(26570): -> sys_read0 bash(26570): -> sys_write0 bash(26570): -> sys_close0 bash(26570): -> sys_pipe0 bash(26570): -> sys_pipe0 bash(26570): -> do_fork0 bash(26570): -> sys_close0 bash(26570): -> sys_close0 bash(26570): -> do_fork0 bash(13775): -> sys_close0 bash(13775): -> sys_read0 bash(13775): -> pipe_read: file ino 209069110 bash(13775): -> pipe_readv: file ino 209069110 bash(13776): -> sys_close0 bash(13776): -> sys_close0 bash(13776): -> sys_close0 bash(13776): -> do_execve0 bash(26570): -> sys_close0 bash(26570): -> sys_close0 bash(26570): -> sys_close0 bash(13775): -> sys_close0 bash(26570): -> sys_wait40 bash(13775): -> sys_close0 bash(13775): -> sys_close0 b.out(13776): -> sys_close0 b.out(13776): -> sys_close0 bash(13775): -> do_execve0 b.out(13776): -> sys_open0 b.out(13776): -> sys_close0 b.out(13776): -> sys_open0 b.out(13776): -> sys_read0 b.out(13776): -> sys_close0 cat(13775): -> sys_close0 cat(13775): -> sys_close0 b.out(13776): -> sys_read0 b.out(13776): -> pipe_read: file ino 209069100 b.out(13776): -> pipe_readv: file ino 209069100 cat(13775): -> sys_open0 cat(13775): -> sys_close0 cat(13775): -> sys_open0 cat(13775): -> sys_read0 cat(13775): -> sys_close0 cat(13775): -> sys_open0 cat(13775): -> sys_close0 cat(13775): -> sys_open0 cat(13775): -> sys_read0 cat(13775): -> sys_write0 cat(13775): -> pipe_write: file ino 209069100 cat(13775): -> pipe_writev: file ino 209069100 cat(13775): -> sys_read0 b.out(13776): -> sys_read0 b.out(13776): -> pipe_read: file ino 209069100 b.out(13776): -> pipe_readv: file ino 209069100 cat(13775): -> sys_close0 cat(13775): -> sys_close0 bash(26570): -> sys_wait40 bash(26570): -> sys_close0 bash(26570): -> sys_wait40 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.sqlstap输出:0 bash(26570): -> sys_read0 bash(26570): -> sys_read0 bash(26570): -> sys_write0 bash(26570): -> sys_read0 bash(26570): -> sys_write0 bash(26570): -> sys_close0 bash(26570): -> sys_pipe0 bash(26570): -> do_fork0 bash(28926): -> sys_close0 bash(28926): -> sys_read0 bash(28926): -> pipe_read: file ino 209209020 bash(28926): -> pipe_readv: file ino 209209020 bash(26570): -> sys_close0 bash(26570): -> sys_close0 bash(26570): -> sys_wait40 bash(28926): -> sys_close0 bash(28926): -> sys_open0 bash(28926): -> sys_close0 bash(28926): -> do_execve0 b.out(28926): -> sys_close0 b.out(28926): -> sys_close0 b.out(28926): -> sys_open0 b.out(28926): -> sys_close0 b.out(28926): -> sys_open0 b.out(28926): -> sys_read0 b.out(28926): -> sys_close0 b.out(28926): -> sys_read0 b.out(28926): -> sys_read0 bash(26570): -> sys_wait40 bash(26570): -> sys_write0 bash(26570): -> sys_readbash fork了一个进程,打开数据文件。

然后把文件句柄搞到0句柄上,这个进程execve运行b.out。

然后b.out直接读取数据。

现在就非常清楚为什么二种场景速度有3倍的差别:

命令1,管道方式: 读二次,写一次,外加一个进程上下文切换。

命令2,重定向方式:只读一次。

结论:Linux下大文件重定向效率更高。

PS: 按惯例,发推广都会发个红包,so,抢红包啦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值