xref: /aosp_15_r20/external/aws-crt-java/codebuild/CanaryWrapper_MetricFunctions.py (revision 3c7ae9de214676c52d19f01067dc1a404272dc11)
1# Contains all of the metric reporting functions for the Canary Wrappers
2
3# Needs to be installed prior to running
4import psutil
5
6
7cache_cpu_psutil_process = None
8def 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 ("Exception: " + str(e), flush=True)
24        return None
25
26# Note: This value is in BYTES.
27def 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 ("Exception: " + str(e), flush=True)
36        return None
37
38
39def 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 ("Exception: " + str(e), flush=True)
48        return None
49