Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8486987

Browse files
committed
Issue #12655: Instead of requiring a custom type, os.sched_getaffinity and
os.sched_setaffinity now use regular sets of integers to represent the CPUs a process is restricted to.
1 parent a0e3feb commit 8486987

4 files changed

Lines changed: 161 additions & 433 deletions

File tree

Doc/library/os.rst

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3141,47 +3141,17 @@ operating system.
31413141
Voluntarily relinquish the CPU.
31423142

31433143

3144-
.. class:: cpu_set(ncpus)
3145-
3146-
:class:`cpu_set` represents a set of CPUs on which a process is eligible to
3147-
run. *ncpus* is the number of CPUs the set should describe. Methods on
3148-
:class:`cpu_set` allow CPUs to be add or removed.
3149-
3150-
:class:`cpu_set` supports the AND, OR, and XOR bitwise operations. For
3151-
example, given two cpu_sets, ``one`` and ``two``, ``one | two`` returns a
3152-
:class:`cpu_set` containing the cpus enabled both in ``one`` and ``two``.
3153-
3154-
.. method:: set(i)
3155-
3156-
Enable CPU *i*.
3157-
3158-
.. method:: clear(i)
3159-
3160-
Remove CPU *i*.
3161-
3162-
.. method:: isset(i)
3163-
3164-
Return ``True`` if CPU *i* is enabled in the set.
3165-
3166-
.. method:: count()
3167-
3168-
Return the number of enabled CPUs in the set.
3169-
3170-
.. method:: zero()
3171-
3172-
Clear the set completely.
3173-
3174-
31753144
.. function:: sched_setaffinity(pid, mask)
31763145

3177-
Restrict the process with PID *pid* to a set of CPUs. *mask* is a
3178-
:class:`cpu_set` instance.
3146+
Restrict the process with PID *pid* (or the current process if zero) to a
3147+
set of CPUs. *mask* is an iterable of integers representing the set of
3148+
CPUs to which the process should be restricted.
31793149

31803150

3181-
.. function:: sched_getaffinity(pid, size)
3151+
.. function:: sched_getaffinity(pid)
31823152

3183-
Return the :class:`cpu_set` the process with PID *pid* is restricted to. The
3184-
result will contain *size* CPUs.
3153+
Return the set of CPUs the process with PID *pid* (or the current process
3154+
if zero) is restricted to.
31853155

31863156

31873157
.. _os-path:

Lib/test/test_posix.py

Lines changed: 21 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ def test_mkfifo_dir_fd(self):
855855

856856
requires_sched_h = unittest.skipUnless(hasattr(posix, 'sched_yield'),
857857
"don't have scheduling support")
858-
requires_sched_affinity = unittest.skipUnless(hasattr(posix, 'cpu_set'),
858+
requires_sched_affinity = unittest.skipUnless(hasattr(posix, 'sched_setaffinity'),
859859
"don't have sched affinity support")
860860

861861
@requires_sched_h
@@ -936,83 +936,28 @@ def test_sched_rr_get_interval(self):
936936
self.assertLess(interval, 1.)
937937

938938
@requires_sched_affinity
939-
def test_sched_affinity(self):
940-
mask = posix.sched_getaffinity(0, 1024)
941-
self.assertGreaterEqual(mask.count(), 1)
942-
self.assertIsInstance(mask, posix.cpu_set)
943-
self.assertRaises(OSError, posix.sched_getaffinity, -1, 1024)
944-
empty = posix.cpu_set(10)
945-
posix.sched_setaffinity(0, mask)
946-
self.assertRaises(OSError, posix.sched_setaffinity, 0, empty)
947-
self.assertRaises(OSError, posix.sched_setaffinity, -1, mask)
948-
949-
@requires_sched_affinity
950-
def test_cpu_set_basic(self):
951-
s = posix.cpu_set(10)
952-
self.assertEqual(len(s), 10)
953-
self.assertEqual(s.count(), 0)
954-
s.set(0)
955-
s.set(9)
956-
self.assertTrue(s.isset(0))
957-
self.assertTrue(s.isset(9))
958-
self.assertFalse(s.isset(5))
959-
self.assertEqual(s.count(), 2)
960-
s.clear(0)
961-
self.assertFalse(s.isset(0))
962-
self.assertEqual(s.count(), 1)
963-
s.zero()
964-
self.assertFalse(s.isset(0))
965-
self.assertFalse(s.isset(9))
966-
self.assertEqual(s.count(), 0)
967-
self.assertRaises(ValueError, s.set, -1)
968-
self.assertRaises(ValueError, s.set, 10)
969-
self.assertRaises(ValueError, s.clear, -1)
970-
self.assertRaises(ValueError, s.clear, 10)
971-
self.assertRaises(ValueError, s.isset, -1)
972-
self.assertRaises(ValueError, s.isset, 10)
973-
974-
@requires_sched_affinity
975-
def test_cpu_set_cmp(self):
976-
self.assertNotEqual(posix.cpu_set(11), posix.cpu_set(12))
977-
l = posix.cpu_set(10)
978-
r = posix.cpu_set(10)
979-
self.assertEqual(l, r)
980-
l.set(1)
981-
self.assertNotEqual(l, r)
982-
r.set(1)
983-
self.assertEqual(l, r)
984-
985-
@requires_sched_affinity
986-
def test_cpu_set_bitwise(self):
987-
l = posix.cpu_set(5)
988-
l.set(0)
989-
l.set(1)
990-
r = posix.cpu_set(5)
991-
r.set(1)
992-
r.set(2)
993-
b = l & r
994-
self.assertEqual(b.count(), 1)
995-
self.assertTrue(b.isset(1))
996-
b = l | r
997-
self.assertEqual(b.count(), 3)
998-
self.assertTrue(b.isset(0))
999-
self.assertTrue(b.isset(1))
1000-
self.assertTrue(b.isset(2))
1001-
b = l ^ r
1002-
self.assertEqual(b.count(), 2)
1003-
self.assertTrue(b.isset(0))
1004-
self.assertFalse(b.isset(1))
1005-
self.assertTrue(b.isset(2))
1006-
b = l
1007-
b |= r
1008-
self.assertIs(b, l)
1009-
self.assertEqual(l.count(), 3)
939+
def test_sched_getaffinity(self):
940+
mask = posix.sched_getaffinity(0)
941+
self.assertIsInstance(mask, set)
942+
self.assertGreaterEqual(len(mask), 1)
943+
self.assertRaises(OSError, posix.sched_getaffinity, -1)
944+
for cpu in mask:
945+
self.assertIsInstance(cpu, int)
946+
self.assertGreaterEqual(cpu, 0)
947+
self.assertLess(cpu, 1 << 32)
1010948

1011949
@requires_sched_affinity
1012-
@support.cpython_only
1013-
def test_cpu_set_sizeof(self):
1014-
self.assertGreater(sys.getsizeof(posix.cpu_set(1000)),
1015-
sys.getsizeof(posix.cpu_set(1)))
950+
def test_sched_setaffinity(self):
951+
mask = posix.sched_getaffinity(0)
952+
if len(mask) > 1:
953+
# Empty masks are forbidden
954+
mask.pop()
955+
posix.sched_setaffinity(0, mask)
956+
self.assertEqual(posix.sched_getaffinity(0), mask)
957+
self.assertRaises(OSError, posix.sched_setaffinity, 0, [])
958+
self.assertRaises(ValueError, posix.sched_setaffinity, 0, [-10])
959+
self.assertRaises(OverflowError, posix.sched_setaffinity, 0, [1<<128])
960+
self.assertRaises(OSError, posix.sched_setaffinity, -1, mask)
1016961

1017962
def test_rtld_constants(self):
1018963
# check presence of major RTLD_* constants

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ Core and Builtins
7777
Library
7878
-------
7979

80+
- Issue #12655: Instead of requiring a custom type, os.sched_getaffinity and
81+
os.sched_setaffinity now use regular sets of integers to represent the CPUs
82+
a process is restricted to.
83+
8084
- Issue #15538: Fix compilation of the getnameinfo() / getaddrinfo()
8185
emulation code. Patch by Philipp Hagemeister.
8286

0 commit comments

Comments
 (0)