Debian Codegraph @

crt/aws-c-mqtt/codebuild/CanaryWrapper_MetricFunctions.py

raw symbols SCIP
1 # Contains all of the metric reporting functions for the Canary Wrappers
2
3 # Needs to be installed prior to running
4 import psutil
5
6
7 cache_cpu_psutil_process = None
8 def get_metric_total_cpu_usage(psutil_process : psutil.Process):
9 global cache_cpu_psutil_process
10
11 try:
12 if (psutil_process == None):
13 print ("ERROR - No psutil.process passed! Cannot gather metric!", flush=True)
14 return None
15 # We always need to skip the first CPU poll
16 if (cache_cpu_psutil_process != psutil_process):
17 psutil.cpu_percent(interval=None)
18 cache_cpu_psutil_process = psutil_process
19 return None
20 return psutil.cpu_percent(interval=None)
21 except Exception as e:
22 print ("ERROR - exception occurred gathering metrics!")
23 print (f"Exception: {repr(e)}", flush=True)
24 return None
25
26 # Note: This value is in BYTES.
27 def get_metric_total_memory_usage_value(psutil_process : psutil.Process):
28 try:
29 if (psutil_process == None):
30 print ("ERROR - No psutil.process passed! Cannot gather metric!", flush=True)
31 return None
32 return psutil.virtual_memory()[3]
33 except Exception as e:
34 print ("ERROR - exception occurred gathering metrics!")
35 print (f"Exception: {repr(e)}", flush=True)
36 return None
37
38
39 def get_metric_total_memory_usage_percent(psutil_process : psutil.Process):
40 try:
41 if (psutil_process == None):
42 print ("ERROR - No psutil.process passed! Cannot gather metric!", flush=True)
43 return None
44 return psutil.virtual_memory()[2]
45 except Exception as e:
46 print ("ERROR - exception occurred gathering metrics!")
47 print (f"Exception: {repr(e)}", flush=True)
48 return None