Debian Codegraph @

tests/linux_kernel/helpers/test_irq.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 from pathlib import Path
5 import unittest
6
7 from _drgn_util.platform import NORMALIZED_MACHINE_NAME
8 from drgn.helpers.linux.cpumask import for_each_cpu
9 from drgn.helpers.linux.irq import (
10 for_each_irq_desc,
11 gate_desc_func,
12 irq_desc_action_names,
13 irq_desc_affinity_mask,
14 irq_desc_chip_name,
15 irq_desc_kstat_cpu,
16 irq_to_desc,
17 )
18 from tests.linux_kernel import LinuxKernelTestCase, parse_range_list, possible_cpus
19
20
21 def proc_irq_smp_affinity_list(path):
22 try:
23 return (path / "smp_affinity_list").read_text()
24 except FileNotFoundError:
25 # The smp_affinity_list file doesn't exist on !SMP or for IRQs without
26 # a handler.
27 return Path("/sys/devices/system/cpu/online").read_text()
28
29
30 class TestIrq(LinuxKernelTestCase):
31 def test_for_each_irq_desc(self):
32 self.assertCountEqual(
33 [irq for irq, _ in for_each_irq_desc(self.prog)],
34 [int(path.name) for path in Path("/sys/kernel/irq").iterdir()],
35 )
36
37 def test_irq_desc_affinity(self):
38 for path in Path("/proc/irq").iterdir():
39 if not path.is_dir():
40 continue
41 irq = int(path.name)
42 self.assertCountEqual(
43 list(for_each_cpu(irq_desc_affinity_mask(irq_to_desc(self.prog, irq)))),
44 parse_range_list(proc_irq_smp_affinity_list(path)),
45 )
46 return
47 self.skipTest("IRQ not found")
48
49 def test_irq_desc_action_names(self):
50 # Try to test an interrupt with one action and one with multiple actions.
51 single = None
52 multiple = None
53 for path in Path("/sys/kernel/irq").iterdir():
54 expected = (path / "actions").read_bytes().rstrip(b"\n")
55 if not expected:
56 continue
57 irq = int(path.name)
58
59 if b"," in expected:
60 multiple = irq, expected
61 else:
62 single = irq, expected
63 if single is not None and multiple is not None:
64 break
65
66 with self.subTest("single"):
67 if single is None:
68 self.skipTest("IRQ with one action not found")
69 irq, expected = single
70 self.assertEqual(
71 b",".join(irq_desc_action_names(irq_to_desc(self.prog, irq))), expected
72 )
73
74 with self.subTest("multiple"):
75 if multiple is None:
76 self.skipTest("IRQ with multiple actions not found")
77 irq, expected = multiple
78 self.assertEqual(
79 b",".join(irq_desc_action_names(irq_to_desc(self.prog, irq))), expected
80 )
81
82 def test_irq_desc_chip_name(self):
83 for path in Path("/sys/kernel/irq").iterdir():
84 expected = (path / "chip_name").read_bytes().rstrip(b"\n")
85 if not expected:
86 continue
87 irq = int(path.name)
88 self.assertEqual(irq_desc_chip_name(irq_to_desc(self.prog, irq)), expected)
89 return
90 self.skipTest("IRQ with chip name not found")
91
92 def test_irq_desc_kstat_cpu(self):
93 cpu = min(possible_cpus())
94 for path in Path("/sys/kernel/irq").iterdir():
95 irq = int(path.name)
96 expected = int((path / "per_cpu_count").read_text().partition(",")[0])
97 # Prefer a non-zero count if we can find one.
98 if expected:
99 break
100 self.assertAlmostEqual(
101 irq_desc_kstat_cpu(irq_to_desc(self.prog, irq), cpu), expected, delta=1000
102 )
103
104 @unittest.skipUnless(NORMALIZED_MACHINE_NAME == "x86_64", "machine is not x86_64")
105 def test_gate_desc_func(self):
106 self.assertIn(
107 "int3", self.prog.symbol(gate_desc_func(self.prog["idt_table"][3])).name
108 )