straceを使ってみました。
- インストールとか
まず環境はこんな感じ。
$ uname -a
Linux pavement1234 3.13.0-165-generic #215-Ubuntu SMP Wed Jan 16 11:46:47 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
インストールはこう。
$sudo apt-get install strace
- -c -- count time, calls, and errors for each syscall and report summary
各システムコールのレポートが表示されます。これは超便利。
$ strace -c ./test
test
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
24.28 0.000486 69 7 mmap
15.98 0.000320 80 4 mprotect
10.34 0.000207 207 1 brk
9.34 0.000187 62 3 3 access
8.34 0.000167 167 1 munmap
8.19 0.000164 55 3 fstat
6.14 0.000123 62 2 open
5.94 0.000119 119 1 execve
5.54 0.000111 111 1 write
2.90 0.000058 29 2 close
2.40 0.000048 48 1 arch_prctl
0.60 0.000012 12 1 read
------ ----------- ----------- --------- --------- ----------------
100.00 0.002002 27 3 total
- -r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs
-rは相対時間(マイクロ秒)。個々の処理時間がわかるのでボトルネックを探しやすそう。
$ strace -r ./test
0.000000 execve("./test", ["./test"], [/* 22 vars */]) = 0
0.002786 brk(0) = 0xf88000
0.000495 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
0.000579 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)…
-tは絶対時間(秒)。秒だとちょっと粗すぎる。
$ strace -t ./test
23:10:20 execve("./test", ["./test"], [/* 22 vars */]) = 0
23:10:20 brk(0) = 0x1ba2000
23:10:20 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
23:10:20 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)…
-ttは絶対時間(マイクロ秒)。これも便利。
$ strace -tt ./test
23:10:39.434994 execve("./test", ["./test"], [/* 22 vars */]) = 0
23:10:39.436785 brk(0) = 0x2364000
23:10:39.437300 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
23:10:39.437792 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)…
- -o file -- send trace output to FILE instead of stderr
straceログをファイルに出力。
$ strace -o test.log ./test
- -p pid -- trace process with process id PID, may be repeated
プロセスにアタッチしてシステムコールを監視できます。。Ctrl+Cでデタッチ。
$ ps
PID TTY TIME CMD
5412 pts/1 00:00:04 bash
7140 pts/1 00:00:00 ps$ sudo strace -p 5412
Process 5412 attached
wait4(-1,^CProcess 5412 detached
<detached ...>

スポンサードリンク