简体   繁体   中英

Determining time for CPU Utilization

I am interested in finding out how long CPU usage of my system stayed at 70% or higher. My sample data looks like below. The complete data is here

Time                    CPUDemandPercentage
2019-03-06 03:55:00     40.17
2019-03-06 14:15:00     77.33
2019-03-06 14:20:00     79.66

To achieve what I want I have explored following things. I was trying to:

  • determine peak location
  • determine peak width
import numpy as np
import matplotlib.pyplot as plt 
import scipy.signal
from pandas import read_csv
data=read_csv('data.csv',header=0,usecols=["CPUDemandPercentage"])
y = np.array(data['CPUDemandPercentage'])
indexes = scipy.signal.find_peaks_cwt(y, np.arange(1, 4))
plt.plot(indexes, y[indexes], "xr"); plt.plot(y); plt.legend(['Peaks'])
plt.show()

This gives me a graph like 峰

  • It is not very accurate, negative peaks are not shown. how can I increase the accuracy here.
  • Also how I find the width of the peaks.

I am out of clue here. Can someone help me.

Not a pandas based solution below. The idea is to look at the previous and current cpu levels and to increment the counter if they both "high enough"

import csv

# Assuming delta time between rows is 5 minutes

DELTA_T = 5


def get_cpu_time_above_pct(pct):
    time_above_pct = 0
    previous_cpu_level = None
    with open('cpu.csv', 'rb') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            current_cpu_level = float(row[1])
            if previous_cpu_level is not None and
               current_cpu_level >= pct and
               previous_cpu_level >= pct:
                   time_above_pct += DELTA_T
            previous_cpu_level = current_cpu_level

    return time_above_pct


print('CPU Time above 70\% : {} minutes'.format(get_cpu_time_above_pct(70)))

Another answer full pandas: this solution is generic, no need to have same timedelta betwwen measures

df['Time']=df['Time'].apply((lambda x: pd.to_datetime(x)))
df['TimeDelta'] = df['Time'].shift(-1) - df['Time']
filter = df['CPUDemandPercentage'] >= 70.0
df['changes'] = [(x,y) for x,y in zip(filter , filter.shift(-1))]
result  = df[df['changes']==(True,True)]['TimeDelta'].sum()

print(f'TimeCPU>=70%: {result} or {result.total_seconds()/60} minutes')

output:

TimeCPU>70%: 0 days 03:10:00 or 190.0 minutes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM