-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu_tools.py
More file actions
39 lines (25 loc) · 1014 Bytes
/
Copy pathcpu_tools.py
File metadata and controls
39 lines (25 loc) · 1014 Bytes
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
from __future__ import division
def get_jiffies():
""" This is for Linux only"""
with open("/proc/stat") as f:
while(True):
line = f.readline().rstrip()
if line.startswith('cpu'):
user, nice, system, idle, iowait, irq, softirq = [int(x) for x in line[5:].split(' ')][:7]
return user, nice, system, idle, iowait, irq, softirq
class CpuMeter(object):
def __init__(self):
self.start()
def start(self):
all_jiffies = get_jiffies()
self.start_total_jiffies = sum(all_jiffies)
self.start_work_jiffies = sum(all_jiffies[:3])
def finish(self):
all_jiffies = get_jiffies()
finish_total_jiffies = sum(all_jiffies)
finish_work_jiffies = sum(all_jiffies[:3])
try:
return (finish_work_jiffies-self.start_work_jiffies) / float(finish_total_jiffies-self.start_total_jiffies)
except ZeroDivisionError:
# whatever
return 1.0