@@ -20,14 +20,14 @@ def __init__(self, environment, clock=time.time):
2020 self .memory_limit_bytes = environment ['memory_limit_bytes' ]
2121
2222 def should_stop_sampling (self , profile = None ):
23- return self .killswitch .is_killswitch_on () \
24- or self .cpu_usage_check .is_sampling_cpu_usage_limit_reached (profile ) \
25- or self ._is_memory_limit_reached (profile )
23+ return ( self .killswitch .is_killswitch_on ()
24+ or self .cpu_usage_check .is_sampling_cpu_usage_limit_reached (profile )
25+ or self ._is_memory_limit_reached (profile ) )
2626
2727 def should_stop_profiling (self , profile = None ):
28- return self .killswitch .is_killswitch_on () \
29- or self .cpu_usage_check .is_overall_cpu_usage_limit_reached (profile ) \
30- or self ._is_memory_limit_reached (profile )
28+ return ( self .killswitch .is_killswitch_on ()
29+ or self .cpu_usage_check .is_overall_cpu_usage_limit_reached (profile )
30+ or self ._is_memory_limit_reached (profile ) )
3131
3232 def _is_memory_limit_reached (self , profile ):
3333 return False if profile is None else profile .get_memory_usage_bytes () > self .memory_limit_bytes
@@ -42,21 +42,25 @@ class CpuUsageCheck:
4242 def __init__ (self , timer ):
4343 self .timer = timer
4444
45- # This function carries out an overall cpu limit check that covers the cpu overhead caused for the full
46- # sampling cycle: sample -> aggregate -> report -> refresh config. We expect this function to be called after
47- # configuration refresh and profile submission.
4845 def is_overall_cpu_usage_limit_reached (self , profile = None ):
46+ """
47+ This function carries out an overall cpu limit check that covers the cpu overhead caused for the full
48+ sampling cycle: refresh config -> (sample -> aggregate) * n -> profile submission. We expect this function to
49+ be called after configuration refresh and profile submission.
50+ """
4951 profiler_metric = self .timer .metrics .get ("runProfiler" )
5052 if not profile or not profiler_metric or profiler_metric .counter < MINIMUM_MEASURES_IN_DURATION_METRICS :
5153 return False
5254
5355 used_time_percentage = 100 * profiler_metric .total / (profile .get_active_millis_since_start ()/ 1000 )
5456
55- if used_time_percentage >= AgentConfiguration .get ().cpu_limit_percentage :
57+ cpu_limit_percentage = AgentConfiguration .get ().cpu_limit_percentage
58+
59+ if used_time_percentage >= cpu_limit_percentage :
5660 logger .debug (self .timer .metrics )
5761 logger .info (
5862 "Profiler overall cpu usage limit reached: {:.2f} % (limit: {:.2f} %), will stop CodeGuru Profiler."
59- .format (used_time_percentage , AgentConfiguration . get (). cpu_limit_percentage ))
63+ .format (used_time_percentage , cpu_limit_percentage ))
6064 return True
6165 else :
6266 return False
@@ -70,11 +74,13 @@ def is_sampling_cpu_usage_limit_reached(self, profile=None):
7074 sampling_interval_seconds = self ._get_average_sampling_interval_seconds (profile )
7175 used_time_percentage = 100 * sample_and_aggregate_metric .average () / sampling_interval_seconds
7276
73- if used_time_percentage >= AgentConfiguration .get ().cpu_limit_percentage :
77+ cpu_limit_percentage = AgentConfiguration .get ().cpu_limit_percentage
78+
79+ if used_time_percentage >= cpu_limit_percentage :
7480 logger .debug (self .timer .metrics )
7581 logger .info (
7682 "Profiler sampling cpu usage limit reached: {:.2f} % (limit: {:.2f} %), will stop CodeGuru Profiler."
77- .format (used_time_percentage , AgentConfiguration . get (). cpu_limit_percentage ))
83+ .format (used_time_percentage , cpu_limit_percentage ))
7884 return True
7985 else :
8086 return False
0 commit comments