国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Linux > 正文

Linux大文件重定向與管道的效率是怎樣的

2024-08-27 23:55:17
字體:
供稿:網(wǎng)友
  這篇文章主要講解了“Linux大文件重定向和管道的效率是怎樣的”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Linux大文件重定向和管道的效率是怎樣的”吧!
 
  導(dǎo)讀 大家先看一下二個 命令,假如huge_dump.sql文件很大,然后猜測一下哪種導(dǎo)入方式效率會更高一些?
  # 命令1,管道導(dǎo)入
  shell> cat huge_dump.sql | mysql -uroot;
  # 命令2,重定向?qū)?br />  shell> mysql -uroot < huge_dump.sql;
  大家先看一下上面二個命令,假如huge_dump.sql文件很大,然后猜測一下哪種導(dǎo)入方式效率會更高一些?
 
  這個問題挺有意思的,我的第一反應(yīng)是:沒比較過,應(yīng)該是一樣的,一個是cat負(fù)責(zé)打開文件,一個是bash
 
  這種場景在MySQL運(yùn)維操作里面應(yīng)該比較多,所以就花了點(diǎn)時間做了個比較和原理上的分析:
 
  我們先構(gòu)造場景:
 
  首先準(zhǔn)備一個程序b.out來模擬mysql對數(shù)據(jù)的消耗:
 
  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/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(":~") }
 
  好了,場景齊全了,我們接著來比較下二種情況下的速度,第一種管道:
 
  # 第一種管道方式
  $ time (cat huge_dump.sql|./b.out)
    
  real    0m0.596s
  user    0m0.001s
  sys     0m0.919s
  從執(zhí)行時間數(shù)看出來速度有3倍左右的差別了,第二種明顯快很多。
 
  是不是有點(diǎn)奇怪?好吧我們來從原來上面分析下,還是繼續(xù)用數(shù)據(jù)說話:
 
  這次準(zhǔn)備個很小的數(shù)據(jù)文件,方便觀察然后在一個窗口運(yùn)行stap
 
  $ echo hello > 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

(編輯:武林網(wǎng))

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武胜县| 北碚区| 平远县| 贵港市| 夏津县| 溧阳市| 周宁县| 民和| 扬州市| 汕头市| 石河子市| 通化县| 嵊泗县| 南京市| 栾川县| 徐水县| 双辽市| 华宁县| 河南省| 灵山县| 靖江市| 三亚市| 睢宁县| 安岳县| 桦甸市| 安丘市| 彭阳县| 永清县| 甘孜| 奈曼旗| 辛集市| 枞阳县| 和平区| 若羌县| 普宁市| 宝应县| 呼玛县| 青岛市| 呼玛县| 南开区| 乌鲁木齐市|