Debian Codegraph @

tests/linux_kernel/helpers/test_nodemask.py

raw
1 # Copyright (c) ByteDance, Inc. and its affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 from pathlib import Path
5 import unittest
6
7 from drgn.helpers.linux.nodemask import for_each_node, for_each_online_node, node_state
8 from tests.linux_kernel import LinuxKernelTestCase, parse_range_list
9
10 NODE_PATH = Path("/sys/devices/system/node")
11
12
13 @unittest.skipUnless(NODE_PATH.exists(), "kernel does not support NUMA")
14 class TestNodeMask(LinuxKernelTestCase):
15 @staticmethod
16 def _parse_node_list(name):
17 return parse_range_list((NODE_PATH / name).read_text())
18
19 def _test_for_each_node(self, func, name):
20 self.assertEqual(list(func(self.prog)), sorted(self._parse_node_list(name)))
21
22 def test_for_each_node(self):
23 self._test_for_each_node(for_each_node, "possible")
24
25 def test_for_each_online_node(self):
26 self._test_for_each_node(for_each_online_node, "online")
27
28 def _test_node_state(self, state_name, file_name):
29 possible = self._parse_node_list("possible")
30 expected = self._parse_node_list(file_name)
31 state = self.prog[state_name]
32 for node in possible:
33 self.assertEqual(node_state(node, state), node in expected)
34
35 def test_node_state(self):
36 self._test_node_state("N_NORMAL_MEMORY", "has_normal_memory")
37 # N_GENERIC_INITIATOR was added in Linux kernel commit 894c26a1c274
38 # ("ACPI: Support Generic Initiator only domains") (in v5.10). Most of
39 # the time it is unset, so if it exists we can use it to test the unset
40 # case.
41 if (NODE_PATH / "has_generic_initiator").exists():
42 self._test_node_state("N_GENERIC_INITIATOR", "has_generic_initiator")