@@ -20,14 +20,14 @@ def __init__(self, environment, clock=time.time):
20
20
self .memory_limit_bytes = environment ['memory_limit_bytes' ]
21
21
22
22
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 ) )
26
26
27
27
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 ) )
31
31
32
32
def _is_memory_limit_reached (self , profile ):
33
33
return False if profile is None else profile .get_memory_usage_bytes () > self .memory_limit_bytes
@@ -42,21 +42,25 @@ class CpuUsageCheck:
42
42
def __init__ (self , timer ):
43
43
self .timer = timer
44
44
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.
48
45
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
+ """
49
51
profiler_metric = self .timer .metrics .get ("runProfiler" )
50
52
if not profile or not profiler_metric or profiler_metric .counter < MINIMUM_MEASURES_IN_DURATION_METRICS :
51
53
return False
52
54
53
55
used_time_percentage = 100 * profiler_metric .total / (profile .get_active_millis_since_start ()/ 1000 )
54
56
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 :
56
60
logger .debug (self .timer .metrics )
57
61
logger .info (
58
62
"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 ))
60
64
return True
61
65
else :
62
66
return False
@@ -70,11 +74,13 @@ def is_sampling_cpu_usage_limit_reached(self, profile=None):
70
74
sampling_interval_seconds = self ._get_average_sampling_interval_seconds (profile )
71
75
used_time_percentage = 100 * sample_and_aggregate_metric .average () / sampling_interval_seconds
72
76
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 :
74
80
logger .debug (self .timer .metrics )
75
81
logger .info (
76
82
"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 ))
78
84
return True
79
85
else :
80
86
return False
0 commit comments