On Wed, 9 Mar 2022, Leah Leshchinsky wrote:
Due to CLI redesign, only a single command can be executed at a
time.
Therefore, multiple functions and variables that supported multiaction
command line arguments are no longer used or necessary.
- Remove global variable update in get_nr_cpus()
- Remove global ps variable from thread_help()
- Remove global ps variable from thread_help()
- Remove global irqs variable from ps_show_thread()
- Remove get_nics() function and global nics variable
- Remove "too many globals" comment
- Remove unused functions do_list_op() and pick_op()
- Rework show_irqs() and remove globals
Signed-off-by: Leah Leshchinsky <lleshchi(a)redhat.com>
diff --git a/tuna-cmd.py b/tuna-cmd.py
index 97042e431ee6..2b8d75a7b150 100755
--- a/tuna-cmd.py
+++ b/tuna-cmd.py
@@ -37,11 +37,7 @@ try:
except:
have_inet_diag = False
-# FIXME: ETOOMANYGLOBALS, we need a class!
-
-nr_cpus = None
ps = None
-irqs = None
class HelpMessageParser(argparse.ArgumentParser):
def error(self, message):
@@ -168,30 +164,11 @@ def gen_parser():
return parser
-
def get_nr_cpus():
- """ Get all cpus including disabled cpus """
- global nr_cpus
- if nr_cpus:
- return nr_cpus
- nr_cpus = os.sysconf('SC_NPROCESSORS_CONF')
- return nr_cpus
-
-nics = None
-
-
-def get_nics():
- global nics
- if nics:
- return nics
- nics = ethtool.get_active_devices()
- return nics
-
+ return os.sysconf('SC_NPROCESSORS_CONF')
def thread_help(tid):
- global ps
- if not ps:
- ps = procfs.pidstats()
+ ps = procfs.pidstats()
if tid not in ps:
print("tuna: " + _("thread %d doesn't exists!") % tid)
@@ -265,7 +242,6 @@ def format_affinity(affinity):
def ps_show_thread(pid, affect_children, ps, has_ctxt_switch_info, sock_inodes,
sock_inode_re, cgroups):
- global irqs
try:
affinity = format_affinity(os.sched_getaffinity(pid))
except OSError as e:
@@ -280,18 +256,18 @@ def ps_show_thread(pid, affect_children, ps, has_ctxt_switch_info,
sock_inodes,
users = ""
if tuna.is_irq_thread(cmd):
try:
- if not irqs:
- irqs = procfs.interrupts()
+ irqs = procfs.interrupts()
+ nics = ethtool.get_active_devices()
if cmd[:4] == "IRQ-":
users = irqs[tuna.irq_thread_number(cmd)]["users"]
for u in users:
- if u in get_nics():
+ if u in nics:
users[users.index(u)] = "%s(%s)" % (
u, ethtool.get_module(u))
users = ",".join(users)
else:
u = cmd[cmd.find('-') + 1:]
- if u in get_nics():
+ if u in nics:
users = ethtool.get_module(u)
except:
users = "Not found in /proc/interrupts!"
@@ -407,7 +383,7 @@ def do_ps(thread_list, cpu_list, irq_list, show_uthreads,
show_kthreads,
def find_drivers_by_users(users):
- nics = get_nics()
+ nics = ethtool.get_active_devices()
drivers = []
for u in users:
try:
@@ -424,22 +400,21 @@ def find_drivers_by_users(users):
def show_irqs(irq_list, cpu_list):
- global irqs
- if not irqs:
- irqs = procfs.interrupts()
-
if sys.stdout.isatty():
print("%4s %-16s %8s" % ("#", _("users"),
_("affinity"),))
+
sorted_irqs = []
+ irqs = procfs.interrupts()
for k in list(irqs.keys()):
try:
irqn = int(k)
- affinity = irqs[irqn]["affinity"]
except:
continue
+
if irq_list and irqn not in irq_list:
continue
+ affinity = irqs[irqn]["affinity"]
if cpu_list and not set(cpu_list).intersection(set(affinity)):
continue
sorted_irqs.append(irqn)
@@ -448,22 +423,10 @@ def show_irqs(irq_list, cpu_list):
for irq in sorted_irqs:
affinity = format_affinity(irqs[irq]["affinity"])
users = irqs[irq]["users"]
- print("%4d %-16s %8s" % (irq, ",".join(users), affinity),
end=' ')
drivers = find_drivers_by_users(users)
- if drivers:
- print(" %s" % ",".join(drivers))
- else:
- print()
-
+ print("%4d %-16s %8s" % (irq, ",".join(users), affinity),
end=' ')
+ print(" %s" % ",".join(drivers) if drivers else
"")
-def do_list_op(op, current_list, op_list):
- if not current_list:
- current_list = []
- if op == '+':
- return list(set(current_list + op_list))
- if op == '-':
- return list(set(current_list) - set(op_list))
- return list(set(op_list))
def threadstring_to_list(threadstr):
global ps
@@ -496,14 +459,6 @@ def irqstring_to_list(irqstr):
return irq_list
-def pick_op(argument):
- if argument == "":
- return (None, argument)
- if argument[0] in ('+', '-'):
- return (argument[0], argument[1:])
- return (None, argument)
-
-
def i18n_init():
(app, localedir) = ('tuna', '/usr/share/locale')
locale.setlocale(locale.LC_ALL, '')
--
2.27.0
I think this needs to be thought out better, I would back this last patch
out for now.
The purpose of the globals isn't just to keep track of state with multiple
commands. The purpose of the globals here is to prevent expensive calls
from happening more than necessary.
So for example, we see the function ps_show_thread being called in a loop.
Now you have
+ irqs = procfs.interrupts()
+ nics = ethtool.get_active_devices()
We don't want to repeatedly call ethtool.get_active_devices() when it's
unlikely that this will change, this will slow down the program.
Same for procfs.interrupts cached in show_irqs
Thanks
John