ååã®çããã¸ã®ã¢ã³ãµã¼ã½ã³ã°ã§ãã
RubyKaigi ããã§ãã£ããé ã... ã¨æã£ããRubyKaigi takeoutã®æ¯ãè¿ããæ¸ãã¦ãªã...
ã¿ã¤ãã«ã®éãï¼éãï¼ï¼ãforkããããã»ã¹ãCoWãã©ããããèµ·ããã¦ãããå¯è¦åãããã¼ã«ãä½ãã¾ããã
ã¾ã ç§ã®ç解ãæµ ãã¨ãããããã®ã§ããããã£ããåä½ä¾ãªã©ãæ²ç¤ºãã¦ã¿ã¾ãã
hiboma.c
ã®å ´å
ä¸ã®hibomaããã®CoWã®ãµã³ãã«ããã°ã©ã ã®ä¾ã§ããããã¸ã§ã¯ãã®miscã¨ãããã£ã¬ã¯ã㪠ã®ä¸ã« exec_waiter.rb
ã¨ããè£å©ã¹ã¯ãªãããç½®ãã¦ããã¾ãããããçµç±ãã¦ããã°ã©ã ãèµ·åããã¨ã親ã®PIDã表示ãã¦å®è¡ãåæ¢ãã¾ãã
cowsnoop/misc$ ruby exec_waiter.rb ./hiboma Program "./hiboma" is waiting now to be traced. Please setup your BPF fantastic tools against: PID = 198039 Then - hit return to continue
å¥ã®ã¿ã¼ããã«ã§ cowsnoop ãèµ·åãã¾ãã
$ sudo ./mruby/bin/cowsnoop --track-first-clone 198039 Tracking first forked child.
æ»ã£ã¦ä½ããã¼ãæ¼ã㨠hiboma
ã³ãã³ãã«execããã®ã§ããã¬ã¼ãµã®æ¹ã«æ»ãã¨ã確ãã«1ç§æ¯ã«1åã®CoWã®çºçãæ¤ç¥ãã¦ãã¾ãã
mmap: 0x100000000000 parent pid:198039 child pid:198142 cow address: 0x100000000000 cow address: 0x100000001000 cow address: 0x100000002000 cow address: 0x100000003000 cow address: 0x100000004000 cow address: 0x100000005000 cow address: 0x100000006000 cow address: 0x100000007000 cow address: 0x100000008000 ...
[!] Tracking: child pid = 198142 [2021-09-24 15:40:02 +0000] 7: @@@@@@@ [2021-09-24 15:40:03 +0000] 1: @ [2021-09-24 15:40:04 +0000] 1: @ [2021-09-24 15:40:05 +0000] 1: @ [2021-09-24 15:40:06 +0000] 1: @ [2021-09-24 15:40:07 +0000] 1: @ [2021-09-24 15:40:08 +0000] 1: @ [2021-09-24 15:40:09 +0000] 1: @ [2021-09-24 15:40:10 +0000] 1: @ [2021-09-24 15:40:11 +0000] 1: @ [2021-09-24 15:40:12 +0000] 1: @ ...
Ruby ã®ã¹ã¯ãªããã®å ´å
以ä¸ã®ãããªRubyã¹ã¯ãªãããä½ãã¾ããããã«ãã¢ãããã¦ãã¾ãã
puts "Current pid: #{$$} / Hit key to continue" gets $a = []; p Process.waitpid(fork do puts $$ loop do $a << Hash.new sleep 0.0001 end end)
ãããåæ§ã«èµ·åãã
$ ruby bigarray.rb Current pid: 198287 / Hit key to continue
ãã¬ã¼ã¹ãã
$ sudo ./mruby/bin/cowsnoop --track-first-clone 198287 Tracking first forked child.
å
ã®ããã°ã©ã ã®èµ·åãåéãã¾ããããã¨ãåããã»ã¹ 198357
ã®CoWã®çºçãæç³»åã§è¿½ããã¨ãã§ãã¾ãã
[!] Tracking: child pid = 198357 [2021-09-24 15:43:53 +0000] 53: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ [2021-09-24 15:43:54 +0000] 7: @@@@@@@ [2021-09-24 15:43:55 +0000] 3: @@@ [2021-09-24 15:43:56 +0000] 8: @@@@@@@@ [2021-09-24 15:43:57 +0000] 4: @@@@ [2021-09-24 15:43:58 +0000] 7: @@@@@@@ [2021-09-24 15:43:59 +0000] 5: @@@@@ [2021-09-24 15:44:00 +0000] 46: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ [2021-09-24 15:44:01 +0000] 0: [2021-09-24 15:44:02 +0000] 383: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ [2021-09-24 15:44:03 +0000] 0: [2021-09-24 15:44:04 +0000] 0: [2021-09-24 15:44:05 +0000] 0: [2021-09-24 15:44:06 +0000] 0: [2021-09-24 15:44:07 +0000] 0: [2021-09-24 15:44:08 +0000] 0: [2021-09-24 15:44:09 +0000] 0: [2021-09-24 15:44:10 +0000] 0: [2021-09-24 15:44:11 +0000] 0: [2021-09-24 15:44:12 +0000] 0: [2021-09-24 15:44:13 +0000] 25: @@@@@@@@@@@@@@@@@@@@@@@@@ [2021-09-24 15:44:14 +0000] 0: [2021-09-24 15:44:15 +0000] 0: [2021-09-24 15:44:17 +0000] 0: [2021-09-24 15:44:18 +0000] 1: @ [2021-09-24 15:44:19 +0000] 0: ...
ãã®æãä¸ç·ã«è¦ªåã®VSZ/RSSããã©ãã¯ããã¨ãæåã®æ¹ã¯åããã»ã¹ã®æ¹ããã¡ããRSSãå°ããç¶æ ã§ãã
$ ps auxf | grep -e RSS -e bigarray USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND vagrant 198287 0.0 0.2 78844 22136 pts/1 S+ 15:43 0:00 | \_ ruby bigarray.rb vagrant 198357 1.6 0.2 78844 19856 pts/1 S+ 15:43 0:00 | \_ ruby bigarray.rb vagrant 198369 0.0 0.0 8008 880 pts/2 S+ 15:44 0:00 \_ grep --color=auto -e RSS -e bigarray
ããã°ã©ã ã®æ§è³ªä¸ã次第ã«åããã»ã¹ã®RSSãè¥å¤§åãã¦ããã¾ããã¾ããã©ããã®ã¿ã¤ãã³ã°ã§VSZãæ¡å¼µãã¾ãã
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND vagrant 198287 0.0 0.2 78844 22136 pts/1 S+ 15:43 0:00 | \_ ruby bigarray.rb vagrant 198357 1.8 0.2 79132 20080 pts/1 S+ 15:43 0:00 | \_ ruby bigarray.rb vagrant 198393 0.0 0.0 8008 816 pts/2 S+ 15:44 0:00 \_ grep --color=auto -e RSS -e bigarray
VSZãæ¡å¼µãããããã§ãCoWãä¸åèµ·ãããªããªãï¼ããªãã¡ã親åã§å ±éã®é åããã¯ã¿åºãã¨ããã«ãªãã¸ã§ã¯ããä½ããã¯ãããï¼ï¼ãã¨ãããã¨ã確èªã§ãã¾ãã
cowsnoopã«æ©è½ã追å ããã°ãå ã ã®é¢å¿ãã¨ã ã£ãunicornãã¯ã¼ã«ã¼ãã¨ã«ã©ããããCoWãçºçããã¦ããã®ãã¨ããç¹ã追ããããããããããã¾ããã
ã¡ãªã¿ã«ãã®cowsnoopã¯ãããããBPF CO-REã®ãã¬ã¼ã¹ãã¼ã«ã§ãããmruby + libbpf ã§æ¸ãã¦ãã¾ãããã¡ããããªãã¸ããã³ã¼ãã¯Cã§æ¸ãã¦ãã¾ãããã¾ãBPFãã¤ããªé¨åã¯æ®éã«ã¨ãããCããä½ã£ã¦ãã¾ãï¼Rucyã¯ã¾ã å ¨ç¶å®æ¦æå ¥ã§ãã¾ãã...ï¼ã
ã§ãããã¬ã¼ãµã®ã¡ã¤ã³é¨åãRubyã§æ¸ããã¨ããã®ã¯ãåã«ã¨ã£ã¦å¤§å¤æ°åã®è¯ããã¨ã§ãã
ã¾ãããã¬ã¼ãµã®ãããªãã¼ã«ãæ¸ãéã¯çµæ§æ¢ç´¢çãªã³ã¼ãã£ã³ã°ãããå¾åãããæ°ããã¦ãã¦ãæå¤ã¨ã¹ã¯ãªããè¨èªã§æ¸ãéè¦ãããããããªããã¨æãã¾ãã
RubyKaigi takeout ãçµã¦ã Observability of Ruby, by Ruby, for Ruby ã®æã声ã§ãRubyã§æ¸ããRubyã®ããã©ã¼ãã³ã¹æ¸¬å®ãã¼ã«ç¾¤ï¼ãã¡ããä»ã®ããã°ã©ã ããã¬ã¼ã¹ã§ããã§ããã...ï¼ã Infrared.rb ã¨ããããã¸ã§ã¯ãã§å§ãããã¨ã«ãã¾ããããã®ããã°ã§ãã¾ãããããã¨ç´¹ä»ã§ããã°ã¨æãã¾ãã