Linuxメモ/Valgrind/その他のチェック
Tag: Linux Valgrind
緑文字が入力コマンド。
桃文字が今回の変更箇所。
メモリのリークチェックを行う。
--toolを省略するとデフォルトのmemcheckとなります。
リークチェック?では--tool=memcheckを省略しています。
$ valgrind --tool=memcheck --leak-check=full COMMAND[ENTER]
キャッシュミス率を測定する。
$ valgrind --tool=cachegrind COMMAND[ENTER]
まずはメモリ配置通りにアクセスしてキャッシュミスが少なくなりそうなプログラムで確認します。
以下のソースをエディタで作成します。
$ vi testc1.c[ENTER]
static char buf[4096][4096]; int main( void ) { int i, j; for ( i = 0; i < 4096; i++ ) { for ( j = 0; j < 4096; j++ ) { buf[i][j] = 'x'; } } return 0; }
普通にコンパイルします。
デバッグオプションは関係ないようです。
$ gcc -Wall -o testc1 testc1.c[ENTER]
valgrindを経由してtestc1を起動して表示を確認します。
$ valgrind --tool=cachegrind ./testc1[ENTER]
testc1実行結果
==11058== Cachegrind, an I1/D1/L2 cache profiler. ==11058== Copyright (C) 2002-2006, and GNU GPL'd, by Nicholas Nethercote et al. ==11058== Using LibVEX rev 1658, a library for dynamic binary translation. ==11058== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP. ==11058== Using valgrind-3.2.1, a dynamic binary instrumentation framework. ==11058== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al. ==11058== For more details, rerun with: -v ==11058== ==11058== ==11058== I refs: 184,658,101 ==11058== I1 misses: 554 ==11058== L2i misses: 553 ==11058== I1 miss rate: 0.00% ==11058== L2i miss rate: 0.00% ==11058== ==11058== D refs: 83,933,993 (67,143,736 rd + 16,790,257 wr) ==11058== D1 misses: 263,020 ( 662 rd + 262,358 wr) ==11058== L2d misses: 262,968 ( 614 rd + 262,354 wr) ==11058== D1 miss rate: 0.3% ( 0.0% + 1.5% ) ==11058== L2d miss rate: 0.3% ( 0.0% + 1.5% ) ==11058== ==11058== L2 refs: 263,574 ( 1,216 rd + 262,358 wr) ==11058== L2 misses: 263,521 ( 1,167 rd + 262,354 wr) ==11058== L2 miss rate: 0.0% ( 0.0% + 1.5% )
D1は0.3%、L2は0.0%という感じでキャッシュミスはほとんどないようです。
次にメモリ配置と違うアクセスをしてキャッシュミスが多くなりそうなプログラムで確認します。
以下のソースをエディタで作成します。
$ vi testc2.c[ENTER]
static char buf[4096][4096]; int main( void ) { int i, j; for ( i = 0; i < 4096; i++ ) { for ( j = 0; j < 4096; j++ ) { buf[j][i] = 'x'; } } return 0; }
普通にコンパイルします。
$ gcc -Wall -o testc1 testc2.c[ENTER]
valgrindを経由してtestc2を起動して表示を確認します。
$ valgrind --tool=cachegrind ./testc2[ENTER]
testc2実行結果
==11814== Cachegrind, an I1/D1/L2 cache profiler. ==11814== Copyright (C) 2002-2006, and GNU GPL'd, by Nicholas Nethercote et al. ==11814== Using LibVEX rev 1658, a library for dynamic binary translation. ==11814== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP. ==11814== Using valgrind-3.2.1, a dynamic binary instrumentation framework. ==11814== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al. ==11814== For more details, rerun with: -v ==11814== ==11814== ==11814== I refs: 184,658,101 ==11814== I1 misses: 554 ==11814== L2i misses: 553 ==11814== I1 miss rate: 0.00% ==11814== L2i miss rate: 0.00% ==11814== ==11814== D refs: 83,933,993 (67,143,736 rd + 16,790,257 wr) ==11814== D1 misses: 16,778,091 ( 662 rd + 16,777,429 wr) ==11814== L2d misses: 16,778,039 ( 614 rd + 16,777,425 wr) ==11814== D1 miss rate: 19.9% ( 0.0% + 99.9% ) ==11814== L2d miss rate: 19.9% ( 0.0% + 99.9% ) ==11814== ==11814== L2 refs: 16,778,645 ( 1,216 rd + 16,777,429 wr) ==11814== L2 misses: 16,778,592 ( 1,167 rd + 16,777,425 wr) ==11814== L2 miss rate: 6.2% ( 0.0% + 99.9% )
D1は19.9%、L2は6.2%という感じで予想通りキャッシュミスが増加しました。
cg_annotateというコマンドを用いるとキャッシュミスが起こっている箇所を特定する事が出来るらしい。
でも、ここまで考えてプログラムを作っている人は組み込み分野でも少ないでしょうね。
メモリの使用量を表示する。
$ valgrind --tool=massif COMMAND[ENTER]
リークチェック?で使用したtest1で確認します。
$ valgrind --tool=massif test1[ENTER]
test1 massif実行結果
==28871== Massif, a space profiler. ==28871== Copyright (C) 2003, Nicholas Nethercote ==28871== Using LibVEX rev 1658, a library for dynamic binary translation. ==28871== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP. ==28871== Using valgrind-3.2.1, a dynamic binary instrumentation framework. ==28871== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al. ==28871== For more details, rerun with: -v ==28871== ==28871== ==28871== Total spacetime: 113,271 ms.B ==28871== heap: 0.4% ==28871== heap admin: 0.3% ==28871== stack(s): 99.1%
ファイルディスクリプタの閉じ忘れを検出する。
$ valgrind --track-fds=yes COMMAND[ENTER]
子プロセスまでトレースする。
$ valgrind --trace-children=yes COMMAND[ENTER]
コメントはありません。 コメント/Linuxメモ/Valgrind/その他のチェック?
Online: 2