DTraceとSystemTapを使用したCPythonのインストルメンテーション—Pythonドキュメント
DTraceとSystemTapを使用したCPythonのインストルメンテーション
- 著者
- デビッドマルコム
- 著者
- ŁukaszLanga
DTraceとSystemTapは監視ツールであり、それぞれがコンピューターシステム上のプロセスが実行していることを検査する方法を提供します。 どちらもドメイン固有言語を使用しているため、ユーザーは次のようなスクリプトを作成できます。
- どのプロセスを監視するかをフィルタリングします
- 関心のあるプロセスからデータを収集する
- データに関するレポートを生成する
Python 3.6以降、CPythonは、DTraceまたはSystemTapスクリプトで監視できる「プローブ」とも呼ばれる埋め込み「マーカー」を使用して構築できるため、システム上のCPythonプロセスの実行内容を簡単に監視できます。
静的マーカーの有効化
macOSには、DTraceのサポートが組み込まれています。 Linuxでは、SystemTapのマーカーが埋め込まれたCPythonを構築するには、SystemTap開発ツールをインストールする必要があります。
Linuxマシンでは、これは次の方法で実行できます。
$ yum install systemtap-sdt-devel
また:
$ sudo apt-get install systemtap-sdt-dev
次に、CPythonを構成する必要があります--with-dtrace
。
checking for --with-dtrace... yes
macOSでは、バックグラウンドでPythonプロセスを実行し、Pythonプロバイダーによって利用可能にされたすべてのプローブを一覧表示することで、利用可能なDTraceプローブを一覧表示できます。
$ python3.6 -q &
$ sudo dtrace -l -P python$! # or: dtrace -l -m python3.6
ID PROVIDER MODULE FUNCTION NAME
29564 python18035 python3.6 _PyEval_EvalFrameDefault function-entry
29565 python18035 python3.6 dtrace_function_entry function-entry
29566 python18035 python3.6 _PyEval_EvalFrameDefault function-return
29567 python18035 python3.6 dtrace_function_return function-return
29568 python18035 python3.6 collect gc-done
29569 python18035 python3.6 collect gc-start
29570 python18035 python3.6 _PyEval_EvalFrameDefault line
29571 python18035 python3.6 maybe_dtrace_line line
Linuxでは、「。note.stapsdt」セクションが含まれているかどうかを確認することで、ビルドされたバイナリにSystemTap静的マーカーが存在するかどうかを確認できます。
$ readelf -S ./python | grep .note.stapsdt
[30] .note.stapsdt NOTE 0000000000000000 00308d78
Pythonを共有ライブラリとして(–enable-sharedを使用して)構築した場合は、代わりに共有ライブラリ内を調べる必要があります。 例えば:
$ readelf -S libpython3.3dm.so.1.0 | grep .note.stapsdt
[29] .note.stapsdt NOTE 0000000000000000 00365b68
十分に現代的なreadelfはメタデータを印刷できます:
$ readelf -n ./python
Displaying notes found at file offset 0x00000254 with length 0x00000020:
Owner Data size Description
GNU 0x00000010 NT_GNU_ABI_TAG (ABI version tag)
OS: Linux, ABI: 2.6.32
Displaying notes found at file offset 0x00000274 with length 0x00000024:
Owner Data size Description
GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring)
Build ID: df924a2b08a7e89f6e11251d4602022977af2670
Displaying notes found at file offset 0x002d6c30 with length 0x00000144:
Owner Data size Description
stapsdt 0x00000031 NT_STAPSDT (SystemTap probe descriptors)
Provider: python
Name: gc__start
Location: 0x00000000004371c3, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf6
Arguments: -4@%ebx
stapsdt 0x00000030 NT_STAPSDT (SystemTap probe descriptors)
Provider: python
Name: gc__done
Location: 0x00000000004374e1, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf8
Arguments: -8@%rax
stapsdt 0x00000045 NT_STAPSDT (SystemTap probe descriptors)
Provider: python
Name: function__entry
Location: 0x000000000053db6c, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6be8
Arguments: 8@%rbp 8@%r12 -4@%eax
stapsdt 0x00000046 NT_STAPSDT (SystemTap probe descriptors)
Provider: python
Name: function__return
Location: 0x000000000053dba8, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bea
Arguments: 8@%rbp 8@%r12 -4@%eax
上記のメタデータには、SystemTapスクリプトで使用されるトレースフックを有効にするために、戦略的に配置されたマシンコード命令にパッチを適用する方法を説明するSystemTapの情報が含まれています。
静的DTraceプローブ
次のDTraceスクリプトの例を使用して、Pythonスクリプトの呼び出し/戻り階層を表示できます。これは、「start」と呼ばれる関数の呼び出し内でのみトレースします。 言い換えると、インポート時の関数呼び出しはリストされません。
self int indent;
python$target:::function-entry
/copyinstr(arg1) == "start"/
{
self->trace = 1;
}
python$target:::function-entry
/self->trace/
{
printf("%d\t%*s:", timestamp, 15, probename);
printf("%*s", self->indent, "");
printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
self->indent++;
}
python$target:::function-return
/self->trace/
{
self->indent--;
printf("%d\t%*s:", timestamp, 15, probename);
printf("%*s", self->indent, "");
printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
}
python$target:::function-return
/copyinstr(arg1) == "start"/
{
self->trace = 0;
}
次のように呼び出すことができます。
$ sudo dtrace -q -s call_stack.d -c "python3.6 script.py"
出力は次のようになります。
156641360502280 function-entry:call_stack.py:start:23
156641360518804 function-entry: call_stack.py:function_1:1
156641360532797 function-entry: call_stack.py:function_3:9
156641360546807 function-return: call_stack.py:function_3:10
156641360563367 function-return: call_stack.py:function_1:2
156641360578365 function-entry: call_stack.py:function_2:5
156641360591757 function-entry: call_stack.py:function_1:1
156641360605556 function-entry: call_stack.py:function_3:9
156641360617482 function-return: call_stack.py:function_3:10
156641360629814 function-return: call_stack.py:function_1:2
156641360642285 function-return: call_stack.py:function_2:6
156641360656770 function-entry: call_stack.py:function_3:9
156641360669707 function-return: call_stack.py:function_3:10
156641360687853 function-entry: call_stack.py:function_4:13
156641360700719 function-return: call_stack.py:function_4:14
156641360719640 function-entry: call_stack.py:function_5:18
156641360732567 function-return: call_stack.py:function_5:21
156641360747370 function-return:call_stack.py:start:28
静的SystemTapマーカー
SystemTap統合を使用する低レベルの方法は、静的マーカーを直接使用することです。 これには、それらを含むバイナリファイルを明示的に指定する必要があります。
たとえば、このSystemTapスクリプトを使用して、Pythonスクリプトの呼び出し/戻り階層を表示できます。
probe process("python").mark("function__entry") {
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
printf("%s => %s in %s:%d\\n",
thread_indent(1), funcname, filename, lineno);
}
probe process("python").mark("function__return") {
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
printf("%s <= %s in %s:%d\\n",
thread_indent(-1), funcname, filename, lineno);
}
次のように呼び出すことができます。
$ stap \
show-call-hierarchy.stp \
-c "./python test.py"
出力は次のようになります。
11408 python(8274): => __contains__ in Lib/_abcoll.py:362
11414 python(8274): => __getitem__ in Lib/os.py:425
11418 python(8274): => encode in Lib/os.py:490
11424 python(8274): <= encode in Lib/os.py:493
11428 python(8274): <= __getitem__ in Lib/os.py:426
11433 python(8274): <= __contains__ in Lib/_abcoll.py:366
列は次のとおりです。
- スクリプトの開始からのマイクロ秒単位の時間
- 実行可能ファイルの名前
- プロセスのPID
残りは、スクリプトの実行時の呼び出し/戻り階層を示します。
CPythonの –enable-shared ビルドの場合、マーカーはlibpython共有ライブラリ内に含まれており、プローブの点線のパスはこれを反映する必要があります。 たとえば、上記の例の次の行は次のとおりです。
probe process("python").mark("function__entry") {
代わりに読む必要があります:
probe process("python").library("libpython3.6dm.so.1.0").mark("function__entry") {
(CPython 3.6のデバッグビルドを想定)
利用可能な静的マーカー
- function__entry(str filename, str funcname, int lineno)
このマーカーは、Python関数の実行が開始されたことを示します。 純粋なPython(バイトコード)関数に対してのみトリガーされます。
ファイル名、関数名、および行番号は、位置引数としてトレーススクリプトに返されます。これらの引数には、
$arg1
、$arg2
、$arg3
を使用してアクセスする必要があります。$arg1
:(const char *)
ファイル名、user_string($arg1)
を使用してアクセス可能$arg2
:(const char *)
関数名、user_string($arg2)
を使用してアクセス可能$arg3
:int
行番号
- function__return(str filename, str funcname, int lineno)
このマーカーは
function__entry()
の逆であり、Python関数の実行が終了したことを示します(return
または例外のいずれかを介して)。 純粋なPython(バイトコード)関数に対してのみトリガーされます。引数は
function__entry()
の場合と同じです。
- line(str filename, str funcname, int lineno)
このマーカーは、Python行が実行されようとしていることを示します。 これは、Pythonプロファイラーを使用した行ごとのトレースに相当します。 C関数内ではトリガーされません。
引数は
function__entry()
と同じです。
- gc__start(int generation)
- Pythonインタープリターがガベージコレクションサイクルを開始すると発生します。
arg0
は、 gc.collect()のように、スキャンする世代です。
- gc__done(long collected)
- Pythonインタープリターがガベージコレクションサイクルを終了すると発生します。
arg0
は、収集されたオブジェクトの数です。
- import__find__load__start(str modulename)
importlib がモジュールを見つけてロードしようとする前に発生します。
arg0
はモジュール名です。バージョン3.7の新機能。
- import__find__load__done(str modulename, int found)
importlib のfind_and_load関数が呼び出された後に発生します。
arg0
はモジュール名、arg1
はモジュールが正常にロードされたかどうかを示します。バージョン3.7の新機能。
- audit(str event, void *tuple)
sys.audit()または PySys_Audit()が呼び出されたときに発生します。
arg0
はC文字列としてのイベント名、arg1
はタプルオブジェクトへの PyObject ポインタです。バージョン3.8の新機能。
SystemTapタップセット
SystemTap統合を使用する高レベルの方法は、「タップセット」を使用することです。これは、静的マーカーの低レベルの詳細の一部を非表示にする、SystemTapのライブラリに相当します。
これは、CPythonの非共有ビルドに基づくタップセットファイルです。
/*
Provide a higher-level wrapping around the function__entry and
function__return markers:
\*/
probe python.function.entry = process("python").mark("function__entry")
{
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
frameptr = $arg4
}
probe python.function.return = process("python").mark("function__return")
{
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
frameptr = $arg4
}
このファイルがSystemTapのtapsetディレクトリにインストールされている場合(例: /usr/share/systemtap/tapset
)、次の追加のプローブポイントが使用可能になります。
- python.function.entry(str filename, str funcname, int lineno, frameptr)
- このプローブポイントは、Python関数の実行が開始されたことを示します。 純粋なPython(バイトコード)関数に対してのみトリガーされます。
- python.function.return(str filename, str funcname, int lineno, frameptr)
- このプローブポイントは
python.function.return
の逆であり、Python関数の実行が終了したことを示します(return
または例外のいずれかを介して)。 純粋なPython(バイトコード)関数に対してのみトリガーされます。
例
このSystemTapスクリプトは、上記のタップセットを使用して、静的マーカーに直接名前を付ける必要なしに、Python関数呼び出し階層をトレースする上記の例をよりクリーンに実装します。
probe python.function.entry
{
printf("%s => %s in %s:%d\n",
thread_indent(1), funcname, filename, lineno);
}
probe python.function.return
{
printf("%s <= %s in %s:%d\n",
thread_indent(-1), funcname, filename, lineno);
}
次のスクリプトは、上記のタップセットを使用して、実行中のすべてのCPythonコードの上部のようなビューを提供し、システム全体で毎秒、最も頻繁に入力される上位20のバイトコードフレームを表示します。
global fn_calls;
probe python.function.entry
{
fn_calls[pid(), filename, funcname, lineno] += 1;
}
probe timer.ms(1000) {
printf("\033[2J\033[1;1H") /* clear screen \*/
printf("%6s %80s %6s %30s %6s\n",
"PID", "FILENAME", "LINE", "FUNCTION", "CALLS")
foreach ([pid, filename, funcname, lineno] in fn_calls- limit 20) {
printf("%6d %80s %6d %30s %6d\n",
pid, filename, lineno, funcname,
fn_calls[pid, filename, funcname, lineno]);
}
delete fn_calls;
}