æ¬æ¥RubyKaigiã§ã話ããã¦ãã¾ãããBPFã®Observabilityé¢é£ã®æ©è½ãç´¹ä»ããªããå®éã«ãµã³ãã«ãRubyã§æ¸ãã¦ããã¿ãããªæµãã§ããã以ä¸ã«è³æãã
ä¸çªè¦ãããã£ããRubyã®GCé¢ä¿USDTãç¨ãã¦ã¬ã¤ãã³ã·ãå¯è¦åãããã¨ãããã¼ã«ã®ãã¢ã...æµããªãã£ãã®ã§*1ãå¿æ®ããæ´ãæµãã¹ãè£è¶³è¨äºãæ¸ãã¾ãã
USDTï¼
Rubyãç¹å®ã®ãªãã·ã§ã³ --enable-dtrace
ã§configureãã¦ãã«ãããã¨ãUSDTã¨å¼ã°ããæ
å ±ãRubyã®ãã¤ããªã«ä»ä¸ããã¾ãããã®æ
å ±ã使ãã¨Rubyã®çãã¦ããããã°ã©ã ã«å¯¾ãã¦ããã¾ãã¾ãªç´°ããæ
å ±ãåå¾ã§ããããã«ãªãã¾ããä¾ãã°ä»¥ä¸ã®ã¿ã¤ãã³ã°ã§ã¤ãã³ããããã¯ããã¾ãã
- ãªãã¸ã§ã¯ãä¸è¬ãã·ã³ãã«ãæååãé åãããã·ã¥ã®ä½æ
- Cå®ç¾©ã®ã¡ã½ããã®å¼ã³åºãããªã¿ã¼ã³
- GCã®éå§ãçµäº
- ä¾å¤çºç
ããããTracePointã¨ããæ©æ§ãããã¾ãããTracePointãããã°ã©ã ããã©ã¼ãã³ã¹ã«å¤§ããå½±é¿ãä¸ãããã¨ã¯å¨ç¥ã®ãã¨ã§ããUSDTçµç±ã§ããã¤BPFã®ãããªä»çµã¿ã§æ å ±ãåå¾ããã¨å®ç¨ä¸å½±é¿ãªãã¬ãã«ã®å¯ä½ç¨ã§ç´°ããæ å ±ãåå¾ã§ãã¾ããã¯ãã
ã¨ããã§USDTã調ã¹ã¦ããã¨ãdtraceãã¨ããè¨èãããåºã¦ãã¾ãããUSDTã¨ããæ¦å¿µã¯ããããDTraceã®æèã§åºã¦ãããã®ã®ããã§ããããLinuxã§ã使ããããã«ããæãã§é²åãã¦ãããã®*2ã¿ããã§ãã
詳細ã¯ãaaronã«ãããã³ã¾ã®DTraceç´¹ä»ï¼first introduced in 2.0ãªãã§ããã / å ¨ãåãProbeã使ãã¾ãï¼ã
kosakiããã«ãã解説ãªã©ãã©ããã
ä½ã£ããã®
çãã¦ããRubyããã»ã¹ã«å¯¾ãã¦ã¢ã¿ãããã1ç§ãã¨ã«ä»¥ä¸ã表示ãã¾ãã
- ãã®æã®RSS(Resident Set Size)
- 1ç§éã«å®è¡ãããGC Markã®åæ°ã¨å¹³åå®è¡æé
- 1ç§éã«å®è¡ãããGC Sweepã®åæ°ã¨å¹³åå®è¡æé
$ sudo ruby ./gcsnoop.rb 186197 $(rbenv which ruby) Start tracing TIME EVENT RSS(KB) ELAPSED(ms/event) 2022-09-09 15:22:52.487468 RSS 23372 2022-09-09 15:22:53.500447 MARK - 2022-09-09 15:22:53.505751 SWEEP - 2022-09-09 15:22:53.531737 RSS 23204 2022-09-09 15:22:54.603751 MARK - 2022-09-09 15:22:54.604788 SWEEP - 2022-09-09 15:22:54.637420 RSS 23444 2022-09-09 15:22:55.712456 MARK - 2022-09-09 15:22:55.712677 SWEEP 0.250, 1 time(s) 2022-09-09 15:22:55.740717 RSS 23588 2022-09-09 15:22:56.799344 MARK - 2022-09-09 15:22:56.801217 SWEEP - 2022-09-09 15:22:56.834391 RSS 23672 2022-09-09 15:22:57.908051 MARK - 2022-09-09 15:22:57.908299 SWEEP 0.149, 1 time(s) 2022-09-09 15:22:57.941105 RSS 23756
å®é¨ã¨ãã¦ããã¨ã¡ã¢ãªãªã¼ã¯ãããããªSinatraã®ã¢ããªãæ¸ãã¾ãããããã¯ã²ã©ãã§ãããå®é¨ã§ãã
require 'sinatra' $count = 0 $leak = [] $next = 100 puts "LEAK MODE: #{! ENV["LEAKY"].nil?}" get '/' do if ENV["LEAKY"] $leak << (1..100).map { Object.new } end "OK" end
$ LEAKY=1 ruby sinatra-app.rb 2>/dev/null & LEAK MODE: true Puma starting in single mode... * Puma version: 5.6.5 (ruby 3.1.2-p20) ("Birdie's Version") * Min threads: 0 * Max threads: 5 * Environment: development * PID: 187396 * Listening on http://127.0.0.1:4567 * Listening on http://[::1]:4567 Use Ctrl-C to stop $ ps auxf | grep -e RSS -e puma USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND ubuntu 187441 1.0 0.3 363388 26396 pts/3 Sl 15:26 0:00 | \_ puma 5.6.5 (tcp://localhost:4567) [rk2022-benchtools] $ ab -c 1000 -t 10 http://127.0.0.1:4567/ ... $ ps auxf | grep -e RSS -e puma USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND ubuntu 187515 33.5 3.6 855116 295512 pts/3 Sl 15:27 0:21 | \_ puma 5.6.5 (tcp://localhost:4567) [rk2022-benchtools]
ç°¡åã«RSSãè¥å¤§åããããã¨ãã§ãã¾ããGCãã©ããªããè¦ã¦ã¿ã¾ãã
$ sudo ruby ./gcsnoop.rb 187441 $(rbenv which ruby) Start tracing TIME EVENT RSS(KB) ELAPSED(ms/event) 2022-09-09 15:30:01.415746 RSS 22860 2022-09-09 15:30:02.489423 MARK - 2022-09-09 15:30:02.489839 SWEEP - 2022-09-09 15:30:02.526016 RSS 22860 ... 2022-09-09 15:30:28.291423 RSS 22860 2022-09-09 15:30:29.292707 MARK 0.552, 17 time(s) 2022-09-09 15:30:29.292734 SWEEP 0.055, 254 time(s) 2022-09-09 15:30:29.297429 RSS 49260 2022-09-09 15:30:30.298606 MARK 1.274, 6 time(s) 2022-09-09 15:30:30.298636 SWEEP 0.060, 247 time(s) 2022-09-09 15:30:30.304768 RSS 63364 2022-09-09 15:30:31.306836 MARK 1.408, 4 time(s) 2022-09-09 15:30:31.306866 SWEEP 0.059, 255 time(s) 2022-09-09 15:30:31.311147 RSS 83784 2022-09-09 15:30:32.312385 MARK 2.297, 2 time(s) 2022-09-09 15:30:32.312416 SWEEP 0.061, 230 time(s) 2022-09-09 15:30:32.317398 RSS 111252 2022-09-09 15:30:33.318531 MARK 4.285, 2 time(s) 2022-09-09 15:30:33.318559 SWEEP 0.067, 195 time(s) ... 2022-09-09 15:30:47.433592 RSS 389912 2022-09-09 15:30:48.434317 MARK 14.691, 1 time(s) 2022-09-09 15:30:48.434350 SWEEP 0.093, 187 time(s) 2022-09-09 15:30:48.441605 RSS 423512 2022-09-09 15:30:49.442609 MARK 2.361, 1 time(s) 2022-09-09 15:30:49.442659 SWEEP 0.072, 306 time(s) 2022-09-09 15:30:49.450451 RSS 425004 2022-09-09 15:30:50.451657 MARK - 2022-09-09 15:30:50.451685 SWEEP 0.078, 336 time(s) 2022-09-09 15:30:50.459964 RSS 432168 2022-09-09 15:30:51.461260 MARK 14.032, 1 time(s) 2022-09-09 15:30:51.461293 SWEEP 0.084, 256 time(s) 2022-09-09 15:30:51.468487 RSS 449268 2022-09-09 15:30:52.469774 MARK - 2022-09-09 15:30:52.469803 SWEEP 0.057, 171 time(s) 2022-09-09 15:30:52.477342 RSS 473020 2022-09-09 15:30:53.478389 MARK 15.491, 1 time(s) 2022-09-09 15:30:53.478409 SWEEP 0.077, 391 time(s) 2022-09-09 15:30:53.485416 RSS 480392 2022-09-09 15:30:54.486348 MARK 16.764, 1 time(s) 2022-09-09 15:30:54.486378 SWEEP 0.078, 160 time(s) 2022-09-09 15:30:54.494123 RSS 512272 2022-09-09 15:30:55.495602 MARK - 2022-09-09 15:30:55.495634 SWEEP 0.066, 414 time(s) 2022-09-09 15:30:55.503252 RSS 513088 2022-09-09 15:30:56.504433 MARK 18.446, 1 time(s) 2022-09-09 15:30:56.504460 SWEEP 0.096, 149 time(s) ... ... 2022-09-09 15:31:46.065511 RSS 1165324 2022-09-09 15:31:47.066567 MARK 34.956, 1 time(s) 2022-09-09 15:31:47.066786 SWEEP 0.104, 135 time(s)
RSSã大ãããªãï¼ï¼Mark対象ã®ãªãã¸ã§ã¯ããå¢ããï¼ã¨Markãã§ã¼ãºã®æè¦æéãå¢ãã¦ãã£ã¦ãããã¨ã観測ã§ãã¾ããRSSã1GBã«éããã30msè¶ ãã«ãªã£ã¦ããããã§ãããªããSweepã®å¯¾è±¡ãå¢ããããã§ã¯ãªãããããã¡ãã¸ã®å½±é¿ã¯ããã¾ã§å¤§ããããã¾ããã
åç»ãä½ã£ã¦ãã¾ãã以ä¸ã§ãã
RSSã®è¨ç®ã«ã¯procfsã®æ å ±ããããã¦Mark/Sweepã®åæ°ã¨ã¬ã¤ãã³ã·ã®è¨æ¸¬ã«USDTã¨ãBPFãRbBCCã使ãã¾ããã
ããã¦ãã®ãã¼ã«èªä½ã¯ USDTæå¹ãã«ãã®Rubyã使ã£ã¦ããã°ã©ããªã¢ããªã±ã¼ã·ã§ã³ã«ã使ãã ã®ã§ãç¾å ´ã®pumaãfluentdã®GCã¬ã¤ãã³ã·ã®è¨æ¸¬ã«ãã®ã¾ã¾ä½¿ããããããã¾ãããUSDTæå¹ãã«ãã®Rubyãæºåããã®ããããªãã«ãã¼ãã«ã§ããææ¦ãã¦ã¿ã¦ãã ããã
ã³ã¼ãã®å ¨ä½ã¯ ãã¡ã ã§ãã
Observabilityç¨éã®BPFãæ´»ç¨ããéãä¾ãã°ãã¡ã¤ã«ã¢ã¯ã»ã¹ããããã¯ã¼ã¯ã®ã¬ã¤ãã³ã·ãªã©ãè¨æ¸¬ããã«ãã¦ããåæã¨ãã¦ã«ã¼ãã«å é¨ã®ç¥èãå¿ è¦ã«ãªããã¡ã§ããä¸æ¹USDTãuprobeã§ããã°ã対象ã¢ããªã±ã¼ã·ã§ã³ã®ä¸èº«ãããã£ã¦ããã°æ´»ç¨ã§ãã¾ãããããããªç¨éãè¦ã¤ããã¨ãããªã¨æãã¾ãã
ã¨è¨ãããã£ãã
ä¼æã¯ã¾ã ã¾ã æ®ã£ã¦ãã¦ãç§ãä¼å ´ãããããã¨ãã¦ãã¾ãã®ã§ãèå³ã®ããæ¹ã¯ã声ãããã ããï¼