| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
import drgn.helpers.linux.cpumask |
| 7 |
from drgn.helpers.linux.cpumask import cpumask_of, cpumask_to_cpulist, for_each_cpu |
| 8 |
from tests.linux_kernel import LinuxKernelTestCase, parse_range_list, possible_cpus |
| 9 |
|
| 10 |
CPU_PATH = Path("/sys/devices/system/cpu") |
| 11 |
|
| 12 |
|
| 13 |
class TestCpuMask(LinuxKernelTestCase): |
| 14 |
_MASKS = ("online", "possible", "present") |
| 15 |
|
| 16 |
@classmethod |
| 17 |
def setUpClass(cls): |
| 18 |
super().setUpClass() |
| 19 |
for online_path in sorted(CPU_PATH.glob("cpu*/online")): |
| 20 |
if int(online_path.read_text()): |
| 21 |
cls.offlined_path = online_path |
| 22 |
online_path.write_text("0") |
| 23 |
cls.addClassCleanup(online_path.write_text, "1") |
| 24 |
break |
| 25 |
|
| 26 |
def test_for_each_cpu(self): |
| 27 |
for name in self._MASKS: |
| 28 |
with self.subTest(name=name): |
| 29 |
self.assertEqual( |
| 30 |
list( |
| 31 |
getattr(drgn.helpers.linux.cpumask, f"for_each_{name}_cpu")( |
| 32 |
self.prog |
| 33 |
) |
| 34 |
), |
| 35 |
sorted(parse_range_list((CPU_PATH / name).read_text())), |
| 36 |
) |
| 37 |
|
| 38 |
def test_cpumask_to_cpulist(self): |
| 39 |
for name in self._MASKS: |
| 40 |
with self.subTest(name=name): |
| 41 |
self.assertEqual( |
| 42 |
cpumask_to_cpulist( |
| 43 |
getattr(drgn.helpers.linux.cpumask, f"cpu_{name}_mask")( |
| 44 |
self.prog |
| 45 |
) |
| 46 |
), |
| 47 |
(CPU_PATH / name).read_text().strip(), |
| 48 |
) |
| 49 |
|
| 50 |
def test_cpumask_weight(self): |
| 51 |
for name in self._MASKS: |
| 52 |
with self.subTest(name=name): |
| 53 |
self.assertEqual( |
| 54 |
getattr(drgn.helpers.linux.cpumask, f"num_{name}_cpus")(self.prog), |
| 55 |
len(parse_range_list((CPU_PATH / name).read_text())), |
| 56 |
) |
| 57 |
|
| 58 |
def test_cpumask_of(self): |
| 59 |
cpu = max(possible_cpus()) |
| 60 |
self.assertEqual(list(for_each_cpu(cpumask_of(self.prog, cpu))), [cpu]) |