mirror of
https://github.com/powerline/powerline.git
synced 2025-07-21 21:05:11 +02:00
Merge remote-tracking branch 'zyx-i/threaded-cpu_load_percent' into develop
This commit is contained in:
commit
0c7cc25a02
@ -560,16 +560,27 @@ try:
|
|||||||
def _get_user(segment_info):
|
def _get_user(segment_info):
|
||||||
return psutil.Process(os.getpid()).username
|
return psutil.Process(os.getpid()).username
|
||||||
|
|
||||||
def cpu_load_percent(pl, measure_interval=.5):
|
class CPULoadPercentSegment(ThreadedSegment):
|
||||||
'''Return the average CPU load as a percentage.
|
interval = 1
|
||||||
|
|
||||||
Requires the ``psutil`` module.
|
def update(self, old_cpu):
|
||||||
|
return psutil.cpu_percent(interval=None)
|
||||||
|
|
||||||
:param float measure_interval:
|
def run(self):
|
||||||
interval used to measure CPU load (in seconds)
|
while not self.shutdown_event.is_set():
|
||||||
'''
|
try:
|
||||||
cpu_percent = int(psutil.cpu_percent(interval=measure_interval))
|
self.update_value = psutil.cpu_percent(interval=self.interval)
|
||||||
return '{0}%'.format(cpu_percent)
|
except Exception as e:
|
||||||
|
self.exception('Exception while calculating cpu_percent: {0}', str(e))
|
||||||
|
|
||||||
|
def render(self, cpu_percent, format='{0:.0f}%', **kwargs):
|
||||||
|
if not cpu_percent:
|
||||||
|
return None
|
||||||
|
return [{
|
||||||
|
'contents': format.format(cpu_percent),
|
||||||
|
'gradient_level': cpu_percent,
|
||||||
|
'highlight_group': ['cpu_load_percent_gradient', 'cpu_load_percent'],
|
||||||
|
}]
|
||||||
except ImportError:
|
except ImportError:
|
||||||
def _get_bytes(interface): # NOQA
|
def _get_bytes(interface): # NOQA
|
||||||
with open('/sys/class/net/{interface}/statistics/rx_bytes'.format(interface=interface), 'rb') as file_obj:
|
with open('/sys/class/net/{interface}/statistics/rx_bytes'.format(interface=interface), 'rb') as file_obj:
|
||||||
@ -587,18 +598,39 @@ except ImportError:
|
|||||||
def _get_user(segment_info): # NOQA
|
def _get_user(segment_info): # NOQA
|
||||||
return segment_info['environ'].get('USER', None)
|
return segment_info['environ'].get('USER', None)
|
||||||
|
|
||||||
def cpu_load_percent(pl, measure_interval=.5): # NOQA
|
class CPULoadPercentSegment(ThreadedSegment): # NOQA
|
||||||
'''Return the average CPU load as a percentage.
|
interval = 1
|
||||||
|
|
||||||
Requires the ``psutil`` module.
|
@staticmethod
|
||||||
|
def startup(**kwargs):
|
||||||
|
pass
|
||||||
|
|
||||||
:param float measure_interval:
|
@staticmethod
|
||||||
interval used to measure CPU load (in seconds)
|
def start():
|
||||||
'''
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def shutdown():
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def render(cpu_percent, pl, format='{0:.0f}%', **kwargs):
|
||||||
pl.warn('psutil package is not installed, thus CPU load is not available')
|
pl.warn('psutil package is not installed, thus CPU load is not available')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
cpu_load_percent = with_docstring(CPULoadPercentSegment(),
|
||||||
|
'''Return the average CPU load as a percentage.
|
||||||
|
|
||||||
|
Requires the ``psutil`` module.
|
||||||
|
|
||||||
|
:param str format:
|
||||||
|
Output format. Accepts measured CPU load as the first argument.
|
||||||
|
|
||||||
|
Highlight groups used: ``cpu_load_percent_gradient`` (gradient) or ``cpu_load_percent``.
|
||||||
|
''')
|
||||||
|
|
||||||
|
|
||||||
username = False
|
username = False
|
||||||
# os.geteuid is not available on windows
|
# os.geteuid is not available on windows
|
||||||
_geteuid = getattr(os, 'geteuid', lambda: 1)
|
_geteuid = getattr(os, 'geteuid', lambda: 1)
|
||||||
|
@ -244,7 +244,16 @@ class TestCommon(TestCase):
|
|||||||
def test_cpu_load_percent(self):
|
def test_cpu_load_percent(self):
|
||||||
pl = Pl()
|
pl = Pl()
|
||||||
with replace_module_module(common, 'psutil', cpu_percent=lambda **kwargs: 52.3):
|
with replace_module_module(common, 'psutil', cpu_percent=lambda **kwargs: 52.3):
|
||||||
self.assertEqual(common.cpu_load_percent(pl=pl), '52%')
|
self.assertEqual(common.cpu_load_percent(pl=pl), [{
|
||||||
|
'contents': '52%',
|
||||||
|
'gradient_level': 52.3,
|
||||||
|
'highlight_group': ['cpu_load_percent_gradient', 'cpu_load_percent'],
|
||||||
|
}])
|
||||||
|
self.assertEqual(common.cpu_load_percent(pl=pl, format='{0:.1f}%'), [{
|
||||||
|
'contents': '52.3%',
|
||||||
|
'gradient_level': 52.3,
|
||||||
|
'highlight_group': ['cpu_load_percent_gradient', 'cpu_load_percent'],
|
||||||
|
}])
|
||||||
|
|
||||||
def test_network_load(self):
|
def test_network_load(self):
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
Loading…
x
Reference in New Issue
Block a user