Post by Lumir BalharHello.
I've ported another Tuna/Tuned dependency to Python 3 compatible form -
python-schedutils. I've tested it in epel 6 and fedora 28 mock and
everything looks good to me.
https://github.com/frenzymadness/python-schedutils/tree/python3
https://copr.fedorainfracloud.org/coprs/g/realtime/testing/build/660503/
Let me know if I can provide something more.
Have a nice day.
Lumír
From 50fd6a695875c16d5d3e6e9e03d901bf58af535c Mon Sep 17 00:00:00 2001
Date: Fri, 10 Nov 2017 09:30:59 +0100
Subject: [PATCH 5/6] Use builtin macro for returning None
---
python-schedutils/schedutils.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/python-schedutils/schedutils.c b/python-schedutils/schedutils.c
index 79fb0bd..a82b878 100644
--- a/python-schedutils/schedutils.c
+++ b/python-schedutils/schedutils.c
@@ -220,8 +220,7 @@ static PyObject *set_scheduler(PyObject *self __unused, PyObject *args)
if (sched_setscheduler(pid, policy, ¶m) < 0)
return PyErr_SetFromErrno(PyExc_OSError);
- Py_INCREF(Py_None);
- return Py_None;
+ Py_RETURN_NONE;
}
static PyObject *get_priority(PyObject *self __unused, PyObject *args)
--
2.13.6
From 328e4b51e3aca4852cd95e269662bf40f68310ec Mon Sep 17 00:00:00 2001
Date: Fri, 10 Nov 2017 09:30:29 +0100
Subject: [PATCH 4/6] python3: Make schedutils Python 3 compatible
---
python-schedutils/schedutils.c | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)
diff --git a/python-schedutils/schedutils.c b/python-schedutils/schedutils.c
index be38e18..79fb0bd 100644
--- a/python-schedutils/schedutils.c
+++ b/python-schedutils/schedutils.c
@@ -1,4 +1,5 @@
#include <Python.h>
+#include "py3compat.h"
#include <sched.h>
#include <errno.h>
#include <syscall.h>
@@ -266,7 +267,7 @@ static PyObject *schedstr(PyObject *self __unused, PyObject *args)
default: s = "UNKNOWN"; break;
}
- return PyString_FromString(s);
+ return PyStr_FromString(s);
}
static PyObject *schedfromstr(PyObject *self __unused, PyObject *args)
@@ -378,11 +379,19 @@ static struct PyMethodDef PySchedutilsModuleMethods[] = {
{ .ml_name = NULL, },
};
-PyMODINIT_FUNC initschedutils(void)
+static struct PyModuleDef moduledef = {
+ PyModuleDef_HEAD_INIT,
+ .m_name = "schedutils",
+ .m_doc = NULL,
+ .m_size = -1,
+ .m_methods = PySchedutilsModuleMethods,
+};
+
+MODULE_INIT_FUNC(schedutils)
{
- PyObject *m = Py_InitModule("schedutils", PySchedutilsModuleMethods);
+ PyObject *m = PyModule_Create(&moduledef);
if (m == NULL)
- return;
+ return NULL;
PyModule_AddIntConstant(m, "SCHED_OTHER", SCHED_OTHER);
PyModule_AddIntConstant(m, "SCHED_FIFO", SCHED_FIFO);
@@ -391,5 +400,7 @@ PyMODINIT_FUNC initschedutils(void)
PyModule_AddIntConstant(m, "SCHED_IDLE", SCHED_IDLE);
PyModule_AddIntConstant(m, "SCHED_DEADLINE", SCHED_DEADLINE);
PyModule_AddIntConstant(m, "SCHED_RESET_ON_FORK", SCHED_RESET_ON_FORK);
+
+ return m;
}
--
2.13.6
From 0963592cbfa1518757261bded13178aa7c49f450 Mon Sep 17 00:00:00 2001
Date: Fri, 10 Nov 2017 09:27:30 +0100
Subject: [PATCH 3/6] python3: Add compatibility header file with necessary
macros
---
python-schedutils/py3compat.h | 60 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+)
create mode 100644 python-schedutils/py3compat.h
diff --git a/python-schedutils/py3compat.h b/python-schedutils/py3compat.h
new file mode 100644
index 0000000..625cddf
--- /dev/null
+++ b/python-schedutils/py3compat.h
@@ -0,0 +1,60 @@
+/*
+ Python 3 compatibility macros
+*/
+
+#include <Python.h>
+
+#if PY_MAJOR_VERSION >= 3
+
+/***** Python 3 *****/
+
+#define IS_PY3 1
+
+/* Ints */
+
+#define PyInt_AsLong PyLong_AsLong
+
+/* Strings */
+
+#define PyStr_FromString PyUnicode_FromString
+
+/* Module init */
+
+#define MODULE_INIT_FUNC(name) \
+ PyMODINIT_FUNC PyInit_ ## name(void); \
+ PyMODINIT_FUNC PyInit_ ## name(void)
+
+#else
+
+/***** Python 2 *****/
+
+#define IS_PY3 0
+
+/* Strings */
+
+#define PyStr_FromString PyString_FromString
+
+/* Module init */
+
+#define PyModuleDef_HEAD_INIT 0
+
+typedef struct PyModuleDef {
+ int m_base;
+ const char* m_name;
+ const char* m_doc;
+ Py_ssize_t m_size;
+ PyMethodDef *m_methods;
+} PyModuleDef;
+
+#define PyModule_Create(def) \
+ Py_InitModule3((def)->m_name, (def)->m_methods, (def)->m_doc)
+
+#define MODULE_INIT_FUNC(name) \
+ static PyObject *PyInit_ ## name(void); \
+ void init ## name(void); \
+ void init ## name(void) { PyInit_ ## name(); } \
+ static PyObject *PyInit_ ## name(void)
+
+
+#endif
--
2.13.6
From df30c2455d3ea1138e8b57d07d59d69eb0c1ab2e Mon Sep 17 00:00:00 2001
Date: Thu, 9 Nov 2017 14:48:04 +0100
Subject: [PATCH 2/6] python3: Fix incompatible prints and exceptions
---
pchrt.py | 22 ++++++++++++----------
ptaskset.py | 15 ++++++++-------
2 files changed, 20 insertions(+), 17 deletions(-)
diff --git a/pchrt.py b/pchrt.py
index c590b37..ddb1265 100755
--- a/pchrt.py
+++ b/pchrt.py
@@ -14,13 +14,14 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
+from __future__ import print_function
import os
import schedutils
import sys
- print '''pchrt (python-schedutils)
+ print('''pchrt (python-schedutils)
usage: pchrt [options] [prio] [pid | cmd [args...]]
manipulate real-time attributes of a process
-b, --batch set policy to SCHED_BATCH
@@ -35,16 +36,16 @@ manipulate real-time attributes of a process
You must give a priority if changing policy.
return
- print "%-32.32s: %d/%d" % (
+ print("%-32.32s: %d/%d" % (
"%s min/max priority" % schedutils.schedstr(policy),
schedutils.get_priority_min(policy),
schedutils.get_priority_max(policy)
- )
+ ))
reset_on_fork = ""
reset_on_fork = "|SCHED_RESET_ON_FORK"
- print '''pid %d's current scheduling policy: %s%s
-pid %d's current scheduling priority: %d''' % (pid, spolicy, reset_on_fork, pid, rtprio)
+ print('''pid %d's current scheduling policy: %s%s
+pid %d's current scheduling priority: %d''' % (pid, spolicy, reset_on_fork,
+ pid, rtprio))
if policy_flag == schedutils.SCHED_RESET_ON_FORK and \
- print "SCHED_RESET_ON_FORK flag is supported for SCHED_FIFO and SCHED_RR policies only"
+ print("SCHED_RESET_ON_FORK flag is supported for SCHED_FIFO and SCHED_RR policies only")
return False
return True
schedutils.set_scheduler(pid, policy | policy_flag, rtprio)
- print "sched_setscheduler: %s" % err[1]
- print "failed to set pid %d's policy" % pid
+ print("sched_setscheduler: %s" % err[1])
+ print("failed to set pid %d's policy" % pid)
diff --git a/ptaskset.py b/ptaskset.py
index de925a9..56fe7af 100755
--- a/ptaskset.py
+++ b/ptaskset.py
@@ -14,19 +14,20 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
+from __future__ import print_function
import os
import schedutils
import sys
- print '''ptaskset (python-schedutils)
+ print('''ptaskset (python-schedutils)
usage: ptaskset [options] [mask | cpu-list] [pid | cmd [args...]]
set or get the affinity of a process
-p, --pid operate on existing given pid
-c, --cpu-list display and specify cpus in list format
- -h, --help display this help'''
+ -h, --help display this help''')
return
raise "Syntax error"
- cpu_list += range(int(ends[0]), int(ends[1]) + 1)
+ cpu_list += list(range(int(ends[0]), int(ends[1]) + 1))
cpu_list += [int(ends[0])]
return list(set(cpu_list))
mask = ",".join([str(a) for a in affinity])
mask = ",".join(["%x" % a for a in hexbitmask(affinity)])
- print "pid %d's %s affinity mask: %s" % (pid, when, mask)
+ print("pid %d's %s affinity mask: %s" % (pid, when, mask))
schedutils.set_affinity(pid, affinity)
- print "sched_setaffinity: %s" % err[1]
- print "failed to set pid %d's affinity" % pid
+ print("sched_setaffinity: %s" % err[1])
+ print("failed to set pid %d's affinity" % pid)
--
2.13.6
From 96d8d005658077ee7a64e5728f0abddec93a8569 Mon Sep 17 00:00:00 2001
Date: Thu, 9 Nov 2017 14:32:45 +0100
Subject: [PATCH 1/6] Fix code style - spaces instead of tabs, indentaion,
imports, blank lines etc.
---
pchrt.py | 170 +++++++++++++++++++++-------------------
ptaskset.py | 253 +++++++++++++++++++++++++++++++-----------------------------
2 files changed, 223 insertions(+), 200 deletions(-)
diff --git a/pchrt.py b/pchrt.py
index 0761792..c590b37 100755
--- a/pchrt.py
+++ b/pchrt.py
@@ -14,10 +14,13 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
-import os, schedutils, sys
+import os
+import schedutils
+import sys
+
- print '''pchrt (python-schedutils)
+ print '''pchrt (python-schedutils)
usage: pchrt [options] [prio] [pid | cmd [args...]]
manipulate real-time attributes of a process
-b, --batch set policy to SCHED_BATCH
@@ -33,97 +36,106 @@ manipulate real-time attributes of a process
You must give a priority if changing policy.
- return
+ return
+
- print "%-32.32s: %d/%d" % ("%s min/max priority" % schedutils.schedstr(policy),
- schedutils.get_priority_min(policy),
- schedutils.get_priority_max(policy))
+ print "%-32.32s: %d/%d" % (
+ "%s min/max priority" % schedutils.schedstr(policy),
+ schedutils.get_priority_min(policy),
+ schedutils.get_priority_max(policy)
+ )
+
- for policy in (schedutils.SCHED_OTHER, schedutils.SCHED_FIFO,
- show_priority_limits(policy)
+ for policy in (schedutils.SCHED_OTHER, schedutils.SCHED_FIFO,
+ show_priority_limits(policy)
+
- policy = schedutils.get_scheduler(pid)
- spolicy = schedutils.schedstr(policy)
- rtprio = schedutils.get_priority(pid)
- reset_on_fork = ""
- reset_on_fork = "|SCHED_RESET_ON_FORK"
- print '''pid %d's current scheduling policy: %s%s
+ policy = schedutils.get_scheduler(pid)
+ spolicy = schedutils.schedstr(policy)
+ rtprio = schedutils.get_priority(pid)
+ reset_on_fork = ""
+ reset_on_fork = "|SCHED_RESET_ON_FORK"
+ print '''pid %d's current scheduling policy: %s%s
pid %d's current scheduling priority: %d''' % (pid, spolicy, reset_on_fork, pid, rtprio)
+
- if policy_flag == schedutils.SCHED_RESET_ON_FORK and \
- print "SCHED_RESET_ON_FORK flag is supported for SCHED_FIFO and SCHED_RR policies only"
- return False
- return True
+ if policy_flag == schedutils.SCHED_RESET_ON_FORK and \
+ print "SCHED_RESET_ON_FORK flag is supported for SCHED_FIFO and SCHED_RR policies only"
+ return False
+ return True
+
- schedutils.set_scheduler(pid, policy | policy_flag, rtprio)
- print "sched_setscheduler: %s" % err[1]
- print "failed to set pid %d's policy" % pid
+ schedutils.set_scheduler(pid, policy | policy_flag, rtprio)
+ print "sched_setscheduler: %s" % err[1]
+ print "failed to set pid %d's policy" % pid
+
- args = sys.argv[1:]
- usage()
- return
-
- policy = schedutils.SCHED_RR
- policy_flag = 0
- o = args.pop(0)
- priority = int(o)
- break
- pass
-
- usage()
- return
- policy = schedutils.SCHED_BATCH
- policy = schedutils.SCHED_FIFO
- policy = schedutils.SCHED_IDLE
- show_all_priority_limits()
- return
- policy = schedutils.SCHED_OTHER
- policy = schedutils.SCHED_RR
- policy_flag |= schedutils.SCHED_RESET_ON_FORK
- priority = int(args.pop(0))
- pid = int(args.pop(0))
- return
- change_settings(pid, policy, policy_flag, priority)
- pid = int(args.pop(0))
- show_settings(pid)
- return
- usage()
- return
-
- return
-
- schedutils.set_scheduler(0, policy | policy_flag, priority)
- os.execvp(args[0], args)
+ args = sys.argv[1:]
+ usage()
+ return
+
+ policy = schedutils.SCHED_RR
+ policy_flag = 0
+ o = args.pop(0)
+ priority = int(o)
+ break
+ pass
+
+ usage()
+ return
+ policy = schedutils.SCHED_BATCH
+ policy = schedutils.SCHED_FIFO
+ policy = schedutils.SCHED_IDLE
+ show_all_priority_limits()
+ return
+ policy = schedutils.SCHED_OTHER
+ policy = schedutils.SCHED_RR
+ policy_flag |= schedutils.SCHED_RESET_ON_FORK
+ priority = int(args.pop(0))
+ pid = int(args.pop(0))
+ return
+ change_settings(pid, policy, policy_flag, priority)
+ pid = int(args.pop(0))
+ show_settings(pid)
+ return
+ usage()
+ return
+
+ return
+
+ schedutils.set_scheduler(0, policy | policy_flag, priority)
+ os.execvp(args[0], args)
+
main()
diff --git a/ptaskset.py b/ptaskset.py
index afd1847..de925a9 100755
--- a/ptaskset.py
+++ b/ptaskset.py
@@ -14,10 +14,13 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
-import os, schedutils, sys
+import os
+import schedutils
+import sys
+
- print '''ptaskset (python-schedutils)
+ print '''ptaskset (python-schedutils)
usage: ptaskset [options] [mask | cpu-list] [pid | cmd [args...]]
set or get the affinity of a process
@@ -25,138 +28,146 @@ set or get the affinity of a process
-c, --cpu-list display and specify cpus in list format
-h, --help display this help'''
- return
+ return
+
- hexbitmask = []
- bit = 0
- mask = 0
- nr_entries = 1 << max(l)
- mask |= (1 << bit)
- bit += 1
- bit = 0
- hexbitmask.insert(0, mask)
- mask = 0
-
- hexbitmask.insert(0, mask)
-
- return hexbitmask
-
- bit = wordsize - 1
- return bit
- bit -= 1
- return 0
+ hexbitmask = []
+ bit = 0
+ mask = 0
+ nr_entries = 1 << max(l)
+ mask |= (1 << bit)
+ bit += 1
+ bit = 0
+ hexbitmask.insert(0, mask)
+ mask = 0
+
+ hexbitmask.insert(0, mask)
+
+ return hexbitmask
+
+
+ bit = wordsize - 1
+ return bit
+ bit -= 1
+ return 0
+
- fields = line.strip().split(",")
- bitmasklist = []
- fields.pop(0)
-
- return []
-
- nr_entries = (len(fields) - 1) * 32 + find_last_bit(int(fields[0], 16)) + 1
-
- entry = 0
- mask = int(fields[i], 16)
- bitmasklist.append(entry)
- mask >>= 1
- entry += 1
- break
- break
- return bitmasklist
+ fields = line.strip().split(",")
+ bitmasklist = []
+ fields.pop(0)
+
+ return []
+
+ nr_entries = (len(fields) - 1) * 32 + find_last_bit(int(fields[0], 16)) + 1
+
+ entry = 0
+ mask = int(fields[i], 16)
+ bitmasklist.append(entry)
+ mask >>= 1
+ entry += 1
+ break
+ break
+ return bitmasklist
+
- """Convert a string of numbers to an integer list.
-
- Given a string of comma-separated numbers and number ranges,
- return a simple sorted list of the integers it represents.
-
- This function will throw exceptions for badly-formatted strings.
-
- Returns a list of integers."""
-
- fields = cpustr.strip().split(",")
- cpu_list = []
- ends = field.split("-")
- raise "Syntax error"
- cpu_list += range(int(ends[0]), int(ends[1])+1)
- cpu_list += [int(ends[0])]
- return list(set(cpu_list))
+ """Convert a string of numbers to an integer list.
+
+ Given a string of comma-separated numbers and number ranges,
+ return a simple sorted list of the integers it represents.
+
+ This function will throw exceptions for badly-formatted strings.
+
+ Returns a list of integers."""
+
+ fields = cpustr.strip().split(",")
+ cpu_list = []
+ ends = field.split("-")
+ raise "Syntax error"
+ cpu_list += range(int(ends[0]), int(ends[1]) + 1)
+ cpu_list += [int(ends[0])]
+ return list(set(cpu_list))
+
- affinity = schedutils.get_affinity(pid)
- mask = ",".join([str(a) for a in affinity])
- mask = ",".join(["%x" % a for a in hexbitmask(affinity)])
- print "pid %d's %s affinity mask: %s" % (pid, when, mask)
+ affinity = schedutils.get_affinity(pid)
+ mask = ",".join([str(a) for a in affinity])
+ mask = ",".join(["%x" % a for a in hexbitmask(affinity)])
+ print "pid %d's %s affinity mask: %s" % (pid, when, mask)
+
- affinity = [ int(a) for a in affinity.split(",") ]
- affinity = cpustring_to_list(affinity)
- affinity = bitmasklist(affinity)
-
- schedutils.set_affinity(pid, affinity)
- print "sched_setaffinity: %s" % err[1]
- print "failed to set pid %d's affinity" % pid
+ affinity = [int(a) for a in affinity.split(",")]
+ affinity = cpustring_to_list(affinity)
+ affinity = bitmasklist(affinity)
+
+ schedutils.set_affinity(pid, affinity)
+ print "sched_setaffinity: %s" % err[1]
+ print "failed to set pid %d's affinity" % pid
+
- args = sys.argv[1:]
- usage()
- return
-
- cpu_list_mode = False
- o = args.pop(0)
-
- usage()
- return
- cpu_list_mode = True
- affinity = args.pop(0)
- pid = int(args.pop(0))
- show_settings(pid, "current", cpu_list_mode)
- change_settings(pid, affinity, cpu_list_mode)
- show_settings(pid, "new", cpu_list_mode)
- pid = int(args.pop(0))
- show_settings(pid, "current", cpu_list_mode)
- return
- break
-
- affinity = o
- change_settings(0, affinity, cpu_list_mode)
- os.execvp(args[0], args)
+ args = sys.argv[1:]
+ usage()
+ return
+
+ cpu_list_mode = False
+ o = args.pop(0)
+
+ usage()
+ return
+ cpu_list_mode = True
+ affinity = args.pop(0)
+ pid = int(args.pop(0))
+ show_settings(pid, "current", cpu_list_mode)
+ change_settings(pid, affinity, cpu_list_mode)
+ show_settings(pid, "new", cpu_list_mode)
+ pid = int(args.pop(0))
+ show_settings(pid, "current", cpu_list_mode)
+ return
+ break
+
+ affinity = o
+ change_settings(0, affinity, cpu_list_mode)
+ os.execvp(args[0], args)
+
main()
--
2.13.6
From 8204200377935a825d872e10f6a53d1ebdabf96a Mon Sep 17 00:00:00 2001
Date: Fri, 10 Nov 2017 11:40:59 +0100
Subject: [PATCH 6/6] python3: Provide necessary flag for function with no args
---
python-schedutils/schedutils.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/python-schedutils/schedutils.c b/python-schedutils/schedutils.c
index a82b878..304de0f 100644
--- a/python-schedutils/schedutils.c
+++ b/python-schedutils/schedutils.c
@@ -374,6 +374,7 @@ static struct PyMethodDef PySchedutilsModuleMethods[] = {
{
.ml_name = "get_max_number_of_cpus",
.ml_meth = (PyCFunction)get_max_number_of_cpus,
+ .ml_flags = METH_NOARGS,
},
{ .ml_name = NULL, },
};