Blame SOURCES/tuna-Adapt-show_threads-cgroup-output-to-terminal-si.patch

4bc72c
From 9dfaafe278f6cccf6911cddef413dc59e87722e7 Mon Sep 17 00:00:00 2001
4bc72c
From: Leah Leshchinsky <lleshchi@redhat.com>
4bc72c
Date: Wed, 16 Nov 2022 10:38:10 -0500
4bc72c
Subject: [PATCH] tuna: Adapt show_threads cgroup output to terminal size
4bc72c
4bc72c
Passing the --cgroup flag for the --show_threads command currently displays
4bc72c
long cgroup strings on the thread output and decreases readability.
4bc72c
4bc72c
Adapt the show_threads output to account for output string and terminal
4bc72c
size, and format output accordingly to improve readability. Add
4bc72c
--spaced flag to --show_threads to print cgroups spacing in
4bc72c
between thread outputs.
4bc72c
4bc72c
Signed-off-by: Leah Leshchinsky <lleshchi@redhat.com>
4bc72c
4bc72c
---
4bc72c
target branch: getopt
4bc72c
4bc72c
Signed-off-by: Leah Leshchinsky <lleshchi@redhat.com>
4bc72c
4bc72c
diff --git a/docs/tuna.8 b/docs/tuna.8
4bc72c
index 3a06556..218ba14 100644
4bc72c
--- a/docs/tuna.8
4bc72c
+++ b/docs/tuna.8
4bc72c
@@ -80,6 +80,9 @@ Disable display of selected CPUs in \fB--gui\fR. Requires \fB-c\R.
4bc72c
 \fB\-G\fR, \fB\-\-cgroup\fR
4bc72c
 Display the processes with the type of cgroups they are in. Requires \fB-P\R.
4bc72c
 .TP
4bc72c
+\fB\-z\fR, \fB\-\-spaced\fR
4bc72c
+Display spaced view for cgroups. Requires \fB-G\R.
4bc72c
+.TP
4bc72c
 \fB\-K\fR, \fB\-\-no_kthreads\fR
4bc72c
 Operations will not affect kernel threads.
4bc72c
 .TP
4bc72c
diff --git a/tuna-cmd.py b/tuna-cmd.py
4bc72c
index 75b63da..54dc567 100755
4bc72c
--- a/tuna-cmd.py
4bc72c
+++ b/tuna-cmd.py
4bc72c
@@ -99,6 +99,8 @@ def usage():
4bc72c
     print(fmt % ('-g, --gui',                   _('Start the GUI')))
4bc72c
     print(fmt % ('-G, --cgroup',
4bc72c
                  _('Display the processes with the type of cgroups they are in')))
4bc72c
+    print(fmt % ('-z, --spaced',
4bc72c
+                "Display spaced view for cgroups"))
4bc72c
     print(fmt % ('-c, --cpus=' + _('CPU-LIST'), _('%(cpulist)s affected by commands') %
4bc72c
                  {"cpulist": _('CPU-LIST')}))
4bc72c
     print(fmt % ('-C, --affect_children',
4bc72c
@@ -249,7 +251,7 @@ def format_affinity(affinity):
4bc72c
     return ",".join(str(hex(a)) for a in procfs.hexbitmask(affinity, get_nr_cpus()))
4bc72c
 
4bc72c
 def ps_show_thread(pid, affect_children, ps, has_ctxt_switch_info, sock_inodes,
4bc72c
-                   sock_inode_re, cgroups):
4bc72c
+                   sock_inode_re, cgroups, columns=None, compact=True):
4bc72c
     global irqs
4bc72c
     try:
4bc72c
         affinity = format_affinity(os.sched_getaffinity(pid))
4bc72c
@@ -286,10 +288,20 @@ def ps_show_thread(pid, affect_children, ps, has_ctxt_switch_info, sock_inodes,
4bc72c
                                           nonvoluntary_ctxt_switches)
4bc72c
 
4bc72c
     # Indent affected children
4bc72c
-    print(" %-5d " % pid if affect_children else "  %-5d" % pid, end=' ')
4bc72c
-    print("%6s %5d %8s%s %15s %s" % (sched, rtprio, affinity,
4bc72c
-                                     ctxt_switch_info, cmd, users), end=' ')
4bc72c
-    print(" %9s" % cgout if cgroups else "")
4bc72c
+    s1 = " %-5d " % pid if affect_children else "  %-5d" % pid
4bc72c
+    print(s1, end=' ')
4bc72c
+    s2 = "%6s %5d %8s%s %15s     %s" % (sched, rtprio, affinity,
4bc72c
+                                     ctxt_switch_info, cmd, users)
4bc72c
+    print(s2, end=' ')
4bc72c
+
4bc72c
+    if cgroups:
4bc72c
+        length = int(columns) - len(s1 + " ") - len(s2 + " ")
4bc72c
+        if len(" %9s" % cgout) <= length:
4bc72c
+            print("%s" % cgout)
4bc72c
+        else:
4bc72c
+            print("\n %s" % cgout + ("" if compact else "\n"))
4bc72c
+    else:
4bc72c
+        print()
4bc72c
 
4bc72c
     if sock_inodes:
4bc72c
         ps_show_sockets(pid, ps, sock_inodes, sock_inode_re,
4bc72c
@@ -298,12 +310,12 @@ def ps_show_thread(pid, affect_children, ps, has_ctxt_switch_info, sock_inodes,
4bc72c
         for tid in list(ps[pid]["threads"].keys()):
4bc72c
             ps_show_thread(tid, False, ps[pid]["threads"],
4bc72c
                            has_ctxt_switch_info,
4bc72c
-                           sock_inodes, sock_inode_re, cgroups)
4bc72c
+                           sock_inodes, sock_inode_re, cgroups, columns, compact)
4bc72c
 
4bc72c
 
4bc72c
 def ps_show(ps, affect_children, thread_list, cpu_list,
4bc72c
             irq_list_numbers, show_uthreads, show_kthreads,
4bc72c
-            has_ctxt_switch_info, sock_inodes, sock_inode_re, cgroups):
4bc72c
+            has_ctxt_switch_info, sock_inodes, sock_inode_re, cgroups, compact):
4bc72c
 
4bc72c
     ps_list = []
4bc72c
     for pid in list(ps.keys()):
4bc72c
@@ -340,9 +352,14 @@ def ps_show(ps, affect_children, thread_list, cpu_list,
4bc72c
 
4bc72c
     ps_list.sort()
4bc72c
 
4bc72c
+    # Width of terminal in columns
4bc72c
+    columns = None
4bc72c
+    if cgroups:
4bc72c
+        _, columns = os.popen('stty size', 'r').read().split()
4bc72c
+
4bc72c
     for pid in ps_list:
4bc72c
         ps_show_thread(pid, affect_children, ps, has_ctxt_switch_info,
4bc72c
-                       sock_inodes, sock_inode_re, cgroups)
4bc72c
+                       sock_inodes, sock_inode_re, cgroups, columns, compact)
4bc72c
 
4bc72c
 
4bc72c
 def load_socktype(socktype, inodes):
4bc72c
@@ -363,7 +380,7 @@ def load_sockets():
4bc72c
 
4bc72c
 
4bc72c
 def do_ps(thread_list, cpu_list, irq_list, show_uthreads, show_kthreads,
4bc72c
-          affect_children, show_sockets, cgroups):
4bc72c
+          affect_children, show_sockets, cgroups, compact):
4bc72c
     ps = procfs.pidstats()
4bc72c
     if affect_children:
4bc72c
         ps.reload_threads()
4bc72c
@@ -380,7 +397,7 @@ def do_ps(thread_list, cpu_list, irq_list, show_uthreads, show_kthreads,
4bc72c
             ps_show_header(has_ctxt_switch_info, cgroups)
4bc72c
         ps_show(ps, affect_children, thread_list,
4bc72c
                 cpu_list, irq_list, show_uthreads, show_kthreads,
4bc72c
-                has_ctxt_switch_info, sock_inodes, sock_inode_re, cgroups)
4bc72c
+                has_ctxt_switch_info, sock_inodes, sock_inode_re, cgroups, compact)
4bc72c
     except IOError:
4bc72c
         # 'tuna -P | head' for instance
4bc72c
         pass
4bc72c
@@ -535,13 +552,13 @@ def main():
4bc72c
 
4bc72c
     i18n_init()
4bc72c
     try:
4bc72c
-        short = "a:c:dDCfgGhiIKlmNp:PQq:r:R:s:S:t:UvWxL:"
4bc72c
+        short = "a:c:dDCfgGzhiIKlmNp:PQq:r:R:s:S:t:UvWxL:"
4bc72c
         long = ["cpus=", "affect_children", "filter", "gui", "help",
4bc72c
                 "isolate", "include", "no_kthreads", "move", "nohz_full",
4bc72c
                 "show_sockets", "priority=", "show_threads",
4bc72c
                 "show_irqs", "irqs=",
4bc72c
                 "save=", "sockets=", "threads=", "no_uthreads",
4bc72c
-                "version", "what_is", "spread", "cgroup", "config_file_apply=",
4bc72c
+                "version", "what_is", "spread", "cgroup", "spaced", "config_file_apply=",
4bc72c
                 "config_file_list", "run=", "refresh=", "disable_perf", "logging=", "debug"]
4bc72c
         if have_inet_diag:
4bc72c
             short += "n"
4bc72c
@@ -556,6 +573,7 @@ def main():
4bc72c
     kthreads = True
4bc72c
     uthreads = True
4bc72c
     cgroups = False
4bc72c
+    compact = True
4bc72c
     cpu_list = None
4bc72c
     debug = False
4bc72c
     irq_list = None
4bc72c
@@ -623,6 +641,8 @@ def main():
4bc72c
             affect_children = True
4bc72c
         elif o in ("-G", "--cgroup"):
4bc72c
             cgroups = True
4bc72c
+        elif o in ("-z", "--spaced"):
4bc72c
+            compact = False
4bc72c
         elif o in ("-t", "--threads"):
4bc72c
             # The -t - will reset thread list
4bc72c
             if a == '-':
4bc72c
@@ -691,7 +711,7 @@ def main():
4bc72c
                 if thread_list_str or irq_list_str:
4bc72c
                     continue
4bc72c
             do_ps(thread_list, cpu_list, irq_list, uthreads,
4bc72c
-                  kthreads, affect_children, show_sockets, cgroups)
4bc72c
+                  kthreads, affect_children, show_sockets, cgroups, compact)
4bc72c
         elif o in ("-Q", "--show_irqs"):
4bc72c
             # If the user specified IRQ names that weren't
4bc72c
             # resolved to IRQs, don't show all IRQs.
4bc72c
-- 
4bc72c
2.38.1
4bc72c