#!/usr/bin/env python3 from time import sleep, time import os from sys import stdout, stderr 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 = 0.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) / 100 print('PSI mem avg:', round(avg, 2)) ta0 = ta1 a0 = a1 stdout.flush() sleep(1)