Rails å®è¡ä¸ã« GC ã«ããã£ã¦ããæé
前回の記事では Rails 実行中のメソッド呼び出しすべてをトレースãã¦ã¿ã¾ããããä»åã®ã¿ã¼ã²ãã㯠Garbage Collection ã«è¦ãã¦ãã CPU æéã§ããåã¨åãããrender :text => "Hello World!" ãªã¢ããªã«å¯¾ã㦠Apache Bench ãã 1ä¸ãªã¯ã¨ã¹ãã®è² è·ãä¸ãããã®éã® GC æéããã¬ã¼ã¹ãã¾ããã
DTrace ã®çµæãã¿ãã¨ãè¨ 75 åã® GC ã確èªãããåè¨ã§ç´ 5 ç§ç¨åº¦ ã® CPU æéãæ¶è²»ãã¦ããããã§ããab ã®å®è¡ããã¦ããç´ 40 ç§éã¯ã(ã¢ã¯ãã£ããã£ã¢ãã¿ã§è¦ã¦ããã¨) ruby ããã»ã¹ã® CPU 使ç¨çãã»ã¼ 100% è¿ãã«å¼µãä»ãã¦ããã®ã§ããã®ãã¡ã®ç´ 1/8 ã GC ã«ä½¿ããã¦ããã¨ãããã¨ã§ããããã
# ã¡ã½ããå¼ã³åºãã®ã¨ãã¨éãããã¬ã¼ã¹å¯¾è±¡ãã²ã£ãããåæ°ãå¤ãã¯ãªãã®ã§ã DTrace ããããããã¨ã«ãããªã¼ãã¼ãããã®å½±é¿ã¯ã»ã¨ãã©ããã¾ããã§ããã(DTrace ãããããã¨ãã¨ããã§ãªãã¨ãã§ãab ã®çµæã¯ã»ã¨ãã©åãã§ãã)
以ä¸ãä½æ¥ãã°ã§ãã
ç°å¢
- MacBook (intel Core 2 Duo 2GHz)
- Mac OS X 10.5.6
- å¦çç³»ãã©ã¤ãã©ãªã®ãã¼ã¸ã§ã³ã¯ä»¥ä¸ã®ã¨ãã
% /usr/bin/ruby -v ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] % dtrace -V dtrace: Sun D 1.2.2 % rails -v Rails 2.2.2 # åç´ã« render :text => "Hello World!" ããã ãã®ã¢ããªãç¨æ % curl http://127.0.0.1:3000/posts Hello World!%
Ruby ã® GC æéããã¬ã¼ã¹ãã DTrace ã¹ã¯ãªãã
- rb_gc.d
ruby$target:::gc-begin { self->start = vtimestamp; } ruby$target:::gc-end /self->start/ { this->elapsed = vtimestamp - self->start; @count["COUNT"] = count(); @sum["SUM[us]"] = sum(this->elapsed); @avg["AVG[us]"] = avg(this->elapsed); self->start = 0; } dtrace:::END { normalize(@sum, 1000); normalize(@avg, 1000); printf("\n"); printa("%-15s %10@d\n", @count); printa("%-15s %10@d\n", @sum); printa("%-15s %10@d\n", @avg); }
DTrace ãããããã« ab ã ãå®è¡
- ã¾ããããªãã®ã§ããã
% ab -c 10 -n 10000 http://127.0.0.1:3000/posts This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) (snip) Server Software: Mongrel Server Hostname: 127.0.0.1 Server Port: 3000 Document Path: /post Document Length: 12 bytes Concurrency Level: 10 Time taken for tests: 38.598 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 4730018 bytes HTML transferred: 120000 bytes Requests per second: 259.08 [#/sec] (mean) Time per request: 38.598 [ms] (mean) Time per request: 3.860 [ms] (mean, across all concurrent requests) Transfer rate: 119.67 [Kbytes/sec] received
ä»åº¦ã¯ãDtrace ããããã¦ããã¦...
% sudo dtrace -s rb_gc.d -p 40211 dtrace: script 'rb_gc.d' matched 3 probes
åã³ ab ãå®è¡
- DTrace ããããã¦ãªãã¨ãã¨ã»ã¨ãã©åãçµæã§ã
- GC ã®ã¯ãã¾ãã¨çµãããã²ã£ããã¦ããã ããªã®ã§ãã»ã¨ãã©ãªã¼ãã¼ãããã¯ããã£ã¦ããªãããã§ãã
% ab -c 10 -n 10000 http://127.0.0.1:3000/posts This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) (snip) Server Software: Mongrel Server Hostname: 127.0.0.1 Server Port: 3000 Document Path: /post Document Length: 12 bytes Concurrency Level: 10 Time taken for tests: 39.412 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 4730018 bytes HTML transferred: 120000 bytes Requests per second: 253.73 [#/sec] (mean) Time per request: 39.412 [ms] (mean) Time per request: 3.941 [ms] (mean, across all concurrent requests) Transfer rate: 117.20 [Kbytes/sec] received
DTrace ã®çµæ
- 5373989 ãã¤ã¯ãç§ãããªãã¡ ç´ 5 ç§ç¨åº¦ãGC ã®å®è¡ã« CPU æéãæ¶è²»ããã¦ãã¾ã
% sudo dtrace -s rb_gc.d -p 40211 dtrace: script 'rb_gc.d' matched 3 probes ^C CPU ID FUNCTION:NAME 0 2 :END COUNT 75 SUM[us] 5373989 AVG[us] 71653