Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions lisa/microsoft/testsuites/power/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Df,
Dmesg,
Fio,
Free,
HibernationSetup,
Hwclock,
Iperf3,
Expand Down Expand Up @@ -49,6 +50,60 @@ def is_distro_supported(node: Node) -> None:
)


def check_hibernation_disk_requirements(node: Node) -> None:
"""
Check if the VM has sufficient disk space for hibernation.
Hibernation requires disk space based on RAM size using the formula:
- 2 × RAM if RAM ≤ 8 GB
- 1.5 × RAM if 8 < RAM ≤ 64 GB
- RAM if 64 < RAM ≤ 256 GB
- Not supported if RAM > 256 GB
"""
free_tool = node.tools[Free]
df_tool = node.tools[Df]

# Get total memory in KB
total_memory_kb = free_tool._get_field_bytes_kib("Mem", "total")
total_memory_gb = total_memory_kb / (1024 * 1024) # Convert KB to GB

# Skip hibernation check for VMs with > 256 GB RAM
if total_memory_gb > 256:
raise SkippedException(
f"Hibernation is not supported for VMs with RAM > 256 GB. "
f"Current RAM: {total_memory_gb:.2f} GB"
)

# Calculate required swap space based on RAM size
if total_memory_gb <= 8:
required_space_gb = 2 * total_memory_gb
formula = "2*RAM"
elif total_memory_gb <= 64:
required_space_gb = 1.5 * total_memory_gb
formula = "1.5*RAM"
else:
required_space_gb = total_memory_gb
formula = "RAM"

# Add 20% buffer for filesystem overhead, defragmentation, and safety margin
required_space_with_buffer = required_space_gb * 1.2

root_partition = df_tool.get_partition_by_mountpoint("/", force_run=True)
assert root_partition is not None, "Unable to determine root partition disk space"

# available_blocks is in 1K blocks, convert to GB
available_space_gb = root_partition.available_blocks / 1024 / 1024

if available_space_gb < required_space_with_buffer:
raise LisaException(
f"Insufficient disk space for hibernation. "
f"Memory size: {total_memory_gb:.2f} GB, "
f"Available space: {available_space_gb:.2f} GB, "
f"Required space: {required_space_gb:.2f} GB ({formula}), "
f"Recommended (with 20% buffer): {required_space_with_buffer:.2f} GB. "
f"Please increase osdisk_size_in_gb to accommodate hibernation requirements."
)


def _prepare_hibernation_environment(node: Node) -> None:
"""
Prepare the hibernation environment by handling OS-specific requirements.
Expand Down Expand Up @@ -167,6 +222,11 @@ def verify_hibernation_by_tool(

hibernation_setup_tool = node.tools[HibernationSetup]

# Verify disk space again before hibernation setup to
# ensure sufficient space after any package installations
# or system changes
check_hibernation_disk_requirements(node)

# Get initial counts before hibernation
entry_before_hibernation = hibernation_setup_tool.check_entry()
exit_before_hibernation = hibernation_setup_tool.check_exit()
Expand Down
3 changes: 3 additions & 0 deletions lisa/microsoft/testsuites/power/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from assertpy import assert_that
from func_timeout import func_timeout
from microsoft.testsuites.power.common import (
check_hibernation_disk_requirements,
cleanup_env,
is_distro_supported,
run_network_workload,
Expand Down Expand Up @@ -46,6 +47,8 @@ def before_case(self, log: Logger, **kwargs: Any) -> None:
if isinstance(node.os, BSD) or isinstance(node.os, Windows):
raise SkippedException(f"{node.os} is not supported.")

check_hibernation_disk_requirements(node)

@TestCaseMetadata(
description="""
This case is to verify vm hibernation with synthetic network.
Expand Down
Loading