3838
3939from gprofiler import merge
4040from gprofiler .exceptions import CalledProcessError , CalledProcessTimeoutError , NoRwExecDirectoryFoundError
41- from gprofiler .gprofiler_types import ProcessToProfileData , ProfileData , StackToSampleCount , positive_integer
41+ from gprofiler .gprofiler_types import (
42+ ProcessToProfileData ,
43+ ProfileData ,
44+ StackToSampleCount ,
45+ integer_range ,
46+ positive_integer ,
47+ )
4248from gprofiler .kernel_messages import get_kernel_messages_provider
4349from gprofiler .log import get_logger_adapter
4450from gprofiler .metadata import application_identifiers
@@ -261,6 +267,8 @@ class AsyncProfiledProcess:
261267 _FDTRANSFER_TIMEOUT = 10
262268 _JATTACH_TIMEOUT = 30 # higher than jattach's timeout
263269
270+ _DEFAULT_MCACHE = 30 # arbitrarily chosen, not too high & not too low.
271+
264272 def __init__ (
265273 self ,
266274 process : Process ,
@@ -271,6 +279,7 @@ def __init__(
271279 ap_safemode : int ,
272280 ap_args : str ,
273281 jattach_timeout : int = _JATTACH_TIMEOUT ,
282+ mcache : int = 0 ,
274283 ):
275284 self .process = process
276285 self ._stop_event = stop_event
@@ -317,6 +326,7 @@ def __init__(
317326 self ._ap_safemode = ap_safemode
318327 self ._ap_args = ap_args
319328 self ._jattach_timeout = jattach_timeout
329+ self ._mcache = mcache
320330
321331 def _find_rw_exec_dir (self , available_dirs : Sequence [str ]) -> str :
322332 """
@@ -439,7 +449,7 @@ def _get_start_cmd(self, interval: int, ap_timeout: int) -> List[str]:
439449
440450 def _get_stop_cmd (self , with_output : bool ) -> List [str ]:
441451 return self ._get_base_cmd () + [
442- f"stop,log={ self ._log_path_process } "
452+ f"stop,log={ self ._log_path_process } ,mcache= { self . _mcache } "
443453 + (self ._get_ap_output_args () if with_output else "" )
444454 + self ._get_extra_ap_args ()
445455 ]
@@ -614,9 +624,8 @@ def parse_jvm_version(version_string: str) -> JvmVersion:
614624 ProfilerArgument (
615625 "--java-async-profiler-safemode" ,
616626 dest = "java_async_profiler_safemode" ,
617- type = int ,
618627 default = JAVA_ASYNC_PROFILER_DEFAULT_SAFEMODE ,
619- choices = range (0 , 128 ),
628+ type = integer_range (0 , 128 ),
620629 metavar = "[0-127]" ,
621630 help = "Controls the 'safemode' parameter passed to async-profiler. This is parameter denotes multiple"
622631 " bits that describe different stack recovery techniques which async-profiler uses (see StackRecovery"
@@ -645,6 +654,15 @@ def parse_jvm_version(version_string: str) -> JvmVersion:
645654 default = AsyncProfiledProcess ._JATTACH_TIMEOUT ,
646655 help = "Timeout for jattach operations (start/stop AP, etc)" ,
647656 ),
657+ ProfilerArgument (
658+ "--java-async-profiler-mcache" ,
659+ dest = "java_async_profiler_mcache" ,
660+ # this is "unsigned char" in AP's code
661+ type = integer_range (0 , 256 ),
662+ metavar = "[0-255]" ,
663+ default = AsyncProfiledProcess ._DEFAULT_MCACHE ,
664+ help = "async-profiler mcache option (defaults to %(default)s)" ,
665+ ),
648666 ],
649667)
650668class JavaProfiler (ProcessProfilerBase ):
@@ -679,6 +697,7 @@ def __init__(
679697 java_async_profiler_args : str ,
680698 java_safemode : str ,
681699 java_jattach_timeout : int ,
700+ java_async_profiler_mcache : int ,
682701 java_mode : str ,
683702 ):
684703 assert java_mode == "ap" , "Java profiler should not be initialized, wrong java_mode value given"
@@ -694,6 +713,7 @@ def __init__(
694713 self ._ap_safemode = java_async_profiler_safemode
695714 self ._ap_args = java_async_profiler_args
696715 self ._jattach_timeout = java_jattach_timeout
716+ self ._ap_mcache = java_async_profiler_mcache
697717 self ._init_java_safemode (java_safemode )
698718 self ._should_profile = True
699719 # if set, profiling is disabled due to this safemode reason.
@@ -894,6 +914,7 @@ def _profile_process_stackcollapse(self, process: Process) -> StackToSampleCount
894914 self ._ap_safemode ,
895915 self ._ap_args ,
896916 self ._jattach_timeout ,
917+ self ._ap_mcache ,
897918 ) as ap_proc :
898919 return self ._profile_ap_process (ap_proc , comm )
899920
0 commit comments