Blob Blame History Raw
From 6e43ce523e7b403cc4f5e94c988d8811d2053e0f Mon Sep 17 00:00:00 2001
From: John Kacur <jkacur@redhat.com>
Date: Tue, 14 Dec 2021 14:59:50 -0500
Subject: [PATCH] tuna: Fix tuna displays incorrect cpu affinity when cpu is
 offlined

If we create a process and query it's affinity, both taskset and tuna
display the same result, but if we then disable a processor, tuna
displays the wrong affinity, as below.

[jkacur@fionn tuna]$ sha1sum /dev/zero &
[1] 10640
[jkacur@fionn tuna]$ taskset -p 10640
pid 10640's current affinity mask: fff
[jkacur@fionn tuna]$ su -c './tuna-cmd.py -t sha1sum -P'
Password:
                      thread       ctxt_switches
    pid SCHED_ rtpri affinity voluntary nonvoluntary             cmd
  10640  OTHER     0    0xfff         0          423         sha1sum
[jkacur@fionn tuna]$ su -c 'chcpu -d 5'
Password:
CPU 5 disabled
[jkacur@fionn tuna]$ taskset -p 10640
pid 10640's current affinity mask: fdf
[jkacur@fionn tuna]$ su -c './tuna-cmd.py -t sha1sum -P'
Password:
                      thread       ctxt_switches
    pid SCHED_ rtpri affinity voluntary nonvoluntary             cmd
  10640  OTHER     0    0x7df         0          919         sha1sum

The reason for this is that after tuna gets the affinity for the
process, when it converts this to a hex value, it passes the number of
enabled processors to hexbitmask in procfs, when what we really need is
the number of processors including disabled ones.

Fix this by querrying SC_NPROCESSORS_CONF and passing that value to
hexbitmask.

Signed-off-by: John Kacur <jkacur@redhat.com>
---
 tuna-cmd.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tuna-cmd.py b/tuna-cmd.py
index d209a2e89493..7e33a128d676 100755
--- a/tuna-cmd.py
+++ b/tuna-cmd.py
@@ -199,7 +199,9 @@ def format_affinity(affinity):
     if len(affinity) <= 4:
         return ",".join(str(a) for a in affinity)
 
-    return ",".join(str(hex(a)) for a in procfs.hexbitmask(affinity, get_nr_cpus()))
+    # We need the number of cpus on a system, including disabled ones
+    ncpus = os.sysconf('SC_NPROCESSORS_CONF')
+    return ",".join(str(hex(a)) for a in procfs.hexbitmask(affinity, ncpus))
 
 
 def ps_show_thread(pid, affect_children, ps, has_ctxt_switch_info, sock_inodes,
-- 
2.31.1