fix psi-top

This commit is contained in:
Alexey Avramov 2019-05-12 18:07:37 +09:00
parent 2981ecd95c
commit 77da0efb9a
4 changed files with 183 additions and 167 deletions

View File

@ -1,51 +1,82 @@
#!/usr/bin/env python3
from time import sleep, time
import os
from sys import stdout
from ctypes import CDLL
from time import sleep
from sys import argv
mlockall = True
"""
Execute the command
find /sys/fs/cgroup -name memory.pressure
to find available memory.pressue files (except /proc/pressure/memory).
(actual for cgroup2)
"""
if mlockall:
from ctypes import CDLL
CDLL('libc.so.6').mlockall(3)
if len(argv) > 1:
psi_path = argv[1]
else:
psi_path = '/proc/pressure/memory'
psi_path = '/proc/pressure/memory'
psi_support = os.path.exists(psi_path)
def mlockall():
def rline1(path):
"""read 1st line from path."""
with open(path) as f:
for line in f:
return line[:-1]
MCL_CURRENT = 1
MCL_FUTURE = 2
MCL_ONFAULT = 4
def psi_mem_some_avg_total():
return float(rline1(psi_path).rpartition('=')[2])
libc = CDLL('libc.so.6', use_errno=True)
avg_min_time = 1
result = libc.mlockall(
MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT
)
if result != 0:
result = libc.mlockall(
MCL_CURRENT | MCL_FUTURE
)
if result != 0:
print('WARNING: cannot lock all memory')
else:
pass
else:
pass
if psi_support:
ta0 = time()
a0 = psi_mem_some_avg_total()
mlockall()
def psi_path_to_metrics(psi_path):
with open(psi_path) as f:
psi_list = f.readlines()
# print(psi_list)
some_list, full_list = psi_list[0].split(' '), psi_list[1].split(' ')
#print(some_list, full_list)
some_avg10 = some_list[1].split('=')[1]
some_avg60 = some_list[2].split('=')[1]
some_avg300 = some_list[3].split('=')[1]
full_avg10 = full_list[1].split('=')[1]
full_avg60 = full_list[2].split('=')[1]
full_avg300 = full_list[3].split('=')[1]
return (some_avg10, some_avg60, some_avg300,
full_avg10, full_avg60, full_avg300)
print('Path to PSI file: {}\n'.format(psi_path))
print(' avg10 avg60 avg300 avg10 avg60 avg300')
while True:
(some_avg10, some_avg60, some_avg300,
full_avg10, full_avg60, full_avg300) = psi_path_to_metrics(psi_path)
if psi_support:
print('some {} {} {} | full {} {} {}'.format(
some_avg10.rjust(6),
some_avg60.rjust(6),
some_avg300.rjust(6),
full_avg10.rjust(6),
full_avg60.rjust(6),
full_avg300.rjust(6)))
ta1= time()
dt = ta1 - ta0
if dt >= avg_min_time:
a1 = psi_mem_some_avg_total()
avg = (a1 - a0) / (ta1 - ta0) / 10000
print('avg time:', round(dt, 1))
print('PSI mem avg:', round(avg, 2))
print(rline1(psi_path), '\n')
ta0 = ta1
a0 = a1
stdout.flush()
sleep(0.1)
sleep(2)

51
trash/psi-monitor-old Executable file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env python3
from time import sleep, time
import os
from sys import stdout
mlockall = True
if mlockall:
from ctypes import CDLL
CDLL('libc.so.6').mlockall(3)
psi_path = '/proc/pressure/memory'
psi_support = os.path.exists(psi_path)
def rline1(path):
"""read 1st line from path."""
with open(path) as f:
for line in f:
return line[:-1]
def psi_mem_some_avg_total():
return float(rline1(psi_path).rpartition('=')[2])
avg_min_time = 1
if psi_support:
ta0 = time()
a0 = psi_mem_some_avg_total()
while True:
if psi_support:
ta1= time()
dt = ta1 - ta0
if dt >= avg_min_time:
a1 = psi_mem_some_avg_total()
avg = (a1 - a0) / (ta1 - ta0) / 10000
print('avg time:', round(dt, 1))
print('PSI mem avg:', round(avg, 2))
print(rline1(psi_path), '\n')
ta0 = ta1
a0 = a1
stdout.flush()
sleep(0.1)

View File

@ -1,8 +1,8 @@
#!/usr/bin/env python3
from ctypes import CDLL
from time import sleep
from sys import argv
from time import sleep, time
import os
"""
Execute the command
@ -11,11 +11,7 @@ from sys import argv
(actual for cgroup2)
"""
if len(argv) > 1:
psi_path = argv[1]
else:
psi_path = '/proc/pressure/memory'
psi_path = '/proc/pressure/memory'
def mlockall():
@ -42,6 +38,7 @@ def mlockall():
mlockall()
t0 = time()
def psi_path_to_metrics(psi_path):
@ -62,21 +59,75 @@ def psi_path_to_metrics(psi_path):
full_avg10, full_avg60, full_avg300)
print('Path to PSI file: {}\n'.format(psi_path))
def cgroup2_root():
"""
"""
with open('/proc/mounts') as f:
for line in f:
if ' cgroup2 ' in line:
# if line.startswith('cgroup2 '):
return line[7:].rpartition(' cgroup2 ')[0]
print(' avg10 avg60 avg300 avg10 avg60 avg300')
def get_psi_mem_files(cgroup2_path):
"""
"""
while True:
(some_avg10, some_avg60, some_avg300,
full_avg10, full_avg60, full_avg300) = psi_path_to_metrics(psi_path)
path_list = []
print('some {} {} {} | full {} {} {}'.format(
for root, dirs, files in os.walk(cgroup2_path):
for file in files:
path = os.path.join(root, file)
if path.endswith('/memory.pressure'): #############
path_list.append(path)
return path_list
def psi_path_to_cgroup2(path):
"""
"""
return path.partition(i)[2][:-16]
i = cgroup2_root()
print('cgroup2 root dir:', i)
if i is not None:
y = get_psi_mem_files(i)
for path in y:
pass # print(psi_path_to_cgroup2(path))
path_list = get_psi_mem_files(i)
print(' avg10 avg60 avg300 avg10 avg60 avg300 cgroup2')
print(' ----- ----- ------ ----- ----- ------ ---------')
(some_avg10, some_avg60, some_avg300, full_avg10, full_avg60, full_avg300) = psi_path_to_metrics('/proc/pressure/memory')
print('some {} {} {} | full {} {} {} {}'.format(
some_avg10.rjust(6),
some_avg60.rjust(6),
some_avg300.rjust(6),
full_avg10.rjust(6),
full_avg60.rjust(6),
full_avg300.rjust(6)))
full_avg300.rjust(6), '[SYSTEM]'))
for psi_path in path_list:
(some_avg10, some_avg60, some_avg300,
full_avg10, full_avg60, full_avg300) = psi_path_to_metrics(psi_path)
print('some {} {} {} | full {} {} {} {}'.format(
some_avg10.rjust(6),
some_avg60.rjust(6),
some_avg300.rjust(6),
full_avg10.rjust(6),
full_avg60.rjust(6),
full_avg300.rjust(6), psi_path_to_cgroup2(psi_path)))
print(time() - t0)
sleep(2)

View File

@ -1,117 +0,0 @@
#!/usr/bin/env python3
from ctypes import CDLL
from time import sleep
import os
"""
Execute the command
find /sys/fs/cgroup -name memory.pressure
to find available memory.pressue files (except /proc/pressure/memory).
(actual for cgroup2)
"""
psi_path = '/proc/pressure/memory'
def mlockall():
MCL_CURRENT = 1
MCL_FUTURE = 2
MCL_ONFAULT = 4
libc = CDLL('libc.so.6', use_errno=True)
result = libc.mlockall(
MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT
)
if result != 0:
result = libc.mlockall(
MCL_CURRENT | MCL_FUTURE
)
if result != 0:
print('WARNING: cannot lock all memory')
else:
pass
else:
pass
mlockall()
def psi_path_to_metrics(psi_path):
with open(psi_path) as f:
psi_list = f.readlines()
# print(psi_list)
some_list, full_list = psi_list[0].split(' '), psi_list[1].split(' ')
#print(some_list, full_list)
some_avg10 = some_list[1].split('=')[1]
some_avg60 = some_list[2].split('=')[1]
some_avg300 = some_list[3].split('=')[1]
full_avg10 = full_list[1].split('=')[1]
full_avg60 = full_list[1].split('=')[1]
full_avg300 = full_list[1].split('=')[1]
return (some_avg10, some_avg60, some_avg300,
full_avg10, full_avg60, full_avg300)
(some_avg10, some_avg60, some_avg300, full_avg10, full_avg60, full_avg300
) = psi_path_to_metrics(psi_path)
def cgroup2_root():
"""
"""
with open('/proc/mounts') as f:
for line in f:
if line.startswith('cgroup2 '):
return line[7:].rpartition(' cgroup2 ')[0]
i = cgroup2_root()
print(i)
def get_psi_mem_files(cgroup2_path):
"""
"""
path_list = []
for root, dirs, files in os.walk(cgroup2_path):
for file in files:
path = os.path.join(root, file)
if path.endswith('/memory.pressure'):
path_list.append(path)
return path_list
if i is not None:
print(get_psi_mem_files(i))