Skip to content

Commit 3d582fd

Browse files
liyuying0000copybara-github
authored andcommitted
Skip CpuSchedulingMode when running on ARM as SMT is unsupported on ARM.
PiperOrigin-RevId: 739983300 Change-Id: I805665609f53853228f54c8d6f29e61dedf41666
1 parent 1d42c7a commit 3d582fd

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

fleetbench/parallel/cpu.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
import enum
1818
import math
1919
import os
20+
import subprocess
2021
from typing import Tuple
2122

2223
from absl import logging
2324
import psutil
2425

2526

27+
# TODO: Consider using `lscpu -p` to get CPU information.
2628
class CpuInfo:
2729
"""Class for reading and parsing CPU information from the cpuinfo file.
2830
@@ -316,3 +318,15 @@ def Utilization(cpus: list[int]) -> Tuple[float, dict[int, float], int]:
316318
usable_utilization,
317319
busy_cpus,
318320
)
321+
322+
323+
def GetCPUArch() -> str:
324+
"""Returns the CPU architecture."""
325+
try:
326+
cpu_arch = subprocess.run(
327+
["uname", "-m"], capture_output=True, text=True, check=True
328+
)
329+
return cpu_arch.stdout.strip()
330+
except subprocess.CalledProcessError:
331+
logging.warning("Could not determine machine architecture. Using x86.")
332+
return "x86_64"

fleetbench/parallel/cpu_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from fleetbench.parallel import cpu
2323

2424

25-
class CpuTest(absltest.TestCase):
25+
class CpuTest(parameterized.TestCase):
2626

2727
def test_Available(self):
2828
self.assertGreaterEqual(len(cpu.Available()), 1)
@@ -38,6 +38,23 @@ def test_Utilization(self, mock_cpu_percent):
3838
with self.assertRaises(ValueError):
3939
cpu.Utilization([])
4040

41+
@parameterized.named_parameters(
42+
("x86", "x86_64"),
43+
("arm", "aarch64"),
44+
)
45+
@mock.patch("subprocess.run")
46+
def test_get_cpu_arch_success(self, cpu_arch, mock_run):
47+
"""Test when subprocess.run succeeds."""
48+
mock_process = mock.Mock()
49+
mock_process.stdout = cpu_arch + "\n"
50+
mock_process.returncode = 0
51+
mock_run.return_value = mock_process
52+
53+
self.assertEqual(cpu.GetCPUArch(), cpu_arch)
54+
mock_run.assert_called_once_with(
55+
["uname", "-m"], capture_output=True, text=True, check=True
56+
)
57+
4158

4259
class SchedlingModeTest(parameterized.TestCase):
4360

fleetbench/parallel/parallel_bench.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,21 @@ def main(argv: Sequence[str]) -> None:
147147
shutil.rmtree(_TEMP_ROOT.value, ignore_errors=True)
148148
os.makedirs(_TEMP_ROOT.value, exist_ok=True)
149149

150-
scheduling_mode = cpu.CpuSchedulingMode(
151-
num_cpus=_NUM_CPUS.value,
152-
utilization=_UTILIZATION.value,
153-
hyperthreading_mode=_HYPERTHREADING_MODE.value,
154-
)
155-
cpus, target_utilization = scheduling_mode.SelectCPURangeAndSetUtilization()
150+
cpu_arch = cpu.GetCPUArch()
151+
if cpu_arch == "aarch64":
152+
logging.info("Running on ARM. SMT is unsupport.")
153+
cpus = list(cpu.Available())[: _NUM_CPUS.value]
154+
target_utilization = _UTILIZATION.value
155+
elif cpu_arch == "x86_64":
156+
logging.info("Running on x86. Adjusting CPU scheduling...")
157+
scheduling_mode = cpu.CpuSchedulingMode(
158+
num_cpus=_NUM_CPUS.value,
159+
utilization=_UTILIZATION.value,
160+
hyperthreading_mode=_HYPERTHREADING_MODE.value,
161+
)
162+
cpus, target_utilization = scheduling_mode.SelectCPURangeAndSetUtilization()
163+
else:
164+
raise ValueError(f"Unsupported CPU architecture: {cpu_arch}")
156165

157166
bench = parallel_bench_lib.ParallelBench(
158167
cpus=cpus,

0 commit comments

Comments
 (0)