#!/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)