17
17
class CpuPercent :
18
18
"""Get and store the CPU percent."""
19
19
20
- def __init__ (self , cached_timer_cpu = 3 ):
21
- self .cpu_info = {'cpu_name' : None , 'cpu_hz_current' : None , 'cpu_hz' : None }
22
- self .cpu_percent = 0
23
- self .percpu_percent = []
24
-
25
- # Get CPU name
26
- self .cpu_info ['cpu_name' ] = self .__get_cpu_name ()
27
-
20
+ def __init__ (self , cached_timer_cpu = 2 ):
28
21
# cached_timer_cpu is the minimum time interval between stats updates
29
22
# since last update is passed (will retrieve old cached info instead)
30
23
self .cached_timer_cpu = cached_timer_cpu
31
- self .timer_cpu = Timer (0 )
32
- self .timer_percpu = Timer (0 )
33
-
34
24
# psutil.cpu_freq() consumes lots of CPU
35
- # So refresh the stats every refresh*2 (6 seconds)
25
+ # So refresh CPU frequency stats every refresh * 2
36
26
self .cached_timer_cpu_info = cached_timer_cpu * 2
27
+
28
+ # Get CPU name
37
29
self .timer_cpu_info = Timer (0 )
30
+ self .cpu_info = {'cpu_name' : self .__get_cpu_name (), 'cpu_hz_current' : None , 'cpu_hz' : None }
31
+
32
+ # Warning from PsUtil documentation
33
+ # The first time this function is called with interval = 0.0 or None
34
+ # it will return a meaningless 0.0 value which you are supposed to ignore.
35
+ self .timer_cpu = Timer (0 )
36
+ self .cpu_percent = self .get_cpu ()
37
+ self .timer_percpu = Timer (0 )
38
+ self .percpu_percent = self .get_percpu ()
38
39
39
40
def get_key (self ):
40
41
"""Return the key of the per CPU list."""
41
42
return 'cpu_number'
42
43
43
- def get (self , percpu = False ):
44
- """Update and/or return the CPU using the psutil library.
45
- If percpu, return the percpu stats"""
46
- if percpu :
47
- return self .__get_percpu ()
48
- return self .__get_cpu ()
49
-
50
44
def get_info (self ):
51
45
"""Get additional information about the CPU"""
52
46
# Never update more than 1 time per cached_timer_cpu_info
@@ -84,21 +78,26 @@ def __get_cpu_name(self):
84
78
break
85
79
return ret if ret else 'CPU'
86
80
87
- def __get_cpu (self ):
81
+ def get_cpu (self ):
88
82
"""Update and/or return the CPU using the psutil library."""
89
83
# Never update more than 1 time per cached_timer_cpu
90
84
if self .timer_cpu .finished ():
91
- self .cpu_percent = psutil .cpu_percent (interval = 0.0 )
92
85
# Reset timer for cache
93
86
self .timer_cpu .reset (duration = self .cached_timer_cpu )
87
+ # Update the stats
88
+ self .cpu_percent = psutil .cpu_percent (interval = 0.0 )
94
89
return self .cpu_percent
95
90
96
- def __get_percpu (self ):
91
+ def get_percpu (self ):
97
92
"""Update and/or return the per CPU list using the psutil library."""
98
93
# Never update more than 1 time per cached_timer_cpu
99
94
if self .timer_percpu .finished ():
100
- self .percpu_percent = []
101
- for cpu_number , cputimes in enumerate (psutil .cpu_times_percent (interval = 0.0 , percpu = True )):
95
+ # Reset timer for cache
96
+ self .timer_percpu .reset (duration = self .cached_timer_cpu )
97
+ # Get stats
98
+ percpu_percent = []
99
+ psutil_percpu = enumerate (psutil .cpu_times_percent (interval = 0.0 , percpu = True ))
100
+ for cpu_number , cputimes in psutil_percpu :
102
101
cpu = {
103
102
'key' : self .get_key (),
104
103
'cpu_number' : cpu_number ,
@@ -123,9 +122,9 @@ def __get_percpu(self):
123
122
if hasattr (cputimes , 'guest_nice' ):
124
123
cpu ['guest_nice' ] = cputimes .guest_nice
125
124
# Append new CPU to the list
126
- self . percpu_percent .append (cpu )
127
- # Reset timer for cache
128
- self .timer_percpu . reset ( duration = self . cached_timer_cpu )
125
+ percpu_percent .append (cpu )
126
+ # Update stats
127
+ self .percpu_percent = percpu_percent
129
128
return self .percpu_percent
130
129
131
130
0 commit comments