Skip to content

Instantly share code, notes, and snippets.

@sonots
Last active February 8, 2026 11:12
Show Gist options
  • Select an option

  • Save sonots/0cdb5309f9d302ddf9b0 to your computer and use it in GitHub Desktop.

Select an option

Save sonots/0cdb5309f9d302ddf9b0 to your computer and use it in GitHub Desktop.
Rubyプロセスを追いかけるツール(番外編)

[C] perf

perf というのは performance analytics tool for Linux とかいうやつでコレを使うと cpu の何クロック使ったとか、L1 cache ミスヒット率といったような、よりハードウェアレイヤーに近いプロファイリング、Kernel レベルのプロファイリングができる。

CentOS なら yum install perf で入る。

perf top コマンドを使うと、イベントが発生した割合を top コマンド的に逐次的に更新して表示してくれる。

$ sudo perf top -p 23975
 11.25%  ruby                [.] vm_exec_core
  7.55%  [kernel]            [k] find_busiest_group
  5.38%  libpthread-2.12.so  [.] pthread_cond_timedwait@@GLIBC_2.3.2
  5.20%  [kernel]            [k] futex_wake
  4.83%  ruby                [.] st_lookup
  4.21%  libc-2.12.so        [.] malloc
  3.90%  [vdso]              [.] 0x0000000000000947
  3.81%  ruby                [.] vm_call_method
  3.81%  ruby                [.] rb_class_real

perf stat で統計情報を見れる。出力はこんなかんじになる。

$ sudo perf stat -p <pid>
^C
 Performance counter stats for process id '<pid>':

         14.360480 task-clock:HG             #    0.003 CPUs utilized
               512 context-switches:HG       #    0.036 M/sec
                34 cpu-migrations:HG         #    0.002 M/sec
                 0 page-faults:HG            #    0.000 K/sec
        38,955,251 cycles:HG                 #    2.713 GHz                     [78.37%]
        32,426,705 stalled-cycles-frontend:HG #   83.24% frontend cycles idle    [87.30%]
        29,652,612 stalled-cycles-backend:HG #   76.12% backend  cycles idle
        11,525,088 instructions:HG           #    0.30  insns per cycle
                                             #    2.81  stalled cycles per insn
         2,262,629 branches:HG               #  157.559 M/sec
           229,015 branch-misses:HG          #   10.12% of all branches         [39.99%]

       5.426184183 seconds time elapsed

計測中に何クロック使った(task-clock)かとか、コンテキストスイッチが何回発生したかとかがわかる。

記録しておいてあとで見たい場合には、perf record と perf report を使える。表示は perf top とそう変わらないので省く。

$ sudo perf record -p <pid>
^C
$ sudo perf report -i perf.data

他にも perf annotate でアセンブラレベルで追いかけたりできるようだ。こちらのページがとても参考になる => http://int.main.jp/txt/perf/

うーん、でも大体 vm_exec_core になっちゃうから ruby だとあまり役にたたないのか?

@sonots
Copy link
Author

sonots commented Aug 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment