1010import subprocess
1111import time
1212
13+
1314from redisbench_admin .profilers .pprof import (
1415 PPROF_FORMAT_TEXT ,
1516 run_pprof ,
1920from redisbench_admin .utils .utils import whereis
2021
2122
23+ PERF_CALLGRAPH_MODE_DEFAULT = "lbr"
24+ LINUX_PERF_SETTINGS_MESSAGE = (
25+ "If running in non-root user please confirm that you have:\n "
26+ + " - access to Kernel address maps."
27+ + " Check if `0` ( disabled ) appears from the output of `cat /proc/sys/kernel/kptr_restrict`\n "
28+ + ' If not then fix via: `sudo sh -c " echo 0 > /proc/sys/kernel/kptr_restrict"`\n '
29+ + " - permission to collect stats."
30+ + " Check if `-1` appears from the output of `cat /proc/sys/kernel/perf_event_paranoid`\n "
31+ + " If not then fix via: `sudo sh -c 'echo -1 > /proc/sys/kernel/perf_event_paranoid'`\n "
32+ )
33+
34+
2235class Perf :
2336 def __init__ (self ):
2437 """
@@ -29,13 +42,11 @@ def __init__(self):
2942 if not self .perf :
3043 self .perf = "perf"
3144
32- self .stack_collapser = os .getenv ("STACKCOLLAPSE_PATH" )
33- if not self .stack_collapser :
34- self .stack_collapser = STACKCOLLAPSE_PATH
35-
36- self .flamegraph_utity = os .getenv ("FLAMEGRAPH_PATH" )
37- if not self .flamegraph_utity :
38- self .flamegraph_utity = FLAMEGRAPH_PATH
45+ self .stack_collapser = os .getenv ("STACKCOLLAPSE_PATH" , STACKCOLLAPSE_PATH )
46+ self .flamegraph_utity = os .getenv ("FLAMEGRAPH_PATH" , FLAMEGRAPH_PATH )
47+ self .callgraph_mode = os .getenv (
48+ "PERF_CALLGRAPH_MODE" , PERF_CALLGRAPH_MODE_DEFAULT
49+ )
3950
4051 self .output = None
4152 self .profiler_process = None
@@ -44,6 +55,7 @@ def __init__(self):
4455 self .profiler_process_exit_code = None
4556 self .trace_file = None
4657 self .collapsed_stack_file = None
58+ self .stack_collapse_file = None
4759 self .collapsed_stacks = []
4860 self .pid = None
4961 self .started_profile = False
@@ -71,7 +83,7 @@ def retrieve_perf_version(self):
7183 self .version_minor = m .group (2 )
7284 return m , self .version_major , self .version_minor
7385
74- def generate_record_command (self , pid , output , frequency = None , call_graph = "lbr" ):
86+ def generate_record_command (self , pid , output , frequency = None ):
7587 self .output = output
7688 self .pid = pid
7789 cmd = [
@@ -85,7 +97,7 @@ def generate_record_command(self, pid, output, frequency=None, call_graph="lbr")
8597 "--output" ,
8698 output ,
8799 "--call-graph" ,
88- call_graph ,
100+ self . callgraph_mode ,
89101 ]
90102 if frequency :
91103 cmd += ["--freq" , "{}" .format (frequency )]
@@ -172,13 +184,7 @@ def stop_profile(self, **kwargs):
172184 "Profiler process exit with error. Exit code: {}\n \n " .format (
173185 self .profiler_process_exit_code
174186 )
175- + "If running in non-root user please confirm that you have:\n "
176- + " - access to Kernel address maps."
177- + " Check if `0` ( disabled ) appears from the output of `cat /proc/sys/kernel/kptr_restrict`\n "
178- + ' If not then fix via: `sudo sh -c " echo 0 > /proc/sys/kernel/kptr_restrict"`\n '
179- + " - permission to collect stats."
180- + " Check if `-1` appears from the output of `cat /proc/sys/kernel/perf_event_paranoid`\n "
181- + " If not then fix via: `sudo sh -c 'echo -1 > /proc/sys/kernel/perf_event_paranoid'`\n "
187+ + LINUX_PERF_SETTINGS_MESSAGE
182188 )
183189
184190 except OSError as e :
0 commit comments