3

I have written a python program to detect faces of a video input (webcam) using Haar Cascade. I would like to know how much CPU, GPU and RAM are being utilized by this specific program and not the overall CPU, GPU, RAM usage.

I came across psutil package (https://pypi.org/project/psutil/) which enables to profile system resources being used but I could not manage to get system resources being used by a specific program. How can I achieve that?

I have also seen this How to get current CPU and RAM usage in Python? but its not what I want.

My python code as follows:

def main():
    #Get current Process ID
    pid = os.getpid()
    #Get resources used by current process
    p = psutil.Process(pid)
    with p.oneshot():
        cpu_percent = p.cpu_percent()
        # Get Memory and GPU usage of this PID as well
7
  • What OS is this on? Have you checked out the per-process information psutil gives you? Commented Jun 17, 2018 at 15:00
  • I am using MacOS at the moment but will be importing the program to Linux on an embedded system. Commented Jun 17, 2018 at 15:03
  • 1
    Getting GPU utilisation info on Linux is entirely tied to per-vendor command-line tools, psutil has no support for this. The rest is available via psutil.Process. Commented Jun 17, 2018 at 15:07
  • A good place to start is probably 26.4. The Python Profilers in the manual. Commented Jun 17, 2018 at 15:16
  • You could find the PID and look in activity monitor since you're on mac Commented Jun 17, 2018 at 15:22

1 Answer 1

1

You can get CPU/RAM metrics (not GPU) for a particular process given its PID:

import psutil
psutil.Process(1234).cpu_percent()
psutil.Process(1234).memory_info()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.