#!/usr/bin/env python3 import getopt import time import errno import os import sys import subprocess import argparse g_verbose = False origRssFile = 0 class bcolors: HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' NORMAL = '\33[37m' def human(num_str, power="Ki", units=None): num = 0.0 try: num = float(num_str) except: return "0" if units is None: powers = ["Ki", "Mi", "Gi", "Ti"] if num >= 1000: #4 digits num /= 1024.0 power = powers[powers.index(power)+1] return "%.4f %sB" % (num, power) else: return "%.f" % ((num * 1024) / units) def runCmd(cmd): out = "-1" try: out = subprocess.check_output(cmd, shell=True) except: pass return out def NvMap(name="gst-launch-1.0"): if name == "all": cmd = "ps a" else: cmd = "ps a | grep %s | grep -v 'grep'" % name out = runCmd(cmd) if out == "-1": print("no this program, exit") sys.exit(-1) running_ps = [] for ps in out.decode().split("\n"): if sys.argv[0] in ps: continue else: if len(ps) > 0: running_ps.append(ps) for ps in running_ps: if g_verbose: print(bcolors.OKBLUE + ps + bcolors.ENDC) pid = ps.split()[0] VmSize = 0 node_path = "/proc/%s/status" % pid if os.path.exists(node_path): cmd = "cat " + node_path + " | grep VmSize:" out = runCmd(cmd) if out != "-1": res = [int(i) for i in out.split() if i.isdigit()] VmSize = res[0] VmRSS = 0 node_path = "/proc/%s/status" % pid if os.path.exists(node_path): cmd = "cat " + node_path + " | grep VmRSS:" out = runCmd(cmd) if out != "-1": res = [int(i) for i in out.split() if i.isdigit()] VmRSS = res[0] rssFile = 0 node_path = "/proc/%s/status" % pid if os.path.exists(node_path): cmd = "cat " + node_path + " | grep RssFile:" out = runCmd(cmd) if out != "-1": res = [int(i) for i in out.split() if i.isdigit()] rssFile = res[0] rssAnon = 0 node_path = "/proc/%s/status" % pid if os.path.exists(node_path): cmd = "cat " + node_path + " | grep RssAnon:" out = runCmd(cmd) if out != "-1": res = [int(i) for i in out.split() if i.isdigit()] rssAnon = res[0] total_iovmm = 0 cmd = "cat /proc/meminfo | grep NvMapMemUsed" out = runCmd(cmd) if out != "-1": total_iovmm = out.split()[-2] iovmm_path = "/sys/kernel/debug/nvmap/iovmm/clients" iovmm = 0 if os.path.exists(iovmm_path): cmd = "cat " + iovmm_path out = runCmd(cmd) iovmm = "0" if out != "-1": for cl in out.split(b"\n"): if pid in cl.split(): v = cl.split()[-1] iovmm = v[:-1] cmd = "lsof -p %s 2>/dev/null | wc -l"%pid out = runCmd(cmd) if out != "-1": lsof_out=out.split(b"\n")[0].decode("utf-8") global origRssFile if True: sys.stdout.write("PID: " + pid + " ") sys.stdout.write(time.strftime("%H:%M:%S", time.localtime()) + "\t") sys.stdout.write("Total used hardware memory: " + human(total_iovmm) + "\t") sys.stdout.write("hardware memory: " + human(iovmm) + "\t\t") sys.stdout.write("VmSize: " + human(VmSize) + "\t") sys.stdout.write("VmRSS: " + human(VmRSS) + "\t") sys.stdout.write("RssFile: " + human(rssFile) + "\t") sys.stdout.write("RssAnon: " + human(rssAnon) + "\t") sys.stdout.write("lsof: "+lsof_out+"\n") def main(): arg_parser = argparse.ArgumentParser(description="Memory usage of pragram") # Essential parameters arg_parser.add_argument("-p","--program", dest="program", nargs="*", default=["gst-launch-1.0"], help="Name of program/process, -p all to see memory usage forall running process") arg_parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=False, help=" show all logs") options = arg_parser.parse_args() global g_verbose g_verbose = options.verbose while True: for program in options.program: NvMap(program) time.sleep(1) if __name__ == '__main__': main()