Skip to content

Commit

Permalink
Reuse the previous get_cpu_info function
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescAlted committed Dec 12, 2024
1 parent d00afa4 commit 24dfd0b
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/blosc2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from functools import lru_cache
from typing import TYPE_CHECKING

import cpuinfo
import numpy as np

import blosc2
Expand Down Expand Up @@ -1151,6 +1152,24 @@ def linux_cache_size(cache_level: int, default_size: int) -> int:
return cache_size


def _get_cpu_info():
cpu_info = cpuinfo.get_cpu_info()
# cpuinfo does not correctly retrieve the cache sizes for Apple Silicon, so do it manually
if platform.system() == "Darwin":
cpu_info["l1_data_cache_size"] = apple_silicon_cache_size(1)
cpu_info["l2_cache_size"] = apple_silicon_cache_size(2)
cpu_info["l3_cache_size"] = apple_silicon_cache_size(3)
# cpuinfo does not correctly retrieve the cache sizes for all CPUs on Linux, so ask the kernel
if platform.system() == "Linux":
l1_data_cache_size = cpu_info.get("l1_data_cache_size", 32 * 1024)
cpu_info["l1_data_cache_size"] = linux_cache_size(1, l1_data_cache_size)
l2_cache_size = cpu_info.get("l2_cache_size", 256 * 1024)
cpu_info["l2_cache_size"] = linux_cache_size(2, l2_cache_size)
l3_cache_size = cpu_info.get("l3_cache_size", 1024 * 1024)
cpu_info["l3_cache_size"] = linux_cache_size(3, l3_cache_size)
return cpu_info


def write_cached_cpu_info(cpu_info_dict: dict[str, any]) -> None:
with open(pathlib.Path.home() / ".blosc2-cpuinfo.json", "w") as f:
json.dump(cpu_info_dict, f, indent=4)
Expand All @@ -1170,11 +1189,7 @@ def get_cpu_info() -> dict:
if cached_info:
return cached_info

try:
import cpuinfo
except ImportError:
return {}
cpu_info_dict = cpuinfo.get_cpu_info()
cpu_info_dict = _get_cpu_info()
with contextlib.suppress(OSError):
# In case cpu info cannot be stored, will need to be recomputed in the next process
write_cached_cpu_info(cpu_info_dict)
Expand Down

0 comments on commit 24dfd0b

Please sign in to comment.