Skip to content

Commit eebd769

Browse files
committed
perCPU and CPU consumption display time to time a total of 100% nicolargo#2849
1 parent 2ee3c86 commit eebd769

File tree

6 files changed

+60
-48
lines changed

6 files changed

+60
-48
lines changed

glances/cpu_percent.py

+26-27
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,30 @@
1717
class CpuPercent:
1818
"""Get and store the CPU percent."""
1919

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):
2821
# cached_timer_cpu is the minimum time interval between stats updates
2922
# since last update is passed (will retrieve old cached info instead)
3023
self.cached_timer_cpu = cached_timer_cpu
31-
self.timer_cpu = Timer(0)
32-
self.timer_percpu = Timer(0)
33-
3424
# 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
3626
self.cached_timer_cpu_info = cached_timer_cpu * 2
27+
28+
# Get CPU name
3729
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()
3839

3940
def get_key(self):
4041
"""Return the key of the per CPU list."""
4142
return 'cpu_number'
4243

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-
5044
def get_info(self):
5145
"""Get additional information about the CPU"""
5246
# Never update more than 1 time per cached_timer_cpu_info
@@ -84,21 +78,26 @@ def __get_cpu_name(self):
8478
break
8579
return ret if ret else 'CPU'
8680

87-
def __get_cpu(self):
81+
def get_cpu(self):
8882
"""Update and/or return the CPU using the psutil library."""
8983
# Never update more than 1 time per cached_timer_cpu
9084
if self.timer_cpu.finished():
91-
self.cpu_percent = psutil.cpu_percent(interval=0.0)
9285
# Reset timer for cache
9386
self.timer_cpu.reset(duration=self.cached_timer_cpu)
87+
# Update the stats
88+
self.cpu_percent = psutil.cpu_percent(interval=0.0)
9489
return self.cpu_percent
9590

96-
def __get_percpu(self):
91+
def get_percpu(self):
9792
"""Update and/or return the per CPU list using the psutil library."""
9893
# Never update more than 1 time per cached_timer_cpu
9994
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:
102101
cpu = {
103102
'key': self.get_key(),
104103
'cpu_number': cpu_number,
@@ -123,9 +122,9 @@ def __get_percpu(self):
123122
if hasattr(cputimes, 'guest_nice'):
124123
cpu['guest_nice'] = cputimes.guest_nice
125124
# 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
129128
return self.percpu_percent
130129

131130

glances/plugins/cpu/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ def update(self):
165165
stats = self.update_local()
166166
elif self.input_method == 'snmp':
167167
stats = self.update_snmp()
168-
else:
169-
stats = self.get_init_value()
170168

171169
# Update the stats
172170
self.stats = stats
@@ -185,7 +183,7 @@ def update_local(self):
185183
# Init new stats
186184
stats = self.get_init_value()
187185

188-
stats['total'] = cpu_percent.get()
186+
stats['total'] = cpu_percent.get_cpu()
189187

190188
# Standards stats
191189
# - user: time spent by normal processes executing in user mode; on Linux this also includes guest time

glances/plugins/percpu/__init__.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,12 @@ def get_key(self):
120120
@GlancesPluginModel._log_result_decorator
121121
def update(self):
122122
"""Update per-CPU stats using the input method."""
123-
# Init new stats
124-
stats = self.get_init_value()
125-
126-
# Grab per-CPU stats using psutil's cpu_percent(percpu=True) and
127-
# cpu_times_percent(percpu=True) methods
123+
# Grab per-CPU stats using psutil's
128124
if self.input_method == 'local':
129-
stats = cpu_percent.get(percpu=True)
125+
stats = cpu_percent.get_percpu()
130126
else:
131127
# Update stats using SNMP
132-
pass
128+
stats = self.get_init_value()
133129

134130
# Update the stats
135131
self.stats = stats

glances/plugins/quicklook/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ def update(self):
118118

119119
# Get the CPU percent value (global and per core)
120120
# Stats is shared across all plugins
121-
stats['cpu'] = cpu_percent.get()
122-
stats['percpu'] = cpu_percent.get(percpu=True)
121+
stats['cpu'] = cpu_percent.get_cpu()
122+
stats['percpu'] = cpu_percent.get_percpu()
123123

124124
# Get the virtual and swap memory
125125
stats['mem'] = psutil.virtual_memory().percent

glances/stats.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -260,19 +260,13 @@ def __update_plugin(self, p):
260260
self._plugins[p].update_views()
261261

262262
def update(self):
263-
"""Wrapper method to update the stats.
263+
"""Wrapper method to update all stats.
264264
265265
Only called by standalone and server modes
266266
"""
267-
threads = []
268267
# Start update of all enable plugins
269-
for p in self.getPluginsList():
270-
thread = threading.Thread(target=self.__update_plugin, args=(p,))
271-
thread.start()
272-
threads.append(thread)
273-
# Wait the end of the update
274-
for t in threads:
275-
t.join()
268+
for p in self.getPluginsList(enable=True):
269+
self.__update_plugin(p)
276270

277271
def export(self, input_stats=None):
278272
"""Export all the stats.

test-data/issues/issue2849.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sys
2+
import time
3+
4+
sys.path.insert(0, '../glances')
5+
6+
###########
7+
8+
# from glances.cpu_percent import cpu_percent
9+
10+
# for _ in range(0, 5):
11+
# print([i['total'] for i in cpu_percent.get_percpu()])
12+
# time.sleep(2)
13+
14+
###########
15+
16+
from glances.main import GlancesMain
17+
from glances.stats import GlancesStats
18+
19+
core = GlancesMain()
20+
stats = GlancesStats(config=core.get_config(), args=core.get_args())
21+
22+
for _ in range(0, 5):
23+
stats.update()
24+
print([i['total'] for i in stats.get_plugin('percpu').get_raw()])
25+
time.sleep(2)

0 commit comments

Comments
 (0)