From e61bed7fd686c126da21d2b821bdf2a7b925caaa Mon Sep 17 00:00:00 2001 From: Dave Pedu Date: Sun, 6 Dec 2015 15:36:58 -0800 Subject: [PATCH] remove unnecessary read --- pymonitor/monitors/procs.py | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/pymonitor/monitors/procs.py b/pymonitor/monitors/procs.py index a997c06..29d8fdc 100644 --- a/pymonitor/monitors/procs.py +++ b/pymonitor/monitors/procs.py @@ -2,8 +2,6 @@ from glob import glob import re KTHREADD_PID = 2 -PAT_UID = re.compile(r'Uid:\s+(?P[0-9]+)\s') -PAT_GID = re.compile(r'Gid:\s+(?P[0-9]+)\s') PAT_REMOVE_PROC_SPACES = re.compile(r'(\([^\)]+\))') def procs(): @@ -35,27 +33,20 @@ def procs(): for f in glob('/proc/[0-9]*/stat'): try: with open(f, "r") as statfile: - with open(f+"us", "r") as statusfile: - # Read stat info - stat = statfile.read().strip() - # Fix spaces in process names - stat = PAT_REMOVE_PROC_SPACES.sub("PROCNAME", stat) - stat = stat.split(" ") - - # Read uid/gid from status - status = statusfile.read() - - proc_uid = PAT_UID.findall(status) - proc_gid = PAT_GID.findall(status) - - proc_id = int(stat[0]) - proc_parent = int(stat[3]) - - if proc_parent == KTHREADD_PID: - num_kthreads+=1 - else: - num_procs+=1 - num_threads += int(stat[19]) + # Read stat info + stat = statfile.read().strip() + # Fix spaces in process names + stat = PAT_REMOVE_PROC_SPACES.sub("PROCNAME", stat) + stat = stat.split(" ") + + proc_id = int(stat[0]) + proc_parent = int(stat[3]) + + if proc_parent == KTHREADD_PID: + num_kthreads+=1 + else: + num_procs+=1 + num_threads += int(stat[19]) except Exception as e: print(e)