0

So, I was creating a monitor function to monitor a process for benchmarking.

This is the function

def monitor(target):
    worker_process = mp.Process(target=target, args=(5, bounds, num_particles, max_iter, None))
    worker_process.start()
    p = psutil.Process(worker_process.pid)
    cpu_percents = []
    while worker_process.is_alive():
      test = p.cpu_percent()
      if test != 0.0:
         cpu_percents.append(test)

    worker_process.join()
    return cpu_percents
cpu_percents = monitor(target=GSO)

i got the cpu usage of the function that i was monitoring, but the cpu percent()/number of cpus was greater than 100, i don't understand what's going on can someone explain.

reason why i have divided by number of cpus is given in this post

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Souldiv
  • 145
  • 1
  • 5

2 Answers2

0

From psutil doc: http://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent

Return a float representing the process CPU utilization as a percentage which can also be > 100.0 in case of a process running multiple threads on different CPUs.

Giampaolo Rodolà
  • 12,488
  • 6
  • 68
  • 60
  • 2
    Yes, that's right. But the cpu_percent when divided by the number of cpus must have percent <= 100, please read the question carefully. – Souldiv Jul 29 '18 at 06:56
0

i had a similar issue with cpu ussage around 300% which makes no sense. The problem was solved by increasing the interval time. Here is an example of how i did it:

import psutil
import pandas as pd
import time
import multiprocessing


def get_running_aps(interval=20):
    df = pd.DataFrame(columns=['pid', 'name', 'username', 'status', 'cpu_percent'])

    # this is t0 (start of interval)
    for proc in psutil.process_iter(['pid', 'name', 'username', 'status', 'cpu_percent']):
        pass

    # interval time waiting
    for i in range(interval):
        print("#" * (interval - i))
        time.sleep(1)

    # measure a second time, now save the results
    for proc in psutil.process_iter(['pid', 'name', 'username', 'status', 'cpu_percent']):
        df = df.append(proc.info, ignore_index=True)
    
    # divide by the number of cpu's
    df.cpu_percent = df.cpu_percent/multiprocessing.cpu_count()

    df = df.sort_values(['cpu_percent'], ascending=False)
    return df


if __name__ == "__main__":
    df = get_running_aps()
    print(df.head())
WillemBoone
  • 93
  • 1
  • 6