File: CanaryWrapper_MetricFunctions.py

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (48 lines) | stat: -rw-r--r-- 1,785 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Contains all of the metric reporting functions for the Canary Wrappers

# Needs to be installed prior to running
import psutil


cache_cpu_psutil_process = None
def get_metric_total_cpu_usage(psutil_process : psutil.Process):
    global cache_cpu_psutil_process

    try:
        if (psutil_process == None):
            print ("ERROR - No psutil.process passed! Cannot gather metric!", flush=True)
            return None
        # We always need to skip the first CPU poll
        if (cache_cpu_psutil_process != psutil_process):
            psutil.cpu_percent(interval=None)
            cache_cpu_psutil_process = psutil_process
            return None
        return psutil.cpu_percent(interval=None)
    except Exception as e:
        print ("ERROR - exception occurred gathering metrics!")
        print (f"Exception: {repr(e)}", flush=True)
        return None

# Note: This value is in BYTES.
def get_metric_total_memory_usage_value(psutil_process : psutil.Process):
    try:
        if (psutil_process == None):
            print ("ERROR - No psutil.process passed! Cannot gather metric!", flush=True)
            return None
        return psutil.virtual_memory()[3]
    except Exception as e:
        print ("ERROR - exception occurred gathering metrics!")
        print (f"Exception: {repr(e)}", flush=True)
        return None


def get_metric_total_memory_usage_percent(psutil_process : psutil.Process):
    try:
        if (psutil_process == None):
            print ("ERROR - No psutil.process passed! Cannot gather metric!", flush=True)
            return None
        return psutil.virtual_memory()[2]
    except Exception as e:
        print ("ERROR - exception occurred gathering metrics!")
        print (f"Exception: {repr(e)}", flush=True)
        return None