Rate This Document
Findability
Accuracy
Completeness
Readability

Hotspot Function Analysis

Command Function

Uses ptrace to sample Python programs and Python & C/C++ hybrid programs, analyzes call stacks, obtains top 20 hotspot functions, and draws flame graphs.

  • The supported Python versions are 3.7.X to 3.11.X.
  • The supported OS is openEuler.
  • Only the PID mode is supported for collection and analysis.

Syntax

1
devkit py-perf hotspot [-h] [-l {0,1,2,3}] [-d <sec>] [-i <msec>] [-p {PID}] [-o <file>] [-t] [--native] [--nolineno]

Parameter Description

Table 1 Parameter description

Parameter

Option

Description

-h/--help

-

Obtains help information. This parameter is optional.

-l/--log-level

0/1/2/3

Log level, which defaults to 1. This parameter is optional.
  • 0: DEBUG
  • 1: INFO
  • 2: WARNING
  • 3: ERROR

-d/--duration

-

Collection duration, in seconds. The minimum value is 1 second. By default collection never ends. You can press Ctrl+\ to cancel the task or press Ctrl+C to stop the collection and start analysis. This parameter is optional.

-i/--interval

-

Collection interval, in milliseconds. The value ranges from 1 to 1000 and the default value is 10. This parameter is optional.

-p/--pid

PID

PID of the process to be collected. Only a single process can be collected. If the process to be collected has subprocesses, the subprocesses are not collected. This parameter is optional.

-o/--output

-

Report file name. By default, the report file is generated in the current directory. The default file name format is FlameGraph-YMD-HMS. This parameter is optional.

-t/--threads

-

Sets the report and flame graph to display call stack information by thread group, facilitating performance distribution analysis in multi-thread scenarios. This parameter is optional.

--native

-

Indicates whether to collect Python and C call stacks. If this parameter is used, Python and C call stacks are collected. Otherwise, only Python call stacks are collected. This parameter is optional.

--nolineno

-

Indicates that the report and flame graph do not display line numbers. This parameter is optional.

Example

1
devkit py-perf hotspot -p 2082596 -d 5 -i 100 -o /home/demo/flamegraph -t --native
  • The -p 2082596 parameter collects information about the process whose ID is 2082596. The -d 5 parameter indicates that the collection duration is 5 seconds. The -i 100 parameter indicates that the collection interval is 100 milliseconds. The -o /home/demo/flamegraph parameter indicates that the flamegraph.html file is generated in /home/demo/. The -t parameter indicates that the report and flame graph display information per thread. The --native parameter indicates the Python and C call stack information is collected.
  • The command output contains too much stack information, most of which has been omitted for easy reading. For the detailed information, see the actual command output.

Command output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Press Ctrl+ \ to cancel the task or Ctrl+ C to stop the task collection and enter the analysis.
Python Hotspot Top20 Summary Report                     Time:2024/11/18 10:39:07
================================================================================
────────────────────────────────────────────────────────────────────
  Function                                                                                                  Runtime(ms)    Runtime(%)
────────────────────────────────────────────────────────────────────
  Test::_calculate(int, double)                                                                                   4814         95.96
      Test::_calculate(int, double) (/home/workspace/pyhotspot_demo/simple-demo/test.cpp:18)
      Test::calculate(int, double, char*, int*, double*, char**) (/home/workspace/pyhotspot_demo/simple-demo/test.cpp:36)
      my_calculate (/home/workspace/pyhotspot_demo/simple-demo/test.cpp:47)
      ffi_prep_go_closure
      ffi_closure_free
      _ctypes_callproc (/home/Python-3.11.2/Modules/_ctypes/callproc.c:943)
      PyCFuncPtr_call (/home/Python-3.11.2/Modules/_ctypes/_ctypes.c:4212)
      _PyObject_MakeTpCall (/home/Python-3.11.2/./Include/internal/pycore_ceval.h:123)
      calculate (/home/demo/pyhotspot_demo/simple-demo/test.py:20)
      <module> (/home/demo/pyhotspot_demo/simple-demo/test.py:67)
      PyEval_EvalCode (/home/Python-3.11.2/./Include/internal/pycore_ceval.h:73)
      run_mod (/home/Python-3.11.2/Python/pythonrun.c:1714)
      _PyRun_SimpleFileObject (/home/Python-3.11.2/Python/pythonrun.c:1630)
      _PyRun_AnyFileObject (/home/Python-3.11.2/Python/pythonrun.c:79)
      pymain_run_python.constprop.0 (/home/Python-3.11.2/Modules/main.c:361)
      Py_BytesMain (/home/Python-3.11.2/Modules/main.c:682)
      __libc_init_first
      __libc_start_main
      _start
  Test::_calculate(int, double)                                                                                   202          4.04
      Test::_calculate(int, double) (/home/workspace/pyhotspot_demo/simple-demo/test.cpp:19)
      Test::calculate(int, double, char*, int*, double*, char**) (/home/workspace/pyhotspot_demo/simple-demo/test.cpp:36)
      my_calculate (/home/workspace/pyhotspot_demo/simple-demo/test.cpp:47)
      ffi_prep_go_closure
      ffi_closure_free
      _ctypes_callproc (/home/Python-3.11.2/Modules/_ctypes/callproc.c:943)
      PyCFuncPtr_call (/home/Python-3.11.2/Modules/_ctypes/_ctypes.c:4212)
      _PyObject_MakeTpCall (/home/Python-3.11.2/./Include/internal/pycore_ceval.h:123)
      calculate (/home/demo/pyhotspot_demo/simple-demo/test.py:20)
      <module> (/home/demo/pyhotspot_demo/simple-demo/test.py:67)
      PyEval_EvalCode (/home/Python-3.11.2/./Include/internal/pycore_ceval.h:73)
      run_mod (/home/Python-3.11.2/Python/pythonrun.c:1714)
      _PyRun_SimpleFileObject (/home/Python-3.11.2/Python/pythonrun.c:1630)
      _PyRun_AnyFileObject (/home/Python-3.11.2/Python/pythonrun.c:79)
      pymain_run_python.constprop.0 (/home/Python-3.11.2/Modules/main.c:361)
      Py_BytesMain (/home/Python-3.11.2/Modules/main.c:682)
      __libc_init_first
      __libc_start_main
      _start
────────────────────────────────────────────────────────────────────
The flamegraph html report: /home/demo/flamegraph.html

You can use a browser to view the HTML file of the flame graph. Because the stack name is too long, you can move the mouse pointer over a stack block to view its information. You can also click a stack block to view its details.

Figure 1 Flame graph