ã·ã§ã«ããã§ãéãå¦çã¨ããã®ã¯ã¡ããã¡ãããã£ã¦ãä¾ãã°è¶ ãã«ããã°ãã¡ã¤ã«ã移åãã¦å§ç¸®ãããã¨ãããä»äºã¯ä¸çä¸ã®ããããå ´æã§æ¯æ¥è¡ããã¦ããããããã³ãã³ãã©ã¤ã³ããã§ã大éã®å§ç¸®æ¸ã¿ãã°ãã¡ã¤ã«ããã£ãºãã«å±éããããã¨ãã
ãããã£ã¬ã¯ããªä»¥ä¸ã«åå¨ããããããã®ãã¡ã¤ã«ã(å§ç¸®æ¸ã¿ã®ãã®ãé¤ãã¦)å ¨é¨ bzip2 å§ç¸®ãããï¼ã¨æã£ãããã¨ããããããã£ã¨æ¬¡ã®ããã«ã³ãã³ãã©ã¤ã³ã§å©ãã°ããã
$ find . -not -name '*.bz2' | xargs bzip2
ããã§ãã¾ããããªã«åé¡ãªãå¹ççã«bzip2å§ç¸®ãã§ãããã ããããã
æè¿ã¯è¤æ°ã³ã¢ã®CPUãæ®éã«è»¢ãã£ã¦ããããã¾ã¤ããHyperThreadingãæå¹ã«ãªã£ã¦ãããã¦OSããè¦ããè«çCPUæ°ããã³ããªããæ®éã«8ã¨ãããããã®ä¸æ¹ã§è¤æ°ã³ã¢ã使ç¨ãã¦ãããã³ãã³ãã¨ããã®ã¯ããã¾ããªãã¦ãå®éä¸ã®ãããªã³ãã³ãã©ã¤ã³ãæ®éã«å©ãã¨ã4Core HTã®ãã·ã³ã§ã¯æ¬¡ã®ããã«å®ã«ãã®æ²ããå¦çç¶æ³ã«ãªã£ã¦ãã¾ãã
å®ã«æ²ãããã©ãæ²ãããåãããªã人ã¯ããã¼ãæ®å¿µã§ããã
CPUã¯ä½ã£ã¦ãã®ã§å¤§éã®ãã¡ã¤ã«ã並åå¦çããããã¨ããå ´åãæã§ãããªãã¾ãè¤æ°ã®ç«¯æ«ãéãã¦ããããã§å®è¡ãã¦ããããããããã¯ããã§ã©ã®ãã¡ã¤ã«ãã©ã®ç«¯æ«ã§å¦çããã®ã¨ãèããã®ãé¢åã ãããã«ã§ããã°ãã·ã§ã«ã¹ã¯ãªããä¸ã§ãå¯è½ãªæ¹æ³ã欲ããã
xargs ã® -P (--max-procs) ãªãã·ã§ã³ã使ã
ã¨ããã§xargsã«ã¯ -P ã¨ãããªãã·ã§ã³ããã(ãã¨ã«ä»æ¥æ°ä»ãã)ã詳ããã¯åèª man xargs ãã¦ããã ããããããªãã¨è¤æ°ããã»ã¹ã並åã«å®è¡ãã¦ãããããã*1ããããã¨ï¼
ãããã㯠xargs ã®åä½ãã¡ããã¨ç解ãã¦ããªãã¨ããããªã以ä¸ã®ããã«ãã£ã¦ãããã¢ã«åããªãã
$ find . -not -name '*.bz2' | xargs -P 2 bzip2
æå¾ ã¨ãã¦ã¯2並åå®è¡ããã¦ååã®æéã§å®äºããã£ã¦ã¨ããã ãããã©ãããã¯ãã®æå¾ éãã«ã¯åããªãããã¶ãæåã®ä¾ã¨åãããã«1CPUã ã使ã£ã¦åä½ããåããããªæéã§å®äºããã
xargsã®åä½ã«ã¤ãã¦å°ã
xargsã®åä½ã«è©³ãããªã人ã®ããã«ãã¡ãã復ç¿ãxargsã¯ä»¥ä¸ã®ããã«åä½ããã
- æ¨æºå
¥åãã次ã
ã¨è¡ãèªã¿è¾¼ã
- çµç«¯ã«éããããã³ãã³ãã«ä¸ããããå¼æ°*2ã«çããæ°ã«éããã¾ã§ç¶ãã
- èªãã ãã¼ã¿ãå¼æ°ã«ä¸ããããã³ãã³ããèµ·åãã
- æ¨æºå ¥åããã¾ã èªãããã¼ã¿ãããã° 1. ã«æ»ã
ã§ããã®ãã³ãã³ãã«ä¸ããããå¼æ°ãã ãããã£ãã大ããã以ä¸ã®ããã«ããã°ããªãã®æå ã®ã·ã¹ãã ã§ã®ãµã¤ãºããããã(80000ã¯æ¯ãåãã¡ããå ´åã«ã¯é©å½ã«å¤§ãããããã¨ã)
$ yes | head -80000 | xargs perl -e 'print scalar(@ARGV),"\n";'
Mac OSX 10.6 ã§å®è¡ãã㨠5000 ã§ãCentOS5.6ã§ãã£ã¦ã¿ãã 16379 ã ã£ã*3ãèªå® ã®Debian(testing kernel 2.6.28.7)ã 㨠65519ã
ã¤ã¾ãä½ãèããã« xargs ãå®è¡ããã¨ããã®æ°ã«éããªãéãã¯1åã®ã³ãã³ãã§å®è¡ããã¡ããã®ã ãã«ã¸ã¥ã¢ã«ãªä¸¦åå®è¡ã«å£ãç«ã¡ã¯ã ããã
xargs -L ãªãã·ã§ã³ãä½µç¨
ã§ãã¡ããã¨åã人ãèãã¦ããã¦ãã¦ããããããã¨ã« xargs ã«ã¯ -L ã¨ãããªãã·ã§ã³ãããããããæå®ããã°ã³ãã³ã1åã®å®è¡ã«ããã¤ã®å¼æ°ã(æ大ã§)ä¸ããããæå®ã§ããã
ãã®ãããææªã±ã¼ã¹ã§ã以ä¸ã®ããã«ããã°ã常ã«ãµãã¤ã® bzip2 ã³ãã³ãã並åã§èµ°ã£ã¦(ã ããã)åéã§å§ç¸®ãå®äºããã¨ããããã ã
$ find . -not -name '*.bz2' | xargs -L 1 -P 2 bzip2
ãã ã注æç¹ãxargsã§èµ·åãããã³ãã³ãã軽ããã㤠xargs çµç±ã§é£ãããå¼æ°ã®æ°ãè¨å¤§ãªãã®ã¨ãªãå ´åã並åå®è¡ã«ããã¡ãªãããããã³ãã³ãèµ·ååæ°(forkåæ°)ã«ãããã¡ãªããã®æ¹ãä¸åãå¯è½æ§ãé«ããããããå ´åã«ã¯ -L ã§æå®ããæ°ãå¤ããããªã©ãã¦å¯¾å¦ãããã
å®éã®ã¨ãã -L 1 ã¨ããæå®ã¯ã»ã¨ãã©ã®å ´åãã³ã»ã³ã¹ã ã¨æããã³ãã³ãå®è¡æã«ãå¼æ°ãã©ã®ãããã«ãªããããããã©ã®ããã並åã§å®è¡ãã¦ãããããèãã¦ãã¾ããããã¼ã« -L -P ãããã調æ´ããã¨ããããã*4
ããããã¦ä¸¦åå®è¡ã確èª
xargs ã«ãã並åå®è¡ãè¡ããã¦ãããã¨ãããä¸åº¦ããã£ã¨ç¢ºèªãã¦ãããã-P ã®ãããªãã§ä»¥ä¸ã®åä½ã®éããè¦ãã°ã¤ãããã
$ yes | head -100 | xargs -L 50 perl -e 'print "start!\n"; sleep(1); print scalar(@ARGV),"\n";' start! 50 start! 50 $ yes | head -100 | xargs -L 50 -P 2 perl -e 'print "start!\n"; sleep(1); print scalar(@ARGV),"\n";' start! start! 50 50
ãããªãã·ã§ã«ã¹ã¯ãªããããã§ãç°¡åã«éãå¦çã並åå®è¡ã§ãããï¼ ãã£ããã£ãï¼
(追è¨) See also: GNU Parallel
ããããã°ã¡ãã£ã¨åã«ãããªã¨ã³ããªãä¸ãã£ã¦ã¦èªåãã¸ã¼ã£ã¨èªãã§ã¾ããã
GNU Parallelがすごすぎて生きるのがつらい - As a Futurist...
xargs -P ã¯ããã¦ãã®ç°å¢ã§ããã£ã¨å®è¡ã§ãã¦ä¾¿å©ã ãã©ãç´°ããå¶å¾¡ã¨ããã¡ããå
¨ç¶å¹ããªãã®ã§ãå³å¯ã«ã³ã³ããã¼ã«ããã並åå¦çããããããªã GNU Parallel ã¯è¯ãããã ãªã¼ã¨æãã¾ãã
*1:ããã©ã«ã㯠-P 1
*2:ã·ã¹ãã ã«ããç°ãªã
*3:'perl', '-e', ããã³ã¹ã¯ãªãããå ã㦠16382 == 2^14 - 2
*4:-P 0 ã¨ãããã¨ãå¯è½ãªéãæ大ã«ä¸¦åå®è¡ãã¨ããã£ã¦ãããã¿ããã ãã©ãä»ã®å¦çã®ããã«ç©ºãCPUãæ®ãã¦ããã®ã¯å¤§äºã ã¨æããã â¦â¦