Commit 94b580d4 authored by Benjamin Peterson's avatar Benjamin Peterson

expose sched.h functions (closes #12655)

parent 4e4d5d2a
......@@ -2744,6 +2744,155 @@ used to determine the disposition of a process.
Availability: Unix.
Interface to the scheduler
--------------------------
These functions control how a process is allocated CPU time by the operating
system. They are only available on some Unix platforms. For more detailed
information, consult your Unix manpages.
.. versionadded:: 3.3
The following scheduling policies are exposed if they are a supported by the
operating system.
.. data:: SCHED_OTHER
The default scheduling policy.
.. data:: SCHED_BATCH
Scheduling policy for CPU-intensive processes that tries to preserve
interactivity on the rest of the computer.
.. data:: SCHED_IDLE
Scheduling policy for extremely low priority background tasks.
.. data:: SCHED_SPORADIC
Scheduling policy for sporadic server programs.
.. data:: SCHED_FIFO
A First In First Out scheduling policy.
.. data:: SCHED_RR
A round-robin scheduling policy.
.. data:: SCHED_RESET_ON_FORK
This flag can OR'ed with any other scheduling policy. When a process with
this flag set forks, its child's scheduling policy and priority are reset to
the default.
.. class:: sched_param(sched_priority)
This class represents tunable scheduling parameters used in
:func:`sched_setparam`, :func:`sched_setscheduler`, and
:func:`sched_getparam`. It is immutable.
At the moment, there is only one possible parameter:
.. attribute:: sched_priority
The scheduling priority for a scheduling policy.
.. function:: sched_get_priority_min(policy)
Get the minimum priority value for *policy*. *policy* is one of the
scheduling policy constants above.
.. function:: sched_get_priority_max(policy)
Get the maximum priority value for *policy*. *policy* is one of the
scheduling policy constants above.
.. function:: sched_setscheduler(pid, policy, param)
Set the scheduling policy for the process with PID *pid*. A *pid* of 0 means
the calling process. *policy* is one of the scheduling policy constants
above. *param* is a :class:`sched_param` instance.
.. function:: sched_getscheduler(pid)
Return the scheduling policy for the process with PID *pid*. A *pid* of 0
means the calling process. The result is one of the scheduling policy
constants above.
.. function:: sched_setparam(pid, param)
Set a scheduling parameters for the process with PID *pid*. A *pid* of 0 means
the calling process. *param* is a :class:`sched_param` instance.
.. function:: sched_getparam(pid)
Return the scheduling parameters as a :class:`sched_param` instance for the
process with PID *pid*. A *pid* of 0 means the calling process.
.. function:: sched_rr_get_interval(pid)
Return the round-robin quantum in seconds for the process with PID *pid*. A
*pid* of 0 means the calling process.
.. function:: sched_yield()
Voluntarily relinquish the CPU.
.. class:: cpu_set(ncpus)
:class:`cpu_set` represents a set of CPUs on which a process is eligible to
run. *ncpus* is the number of CPUs the set should describe. Methods on
:class:`cpu_set` allow CPUs to be add or removed.
:class:`cpu_set` supports the AND, OR, and XOR bitwise operations. For
example, given two cpu_sets, ``one`` and ``two``, ``one | two`` returns a
:class:`cpu_set` containing the cpus enabled both in ``one`` and ``two``.
.. method:: set(i)
Enable CPU *i*.
.. method:: clear(i)
Remove CPU *i*.
.. method:: isset(i)
Return ``True`` if CPU *i* is enabled in the set.
.. method:: count()
Return the number of enabled CPUs in the set.
.. method:: zero()
Clear the set completely.
.. function:: sched_setaffinity(pid, mask)
Restrict the process with PID *pid* to a set of CPUs. *mask* is a
:class:`cpu_set` instance.
.. function:: sched_getaffinity(pid, size)
Return the :class:`cpu_set` the process with PID *pid* is restricted to. The
result will contain *size* CPUs.
.. _os-path:
Miscellaneous System Information
......
......@@ -829,6 +829,138 @@ class PosixTester(unittest.TestCase):
finally:
posix.close(f)
requires_sched_h = unittest.skipUnless(hasattr(posix, 'sched_yield'),
"don't have scheduling support")
@requires_sched_h
def test_sched_yield(self):
# This has no error conditions (at least on Linux).
posix.sched_yield()
@requires_sched_h
def test_sched_priority(self):
# Round-robin usually has interesting priorities.
pol = posix.SCHED_RR
lo = posix.sched_get_priority_min(pol)
hi = posix.sched_get_priority_max(pol)
self.assertIsInstance(lo, int)
self.assertIsInstance(hi, int)
self.assertGreaterEqual(hi, lo)
self.assertRaises(OSError, posix.sched_get_priority_min, -23)
self.assertRaises(OSError, posix.sched_get_priority_max, -23)
@requires_sched_h
def test_get_and_set_scheduler_and_param(self):
possible_schedulers = [sched for name, sched in posix.__dict__.items()
if name.startswith("SCHED_")]
mine = posix.sched_getscheduler(0)
self.assertIn(mine, possible_schedulers)
try:
init = posix.sched_getscheduler(1)
except OSError as e:
if e.errno != errno.EPERM:
raise
else:
self.assertIn(init, possible_schedulers)
self.assertRaises(OSError, posix.sched_getscheduler, -1)
self.assertRaises(OSError, posix.sched_getparam, -1)
param = posix.sched_getparam(0)
self.assertIsInstance(param.sched_priority, int)
posix.sched_setscheduler(0, mine, param)
posix.sched_setparam(0, param)
self.assertRaises(OSError, posix.sched_setparam, -1, param)
self.assertRaises(OSError, posix.sched_setscheduler, -1, mine, param)
self.assertRaises(TypeError, posix.sched_setscheduler, 0, mine, None)
self.assertRaises(TypeError, posix.sched_setparam, 0, 43)
param = posix.sched_param(None)
self.assertRaises(TypeError, posix.sched_setparam, 0, param)
large = 214748364700
param = posix.sched_param(large)
self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
param = posix.sched_param(sched_priority=-large)
self.assertRaises(OverflowError, posix.sched_setparam, 0, param)
@requires_sched_h
def test_sched_rr_get_interval(self):
interval = posix.sched_rr_get_interval(0)
self.assertIsInstance(interval, float)
# Reasonable constraints, I think.
self.assertGreaterEqual(interval, 0.)
self.assertLess(interval, 1.)
@requires_sched_h
def test_sched_affinity(self):
mask = posix.sched_getaffinity(0, 1024)
self.assertGreaterEqual(mask.count(), 1)
self.assertIsInstance(mask, posix.cpu_set)
self.assertRaises(OSError, posix.sched_getaffinity, -1, 1024)
empty = posix.cpu_set(10)
posix.sched_setaffinity(0, mask)
self.assertRaises(OSError, posix.sched_setaffinity, 0, empty)
self.assertRaises(OSError, posix.sched_setaffinity, -1, mask)
@requires_sched_h
def test_cpu_set_basic(self):
s = posix.cpu_set(10)
self.assertEqual(len(s), 10)
self.assertEqual(s.count(), 0)
s.set(0)
s.set(9)
self.assertTrue(s.isset(0))
self.assertTrue(s.isset(9))
self.assertFalse(s.isset(5))
self.assertEqual(s.count(), 2)
s.clear(0)
self.assertFalse(s.isset(0))
self.assertEqual(s.count(), 1)
s.zero()
self.assertFalse(s.isset(0))
self.assertFalse(s.isset(9))
self.assertEqual(s.count(), 0)
self.assertRaises(ValueError, s.set, -1)
self.assertRaises(ValueError, s.set, 10)
self.assertRaises(ValueError, s.clear, -1)
self.assertRaises(ValueError, s.clear, 10)
self.assertRaises(ValueError, s.isset, -1)
self.assertRaises(ValueError, s.isset, 10)
@requires_sched_h
def test_cpu_set_cmp(self):
self.assertNotEqual(posix.cpu_set(11), posix.cpu_set(12))
l = posix.cpu_set(10)
r = posix.cpu_set(10)
self.assertEqual(l, r)
l.set(1)
self.assertNotEqual(l, r)
r.set(1)
self.assertEqual(l, r)
@requires_sched_h
def test_cpu_set_bitwise(self):
l = posix.cpu_set(5)
l.set(0)
l.set(1)
r = posix.cpu_set(5)
r.set(1)
r.set(2)
b = l & r
self.assertEqual(b.count(), 1)
self.assertTrue(b.isset(1))
b = l | r
self.assertEqual(b.count(), 3)
self.assertTrue(b.isset(0))
self.assertTrue(b.isset(1))
self.assertTrue(b.isset(2))
b = l ^ r
self.assertEqual(b.count(), 2)
self.assertTrue(b.isset(0))
self.assertFalse(b.isset(1))
self.assertTrue(b.isset(2))
b = l
b |= r
self.assertIs(b, l)
self.assertEqual(l.count(), 3)
class PosixGroupsTester(unittest.TestCase):
def setUp(self):
......@@ -864,7 +996,6 @@ class PosixGroupsTester(unittest.TestCase):
posix.setgroups(groups)
self.assertListEqual(groups, posix.getgroups())
def test_main():
try:
support.run_unittest(PosixTester, PosixGroupsTester)
......
......@@ -251,6 +251,11 @@ Core and Builtins
Library
-------
- Issue #12655: Expose functions from sched.h in the os module: sched_yield(),
sched_setscheduler(), sched_getscheduler(), sched_setparam(),
sched_get_min_priority(), sched_get_max_priority(), sched_rr_get_interval(),
sched_getaffinity(), sched_setaffinity().
- Issues #11104, #8688: Fix the behavior of distutils' sdist command with
manually-maintained MANIFEST files.
......
This diff is collapsed.
......@@ -6092,7 +6092,7 @@ fi
for ac_header in asm/types.h conio.h curses.h direct.h dlfcn.h errno.h \
fcntl.h grp.h \
ieeefp.h io.h langinfo.h libintl.h ncurses.h poll.h process.h pthread.h \
shadow.h signal.h stdint.h stropts.h termios.h \
sched.h shadow.h signal.h stdint.h stropts.h termios.h \
unistd.h utime.h \
sys/audioio.h sys/bsdtty.h sys/epoll.h sys/event.h sys/file.h sys/loadavg.h \
sys/lock.h sys/mkdev.h sys/modem.h \
......@@ -14418,8 +14418,8 @@ esac
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
# Files that config.status was made for.
config_files="`echo $ac_config_files`"
config_headers="`echo $ac_config_headers`"
config_files="$ac_config_files"
config_headers="$ac_config_headers"
_ACEOF
......
......@@ -1301,7 +1301,7 @@ AC_HEADER_STDC
AC_CHECK_HEADERS(asm/types.h conio.h curses.h direct.h dlfcn.h errno.h \
fcntl.h grp.h \
ieeefp.h io.h langinfo.h libintl.h ncurses.h poll.h process.h pthread.h \
shadow.h signal.h stdint.h stropts.h termios.h \
sched.h shadow.h signal.h stdint.h stropts.h termios.h \
unistd.h utime.h \
sys/audioio.h sys/bsdtty.h sys/epoll.h sys/event.h sys/file.h sys/loadavg.h \
sys/lock.h sys/mkdev.h sys/modem.h \
......
......@@ -650,6 +650,9 @@
/* Define to 1 if you have the `round' function. */
#undef HAVE_ROUND
/* Define to 1 if you have the <sched.h> header file. */
#undef HAVE_SCHED_H
/* Define to 1 if you have the `select' function. */
#undef HAVE_SELECT
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment