Browse Source

Caching the cpu and memory percentages.

This ensures the cpu value is available and we're using the same numbers
that were used for the addition.
pull/10/head
Egon Rijpkema 3 years ago
parent
commit
4463b42195
  1. 17
      roles/kill_memory_hogs/files/kill_hoggs.py

17
roles/kill_memory_hogs/files/kill_hoggs.py

@ -87,22 +87,23 @@ def kill_hogs(memory_threshold, @@ -87,22 +87,23 @@ def kill_hogs(memory_threshold,
try:
# First call t0 cpu_percent() without blocking interval is meaningless.
# see https://psutil.readthedocs.io/en/latest/
proc_cpu_percent = proc.cpu_percent()
proc.cached_cpu_percent = proc.cpu_percent()
proc.cached_memory_percent = proc.memory_percent()
except Exception as e:
logging.exception(e)
proc_cpu_percent = 0
if proc.uids().real == 0 or (proc.memory_percent() < .1
and proc_cpu_percent < 1):
if proc.uids().real == 0 or (proc.cached_memory_percent < .1
and proc.cached_cpu_percent < 1):
continue # do not kill root processes.
# Check username here. It is somewhat expensive.
username = proc.username()
if not ((username[0] in ('s', 'p', 'f') and username[1:].isdigit())
or username[:5] == 'umcg-'):
continue # we only kill processes of p, s and f accounts.
continue # we only kill processes of p, s and f accounts.a
users[username]['memory_percent'] += proc.memory_percent()
users[username]['cpu_percent'] += proc_cpu_percent
users[username]['memory_percent'] += proc.cached_memory_percent
users[username]['cpu_percent'] += proc.cached_cpu_percent
users[username]['processes'].append(proc)
@ -118,8 +119,8 @@ def kill_hogs(memory_threshold, @@ -118,8 +119,8 @@ def kill_hogs(memory_threshold,
for proc in data['processes']:
message.append('{} pid {} {} memory {:.2f}% cpu {:.2f}%'.format(
proc.username(), proc.pid, proc.name(),
proc.memory_percent(),
proc_cpu_percent))
proc.cached_memory_percent,
proc.cached_cpu_percent))
logging.info('\n'.join(message))
if slack:
post_to_slack('\n'.join(message))

Loading…
Cancel
Save