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

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