From d8c7f4725d5f3b88d5714019a2b338568c166632 Mon Sep 17 00:00:00 2001 From: Sasha Goldshtein Date: Thu, 27 Oct 2016 15:17:58 -0700 Subject: [PATCH] ustat: Replace \0 with spaces in cmdline The /proc/PID/cmdline file has \0 for spaces in the command line, and there may be trailing \0 characters as well. Replace them all with spaces. --- tools/ustat.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/ustat.py b/tools/ustat.py index 75e33f8..55e8493 100755 --- a/tools/ustat.py +++ b/tools/ustat.py @@ -55,7 +55,7 @@ class Probe(object): comm = open('/proc/%d/comm' % pid).read().strip() if comm in self.procnames: cmdline = open('/proc/%d/cmdline' % pid).read() - self.targets[pid] = cmdline + self.targets[pid] = cmdline.replace('\0', ' ') except IOError: continue # process may already have terminated @@ -220,8 +220,9 @@ class Tool(object): print() with open("/proc/loadavg") as stats: print("%-8s loadavg: %s" % (strftime("%H:%M:%S"), stats.read())) - print("%-6s %-16s %-10s %-6s %-10s %-8s %-8s %-10s" % ("PID", "CMDLINE", - "METHOD/s", "GC/s", "OBJNEW/s", "CLOAD/s", "EXCP/s", "THREAD/s")) + print("%-6s %-20s %-10s %-6s %-10s %-8s %-6s %-6s" % ( + "PID", "CMDLINE", "METHOD/s", "GC/s", "OBJNEW/s", + "CLOAD/s", "EXC/s", "THR/s")) line = 0 counts = {} @@ -235,8 +236,8 @@ class Tool(object): else: counts = sorted(counts.items(), key=lambda (k, _): k) for pid, stats in counts: - print("%-6s %-16s %-10d %-6d %-10d %-8d %-8d %-10d" % ( - pid, targets[pid][0:16], + print("%-6d %-20s %-10d %-6d %-10d %-8d %-6d %-6d" % ( + pid, targets[pid][:20], stats.get(Category.METHOD, 0) / self.args.interval, stats.get(Category.GC, 0) / self.args.interval, stats.get(Category.OBJNEW, 0) / self.args.interval, -- 2.7.4