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
TheCoder
  • 87
  • 2
  • 11
  • What OS is this on? Have you checked out the [per-process information `psutil` gives you](http://psutil.readthedocs.io/en/latest/#process-class)? – Martijn Pieters Jun 17 '18 at 15:00
  • I am using MacOS at the moment but will be importing the program to Linux on an embedded system. – TheCoder Jun 17 '18 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. – Martijn Pieters Jun 17 '18 at 15:07
  • A good place to start is probably [26.4. The Python Profilers](https://docs.python.org/2/library/profile.html) in the manual. – jww Jun 17 '18 at 15:16
  • You could find the PID and look in activity monitor since you're on mac – whackamadoodle3000 Jun 17 '18 at 15:22
  • But using per-process information (psutil.Process()) I can only get CPU not the GPU or RAM usage or am I missing something? at least I couldnt find it on the documentation? – TheCoder Jun 17 '18 at 15:26
  • As I already stated: *Getting GPU utilisation info on Linux is entirely tied to per-vendor command-line tools, psutil has no support for this.* – Martijn Pieters Jun 17 '18 at 16:08

1 Answers1

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()
Giampaolo Rodolà
  • 12,488
  • 6
  • 68
  • 60