Discussion:
[PATCH 1/2] tuna: Use errno codes instead of numbers
John Kacur
2017-09-13 11:38:57 UTC
Permalink
Use errno codes instead of numbers since they
are self documenting.

Simplify the comments as a result

Signed-off-by: John Kacur <***@redhat.com>
---
tuna-cmd.py | 12 +++++-----
tuna/tuna.py | 78 ++++++++++++++++++++++++++++++------------------------------
2 files changed, 45 insertions(+), 45 deletions(-)

diff --git a/tuna-cmd.py b/tuna-cmd.py
index 39666c7c77f1..f0c5c0662ebd 100755
--- a/tuna-cmd.py
+++ b/tuna-cmd.py
@@ -14,7 +14,7 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

-import getopt, ethtool, fnmatch, os, procfs, re, schedutils, sys
+import getopt, ethtool, fnmatch, errno, os, procfs, re, schedutils, sys
from tuna import tuna, sysfs

import gettext
@@ -180,8 +180,8 @@ def ps_show_thread(pid, affect_children, ps,
global irqs
try:
affinity = format_affinity(schedutils.get_affinity(pid))
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
return
raise e

@@ -262,8 +262,8 @@ def ps_show(ps, affect_children, thread_list, cpu_list,
continue
try:
affinity = schedutils.get_affinity(pid)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e
if cpu_list and not set(cpu_list).intersection(set(affinity)):
@@ -558,7 +558,7 @@ def main():
else:
try:
tuna.threads_set_priority(thread_list, a, affect_children)
- except (SystemError, OSError) as err: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
+ except (SystemError, OSError) as err: # old python-schedutils incorrectly raised SystemError
print "tuna: %s" % err
sys.exit(2)
elif o in ("-P", "--show_threads"):
diff --git a/tuna/tuna.py b/tuna/tuna.py
index 9aab16a409d2..562c7fcf92d9 100755
--- a/tuna/tuna.py
+++ b/tuna/tuna.py
@@ -1,7 +1,7 @@
# -*- python -*-
# -*- coding: utf-8 -*-

-import copy, ethtool, os, procfs, re, schedutils, sys, shlex
+import copy, ethtool, errno, os, procfs, re, schedutils, sys, shlex
import help, fnmatch
from procfs import utilist

@@ -209,8 +209,8 @@ def move_threads_to_cpu(cpus, pid_list, set_affinity_warning = None,
try:
try:
curr_affinity = schedutils.get_affinity(pid)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3: # 'No such process'
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
curr_affinity = None
raise e
@@ -218,8 +218,8 @@ def move_threads_to_cpu(cpus, pid_list, set_affinity_warning = None,
try:
schedutils.set_affinity(pid, new_affinity)
curr_affinity = schedutils.get_affinity(pid)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3: # 'No such process'
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
curr_affinity == None
raise e
@@ -247,16 +247,16 @@ def move_threads_to_cpu(cpus, pid_list, set_affinity_warning = None,
for tid in threads.keys():
try:
curr_affinity = schedutils.get_affinity(tid)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e
if set(curr_affinity) != set(new_affinity):
try:
schedutils.set_affinity(tid, new_affinity)
curr_affinity = schedutils.get_affinity(tid)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e
if set(curr_affinity) == set(new_affinity):
@@ -267,12 +267,12 @@ def move_threads_to_cpu(cpus, pid_list, set_affinity_warning = None,
print "move_threads_to_cpu: %s " % \
(_("could not change %(pid)d affinity to %(new_affinity)s") % \
{'pid':pid, 'new_affinity':new_affinity})
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
# process died
continue
- elif e[0] == 22: # (22, EINVAL - unmovable thread)
- print "thread %(pid)d cannot be moved as requested" %{'pid':pid}
+ elif e[0] == errno.EINVAL: # unmovable thread)
+ print >> stderr, "thread %(pid)d cannot be moved as requested" %{'pid':pid}
continue
raise e
return changed
@@ -317,8 +317,8 @@ def move_irqs_to_cpu(cpus, irq_list, spread = False):
pid = int(pid[0])
try:
schedutils.set_affinity(pid, new_affinity)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
unprocessed.append(i)
changed -= 1
continue
@@ -351,8 +351,8 @@ def isolate_cpus(cpus, nr_cpus):
continue
try:
affinity = schedutils.get_affinity(pid)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e
if set(affinity).intersection(set(cpus)):
@@ -360,8 +360,8 @@ def isolate_cpus(cpus, nr_cpus):
affinity = affinity_remove_cpus(affinity, cpus, nr_cpus)
try:
schedutils.set_affinity(pid, affinity)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e

@@ -373,8 +373,8 @@ def isolate_cpus(cpus, nr_cpus):
continue
try:
affinity = schedutils.get_affinity(tid)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e
if set(affinity).intersection(set(cpus)):
@@ -382,8 +382,8 @@ def isolate_cpus(cpus, nr_cpus):
affinity = affinity_remove_cpus(affinity, cpus, nr_cpus)
try:
schedutils.set_affinity(tid, affinity)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e

@@ -419,8 +419,8 @@ def include_cpus(cpus, nr_cpus):
continue
try:
affinity = schedutils.get_affinity(pid)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e
if set(affinity).intersection(set(cpus)) != set(cpus):
@@ -428,8 +428,8 @@ def include_cpus(cpus, nr_cpus):
affinity = list(set(affinity + cpus))
try:
schedutils.set_affinity(pid, affinity)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e

@@ -441,8 +441,8 @@ def include_cpus(cpus, nr_cpus):
continue
try:
affinity = schedutils.get_affinity(tid)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e
if set(affinity).intersection(set(cpus)) != set(cpus):
@@ -450,8 +450,8 @@ def include_cpus(cpus, nr_cpus):
affinity = list(set(affinity + cpus))
try:
schedutils.set_affinity(tid, affinity)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e

@@ -518,8 +518,8 @@ def thread_filtered(tid, cpus_filtered, show_kthreads, show_uthreads):
if cpus_filtered:
try:
affinity = schedutils.get_affinity(tid)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
return False
raise e

@@ -557,8 +557,8 @@ def threads_set_priority(tids, parm, affect_children = False):
for tid in tids:
try:
thread_set_priority(tid, policy, rtprio)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e
if affect_children:
@@ -566,8 +566,8 @@ def threads_set_priority(tids, parm, affect_children = False):
if child != tid:
try:
thread_set_priority(child, policy, rtprio)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e

@@ -597,8 +597,8 @@ def get_kthread_sched_tunings(proc = None):
try:
policy = schedutils.get_scheduler(pid)
affinity = schedutils.get_affinity(pid)
- except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError
- if e[0] == 3:
+ except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
+ if e[0] == errno.ESRCH:
continue
raise e
percpu = iskthread(pid) and \
--
2.9.5
_______________________________________________
tuna-devel mailing list -- tuna-***@lists.fedorahosted.org
To unsubscribe send an email to tuna-devel-***@lists.fedorahos
John Kacur
2017-09-13 11:38:58 UTC
Permalink
isolate_cpus can create a traceback if passed an illegal cpuset
If this happens, exit with a message instead of a traceback

Signed-off-by: John Kacur <***@redhat.com>
---
tuna/tuna.py | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/tuna/tuna.py b/tuna/tuna.py
index 562c7fcf92d9..213bbf667caf 100755
--- a/tuna/tuna.py
+++ b/tuna/tuna.py
@@ -343,6 +343,7 @@ def parse_irq_affinity_filename(filename, nr_cpus):


def isolate_cpus(cpus, nr_cpus):
+ fname = sys._getframe( ).f_code.co_name # Function name
ps = procfs.pidstats()
ps.reload_threads()
previous_pid_affinities = {}
@@ -354,6 +355,9 @@ def isolate_cpus(cpus, nr_cpus):
except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
if e[0] == errno.ESRCH:
continue
+ elif e[0] == errno.EINVAL:
+ print >> sys.stderr, "Function:", fname, ",", e.strerror
+ sys.exit(2)
raise e
if set(affinity).intersection(set(cpus)):
previous_pid_affinities[pid] = copy.copy(affinity)
@@ -363,6 +367,9 @@ def isolate_cpus(cpus, nr_cpus):
except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
if e[0] == errno.ESRCH:
continue
+ elif e[0] == errno.EINVAL:
+ print >> sys.stderr, "Function:", fname, ",", e.strerror
+ sys.exit(2)
raise e

if not ps[pid].has_key("threads"):
@@ -376,6 +383,9 @@ def isolate_cpus(cpus, nr_cpus):
except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
if e[0] == errno.ESRCH:
continue
+ elif e[0] == errno.EINVAL:
+ print >> sys.stderr, "Function:", fname, ",", e.strerror
+ sys.exit(2)
raise e
if set(affinity).intersection(set(cpus)):
previous_pid_affinities[tid] = copy.copy(affinity)
@@ -385,6 +395,9 @@ def isolate_cpus(cpus, nr_cpus):
except (SystemError, OSError) as e: # old python-schedutils incorrectly raised SystemError
if e[0] == errno.ESRCH:
continue
+ elif e[0] == errno.EINVAL:
+ print >> sys.stderr, "Function:", fname, ",", e.strerror
+ sys.exit(2)
raise e

del ps
--
2.9.5
_______________________________________________
tuna-devel mailing list -- tuna-***@lists.fedorahosted.org
To unsubscribe send an email to tuna-devel-***@lists

Loading...