File: CanaryWrapper_MetricFunctions.py

package info (click to toggle)
aws-crt-python 0.24.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 75,932 kB
  • sloc: ansic: 418,984; python: 23,626; makefile: 6,035; sh: 4,075; ruby: 208; java: 82; perl: 73; cpp: 25; xml: 11
file content (48 lines) | stat: -rw-r--r-- 1,782 bytes parent folder | download | duplicates (4)
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 ("Exception: " + str(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 ("Exception: " + str(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 ("Exception: " + str(e), flush=True)
        return None