Skip to content

Commit

Permalink
Calculate vCPU count by using core per cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
lubaihua33 committed Feb 18, 2022
1 parent c36e493 commit 559a083
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
57 changes: 53 additions & 4 deletions lisa/tools/lscpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,16 @@ def __repr__(self) -> str:
class Lscpu(Tool):
# CPU(s): 16
__vcpu = re.compile(r"^CPU\(s\):[ ]+([\d]+)\r?$", re.M)
# Thread(s) per core: 1
__thread_per_core = re.compile(r"^Thread\(s\) per core:[ ]+([\d]+)\r?$", re.M)
# Core(s) per socket: 8
__core_per_socket = re.compile(r"^Core\(s\) per socket:[ ]+([\d]+)\r?$", re.M)
# Core(s) per cluster: 32
__core_per_cluster = re.compile(r"^Core\(s\) per cluster:[ ]+([\d]+)\r?$", re.M)
# Socket(s): 1
__sockets = re.compile(r"^Socket\(s\):[ ]+([\d]+)\r?$", re.M)
# Cluster(s): 1
__clusters = re.compile(r"^Cluster\(s\):[ ]+([\d]+)\r?$", re.M)
# Architecture: x86_64
__architecture_pattern = re.compile(r"^Architecture:\s+(.*)?\r$", re.M)
__valid_architecture_list = ["x86_64", "aarch64"]
Expand Down Expand Up @@ -103,7 +110,7 @@ def get_core_count(self, force_run: bool = False) -> int:
len(matched),
f"cpu count should have exact one line, but got {matched}",
).is_equal_to(1)
self._core_count = int(matched[0]) * 1
self._core_count = int(matched[0])

return self._core_count

Expand All @@ -115,7 +122,7 @@ def get_thread_per_core_count(self, force_run: bool = False) -> int:
f"thread per core count should have exact one line, but got {matched}",
).is_equal_to(1)

return int(matched[0]) * 1
return int(matched[0])

def get_core_per_socket_count(self, force_run: bool = False) -> int:
result = self.run(force_run=force_run)
Expand All @@ -125,7 +132,17 @@ def get_core_per_socket_count(self, force_run: bool = False) -> int:
f"core per socket count should have exact one line, but got {matched}",
).is_equal_to(1)

return int(matched[0]) * 1
return int(matched[0])

def get_core_per_cluster_count(self, force_run: bool = False) -> int:
result = self.run(force_run=force_run)
matched = self.__core_per_cluster.findall(result.stdout)
assert_that(
len(matched),
f"core per cluster count should have exact one line, but got {matched}",
).is_equal_to(1)

return int(matched[0])

def get_socket_count(self, force_run: bool = False) -> int:
result = self.run(force_run=force_run)
Expand All @@ -135,7 +152,39 @@ def get_socket_count(self, force_run: bool = False) -> int:
f"socket count should have exact one line, but got {matched}",
).is_equal_to(1)

return int(matched[0]) * 1
return int(matched[0])

def get_cluster_count(self, force_run: bool = False) -> int:
result = self.run(force_run=force_run)
matched = self.__clusters.findall(result.stdout)
assert_that(
len(matched),
f"cluster count should have exact one line, but got {matched}",
).is_equal_to(1)

return int(matched[0])

def calculate_vcpu_count(self, force_run: bool = False) -> int:
# The concept of a "cluster" of CPUs was recently added in the Linux
# 5.16 kernel in commit c5e22feffdd7. There is "Core(s) per cluster"
# and "Cluster(s)" in the output of lscpu. If there is cluster topology,
# calculate vCPU count by core_per_cluster_count * cluster_count *
# thread_per_core_count, else by core_per_socket_count * socket_count *
# thread_per_core_count.
result = self.run(force_run=force_run)
if "Core(s) per cluster" in result.stdout:
calculated_cpu_count = (
self.get_core_per_cluster_count()
* self.get_cluster_count()
* self.get_thread_per_core_count()
)
else:
calculated_cpu_count = (
self.get_core_per_socket_count()
* self.get_socket_count()
* self.get_thread_per_core_count()
)
return calculated_cpu_count

def get_cpu_type(self, force_run: bool = False) -> CpuType:
result = self.run(force_run=force_run)
Expand Down
11 changes: 3 additions & 8 deletions microsoft/testsuites/core/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,13 @@ def cpu_count_check(self, node: Node, log: Logger) -> None:
# 1. Get vCPU count.
cpu_count = lscpu.get_core_count()
log.debug(f"{cpu_count} CPU cores detected...")
# 2. Caculate vCPU count by core_per_socket_count * socket_count *
# thread_per_core_count.
caculated_cpu_count = (
lscpu.get_core_per_socket_count()
* lscpu.get_socket_count()
* lscpu.get_thread_per_core_count()
)
# 2. Calculate vCPU count
calculated_cpu_count = lscpu.calculate_vcpu_count()
# 3. Judge whether the actual vCPU count equals to expected value.
assert_that(cpu_count).described_as(
"The VM may end up being incorrectly configured on some Azure hosts,"
" it is a known host bug, please check the host version."
).is_equal_to(caculated_cpu_count)
).is_equal_to(calculated_cpu_count)

@TestCaseMetadata(
description="""
Expand Down

0 comments on commit 559a083

Please sign in to comment.