Skip to content

Instantly share code, notes, and snippets.

@ryu22e
Last active November 3, 2023 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryu22e/0f5f52712194e4e38c211958288e6267 to your computer and use it in GitHub Desktop.
Save ryu22e/0f5f52712194e4e38c211958288e6267 to your computer and use it in GitHub Desktop.
Linux perfのCPythonサポートサンプル
def foo(n):
result = 0
for _ in range(n):
result += 1
return result
def bar(n):
foo(n)
def baz(n):
bar(n)
if __name__ == "__main__":
baz(1000000)
$ sudo perf record -F 9999 -g -o perf.data python3.11 my_script.py
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.048 MB perf.data (237 samples) ]
$ sudo perf report --stdio -n -g  # C言語のPyEval_EvalFrameDefault関数しか表示されない
# To display the perf.data header info, please use --header/--header-only options.
#
#
# Total Lost Samples: 0
#
# Samples: 237  of event 'cpu-clock:pppH'
# Event count (approx.): 23702370
#
# Children      Self       Samples  Command     Shared Object          Symbol
# ........  ........  ............  ..........  .....................  ..........................................
#
    93.67%     0.00%             0  python3.11  python3.11             [.] _start
            |
            ---_start
               __libc_start_main
               0xffffa58073fc
               Py_BytesMain
               |
               |--71.31%--pymain_run_python.constprop.0
               |          |
               |           --70.89%--_PyRun_AnyFileObject
               |                     _PyRun_SimpleFileObject
               |                     run_mod
               |                     |
               |                      --70.46%--PyEval_EvalCode
               |                                |
               |                                |--63.29%--_PyEval_EvalFrameDefault
               |                                |          |
               |                                |          |--14.77%--PyLong_FromLong
               |                                |          |          |
               |                                |          |          |--5.06%--_PyObject_Malloc
               |                                |          |          |
               |                                |          |           --3.38%--_Py_NewReference
               |                                |          |
               |                                |          |--10.97%--_PyObject_Free
               |                                |          |
               |                                |          |--10.55%--_PyLong_Add
               |                                |          |          |
               |                                |          |           --4.22%--_PyObject_Malloc
               |                                |          |
               |                                |          |--2.53%--PyObject_Malloc
               |                                |          |
               |                                |           --1.27%--_PyObject_Malloc
               |                                |
               |                                |--4.64%--object_dealloc
               |                                |
               |                                |--1.27%--rangeiter_next
               |                                |
               |                                 --1.27%--PyObject_Free
(省略)
$ sudo perf record -F 9999 -g -o perf.data python3.12 -X perf my_script.py
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.078 MB perf.data (265 samples) ]
$ sudo perf report --stdio -n -g  # foo、bar、bazzなどの関数名が表示される
# To display the perf.data header info, please use --header/--header-only options.
#
#
# Total Lost Samples: 0
#
# Samples: 265  of event 'cpu-clock:pppH'
# Event count (approx.): 26502650
#
# Children      Self       Samples  Command     Shared Object          Symbol                                                       >
# ........  ........  ............  ..........  .....................  .............................................................>
#
    97.36%     0.00%             0  python3.12  python3.12             [.] _start
            |
            ---_start
               __libc_start_main
               |
                --96.98%--0xffffac6473fc
                          |
                           --96.60%--Py_BytesMain
                                     |
                                     |--75.47%--pymain_run_python.constprop.0
                                     |          |
                                     |           --75.09%--_PyRun_AnyFileObject
                                     |                     _PyRun_SimpleFileObject
                                     |                     |
                                     |                      --74.72%--run_mod
                                     |                                run_eval_code_obj
                                     |                                PyEval_EvalCode
                                     |                                py_trampoline_evaluator
                                     |                                py::<module>:/home/ubuntu/my_script.py
                                     |                                _PyEval_EvalFrameDefault
                                     |                                PyObject_Vectorcall
                                     |                                py_trampoline_evaluator
                                     |                                py::baz:/home/ubuntu/my_script.py
                                     |                                _PyEval_EvalFrameDefault
                                     |                                PyObject_Vectorcall
                                     |                                py_trampoline_evaluator
                                     |                                py::bar:/home/ubuntu/my_script.py
                                     |                                _PyEval_EvalFrameDefault
                                     |                                PyObject_Vectorcall
                                     |                                py_trampoline_evaluator
                                     |                                py::foo:/home/ubuntu/my_script.py
                                     |                                |
(省略)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment