Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe setting of CPU Info #146

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Changes from 1 commit
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
53 changes: 32 additions & 21 deletions src/nnbench/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import itertools
import logging
import platform
import sys
from typing import Any, Callable, Iterator, Literal
Expand Down Expand Up @@ -164,29 +165,39 @@ def __call__(self) -> dict[str, Any]:
)

result: dict[str, Any] = dict()
logger = logging.getLogger(__name__)

def safe_set(key: str, value: Callable) -> None:
try:
result[key] = value()
except Exception as e:
logger.warning(f"Failed saving value for {key}: {e}")
result[key] = None

# first, the platform info.
result["architecture"] = platform.machine()
result["bitness"] = platform.architecture()[0]
result["processor"] = platform.processor()
result["system"] = platform.system()
result["system-version"] = platform.release()

freq_struct = psutil.cpu_freq()
freq_conversion = self.conversion_table[self.frequnit[0]]
# result is in MHz, so we convert to Hz and apply the conversion factor.
result["frequency"] = freq_struct.current * 1e6 / freq_conversion
result["frequency_unit"] = self.frequnit
result["min_frequency"] = freq_struct.min
result["max_frequency"] = freq_struct.max
result["num_cpus"] = psutil.cpu_count(logical=False)
result["num_logical_cpus"] = psutil.cpu_count()

mem_struct = psutil.virtual_memory()
mem_conversion = self.conversion_table[self.memunit[0]]
# result is in bytes, so no need for base conversion.
result["total_memory"] = mem_struct.total / mem_conversion
result["memory_unit"] = self.memunit
safe_set("architecture", platform.machine)
safe_set("bitness", lambda: platform.architecture()[0])
safe_set("processor", platform.processor)
safe_set("system", platform.system)
safe_set("system-version", platform.release)

safe_set(
"frequency",
# result is in MHz, so we convert to Hz and apply the conversion factor.
lambda: psutil.cpu_freq().current * 1e6 / self.conversion_table[self.frequnit[0]],
)
safe_set("frequency_unit", lambda: self.frequnit)
safe_set("min_frequency", lambda: psutil.cpu_freq().min)
safe_set("max_frequency", lambda: psutil.cpu_freq().max)
safe_set("num_cpus", lambda: psutil.cpu_count(logical=False))
safe_set("num_logical_cpus", psutil.cpu_count)

safe_set(
"total_memory",
# result is in bytes, so no need for base conversion.
lambda: psutil.virtual_memory().total / self.conversion_table[self.memunit[0]],
)
safe_set("memory_unit", lambda: self.memunit)
# TODO: Lacks CPU cache info, which requires a solution other than psutil.
return {self.key: result}

Expand Down
Loading