1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# |
3
|
|
|
# This file is part of Glances. |
4
|
|
|
# |
5
|
|
|
# Copyright (C) 2019 Nicolargo <[email protected]> |
6
|
|
|
# |
7
|
|
|
# Glances is free software; you can redistribute it and/or modify |
8
|
|
|
# it under the terms of the GNU Lesser General Public License as published by |
9
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
# (at your option) any later version. |
11
|
|
|
# |
12
|
|
|
# Glances is distributed in the hope that it will be useful, |
13
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
# GNU Lesser General Public License for more details. |
16
|
|
|
# |
17
|
|
|
# You should have received a copy of the GNU Lesser General Public License |
18
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
|
20
|
|
|
"""CPU percent stats shared between CPU and Quicklook plugins.""" |
21
|
|
|
|
22
|
|
|
from glances.timer import Timer |
|
|
|
|
23
|
|
|
|
24
|
|
|
import psutil |
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class CpuPercent(object): |
28
|
|
|
|
29
|
|
|
"""Get and store the CPU percent.""" |
30
|
|
|
|
31
|
|
|
def __init__(self, cached_time=1): |
32
|
|
|
self.cpu_percent = 0 |
33
|
|
|
self.percpu_percent = [] |
34
|
|
|
|
35
|
|
|
# cached_time is the minimum time interval between stats updates |
36
|
|
|
# since last update is passed (will retrieve old cached info instead) |
37
|
|
|
self.timer_cpu = Timer(0) |
38
|
|
|
self.timer_percpu = Timer(0) |
39
|
|
|
self.cached_time = cached_time |
40
|
|
|
|
41
|
|
|
def get_key(self): |
|
|
|
|
42
|
|
|
"""Return the key of the per CPU list.""" |
43
|
|
|
return 'cpu_number' |
44
|
|
|
|
45
|
|
|
def get(self, percpu=False): |
46
|
|
|
"""Update and/or return the CPU using the psutil library. |
47
|
|
|
If percpu, return the percpu stats""" |
48
|
|
|
if percpu: |
|
|
|
|
49
|
|
|
return self.__get_percpu() |
50
|
|
|
else: |
51
|
|
|
return self.__get_cpu() |
52
|
|
|
|
53
|
|
|
def __get_cpu(self): |
54
|
|
|
"""Update and/or return the CPU using the psutil library.""" |
55
|
|
|
# Never update more than 1 time per cached_time |
56
|
|
|
if self.timer_cpu.finished(): |
57
|
|
|
self.cpu_percent = psutil.cpu_percent(interval=0.0) |
58
|
|
|
# Reset timer for cache |
59
|
|
|
self.timer_cpu = Timer(self.cached_time) |
60
|
|
|
return self.cpu_percent |
61
|
|
|
|
62
|
|
|
def __get_percpu(self): |
63
|
|
|
"""Update and/or return the per CPU list using the psutil library.""" |
64
|
|
|
# Never update more than 1 time per cached_time |
65
|
|
|
if self.timer_percpu.finished(): |
66
|
|
|
self.percpu_percent = [] |
67
|
|
|
for cpu_number, cputimes in enumerate(psutil.cpu_times_percent(interval=0.0, percpu=True)): |
|
|
|
|
68
|
|
|
cpu = {'key': self.get_key(), |
69
|
|
|
'cpu_number': cpu_number, |
70
|
|
|
'total': round(100 - cputimes.idle, 1), |
|
|
|
|
71
|
|
|
'user': cputimes.user, |
72
|
|
|
'system': cputimes.system, |
73
|
|
|
'idle': cputimes.idle} |
74
|
|
|
# The following stats are for API purposes only |
75
|
|
|
if hasattr(cputimes, 'nice'): |
76
|
|
|
cpu['nice'] = cputimes.nice |
77
|
|
|
if hasattr(cputimes, 'iowait'): |
78
|
|
|
cpu['iowait'] = cputimes.iowait |
79
|
|
|
if hasattr(cputimes, 'irq'): |
80
|
|
|
cpu['irq'] = cputimes.irq |
81
|
|
|
if hasattr(cputimes, 'softirq'): |
82
|
|
|
cpu['softirq'] = cputimes.softirq |
83
|
|
|
if hasattr(cputimes, 'steal'): |
84
|
|
|
cpu['steal'] = cputimes.steal |
85
|
|
|
if hasattr(cputimes, 'guest'): |
86
|
|
|
cpu['guest'] = cputimes.guest |
87
|
|
|
if hasattr(cputimes, 'guest_nice'): |
88
|
|
|
cpu['guest_nice'] = cputimes.guest_nice |
89
|
|
|
# Append new CPU to the list |
90
|
|
|
self.percpu_percent.append(cpu) |
91
|
|
|
# Reset timer for cache |
92
|
|
|
self.timer_percpu = Timer(self.cached_time) |
93
|
|
|
return self.percpu_percent |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
# CpuPercent instance shared between plugins |
97
|
|
|
cpu_percent = CpuPercent() |
|
|
|
|
98
|
|
|
|