1
|
|
|
# |
2
|
|
|
# This file is part of Glances. |
3
|
|
|
# |
4
|
|
|
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]> |
5
|
|
|
# |
6
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only |
7
|
|
|
# |
8
|
|
|
|
9
|
|
|
"""CPU percent stats shared between CPU and Quicklook plugins.""" |
10
|
|
|
|
11
|
|
|
import psutil |
12
|
|
|
|
13
|
|
|
from glances.logger import logger |
14
|
|
|
from glances.timer import Timer |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class CpuPercent: |
18
|
|
|
"""Get and store the CPU percent.""" |
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
|
|
|
|
28
|
|
|
# cached_timer_cpu is the minimum time interval between stats updates |
29
|
|
|
# since last update is passed (will retrieve old cached info instead) |
30
|
|
|
self.cached_timer_cpu = cached_timer_cpu |
31
|
|
|
self.timer_cpu = Timer(0) |
32
|
|
|
self.timer_percpu = Timer(0) |
33
|
|
|
|
34
|
|
|
# psutil.cpu_freq() consumes lots of CPU |
35
|
|
|
# So refresh the stats every refresh*2 (6 seconds) |
36
|
|
|
self.cached_timer_cpu_info = cached_timer_cpu * 2 |
37
|
|
|
self.timer_cpu_info = Timer(0) |
38
|
|
|
|
39
|
|
|
def get_key(self): |
40
|
|
|
"""Return the key of the per CPU list.""" |
41
|
|
|
return 'cpu_number' |
42
|
|
|
|
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
|
|
|
def get_info(self): |
51
|
|
|
"""Get additional information about the CPU""" |
52
|
|
|
# Never update more than 1 time per cached_timer_cpu_info |
53
|
|
|
if self.timer_cpu_info.finished() and hasattr(psutil, 'cpu_freq'): |
54
|
|
|
# Get the CPU freq current/max |
55
|
|
|
try: |
56
|
|
|
cpu_freq = psutil.cpu_freq() |
57
|
|
|
except Exception as e: |
58
|
|
|
logger.debug(f'Can not grab CPU information ({e})') |
59
|
|
|
else: |
60
|
|
|
if hasattr(cpu_freq, 'current'): |
61
|
|
|
self.cpu_info['cpu_hz_current'] = cpu_freq.current |
62
|
|
|
else: |
63
|
|
|
self.cpu_info['cpu_hz_current'] = None |
64
|
|
|
if hasattr(cpu_freq, 'max'): |
65
|
|
|
self.cpu_info['cpu_hz'] = cpu_freq.max |
66
|
|
|
else: |
67
|
|
|
self.cpu_info['cpu_hz'] = None |
68
|
|
|
# Reset timer for cache |
69
|
|
|
self.timer_cpu_info.reset(duration=self.cached_timer_cpu_info) |
70
|
|
|
return self.cpu_info |
71
|
|
|
|
72
|
|
|
def __get_cpu_name(self): |
73
|
|
|
# Get the CPU name once from the /proc/cpuinfo file |
74
|
|
|
# Read the first line with the "model name" |
75
|
|
|
ret = None |
76
|
|
|
try: |
77
|
|
|
cpuinfo_file = open('/proc/cpuinfo').readlines() |
78
|
|
|
except (FileNotFoundError, PermissionError): |
79
|
|
|
pass |
80
|
|
|
else: |
81
|
|
|
for line in cpuinfo_file: |
82
|
|
|
if line.startswith('model name'): |
83
|
|
|
ret = line.split(':')[1].strip() |
84
|
|
|
break |
85
|
|
|
return ret if ret else 'CPU' |
86
|
|
|
|
87
|
|
|
def __get_cpu(self): |
88
|
|
|
"""Update and/or return the CPU using the psutil library.""" |
89
|
|
|
# Never update more than 1 time per cached_timer_cpu |
90
|
|
|
if self.timer_cpu.finished(): |
91
|
|
|
self.cpu_percent = psutil.cpu_percent(interval=0.0) |
92
|
|
|
# Reset timer for cache |
93
|
|
|
self.timer_cpu.reset(duration=self.cached_timer_cpu) |
94
|
|
|
return self.cpu_percent |
95
|
|
|
|
96
|
|
|
def __get_percpu(self): |
97
|
|
|
"""Update and/or return the per CPU list using the psutil library.""" |
98
|
|
|
# Never update more than 1 time per cached_timer_cpu |
99
|
|
|
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)): |
102
|
|
|
cpu = { |
103
|
|
|
'key': self.get_key(), |
104
|
|
|
'cpu_number': cpu_number, |
105
|
|
|
'total': round(100 - cputimes.idle, 1), |
106
|
|
|
'user': cputimes.user, |
107
|
|
|
'system': cputimes.system, |
108
|
|
|
'idle': cputimes.idle, |
109
|
|
|
} |
110
|
|
|
# The following stats are for API purposes only |
111
|
|
|
if hasattr(cputimes, 'nice'): |
112
|
|
|
cpu['nice'] = cputimes.nice |
113
|
|
|
if hasattr(cputimes, 'iowait'): |
114
|
|
|
cpu['iowait'] = cputimes.iowait |
115
|
|
|
if hasattr(cputimes, 'irq'): |
116
|
|
|
cpu['irq'] = cputimes.irq |
117
|
|
|
if hasattr(cputimes, 'softirq'): |
118
|
|
|
cpu['softirq'] = cputimes.softirq |
119
|
|
|
if hasattr(cputimes, 'steal'): |
120
|
|
|
cpu['steal'] = cputimes.steal |
121
|
|
|
if hasattr(cputimes, 'guest'): |
122
|
|
|
cpu['guest'] = cputimes.guest |
123
|
|
|
if hasattr(cputimes, 'guest_nice'): |
124
|
|
|
cpu['guest_nice'] = cputimes.guest_nice |
125
|
|
|
# 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) |
129
|
|
|
return self.percpu_percent |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
# CpuPercent instance shared between plugins |
133
|
|
|
cpu_percent = CpuPercent() |
134
|
|
|
|