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
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
19 changes: 13 additions & 6 deletions src/nnbench/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,20 @@ def __call__(self) -> dict[str, Any]:
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
try:
# The CPU frequency is not available on some ARM devices
freq_struct = psutil.cpu_freq()
result["min_frequency"] = freq_struct.min
result["max_frequency"] = freq_struct.max
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
except RuntimeError:
result["frequency"] = 0
result["min_frequency"] = 0
result["max_frequency"] = 0

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()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_cpu_info_provider() -> None:
# the popular CPU architectures.
assert res["architecture"] != ""
assert res["system"] != ""
assert res["frequency"] > 0
assert res["frequency"] >= 0
assert res["num_cpus"] > 0
assert res["total_memory"] > 0

Expand Down